fix non-standard c warnings - stab to fix overflow warnings
[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 * 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.
90 ****************************************************************/
91
92 wxFloat64 ConvertFromIeeeExtended(wxInt8* bytes)
93 {
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;
126 }
127
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
180 #endif /* wxUSE_APPLE_IEEE */