Subversion Repositories HelenOS-historic

Rev

Rev 1657 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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