1 /***********************************************************************
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.
10 ** Written by: Jon Okada, SANEitation Engineer, ext. 4-4838
12 ** Copyright: © 1992 by Apple Computer, Inc., all rights reserved
14 ** Change History ( most recent first ):
16 ** 28 May 97 ali made an speed improvement for large n,
18 ** 12 Dec 92 JPO First created.
20 ***********************************************************************/
28 #if (__BYTE_ORDER == __BIG_ENDIAN)
39 static const double twoTo1023 = 8.988465674311579539e307; // 0x1p1023
40 static const double twoToM1022 = 2.225073858507201383e-308; // 0x1p-1022
43 /***********************************************************************
44 double scalb( double x, long int n ) returns its argument x scaled
45 by the factor 2^m. NaNs, signed zeros, and infinities are propagated
46 by this function regardless of the value of n.
48 Exceptions: OVERFLOW/INEXACT or UNDERFLOW inexact may occur;
49 INVALID for signaling NaN inputs ( quiet NaN returned ).
52 ***********************************************************************/
54 libm_hidden_proto(scalb)
56 double scalb ( double x, int n )
58 double scalb ( double x, double n )
63 xInHex.words.lo = 0UL; // init. low half of xInHex
66 { // large positive scaling
67 if ( n > 2097 ) // huge scaling
68 return ( ( x * twoTo1023 ) * twoTo1023 ) * twoTo1023;
70 { // scale reduction loop
71 x *= twoTo1023; // scale x by 2^1023
72 n -= 1023; // reduce n by 1023
77 { // large negative scaling
78 if ( n < -2098 ) // huge negative scaling
79 return ( ( x * twoToM1022 ) * twoToM1022 ) * twoToM1022;
81 { // scale reduction loop
82 x *= twoToM1022; // scale x by 2^( -1022 )
83 n += 1022; // incr n by 1022
87 /*******************************************************************************
88 * -1022 <= n <= 1023; convert n to double scale factor. *
89 *******************************************************************************/
91 xInHex.words.hi = ( ( unsigned long ) ( n + 1023 ) ) << 20;
92 return ( x * xInHex.dbl );
94 libm_hidden_def(scalb)