TODO update
[libm] / src / math / scalbnf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_scalbnf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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 "libm.h"
17
18 static const float
19 two25  = 3.355443200e+07,  /* 0x4c000000 */
20 twom25 = 2.9802322388e-08, /* 0x33000000 */
21 huge   = 1.0e+30,
22 tiny   = 1.0e-30;
23
24 float scalbnf(float x, int n)
25 {
26         int32_t k, ix;
27         GET_FLOAT_WORD(ix, x);
28         k = (ix&0x7f800000)>>23;           /* extract exponent */
29         if (k == 0) {                      /* 0 or subnormal x */
30                 if ((ix&0x7fffffff) == 0)  /* +-0 */
31                         return x;
32                 x *= two25;
33                 GET_FLOAT_WORD(ix, x);
34                 k = ((ix&0x7f800000)>>23) - 25;
35                 if (n < -50000)
36                         return tiny*x;  /*underflow*/
37         }
38         if (k == 0xff)                     /* NaN or Inf */
39                 return x + x;
40         k = k + n;
41         if (k > 0xfe)
42                 return huge*copysignf(huge, x);  /* overflow  */
43         if (k > 0) {                       /* normal result */
44                 SET_FLOAT_WORD(x, (ix&0x807fffff)|(k<<23));
45                 return x;
46         }
47         if (k <= -25)
48                 if (n > 50000)  /* in case integer overflow in n+k */
49                         return huge*copysignf(huge,x);  /*overflow*/
50                 return tiny*copysignf(tiny, x);  /*underflow*/
51         k += 25;                           /* subnormal result */
52         SET_FLOAT_WORD(x, (ix&0x807fffff)|(k<<23));
53         return x*twom25;
54 }