]>
git.saurik.com Git - wxWidgets.git/blob - src/common/extended.c
8 * C O N V E R T T O I E E E E X T E N D E D
11 /* Copyright (C) 1988-1991 Apple Computer, Inc.
12 * All rights reserved.
14 * Machine-independent I/O routines for IEEE floating-point numbers.
16 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
17 * happens to be infinity on IEEE machines. Unfortunately, it is
18 * impossible to preserve NaN's in a machine-independent way.
19 * Infinities are, however, preserved on IEEE machines.
21 * These routines have been tested on the following machines:
22 * Apple Macintosh, MPW 3.1 C compiler
23 * Apple Macintosh, THINK C compiler
24 * Silicon Graphics IRIS, MIPS compiler
26 * Digital Equipment VAX
29 * Implemented by Malcolm Slaney and Ken Turkowski.
31 * Malcolm Slaney contributions during 1988-1990 include big- and little-
32 * endian file I/O, conversion to and from Motorola's extended 80-bit
33 * floating-point format, and conversions to and from IEEE single-
34 * precision floating-point format.
36 * In 1991, Ken Turkowski implemented the conversions to and from
37 * IEEE double-precision format, added more precision to the extended
38 * conversions, and accommodated conversions involving +/- infinity,
39 * NaN's, and denormalized numbers.
46 #define FloatToUnsigned(f) ((unsigned long) (((long) (f - 2147483648.0)) + 2147483647L) + 1)
48 void ConvertToIeeeExtended(double num
, unsigned char *bytes
)
53 unsigned long hiMant
, loMant
;
63 expon
= 0; hiMant
= 0; loMant
= 0;
66 fMant
= frexp(num
, &expon
);
67 if ((expon
> 16384) || !(fMant
< 1)) { /* Infinity or NaN */
68 expon
= sign
|0x7FFF; hiMant
= 0; loMant
= 0; /* infinity */
72 if (expon
< 0) { /* denormalized */
73 fMant
= ldexp(fMant
, expon
);
77 fMant
= ldexp(fMant
, 32);
78 fsMant
= floor(fMant
);
79 hiMant
= FloatToUnsigned(fsMant
);
80 fMant
= ldexp(fMant
- fsMant
, 32);
81 fsMant
= floor(fMant
);
82 loMant
= FloatToUnsigned(fsMant
);
86 /* disable the warning about 'possible loss of data' & 'conversion between
89 #pragma warning(disable: 4244)
90 #pragma warning(disable: 4135)
91 #endif /* Visual C++ */
93 bytes
[0] = (expon
>> 8) & 0xff;
94 bytes
[1] = expon
& 0xff;
95 bytes
[2] = (unsigned char) ((hiMant
>> 24) & 0xff);
96 bytes
[3] = (unsigned char) ((hiMant
>> 16) & 0xff);
97 bytes
[4] = (unsigned char) ((hiMant
>> 8) & 0xff);
98 bytes
[5] = (unsigned char) (hiMant
& 0xff);
99 bytes
[6] = (unsigned char) ((loMant
>> 24) & 0xff);
100 bytes
[7] = (unsigned char) ((loMant
>> 16) & 0xff);
101 bytes
[8] = (unsigned char) ((loMant
>> 8) & 0xff);
102 bytes
[9] = (unsigned char) (loMant
& 0xff);
105 #pragma warning(default: 4244)
106 #pragma warning(default: 4135)
107 #endif /* Visual C++ */
111 * C O N V E R T F R O M I E E E E X T E N D E D
115 * Copyright (C) 1988-1991 Apple Computer, Inc.
116 * All rights reserved.
118 * Machine-independent I/O routines for IEEE floating-point numbers.
120 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
121 * happens to be infinity on IEEE machines. Unfortunately, it is
122 * impossible to preserve NaN's in a machine-independent way.
123 * Infinities are, however, preserved on IEEE machines.
125 * These routines have been tested on the following machines:
126 * Apple Macintosh, MPW 3.1 C compiler
127 * Apple Macintosh, THINK C compiler
128 * Silicon Graphics IRIS, MIPS compiler
130 * Digital Equipment VAX
133 * Implemented by Malcolm Slaney and Ken Turkowski.
135 * Malcolm Slaney contributions during 1988-1990 include big- and little-
136 * endian file I/O, conversion to and from Motorola's extended 80-bit
137 * floating-point format, and conversions to and from IEEE single-
138 * precision floating-point format.
140 * In 1991, Ken Turkowski implemented the conversions to and from
141 * IEEE double-precision format, added more precision to the extended
142 * conversions, and accommodated conversions involving +/- infinity,
143 * NaN's, and denormalized numbers.
147 # define HUGE_VAL HUGE
150 # define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0)
152 /****************************************************************
153 * Extended precision IEEE floating-point conversion routine.
154 ****************************************************************/
156 double ConvertFromIeeeExtended(const unsigned char *bytes
)
160 unsigned long hiMant
, loMant
;
162 expon
= ((bytes
[0] & 0x7F) << 8) | (bytes
[1] & 0xFF);
163 hiMant
= ((unsigned long)(bytes
[2] & 0xFF) << 24)
164 | ((unsigned long) (bytes
[3] & 0xFF) << 16)
165 | ((unsigned long) (bytes
[4] & 0xFF) << 8)
166 | ((unsigned long) (bytes
[5] & 0xFF));
167 loMant
= ((unsigned long) (bytes
[6] & 0xFF) << 24)
168 | ((unsigned long) (bytes
[7] & 0xFF) << 16)
169 | ((unsigned long) (bytes
[8] & 0xFF) << 8)
170 | ((unsigned long) (bytes
[9] & 0xFF));
172 if (expon
== 0 && hiMant
== 0 && loMant
== 0) {
176 if (expon
== 0x7FFF) { /* Infinity or NaN */
181 f
= ldexp(UnsignedToFloat(hiMant
), expon
-=31);
182 f
+= ldexp(UnsignedToFloat(loMant
), expon
-=32);
192 #endif /* wxUSE_APPLE_IEEE */