1 | |
---|
2 | /* @(#)z_ldexp.c 1.0 98/08/13 */ |
---|
3 | |
---|
4 | /* |
---|
5 | FUNCTION |
---|
6 | <<ldexp>>, <<ldexpf>>---load exponent |
---|
7 | |
---|
8 | INDEX |
---|
9 | ldexp |
---|
10 | INDEX |
---|
11 | ldexpf |
---|
12 | |
---|
13 | SYNOPSIS |
---|
14 | #include <math.h> |
---|
15 | double ldexp(double <[val]>, int <[exp]>); |
---|
16 | float ldexpf(float <[val]>, int <[exp]>); |
---|
17 | |
---|
18 | DESCRIPTION |
---|
19 | <<ldexp>> calculates the value |
---|
20 | @ifnottex |
---|
21 | <[val]> times 2 to the power <[exp]>. |
---|
22 | @end ifnottex |
---|
23 | @tex |
---|
24 | $val\times 2^{exp}$. |
---|
25 | @end tex |
---|
26 | <<ldexpf>> is identical, save that it takes and returns <<float>> |
---|
27 | rather than <<double>> values. |
---|
28 | |
---|
29 | RETURNS |
---|
30 | <<ldexp>> returns the calculated value. |
---|
31 | |
---|
32 | Underflow and overflow both set <<errno>> to <<ERANGE>>. |
---|
33 | On underflow, <<ldexp>> and <<ldexpf>> return 0.0. |
---|
34 | On overflow, <<ldexp>> returns plus or minus <<HUGE_VAL>>. |
---|
35 | |
---|
36 | PORTABILITY |
---|
37 | <<ldexp>> is ANSI. <<ldexpf>> is an extension. |
---|
38 | |
---|
39 | */ |
---|
40 | |
---|
41 | /****************************************************************** |
---|
42 | * ldexp |
---|
43 | * |
---|
44 | * Input: |
---|
45 | * d - a floating point value |
---|
46 | * e - an exponent value |
---|
47 | * |
---|
48 | * Output: |
---|
49 | * A floating point value f such that f = d * 2 ^ e. |
---|
50 | * |
---|
51 | * Description: |
---|
52 | * This function creates a floating point number f such that |
---|
53 | * f = d * 2 ^ e. |
---|
54 | * |
---|
55 | *****************************************************************/ |
---|
56 | |
---|
57 | #include <float.h> |
---|
58 | #include "fdlibm.h" |
---|
59 | #include "zmath.h" |
---|
60 | |
---|
61 | #ifndef _DOUBLE_IS_32BITS |
---|
62 | |
---|
63 | #define DOUBLE_EXP_OFFS 1023 |
---|
64 | |
---|
65 | double |
---|
66 | ldexp (double d, |
---|
67 | int e) |
---|
68 | { |
---|
69 | int exp; |
---|
70 | __uint32_t hd; |
---|
71 | |
---|
72 | GET_HIGH_WORD (hd, d); |
---|
73 | |
---|
74 | /* Check for special values and then scale d by e. */ |
---|
75 | switch (numtest (d)) |
---|
76 | { |
---|
77 | case NAN: |
---|
78 | errno = EDOM; |
---|
79 | break; |
---|
80 | |
---|
81 | case INF: |
---|
82 | errno = ERANGE; |
---|
83 | break; |
---|
84 | |
---|
85 | case 0: |
---|
86 | break; |
---|
87 | |
---|
88 | default: |
---|
89 | exp = (hd & 0x7ff00000) >> 20; |
---|
90 | exp += e; |
---|
91 | |
---|
92 | if (exp > DBL_MAX_EXP + DOUBLE_EXP_OFFS) |
---|
93 | { |
---|
94 | errno = ERANGE; |
---|
95 | d = z_infinity.d; |
---|
96 | } |
---|
97 | else if (exp < DBL_MIN_EXP + DOUBLE_EXP_OFFS) |
---|
98 | { |
---|
99 | errno = ERANGE; |
---|
100 | d = -z_infinity.d; |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | hd &= 0x800fffff; |
---|
105 | hd |= exp << 20; |
---|
106 | SET_HIGH_WORD (d, hd); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | return (d); |
---|
111 | } |
---|
112 | |
---|
113 | #endif /* _DOUBLE_IS_32BITS */ |
---|