w_scalb.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <endian.h>
  22. typedef union
  23. {
  24. struct {
  25. #if (__BYTE_ORDER == __BIG_ENDIAN)
  26. unsigned long int hi;
  27. unsigned long int lo;
  28. #else
  29. unsigned long int lo;
  30. unsigned long int hi;
  31. #endif
  32. } words;
  33. double dbl;
  34. } DblInHex;
  35. static const double twoTo1023 = 8.988465674311579539e307; // 0x1p1023
  36. static const double twoToM1022 = 2.225073858507201383e-308; // 0x1p-1022
  37. /***********************************************************************
  38. double scalb( double x, long int n ) returns its argument x scaled
  39. by the factor 2^m. NaNs, signed zeros, and infinities are propagated
  40. by this function regardless of the value of n.
  41. Exceptions: OVERFLOW/INEXACT or UNDERFLOW inexact may occur;
  42. INVALID for signaling NaN inputs ( quiet NaN returned ).
  43. Calls: none.
  44. ***********************************************************************/
  45. libm_hidden_proto(scalb)
  46. double scalb ( double x, int n )
  47. {
  48. DblInHex xInHex;
  49. xInHex.words.lo = 0UL; // init. low half of xInHex
  50. if ( n > 1023 )
  51. { // large positive scaling
  52. if ( n > 2097 ) // huge scaling
  53. return ( ( x * twoTo1023 ) * twoTo1023 ) * twoTo1023;
  54. while ( n > 1023 )
  55. { // scale reduction loop
  56. x *= twoTo1023; // scale x by 2^1023
  57. n -= 1023; // reduce n by 1023
  58. }
  59. }
  60. else if ( n < -1022 )
  61. { // large negative scaling
  62. if ( n < -2098 ) // huge negative scaling
  63. return ( ( x * twoToM1022 ) * twoToM1022 ) * twoToM1022;
  64. while ( n < -1022 )
  65. { // scale reduction loop
  66. x *= twoToM1022; // scale x by 2^( -1022 )
  67. n += 1022; // incr n by 1022
  68. }
  69. }
  70. /*******************************************************************************
  71. * -1022 <= n <= 1023; convert n to double scale factor. *
  72. *******************************************************************************/
  73. xInHex.words.hi = ( ( unsigned long ) ( n + 1023 ) ) << 20;
  74. return ( x * xInHex.dbl );
  75. }
  76. libm_hidden_def(scalb)