]>
Commit | Line | Data |
---|---|---|
197dd9af | 1 | # pragma warning(disable:4001) /* non standard extension used: single line comment */ |
cf447356 GL |
2 | #include "wx/setup.h" |
3 | #include <math.h> | |
4 | ||
126eb09b | 5 | #if wxUSE_APPLE_IEEE |
cf447356 GL |
6 | |
7 | /* | |
8 | * C O N V E R T T O I E E E E X T E N D E D | |
9 | */ | |
10 | ||
11 | /* Copyright (C) 1988-1991 Apple Computer, Inc. | |
12 | * All rights reserved. | |
13 | * | |
14 | * Machine-independent I/O routines for IEEE floating-point numbers. | |
15 | * | |
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. | |
20 | * | |
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 | |
25 | * Cray X/MP and Y/MP | |
26 | * Digital Equipment VAX | |
27 | * | |
28 | * | |
29 | * Implemented by Malcolm Slaney and Ken Turkowski. | |
30 | * | |
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. | |
35 | * | |
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. | |
40 | */ | |
41 | ||
42 | #ifndef HUGE_VAL | |
43 | #define HUGE_VAL HUGE | |
44 | #endif /*HUGE_VAL*/ | |
45 | ||
46 | #define FloatToUnsigned(f) ((unsigned long) (((long) (f - 2147483648.0)) + 2147483647L) + 1) | |
47 | ||
48 | void ConvertToIeeeExtended(double num, unsigned char *bytes) | |
49 | { | |
50 | int sign; | |
51 | int expon; | |
52 | double fMant, fsMant; | |
53 | unsigned long hiMant, loMant; | |
54 | ||
55 | if (num < 0) { | |
56 | sign = 0x8000; | |
57 | num *= -1; | |
58 | } else { | |
59 | sign = 0; | |
60 | } | |
61 | ||
62 | if (num == 0) { | |
63 | expon = 0; hiMant = 0; loMant = 0; | |
64 | } | |
65 | else { | |
66 | fMant = frexp(num, &expon); | |
67 | if ((expon > 16384) || !(fMant < 1)) { /* Infinity or NaN */ | |
68 | expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */ | |
69 | } | |
70 | else { /* Finite */ | |
71 | expon += 16382; | |
72 | if (expon < 0) { /* denormalized */ | |
73 | fMant = ldexp(fMant, expon); | |
74 | expon = 0; | |
75 | } | |
76 | expon |= sign; | |
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); | |
83 | } | |
84 | } | |
85 | ||
197dd9af | 86 | /* disable the warning about 'possible loss of data' & 'conversion between diff types' */ |
84a19dec VZ |
87 | #ifdef _MSC_VER |
88 | #pragma warning(disable: 4244) | |
197dd9af | 89 | #pragma warning(disable: 4135) |
777553d2 | 90 | #endif /* Visual C++ */ |
84a19dec | 91 | |
e03bcf0d GL |
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); | |
44719c47 | 101 | bytes[9] = (unsigned char) (loMant & 0xff); |
84a19dec VZ |
102 | |
103 | #ifdef _MSC_VER | |
104 | #pragma warning(default: 4244) | |
197dd9af | 105 | #pragma warning(default: 4135) |
777553d2 | 106 | #endif /* Visual C++ */ |
cf447356 GL |
107 | } |
108 | ||
109 | /* | |
110 | * C O N V E R T F R O M I E E E E X T E N D E D | |
111 | */ | |
112 | ||
113 | /* | |
114 | * Copyright (C) 1988-1991 Apple Computer, Inc. | |
115 | * All rights reserved. | |
116 | * | |
117 | * Machine-independent I/O routines for IEEE floating-point numbers. | |
118 | * | |
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. | |
123 | * | |
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 | |
128 | * Cray X/MP and Y/MP | |
129 | * Digital Equipment VAX | |
130 | * | |
131 | * | |
132 | * Implemented by Malcolm Slaney and Ken Turkowski. | |
133 | * | |
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. | |
138 | * | |
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. | |
143 | */ | |
144 | ||
145 | #ifndef HUGE_VAL | |
146 | # define HUGE_VAL HUGE | |
147 | #endif /*HUGE_VAL*/ | |
148 | ||
149 | # define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0) | |
150 | ||
151 | /**************************************************************** | |
152 | * Extended precision IEEE floating-point conversion routine. | |
153 | ****************************************************************/ | |
154 | ||
155 | double ConvertFromIeeeExtended(const unsigned char *bytes) | |
156 | { | |
157 | double f; | |
158 | int expon; | |
159 | unsigned long hiMant, loMant; | |
160 | ||
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)); | |
170 | ||
171 | if (expon == 0 && hiMant == 0 && loMant == 0) { | |
172 | f = 0; | |
173 | } | |
174 | else { | |
175 | if (expon == 0x7FFF) { /* Infinity or NaN */ | |
176 | f = HUGE_VAL; | |
177 | } | |
178 | else { | |
179 | expon -= 16383; | |
180 | f = ldexp(UnsignedToFloat(hiMant), expon-=31); | |
181 | f += ldexp(UnsignedToFloat(loMant), expon-=32); | |
182 | } | |
183 | } | |
184 | ||
185 | if (bytes[0] & 0x80) | |
186 | return -f; | |
187 | else | |
188 | return f; | |
189 | } | |
190 | ||
44c4a334 | 191 | #endif /* wxUSE_APPLE_IEEE */ |