w_scalb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /***********************************************************************
  2. ** File: scalb.c
  3. **
  4. ** Contains: C source code for implementations of floating-point
  5. ** scalb functions defined in header <fp.h>. In
  6. ** particular, this file contains implementations of
  7. ** functions scalb and scalbl for double and long double
  8. ** formats on PowerPC platforms.
  9. **
  10. ** Written by: Jon Okada, SANEitation Engineer, ext. 4-4838
  11. **
  12. ** Copyright: © 1992 by Apple Computer, Inc., all rights reserved
  13. **
  14. ** Change History ( most recent first ):
  15. **
  16. ** 28 May 97 ali made an speed improvement for large n,
  17. ** removed scalbl.
  18. ** 12 Dec 92 JPO First created.
  19. **
  20. ***********************************************************************/
  21. #include <math.h>
  22. #include <endian.h>
  23. typedef union
  24. {
  25. struct {
  26. #if (__BYTE_ORDER == __BIG_ENDIAN)
  27. unsigned long int hi;
  28. unsigned long int lo;
  29. #else
  30. unsigned long int lo;
  31. unsigned long int hi;
  32. #endif
  33. } words;
  34. double dbl;
  35. } DblInHex;
  36. static const double twoTo1023 = 8.988465674311579539e307; // 0x1p1023
  37. static const double twoToM1022 = 2.225073858507201383e-308; // 0x1p-1022
  38. /***********************************************************************
  39. double scalb( double x, long int n ) returns its argument x scaled
  40. by the factor 2^m. NaNs, signed zeros, and infinities are propagated
  41. by this function regardless of the value of n.
  42. Exceptions: OVERFLOW/INEXACT or UNDERFLOW inexact may occur;
  43. INVALID for signaling NaN inputs ( quiet NaN returned ).
  44. Calls: none.
  45. ***********************************************************************/
  46. libm_hidden_proto(scalb)
  47. double scalb ( double x, int n )
  48. {
  49. DblInHex xInHex;
  50. xInHex.words.lo = 0UL; // init. low half of xInHex
  51. if ( n > 1023 )
  52. { // large positive scaling
  53. if ( n > 2097 ) // huge scaling
  54. return ( ( x * twoTo1023 ) * twoTo1023 ) * twoTo1023;
  55. while ( n > 1023 )
  56. { // scale reduction loop
  57. x *= twoTo1023; // scale x by 2^1023
  58. n -= 1023; // reduce n by 1023
  59. }
  60. }
  61. else if ( n < -1022 )
  62. { // large negative scaling
  63. if ( n < -2098 ) // huge negative scaling
  64. return ( ( x * twoToM1022 ) * twoToM1022 ) * twoToM1022;
  65. while ( n < -1022 )
  66. { // scale reduction loop
  67. x *= twoToM1022; // scale x by 2^( -1022 )
  68. n += 1022; // incr n by 1022
  69. }
  70. }
  71. /*******************************************************************************
  72. * -1022 <= n <= 1023; convert n to double scale factor. *
  73. *******************************************************************************/
  74. xInHex.words.hi = ( ( unsigned long ) ( n + 1023 ) ) << 20;
  75. return ( x * xInHex.dbl );
  76. }
  77. libm_hidden_def(scalb)