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 $ */
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
12 * ====================================================
15 #include <sys/types.h>
16 #include <machine/endian.h>
19 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
21 #if BYTE_ORDER == BIG_ENDIAN
31 } ieee_double_shape_type;
35 #if BYTE_ORDER == LITTLE_ENDIAN
45 } ieee_double_shape_type;
49 /* Get two 32 bit ints from a double. */
51 #define EXTRACT_WORDS(ix0,ix1,d) \
53 ieee_double_shape_type ew_u; \
55 (ix0) = ew_u.parts.msw; \
56 (ix1) = ew_u.parts.lsw; \
59 /* Get the more significant 32 bit int from a double. */
61 #define GET_HIGH_WORD(i,d) \
63 ieee_double_shape_type gh_u; \
65 (i) = gh_u.parts.msw; \
68 /* Set the more significant 32 bits of a double from an int. */
70 #define SET_HIGH_WORD(d,v) \
72 ieee_double_shape_type sh_u; \
74 sh_u.parts.msw = (v); \
80 two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
81 twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
86 _copysign(double x, double y)
91 SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000));
96 ldexp(double x, int n)
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 */
105 k = ((hx&0x7ff00000)>>20) - 54;
106 if (n< -50000) return tiny*x; /*underflow*/
108 if (k==0x7ff) return x+x; /* NaN or Inf */
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;}
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*/
118 k += 54; /* subnormal result */
119 SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));