* defs.h: Incorporate param.h. All users changed.
[deliverable/binutils-gdb.git] / gdb / ieee-float.c
CommitLineData
dd3b648e
RP
1/* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
99a7de40 6This program is free software; you can redistribute it and/or modify
dd3b648e 7it under the terms of the GNU General Public License as published by
99a7de40
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
dd3b648e 10
99a7de40 11This program is distributed in the hope that it will be useful,
dd3b648e
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
99a7de40
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
dd3b648e
RP
19
20#include "defs.h"
dd3b648e
RP
21#include "ieee-float.h"
22#include <math.h> /* ldexp */
23
24/* Convert an IEEE extended float to a double.
25 FROM is the address of the extended float.
26 Store the double in *TO. */
27
28void
29ieee_extended_to_double (ext_format, from, to)
30 struct ext_format *ext_format;
31 char *from;
32 double *to;
33{
34 unsigned char *ufrom = (unsigned char *)from;
35 double dto;
e1ce8aa5 36 unsigned long mant0, mant1, exponent;
dd3b648e
RP
37
38 bcopy (&from[MANBYTE_H], &mant0, 4);
39 bcopy (&from[MANBYTE_L], &mant1, 4);
e1ce8aa5 40 exponent = ((ufrom[EXPBYTE_H] & (unsigned char)~SIGNMASK) << 8) | ufrom[EXPBYTE_L];
dd3b648e 41
3f2e006b
JG
42#if 0
43 /* We can't do anything useful with a NaN anyway, so ignore its
44 difference. It will end up as Infinity or something close. */
e1ce8aa5 45 if (exponent == EXT_EXP_NAN) {
dd3b648e 46 /* We have a NaN source. */
3f2e006b
JG
47 dto = 0.123456789; /* Not much else useful to do -- we don't know if
48 the host system even *has* NaNs, nor how to
49 generate an innocuous one if it does. */
50 } else
51#endif
e1ce8aa5 52 if (exponent == 0 && mant0 == 0 && mant1 == 0) {
dd3b648e
RP
53 dto = 0;
54 } else {
55 /* Build the result algebraically. Might go infinite, underflow, etc;
56 who cares. */
57 mant0 |= 0x80000000;
e1ce8aa5
JK
58 dto = ldexp ((double)mant0, exponent - EXT_EXP_BIAS - 31);
59 dto += ldexp ((double)mant1, exponent - EXT_EXP_BIAS - 31 - 32);
3f2e006b
JG
60 if (ufrom[EXPBYTE_H] & SIGNMASK) /* If negative... */
61 dto = -dto; /* ...negate. */
dd3b648e
RP
62 }
63 *to = dto;
64}
65
66/* The converse: convert the double *FROM to an extended float
67 and store where TO points. */
68
69void
70double_to_ieee_extended (ext_format, from, to)
71 struct ext_format *ext_format;
72 double *from;
73 char *to;
74{
75 double dfrom = *from;
76 unsigned long twolongs[2];
e1ce8aa5 77 unsigned long mant0, mant1, exponent;
5c9878f1 78 unsigned char tobytes[8];
dd3b648e
RP
79
80 bzero (to, TOTALSIZE);
81 if (dfrom == 0)
82 return; /* Result is zero */
83 if (dfrom != dfrom) {
84 /* From is NaN */
85 to[EXPBYTE_H] = (unsigned char)(EXT_EXP_NAN >> 8);
86 to[EXPBYTE_L] = (unsigned char)EXT_EXP_NAN;
87 to[MANBYTE_H] = 1; /* Be sure it's not infinity, but NaN value is irrel */
88 return; /* Result is NaN */
89 }
90 if (dfrom < 0)
91 to[SIGNBYTE] |= SIGNMASK; /* Set negative sign */
92 /* How to tell an infinity from an ordinary number? FIXME-someday */
93
94 /* The following code assumes that the host has IEEE doubles. FIXME-someday.
95 It also assumes longs are 32 bits! FIXME-someday. */
96 bcopy (from, twolongs, 8);
5c9878f1 97 bcopy (from, tobytes, 8);
dd3b648e 98#if HOST_BYTE_ORDER == BIG_ENDIAN
5c9878f1 99 exponent = ((tobytes[1] & 0xF0) >> 4) | (tobytes[0] & 0x7F) << 4;
dd3b648e
RP
100 mant0 = (twolongs[0] << 11) | twolongs[1] >> 21;
101 mant1 = (twolongs[1] << 11);
102#else
5c9878f1 103 exponent = ((tobytes[6] & 0xF0) >> 4) | (tobytes[7] & 0x7F) << 4;
dd3b648e
RP
104 mant0 = (twolongs[1] << 11) | twolongs[0] >> 21;
105 mant1 = (twolongs[0] << 11);
106#endif
107
108 /* Fiddle with leading 1-bit, implied in double, explicit in extended. */
e1ce8aa5 109 if (exponent == 0)
dd3b648e
RP
110 mant0 &= 0x7FFFFFFF;
111 else
112 mant0 |= 0x80000000;
113
e1ce8aa5
JK
114 exponent -= DBL_EXP_BIAS; /* Get integer exp */
115 exponent += EXT_EXP_BIAS; /* Offset for extended */
dd3b648e
RP
116
117 /* OK, now store it in extended format. */
e1ce8aa5
JK
118 to[EXPBYTE_H] |= (unsigned char)(exponent >> 8); /* Retain sign */
119 to[EXPBYTE_L] = (unsigned char) exponent;
dd3b648e
RP
120
121 bcopy (&mant0, &to[MANBYTE_H], 4);
122 bcopy (&mant1, &to[MANBYTE_L], 4);
123}
124
125
126#ifdef DEBUG
127
128/* Test some numbers to see that extended/double conversion works for them. */
129
130ieee_test (n)
131 int n;
132{
133 union { double d; int i[2]; } di;
134 double result;
135 int i;
136 char exten[16];
137 extern struct ext_format ext_format_68881;
138
139 for (i = 0; i < n; i++) {
3f2e006b
JG
140 di.i[0] = (random() << 16) | (random() & 0xffff);
141 di.i[1] = (random() << 16) | (random() & 0xffff);
142 double_to_ieee_extended (&ext_format_68881, &di.d, exten);
143 ieee_extended_to_double (&ext_format_68881, exten, &result);
dd3b648e
RP
144 if (di.d != result)
145 printf ("Differ: %x %x %g => %x %x %g\n", di.d, di.d, result, result);
146 }
147}
148
149#endif
This page took 0.110773 seconds and 4 git commands to generate.