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