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