Merge branch 'vendor/BZIP'
[dragonfly.git] / lib / libc / gen / ldexp.c
1 /* @(#)s_scalbn.c 5.1 93/09/24 */
2 /* @(#)fdlibm.h 5.1 93/09/24 */
3 /* $FreeBSD: head/lib/libc/gen/ldexp.c 140607 2005-01-22 06:03:40Z das $ */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunPro, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14
15 #include <sys/types.h>
16 #include <machine/endian.h>
17 #include <math.h>
18
19 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
20
21 #if BYTE_ORDER == BIG_ENDIAN
22
23 typedef union
24 {
25   double value;
26   struct
27   {
28     u_int32_t msw;
29     u_int32_t lsw;
30   } parts;
31 } ieee_double_shape_type;
32
33 #endif
34
35 #if BYTE_ORDER == LITTLE_ENDIAN
36
37 typedef union
38 {
39   double value;
40   struct
41   {
42     u_int32_t lsw;
43     u_int32_t msw;
44   } parts;
45 } ieee_double_shape_type;
46
47 #endif
48
49 /* Get two 32 bit ints from a double.  */
50
51 #define EXTRACT_WORDS(ix0,ix1,d)                                \
52 do {                                                            \
53   ieee_double_shape_type ew_u;                                  \
54   ew_u.value = (d);                                             \
55   (ix0) = ew_u.parts.msw;                                       \
56   (ix1) = ew_u.parts.lsw;                                       \
57 } while (0)
58
59 /* Get the more significant 32 bit int from a double.  */
60
61 #define GET_HIGH_WORD(i,d)                                      \
62 do {                                                            \
63   ieee_double_shape_type gh_u;                                  \
64   gh_u.value = (d);                                             \
65   (i) = gh_u.parts.msw;                                         \
66 } while (0)
67
68 /* Set the more significant 32 bits of a double from an int.  */
69
70 #define SET_HIGH_WORD(d,v)                                      \
71 do {                                                            \
72   ieee_double_shape_type sh_u;                                  \
73   sh_u.value = (d);                                             \
74   sh_u.parts.msw = (v);                                         \
75   (d) = sh_u.value;                                             \
76 } while (0)
77
78
79 static const double
80 two54   =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
81 twom54  =  5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
82 huge   = 1.0e+300,
83 tiny   = 1.0e-300;
84
85 static double
86 _copysign(double x, double y)
87 {
88         u_int32_t hx,hy;
89         GET_HIGH_WORD(hx,x);
90         GET_HIGH_WORD(hy,y);
91         SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000));
92         return x;
93 }
94
95 double
96 ldexp(double x, int n)
97 {
98         int32_t k,hx,lx;
99         EXTRACT_WORDS(hx,lx,x);
100         k = (hx&0x7ff00000)>>20;                /* extract exponent */
101         if (k==0) {                             /* 0 or subnormal x */
102             if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
103             x *= two54;
104             GET_HIGH_WORD(hx,x);
105             k = ((hx&0x7ff00000)>>20) - 54;
106             if (n< -50000) return tiny*x;       /*underflow*/
107             }
108         if (k==0x7ff) return x+x;               /* NaN or Inf */
109         k = k+n;
110         if (k >  0x7fe) return huge*_copysign(huge,x); /* overflow  */
111         if (k > 0)                              /* normal result */
112             {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
113         if (k <= -54) {
114             if (n > 50000)      /* in case integer overflow in n+k */
115                 return huge*_copysign(huge,x);  /*overflow*/
116             else return tiny*_copysign(tiny,x);         /*underflow*/
117         }
118         k += 54;                                /* subnormal result */
119         SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
120         return x*twom54;
121 }