Subversion Repositories HelenOS-historic

Rev

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

Rev 865 Rev 874
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
 
33
 
33
float64 convertFloat32ToFloat64(float32 a)
34
float64 convertFloat32ToFloat64(float32 a)
34
{
35
{
35
    float64 result;
36
    float64 result;
36
    __u64 frac;
37
    __u64 frac;
37
   
38
   
38
    result.parts.sign = a.parts.sign;
39
    result.parts.sign = a.parts.sign;
39
    result.parts.fraction = a.parts.fraction;
40
    result.parts.fraction = a.parts.fraction;
40
    result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE );
41
    result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE );
41
   
42
   
42
    if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
43
    if ((isFloat32Infinity(a))||(isFloat32NaN(a))) {
43
        result.parts.exp = 0x7FF;
44
        result.parts.exp = 0x7FF;
44
        /* TODO; check if its correct for SigNaNs*/
45
        /* TODO; check if its correct for SigNaNs*/
45
        return result;
46
        return result;
46
    };
47
    };
47
   
48
   
48
    result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
49
    result.parts.exp = a.parts.exp + ( (int)FLOAT64_BIAS - FLOAT32_BIAS );
49
    if (a.parts.exp == 0) {
50
    if (a.parts.exp == 0) {
50
        /* normalize denormalized numbers */
51
        /* normalize denormalized numbers */
51
 
52
 
52
        if (result.parts.fraction == 0ll) { /* fix zero */
53
        if (result.parts.fraction == 0ll) { /* fix zero */
53
            result.parts.exp = 0ll;
54
            result.parts.exp = 0ll;
54
            return result;
55
            return result;
55
        }
56
        }
56
           
57
           
57
        frac = result.parts.fraction;
58
        frac = result.parts.fraction;
58
       
59
       
59
        while (!(frac & (0x10000000000000ll))) {
60
        while (!(frac & (0x10000000000000ll))) {
60
            frac <<= 1;
61
            frac <<= 1;
61
            --result.parts.exp;
62
            --result.parts.exp;
62
        };
63
        };
63
       
64
       
64
        ++result.parts.exp;
65
        ++result.parts.exp;
65
        result.parts.fraction = frac;
66
        result.parts.fraction = frac;
66
    };
67
    };
67
   
68
   
68
    return result;
69
    return result;
69
   
70
   
70
}
71
}
71
 
72
 
72
float32 convertFloat64ToFloat32(float64 a)
73
float32 convertFloat64ToFloat32(float64 a)
73
{
74
{
74
    float32 result;
75
    float32 result;
75
    __s32 exp;
76
    __s32 exp;
76
    __u64 frac;
77
    __u64 frac;
77
   
78
   
78
    result.parts.sign = a.parts.sign;
79
    result.parts.sign = a.parts.sign;
79
   
80
   
80
    if (isFloat64NaN(a)) {
81
    if (isFloat64NaN(a)) {
81
       
82
       
82
        result.parts.exp = 0xFF;
83
        result.parts.exp = 0xFF;
83
       
84
       
84
        if (isFloat64SigNaN(a)) {
85
        if (isFloat64SigNaN(a)) {
85
            result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */
86
            result.parts.fraction = 0x800000; /* set first bit of fraction nonzero */
86
            return result;
87
            return result;
87
        }
88
        }
88
   
89
   
89
        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 */
90
        return result;
91
        return result;
91
    };
92
    };
92
 
93
 
93
    if (isFloat64Infinity(a)) {
94
    if (isFloat64Infinity(a)) {
94
        result.parts.fraction = 0;
95
        result.parts.fraction = 0;
95
        result.parts.exp = 0xFF;
96
        result.parts.exp = 0xFF;
96
        return result;
97
        return result;
97
    };
98
    };
98
 
99
 
99
    exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
100
    exp = (int)a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS;
100
   
101
   
101
    if (exp >= 0xFF) {
102
    if (exp >= 0xFF) {
102
        /*FIXME: overflow*/
103
        /*FIXME: overflow*/
103
        result.parts.fraction = 0;
104
        result.parts.fraction = 0;
104
        result.parts.exp = 0xFF;
105
        result.parts.exp = 0xFF;
105
        return result;
106
        return result;
106
       
107
       
107
    } else if (exp <= 0 ) {
108
    } else if (exp <= 0 ) {
108
       
109
       
109
        /* underflow or denormalized */
110
        /* underflow or denormalized */
110
       
111
       
111
        result.parts.exp = 0;
112
        result.parts.exp = 0;
112
       
113
       
113
        exp *= -1; 
114
        exp *= -1; 
114
        if (exp > FLOAT32_FRACTION_SIZE ) {
115
        if (exp > FLOAT32_FRACTION_SIZE ) {
115
            /* FIXME: underflow */
116
            /* FIXME: underflow */
116
            result.parts.fraction = 0;
117
            result.parts.fraction = 0;
117
            return result;
118
            return result;
118
        };
119
        };
119
       
120
       
120
        /* denormalized */
121
        /* denormalized */
121
       
122
       
122
        frac = a.parts.fraction;
123
        frac = a.parts.fraction;
123
        frac |= 0x10000000000000ll; /* denormalize and set hidden bit */
124
        frac |= 0x10000000000000ll; /* denormalize and set hidden bit */
124
       
125
       
125
        frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
126
        frac >>= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1);
126
       
127
       
127
        while (exp > 0) {
128
        while (exp > 0) {
128
            --exp;
129
            --exp;
129
            frac >>= 1;
130
            frac >>= 1;
130
        };
131
        };
131
        result.parts.fraction = frac;
132
        result.parts.fraction = frac;
132
       
133
       
133
        return result;
134
        return result;
134
    };
135
    };
135
 
136
 
136
    result.parts.exp = exp;
137
    result.parts.exp = exp;
137
    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);
138
    return result;
139
    return result;
139
}
140
}
140
 
141
 
141
 
142
 
142
/** Helping procedure for converting float32 to uint32
143
/** Helping procedure for converting float32 to uint32
143
 * @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 )
144
 * @return unsigned integer
145
 * @return unsigned integer
145
 */
146
 */
146
static __u32 _float32_to_uint32_helper(float32 a)
147
static __u32 _float32_to_uint32_helper(float32 a)
147
{
148
{
148
    __u32 frac;
149
    __u32 frac;
149
   
150
   
150
    if (a.parts.exp < FLOAT32_BIAS) {
151
    if (a.parts.exp < FLOAT32_BIAS) {
151
        /*TODO: rounding*/
152
        /*TODO: rounding*/
152
        return 0;
153
        return 0;
153
    }
154
    }
154
   
155
   
155
    frac = a.parts.fraction;
156
    frac = a.parts.fraction;
156
   
157
   
157
    frac |= FLOAT32_HIDDEN_BIT_MASK;
158
    frac |= FLOAT32_HIDDEN_BIT_MASK;
158
    /* 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 */
159
    frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
160
    frac <<= 32 - FLOAT32_FRACTION_SIZE - 1;
160
 
161
 
161
    frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
162
    frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1;
162
    if ((a.parts.sign == 1) && (frac != 0)) {
163
    if ((a.parts.sign == 1) && (frac != 0)) {
163
        frac = ~frac;
164
        frac = ~frac;
164
        ++frac;
165
        ++frac;
165
    }
166
    }
166
   
167
   
167
    return frac;
168
    return frac;
168
}
169
}
169
 
170
 
170
/* Convert float to unsigned int32
171
/* Convert float to unsigned int32
171
 * FIXME: Im not sure what to return if overflow/underflow happens
172
 * FIXME: Im not sure what to return if overflow/underflow happens
172
 *  - now its the biggest or the smallest int
173
 *  - now its the biggest or the smallest int
173
 */
174
 */
174
__u32 float32_to_uint32(float32 a)
175
__u32 float32_to_uint32(float32 a)
175
{
176
{
176
    if (isFloat32NaN(a)) {
177
    if (isFloat32NaN(a)) {
177
        return MAX_UINT32;
178
        return MAX_UINT32;
178
    }
179
    }
179
   
180
   
180
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
181
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
181
        if (a.parts.sign) {
182
        if (a.parts.sign) {
182
            return MIN_UINT32;
183
            return MIN_UINT32;
183
        }
184
        }
184
        return MAX_UINT32;
185
        return MAX_UINT32;
185
    }
186
    }
186
   
187
   
187
    return _float32_to_uint32_helper(a);   
188
    return _float32_to_uint32_helper(a);   
188
}
189
}
189
 
190
 
190
/* Convert float to signed int32
191
/* Convert float to signed int32
191
 * FIXME: Im not sure what to return if overflow/underflow happens
192
 * FIXME: Im not sure what to return if overflow/underflow happens
192
 *  - now its the biggest or the smallest int
193
 *  - now its the biggest or the smallest int
193
 */
194
 */
194
__s32 float32_to_int32(float32 a)
195
__s32 float32_to_int32(float32 a)
195
{
196
{
196
    if (isFloat32NaN(a)) {
197
    if (isFloat32NaN(a)) {
197
        return MAX_INT32;
198
        return MAX_INT32;
198
    }
199
    }
199
   
200
   
200
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
201
    if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS)))  {
201
        if (a.parts.sign) {
202
        if (a.parts.sign) {
202
            return MIN_INT32;
203
            return MIN_INT32;
203
        }
204
        }
204
        return MAX_INT32;
205
        return MAX_INT32;
205
    }
206
    }
206
    return _float32_to_uint32_helper(a);
207
    return _float32_to_uint32_helper(a);
207
}  
208
}  
208
 
209
 
209
 
210
 
210
/** Helping procedure for converting float64 to uint64
211
/** Helping procedure for converting float64 to uint64
211
 * @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 )
212
 * @return unsigned integer
213
 * @return unsigned integer
213
 */
214
 */
214
static __u64 _float64_to_uint64_helper(float64 a)
215
static __u64 _float64_to_uint64_helper(float64 a)
215
{
216
{
216
    __u64 frac;
217
    __u64 frac;
217
   
218
   
218
    if (a.parts.exp < FLOAT64_BIAS) {
219
    if (a.parts.exp < FLOAT64_BIAS) {
219
        /*TODO: rounding*/
220
        /*TODO: rounding*/
220
        return 0;
221
        return 0;
221
    }
222
    }
222
   
223
   
223
    frac = a.parts.fraction;
224
    frac = a.parts.fraction;
224
   
225
   
225
    frac |= FLOAT64_HIDDEN_BIT_MASK;
226
    frac |= FLOAT64_HIDDEN_BIT_MASK;
226
    /* 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 */
227
    frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
228
    frac <<= 64 - FLOAT64_FRACTION_SIZE - 1;
228
 
229
 
229
    frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
230
    frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1;
230
    if ((a.parts.sign == 1) && (frac != 0)) {
231
    if ((a.parts.sign == 1) && (frac != 0)) {
231
        frac = ~frac;
232
        frac = ~frac;
232
        ++frac;
233
        ++frac;
233
    }
234
    }
234
   
235
   
235
    return frac;
236
    return frac;
236
}
237
}
237
 
238
 
238
/* Convert float to unsigned int64
239
/* Convert float to unsigned int64
239
 * FIXME: Im not sure what to return if overflow/underflow happens
240
 * FIXME: Im not sure what to return if overflow/underflow happens
240
 *  - now its the biggest or the smallest int
241
 *  - now its the biggest or the smallest int
241
 */
242
 */
242
__u64 float64_to_uint64(float64 a)
243
__u64 float64_to_uint64(float64 a)
243
{
244
{
244
    if (isFloat64NaN(a)) {
245
    if (isFloat64NaN(a)) {
245
        return MAX_UINT64;
246
        return MAX_UINT64;
246
    }
247
    }
247
   
248
   
248
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
249
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
249
        if (a.parts.sign) {
250
        if (a.parts.sign) {
250
            return MIN_UINT64;
251
            return MIN_UINT64;
251
        }
252
        }
252
        return MAX_UINT64;
253
        return MAX_UINT64;
253
    }
254
    }
254
   
255
   
255
    return _float64_to_uint64_helper(a);   
256
    return _float64_to_uint64_helper(a);   
256
}
257
}
257
 
258
 
258
/* Convert float to signed int64
259
/* Convert float to signed int64
259
 * FIXME: Im not sure what to return if overflow/underflow happens
260
 * FIXME: Im not sure what to return if overflow/underflow happens
260
 *  - now its the biggest or the smallest int
261
 *  - now its the biggest or the smallest int
261
 */
262
 */
262
__s64 float64_to_int64(float64 a)
263
__s64 float64_to_int64(float64 a)
263
{
264
{
264
    if (isFloat64NaN(a)) {
265
    if (isFloat64NaN(a)) {
265
        return MAX_INT64;
266
        return MAX_INT64;
266
    }
267
    }
267
   
268
   
268
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
269
    if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS)))  {
269
        if (a.parts.sign) {
270
        if (a.parts.sign) {
270
            return MIN_INT64;
271
            return MIN_INT64;
271
        }
272
        }
272
        return MAX_INT64;
273
        return MAX_INT64;
273
    }
274
    }
274
    return _float64_to_uint64_helper(a);
275
    return _float64_to_uint64_helper(a);
275
}  
276
}  
276
 
277
 
277
 
278
 
278
 
279
 
279
 
280
 
280
 
281
 
281
/** Helping procedure for converting float32 to uint64
282
/** Helping procedure for converting float32 to uint64
282
 * @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 )
283
 * @return unsigned integer
284
 * @return unsigned integer
284
 */
285
 */
285
static __u64 _float32_to_uint64_helper(float32 a)
286
static __u64 _float32_to_uint64_helper(float32 a)
286
{
287
{
287
    __u64 frac;
288
    __u64 frac;
288
   
289
   
289
    if (a.parts.exp < FLOAT32_BIAS) {
290
    if (a.parts.exp < FLOAT32_BIAS) {
290
        /*TODO: rounding*/
291
        /*TODO: rounding*/
291
        return 0;
292
        return 0;
292
    }
293
    }
293
   
294
   
294
    frac = a.parts.fraction;
295
    frac = a.parts.fraction;
295
   
296
   
296
    frac |= FLOAT32_HIDDEN_BIT_MASK;
297
    frac |= FLOAT32_HIDDEN_BIT_MASK;
297
    /* 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 */
298
    frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
299
    frac <<= 64 - FLOAT32_FRACTION_SIZE - 1;
299
 
300
 
300
    frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
301
    frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1;
301
    if ((a.parts.sign == 1) && (frac != 0)) {
302
    if ((a.parts.sign == 1) && (frac != 0)) {
302
        frac = ~frac;
303
        frac = ~frac;
303
        ++frac;
304
        ++frac;
304
    }
305
    }
305
   
306
   
306
    return frac;
307
    return frac;
307
}
308
}
308
 
309
 
309
/* Convert float to unsigned int64
310
/* Convert float to unsigned int64
310
 * FIXME: Im not sure what to return if overflow/underflow happens
311
 * FIXME: Im not sure what to return if overflow/underflow happens
311
 *  - now its the biggest or the smallest int
312
 *  - now its the biggest or the smallest int
312
 */
313
 */
313
__u64 float32_to_uint64(float32 a)
314
__u64 float32_to_uint64(float32 a)
314
{
315
{
315
    if (isFloat32NaN(a)) {
316
    if (isFloat32NaN(a)) {
316
        return MAX_UINT64;
317
        return MAX_UINT64;
317
    }
318
    }
318
   
319
   
319
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
320
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
320
        if (a.parts.sign) {
321
        if (a.parts.sign) {
321
            return MIN_UINT64;
322
            return MIN_UINT64;
322
        }
323
        }
323
        return MAX_UINT64;
324
        return MAX_UINT64;
324
    }
325
    }
325
   
326
   
326
    return _float32_to_uint64_helper(a);   
327
    return _float32_to_uint64_helper(a);   
327
}
328
}
328
 
329
 
329
/* Convert float to signed int64
330
/* Convert float to signed int64
330
 * FIXME: Im not sure what to return if overflow/underflow happens
331
 * FIXME: Im not sure what to return if overflow/underflow happens
331
 *  - now its the biggest or the smallest int
332
 *  - now its the biggest or the smallest int
332
 */
333
 */
333
__s64 float32_to_int64(float32 a)
334
__s64 float32_to_int64(float32 a)
334
{
335
{
335
    if (isFloat32NaN(a)) {
336
    if (isFloat32NaN(a)) {
336
        return MAX_INT64;
337
        return MAX_INT64;
337
    }
338
    }
338
   
339
   
339
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
340
    if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS)))  {
340
        if (a.parts.sign) {
341
        if (a.parts.sign) {
341
            return (MIN_INT64);
342
            return (MIN_INT64);
342
        }
343
        }
343
        return MAX_INT64;
344
        return MAX_INT64;
344
    }
345
    }
345
    return _float32_to_uint64_helper(a);
346
    return _float32_to_uint64_helper(a);
346
}  
347
}  
347
 
348
 
348
 
349
 
349
/* Convert float64 to unsigned int32
350
/* Convert float64 to unsigned int32
350
 * FIXME: Im not sure what to return if overflow/underflow happens
351
 * FIXME: Im not sure what to return if overflow/underflow happens
351
 *  - now its the biggest or the smallest int
352
 *  - now its the biggest or the smallest int
352
 */
353
 */
353
__u32 float64_to_uint32(float64 a)
354
__u32 float64_to_uint32(float64 a)
354
{
355
{
355
    if (isFloat64NaN(a)) {
356
    if (isFloat64NaN(a)) {
356
        return MAX_UINT32;
357
        return MAX_UINT32;
357
    }
358
    }
358
   
359
   
359
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
360
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
360
        if (a.parts.sign) {
361
        if (a.parts.sign) {
361
            return MIN_UINT32;
362
            return MIN_UINT32;
362
        }
363
        }
363
        return MAX_UINT32;
364
        return MAX_UINT32;
364
    }
365
    }
365
   
366
   
366
    return (__u32)_float64_to_uint64_helper(a);
367
    return (__u32)_float64_to_uint64_helper(a);
367
}
368
}
368
 
369
 
369
/* Convert float64 to signed int32
370
/* Convert float64 to signed int32
370
 * FIXME: Im not sure what to return if overflow/underflow happens
371
 * FIXME: Im not sure what to return if overflow/underflow happens
371
 *  - now its the biggest or the smallest int
372
 *  - now its the biggest or the smallest int
372
 */
373
 */
373
__s32 float64_to_int32(float64 a)
374
__s32 float64_to_int32(float64 a)
374
{
375
{
375
    if (isFloat64NaN(a)) {
376
    if (isFloat64NaN(a)) {
376
        return MAX_INT32;
377
        return MAX_INT32;
377
    }
378
    }
378
   
379
   
379
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
380
    if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS)))  {
380
        if (a.parts.sign) {
381
        if (a.parts.sign) {
381
            return MIN_INT32;
382
            return MIN_INT32;
382
        }
383
        }
383
        return MAX_INT32;
384
        return MAX_INT32;
384
    }
385
    }
385
    return (__s32)_float64_to_uint64_helper(a);
386
    return (__s32)_float64_to_uint64_helper(a);
386
}  
387
}  
387
 
388
 
-
 
389
   
-
 
390
/** Convert unsigned integer to float32
-
 
391
 *
-
 
392
 *
-
 
393
 */
-
 
394
float32 uint32_to_float32(__u32 i)
-
 
395
{
-
 
396
    int counter;
-
 
397
    __s32 exp;
-
 
398
    float32 result;
-
 
399
   
-
 
400
    result.parts.sign = 0;
-
 
401
    result.parts.fraction = 0;
-
 
402
 
-
 
403
    counter = countZeroes32(i);
-
 
404
 
-
 
405
    exp = FLOAT32_BIAS + 32 - counter - 1;
-
 
406
   
-
 
407
    if (counter == 32) {
-
 
408
        result.binary = 0;
-
 
409
        return result;
-
 
410
    }
-
 
411
   
-
 
412
    if (counter > 0) {
-
 
413
        i <<= counter - 1;
-
 
414
    } else {
-
 
415
        i >>= 1;
-
 
416
    }
-
 
417
 
-
 
418
    roundFloat32(&exp, &i);
-
 
419
 
-
 
420
    result.parts.fraction = i >> 7;
-
 
421
    result.parts.exp = exp;
-
 
422
 
-
 
423
    return result;
-
 
424
}
388
 
425
 
-
 
426
float32 int32_to_float32(__s32 i)
-
 
427
{
-
 
428
    float32 result;
-
 
429
 
-
 
430
    if (i < 0) {
-
 
431
        result = uint32_to_float32((__u32)(-i));
-
 
432
    } else {
-
 
433
        result = uint32_to_float32((__u32)i);
-
 
434
    }
-
 
435
   
-
 
436
    result.parts.sign = i < 0;
-
 
437
 
-
 
438
    return result;
-
 
439
}
-
 
440
 
-
 
441
 
-
 
442
float32 uint64_to_float32(__u64 i)
-
 
443
{
-
 
444
}
-
 
445
 
-
 
446
float32 int64_to_float32(__s64 i)
-
 
447
{
-
 
448
    float32 result;
-
 
449
 
-
 
450
    if (i < 0) {
-
 
451
        result = uint64_to_float32((__u64)(-i));
-
 
452
    } else {
-
 
453
        result = uint64_to_float32((__u64)i);
-
 
454
    }
-
 
455
   
-
 
456
    result.parts.sign = i < 0;
-
 
457
 
-
 
458
    return result;
-
 
459
}
389
 
460