1*38fd1498Szrj /* IEEE floating point support routines, for GDB, the GNU Debugger.
2*38fd1498Szrj Copyright (C) 1991-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GDB.
5*38fd1498Szrj
6*38fd1498Szrj This program is free software; you can redistribute it and/or modify
7*38fd1498Szrj it under the terms of the GNU General Public License as published by
8*38fd1498Szrj the Free Software Foundation; either version 2 of the License, or
9*38fd1498Szrj (at your option) any later version.
10*38fd1498Szrj
11*38fd1498Szrj This program is distributed in the hope that it will be useful,
12*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with this program; if not, write to the Free Software
18*38fd1498Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
19*38fd1498Szrj
20*38fd1498Szrj /* This is needed to pick up the NAN macro on some systems. */
21*38fd1498Szrj #ifndef _GNU_SOURCE
22*38fd1498Szrj #define _GNU_SOURCE
23*38fd1498Szrj #endif
24*38fd1498Szrj
25*38fd1498Szrj #ifdef HAVE_CONFIG_H
26*38fd1498Szrj #include "config.h"
27*38fd1498Szrj #endif
28*38fd1498Szrj
29*38fd1498Szrj #include <math.h>
30*38fd1498Szrj
31*38fd1498Szrj #ifdef HAVE_STRING_H
32*38fd1498Szrj #include <string.h>
33*38fd1498Szrj #endif
34*38fd1498Szrj
35*38fd1498Szrj /* On some platforms, <float.h> provides DBL_QNAN. */
36*38fd1498Szrj #ifdef STDC_HEADERS
37*38fd1498Szrj #include <float.h>
38*38fd1498Szrj #endif
39*38fd1498Szrj
40*38fd1498Szrj #include "ansidecl.h"
41*38fd1498Szrj #include "libiberty.h"
42*38fd1498Szrj #include "floatformat.h"
43*38fd1498Szrj
44*38fd1498Szrj #ifndef INFINITY
45*38fd1498Szrj #ifdef HUGE_VAL
46*38fd1498Szrj #define INFINITY HUGE_VAL
47*38fd1498Szrj #else
48*38fd1498Szrj #define INFINITY (1.0 / 0.0)
49*38fd1498Szrj #endif
50*38fd1498Szrj #endif
51*38fd1498Szrj
52*38fd1498Szrj #ifndef NAN
53*38fd1498Szrj #ifdef DBL_QNAN
54*38fd1498Szrj #define NAN DBL_QNAN
55*38fd1498Szrj #else
56*38fd1498Szrj #define NAN (0.0 / 0.0)
57*38fd1498Szrj #endif
58*38fd1498Szrj #endif
59*38fd1498Szrj
60*38fd1498Szrj static int mant_bits_set (const struct floatformat *, const unsigned char *);
61*38fd1498Szrj static unsigned long get_field (const unsigned char *,
62*38fd1498Szrj enum floatformat_byteorders,
63*38fd1498Szrj unsigned int,
64*38fd1498Szrj unsigned int,
65*38fd1498Szrj unsigned int);
66*38fd1498Szrj static int floatformat_always_valid (const struct floatformat *fmt,
67*38fd1498Szrj const void *from);
68*38fd1498Szrj
69*38fd1498Szrj static int
floatformat_always_valid(const struct floatformat * fmt ATTRIBUTE_UNUSED,const void * from ATTRIBUTE_UNUSED)70*38fd1498Szrj floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
71*38fd1498Szrj const void *from ATTRIBUTE_UNUSED)
72*38fd1498Szrj {
73*38fd1498Szrj return 1;
74*38fd1498Szrj }
75*38fd1498Szrj
76*38fd1498Szrj /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
77*38fd1498Szrj going to bother with trying to muck around with whether it is defined in
78*38fd1498Szrj a system header, what we do if not, etc. */
79*38fd1498Szrj #define FLOATFORMAT_CHAR_BIT 8
80*38fd1498Szrj
81*38fd1498Szrj /* floatformats for IEEE half, single and double, big and little endian. */
82*38fd1498Szrj const struct floatformat floatformat_ieee_half_big =
83*38fd1498Szrj {
84*38fd1498Szrj floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
85*38fd1498Szrj floatformat_intbit_no,
86*38fd1498Szrj "floatformat_ieee_half_big",
87*38fd1498Szrj floatformat_always_valid,
88*38fd1498Szrj NULL
89*38fd1498Szrj };
90*38fd1498Szrj const struct floatformat floatformat_ieee_half_little =
91*38fd1498Szrj {
92*38fd1498Szrj floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
93*38fd1498Szrj floatformat_intbit_no,
94*38fd1498Szrj "floatformat_ieee_half_little",
95*38fd1498Szrj floatformat_always_valid,
96*38fd1498Szrj NULL
97*38fd1498Szrj };
98*38fd1498Szrj const struct floatformat floatformat_ieee_single_big =
99*38fd1498Szrj {
100*38fd1498Szrj floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
101*38fd1498Szrj floatformat_intbit_no,
102*38fd1498Szrj "floatformat_ieee_single_big",
103*38fd1498Szrj floatformat_always_valid,
104*38fd1498Szrj NULL
105*38fd1498Szrj };
106*38fd1498Szrj const struct floatformat floatformat_ieee_single_little =
107*38fd1498Szrj {
108*38fd1498Szrj floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
109*38fd1498Szrj floatformat_intbit_no,
110*38fd1498Szrj "floatformat_ieee_single_little",
111*38fd1498Szrj floatformat_always_valid,
112*38fd1498Szrj NULL
113*38fd1498Szrj };
114*38fd1498Szrj const struct floatformat floatformat_ieee_double_big =
115*38fd1498Szrj {
116*38fd1498Szrj floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
117*38fd1498Szrj floatformat_intbit_no,
118*38fd1498Szrj "floatformat_ieee_double_big",
119*38fd1498Szrj floatformat_always_valid,
120*38fd1498Szrj NULL
121*38fd1498Szrj };
122*38fd1498Szrj const struct floatformat floatformat_ieee_double_little =
123*38fd1498Szrj {
124*38fd1498Szrj floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
125*38fd1498Szrj floatformat_intbit_no,
126*38fd1498Szrj "floatformat_ieee_double_little",
127*38fd1498Szrj floatformat_always_valid,
128*38fd1498Szrj NULL
129*38fd1498Szrj };
130*38fd1498Szrj
131*38fd1498Szrj /* floatformat for IEEE double, little endian byte order, with big endian word
132*38fd1498Szrj ordering, as on the ARM. */
133*38fd1498Szrj
134*38fd1498Szrj const struct floatformat floatformat_ieee_double_littlebyte_bigword =
135*38fd1498Szrj {
136*38fd1498Szrj floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
137*38fd1498Szrj floatformat_intbit_no,
138*38fd1498Szrj "floatformat_ieee_double_littlebyte_bigword",
139*38fd1498Szrj floatformat_always_valid,
140*38fd1498Szrj NULL
141*38fd1498Szrj };
142*38fd1498Szrj
143*38fd1498Szrj /* floatformat for VAX. Not quite IEEE, but close enough. */
144*38fd1498Szrj
145*38fd1498Szrj const struct floatformat floatformat_vax_f =
146*38fd1498Szrj {
147*38fd1498Szrj floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
148*38fd1498Szrj floatformat_intbit_no,
149*38fd1498Szrj "floatformat_vax_f",
150*38fd1498Szrj floatformat_always_valid,
151*38fd1498Szrj NULL
152*38fd1498Szrj };
153*38fd1498Szrj const struct floatformat floatformat_vax_d =
154*38fd1498Szrj {
155*38fd1498Szrj floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
156*38fd1498Szrj floatformat_intbit_no,
157*38fd1498Szrj "floatformat_vax_d",
158*38fd1498Szrj floatformat_always_valid,
159*38fd1498Szrj NULL
160*38fd1498Szrj };
161*38fd1498Szrj const struct floatformat floatformat_vax_g =
162*38fd1498Szrj {
163*38fd1498Szrj floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
164*38fd1498Szrj floatformat_intbit_no,
165*38fd1498Szrj "floatformat_vax_g",
166*38fd1498Szrj floatformat_always_valid,
167*38fd1498Szrj NULL
168*38fd1498Szrj };
169*38fd1498Szrj
170*38fd1498Szrj static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
171*38fd1498Szrj const void *from);
172*38fd1498Szrj
173*38fd1498Szrj static int
floatformat_i387_ext_is_valid(const struct floatformat * fmt,const void * from)174*38fd1498Szrj floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
175*38fd1498Szrj {
176*38fd1498Szrj /* In the i387 double-extended format, if the exponent is all ones,
177*38fd1498Szrj then the integer bit must be set. If the exponent is neither 0
178*38fd1498Szrj nor ~0, the intbit must also be set. Only if the exponent is
179*38fd1498Szrj zero can it be zero, and then it must be zero. */
180*38fd1498Szrj unsigned long exponent, int_bit;
181*38fd1498Szrj const unsigned char *ufrom = (const unsigned char *) from;
182*38fd1498Szrj
183*38fd1498Szrj exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
184*38fd1498Szrj fmt->exp_start, fmt->exp_len);
185*38fd1498Szrj int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
186*38fd1498Szrj fmt->man_start, 1);
187*38fd1498Szrj
188*38fd1498Szrj if ((exponent == 0) != (int_bit == 0))
189*38fd1498Szrj return 0;
190*38fd1498Szrj else
191*38fd1498Szrj return 1;
192*38fd1498Szrj }
193*38fd1498Szrj
194*38fd1498Szrj const struct floatformat floatformat_i387_ext =
195*38fd1498Szrj {
196*38fd1498Szrj floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
197*38fd1498Szrj floatformat_intbit_yes,
198*38fd1498Szrj "floatformat_i387_ext",
199*38fd1498Szrj floatformat_i387_ext_is_valid,
200*38fd1498Szrj NULL
201*38fd1498Szrj };
202*38fd1498Szrj const struct floatformat floatformat_m68881_ext =
203*38fd1498Szrj {
204*38fd1498Szrj /* Note that the bits from 16 to 31 are unused. */
205*38fd1498Szrj floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
206*38fd1498Szrj floatformat_intbit_yes,
207*38fd1498Szrj "floatformat_m68881_ext",
208*38fd1498Szrj floatformat_always_valid,
209*38fd1498Szrj NULL
210*38fd1498Szrj };
211*38fd1498Szrj const struct floatformat floatformat_i960_ext =
212*38fd1498Szrj {
213*38fd1498Szrj /* Note that the bits from 0 to 15 are unused. */
214*38fd1498Szrj floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
215*38fd1498Szrj floatformat_intbit_yes,
216*38fd1498Szrj "floatformat_i960_ext",
217*38fd1498Szrj floatformat_always_valid,
218*38fd1498Szrj NULL
219*38fd1498Szrj };
220*38fd1498Szrj const struct floatformat floatformat_m88110_ext =
221*38fd1498Szrj {
222*38fd1498Szrj floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
223*38fd1498Szrj floatformat_intbit_yes,
224*38fd1498Szrj "floatformat_m88110_ext",
225*38fd1498Szrj floatformat_always_valid,
226*38fd1498Szrj NULL
227*38fd1498Szrj };
228*38fd1498Szrj const struct floatformat floatformat_m88110_harris_ext =
229*38fd1498Szrj {
230*38fd1498Szrj /* Harris uses raw format 128 bytes long, but the number is just an ieee
231*38fd1498Szrj double, and the last 64 bits are wasted. */
232*38fd1498Szrj floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
233*38fd1498Szrj floatformat_intbit_no,
234*38fd1498Szrj "floatformat_m88110_ext_harris",
235*38fd1498Szrj floatformat_always_valid,
236*38fd1498Szrj NULL
237*38fd1498Szrj };
238*38fd1498Szrj const struct floatformat floatformat_arm_ext_big =
239*38fd1498Szrj {
240*38fd1498Szrj /* Bits 1 to 16 are unused. */
241*38fd1498Szrj floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
242*38fd1498Szrj floatformat_intbit_yes,
243*38fd1498Szrj "floatformat_arm_ext_big",
244*38fd1498Szrj floatformat_always_valid,
245*38fd1498Szrj NULL
246*38fd1498Szrj };
247*38fd1498Szrj const struct floatformat floatformat_arm_ext_littlebyte_bigword =
248*38fd1498Szrj {
249*38fd1498Szrj /* Bits 1 to 16 are unused. */
250*38fd1498Szrj floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
251*38fd1498Szrj floatformat_intbit_yes,
252*38fd1498Szrj "floatformat_arm_ext_littlebyte_bigword",
253*38fd1498Szrj floatformat_always_valid,
254*38fd1498Szrj NULL
255*38fd1498Szrj };
256*38fd1498Szrj const struct floatformat floatformat_ia64_spill_big =
257*38fd1498Szrj {
258*38fd1498Szrj floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
259*38fd1498Szrj floatformat_intbit_yes,
260*38fd1498Szrj "floatformat_ia64_spill_big",
261*38fd1498Szrj floatformat_always_valid,
262*38fd1498Szrj NULL
263*38fd1498Szrj };
264*38fd1498Szrj const struct floatformat floatformat_ia64_spill_little =
265*38fd1498Szrj {
266*38fd1498Szrj floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
267*38fd1498Szrj floatformat_intbit_yes,
268*38fd1498Szrj "floatformat_ia64_spill_little",
269*38fd1498Szrj floatformat_always_valid,
270*38fd1498Szrj NULL
271*38fd1498Szrj };
272*38fd1498Szrj const struct floatformat floatformat_ia64_quad_big =
273*38fd1498Szrj {
274*38fd1498Szrj floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
275*38fd1498Szrj floatformat_intbit_no,
276*38fd1498Szrj "floatformat_ia64_quad_big",
277*38fd1498Szrj floatformat_always_valid,
278*38fd1498Szrj NULL
279*38fd1498Szrj };
280*38fd1498Szrj const struct floatformat floatformat_ia64_quad_little =
281*38fd1498Szrj {
282*38fd1498Szrj floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
283*38fd1498Szrj floatformat_intbit_no,
284*38fd1498Szrj "floatformat_ia64_quad_little",
285*38fd1498Szrj floatformat_always_valid,
286*38fd1498Szrj NULL
287*38fd1498Szrj };
288*38fd1498Szrj
289*38fd1498Szrj static int
floatformat_ibm_long_double_is_valid(const struct floatformat * fmt,const void * from)290*38fd1498Szrj floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
291*38fd1498Szrj const void *from)
292*38fd1498Szrj {
293*38fd1498Szrj const unsigned char *ufrom = (const unsigned char *) from;
294*38fd1498Szrj const struct floatformat *hfmt = fmt->split_half;
295*38fd1498Szrj long top_exp, bot_exp;
296*38fd1498Szrj int top_nan = 0;
297*38fd1498Szrj
298*38fd1498Szrj top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
299*38fd1498Szrj hfmt->exp_start, hfmt->exp_len);
300*38fd1498Szrj bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
301*38fd1498Szrj hfmt->exp_start, hfmt->exp_len);
302*38fd1498Szrj
303*38fd1498Szrj if ((unsigned long) top_exp == hfmt->exp_nan)
304*38fd1498Szrj top_nan = mant_bits_set (hfmt, ufrom);
305*38fd1498Szrj
306*38fd1498Szrj /* A NaN is valid with any low part. */
307*38fd1498Szrj if (top_nan)
308*38fd1498Szrj return 1;
309*38fd1498Szrj
310*38fd1498Szrj /* An infinity, zero or denormal requires low part 0 (positive or
311*38fd1498Szrj negative). */
312*38fd1498Szrj if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
313*38fd1498Szrj {
314*38fd1498Szrj if (bot_exp != 0)
315*38fd1498Szrj return 0;
316*38fd1498Szrj
317*38fd1498Szrj return !mant_bits_set (hfmt, ufrom + 8);
318*38fd1498Szrj }
319*38fd1498Szrj
320*38fd1498Szrj /* The top part is now a finite normal value. The long double value
321*38fd1498Szrj is the sum of the two parts, and the top part must equal the
322*38fd1498Szrj result of rounding the long double value to nearest double. Thus
323*38fd1498Szrj the bottom part must be <= 0.5ulp of the top part in absolute
324*38fd1498Szrj value, and if it is < 0.5ulp then the long double is definitely
325*38fd1498Szrj valid. */
326*38fd1498Szrj if (bot_exp < top_exp - 53)
327*38fd1498Szrj return 1;
328*38fd1498Szrj if (bot_exp > top_exp - 53 && bot_exp != 0)
329*38fd1498Szrj return 0;
330*38fd1498Szrj if (bot_exp == 0)
331*38fd1498Szrj {
332*38fd1498Szrj /* The bottom part is 0 or denormal. Determine which, and if
333*38fd1498Szrj denormal the first two set bits. */
334*38fd1498Szrj int first_bit = -1, second_bit = -1, cur_bit;
335*38fd1498Szrj for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
336*38fd1498Szrj if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
337*38fd1498Szrj hfmt->man_start + cur_bit, 1))
338*38fd1498Szrj {
339*38fd1498Szrj if (first_bit == -1)
340*38fd1498Szrj first_bit = cur_bit;
341*38fd1498Szrj else
342*38fd1498Szrj {
343*38fd1498Szrj second_bit = cur_bit;
344*38fd1498Szrj break;
345*38fd1498Szrj }
346*38fd1498Szrj }
347*38fd1498Szrj /* Bottom part 0 is OK. */
348*38fd1498Szrj if (first_bit == -1)
349*38fd1498Szrj return 1;
350*38fd1498Szrj /* The real exponent of the bottom part is -first_bit. */
351*38fd1498Szrj if (-first_bit < top_exp - 53)
352*38fd1498Szrj return 1;
353*38fd1498Szrj if (-first_bit > top_exp - 53)
354*38fd1498Szrj return 0;
355*38fd1498Szrj /* The bottom part is at least 0.5ulp of the top part. For this
356*38fd1498Szrj to be OK, the bottom part must be exactly 0.5ulp (i.e. no
357*38fd1498Szrj more bits set) and the top part must have last bit 0. */
358*38fd1498Szrj if (second_bit != -1)
359*38fd1498Szrj return 0;
360*38fd1498Szrj return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
361*38fd1498Szrj hfmt->man_start + hfmt->man_len - 1, 1);
362*38fd1498Szrj }
363*38fd1498Szrj else
364*38fd1498Szrj {
365*38fd1498Szrj /* The bottom part is at least 0.5ulp of the top part. For this
366*38fd1498Szrj to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
367*38fd1498Szrj set) and the top part must have last bit 0. */
368*38fd1498Szrj if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
369*38fd1498Szrj hfmt->man_start + hfmt->man_len - 1, 1))
370*38fd1498Szrj return 0;
371*38fd1498Szrj return !mant_bits_set (hfmt, ufrom + 8);
372*38fd1498Szrj }
373*38fd1498Szrj }
374*38fd1498Szrj
375*38fd1498Szrj const struct floatformat floatformat_ibm_long_double_big =
376*38fd1498Szrj {
377*38fd1498Szrj floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
378*38fd1498Szrj floatformat_intbit_no,
379*38fd1498Szrj "floatformat_ibm_long_double_big",
380*38fd1498Szrj floatformat_ibm_long_double_is_valid,
381*38fd1498Szrj &floatformat_ieee_double_big
382*38fd1498Szrj };
383*38fd1498Szrj
384*38fd1498Szrj const struct floatformat floatformat_ibm_long_double_little =
385*38fd1498Szrj {
386*38fd1498Szrj floatformat_little, 128, 0, 1, 11, 1023, 2047, 12, 52,
387*38fd1498Szrj floatformat_intbit_no,
388*38fd1498Szrj "floatformat_ibm_long_double_little",
389*38fd1498Szrj floatformat_ibm_long_double_is_valid,
390*38fd1498Szrj &floatformat_ieee_double_little
391*38fd1498Szrj };
392*38fd1498Szrj
393*38fd1498Szrj
394*38fd1498Szrj #ifndef min
395*38fd1498Szrj #define min(a, b) ((a) < (b) ? (a) : (b))
396*38fd1498Szrj #endif
397*38fd1498Szrj
398*38fd1498Szrj /* Return 1 if any bits are explicitly set in the mantissa of UFROM,
399*38fd1498Szrj format FMT, 0 otherwise. */
400*38fd1498Szrj static int
mant_bits_set(const struct floatformat * fmt,const unsigned char * ufrom)401*38fd1498Szrj mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
402*38fd1498Szrj {
403*38fd1498Szrj unsigned int mant_bits, mant_off;
404*38fd1498Szrj int mant_bits_left;
405*38fd1498Szrj
406*38fd1498Szrj mant_off = fmt->man_start;
407*38fd1498Szrj mant_bits_left = fmt->man_len;
408*38fd1498Szrj while (mant_bits_left > 0)
409*38fd1498Szrj {
410*38fd1498Szrj mant_bits = min (mant_bits_left, 32);
411*38fd1498Szrj
412*38fd1498Szrj if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
413*38fd1498Szrj mant_off, mant_bits) != 0)
414*38fd1498Szrj return 1;
415*38fd1498Szrj
416*38fd1498Szrj mant_off += mant_bits;
417*38fd1498Szrj mant_bits_left -= mant_bits;
418*38fd1498Szrj }
419*38fd1498Szrj return 0;
420*38fd1498Szrj }
421*38fd1498Szrj
422*38fd1498Szrj /* Extract a field which starts at START and is LEN bits long. DATA and
423*38fd1498Szrj TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
424*38fd1498Szrj static unsigned long
get_field(const unsigned char * data,enum floatformat_byteorders order,unsigned int total_len,unsigned int start,unsigned int len)425*38fd1498Szrj get_field (const unsigned char *data, enum floatformat_byteorders order,
426*38fd1498Szrj unsigned int total_len, unsigned int start, unsigned int len)
427*38fd1498Szrj {
428*38fd1498Szrj unsigned long result = 0;
429*38fd1498Szrj unsigned int cur_byte;
430*38fd1498Szrj int lo_bit, hi_bit, cur_bitshift = 0;
431*38fd1498Szrj int nextbyte = (order == floatformat_little) ? 1 : -1;
432*38fd1498Szrj
433*38fd1498Szrj /* Start is in big-endian bit order! Fix that first. */
434*38fd1498Szrj start = total_len - (start + len);
435*38fd1498Szrj
436*38fd1498Szrj /* Start at the least significant part of the field. */
437*38fd1498Szrj if (order == floatformat_little)
438*38fd1498Szrj cur_byte = start / FLOATFORMAT_CHAR_BIT;
439*38fd1498Szrj else
440*38fd1498Szrj cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
441*38fd1498Szrj
442*38fd1498Szrj lo_bit = start % FLOATFORMAT_CHAR_BIT;
443*38fd1498Szrj hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
444*38fd1498Szrj
445*38fd1498Szrj do
446*38fd1498Szrj {
447*38fd1498Szrj unsigned int shifted = *(data + cur_byte) >> lo_bit;
448*38fd1498Szrj unsigned int bits = hi_bit - lo_bit;
449*38fd1498Szrj unsigned int mask = (1 << bits) - 1;
450*38fd1498Szrj result |= (shifted & mask) << cur_bitshift;
451*38fd1498Szrj len -= bits;
452*38fd1498Szrj cur_bitshift += bits;
453*38fd1498Szrj cur_byte += nextbyte;
454*38fd1498Szrj lo_bit = 0;
455*38fd1498Szrj hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
456*38fd1498Szrj }
457*38fd1498Szrj while (len != 0);
458*38fd1498Szrj
459*38fd1498Szrj return result;
460*38fd1498Szrj }
461*38fd1498Szrj
462*38fd1498Szrj /* Convert from FMT to a double.
463*38fd1498Szrj FROM is the address of the extended float.
464*38fd1498Szrj Store the double in *TO. */
465*38fd1498Szrj
466*38fd1498Szrj void
floatformat_to_double(const struct floatformat * fmt,const void * from,double * to)467*38fd1498Szrj floatformat_to_double (const struct floatformat *fmt,
468*38fd1498Szrj const void *from, double *to)
469*38fd1498Szrj {
470*38fd1498Szrj const unsigned char *ufrom = (const unsigned char *) from;
471*38fd1498Szrj double dto;
472*38fd1498Szrj long exponent;
473*38fd1498Szrj unsigned long mant;
474*38fd1498Szrj unsigned int mant_bits, mant_off;
475*38fd1498Szrj int mant_bits_left;
476*38fd1498Szrj
477*38fd1498Szrj /* Split values are not handled specially, since the top half has
478*38fd1498Szrj the correctly rounded double value (in the only supported case of
479*38fd1498Szrj split values). */
480*38fd1498Szrj
481*38fd1498Szrj exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
482*38fd1498Szrj fmt->exp_start, fmt->exp_len);
483*38fd1498Szrj
484*38fd1498Szrj /* If the exponent indicates a NaN, we don't have information to
485*38fd1498Szrj decide what to do. So we handle it like IEEE, except that we
486*38fd1498Szrj don't try to preserve the type of NaN. FIXME. */
487*38fd1498Szrj if ((unsigned long) exponent == fmt->exp_nan)
488*38fd1498Szrj {
489*38fd1498Szrj int nan = mant_bits_set (fmt, ufrom);
490*38fd1498Szrj
491*38fd1498Szrj /* On certain systems (such as GNU/Linux), the use of the
492*38fd1498Szrj INFINITY macro below may generate a warning that can not be
493*38fd1498Szrj silenced due to a bug in GCC (PR preprocessor/11931). The
494*38fd1498Szrj preprocessor fails to recognise the __extension__ keyword in
495*38fd1498Szrj conjunction with the GNU/C99 extension for hexadecimal
496*38fd1498Szrj floating point constants and will issue a warning when
497*38fd1498Szrj compiling with -pedantic. */
498*38fd1498Szrj if (nan)
499*38fd1498Szrj dto = NAN;
500*38fd1498Szrj else
501*38fd1498Szrj dto = INFINITY;
502*38fd1498Szrj
503*38fd1498Szrj if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
504*38fd1498Szrj dto = -dto;
505*38fd1498Szrj
506*38fd1498Szrj *to = dto;
507*38fd1498Szrj
508*38fd1498Szrj return;
509*38fd1498Szrj }
510*38fd1498Szrj
511*38fd1498Szrj mant_bits_left = fmt->man_len;
512*38fd1498Szrj mant_off = fmt->man_start;
513*38fd1498Szrj dto = 0.0;
514*38fd1498Szrj
515*38fd1498Szrj /* Build the result algebraically. Might go infinite, underflow, etc;
516*38fd1498Szrj who cares. */
517*38fd1498Szrj
518*38fd1498Szrj /* For denorms use minimum exponent. */
519*38fd1498Szrj if (exponent == 0)
520*38fd1498Szrj exponent = 1 - fmt->exp_bias;
521*38fd1498Szrj else
522*38fd1498Szrj {
523*38fd1498Szrj exponent -= fmt->exp_bias;
524*38fd1498Szrj
525*38fd1498Szrj /* If this format uses a hidden bit, explicitly add it in now.
526*38fd1498Szrj Otherwise, increment the exponent by one to account for the
527*38fd1498Szrj integer bit. */
528*38fd1498Szrj
529*38fd1498Szrj if (fmt->intbit == floatformat_intbit_no)
530*38fd1498Szrj dto = ldexp (1.0, exponent);
531*38fd1498Szrj else
532*38fd1498Szrj exponent++;
533*38fd1498Szrj }
534*38fd1498Szrj
535*38fd1498Szrj while (mant_bits_left > 0)
536*38fd1498Szrj {
537*38fd1498Szrj mant_bits = min (mant_bits_left, 32);
538*38fd1498Szrj
539*38fd1498Szrj mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
540*38fd1498Szrj mant_off, mant_bits);
541*38fd1498Szrj
542*38fd1498Szrj dto += ldexp ((double) mant, exponent - mant_bits);
543*38fd1498Szrj exponent -= mant_bits;
544*38fd1498Szrj mant_off += mant_bits;
545*38fd1498Szrj mant_bits_left -= mant_bits;
546*38fd1498Szrj }
547*38fd1498Szrj
548*38fd1498Szrj /* Negate it if negative. */
549*38fd1498Szrj if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
550*38fd1498Szrj dto = -dto;
551*38fd1498Szrj *to = dto;
552*38fd1498Szrj }
553*38fd1498Szrj
554*38fd1498Szrj static void put_field (unsigned char *, enum floatformat_byteorders,
555*38fd1498Szrj unsigned int,
556*38fd1498Szrj unsigned int,
557*38fd1498Szrj unsigned int,
558*38fd1498Szrj unsigned long);
559*38fd1498Szrj
560*38fd1498Szrj /* Set a field which starts at START and is LEN bits long. DATA and
561*38fd1498Szrj TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
562*38fd1498Szrj static void
put_field(unsigned char * data,enum floatformat_byteorders order,unsigned int total_len,unsigned int start,unsigned int len,unsigned long stuff_to_put)563*38fd1498Szrj put_field (unsigned char *data, enum floatformat_byteorders order,
564*38fd1498Szrj unsigned int total_len, unsigned int start, unsigned int len,
565*38fd1498Szrj unsigned long stuff_to_put)
566*38fd1498Szrj {
567*38fd1498Szrj unsigned int cur_byte;
568*38fd1498Szrj int lo_bit, hi_bit;
569*38fd1498Szrj int nextbyte = (order == floatformat_little) ? 1 : -1;
570*38fd1498Szrj
571*38fd1498Szrj /* Start is in big-endian bit order! Fix that first. */
572*38fd1498Szrj start = total_len - (start + len);
573*38fd1498Szrj
574*38fd1498Szrj /* Start at the least significant part of the field. */
575*38fd1498Szrj if (order == floatformat_little)
576*38fd1498Szrj cur_byte = start / FLOATFORMAT_CHAR_BIT;
577*38fd1498Szrj else
578*38fd1498Szrj cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
579*38fd1498Szrj
580*38fd1498Szrj lo_bit = start % FLOATFORMAT_CHAR_BIT;
581*38fd1498Szrj hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
582*38fd1498Szrj
583*38fd1498Szrj do
584*38fd1498Szrj {
585*38fd1498Szrj unsigned char *byte_ptr = data + cur_byte;
586*38fd1498Szrj unsigned int bits = hi_bit - lo_bit;
587*38fd1498Szrj unsigned int mask = ((1 << bits) - 1) << lo_bit;
588*38fd1498Szrj *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
589*38fd1498Szrj stuff_to_put >>= bits;
590*38fd1498Szrj len -= bits;
591*38fd1498Szrj cur_byte += nextbyte;
592*38fd1498Szrj lo_bit = 0;
593*38fd1498Szrj hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
594*38fd1498Szrj }
595*38fd1498Szrj while (len != 0);
596*38fd1498Szrj }
597*38fd1498Szrj
598*38fd1498Szrj /* The converse: convert the double *FROM to an extended float
599*38fd1498Szrj and store where TO points. Neither FROM nor TO have any alignment
600*38fd1498Szrj restrictions. */
601*38fd1498Szrj
602*38fd1498Szrj void
floatformat_from_double(const struct floatformat * fmt,const double * from,void * to)603*38fd1498Szrj floatformat_from_double (const struct floatformat *fmt,
604*38fd1498Szrj const double *from, void *to)
605*38fd1498Szrj {
606*38fd1498Szrj double dfrom;
607*38fd1498Szrj int exponent;
608*38fd1498Szrj double mant;
609*38fd1498Szrj unsigned int mant_bits, mant_off;
610*38fd1498Szrj int mant_bits_left;
611*38fd1498Szrj unsigned char *uto = (unsigned char *) to;
612*38fd1498Szrj
613*38fd1498Szrj dfrom = *from;
614*38fd1498Szrj memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
615*38fd1498Szrj
616*38fd1498Szrj /* Split values are not handled specially, since a bottom half of
617*38fd1498Szrj zero is correct for any value representable as double (in the
618*38fd1498Szrj only supported case of split values). */
619*38fd1498Szrj
620*38fd1498Szrj /* If negative, set the sign bit. */
621*38fd1498Szrj if (dfrom < 0)
622*38fd1498Szrj {
623*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
624*38fd1498Szrj dfrom = -dfrom;
625*38fd1498Szrj }
626*38fd1498Szrj
627*38fd1498Szrj if (dfrom == 0)
628*38fd1498Szrj {
629*38fd1498Szrj /* 0.0. */
630*38fd1498Szrj return;
631*38fd1498Szrj }
632*38fd1498Szrj
633*38fd1498Szrj if (dfrom != dfrom)
634*38fd1498Szrj {
635*38fd1498Szrj /* NaN. */
636*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
637*38fd1498Szrj fmt->exp_len, fmt->exp_nan);
638*38fd1498Szrj /* Be sure it's not infinity, but NaN value is irrelevant. */
639*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
640*38fd1498Szrj 32, 1);
641*38fd1498Szrj return;
642*38fd1498Szrj }
643*38fd1498Szrj
644*38fd1498Szrj if (dfrom + dfrom == dfrom)
645*38fd1498Szrj {
646*38fd1498Szrj /* This can only happen for an infinite value (or zero, which we
647*38fd1498Szrj already handled above). */
648*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
649*38fd1498Szrj fmt->exp_len, fmt->exp_nan);
650*38fd1498Szrj return;
651*38fd1498Szrj }
652*38fd1498Szrj
653*38fd1498Szrj mant = frexp (dfrom, &exponent);
654*38fd1498Szrj if (exponent + fmt->exp_bias - 1 > 0)
655*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
656*38fd1498Szrj fmt->exp_len, exponent + fmt->exp_bias - 1);
657*38fd1498Szrj else
658*38fd1498Szrj {
659*38fd1498Szrj /* Handle a denormalized number. FIXME: What should we do for
660*38fd1498Szrj non-IEEE formats? */
661*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
662*38fd1498Szrj fmt->exp_len, 0);
663*38fd1498Szrj mant = ldexp (mant, exponent + fmt->exp_bias - 1);
664*38fd1498Szrj }
665*38fd1498Szrj
666*38fd1498Szrj mant_bits_left = fmt->man_len;
667*38fd1498Szrj mant_off = fmt->man_start;
668*38fd1498Szrj while (mant_bits_left > 0)
669*38fd1498Szrj {
670*38fd1498Szrj unsigned long mant_long;
671*38fd1498Szrj mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
672*38fd1498Szrj
673*38fd1498Szrj mant *= 4294967296.0;
674*38fd1498Szrj mant_long = (unsigned long)mant;
675*38fd1498Szrj mant -= mant_long;
676*38fd1498Szrj
677*38fd1498Szrj /* If the integer bit is implicit, and we are not creating a
678*38fd1498Szrj denormalized number, then we need to discard it. */
679*38fd1498Szrj if ((unsigned int) mant_bits_left == fmt->man_len
680*38fd1498Szrj && fmt->intbit == floatformat_intbit_no
681*38fd1498Szrj && exponent + fmt->exp_bias - 1 > 0)
682*38fd1498Szrj {
683*38fd1498Szrj mant_long &= 0x7fffffff;
684*38fd1498Szrj mant_bits -= 1;
685*38fd1498Szrj }
686*38fd1498Szrj else if (mant_bits < 32)
687*38fd1498Szrj {
688*38fd1498Szrj /* The bits we want are in the most significant MANT_BITS bits of
689*38fd1498Szrj mant_long. Move them to the least significant. */
690*38fd1498Szrj mant_long >>= 32 - mant_bits;
691*38fd1498Szrj }
692*38fd1498Szrj
693*38fd1498Szrj put_field (uto, fmt->byteorder, fmt->totalsize,
694*38fd1498Szrj mant_off, mant_bits, mant_long);
695*38fd1498Szrj mant_off += mant_bits;
696*38fd1498Szrj mant_bits_left -= mant_bits;
697*38fd1498Szrj }
698*38fd1498Szrj }
699*38fd1498Szrj
700*38fd1498Szrj /* Return non-zero iff the data at FROM is a valid number in format FMT. */
701*38fd1498Szrj
702*38fd1498Szrj int
floatformat_is_valid(const struct floatformat * fmt,const void * from)703*38fd1498Szrj floatformat_is_valid (const struct floatformat *fmt, const void *from)
704*38fd1498Szrj {
705*38fd1498Szrj return fmt->is_valid (fmt, from);
706*38fd1498Szrj }
707*38fd1498Szrj
708*38fd1498Szrj
709*38fd1498Szrj #ifdef IEEE_DEBUG
710*38fd1498Szrj
711*38fd1498Szrj #include <stdio.h>
712*38fd1498Szrj
713*38fd1498Szrj /* This is to be run on a host which uses IEEE floating point. */
714*38fd1498Szrj
715*38fd1498Szrj void
ieee_test(double n)716*38fd1498Szrj ieee_test (double n)
717*38fd1498Szrj {
718*38fd1498Szrj double result;
719*38fd1498Szrj
720*38fd1498Szrj floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
721*38fd1498Szrj if ((n != result && (! isnan (n) || ! isnan (result)))
722*38fd1498Szrj || (n < 0 && result >= 0)
723*38fd1498Szrj || (n >= 0 && result < 0))
724*38fd1498Szrj printf ("Differ(to): %.20g -> %.20g\n", n, result);
725*38fd1498Szrj
726*38fd1498Szrj floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
727*38fd1498Szrj if ((n != result && (! isnan (n) || ! isnan (result)))
728*38fd1498Szrj || (n < 0 && result >= 0)
729*38fd1498Szrj || (n >= 0 && result < 0))
730*38fd1498Szrj printf ("Differ(from): %.20g -> %.20g\n", n, result);
731*38fd1498Szrj
732*38fd1498Szrj #if 0
733*38fd1498Szrj {
734*38fd1498Szrj char exten[16];
735*38fd1498Szrj
736*38fd1498Szrj floatformat_from_double (&floatformat_m68881_ext, &n, exten);
737*38fd1498Szrj floatformat_to_double (&floatformat_m68881_ext, exten, &result);
738*38fd1498Szrj if (n != result)
739*38fd1498Szrj printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
740*38fd1498Szrj }
741*38fd1498Szrj #endif
742*38fd1498Szrj
743*38fd1498Szrj #if IEEE_DEBUG > 1
744*38fd1498Szrj /* This is to be run on a host which uses 68881 format. */
745*38fd1498Szrj {
746*38fd1498Szrj long double ex = *(long double *)exten;
747*38fd1498Szrj if (ex != n)
748*38fd1498Szrj printf ("Differ(from vs. extended): %.20g\n", n);
749*38fd1498Szrj }
750*38fd1498Szrj #endif
751*38fd1498Szrj }
752*38fd1498Szrj
753*38fd1498Szrj int
main(void)754*38fd1498Szrj main (void)
755*38fd1498Szrj {
756*38fd1498Szrj ieee_test (0.0);
757*38fd1498Szrj ieee_test (0.5);
758*38fd1498Szrj ieee_test (1.1);
759*38fd1498Szrj ieee_test (256.0);
760*38fd1498Szrj ieee_test (0.12345);
761*38fd1498Szrj ieee_test (234235.78907234);
762*38fd1498Szrj ieee_test (-512.0);
763*38fd1498Szrj ieee_test (-0.004321);
764*38fd1498Szrj ieee_test (1.2E-70);
765*38fd1498Szrj ieee_test (1.2E-316);
766*38fd1498Szrj ieee_test (4.9406564584124654E-324);
767*38fd1498Szrj ieee_test (- 4.9406564584124654E-324);
768*38fd1498Szrj ieee_test (- 0.0);
769*38fd1498Szrj ieee_test (- INFINITY);
770*38fd1498Szrj ieee_test (- NAN);
771*38fd1498Szrj ieee_test (INFINITY);
772*38fd1498Szrj ieee_test (NAN);
773*38fd1498Szrj return 0;
774*38fd1498Szrj }
775*38fd1498Szrj #endif
776