replace apple extended code
[wxWidgets.git] / src / common / extended.c
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 *****************************************************************************/
9
10
11 #include "wx/defs.h"
12
13 #if wxUSE_APPLE_IEEE
14
15 #include <math.h>
16
17 /* Copyright (C) 1989-1991 Ken Turkowski. <turk@computer.org>
18 *
19 * All rights reserved.
20 *
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 *
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
35 * happens to be infinity on IEEE machines. Unfortunately, it is
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:
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
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
63 # define HUGE_VAL HUGE
64 #endif HUGE_VAL
65
66
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 ****************************************************************/
74
75 #ifdef applec /* The Apple C compiler works */
76 # define FloatToUnsigned(f) ((wxUint32)(f))
77 # define UnsignedToFloat(u) ((wxFloat64)(u))
78 #else applec
79 # define FloatToUnsigned(f) ((wxUint32)(((wxInt32)((f) - 2147483648.0)) + 2147483647L + 1))
80 # define UnsignedToFloat(u) (((wxFloat64)((wxInt32)((u) - 2147483647L - 1))) + 2147483648.0)
81 #endif applec
82
83
84
85
86 /****************************************************************
87 * Extended precision IEEE floating-point conversion routines.
88 * Extended is an 80-bit number as defined by Motorola,
89 * with a sign bit, 15 bits of exponent (offset 16383?),
90 * and a 64-bit mantissa, with no hidden bit.
91 ****************************************************************/
92
93 wxFloat64 ConvertFromIeeeExtended(wxInt8* bytes)
94 {
95 wxFloat64 f;
96 wxInt32 expon;
97 wxUint32 hiMant, loMant;
98
99 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
100 hiMant = ((wxUint32)(bytes[2] & 0xFF) << 24)
101 | ((wxUint32)(bytes[3] & 0xFF) << 16)
102 | ((wxUint32)(bytes[4] & 0xFF) << 8)
103 | ((wxUint32)(bytes[5] & 0xFF));
104 loMant = ((wxUint32)(bytes[6] & 0xFF) << 24)
105 | ((wxUint32)(bytes[7] & 0xFF) << 16)
106 | ((wxUint32)(bytes[8] & 0xFF) << 8)
107 | ((wxUint32)(bytes[9] & 0xFF));
108
109 if (expon == 0 && hiMant == 0 && loMant == 0) {
110 f = 0;
111 }
112 else {
113 if (expon == 0x7FFF) { /* Infinity or NaN */
114 f = HUGE_VAL;
115 }
116 else {
117 expon -= 16383;
118 f = ldexp(UnsignedToFloat(hiMant), expon-=31);
119 f += ldexp(UnsignedToFloat(loMant), expon-=32);
120 }
121 }
122
123 if (bytes[0] & 0x80)
124 return -f;
125 else
126 return f;
127 }
128
129
130 /****************************************************************/
131
132
133 void ConvertToIeeeExtended(wxFloat64 num, wxInt8 *bytes)
134 {
135 wxInt32 sign;
136 wxInt32 expon;
137 wxFloat64 fMant, fsMant;
138 wxUint32 hiMant, loMant;
139
140 if (num < 0) {
141 sign = 0x8000;
142 num *= -1;
143 } else {
144 sign = 0;
145 }
146
147 if (num == 0) {
148 expon = 0; hiMant = 0; loMant = 0;
149 }
150 else {
151 fMant = frexp(num, &expon);
152 if ((expon > 16384) || !(fMant < 1)) { /* Infinity or NaN */
153 expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
154 }
155 else { /* Finite */
156 expon += 16382;
157 if (expon < 0) { /* denormalized */
158 fMant = ldexp(fMant, expon);
159 expon = 0;
160 }
161 expon |= sign;
162 fMant = ldexp(fMant, 32); fsMant = floor(fMant); hiMant = FloatToUnsigned(fsMant);
163 fMant = ldexp(fMant - fsMant, 32); fsMant = floor(fMant); loMant = FloatToUnsigned(fsMant);
164 }
165 }
166
167 bytes[0] = expon >> 8;
168 bytes[1] = expon;
169 bytes[2] = hiMant >> 24;
170 bytes[3] = hiMant >> 16;
171 bytes[4] = hiMant >> 8;
172 bytes[5] = hiMant;
173 bytes[6] = loMant >> 24;
174 bytes[7] = loMant >> 16;
175 bytes[8] = loMant >> 8;
176 bytes[9] = loMant;
177 }
178
179
180
181 #endif /* wxUSE_APPLE_IEEE */