]>
git.saurik.com Git - wxWidgets.git/blob - src/common/extended.c
1 #ifndef __VISAGECPP__ /* DW: why does anyone need this for this module? */
12 * C O N V E R T T O I E E E E X T E N D E D
15 /* Copyright (C) 1988-1991 Apple Computer, Inc.
16 * All rights reserved.
18 * Machine-independent I/O routines for IEEE floating-point numbers.
20 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
21 * happens to be infinity on IEEE machines. Unfortunately, it is
22 * impossible to preserve NaN's in a machine-independent way.
23 * Infinities are, however, preserved on IEEE machines.
25 * These routines have been tested on the following machines:
26 * Apple Macintosh, MPW 3.1 C compiler
27 * Apple Macintosh, THINK C compiler
28 * Silicon Graphics IRIS, MIPS compiler
30 * Digital Equipment VAX
33 * Implemented by Malcolm Slaney and Ken Turkowski.
35 * Malcolm Slaney contributions during 1988-1990 include big- and little-
36 * endian file I/O, conversion to and from Motorola's extended 80-bit
37 * floating-point format, and conversions to and from IEEE single-
38 * precision floating-point format.
40 * In 1991, Ken Turkowski implemented the conversions to and from
41 * IEEE double-precision format, added more precision to the extended
42 * conversions, and accommodated conversions involving +/- infinity,
43 * NaN's, and denormalized numbers.
50 #define FloatToUnsigned(f) ((unsigned long) (((long) (f - 2147483648.0)) + 2147483647L) + 1)
52 void ConvertToIeeeExtended(double num
, unsigned char *bytes
)
57 unsigned long hiMant
, loMant
;
67 expon
= 0; hiMant
= 0; loMant
= 0;
70 fMant
= frexp(num
, &expon
);
71 if ((expon
> 16384) || !(fMant
< 1)) { /* Infinity or NaN */
72 expon
= sign
|0x7FFF; hiMant
= 0; loMant
= 0; /* infinity */
76 if (expon
< 0) { /* denormalized */
77 fMant
= ldexp(fMant
, expon
);
81 fMant
= ldexp(fMant
, 32);
82 fsMant
= floor(fMant
);
83 hiMant
= FloatToUnsigned(fsMant
);
84 fMant
= ldexp(fMant
- fsMant
, 32);
85 fsMant
= floor(fMant
);
86 loMant
= FloatToUnsigned(fsMant
);
90 /* disable the warning about 'possible loss of data' & 'conversion between
93 #pragma warning(disable: 4244)
94 #pragma warning(disable: 4135)
95 #endif /* Visual C++ */
97 bytes
[0] = (expon
>> 8) & 0xff;
98 bytes
[1] = expon
& 0xff;
99 bytes
[2] = (unsigned char) ((hiMant
>> 24) & 0xff);
100 bytes
[3] = (unsigned char) ((hiMant
>> 16) & 0xff);
101 bytes
[4] = (unsigned char) ((hiMant
>> 8) & 0xff);
102 bytes
[5] = (unsigned char) (hiMant
& 0xff);
103 bytes
[6] = (unsigned char) ((loMant
>> 24) & 0xff);
104 bytes
[7] = (unsigned char) ((loMant
>> 16) & 0xff);
105 bytes
[8] = (unsigned char) ((loMant
>> 8) & 0xff);
106 bytes
[9] = (unsigned char) (loMant
& 0xff);
109 #pragma warning(default: 4244)
110 #pragma warning(default: 4135)
111 #endif /* Visual C++ */
115 * C O N V E R T F R O M I E E E E X T E N D E D
119 * Copyright (C) 1988-1991 Apple Computer, Inc.
120 * All rights reserved.
122 * Machine-independent I/O routines for IEEE floating-point numbers.
124 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
125 * happens to be infinity on IEEE machines. Unfortunately, it is
126 * impossible to preserve NaN's in a machine-independent way.
127 * Infinities are, however, preserved on IEEE machines.
129 * These routines have been tested on the following machines:
130 * Apple Macintosh, MPW 3.1 C compiler
131 * Apple Macintosh, THINK C compiler
132 * Silicon Graphics IRIS, MIPS compiler
134 * Digital Equipment VAX
137 * Implemented by Malcolm Slaney and Ken Turkowski.
139 * Malcolm Slaney contributions during 1988-1990 include big- and little-
140 * endian file I/O, conversion to and from Motorola's extended 80-bit
141 * floating-point format, and conversions to and from IEEE single-
142 * precision floating-point format.
144 * In 1991, Ken Turkowski implemented the conversions to and from
145 * IEEE double-precision format, added more precision to the extended
146 * conversions, and accommodated conversions involving +/- infinity,
147 * NaN's, and denormalized numbers.
151 # define HUGE_VAL HUGE
154 # define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0)
156 /****************************************************************
157 * Extended precision IEEE floating-point conversion routine.
158 ****************************************************************/
160 double ConvertFromIeeeExtended(const unsigned char *bytes
)
164 unsigned long hiMant
, loMant
;
166 expon
= ((bytes
[0] & 0x7F) << 8) | (bytes
[1] & 0xFF);
167 hiMant
= ((unsigned long)(bytes
[2] & 0xFF) << 24)
168 | ((unsigned long) (bytes
[3] & 0xFF) << 16)
169 | ((unsigned long) (bytes
[4] & 0xFF) << 8)
170 | ((unsigned long) (bytes
[5] & 0xFF));
171 loMant
= ((unsigned long) (bytes
[6] & 0xFF) << 24)
172 | ((unsigned long) (bytes
[7] & 0xFF) << 16)
173 | ((unsigned long) (bytes
[8] & 0xFF) << 8)
174 | ((unsigned long) (bytes
[9] & 0xFF));
176 if (expon
== 0 && hiMant
== 0 && loMant
== 0) {
180 if (expon
== 0x7FFF) { /* Infinity or NaN */
185 f
= ldexp(UnsignedToFloat(hiMant
), expon
-=31);
186 f
+= ldexp(UnsignedToFloat(loMant
), expon
-=32);
196 #endif /* wxUSE_APPLE_IEEE */