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