sl@0: /* S_SCALBN.C
sl@0:  * 
sl@0:  * Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0:  * All rights reserved.
sl@0:  */
sl@0: 
sl@0: 
sl@0: /* @(#)s_scalbn.c 5.1 93/09/24 */
sl@0: /*
sl@0:  * ====================================================
sl@0:  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
sl@0:  *
sl@0:  * Developed at SunPro, a Sun Microsystems, Inc. business.
sl@0:  * Permission to use, copy, modify, and distribute this
sl@0:  * software is freely granted, provided that this notice 
sl@0:  * is preserved.
sl@0:  * ====================================================
sl@0:  */
sl@0: 
sl@0: /*
sl@0: FUNCTION
sl@0: <<scalbn>>, <<scalbnf>>---scale by integer
sl@0: INDEX
sl@0: 	scalbn
sl@0: INDEX
sl@0: 	scalbnf
sl@0: 
sl@0: ANSI_SYNOPSIS
sl@0: 	#include <math.h>
sl@0: 	double scalbn(double <[x]>, int <[y]>);
sl@0: 	float scalbnf(float <[x]>, int <[y]>);
sl@0: 
sl@0: TRAD_SYNOPSIS
sl@0: 	#include <math.h>
sl@0: 	double scalbn(<[x]>,<[y]>)
sl@0: 	double <[x]>;
sl@0: 	int <[y]>;
sl@0: 	float scalbnf(<[x]>,<[y]>)
sl@0: 	float <[x]>;
sl@0: 	int <[y]>;
sl@0: 
sl@0: DESCRIPTION
sl@0: <<scalbn>> and <<scalbnf>> scale <[x]> by <[n]>, returning <[x]> times
sl@0: 2 to the power <[n]>.  The result is computed by manipulating the
sl@0: exponent, rather than by actually performing an exponentiation or
sl@0: multiplication.
sl@0: 
sl@0: RETURNS
sl@0: <[x]> times 2 to the power <[n]>.
sl@0: 
sl@0: PORTABILITY
sl@0: Neither <<scalbn>> nor <<scalbnf>> is required by ANSI C or by the System V
sl@0: Interface Definition (Issue 2).
sl@0: 
sl@0: */
sl@0: 
sl@0: /* 
sl@0:  * scalbn (double x, int n)
sl@0:  * scalbn(x,n) returns x* 2**n  computed by  exponent  
sl@0:  * manipulation rather than by actually performing an 
sl@0:  * exponentiation or a multiplication.
sl@0:  */
sl@0: 
sl@0: #include "FDLIBM.H"
sl@0: 
sl@0: static const double
sl@0: two54   =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
sl@0: twom54  =  5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
sl@0: huge   = 1.0e+300,
sl@0: tiny   = 1.0e-300;
sl@0: 
sl@0: /**
sl@0: Scales x by n, returning x times
sl@0: 2 to the power n.  The result is computed by manipulating the
sl@0: exponent, rather than by actually performing an exponentiation or
sl@0: multiplication.
sl@0: @return x times 2 to the power n.
sl@0: @param x floating point value
sl@0: @param n integer power
sl@0: */
sl@0: EXPORT_C double scalbn (double x, int n) __SOFTFP
sl@0: {
sl@0: 	__int32_t  k,hx,lx;
sl@0: 	EXTRACT_WORDS(hx,lx,x);
sl@0:         k = (hx&0x7ff00000)>>20;		/* extract exponent */
sl@0:         if (k==0) {				/* 0 or subnormal x */
sl@0:             if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
sl@0: 	    x *= two54; 
sl@0: 	    GET_HIGH_WORD(hx,x);
sl@0: 	    k = ((hx&0x7ff00000)>>20) - 54; 
sl@0:             if (n< -50000) return tiny*x; 	/*underflow*/
sl@0: 	    }
sl@0:         if (k==0x7ff) return x+x;		/* NaN or Inf */
sl@0:         k = k+n; 
sl@0:         if (k >  0x7fe) return huge*copysign(huge,x); /* overflow  */
sl@0:         if (k > 0) 				/* normal result */
sl@0: 	    {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
sl@0:         if (k <= -54) {
sl@0:             if (n > 50000) 	/* in case integer overflow in n+k */
sl@0: 			     return huge*copysign(huge,x);	/*overflow*/
sl@0: 			else return tiny*copysign(tiny,x); 	/*underflow*/
sl@0: 		}
sl@0:         k += 54;				/* subnormal result */
sl@0: 	SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
sl@0:         return x*twom54;
sl@0: }