Subversion Repositories HelenOS-historic

Rev

Rev 876 | Rev 1035 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 876 Rev 1031
1
/*
1
/*
2
 * Copyright (C) 2005 Josef Cejka
2
 * Copyright (C) 2005 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include "sftypes.h"
29
#include "sftypes.h"
30
#include "conversion.h"
30
#include "conversion.h"
31
#include "comparison.h"
31
#include "comparison.h"
32
#include "common.h"
32
#include "common.h"
33
 
33
 
34
float64 convertFloat32ToFloat64(float32 a)
34
float64 convertFloat32ToFloat64(float32 a)
35
{
35
{
36
    float64 result;
36
    float64 result;
37
    __u64 frac;
37
    uint64_t frac;
38
   
38
   
39
    result.parts.sign = a.parts.sign;
39
    result.parts.sign = a.parts.sign;
40
    result.parts.fraction = a.parts.fraction;
40
    result.parts.fraction = a.parts.fraction;
41
    result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE );
41
    result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE );
42
   
42
   
43
    if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
43
    if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
44
        result.parts.exp = 0x7FF;
44
        result.parts.exp = 0x7FF;
45
        /* TODO; check if its correct for SigNaNs*/
45
        /* TODO; check if its correct for SigNaNs*/
46
        return result;
46
        return result;
47
    };
47
    };
48
   
48
   
49
    result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
49
    result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
50
    if (a.parts.exp == 0) {
50
    if (a.parts.exp == 0) {
51
        /* normalize denormalized numbers */
51
        /* normalize denormalized numbers */
52
 
52
 
53
        if (result.parts.fraction == 0ll) { /* fix zero */
53
        if (result.parts.fraction == 0ll) { /* fix zero */
54
            result.parts.exp = 0ll;
54
            result.parts.exp = 0ll;
55
            return result;
55
            return result;
56
        }
56
        }
57
           
57
           
58
        frac = result.parts.fraction;
58
        frac = result.parts.fraction;
59
       
59
       
60
        while (!(frac & (0x10000000000000ll))) {
60
        while (!(frac & (0x10000000000000ll))) {
61
            frac <<= 1;
61
            frac <<= 1;
62
            --result.parts.exp;
62
            --result.parts.exp;
63
        };
63
        };
64
       
64
       
65
        ++result.parts.exp;
65
        ++result.parts.exp;
66
        result.parts.fraction = frac;
66
        result.parts.fraction = frac;
67
    };
67
    };
68
   
68
   
69
    return result;
69
    return result;
70
   
70
   
71
}
71
}
72
 
72
 
73
float32 convertFloat64ToFloat32(float64 a)
73
float32 convertFloat64ToFloat32(float64 a)
74
{
74
{
75
    float32 result;
75
    float32 result;
76
    __s32 exp;
76
    int32_t exp;
77
    __u64 frac;
77
    uint64_t frac;
78
   
78
   
79
    result.parts.sign = a.parts.sign;
79
    result.parts.sign = a.parts.sign;
80
   
80
   
81
    if (isFloat64NaN(a)) {
81
    if (isFloat64NaN(a)) {
82
       
82
       
83
        result.parts.exp = 0xFF;
83
        result.parts.exp = 0xFF;
84
       
84
       
85
        if (isFloat64SigNaN(a)) {
85
        if (isFloat64SigNaN(a)) {
86
            result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */
86
            result.parts.fraction = 0x400000; /* set first bit of fraction nonzero */
87
            return result;
87
            return result;
88
        }
88
        }
89
   
89
   
90
        result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */
90
        result.parts.fraction = 0x1; /* fraction nonzero but its first bit is zero */
91
        return result;
91
        return result;
92
    };
92
    };
93
 
93
 
94
    if (isFloat64Infinity(a)) {
94
    if (isFloat64Infinity(a)) {
95
        result.parts.fraction = 0;
95
        result.parts.fraction = 0;
96
        result.parts.exp = 0xFF;
96
        result.parts.exp = 0xFF;
97
        return result;
97
        return result;
98
    };
98
    };
99
 
99
 
100
    exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
100
    exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
101
   
101
   
102
    if (exp >= 0xFF) {
102
    if (exp >= 0xFF) {
103
        /*FIXME: overflow*/
103
        /*FIXME: overflow*/
104
        result.parts.fraction = 0;
104
        result.parts.fraction = 0;
105
        result.parts.exp = 0xFF;
105
        result.parts.exp = 0xFF;
106
        return result;
106
        return result;
107
       
107
       
108
    } else if (exp <= 0 ) {
108
    } else if (exp <= 0 ) {
109
       
109
       
110
        /* underflow or denormalized */
110
        /* underflow or denormalized */
111
       
111
       
112
        result.parts.exp = 0;
112
        result.parts.exp = 0;
113
       
113
       
114
        exp *= -1; 
114
        exp *= -1; 
115
        if (exp > FLOAT32_FRACTION_SIZE ) {
115
        if (exp > FLOAT32_FRACTION_SIZE ) {
116
            /* FIXME: underflow */
116
            /* FIXME: underflow */
117
            result.parts.fraction = 0;
117
            result.parts.fraction = 0;
118
            return result;
118
            return result;
119
        };
119
        };
120
       
120
       
121
        /* denormalized */
121
        /* denormalized */
122
       
122
       
123
        frac = a.parts.fraction;
123
        frac = a.parts.fraction;
124
        frac |= 0x10000000000000ll; /* denormalize and set hidden bit */
124
        frac |= 0x10000000000000ll; /* denormalize and set hidden bit */
125
       
125
       
126
        frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
126
        frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
127
       
127
       
128
        while (exp > 0) {
128
        while (exp > 0) {
129
            --exp;
129
            --exp;
130
            frac >>= 1;
130
            frac >>= 1;
131
        };
131
        };
132
        result.parts.fraction = frac;
132
        result.parts.fraction = frac;
133
       
133
       
134
        return result;
134
        return result;
135
    };
135
    };
136
 
136
 
137
    result.parts.exp = exp;
137
    result.parts.exp = exp;
138
    result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
138
    result.parts.fraction = a.parts.fraction >> (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE);
139
    return result;
139
    return result;
140
}
140
}
141
 
141
 
142
 
142
 
143
/** Helping procedure for converting float32 to uint32
143
/** Helping procedure for converting float32 to uint32
144
 * @param a floating point number in normalized form (no NaNs or Inf are checked )
144
 * @param a floating point number in normalized form (no NaNs or Inf are checked )
145
 * @return unsigned integer
145
 * @return unsigned integer
146
 */
146
 */
147
static __u32 _float32_to_uint32_helper(float32 a)
147
static uint32_t _float32_to_uint32_helper(float32 a)
148
{
148
{
149
    __u32 frac;
149
    uint32_t frac;
150
   
150
   
151
    if (a.parts.exp < FLOAT32_BIAS) {
151
    if (a.parts.exp < FLOAT32_BIAS) {
152
        /*TODO: rounding*/
152
        /*TODO: rounding*/
153
        return 0;
153
        return 0;
154
    }
154
    }
155
   
155
   
156
    frac = a.parts.fraction;
156
    frac = a.parts.fraction;
157
   
157
   
158
    frac |= FLOAT32_HIDDEN_BIT_MASK;
158
    frac |= FLOAT32_HIDDEN_BIT_MASK;
159
    /* shift fraction to left so hidden bit will be the most significant bit */
159
    /* shift fraction to left so hidden bit will be the most significant bit */
160
    frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
160
    frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
161
 
161
 
162
    frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
162
    frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
163
    if ((a.parts.sign == 1) && (frac != 0)) {
163
    if ((a.parts.sign == 1) && (frac != 0)) {
164
        frac = ~frac;
164
        frac = ~frac;
165
        ++frac;
165
        ++frac;
166
    }
166
    }
167
   
167
   
168
    return frac;
168
    return frac;
169
}
169
}
170
 
170
 
171
/* Convert float to unsigned int32
171
/* Convert float to unsigned int32
172
 * FIXME: Im not sure what to return if overflow/underflow happens
172
 * FIXME: Im not sure what to return if overflow/underflow happens
173
 *  - now its the biggest or the smallest int
173
 *  - now its the biggest or the smallest int
174
 */
174
 */
175
__u32 float32_to_uint32(float32 a)
175
uint32_t float32_to_uint32(float32 a)
176
{
176
{
177
    if (isFloat32NaN(a)) {
177
    if (isFloat32NaN(a)) {
178
        return MAX_UINT32;
178
        return MAX_UINT32;
179
    }
179
    }
180
   
180
   
181
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
181
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
182
        if (a.parts.sign) {
182
        if (a.parts.sign) {
183
            return MIN_UINT32;
183
            return MIN_UINT32;
184
        }
184
        }
185
        return MAX_UINT32;
185
        return MAX_UINT32;
186
    }
186
    }
187
   
187
   
188
    return _float32_to_uint32_helper(a);   
188
    return _float32_to_uint32_helper(a);   
189
}
189
}
190
 
190
 
191
/* Convert float to signed int32
191
/* Convert float to signed int32
192
 * FIXME: Im not sure what to return if overflow/underflow happens
192
 * FIXME: Im not sure what to return if overflow/underflow happens
193
 *  - now its the biggest or the smallest int
193
 *  - now its the biggest or the smallest int
194
 */
194
 */
195
__s32 float32_to_int32(float32 a)
195
int32_t float32_to_int32(float32 a)
196
{
196
{
197
    if (isFloat32NaN(a)) {
197
    if (isFloat32NaN(a)) {
198
        return MAX_INT32;
198
        return MAX_INT32;
199
    }
199
    }
200
   
200
   
201
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
201
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
202
        if (a.parts.sign) {
202
        if (a.parts.sign) {
203
            return MIN_INT32;
203
            return MIN_INT32;
204
        }
204
        }
205
        return MAX_INT32;
205
        return MAX_INT32;
206
    }
206
    }
207
    return _float32_to_uint32_helper(a);
207
    return _float32_to_uint32_helper(a);
208
}  
208
}  
209
 
209
 
210
 
210
 
211
/** Helping procedure for converting float64 to uint64
211
/** Helping procedure for converting float64 to uint64
212
 * @param a floating point number in normalized form (no NaNs or Inf are checked )
212
 * @param a floating point number in normalized form (no NaNs or Inf are checked )
213
 * @return unsigned integer
213
 * @return unsigned integer
214
 */
214
 */
215
static __u64 _float64_to_uint64_helper(float64 a)
215
static uint64_t _float64_to_uint64_helper(float64 a)
216
{
216
{
217
    __u64 frac;
217
    uint64_t frac;
218
   
218
   
219
    if (a.parts.exp < FLOAT64_BIAS) {
219
    if (a.parts.exp < FLOAT64_BIAS) {
220
        /*TODO: rounding*/
220
        /*TODO: rounding*/
221
        return 0;
221
        return 0;
222
    }
222
    }
223
   
223
   
224
    frac = a.parts.fraction;
224
    frac = a.parts.fraction;
225
   
225
   
226
    frac |= FLOAT64_HIDDEN_BIT_MASK;
226
    frac |= FLOAT64_HIDDEN_BIT_MASK;
227
    /* shift fraction to left so hidden bit will be the most significant bit */
227
    /* shift fraction to left so hidden bit will be the most significant bit */
228
    frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
228
    frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
229
 
229
 
230
    frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
230
    frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
231
    if ((a.parts.sign == 1) && (frac != 0)) {
231
    if ((a.parts.sign == 1) && (frac != 0)) {
232
        frac = ~frac;
232
        frac = ~frac;
233
        ++frac;
233
        ++frac;
234
    }
234
    }
235
   
235
   
236
    return frac;
236
    return frac;
237
}
237
}
238
 
238
 
239
/* Convert float to unsigned int64
239
/* Convert float to unsigned int64
240
 * FIXME: Im not sure what to return if overflow/underflow happens
240
 * FIXME: Im not sure what to return if overflow/underflow happens
241
 *  - now its the biggest or the smallest int
241
 *  - now its the biggest or the smallest int
242
 */
242
 */
243
__u64 float64_to_uint64(float64 a)
243
uint64_t float64_to_uint64(float64 a)
244
{
244
{
245
    if (isFloat64NaN(a)) {
245
    if (isFloat64NaN(a)) {
246
        return MAX_UINT64;
246
        return MAX_UINT64;
247
    }
247
    }
248
   
248
   
249
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
249
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
250
        if (a.parts.sign) {
250
        if (a.parts.sign) {
251
            return MIN_UINT64;
251
            return MIN_UINT64;
252
        }
252
        }
253
        return MAX_UINT64;
253
        return MAX_UINT64;
254
    }
254
    }
255
   
255
   
256
    return _float64_to_uint64_helper(a);   
256
    return _float64_to_uint64_helper(a);   
257
}
257
}
258
 
258
 
259
/* Convert float to signed int64
259
/* Convert float to signed int64
260
 * FIXME: Im not sure what to return if overflow/underflow happens
260
 * FIXME: Im not sure what to return if overflow/underflow happens
261
 *  - now its the biggest or the smallest int
261
 *  - now its the biggest or the smallest int
262
 */
262
 */
263
__s64 float64_to_int64(float64 a)
263
int64_t float64_to_int64(float64 a)
264
{
264
{
265
    if (isFloat64NaN(a)) {
265
    if (isFloat64NaN(a)) {
266
        return MAX_INT64;
266
        return MAX_INT64;
267
    }
267
    }
268
   
268
   
269
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
269
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
270
        if (a.parts.sign) {
270
        if (a.parts.sign) {
271
            return MIN_INT64;
271
            return MIN_INT64;
272
        }
272
        }
273
        return MAX_INT64;
273
        return MAX_INT64;
274
    }
274
    }
275
    return _float64_to_uint64_helper(a);
275
    return _float64_to_uint64_helper(a);
276
}  
276
}  
277
 
277
 
278
 
278
 
279
 
279
 
280
 
280
 
281
 
281
 
282
/** Helping procedure for converting float32 to uint64
282
/** Helping procedure for converting float32 to uint64
283
 * @param a floating point number in normalized form (no NaNs or Inf are checked )
283
 * @param a floating point number in normalized form (no NaNs or Inf are checked )
284
 * @return unsigned integer
284
 * @return unsigned integer
285
 */
285
 */
286
static __u64 _float32_to_uint64_helper(float32 a)
286
static uint64_t _float32_to_uint64_helper(float32 a)
287
{
287
{
288
    __u64 frac;
288
    uint64_t frac;
289
   
289
   
290
    if (a.parts.exp < FLOAT32_BIAS) {
290
    if (a.parts.exp < FLOAT32_BIAS) {
291
        /*TODO: rounding*/
291
        /*TODO: rounding*/
292
        return 0;
292
        return 0;
293
    }
293
    }
294
   
294
   
295
    frac = a.parts.fraction;
295
    frac = a.parts.fraction;
296
   
296
   
297
    frac |= FLOAT32_HIDDEN_BIT_MASK;
297
    frac |= FLOAT32_HIDDEN_BIT_MASK;
298
    /* shift fraction to left so hidden bit will be the most significant bit */
298
    /* shift fraction to left so hidden bit will be the most significant bit */
299
    frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
299
    frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
300
 
300
 
301
    frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
301
    frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
302
    if ((a.parts.sign == 1) && (frac != 0)) {
302
    if ((a.parts.sign == 1) && (frac != 0)) {
303
        frac = ~frac;
303
        frac = ~frac;
304
        ++frac;
304
        ++frac;
305
    }
305
    }
306
   
306
   
307
    return frac;
307
    return frac;
308
}
308
}
309
 
309
 
310
/* Convert float to unsigned int64
310
/* Convert float to unsigned int64
311
 * FIXME: Im not sure what to return if overflow/underflow happens
311
 * FIXME: Im not sure what to return if overflow/underflow happens
312
 *  - now its the biggest or the smallest int
312
 *  - now its the biggest or the smallest int
313
 */
313
 */
314
__u64 float32_to_uint64(float32 a)
314
uint64_t float32_to_uint64(float32 a)
315
{
315
{
316
    if (isFloat32NaN(a)) {
316
    if (isFloat32NaN(a)) {
317
        return MAX_UINT64;
317
        return MAX_UINT64;
318
    }
318
    }
319
   
319
   
320
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
320
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
321
        if (a.parts.sign) {
321
        if (a.parts.sign) {
322
            return MIN_UINT64;
322
            return MIN_UINT64;
323
        }
323
        }
324
        return MAX_UINT64;
324
        return MAX_UINT64;
325
    }
325
    }
326
   
326
   
327
    return _float32_to_uint64_helper(a);   
327
    return _float32_to_uint64_helper(a);   
328
}
328
}
329
 
329
 
330
/* Convert float to signed int64
330
/* Convert float to signed int64
331
 * FIXME: Im not sure what to return if overflow/underflow happens
331
 * FIXME: Im not sure what to return if overflow/underflow happens
332
 *  - now its the biggest or the smallest int
332
 *  - now its the biggest or the smallest int
333
 */
333
 */
334
__s64 float32_to_int64(float32 a)
334
int64_t float32_to_int64(float32 a)
335
{
335
{
336
    if (isFloat32NaN(a)) {
336
    if (isFloat32NaN(a)) {
337
        return MAX_INT64;
337
        return MAX_INT64;
338
    }
338
    }
339
   
339
   
340
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
340
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
341
        if (a.parts.sign) {
341
        if (a.parts.sign) {
342
            return (MIN_INT64);
342
            return (MIN_INT64);
343
        }
343
        }
344
        return MAX_INT64;
344
        return MAX_INT64;
345
    }
345
    }
346
    return _float32_to_uint64_helper(a);
346
    return _float32_to_uint64_helper(a);
347
}  
347
}  
348
 
348
 
349
 
349
 
350
/* Convert float64 to unsigned int32
350
/* Convert float64 to unsigned int32
351
 * FIXME: Im not sure what to return if overflow/underflow happens
351
 * FIXME: Im not sure what to return if overflow/underflow happens
352
 *  - now its the biggest or the smallest int
352
 *  - now its the biggest or the smallest int
353
 */
353
 */
354
__u32 float64_to_uint32(float64 a)
354
uint32_t float64_to_uint32(float64 a)
355
{
355
{
356
    if (isFloat64NaN(a)) {
356
    if (isFloat64NaN(a)) {
357
        return MAX_UINT32;
357
        return MAX_UINT32;
358
    }
358
    }
359
   
359
   
360
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
360
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
361
        if (a.parts.sign) {
361
        if (a.parts.sign) {
362
            return MIN_UINT32;
362
            return MIN_UINT32;
363
        }
363
        }
364
        return MAX_UINT32;
364
        return MAX_UINT32;
365
    }
365
    }
366
   
366
   
367
    return (__u32)_float64_to_uint64_helper(a);
367
    return (uint32_t)_float64_to_uint64_helper(a); 
368
}
368
}
369
 
369
 
370
/* Convert float64 to signed int32
370
/* Convert float64 to signed int32
371
 * FIXME: Im not sure what to return if overflow/underflow happens
371
 * FIXME: Im not sure what to return if overflow/underflow happens
372
 *  - now its the biggest or the smallest int
372
 *  - now its the biggest or the smallest int
373
 */
373
 */
374
__s32 float64_to_int32(float64 a)
374
int32_t float64_to_int32(float64 a)
375
{
375
{
376
    if (isFloat64NaN(a)) {
376
    if (isFloat64NaN(a)) {
377
        return MAX_INT32;
377
        return MAX_INT32;
378
    }
378
    }
379
   
379
   
380
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
380
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
381
        if (a.parts.sign) {
381
        if (a.parts.sign) {
382
            return MIN_INT32;
382
            return MIN_INT32;
383
        }
383
        }
384
        return MAX_INT32;
384
        return MAX_INT32;
385
    }
385
    }
386
    return (__s32)_float64_to_uint64_helper(a);
386
    return (int32_t)_float64_to_uint64_helper(a);
387
}  
387
}  
388
 
388
 
389
/** Convert unsigned integer to float32
389
/** Convert unsigned integer to float32
390
 *
390
 *
391
 *
391
 *
392
 */
392
 */
393
float32 uint32_to_float32(__u32 i)
393
float32 uint32_to_float32(uint32_t i)
394
{
394
{
395
    int counter;
395
    int counter;
396
    __s32 exp;
396
    int32_t exp;
397
    float32 result;
397
    float32 result;
398
   
398
   
399
    result.parts.sign = 0;
399
    result.parts.sign = 0;
400
    result.parts.fraction = 0;
400
    result.parts.fraction = 0;
401
 
401
 
402
    counter = countZeroes32(i);
402
    counter = countZeroes32(i);
403
 
403
 
404
    exp = FLOAT32_BIAS + 32 - counter - 1;
404
    exp = FLOAT32_BIAS + 32 - counter - 1;
405
   
405
   
406
    if (counter == 32) {
406
    if (counter == 32) {
407
        result.binary = 0;
407
        result.binary = 0;
408
        return result;
408
        return result;
409
    }
409
    }
410
   
410
   
411
    if (counter > 0) {
411
    if (counter > 0) {
412
        i <<= counter - 1;
412
        i <<= counter - 1;
413
    } else {
413
    } else {
414
        i >>= 1;
414
        i >>= 1;
415
    }
415
    }
416
 
416
 
417
    roundFloat32(&exp, &i);
417
    roundFloat32(&exp, &i);
418
 
418
 
419
    result.parts.fraction = i >> 7;
419
    result.parts.fraction = i >> 7;
420
    result.parts.exp = exp;
420
    result.parts.exp = exp;
421
 
421
 
422
    return result;
422
    return result;
423
}
423
}
424
 
424
 
425
float32 int32_to_float32(__s32 i)
425
float32 int32_to_float32(int32_t i)
426
{
426
{
427
    float32 result;
427
    float32 result;
428
 
428
 
429
    if (i < 0) {
429
    if (i < 0) {
430
        result = uint32_to_float32((__u32)(-i));
430
        result = uint32_to_float32((uint32_t)(-i));
431
    } else {
431
    } else {
432
        result = uint32_to_float32((__u32)i);
432
        result = uint32_to_float32((uint32_t)i);
433
    }
433
    }
434
   
434
   
435
    result.parts.sign = i < 0;
435
    result.parts.sign = i < 0;
436
 
436
 
437
    return result;
437
    return result;
438
}
438
}
439
 
439
 
440
 
440
 
441
float32 uint64_to_float32(__u64 i)
441
float32 uint64_to_float32(uint64_t i)
442
{
442
{
443
    int counter;
443
    int counter;
444
    __s32 exp;
444
    int32_t exp;
-
 
445
    int32_t j;
445
    float32 result;
446
    float32 result;
446
   
447
   
447
    result.parts.sign = 0;
448
    result.parts.sign = 0;
448
    result.parts.fraction = 0;
449
    result.parts.fraction = 0;
449
 
450
 
450
    counter = countZeroes64(i);
451
    counter = countZeroes64(i);
451
 
452
 
452
    exp = FLOAT32_BIAS + 64 - counter - 1;
453
    exp = FLOAT32_BIAS + 64 - counter - 1;
453
   
454
   
454
    if (counter == 64) {
455
    if (counter == 64) {
455
        result.binary = 0;
456
        result.binary = 0;
456
        return result;
457
        return result;
457
    }
458
    }
458
   
459
   
459
    /* Shift all to the first 31 bits (31. will be hidden 1)*/
460
    /* Shift all to the first 31 bits (31. will be hidden 1)*/
460
    if (counter > 33) {
461
    if (counter > 33) {
461
        i <<= counter - 1 - 32;
462
        i <<= counter - 1 - 32;
462
    } else {
463
    } else {
463
        i >>= 1 + 32 - counter;
464
        i >>= 1 + 32 - counter;
464
    }
465
    }
-
 
466
   
-
 
467
    j = (uint32_t)i;
-
 
468
    roundFloat32(&exp, &j);
465
 
469
 
466
    roundFloat32(&exp, &i);
-
 
467
 
-
 
468
    result.parts.fraction = i >> 7;
470
    result.parts.fraction = j >> 7;
469
    result.parts.exp = exp;
471
    result.parts.exp = exp;
470
    return result;
472
    return result;
471
}
473
}
472
 
474
 
473
float32 int64_to_float32(__s64 i)
475
float32 int64_to_float32(int64_t i)
474
{
476
{
475
    float32 result;
477
    float32 result;
476
 
478
 
477
    if (i < 0) {
479
    if (i < 0) {
478
        result = uint64_to_float32((__u64)(-i));
480
        result = uint64_to_float32((uint64_t)(-i));
479
    } else {
481
    } else {
480
        result = uint64_to_float32((__u64)i);
482
        result = uint64_to_float32((uint64_t)i);
481
    }
483
    }
482
   
484
   
483
    result.parts.sign = i < 0;
485
    result.parts.sign = i < 0;
484
 
486
 
485
    return result;
487
    return result;
486
}
488
}
487
 
489
 
488
/** Convert unsigned integer to float64
490
/** Convert unsigned integer to float64
489
 *
491
 *
490
 *
492
 *
491
 */
493
 */
492
float64 uint32_to_float64(__u32 i)
494
float64 uint32_to_float64(uint32_t i)
493
{
495
{
494
    int counter;
496
    int counter;
495
    __s32 exp;
497
    int32_t exp;
496
    float64 result;
498
    float64 result;
497
    __u64 frac;
499
    uint64_t frac;
498
   
500
   
499
    result.parts.sign = 0;
501
    result.parts.sign = 0;
500
    result.parts.fraction = 0;
502
    result.parts.fraction = 0;
501
 
503
 
502
    counter = countZeroes32(i);
504
    counter = countZeroes32(i);
503
 
505
 
504
    exp = FLOAT64_BIAS + 32 - counter - 1;
506
    exp = FLOAT64_BIAS + 32 - counter - 1;
505
   
507
   
506
    if (counter == 32) {
508
    if (counter == 32) {
507
        result.binary = 0;
509
        result.binary = 0;
508
        return result;
510
        return result;
509
    }
511
    }
510
   
512
   
511
    frac = i;
513
    frac = i;
512
    frac <<= counter + 32 - 1;
514
    frac <<= counter + 32 - 1;
513
 
515
 
514
    roundFloat64(&exp, &frac);
516
    roundFloat64(&exp, &frac);
515
 
517
 
516
    result.parts.fraction = frac >> 10;
518
    result.parts.fraction = frac >> 10;
517
    result.parts.exp = exp;
519
    result.parts.exp = exp;
518
 
520
 
519
    return result;
521
    return result;
520
}
522
}
521
 
523
 
522
float64 int32_to_float64(__s32 i)
524
float64 int32_to_float64(int32_t i)
523
{
525
{
524
    float64 result;
526
    float64 result;
525
 
527
 
526
    if (i < 0) {
528
    if (i < 0) {
527
        result = uint32_to_float64((__u32)(-i));
529
        result = uint32_to_float64((uint32_t)(-i));
528
    } else {
530
    } else {
529
        result = uint32_to_float64((__u32)i);
531
        result = uint32_to_float64((uint32_t)i);
530
    }
532
    }
531
   
533
   
532
    result.parts.sign = i < 0;
534
    result.parts.sign = i < 0;
533
 
535
 
534
    return result;
536
    return result;
535
}
537
}
536
 
538
 
537
 
539
 
538
float64 uint64_to_float64(__u64 i)
540
float64 uint64_to_float64(uint64_t i)
539
{
541
{
540
    int counter;
542
    int counter;
541
    __s32 exp;
543
    int32_t exp;
542
    float64 result;
544
    float64 result;
543
   
545
   
544
    result.parts.sign = 0;
546
    result.parts.sign = 0;
545
    result.parts.fraction = 0;
547
    result.parts.fraction = 0;
546
 
548
 
547
    counter = countZeroes64(i);
549
    counter = countZeroes64(i);
548
 
550
 
549
    exp = FLOAT64_BIAS + 64 - counter - 1;
551
    exp = FLOAT64_BIAS + 64 - counter - 1;
550
   
552
   
551
    if (counter == 64) {
553
    if (counter == 64) {
552
        result.binary = 0;
554
        result.binary = 0;
553
        return result;
555
        return result;
554
    }
556
    }
555
   
557
   
556
    if (counter > 0) {
558
    if (counter > 0) {
557
        i <<= counter - 1;
559
        i <<= counter - 1;
558
    } else {
560
    } else {
559
        i >>= 1;
561
        i >>= 1;
560
    }
562
    }
561
 
563
 
562
    roundFloat64(&exp, &i);
564
    roundFloat64(&exp, &i);
563
 
565
 
564
    result.parts.fraction = i >> 10;
566
    result.parts.fraction = i >> 10;
565
    result.parts.exp = exp;
567
    result.parts.exp = exp;
566
    return result;
568
    return result;
567
}
569
}
568
 
570
 
569
float64 int64_to_float64(__s64 i)
571
float64 int64_to_float64(int64_t i)
570
{
572
{
571
    float64 result;
573
    float64 result;
572
 
574
 
573
    if (i < 0) {
575
    if (i < 0) {
574
        result = uint64_to_float64((__u64)(-i));
576
        result = uint64_to_float64((uint64_t)(-i));
575
    } else {
577
    } else {
576
        result = uint64_to_float64((__u64)i);
578
        result = uint64_to_float64((uint64_t)i);
577
    }
579
    }
578
   
580
   
579
    result.parts.sign = i < 0;
581
    result.parts.sign = i < 0;
580
 
582
 
581
    return result;
583
    return result;
582
}
584
}
583
 
585
 
584
 
586
 
585
 
587