]>
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 *****************************************************************************/
17 /* Copyright (C) 1989-1991 Ken Turkowski. <turk@computer.org>
19 * All rights reserved.
21 * Warranty Information
22 * Even though I have reviewed this software, I make no warranty
23 * or representation, either express or implied, with respect to this
24 * software, its quality, accuracy, merchantability, or fitness for a
25 * particular purpose. As a result, this software is provided "as is,"
26 * and you, its user, are assuming the entire risk as to its quality
29 * This code may be used and freely distributed as long as it includes
30 * this copyright notice and the above warranty information.
32 * Machine-independent I/O routines for IEEE floating-point numbers.
34 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
35 * happens to be infinity on IEEE machines. Unfortunately, it is
36 * impossible to preserve NaN's in a machine-independent way.
37 * Infinities are, however, preserved on IEEE machines.
39 * These routines have been tested on the following machines:
40 * Apple Macintosh, MPW 3.1 C compiler
41 * Apple Macintosh, THINK C compiler
42 * Silicon Graphics IRIS, MIPS compiler
44 * Digital Equipment VAX
45 * Sequent Balance (Multiprocesor 386)
49 * Implemented by Malcolm Slaney and Ken Turkowski.
51 * Malcolm Slaney contributions during 1988-1990 include big- and little-
52 * endian file I/O, conversion to and from Motorola's extended 80-bit
53 * floating-point format, and conversions to and from IEEE single-
54 * precision floating-point format.
56 * In 1991, Ken Turkowski implemented the conversions to and from
57 * IEEE double-precision format, added more precision to the extended
58 * conversions, and accommodated conversions involving +/- infinity,
59 * NaN's, and denormalized numbers.
63 # define HUGE_VAL HUGE
67 /****************************************************************
68 * The following two routines make up for deficiencies in many
69 * compilers to convert properly between unsigned integers and
70 * floating-point. Some compilers which have this bug are the
71 * THINK_C compiler for the Macintosh and the C compiler for the
72 * Silicon Graphics MIPS-based Iris.
73 ****************************************************************/
75 #ifdef applec /* The Apple C compiler works */
76 # define FloatToUnsigned(f) ((wxUint32)(f))
77 # define UnsignedToFloat(u) ((wxFloat64)(u))
79 # define FloatToUnsigned(f) ((wxUint32)((((wxInt32)((f) - 2147483648.0)) + 2147483647L) + 1))
80 # define UnsignedToFloat(u) (((wxFloat64)((wxInt32)((u) - 2147483647L - 1))) + 2147483648.0)
85 /****************************************************************
86 * Extended precision IEEE floating-point conversion routines.
87 * Extended is an 80-bit number as defined by Motorola,
88 * with a sign bit, 15 bits of exponent (offset 16383?),
89 * and a 64-bit mantissa, with no hidden bit.
90 ****************************************************************/
92 wxFloat64
ConvertFromIeeeExtended(wxInt8
* bytes
)
96 wxUint32 hiMant
, loMant
;
98 expon
= ((bytes
[0] & 0x7F) << 8) | (bytes
[1] & 0xFF);
99 hiMant
= ((wxUint32
)(bytes
[2] & 0xFF) << 24)
100 | ((wxUint32
)(bytes
[3] & 0xFF) << 16)
101 | ((wxUint32
)(bytes
[4] & 0xFF) << 8)
102 | ((wxUint32
)(bytes
[5] & 0xFF));
103 loMant
= ((wxUint32
)(bytes
[6] & 0xFF) << 24)
104 | ((wxUint32
)(bytes
[7] & 0xFF) << 16)
105 | ((wxUint32
)(bytes
[8] & 0xFF) << 8)
106 | ((wxUint32
)(bytes
[9] & 0xFF));
108 if (expon
== 0 && hiMant
== 0 && loMant
== 0) {
112 if (expon
== 0x7FFF) { /* Infinity or NaN */
117 f
= ldexp(UnsignedToFloat(hiMant
), expon
-=31);
118 f
+= ldexp(UnsignedToFloat(loMant
), expon
-=32);
129 /****************************************************************/
132 void ConvertToIeeeExtended(wxFloat64 num
, wxInt8
*bytes
)
136 wxFloat64 fMant
, fsMant
;
137 wxUint32 hiMant
, loMant
;
147 expon
= 0; hiMant
= 0; loMant
= 0;
150 fMant
= frexp(num
, &expon
);
151 if ((expon
> 16384) || !(fMant
< 1)) { /* Infinity or NaN */
152 expon
= sign
|0x7FFF; hiMant
= 0; loMant
= 0; /* infinity */
156 if (expon
< 0) { /* denormalized */
157 fMant
= ldexp(fMant
, expon
);
161 fMant
= ldexp(fMant
, 32); fsMant
= floor(fMant
); hiMant
= FloatToUnsigned(fsMant
);
162 fMant
= ldexp(fMant
- fsMant
, 32); fsMant
= floor(fMant
); loMant
= FloatToUnsigned(fsMant
);
166 bytes
[0] = expon
>> 8;
168 bytes
[2] = hiMant
>> 24;
169 bytes
[3] = hiMant
>> 16;
170 bytes
[4] = hiMant
>> 8;
172 bytes
[6] = loMant
>> 24;
173 bytes
[7] = loMant
>> 16;
174 bytes
[8] = loMant
>> 8;
180 #endif /* wxUSE_APPLE_IEEE */