1 /* Copyright (C) 1989-1991 Apple Computer, Inc.
2 *
3 * All rights reserved.
4 *
5 * Warranty Information
6 * Even though Apple has reviewed this software, Apple makes no warranty
7 * or representation, either express or implied, with respect to this
8 * software, its quality, accuracy, merchantability, or fitness for a
9 * particular purpose. As a result, this software is provided "as is,"
10 * and you, its user, are assuming the entire risk as to its quality
11 * and accuracy.
12 *
13 * This code may be used and freely distributed as long as it includes
14 * this copyright notice and the above warranty information.
15 *
16 * Machine-independent I/O routines for IEEE floating-point numbers.
17 *
18 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
19 * happens to be infinity on IEEE machines. Unfortunately, it is
20 * impossible to preserve NaN's in a machine-independent way.
21 * Infinities are, however, preserved on IEEE machines.
22 *
23 * These routines have been tested on the following machines:
24 * Apple Macintosh, MPW 3.1 C compiler
25 * Apple Macintosh, THINK C compiler
26 * Silicon Graphics IRIS, MIPS compiler
27 * Cray X/MP and Y/MP
28 * Digital Equipment VAX
29 * Sequent Balance (Multiprocesor 386)
30 * NeXT
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 /****************************************************************
47 * Extended precision IEEE floating-point conversion routines.
48 * Extended is an 80-bit number as defined by Motorola,
49 * with a sign bit, 15 bits of exponent (offset 16383?),
50 * and a 64-bit mantissa, with no hidden bit.
51 ****************************************************************/
52
53 #include "extended.h"
54
55 #include <math.h>
56
57 #ifndef HUGE_VAL
58 #define HUGE_VAL HUGE
59 #endif
60
61 #define FloatToUnsigned(f) ((unsigned long) (((long) (f - 2147483648.0)) + 2147483647L) + 1)
62
_af_convert_to_ieee_extended(double num,unsigned char * bytes)63 void _af_convert_to_ieee_extended (double num, unsigned char *bytes)
64 {
65 int sign;
66 int expon;
67 double fMant, fsMant;
68 unsigned long hiMant, loMant;
69
70 if (num < 0) {
71 sign = 0x8000;
72 num *= -1;
73 } else {
74 sign = 0;
75 }
76
77 if (num == 0) {
78 expon = 0; hiMant = 0; loMant = 0;
79 }
80 else {
81 fMant = frexp(num, &expon);
82 if ((expon > 16384) || !(fMant < 1)) { /* Infinity or NaN */
83 expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
84 }
85 else { /* Finite */
86 expon += 16382;
87 if (expon < 0) { /* denormalized */
88 fMant = ldexp(fMant, expon);
89 expon = 0;
90 }
91 expon |= sign;
92 fMant = ldexp(fMant, 32);
93 fsMant = floor(fMant);
94 hiMant = FloatToUnsigned(fsMant);
95 fMant = ldexp(fMant - fsMant, 32);
96 fsMant = floor(fMant);
97 loMant = FloatToUnsigned(fsMant);
98 }
99 }
100
101 bytes[0] = expon >> 8;
102 bytes[1] = expon;
103 bytes[2] = hiMant >> 24;
104 bytes[3] = hiMant >> 16;
105 bytes[4] = hiMant >> 8;
106 bytes[5] = hiMant;
107 bytes[6] = loMant >> 24;
108 bytes[7] = loMant >> 16;
109 bytes[8] = loMant >> 8;
110 bytes[9] = loMant;
111 }
112
113 #define UnsignedToFloat(u) (((double) ((long) (u - 2147483647L - 1))) + 2147483648.0)
114
_af_convert_from_ieee_extended(const unsigned char * bytes)115 double _af_convert_from_ieee_extended (const unsigned char *bytes)
116 {
117 double f;
118 int expon;
119 unsigned long hiMant, loMant;
120
121 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
122 hiMant = ((unsigned long)(bytes[2] & 0xFF) << 24)
123 | ((unsigned long) (bytes[3] & 0xFF) << 16)
124 | ((unsigned long) (bytes[4] & 0xFF) << 8)
125 | ((unsigned long) (bytes[5] & 0xFF));
126 loMant = ((unsigned long) (bytes[6] & 0xFF) << 24)
127 | ((unsigned long) (bytes[7] & 0xFF) << 16)
128 | ((unsigned long) (bytes[8] & 0xFF) << 8)
129 | ((unsigned long) (bytes[9] & 0xFF));
130
131 if (expon == 0 && hiMant == 0 && loMant == 0) {
132 f = 0;
133 }
134 else {
135 if (expon == 0x7FFF) { /* Infinity or NaN */
136 f = HUGE_VAL;
137 }
138 else {
139 expon -= 16383;
140 f = ldexp(UnsignedToFloat(hiMant), expon-=31);
141 f += ldexp(UnsignedToFloat(loMant), expon-=32);
142 }
143 }
144
145 if (bytes[0] & 0x80)
146 return -f;
147 else
148 return f;
149 }
150