os/ossrv/genericopenlibs/cstdlib/LMATH/S_SCALBN.C
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /* S_SCALBN.C
     2  * 
     3  * Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies).
     4  * All rights reserved.
     5  */
     6 
     7 
     8 /* @(#)s_scalbn.c 5.1 93/09/24 */
     9 /*
    10  * ====================================================
    11  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
    12  *
    13  * Developed at SunPro, a Sun Microsystems, Inc. business.
    14  * Permission to use, copy, modify, and distribute this
    15  * software is freely granted, provided that this notice 
    16  * is preserved.
    17  * ====================================================
    18  */
    19 
    20 /*
    21 FUNCTION
    22 <<scalbn>>, <<scalbnf>>---scale by integer
    23 INDEX
    24 	scalbn
    25 INDEX
    26 	scalbnf
    27 
    28 ANSI_SYNOPSIS
    29 	#include <math.h>
    30 	double scalbn(double <[x]>, int <[y]>);
    31 	float scalbnf(float <[x]>, int <[y]>);
    32 
    33 TRAD_SYNOPSIS
    34 	#include <math.h>
    35 	double scalbn(<[x]>,<[y]>)
    36 	double <[x]>;
    37 	int <[y]>;
    38 	float scalbnf(<[x]>,<[y]>)
    39 	float <[x]>;
    40 	int <[y]>;
    41 
    42 DESCRIPTION
    43 <<scalbn>> and <<scalbnf>> scale <[x]> by <[n]>, returning <[x]> times
    44 2 to the power <[n]>.  The result is computed by manipulating the
    45 exponent, rather than by actually performing an exponentiation or
    46 multiplication.
    47 
    48 RETURNS
    49 <[x]> times 2 to the power <[n]>.
    50 
    51 PORTABILITY
    52 Neither <<scalbn>> nor <<scalbnf>> is required by ANSI C or by the System V
    53 Interface Definition (Issue 2).
    54 
    55 */
    56 
    57 /* 
    58  * scalbn (double x, int n)
    59  * scalbn(x,n) returns x* 2**n  computed by  exponent  
    60  * manipulation rather than by actually performing an 
    61  * exponentiation or a multiplication.
    62  */
    63 
    64 #include "FDLIBM.H"
    65 
    66 static const double
    67 two54   =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
    68 twom54  =  5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
    69 huge   = 1.0e+300,
    70 tiny   = 1.0e-300;
    71 
    72 /**
    73 Scales x by n, returning x times
    74 2 to the power n.  The result is computed by manipulating the
    75 exponent, rather than by actually performing an exponentiation or
    76 multiplication.
    77 @return x times 2 to the power n.
    78 @param x floating point value
    79 @param n integer power
    80 */
    81 EXPORT_C double scalbn (double x, int n) __SOFTFP
    82 {
    83 	__int32_t  k,hx,lx;
    84 	EXTRACT_WORDS(hx,lx,x);
    85         k = (hx&0x7ff00000)>>20;		/* extract exponent */
    86         if (k==0) {				/* 0 or subnormal x */
    87             if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
    88 	    x *= two54; 
    89 	    GET_HIGH_WORD(hx,x);
    90 	    k = ((hx&0x7ff00000)>>20) - 54; 
    91             if (n< -50000) return tiny*x; 	/*underflow*/
    92 	    }
    93         if (k==0x7ff) return x+x;		/* NaN or Inf */
    94         k = k+n; 
    95         if (k >  0x7fe) return huge*copysign(huge,x); /* overflow  */
    96         if (k > 0) 				/* normal result */
    97 	    {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
    98         if (k <= -54) {
    99             if (n > 50000) 	/* in case integer overflow in n+k */
   100 			     return huge*copysign(huge,x);	/*overflow*/
   101 			else return tiny*copysign(tiny,x); 	/*underflow*/
   102 		}
   103         k += 54;				/* subnormal result */
   104 	SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
   105         return x*twom54;
   106 }