1 /* @(#)s_scalbn.c 5.1 93/09/24 */
2 /* @(#)fdlibm.h 5.1 93/09/24 */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
14 #include <sys/cdefs.h>
15 __FBSDID("$FreeBSD$");
17 #include <sys/types.h>
18 #include <machine/endian.h>
21 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
23 #if BYTE_ORDER == BIG_ENDIAN
33 } ieee_double_shape_type;
37 #if BYTE_ORDER == LITTLE_ENDIAN
47 } ieee_double_shape_type;
51 /* Get two 32 bit ints from a double. */
53 #define EXTRACT_WORDS(ix0,ix1,d) \
55 ieee_double_shape_type ew_u; \
57 (ix0) = ew_u.parts.msw; \
58 (ix1) = ew_u.parts.lsw; \
61 /* Get the more significant 32 bit int from a double. */
63 #define GET_HIGH_WORD(i,d) \
65 ieee_double_shape_type gh_u; \
67 (i) = gh_u.parts.msw; \
70 /* Set the more significant 32 bits of a double from an int. */
72 #define SET_HIGH_WORD(d,v) \
74 ieee_double_shape_type sh_u; \
76 sh_u.parts.msw = (v); \
82 two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
83 twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
88 _copysign(double x, double y)
93 SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000));
98 ldexp(double x, int n)
101 EXTRACT_WORDS(hx,lx,x);
102 k = (hx&0x7ff00000)>>20; /* extract exponent */
103 if (k==0) { /* 0 or subnormal x */
104 if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
107 k = ((hx&0x7ff00000)>>20) - 54;
108 if (n< -50000) return tiny*x; /*underflow*/
110 if (k==0x7ff) return x+x; /* NaN or Inf */
112 if (k > 0x7fe) return huge*_copysign(huge,x); /* overflow */
113 if (k > 0) /* normal result */
114 {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
116 if (n > 50000) /* in case integer overflow in n+k */
117 return huge*_copysign(huge,x); /*overflow*/
118 else return tiny*_copysign(tiny,x); /*underflow*/
120 k += 54; /* subnormal result */
121 SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));