]>
git.saurik.com Git - wxWidgets.git/blob - src/common/extended.c
1 # pragma warning(disable:4001) /* non standard extension used: single line comment */
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 diff types' */
88 #pragma warning(disable: 4244)
89 #pragma warning(disable: 4135)
90 #endif /* Visual C++ */
92 bytes
[0] = (expon
>> 8) & 0xff;
93 bytes
[1] = expon
& 0xff;
94 bytes
[2] = (unsigned char) ((hiMant
>> 24) & 0xff);
95 bytes
[3] = (unsigned char) ((hiMant
>> 16) & 0xff);
96 bytes
[4] = (unsigned char) ((hiMant
>> 8) & 0xff);
97 bytes
[5] = (unsigned char) (hiMant
& 0xff);
98 bytes
[6] = (unsigned char) ((loMant
>> 24) & 0xff);
99 bytes
[7] = (unsigned char) ((loMant
>> 16) & 0xff);
100 bytes
[8] = (unsigned char) ((loMant
>> 8) & 0xff);
101 bytes
[9] = (unsigned char) (loMant
& 0xff);
104 #pragma warning(default: 4244)
105 #pragma warning(default: 4135)
106 #endif /* Visual C++ */
110 * C O N V E R T F R O M I E E E E X T E N D E D
114 * Copyright (C) 1988-1991 Apple Computer, Inc.
115 * All rights reserved.
117 * Machine-independent I/O routines for IEEE floating-point numbers.
119 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
120 * happens to be infinity on IEEE machines. Unfortunately, it is
121 * impossible to preserve NaN's in a machine-independent way.
122 * Infinities are, however, preserved on IEEE machines.
124 * These routines have been tested on the following machines:
125 * Apple Macintosh, MPW 3.1 C compiler
126 * Apple Macintosh, THINK C compiler
127 * Silicon Graphics IRIS, MIPS compiler
129 * Digital Equipment VAX
132 * Implemented by Malcolm Slaney and Ken Turkowski.
134 * Malcolm Slaney contributions during 1988-1990 include big- and little-
135 * endian file I/O, conversion to and from Motorola's extended 80-bit
136 * floating-point format, and conversions to and from IEEE single-
137 * precision floating-point format.
139 * In 1991, Ken Turkowski implemented the conversions to and from
140 * IEEE double-precision format, added more precision to the extended
141 * conversions, and accommodated conversions involving +/- infinity,
142 * NaN's, and denormalized numbers.
146 # define HUGE_VAL HUGE
149 # define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0)
151 /****************************************************************
152 * Extended precision IEEE floating-point conversion routine.
153 ****************************************************************/
155 double ConvertFromIeeeExtended(const unsigned char *bytes
)
159 unsigned long hiMant
, loMant
;
161 expon
= ((bytes
[0] & 0x7F) << 8) | (bytes
[1] & 0xFF);
162 hiMant
= ((unsigned long)(bytes
[2] & 0xFF) << 24)
163 | ((unsigned long) (bytes
[3] & 0xFF) << 16)
164 | ((unsigned long) (bytes
[4] & 0xFF) << 8)
165 | ((unsigned long) (bytes
[5] & 0xFF));
166 loMant
= ((unsigned long) (bytes
[6] & 0xFF) << 24)
167 | ((unsigned long) (bytes
[7] & 0xFF) << 16)
168 | ((unsigned long) (bytes
[8] & 0xFF) << 8)
169 | ((unsigned long) (bytes
[9] & 0xFF));
171 if (expon
== 0 && hiMant
== 0 && loMant
== 0) {
175 if (expon
== 0x7FFF) { /* Infinity or NaN */
180 f
= ldexp(UnsignedToFloat(hiMant
), expon
-=31);
181 f
+= ldexp(UnsignedToFloat(loMant
), expon
-=32);
191 #endif /* wxUSE_APPLE_IEEE */