1 /*
2 (C) Copyright IBM Corp. 2009
3 
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 * Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 * Neither the name of IBM nor the names of its contributors may be
15 used to endorse or promote products derived from this software without
16 specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "math_config.h"
32 
33 #if defined(_NEED_FLOAT_HUGE) && !defined(_HAVE_LONG_DOUBLE_MATH)
34 # if (LDBL_MANT_DIG == 53) /* 64-bit long double */
35 static const long double scale = 0x1p54;
36 
37 union ldbl {
38   long double x;
39   struct {
40 #  ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */
41     __uint32_t fracl;
42     __uint32_t frach:20;
43     __uint32_t exp:11;
44     __uint32_t sign:1;
45 #  endif
46 #  ifdef __IEEE_BIG_ENDIAN
47     __uint32_t sign:1;
48     __uint32_t exp:11;
49     __uint32_t frach:20;
50 #   ifndef ___IEEE_BYTES_LITTLE_ENDIAN
51 #   else /* ARMEL without __VFP_FP__ */
52     __uint32_t frach:20;
53     __uint32_t exp:11;
54     __uint32_t sign:1;
55 #   endif
56     __uint32_t fracl;
57 #  endif
58   } u32;
59 };
60 # elif (LDBL_MANT_DIG == 64) /* 80-bit long double */
61 static const double scale = 0x1p65;
62 
63 union ldbl {
64   long double x;
65   struct {
66 #  ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */
67     __uint32_t fracl;
68     __uint32_t frach;
69     __uint32_t exp:15;
70     __uint32_t sign:1;
71     __uint32_t pad:16;
72 #  endif
73 #  ifdef __IEEE_BIG_ENDIAN
74 #   ifndef ___IEEE_BYTES_LITTLE_ENDIAN /* for m86k */
75     __uint32_t sign:1;
76     __uint32_t exp:15;
77     __uint32_t pad:16;
78 #   else /* ARM FPA10 math copprocessor */
79     __uint32_t exp:15;
80     __uint32_t pad:16;
81     __uint32_t sign:1;
82 #   endif
83     __uint32_t frach;
84     __uint32_t fracl;
85 #  endif
86   } u32;
87 };
88 # elif (LDBL_MANT_DIG == 106) /* 128-bit double double */
89 static const long double scale = 0x1p107l;
90 
91 #define EXP_EXTRA_BIAS 100
92 
93 union ldbl {
94   long double x;
95   struct {
96 #  ifdef __IEEE_LITTLE_ENDIAN
97     __uint32_t frachm;
98     __uint32_t frach:20;
99     __uint32_t exp:11;
100     __uint32_t sign:1;
101     __uint32_t fracl;
102     __uint32_t fraclm:20;
103     __uint32_t exp_extra:11;
104     __uint32_t sign_extra:1;
105 #  endif
106 #  ifdef __IEEE_BIG_ENDIAN
107     __uint32_t sign:1;
108     __uint32_t exp:11;
109     __uint32_t frach:20;
110     __uint32_t frachm;
111     __uint32_t sign_extra:1;
112     __uint32_t exp_extra:11;
113     __uint32_t fraclm:20;
114     __uint32_t fracl;
115 #  endif
116   } u32;
117 };
118 # elif (LDBL_MANT_DIG == 113) /* 128-bit long double */
119 static const long double scale = 0x1p114l;
120 
121 union ldbl {
122   long double x;
123   struct {
124 #  ifdef __IEEE_LITTLE_ENDIAN
125     __uint32_t fracl;
126     __uint32_t fraclm;
127     __uint32_t frachm;
128     __uint32_t frach:16;
129     __uint32_t exp:15;
130     __uint32_t sign:1;
131 #  endif
132 #  ifdef __IEEE_BIG_ENDIAN
133 #   ifndef ___IEEE_BYTES_LITTLE_ENDIAN
134     __uint32_t sign:1;
135     __uint32_t exp:15;
136     __uint32_t frach:16;
137 #   else /* ARMEL without __VFP_FP__ */
138     __uint32_t frach:16;
139     __uint32_t exp:15;
140     __uint32_t sign:1;
141 #   endif
142     __uint32_t frachm;
143     __uint32_t fraclm;
144     __uint32_t fracl;
145 #  endif
146   } u32;
147 };
148 # else
149 #  error Unsupported long double format.
150 # endif
151 
152 static const int scale_exp = LDBL_MANT_DIG + 1;
153 
154 long double
frexpl(long double x,int * eptr)155 frexpl (long double x, int *eptr)
156 {
157   union ldbl u;
158   u.x = x;
159   int e = u.u32.exp;
160   *eptr = 0;
161   if (e == (LDBL_MAX_EXP*2 - 1) || x == 0)
162     return x; /* inf,nan,0 */
163   if (e == 0) /* subnormal */
164     {
165       u.x *= (long double) scale;
166       e = u.u32.exp;
167       *eptr -= scale_exp;
168     }
169   *eptr += e - (LDBL_MAX_EXP - 2);
170   u.u32.exp = LDBL_MAX_EXP - 2; /* -1 */
171 #ifdef EXP_EXTRA_BIAS
172   if (u.u32.exp_extra != 0)
173     u.u32.exp_extra = u.u32.exp - EXP_EXTRA_BIAS;
174 #endif
175   return u.x;
176 }
177 #endif /* _NEED_FLOAT_HUGE */
178