xref: /netbsd-src/lib/libm/src/s_scalbnf.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /* s_scalbnf.c -- float version of s_scalbn.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: s_scalbnf.c,v 1.7 1999/07/02 15:37:43 simonb Exp $");
19 #endif
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 #ifdef __STDC__
25 static const float
26 #else
27 static float
28 #endif
29 two25   =  3.355443200e+07,	/* 0x4c000000 */
30 twom25  =  2.9802322388e-08,	/* 0x33000000 */
31 huge   = 1.0e+30,
32 tiny   = 1.0e-30;
33 
34 #ifdef __STDC__
35 	float scalbnf (float x, int n)
36 #else
37 	float scalbn (x,n)
38 	float x; int n;
39 #endif
40 {
41 	int32_t k,ix;
42 	GET_FLOAT_WORD(ix,x);
43         k = (ix&0x7f800000)>>23;		/* extract exponent */
44         if (k==0) {				/* 0 or subnormal x */
45             if ((ix&0x7fffffff)==0) return x; /* +-0 */
46 	    x *= two25;
47 	    GET_FLOAT_WORD(ix,x);
48 	    k = ((ix&0x7f800000)>>23) - 25;
49             if (n< -50000) return tiny*x; 	/*underflow*/
50 	    }
51         if (k==0xff) return x+x;		/* NaN or Inf */
52         k = k+n;
53         if (k >  0xfe) return huge*copysignf(huge,x); /* overflow  */
54         if (k > 0) 				/* normal result */
55 	    {SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); return x;}
56         if (k <= -25) {
57             if (n > 50000) 	/* in case integer overflow in n+k */
58 		return huge*copysignf(huge,x);	/*overflow*/
59 	    else return tiny*copysignf(tiny,x);	/*underflow*/
60 	}
61         k += 25;				/* subnormal result */
62 	SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23));
63         return x*twom25;
64 }
65