Rev 876 | Rev 1035 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 876 | Rev 1031 | ||
|---|---|---|---|
| Line 32... | Line 32... | ||
| 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 | ||
| Line 71... | Line 71... | ||
| 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; |
| Line 142... | Line 142... | ||
| 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 | } |
| Line 170... | Line 170... | ||
| 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 | ||
| Line 190... | Line 190... | ||
| 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 | ||
| Line 210... | Line 210... | ||
| 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 | } |
| Line 238... | Line 238... | ||
| 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 | ||
| Line 258... | Line 258... | ||
| 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 | ||
| Line 281... | Line 281... | ||
| 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 | } |
| Line 309... | Line 309... | ||
| 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 | ||
| Line 329... | Line 329... | ||
| 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 | ||
| Line 349... | Line 349... | ||
| 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 | ||
| Line 362... | Line 362... | ||
| 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 | ||
| Line 381... | Line 381... | ||
| 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 | ||
| Line 420... | Line 420... | ||
| 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 | ||
| Line 460... | Line 461... | ||
| 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; |
| Line 487... | Line 489... | ||
| 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); |
| Line 517... | Line 519... | ||
| 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 | ||
| Line 564... | Line 566... | ||
| 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; |