1991-03-28 17:28:29 +01:00
|
|
|
/* IEEE floating point support routines, for GDB, the GNU Debugger.
|
|
|
|
Copyright (C) 1991 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
1991-06-04 09:31:55 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
1991-03-28 17:28:29 +01:00
|
|
|
it under the terms of the GNU General Public License as published by
|
1991-06-04 09:31:55 +02:00
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
1991-03-28 17:28:29 +01:00
|
|
|
|
1991-06-04 09:31:55 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
1991-03-28 17:28:29 +01:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1991-06-04 09:31:55 +02:00
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
1991-03-28 17:28:29 +01:00
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "ieee-float.h"
|
|
|
|
#include <math.h> /* ldexp */
|
|
|
|
|
|
|
|
/* Convert an IEEE extended float to a double.
|
|
|
|
FROM is the address of the extended float.
|
|
|
|
Store the double in *TO. */
|
|
|
|
|
|
|
|
void
|
|
|
|
ieee_extended_to_double (ext_format, from, to)
|
1992-02-20 20:23:42 +01:00
|
|
|
const struct ext_format *ext_format;
|
1991-03-28 17:28:29 +01:00
|
|
|
char *from;
|
|
|
|
double *to;
|
|
|
|
{
|
|
|
|
unsigned char *ufrom = (unsigned char *)from;
|
|
|
|
double dto;
|
1991-05-02 06:28:42 +02:00
|
|
|
unsigned long mant0, mant1, exponent;
|
1991-03-28 17:28:29 +01:00
|
|
|
|
1992-02-20 20:23:42 +01:00
|
|
|
memcpy (&mant0, &from[MANBYTE_H], 4);
|
|
|
|
memcpy (&mant1, &from[MANBYTE_L], 4);
|
1991-05-02 06:28:42 +02:00
|
|
|
exponent = ((ufrom[EXPBYTE_H] & (unsigned char)~SIGNMASK) << 8) | ufrom[EXPBYTE_L];
|
1991-03-28 17:28:29 +01:00
|
|
|
|
1991-04-05 11:02:10 +02:00
|
|
|
#if 0
|
|
|
|
/* We can't do anything useful with a NaN anyway, so ignore its
|
|
|
|
difference. It will end up as Infinity or something close. */
|
1991-05-02 06:28:42 +02:00
|
|
|
if (exponent == EXT_EXP_NAN) {
|
1991-03-28 17:28:29 +01:00
|
|
|
/* We have a NaN source. */
|
1991-04-05 11:02:10 +02:00
|
|
|
dto = 0.123456789; /* Not much else useful to do -- we don't know if
|
|
|
|
the host system even *has* NaNs, nor how to
|
|
|
|
generate an innocuous one if it does. */
|
|
|
|
} else
|
|
|
|
#endif
|
1991-05-02 06:28:42 +02:00
|
|
|
if (exponent == 0 && mant0 == 0 && mant1 == 0) {
|
1991-03-28 17:28:29 +01:00
|
|
|
dto = 0;
|
|
|
|
} else {
|
|
|
|
/* Build the result algebraically. Might go infinite, underflow, etc;
|
|
|
|
who cares. */
|
|
|
|
mant0 |= 0x80000000;
|
1991-05-02 06:28:42 +02:00
|
|
|
dto = ldexp ((double)mant0, exponent - EXT_EXP_BIAS - 31);
|
|
|
|
dto += ldexp ((double)mant1, exponent - EXT_EXP_BIAS - 31 - 32);
|
1991-04-05 11:02:10 +02:00
|
|
|
if (ufrom[EXPBYTE_H] & SIGNMASK) /* If negative... */
|
|
|
|
dto = -dto; /* ...negate. */
|
1991-03-28 17:28:29 +01:00
|
|
|
}
|
|
|
|
*to = dto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The converse: convert the double *FROM to an extended float
|
|
|
|
and store where TO points. */
|
|
|
|
|
|
|
|
void
|
|
|
|
double_to_ieee_extended (ext_format, from, to)
|
1992-02-20 20:23:42 +01:00
|
|
|
const struct ext_format *ext_format;
|
1991-03-28 17:28:29 +01:00
|
|
|
double *from;
|
|
|
|
char *to;
|
|
|
|
{
|
|
|
|
double dfrom = *from;
|
|
|
|
unsigned long twolongs[2];
|
1991-05-02 06:28:42 +02:00
|
|
|
unsigned long mant0, mant1, exponent;
|
1991-08-19 22:55:06 +02:00
|
|
|
unsigned char tobytes[8];
|
1991-03-28 17:28:29 +01:00
|
|
|
|
* breakpoint.c, buildsym.c, c-exp.y, coffread.c, command.c,
core.c, cplus-dem.c, dbxread.c, dwarfread.c, elfread.c, environ.c,
eval.c, findvar.c, gdbtypes.c, hppabsd-tdep.c, hppahpux-tdep.c,
i386-tdep.c, ieee-float.c, infcmd.c, inflow.c, infptrace.c,
infrun.c, m2-exp.y, mipsread.c, objfiles.c, parse.c, procfs.c,
putenv.c, remote-mm.c, remote-vx.c, solib.c, sparc-tdep.c,
sparc-xdep.c, stack.c, symfile.c, symtab.c, symtab.h, target.c,
tm-i386v.h, tm-sparc.h, utils.c, valarith.c, valops.c, valprint.c,
values.c, xcoffread.c:
Remove "(void)" casts from function calls where the return value
is ignored, in accordance with GNU coding standards.
1992-07-04 05:22:08 +02:00
|
|
|
memset (to, 0, TOTALSIZE);
|
1991-03-28 17:28:29 +01:00
|
|
|
if (dfrom == 0)
|
|
|
|
return; /* Result is zero */
|
|
|
|
if (dfrom != dfrom) {
|
|
|
|
/* From is NaN */
|
|
|
|
to[EXPBYTE_H] = (unsigned char)(EXT_EXP_NAN >> 8);
|
|
|
|
to[EXPBYTE_L] = (unsigned char)EXT_EXP_NAN;
|
|
|
|
to[MANBYTE_H] = 1; /* Be sure it's not infinity, but NaN value is irrel */
|
|
|
|
return; /* Result is NaN */
|
|
|
|
}
|
|
|
|
if (dfrom < 0)
|
|
|
|
to[SIGNBYTE] |= SIGNMASK; /* Set negative sign */
|
|
|
|
/* How to tell an infinity from an ordinary number? FIXME-someday */
|
|
|
|
|
|
|
|
/* The following code assumes that the host has IEEE doubles. FIXME-someday.
|
|
|
|
It also assumes longs are 32 bits! FIXME-someday. */
|
1992-02-20 20:23:42 +01:00
|
|
|
memcpy (twolongs, from, 8);
|
|
|
|
memcpy (tobytes, from, 8);
|
1991-03-28 17:28:29 +01:00
|
|
|
#if HOST_BYTE_ORDER == BIG_ENDIAN
|
1991-08-19 22:55:06 +02:00
|
|
|
exponent = ((tobytes[1] & 0xF0) >> 4) | (tobytes[0] & 0x7F) << 4;
|
1991-03-28 17:28:29 +01:00
|
|
|
mant0 = (twolongs[0] << 11) | twolongs[1] >> 21;
|
|
|
|
mant1 = (twolongs[1] << 11);
|
|
|
|
#else
|
1991-08-19 22:55:06 +02:00
|
|
|
exponent = ((tobytes[6] & 0xF0) >> 4) | (tobytes[7] & 0x7F) << 4;
|
1991-03-28 17:28:29 +01:00
|
|
|
mant0 = (twolongs[1] << 11) | twolongs[0] >> 21;
|
|
|
|
mant1 = (twolongs[0] << 11);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Fiddle with leading 1-bit, implied in double, explicit in extended. */
|
1991-05-02 06:28:42 +02:00
|
|
|
if (exponent == 0)
|
1991-03-28 17:28:29 +01:00
|
|
|
mant0 &= 0x7FFFFFFF;
|
|
|
|
else
|
|
|
|
mant0 |= 0x80000000;
|
|
|
|
|
1991-05-02 06:28:42 +02:00
|
|
|
exponent -= DBL_EXP_BIAS; /* Get integer exp */
|
|
|
|
exponent += EXT_EXP_BIAS; /* Offset for extended */
|
1991-03-28 17:28:29 +01:00
|
|
|
|
|
|
|
/* OK, now store it in extended format. */
|
1991-05-02 06:28:42 +02:00
|
|
|
to[EXPBYTE_H] |= (unsigned char)(exponent >> 8); /* Retain sign */
|
|
|
|
to[EXPBYTE_L] = (unsigned char) exponent;
|
1991-03-28 17:28:29 +01:00
|
|
|
|
1992-02-20 20:23:42 +01:00
|
|
|
memcpy (&to[MANBYTE_H], &mant0, 4);
|
|
|
|
memcpy (&to[MANBYTE_L], &mant1, 4);
|
1991-03-28 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1991-12-27 04:55:12 +01:00
|
|
|
#ifdef IEEE_DEBUG
|
1991-03-28 17:28:29 +01:00
|
|
|
|
|
|
|
/* Test some numbers to see that extended/double conversion works for them. */
|
|
|
|
|
|
|
|
ieee_test (n)
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
union { double d; int i[2]; } di;
|
|
|
|
double result;
|
|
|
|
int i;
|
|
|
|
char exten[16];
|
|
|
|
extern struct ext_format ext_format_68881;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
1991-04-05 11:02:10 +02:00
|
|
|
di.i[0] = (random() << 16) | (random() & 0xffff);
|
|
|
|
di.i[1] = (random() << 16) | (random() & 0xffff);
|
|
|
|
double_to_ieee_extended (&ext_format_68881, &di.d, exten);
|
|
|
|
ieee_extended_to_double (&ext_format_68881, exten, &result);
|
1991-03-28 17:28:29 +01:00
|
|
|
if (di.d != result)
|
|
|
|
printf ("Differ: %x %x %g => %x %x %g\n", di.d, di.d, result, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|