Elimiante include of defs.h for VisualAge compiles
[wxWidgets.git] / src / common / extended.c
1 #ifndef __VISAGECPP__ /* DW: why does anyone need this for this module? */
2 #include "wx/defs.h"
3 #endif
4
5 #include "wx/setup.h"
6
7 #include <math.h>
8
9 #if wxUSE_APPLE_IEEE
10
11 /*
12 * C O N V E R T T O I E E E E X T E N D E D
13 */
14
15 /* Copyright (C) 1988-1991 Apple Computer, Inc.
16 * All rights reserved.
17 *
18 * Machine-independent I/O routines for IEEE floating-point numbers.
19 *
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.
24 *
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
29 * Cray X/MP and Y/MP
30 * Digital Equipment VAX
31 *
32 *
33 * Implemented by Malcolm Slaney and Ken Turkowski.
34 *
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.
39 *
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.
44 */
45
46 #ifndef HUGE_VAL
47 #define HUGE_VAL HUGE
48 #endif /*HUGE_VAL*/
49
50 #define FloatToUnsigned(f) ((unsigned long) (((long) (f - 2147483648.0)) + 2147483647L) + 1)
51
52 void ConvertToIeeeExtended(double num, unsigned char *bytes)
53 {
54 int sign;
55 int expon;
56 double fMant, fsMant;
57 unsigned long hiMant, loMant;
58
59 if (num < 0) {
60 sign = 0x8000;
61 num *= -1;
62 } else {
63 sign = 0;
64 }
65
66 if (num == 0) {
67 expon = 0; hiMant = 0; loMant = 0;
68 }
69 else {
70 fMant = frexp(num, &expon);
71 if ((expon > 16384) || !(fMant < 1)) { /* Infinity or NaN */
72 expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
73 }
74 else { /* Finite */
75 expon += 16382;
76 if (expon < 0) { /* denormalized */
77 fMant = ldexp(fMant, expon);
78 expon = 0;
79 }
80 expon |= sign;
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);
87 }
88 }
89
90 /* disable the warning about 'possible loss of data' & 'conversion between
91 * diff types' */
92 #ifdef _MSC_VER
93 #pragma warning(disable: 4244)
94 #pragma warning(disable: 4135)
95 #endif /* Visual C++ */
96
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);
107
108 #ifdef _MSC_VER
109 #pragma warning(default: 4244)
110 #pragma warning(default: 4135)
111 #endif /* Visual C++ */
112 }
113
114 /*
115 * C O N V E R T F R O M I E E E E X T E N D E D
116 */
117
118 /*
119 * Copyright (C) 1988-1991 Apple Computer, Inc.
120 * All rights reserved.
121 *
122 * Machine-independent I/O routines for IEEE floating-point numbers.
123 *
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.
128 *
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
133 * Cray X/MP and Y/MP
134 * Digital Equipment VAX
135 *
136 *
137 * Implemented by Malcolm Slaney and Ken Turkowski.
138 *
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.
143 *
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.
148 */
149
150 #ifndef HUGE_VAL
151 # define HUGE_VAL HUGE
152 #endif /*HUGE_VAL*/
153
154 # define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0)
155
156 /****************************************************************
157 * Extended precision IEEE floating-point conversion routine.
158 ****************************************************************/
159
160 double ConvertFromIeeeExtended(const unsigned char *bytes)
161 {
162 double f;
163 int expon;
164 unsigned long hiMant, loMant;
165
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));
175
176 if (expon == 0 && hiMant == 0 && loMant == 0) {
177 f = 0;
178 }
179 else {
180 if (expon == 0x7FFF) { /* Infinity or NaN */
181 f = HUGE_VAL;
182 }
183 else {
184 expon -= 16383;
185 f = ldexp(UnsignedToFloat(hiMant), expon-=31);
186 f += ldexp(UnsignedToFloat(loMant), expon-=32);
187 }
188 }
189
190 if (bytes[0] & 0x80)
191 return -f;
192 else
193 return f;
194 }
195
196 #endif /* wxUSE_APPLE_IEEE */