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