s_frexp.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*******************************************************************************
  2. * *
  3. * File frexpldexp.c, *
  4. * Functions frexp(x) and ldexp(x), *
  5. * Implementation of frexp and ldexp functions for the PowerPC. *
  6. * *
  7. * Copyright © 1991 Apple Computer, Inc. All rights reserved. *
  8. * *
  9. * Written by Ali Sazegari, started on January 1991, *
  10. * *
  11. * W A R N I N G: This routine expects a 64 bit double model. *
  12. * *
  13. * December03 1992: first rs6000 implementation. *
  14. * October 05 1993: added special cases for NaN and ° in frexp. *
  15. * May 27 1997: improved the performance of frexp by eliminating the *
  16. * switch statement. *
  17. * June 13 2001: (ram) rewrote frexp to eliminate calls to scalb and *
  18. * logb. *
  19. * *
  20. *******************************************************************************/
  21. #include <limits.h>
  22. #include <math.h>
  23. #include <endian.h>
  24. static const double two54 = 1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */
  25. typedef union
  26. {
  27. struct {
  28. #if (__BYTE_ORDER == __BIG_ENDIAN)
  29. unsigned long int hi;
  30. unsigned long int lo;
  31. #else
  32. unsigned long int lo;
  33. unsigned long int hi;
  34. #endif
  35. } words;
  36. double dbl;
  37. } DblInHex;
  38. libm_hidden_proto(frexp)
  39. double frexp ( double value, int *eptr )
  40. {
  41. DblInHex argument;
  42. unsigned long int valueHead;
  43. argument.dbl = value;
  44. valueHead = argument.words.hi & 0x7fffffffUL; // valueHead <- |x|
  45. *eptr = 0;
  46. if ( valueHead >= 0x7ff00000 || ( valueHead | argument.words.lo ) == 0 )
  47. return value; // 0, inf, or NaN
  48. if ( valueHead < 0x00100000 )
  49. { // denorm
  50. argument.dbl = two54 * value;
  51. valueHead = argument.words.hi &0x7fffffff;
  52. *eptr = -54;
  53. }
  54. *eptr += ( valueHead >> 20 ) - 1022;
  55. argument.words.hi = ( argument.words.hi & 0x800fffff ) | 0x3fe00000;
  56. return argument.dbl;
  57. }
  58. libm_hidden_def(frexp)