base/math/scalb.c

Go to the documentation of this file.
00001 #if defined(__ppc__)
00002 /***********************************************************************
00003 **      File:    scalb.c
00004 **      
00005 **      Contains: C source code for implementations of floating-point
00006 **                scalb functions defined in header <fp.h>.  In
00007 **                particular, this file contains implementations of
00008 **                functions scalb and scalbl for double and long double
00009 **                formats on PowerPC platforms.
00010 **                        
00011 **      Written by: Jon Okada, SANEitation Engineer, ext. 4-4838
00012 **      
00013 **      Copyright: © 1992 by Apple Computer, Inc., all rights reserved
00014 **      
00015 **      Change History ( most recent first ):
00016 **
00017 **      28 May 97  ali   made an speed improvement for large n,
00018 **                       removed scalbl.
00019 **      12 Dec 92  JPO   First created.
00020 **                        
00021 ***********************************************************************/
00022 
00023 typedef union           
00024       { 
00025       struct {
00026 #if defined(__BIG_ENDIAN__)
00027         unsigned long int hi;
00028         unsigned long int lo;
00029 #else
00030         unsigned long int lo;
00031         unsigned long int hi;
00032 #endif
00033       } words;
00034       double dbl;
00035       } DblInHex;
00036 
00037 static const double twoTo1023  = 8.988465674311579539e307;   // 0x1p1023
00038 static const double twoToM1022 = 2.225073858507201383e-308;  // 0x1p-1022
00039 
00040 
00041 /***********************************************************************
00042       double  scalb( double  x, long int n ) returns its argument x scaled
00043       by the factor 2^m.  NaNs, signed zeros, and infinities are propagated
00044       by this function regardless of the value of n.
00045       
00046       Exceptions:  OVERFLOW/INEXACT or UNDERFLOW inexact may occur;
00047                          INVALID for signaling NaN inputs ( quiet NaN returned ).
00048       
00049       Calls:  none.
00050 ***********************************************************************/
00051 
00052 double scalb ( double x, int n  )
00053       {
00054       DblInHex xInHex;
00055       
00056       xInHex.words.lo = 0UL;                     // init. low half of xInHex
00057       
00058       if ( n > 1023 ) 
00059             {                                   // large positive scaling
00060             if ( n > 2097 )                     // huge scaling
00061                 return ( ( x * twoTo1023 ) * twoTo1023 ) * twoTo1023;
00062             while ( n > 1023 ) 
00063                   {                             // scale reduction loop
00064                   x *= twoTo1023;               // scale x by 2^1023
00065                   n -= 1023;                    // reduce n by 1023
00066                   }
00067             }
00068       
00069       else if ( n < -1022 ) 
00070             {                                   // large negative scaling
00071             if ( n < -2098 )                    // huge negative scaling
00072                 return ( ( x * twoToM1022 ) * twoToM1022 ) * twoToM1022;
00073             while ( n < -1022 ) 
00074                   {                             // scale reduction loop
00075                   x *= twoToM1022;              // scale x by 2^( -1022 )
00076                   n += 1022;                    // incr n by 1022
00077                   }
00078             }
00079 
00080 /*******************************************************************************
00081 *      -1022 <= n <= 1023; convert n to double scale factor.                   *
00082 *******************************************************************************/
00083 
00084       xInHex.words.hi = ( ( unsigned long ) ( n + 1023 ) ) << 20;
00085       return ( x * xInHex.dbl );
00086       }
00087 #endif /* __ppc__ */

Generated on Tue Feb 2 17:46:05 2010 for RTAI API by  doxygen 1.4.7