about summary refs log tree commit diff
path: root/converter/other/fiasco/lib/rpf.c
blob: e6ff6e0968e61b1393d494bd5473e956ee803b20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 *  rpf.c:      Conversion of float to reduced precision format values
 *
 *  Written by:     Stefan Frank
 *          Richard Krampfl
 *          Ullrich Hafner
 *      
 *  This file is part of FIASCO («F»ractal «I»mage «A»nd «S»equence «CO»dec)
 *  Copyright (C) 1994-2000 Ullrich Hafner <hafner@bigfoot.de>
 */

/*
 *  $Date: 2000/06/14 20:49:37 $
 *  $Author: hafner $
 *  $Revision: 5.1 $
 *  $State: Exp $
 */

#include "pm_config.h"
#include "config.h"
#include "mallocvar.h"

#include "types.h"
#include "macros.h"
#include "error.h"

#include "misc.h"
#include "rpf.h"

int const RPF_ZERO = -1;

/*****************************************************************************

                   private code
  
*****************************************************************************/


typedef struct {
    double fraction;
    int    exponent;
}  FracExp;



static FracExp
fracExpFromDouble(double const x) {

    FracExp retval;

    retval.fraction = frexp(x, &retval.exponent);

    return retval;
}



int
rtob (real_t        const f,
      const rpf_t * const rpfP)
/*
 *  Convert real number 'f' into fixed point format.
 *  The real number in [-'range'; +'range'] is scaled to [-1 ; +1].
 *  Sign and the first 'precision' - 1 bits of the mantissa are
 *  packed into one integer.  
 *
 *  Return value:
 *  real value in reduced precision format
 */
{  
    /*
     *  Extract mantissa (23 Bits), exponent (8 Bits) and sign (1 Bit)
     */

    double const normalized = f / rpfP->range;
        /* 'f' scaled to [-1,+1] */    
    FracExp const fracExp = fracExpFromDouble(normalized);
    unsigned int const signedMantissa =
        (unsigned int) (fracExp.fraction * (1<<23));

    unsigned int mantissa;
    unsigned int sign;  /* 0 for positive; 1 for negative */
    
    if (signedMantissa < 0) {
        mantissa = -signedMantissa;
        sign = 1;
    } else {
        mantissa = +signedMantissa;
        sign = 0;
    }

    /*
     *  Generate reduced precision mantissa.
     */
    if (fracExp.exponent > 0) 
        mantissa <<= fracExp.exponent;
    else
        mantissa >>= -fracExp.exponent;  
    
    mantissa >>= (23 - rpfP->mantissa_bits - 1);

    mantissa +=  1;          /* Round last bit. */
    mantissa >>= 1;
   
    if (mantissa == 0)           /* close to zero */
        return RPF_ZERO;
    else if (mantissa >= (1U << rpfP->mantissa_bits)) /* overflow */
        return sign;
    else
        return ((mantissa & ((1U << rpfP->mantissa_bits) - 1)) << 1) | sign;
}



float
btor (int           const binary,
      const rpf_t * const rpfP)
/*
 *  Convert value 'binary' in reduced precision format to a real value.
 *  For more information refer to function rtob() above.
 *
 *  Return value:
 *  converted value
 */
{
    unsigned int mantissa;
    float sign;
    float f;
 
    if (binary == RPF_ZERO)
        return 0;

    if (binary < 0 || binary >= 1 << (rpfP->mantissa_bits + 1))
        error ("Reduced precision format: value %d out of range.", binary);

    /*
     *  Restore IEEE float format:
     *  mantissa (23 Bits), exponent (8 Bits) and sign (1 Bit)
     */
   
    sign       = (binary & 0x1) == 0 ? 1.0 : -1.0;
    mantissa   = (binary & ((0x1 << (rpfP->mantissa_bits + 1)) - 1)) >> 1; 
    mantissa <<= (23 - rpfP->mantissa_bits);

    if (mantissa == 0) 
        f = sign;
    else
        f =  sign * (float) mantissa / 8388608;
   
    return f * rpfP->range;       /* expand [ -1 ; +1 ] to
                                     [ -range ; +range ] */
}




rpf_t *
alloc_rpf (unsigned           const mantissa,
           fiasco_rpf_range_e const range)
/*
 *  Reduced precision format constructor.
 *  Allocate memory for the rpf_t structure.
 *  Number of mantissa bits is given by `mantissa'.
 *  The range of the real values is in the interval [-`range', +`range'].
 *  In case of invalid parameters, a structure with default values is
 *  returned. 
 *
 *  Return value
 *  pointer to the new rpf structure
 */
{
    rpf_t * rpfP;

    MALLOCVAR(rpfP);
   
    if (mantissa < 2) {
        warning (_("Size of RPF mantissa has to be in the interval [2,8]. "
                   "Using minimum value 2.\n"));
        rpfP->mantissa_bits = 2;
    } else if (mantissa > 8) {
        warning (_("Size of RPF mantissa has to be in the interval [2,8]. "
                   "Using maximum value 8.\n"));
        rpfP->mantissa_bits = 2;
    } else
        rpfP->mantissa_bits = mantissa;

    switch (range) {
    case FIASCO_RPF_RANGE_0_75:
        rpfP->range   = 0.75;
        rpfP->range_e = range;
        break;
    case FIASCO_RPF_RANGE_1_50:
        rpfP->range   = 1.50;
        rpfP->range_e = range;
        break;
    case FIASCO_RPF_RANGE_2_00:
        rpfP->range   = 2.00;
        rpfP->range_e = range;
        break;
    case FIASCO_RPF_RANGE_1_00:
        rpfP->range   = 1.00;
        rpfP->range_e = range;
        break;
    default:
        warning (_("Invalid RPF range specified. Using default value 1.0."));
        rpfP->range   = 1.00;
        rpfP->range_e = FIASCO_RPF_RANGE_1_00;
        break;
    }
    return rpfP;
}