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