]>
Commit | Line | Data |
---|---|---|
d4052bcb RN |
1 | /***************************************************************************** |
2 | ** Name: extended.c | |
3 | ** Purpose: IEEE Extended<->Double routines to save floats to file | |
4 | ** Maintainer: Ryan Norton | |
5 | ** Modified by: | |
6 | ** Created: 11/24/04 | |
7 | ** RCS-ID: $Id$ | |
8 | *****************************************************************************/ | |
5b7f1aab | 9 | |
d4052bcb RN |
10 | |
11 | #include "wx/defs.h" | |
cf447356 | 12 | |
126eb09b | 13 | #if wxUSE_APPLE_IEEE |
cf447356 | 14 | |
d4052bcb | 15 | #include <math.h> |
cf447356 | 16 | |
d4052bcb RN |
17 | /* Copyright (C) 1989-1991 Ken Turkowski. <turk@computer.org> |
18 | * | |
cf447356 GL |
19 | * All rights reserved. |
20 | * | |
d4052bcb RN |
21 | * Warranty Information |
22 | * Even though I have reviewed this software, I make no warranty | |
23 | * or representation, either express or implied, with respect to this | |
24 | * software, its quality, accuracy, merchantability, or fitness for a | |
25 | * particular purpose. As a result, this software is provided "as is," | |
26 | * and you, its user, are assuming the entire risk as to its quality | |
27 | * and accuracy. | |
28 | * | |
29 | * This code may be used and freely distributed as long as it includes | |
30 | * this copyright notice and the above warranty information. | |
31 | * | |
cf447356 GL |
32 | * Machine-independent I/O routines for IEEE floating-point numbers. |
33 | * | |
34 | * NaN's and infinities are converted to HUGE_VAL or HUGE, which | |
d4052bcb | 35 | * happens to be infinity on IEEE machines. Unfortunately, it is |
cf447356 GL |
36 | * impossible to preserve NaN's in a machine-independent way. |
37 | * Infinities are, however, preserved on IEEE machines. | |
38 | * | |
39 | * These routines have been tested on the following machines: | |
d4052bcb RN |
40 | * Apple Macintosh, MPW 3.1 C compiler |
41 | * Apple Macintosh, THINK C compiler | |
42 | * Silicon Graphics IRIS, MIPS compiler | |
43 | * Cray X/MP and Y/MP | |
44 | * Digital Equipment VAX | |
45 | * Sequent Balance (Multiprocesor 386) | |
46 | * NeXT | |
cf447356 GL |
47 | * |
48 | * | |
49 | * Implemented by Malcolm Slaney and Ken Turkowski. | |
50 | * | |
51 | * Malcolm Slaney contributions during 1988-1990 include big- and little- | |
52 | * endian file I/O, conversion to and from Motorola's extended 80-bit | |
53 | * floating-point format, and conversions to and from IEEE single- | |
54 | * precision floating-point format. | |
55 | * | |
56 | * In 1991, Ken Turkowski implemented the conversions to and from | |
57 | * IEEE double-precision format, added more precision to the extended | |
58 | * conversions, and accommodated conversions involving +/- infinity, | |
59 | * NaN's, and denormalized numbers. | |
60 | */ | |
61 | ||
62 | #ifndef HUGE_VAL | |
d4052bcb | 63 | # define HUGE_VAL HUGE |
47b3cc5c | 64 | #endif /*HUGE_VAL*/ |
cf447356 | 65 | |
cf447356 | 66 | |
d4052bcb RN |
67 | /**************************************************************** |
68 | * The following two routines make up for deficiencies in many | |
69 | * compilers to convert properly between unsigned integers and | |
70 | * floating-point. Some compilers which have this bug are the | |
71 | * THINK_C compiler for the Macintosh and the C compiler for the | |
72 | * Silicon Graphics MIPS-based Iris. | |
73 | ****************************************************************/ | |
cf447356 | 74 | |
d4052bcb RN |
75 | #ifdef applec /* The Apple C compiler works */ |
76 | # define FloatToUnsigned(f) ((wxUint32)(f)) | |
77 | # define UnsignedToFloat(u) ((wxFloat64)(u)) | |
47b3cc5c RN |
78 | #else /*applec*/ |
79 | # define FloatToUnsigned(f) ((wxUint32)((((wxInt32)((f) - 2147483648.0)) + 2147483647L) + 1)) | |
d4052bcb | 80 | # define UnsignedToFloat(u) (((wxFloat64)((wxInt32)((u) - 2147483647L - 1))) + 2147483648.0) |
47b3cc5c | 81 | #endif /*applec*/ |
cf447356 | 82 | |
cf447356 | 83 | |
d4052bcb | 84 | |
cf447356 | 85 | /**************************************************************** |
d4052bcb RN |
86 | * Extended precision IEEE floating-point conversion routines. |
87 | * Extended is an 80-bit number as defined by Motorola, | |
88 | * with a sign bit, 15 bits of exponent (offset 16383?), | |
89 | * and a 64-bit mantissa, with no hidden bit. | |
cf447356 GL |
90 | ****************************************************************/ |
91 | ||
d4052bcb | 92 | wxFloat64 ConvertFromIeeeExtended(wxInt8* bytes) |
cf447356 | 93 | { |
d4052bcb RN |
94 | wxFloat64 f; |
95 | wxInt32 expon; | |
96 | wxUint32 hiMant, loMant; | |
97 | ||
98 | expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF); | |
99 | hiMant = ((wxUint32)(bytes[2] & 0xFF) << 24) | |
100 | | ((wxUint32)(bytes[3] & 0xFF) << 16) | |
101 | | ((wxUint32)(bytes[4] & 0xFF) << 8) | |
102 | | ((wxUint32)(bytes[5] & 0xFF)); | |
103 | loMant = ((wxUint32)(bytes[6] & 0xFF) << 24) | |
104 | | ((wxUint32)(bytes[7] & 0xFF) << 16) | |
105 | | ((wxUint32)(bytes[8] & 0xFF) << 8) | |
106 | | ((wxUint32)(bytes[9] & 0xFF)); | |
107 | ||
108 | if (expon == 0 && hiMant == 0 && loMant == 0) { | |
109 | f = 0; | |
110 | } | |
111 | else { | |
112 | if (expon == 0x7FFF) { /* Infinity or NaN */ | |
113 | f = HUGE_VAL; | |
114 | } | |
115 | else { | |
116 | expon -= 16383; | |
117 | f = ldexp(UnsignedToFloat(hiMant), expon-=31); | |
118 | f += ldexp(UnsignedToFloat(loMant), expon-=32); | |
119 | } | |
120 | } | |
121 | ||
122 | if (bytes[0] & 0x80) | |
123 | return -f; | |
124 | else | |
125 | return f; | |
cf447356 GL |
126 | } |
127 | ||
d4052bcb RN |
128 | |
129 | /****************************************************************/ | |
130 | ||
131 | ||
132 | void ConvertToIeeeExtended(wxFloat64 num, wxInt8 *bytes) | |
133 | { | |
134 | wxInt32 sign; | |
135 | wxInt32 expon; | |
136 | wxFloat64 fMant, fsMant; | |
137 | wxUint32 hiMant, loMant; | |
138 | ||
139 | if (num < 0) { | |
140 | sign = 0x8000; | |
141 | num *= -1; | |
142 | } else { | |
143 | sign = 0; | |
144 | } | |
145 | ||
146 | if (num == 0) { | |
147 | expon = 0; hiMant = 0; loMant = 0; | |
148 | } | |
149 | else { | |
150 | fMant = frexp(num, &expon); | |
151 | if ((expon > 16384) || !(fMant < 1)) { /* Infinity or NaN */ | |
152 | expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */ | |
153 | } | |
154 | else { /* Finite */ | |
155 | expon += 16382; | |
156 | if (expon < 0) { /* denormalized */ | |
157 | fMant = ldexp(fMant, expon); | |
158 | expon = 0; | |
159 | } | |
160 | expon |= sign; | |
161 | fMant = ldexp(fMant, 32); fsMant = floor(fMant); hiMant = FloatToUnsigned(fsMant); | |
162 | fMant = ldexp(fMant - fsMant, 32); fsMant = floor(fMant); loMant = FloatToUnsigned(fsMant); | |
163 | } | |
164 | } | |
165 | ||
166 | bytes[0] = expon >> 8; | |
167 | bytes[1] = expon; | |
168 | bytes[2] = hiMant >> 24; | |
169 | bytes[3] = hiMant >> 16; | |
170 | bytes[4] = hiMant >> 8; | |
171 | bytes[5] = hiMant; | |
172 | bytes[6] = loMant >> 24; | |
173 | bytes[7] = loMant >> 16; | |
174 | bytes[8] = loMant >> 8; | |
175 | bytes[9] = loMant; | |
176 | } | |
177 | ||
178 | ||
179 | ||
44c4a334 | 180 | #endif /* wxUSE_APPLE_IEEE */ |