]>
git.saurik.com Git - wxWidgets.git/blob - src/common/extended.c
7 * C O N V E R T T O I E E E E X T E N D E D
10 /* Copyright (C) 1988-1991 Apple Computer, Inc.
11 * All rights reserved.
13 * Machine-independent I/O routines for IEEE floating-point numbers.
15 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
16 * happens to be infinity on IEEE machines. Unfortunately, it is
17 * impossible to preserve NaN's in a machine-independent way.
18 * Infinities are, however, preserved on IEEE machines.
20 * These routines have been tested on the following machines:
21 * Apple Macintosh, MPW 3.1 C compiler
22 * Apple Macintosh, THINK C compiler
23 * Silicon Graphics IRIS, MIPS compiler
25 * Digital Equipment VAX
28 * Implemented by Malcolm Slaney and Ken Turkowski.
30 * Malcolm Slaney contributions during 1988-1990 include big- and little-
31 * endian file I/O, conversion to and from Motorola's extended 80-bit
32 * floating-point format, and conversions to and from IEEE single-
33 * precision floating-point format.
35 * In 1991, Ken Turkowski implemented the conversions to and from
36 * IEEE double-precision format, added more precision to the extended
37 * conversions, and accommodated conversions involving +/- infinity,
38 * NaN's, and denormalized numbers.
45 #define FloatToUnsigned(f) ((unsigned long) (((long) (f - 2147483648.0)) + 2147483647L) + 1)
47 void ConvertToIeeeExtended(double num
, unsigned char *bytes
)
52 unsigned long hiMant
, loMant
;
62 expon
= 0; hiMant
= 0; loMant
= 0;
65 fMant
= frexp(num
, &expon
);
66 if ((expon
> 16384) || !(fMant
< 1)) { /* Infinity or NaN */
67 expon
= sign
|0x7FFF; hiMant
= 0; loMant
= 0; /* infinity */
71 if (expon
< 0) { /* denormalized */
72 fMant
= ldexp(fMant
, expon
);
76 fMant
= ldexp(fMant
, 32);
77 fsMant
= floor(fMant
);
78 hiMant
= FloatToUnsigned(fsMant
);
79 fMant
= ldexp(fMant
- fsMant
, 32);
80 fsMant
= floor(fMant
);
81 loMant
= FloatToUnsigned(fsMant
);
85 bytes
[0] = expon
>> 8;
87 bytes
[2] = hiMant
>> 24;
88 bytes
[3] = hiMant
>> 16;
89 bytes
[4] = hiMant
>> 8;
91 bytes
[6] = loMant
>> 24;
92 bytes
[7] = loMant
>> 16;
93 bytes
[8] = loMant
>> 8;
98 * C O N V E R T F R O M I E E E E X T E N D E D
102 * Copyright (C) 1988-1991 Apple Computer, Inc.
103 * All rights reserved.
105 * Machine-independent I/O routines for IEEE floating-point numbers.
107 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
108 * happens to be infinity on IEEE machines. Unfortunately, it is
109 * impossible to preserve NaN's in a machine-independent way.
110 * Infinities are, however, preserved on IEEE machines.
112 * These routines have been tested on the following machines:
113 * Apple Macintosh, MPW 3.1 C compiler
114 * Apple Macintosh, THINK C compiler
115 * Silicon Graphics IRIS, MIPS compiler
117 * Digital Equipment VAX
120 * Implemented by Malcolm Slaney and Ken Turkowski.
122 * Malcolm Slaney contributions during 1988-1990 include big- and little-
123 * endian file I/O, conversion to and from Motorola's extended 80-bit
124 * floating-point format, and conversions to and from IEEE single-
125 * precision floating-point format.
127 * In 1991, Ken Turkowski implemented the conversions to and from
128 * IEEE double-precision format, added more precision to the extended
129 * conversions, and accommodated conversions involving +/- infinity,
130 * NaN's, and denormalized numbers.
134 # define HUGE_VAL HUGE
137 # define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0)
139 /****************************************************************
140 * Extended precision IEEE floating-point conversion routine.
141 ****************************************************************/
143 double ConvertFromIeeeExtended(const unsigned char *bytes
)
147 unsigned long hiMant
, loMant
;
149 expon
= ((bytes
[0] & 0x7F) << 8) | (bytes
[1] & 0xFF);
150 hiMant
= ((unsigned long)(bytes
[2] & 0xFF) << 24)
151 | ((unsigned long) (bytes
[3] & 0xFF) << 16)
152 | ((unsigned long) (bytes
[4] & 0xFF) << 8)
153 | ((unsigned long) (bytes
[5] & 0xFF));
154 loMant
= ((unsigned long) (bytes
[6] & 0xFF) << 24)
155 | ((unsigned long) (bytes
[7] & 0xFF) << 16)
156 | ((unsigned long) (bytes
[8] & 0xFF) << 8)
157 | ((unsigned long) (bytes
[9] & 0xFF));
159 if (expon
== 0 && hiMant
== 0 && loMant
== 0) {
163 if (expon
== 0x7FFF) { /* Infinity or NaN */
168 f
= ldexp(UnsignedToFloat(hiMant
), expon
-=31);
169 f
+= ldexp(UnsignedToFloat(loMant
), expon
-=32);