* configure.in (mips-big-* target): Same as Sony News.
[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 19
088c3a0b
JG
20#include <stdio.h>
21
dd3b648e 22#include "defs.h"
dd3b648e
RP
23#include "ieee-float.h"
24#include <math.h> /* ldexp */
25
26/* Convert an IEEE extended float to a double.
27 FROM is the address of the extended float.
28 Store the double in *TO. */
29
30void
31ieee_extended_to_double (ext_format, from, to)
088c3a0b 32 const struct ext_format *ext_format;
dd3b648e
RP
33 char *from;
34 double *to;
35{
36 unsigned char *ufrom = (unsigned char *)from;
37 double dto;
e1ce8aa5 38 unsigned long mant0, mant1, exponent;
dd3b648e 39
088c3a0b
JG
40 memcpy (&mant0, &from[MANBYTE_H], 4);
41 memcpy (&mant1, &from[MANBYTE_L], 4);
e1ce8aa5 42 exponent = ((ufrom[EXPBYTE_H] & (unsigned char)~SIGNMASK) << 8) | ufrom[EXPBYTE_L];
dd3b648e 43
3f2e006b
JG
44#if 0
45 /* We can't do anything useful with a NaN anyway, so ignore its
46 difference. It will end up as Infinity or something close. */
e1ce8aa5 47 if (exponent == EXT_EXP_NAN) {
dd3b648e 48 /* We have a NaN source. */
3f2e006b
JG
49 dto = 0.123456789; /* Not much else useful to do -- we don't know if
50 the host system even *has* NaNs, nor how to
51 generate an innocuous one if it does. */
52 } else
53#endif
e1ce8aa5 54 if (exponent == 0 && mant0 == 0 && mant1 == 0) {
dd3b648e
RP
55 dto = 0;
56 } else {
57 /* Build the result algebraically. Might go infinite, underflow, etc;
58 who cares. */
59 mant0 |= 0x80000000;
e1ce8aa5
JK
60 dto = ldexp ((double)mant0, exponent - EXT_EXP_BIAS - 31);
61 dto += ldexp ((double)mant1, exponent - EXT_EXP_BIAS - 31 - 32);
3f2e006b
JG
62 if (ufrom[EXPBYTE_H] & SIGNMASK) /* If negative... */
63 dto = -dto; /* ...negate. */
dd3b648e
RP
64 }
65 *to = dto;
66}
67
68/* The converse: convert the double *FROM to an extended float
69 and store where TO points. */
70
71void
72double_to_ieee_extended (ext_format, from, to)
088c3a0b 73 const struct ext_format *ext_format;
dd3b648e
RP
74 double *from;
75 char *to;
76{
77 double dfrom = *from;
78 unsigned long twolongs[2];
e1ce8aa5 79 unsigned long mant0, mant1, exponent;
5c9878f1 80 unsigned char tobytes[8];
dd3b648e
RP
81
82 bzero (to, TOTALSIZE);
83 if (dfrom == 0)
84 return; /* Result is zero */
85 if (dfrom != dfrom) {
86 /* From is NaN */
87 to[EXPBYTE_H] = (unsigned char)(EXT_EXP_NAN >> 8);
88 to[EXPBYTE_L] = (unsigned char)EXT_EXP_NAN;
89 to[MANBYTE_H] = 1; /* Be sure it's not infinity, but NaN value is irrel */
90 return; /* Result is NaN */
91 }
92 if (dfrom < 0)
93 to[SIGNBYTE] |= SIGNMASK; /* Set negative sign */
94 /* How to tell an infinity from an ordinary number? FIXME-someday */
95
96 /* The following code assumes that the host has IEEE doubles. FIXME-someday.
97 It also assumes longs are 32 bits! FIXME-someday. */
088c3a0b
JG
98 memcpy (twolongs, from, 8);
99 memcpy (tobytes, from, 8);
dd3b648e 100#if HOST_BYTE_ORDER == BIG_ENDIAN
5c9878f1 101 exponent = ((tobytes[1] & 0xF0) >> 4) | (tobytes[0] & 0x7F) << 4;
dd3b648e
RP
102 mant0 = (twolongs[0] << 11) | twolongs[1] >> 21;
103 mant1 = (twolongs[1] << 11);
104#else
5c9878f1 105 exponent = ((tobytes[6] & 0xF0) >> 4) | (tobytes[7] & 0x7F) << 4;
dd3b648e
RP
106 mant0 = (twolongs[1] << 11) | twolongs[0] >> 21;
107 mant1 = (twolongs[0] << 11);
108#endif
109
110 /* Fiddle with leading 1-bit, implied in double, explicit in extended. */
e1ce8aa5 111 if (exponent == 0)
dd3b648e
RP
112 mant0 &= 0x7FFFFFFF;
113 else
114 mant0 |= 0x80000000;
115
e1ce8aa5
JK
116 exponent -= DBL_EXP_BIAS; /* Get integer exp */
117 exponent += EXT_EXP_BIAS; /* Offset for extended */
dd3b648e
RP
118
119 /* OK, now store it in extended format. */
e1ce8aa5
JK
120 to[EXPBYTE_H] |= (unsigned char)(exponent >> 8); /* Retain sign */
121 to[EXPBYTE_L] = (unsigned char) exponent;
dd3b648e 122
088c3a0b
JG
123 memcpy (&to[MANBYTE_H], &mant0, 4);
124 memcpy (&to[MANBYTE_L], &mant1, 4);
dd3b648e
RP
125}
126
127
b440b1e9 128#ifdef IEEE_DEBUG
dd3b648e
RP
129
130/* Test some numbers to see that extended/double conversion works for them. */
131
132ieee_test (n)
133 int n;
134{
135 union { double d; int i[2]; } di;
136 double result;
137 int i;
138 char exten[16];
139 extern struct ext_format ext_format_68881;
140
141 for (i = 0; i < n; i++) {
3f2e006b
JG
142 di.i[0] = (random() << 16) | (random() & 0xffff);
143 di.i[1] = (random() << 16) | (random() & 0xffff);
144 double_to_ieee_extended (&ext_format_68881, &di.d, exten);
145 ieee_extended_to_double (&ext_format_68881, exten, &result);
dd3b648e
RP
146 if (di.d != result)
147 printf ("Differ: %x %x %g => %x %x %g\n", di.d, di.d, result, result);
148 }
149}
150
151#endif
This page took 0.052571 seconds and 4 git commands to generate.