Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / common / s_scalbn.c
blob72da0e4a1673ab81398f9f1d34653ccff54c3976
2 /* @(#)s_scalbn.c 5.1 93/09/24 */
3 /*
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
10 * is preserved.
11 * ====================================================
15 FUNCTION
16 <<scalbn>>, <<scalbnf>>, <<scalbln>>, <<scalblnf>>---scale by power of FLT_RADIX (=2)
17 INDEX
18 scalbn
19 INDEX
20 scalbnf
21 INDEX
22 scalbln
23 INDEX
24 scalblnf
26 SYNOPSIS
27 #include <math.h>
28 double scalbn(double <[x]>, int <[n]>);
29 float scalbnf(float <[x]>, int <[n]>);
30 double scalbln(double <[x]>, long int <[n]>);
31 float scalblnf(float <[x]>, long int <[n]>);
33 DESCRIPTION
34 The <<scalbn>> and <<scalbln>> functions compute
35 @ifnottex
36 <[x]> times FLT_RADIX to the power <[n]>.
37 @end ifnottex
38 @tex
39 $x \cdot FLT\_RADIX^n$.
40 @end tex
41 efficiently. The result is computed by manipulating the exponent, rather than
42 by actually performing an exponentiation or multiplication. In this
43 floating-point implementation FLT_RADIX=2, which makes the <<scalbn>>
44 functions equivalent to the <<ldexp>> functions.
46 RETURNS
47 <[x]> times 2 to the power <[n]>. A range error may occur.
49 PORTABILITY
50 ANSI C, POSIX
52 SEEALSO
53 <<ldexp>>
57 /*
58 * scalbn (double x, int n)
59 * scalbn(x,n) returns x* 2**n computed by exponent
60 * manipulation rather than by actually performing an
61 * exponentiation or a multiplication.
64 #include "fdlibm.h"
66 #ifndef _DOUBLE_IS_32BITS
68 #ifdef __STDC__
69 static const double
70 #else
71 static double
72 #endif
73 two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
74 twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
75 huge = 1.0e+300,
76 tiny = 1.0e-300;
78 #ifdef __STDC__
79 double scalbn (double x, int n)
80 #else
81 double scalbn (x,n)
82 double x; int n;
83 #endif
85 __int32_t k,hx,lx;
86 EXTRACT_WORDS(hx,lx,x);
87 k = (hx&0x7ff00000)>>20; /* extract exponent */
88 if (k==0) { /* 0 or subnormal x */
89 if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
90 x *= two54;
91 GET_HIGH_WORD(hx,x);
92 k = ((hx&0x7ff00000)>>20) - 54;
93 if (n< -50000) return tiny*x; /*underflow*/
95 if (k==0x7ff) return x+x; /* NaN or Inf */
96 if (n > 50000) /* in case integer overflow in n+k */
97 return huge*copysign(huge,x); /*overflow*/
98 k = k+n;
99 if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
100 if (k > 0) /* normal result */
101 {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
102 if (k <= -54)
103 return tiny*copysign(tiny,x); /*underflow*/
104 k += 54; /* subnormal result */
105 SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
106 return x*twom54;
109 #endif /* _DOUBLE_IS_32BITS */