]>
git.saurik.com Git - wxWidgets.git/blob - src/common/extended.c
1 /*****************************************************************************
3 ** Purpose: IEEE Extended<->Double routines to save floats to file
4 ** Maintainer: Ryan Norton
8 *****************************************************************************/
22 /* Copyright (C) 1989-1991 Ken Turkowski. <turk@computer.org>
24 * All rights reserved.
26 * Warranty Information
27 * Even though I have reviewed this software, I make no warranty
28 * or representation, either express or implied, with respect to this
29 * software, its quality, accuracy, merchantability, or fitness for a
30 * particular purpose. As a result, this software is provided "as is,"
31 * and you, its user, are assuming the entire risk as to its quality
34 * This code may be used and freely distributed as long as it includes
35 * this copyright notice and the above warranty information.
37 * Machine-independent I/O routines for IEEE floating-point numbers.
39 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
40 * happens to be infinity on IEEE machines. Unfortunately, it is
41 * impossible to preserve NaN's in a machine-independent way.
42 * Infinities are, however, preserved on IEEE machines.
44 * These routines have been tested on the following machines:
45 * Apple Macintosh, MPW 3.1 C compiler
46 * Apple Macintosh, THINK C compiler
47 * Silicon Graphics IRIS, MIPS compiler
49 * Digital Equipment VAX
50 * Sequent Balance (Multiprocesor 386)
54 * Implemented by Malcolm Slaney and Ken Turkowski.
56 * Malcolm Slaney contributions during 1988-1990 include big- and little-
57 * endian file I/O, conversion to and from Motorola's extended 80-bit
58 * floating-point format, and conversions to and from IEEE single-
59 * precision floating-point format.
61 * In 1991, Ken Turkowski implemented the conversions to and from
62 * IEEE double-precision format, added more precision to the extended
63 * conversions, and accommodated conversions involving +/- infinity,
64 * NaN's, and denormalized numbers.
68 # define HUGE_VAL HUGE
72 /****************************************************************
73 * The following two routines make up for deficiencies in many
74 * compilers to convert properly between unsigned integers and
75 * floating-point. Some compilers which have this bug are the
76 * THINK_C compiler for the Macintosh and the C compiler for the
77 * Silicon Graphics MIPS-based Iris.
78 ****************************************************************/
80 #ifdef applec /* The Apple C compiler works */
81 # define FloatToUnsigned(f) ((wxUint32)(f))
82 # define UnsignedToFloat(u) ((wxFloat64)(u))
84 # define FloatToUnsigned(f) ((wxUint32)(((wxInt32)((f) - 2147483648.0)) + 2147483647L) + 1)
85 # define UnsignedToFloat(u) (((wxFloat64)((wxInt32)((u) - 2147483647L - 1))) + 2147483648.0)
90 /****************************************************************
91 * Extended precision IEEE floating-point conversion routines.
92 * Extended is an 80-bit number as defined by Motorola,
93 * with a sign bit, 15 bits of exponent (offset 16383?),
94 * and a 64-bit mantissa, with no hidden bit.
95 ****************************************************************/
97 wxFloat64
ConvertFromIeeeExtended(wxInt8
* bytes
)
101 wxUint32 hiMant
, loMant
;
103 expon
= ((bytes
[0] & 0x7F) << 8) | (bytes
[1] & 0xFF);
104 hiMant
= ((wxUint32
)(bytes
[2] & 0xFF) << 24)
105 | ((wxUint32
)(bytes
[3] & 0xFF) << 16)
106 | ((wxUint32
)(bytes
[4] & 0xFF) << 8)
107 | ((wxUint32
)(bytes
[5] & 0xFF));
108 loMant
= ((wxUint32
)(bytes
[6] & 0xFF) << 24)
109 | ((wxUint32
)(bytes
[7] & 0xFF) << 16)
110 | ((wxUint32
)(bytes
[8] & 0xFF) << 8)
111 | ((wxUint32
)(bytes
[9] & 0xFF));
113 if (expon
== 0 && hiMant
== 0 && loMant
== 0) {
117 if (expon
== 0x7FFF) { /* Infinity or NaN */
122 f
= ldexp(UnsignedToFloat(hiMant
), expon
-=31);
123 f
+= ldexp(UnsignedToFloat(loMant
), expon
-=32);
134 /****************************************************************/
137 void ConvertToIeeeExtended(wxFloat64 num
, wxInt8
*bytes
)
141 wxFloat64 fMant
, fsMant
;
142 wxUint32 hiMant
, loMant
;
152 expon
= 0; hiMant
= 0; loMant
= 0;
155 fMant
= frexp(num
, &expon
);
156 if ((expon
> 16384) || !(fMant
< 1)) { /* Infinity or NaN */
157 expon
= sign
|0x7FFF; hiMant
= 0; loMant
= 0; /* infinity */
161 if (expon
< 0) { /* denormalized */
162 fMant
= ldexp(fMant
, expon
);
166 fMant
= ldexp(fMant
, 32); fsMant
= floor(fMant
); hiMant
= FloatToUnsigned(fsMant
);
167 fMant
= ldexp(fMant
- fsMant
, 32); fsMant
= floor(fMant
); loMant
= FloatToUnsigned(fsMant
);
171 bytes
[0] = expon
>> 8;
173 bytes
[2] = hiMant
>> 24;
174 bytes
[3] = hiMant
>> 16;
175 bytes
[4] = hiMant
>> 8;
177 bytes
[6] = loMant
>> 24;
178 bytes
[7] = loMant
>> 16;
179 bytes
[8] = loMant
>> 8;
185 #endif /* wxUSE_APPLE_IEEE */