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