sl@0
|
1 |
/* S_SCALBN.C
|
sl@0
|
2 |
*
|
sl@0
|
3 |
* Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
4 |
* All rights reserved.
|
sl@0
|
5 |
*/
|
sl@0
|
6 |
|
sl@0
|
7 |
|
sl@0
|
8 |
/* @(#)s_scalbn.c 5.1 93/09/24 */
|
sl@0
|
9 |
/*
|
sl@0
|
10 |
* ====================================================
|
sl@0
|
11 |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
sl@0
|
12 |
*
|
sl@0
|
13 |
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
sl@0
|
14 |
* Permission to use, copy, modify, and distribute this
|
sl@0
|
15 |
* software is freely granted, provided that this notice
|
sl@0
|
16 |
* is preserved.
|
sl@0
|
17 |
* ====================================================
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
|
sl@0
|
20 |
/*
|
sl@0
|
21 |
FUNCTION
|
sl@0
|
22 |
<<scalbn>>, <<scalbnf>>---scale by integer
|
sl@0
|
23 |
INDEX
|
sl@0
|
24 |
scalbn
|
sl@0
|
25 |
INDEX
|
sl@0
|
26 |
scalbnf
|
sl@0
|
27 |
|
sl@0
|
28 |
ANSI_SYNOPSIS
|
sl@0
|
29 |
#include <math.h>
|
sl@0
|
30 |
double scalbn(double <[x]>, int <[y]>);
|
sl@0
|
31 |
float scalbnf(float <[x]>, int <[y]>);
|
sl@0
|
32 |
|
sl@0
|
33 |
TRAD_SYNOPSIS
|
sl@0
|
34 |
#include <math.h>
|
sl@0
|
35 |
double scalbn(<[x]>,<[y]>)
|
sl@0
|
36 |
double <[x]>;
|
sl@0
|
37 |
int <[y]>;
|
sl@0
|
38 |
float scalbnf(<[x]>,<[y]>)
|
sl@0
|
39 |
float <[x]>;
|
sl@0
|
40 |
int <[y]>;
|
sl@0
|
41 |
|
sl@0
|
42 |
DESCRIPTION
|
sl@0
|
43 |
<<scalbn>> and <<scalbnf>> scale <[x]> by <[n]>, returning <[x]> times
|
sl@0
|
44 |
2 to the power <[n]>. The result is computed by manipulating the
|
sl@0
|
45 |
exponent, rather than by actually performing an exponentiation or
|
sl@0
|
46 |
multiplication.
|
sl@0
|
47 |
|
sl@0
|
48 |
RETURNS
|
sl@0
|
49 |
<[x]> times 2 to the power <[n]>.
|
sl@0
|
50 |
|
sl@0
|
51 |
PORTABILITY
|
sl@0
|
52 |
Neither <<scalbn>> nor <<scalbnf>> is required by ANSI C or by the System V
|
sl@0
|
53 |
Interface Definition (Issue 2).
|
sl@0
|
54 |
|
sl@0
|
55 |
*/
|
sl@0
|
56 |
|
sl@0
|
57 |
/*
|
sl@0
|
58 |
* scalbn (double x, int n)
|
sl@0
|
59 |
* scalbn(x,n) returns x* 2**n computed by exponent
|
sl@0
|
60 |
* manipulation rather than by actually performing an
|
sl@0
|
61 |
* exponentiation or a multiplication.
|
sl@0
|
62 |
*/
|
sl@0
|
63 |
|
sl@0
|
64 |
#include "FDLIBM.H"
|
sl@0
|
65 |
|
sl@0
|
66 |
static const double
|
sl@0
|
67 |
two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
|
sl@0
|
68 |
twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
|
sl@0
|
69 |
huge = 1.0e+300,
|
sl@0
|
70 |
tiny = 1.0e-300;
|
sl@0
|
71 |
|
sl@0
|
72 |
/**
|
sl@0
|
73 |
Scales x by n, returning x times
|
sl@0
|
74 |
2 to the power n. The result is computed by manipulating the
|
sl@0
|
75 |
exponent, rather than by actually performing an exponentiation or
|
sl@0
|
76 |
multiplication.
|
sl@0
|
77 |
@return x times 2 to the power n.
|
sl@0
|
78 |
@param x floating point value
|
sl@0
|
79 |
@param n integer power
|
sl@0
|
80 |
*/
|
sl@0
|
81 |
EXPORT_C double scalbn (double x, int n) __SOFTFP
|
sl@0
|
82 |
{
|
sl@0
|
83 |
__int32_t k,hx,lx;
|
sl@0
|
84 |
EXTRACT_WORDS(hx,lx,x);
|
sl@0
|
85 |
k = (hx&0x7ff00000)>>20; /* extract exponent */
|
sl@0
|
86 |
if (k==0) { /* 0 or subnormal x */
|
sl@0
|
87 |
if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
|
sl@0
|
88 |
x *= two54;
|
sl@0
|
89 |
GET_HIGH_WORD(hx,x);
|
sl@0
|
90 |
k = ((hx&0x7ff00000)>>20) - 54;
|
sl@0
|
91 |
if (n< -50000) return tiny*x; /*underflow*/
|
sl@0
|
92 |
}
|
sl@0
|
93 |
if (k==0x7ff) return x+x; /* NaN or Inf */
|
sl@0
|
94 |
k = k+n;
|
sl@0
|
95 |
if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
|
sl@0
|
96 |
if (k > 0) /* normal result */
|
sl@0
|
97 |
{SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;}
|
sl@0
|
98 |
if (k <= -54) {
|
sl@0
|
99 |
if (n > 50000) /* in case integer overflow in n+k */
|
sl@0
|
100 |
return huge*copysign(huge,x); /*overflow*/
|
sl@0
|
101 |
else return tiny*copysign(tiny,x); /*underflow*/
|
sl@0
|
102 |
}
|
sl@0
|
103 |
k += 54; /* subnormal result */
|
sl@0
|
104 |
SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20));
|
sl@0
|
105 |
return x*twom54;
|
sl@0
|
106 |
}
|