strtopdd.c revision 1.1.1.2 1 1.1 kleink /****************************************************************
2 1.1 kleink
3 1.1 kleink The author of this software is David M. Gay.
4 1.1 kleink
5 1.1 kleink Copyright (C) 1998, 2000 by Lucent Technologies
6 1.1 kleink All Rights Reserved
7 1.1 kleink
8 1.1 kleink Permission to use, copy, modify, and distribute this software and
9 1.1 kleink its documentation for any purpose and without fee is hereby
10 1.1 kleink granted, provided that the above copyright notice appear in all
11 1.1 kleink copies and that both that the copyright notice and this
12 1.1 kleink permission notice and warranty disclaimer appear in supporting
13 1.1 kleink documentation, and that the name of Lucent or any of its entities
14 1.1 kleink not be used in advertising or publicity pertaining to
15 1.1 kleink distribution of the software without specific, written prior
16 1.1 kleink permission.
17 1.1 kleink
18 1.1 kleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 1.1 kleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 1.1 kleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 1.1 kleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 1.1 kleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 1.1 kleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 1.1 kleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 1.1 kleink THIS SOFTWARE.
26 1.1 kleink
27 1.1 kleink ****************************************************************/
28 1.1 kleink
29 1.1 kleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 1.1 kleink * with " at " changed at "@" and " dot " changed to "."). */
31 1.1 kleink
32 1.1 kleink #include "gdtoaimp.h"
33 1.1 kleink
34 1.1 kleink int
35 1.1 kleink #ifdef KR_headers
36 1.1 kleink strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd;
37 1.1 kleink #else
38 1.1 kleink strtopdd(CONST char *s, char **sp, double *dd)
39 1.1 kleink #endif
40 1.1 kleink {
41 1.1 kleink #ifdef Sudden_Underflow
42 1.1.1.2 christos static FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1 };
43 1.1 kleink #else
44 1.1.1.2 christos static FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 };
45 1.1 kleink #endif
46 1.1 kleink ULong bits[4];
47 1.1 kleink Long exp;
48 1.1 kleink int i, j, rv;
49 1.1 kleink typedef union {
50 1.1 kleink double d[2];
51 1.1 kleink ULong L[4];
52 1.1 kleink } U;
53 1.1 kleink U *u;
54 1.1.1.2 christos #ifdef Honor_FLT_ROUNDS
55 1.1.1.2 christos #include "gdtoa_fltrnds.h"
56 1.1.1.2 christos #else
57 1.1.1.2 christos #define fpi &fpi0
58 1.1.1.2 christos #endif
59 1.1 kleink
60 1.1.1.2 christos rv = strtodg(s, sp, fpi, &exp, bits);
61 1.1 kleink u = (U*)dd;
62 1.1 kleink switch(rv & STRTOG_Retmask) {
63 1.1 kleink case STRTOG_NoNumber:
64 1.1 kleink case STRTOG_Zero:
65 1.1 kleink u->d[0] = u->d[1] = 0.;
66 1.1 kleink break;
67 1.1 kleink
68 1.1 kleink case STRTOG_Normal:
69 1.1 kleink u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL;
70 1.1.1.2 christos u->L[_0] = (bits[2] >> 21) | ((bits[3] << 11) & 0xfffff)
71 1.1.1.2 christos | ((exp + 0x3ff + 105) << 20);
72 1.1 kleink exp += 0x3ff + 52;
73 1.1 kleink if (bits[1] &= 0x1fffff) {
74 1.1 kleink i = hi0bits(bits[1]) - 11;
75 1.1 kleink if (i >= exp) {
76 1.1 kleink i = exp - 1;
77 1.1 kleink exp = 0;
78 1.1 kleink }
79 1.1 kleink else
80 1.1 kleink exp -= i;
81 1.1 kleink if (i > 0) {
82 1.1.1.2 christos bits[1] = bits[1] << i | bits[0] >> (32-i);
83 1.1 kleink bits[0] = bits[0] << i & 0xffffffffL;
84 1.1 kleink }
85 1.1 kleink }
86 1.1 kleink else if (bits[0]) {
87 1.1 kleink i = hi0bits(bits[0]) + 21;
88 1.1 kleink if (i >= exp) {
89 1.1 kleink i = exp - 1;
90 1.1 kleink exp = 0;
91 1.1 kleink }
92 1.1 kleink else
93 1.1 kleink exp -= i;
94 1.1 kleink if (i < 32) {
95 1.1.1.2 christos bits[1] = bits[0] >> (32 - i);
96 1.1 kleink bits[0] = bits[0] << i & 0xffffffffL;
97 1.1 kleink }
98 1.1 kleink else {
99 1.1.1.2 christos bits[1] = bits[0] << (i - 32);
100 1.1 kleink bits[0] = 0;
101 1.1 kleink }
102 1.1 kleink }
103 1.1 kleink else {
104 1.1 kleink u->L[2] = u->L[3] = 0;
105 1.1 kleink break;
106 1.1 kleink }
107 1.1 kleink u->L[2+_1] = bits[0];
108 1.1.1.2 christos u->L[2+_0] = (bits[1] & 0xfffff) | (exp << 20);
109 1.1 kleink break;
110 1.1 kleink
111 1.1 kleink case STRTOG_Denormal:
112 1.1 kleink if (bits[3])
113 1.1 kleink goto nearly_normal;
114 1.1 kleink if (bits[2])
115 1.1 kleink goto partly_normal;
116 1.1 kleink if (bits[1] & 0xffe00000)
117 1.1 kleink goto hardly_normal;
118 1.1 kleink /* completely denormal */
119 1.1 kleink u->L[2] = u->L[3] = 0;
120 1.1 kleink u->L[_1] = bits[0];
121 1.1 kleink u->L[_0] = bits[1];
122 1.1 kleink break;
123 1.1 kleink
124 1.1 kleink nearly_normal:
125 1.1 kleink i = hi0bits(bits[3]) - 11; /* i >= 12 */
126 1.1 kleink j = 32 - i;
127 1.1.1.2 christos u->L[_0] = ((bits[3] << i | bits[2] >> j) & 0xfffff)
128 1.1.1.2 christos | ((65 - i) << 20);
129 1.1 kleink u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
130 1.1.1.2 christos u->L[2+_0] = bits[1] & ((1L << j) - 1);
131 1.1 kleink u->L[2+_1] = bits[0];
132 1.1 kleink break;
133 1.1 kleink
134 1.1 kleink partly_normal:
135 1.1 kleink i = hi0bits(bits[2]) - 11;
136 1.1 kleink if (i < 0) {
137 1.1 kleink j = -i;
138 1.1 kleink i += 32;
139 1.1.1.2 christos u->L[_0] = (bits[2] >> j & 0xfffff) | (33 + j) << 20;
140 1.1.1.2 christos u->L[_1] = ((bits[2] << i) | (bits[1] >> j)) & 0xffffffffL;
141 1.1.1.2 christos u->L[2+_0] = bits[1] & ((1L << j) - 1);
142 1.1 kleink u->L[2+_1] = bits[0];
143 1.1 kleink break;
144 1.1 kleink }
145 1.1 kleink if (i == 0) {
146 1.1.1.2 christos u->L[_0] = (bits[2] & 0xfffff) | (33 << 20);
147 1.1 kleink u->L[_1] = bits[1];
148 1.1 kleink u->L[2+_0] = 0;
149 1.1 kleink u->L[2+_1] = bits[0];
150 1.1 kleink break;
151 1.1 kleink }
152 1.1 kleink j = 32 - i;
153 1.1.1.2 christos u->L[_0] = (((bits[2] << i) | (bits[1] >> j)) & 0xfffff)
154 1.1.1.2 christos | ((j + 1) << 20);
155 1.1 kleink u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
156 1.1 kleink u->L[2+_0] = 0;
157 1.1.1.2 christos u->L[2+_1] = bits[0] & ((1L << j) - 1);
158 1.1 kleink break;
159 1.1 kleink
160 1.1 kleink hardly_normal:
161 1.1 kleink j = 11 - hi0bits(bits[1]);
162 1.1 kleink i = 32 - j;
163 1.1.1.2 christos u->L[_0] = (bits[1] >> j & 0xfffff) | ((j + 1) << 20);
164 1.1 kleink u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
165 1.1 kleink u->L[2+_0] = 0;
166 1.1.1.2 christos u->L[2+_1] = bits[0] & ((1L << j) - 1);
167 1.1 kleink break;
168 1.1 kleink
169 1.1 kleink case STRTOG_Infinite:
170 1.1 kleink u->L[_0] = u->L[2+_0] = 0x7ff00000;
171 1.1 kleink u->L[_1] = u->L[2+_1] = 0;
172 1.1 kleink break;
173 1.1 kleink
174 1.1 kleink case STRTOG_NaN:
175 1.1 kleink u->L[0] = u->L[2] = d_QNAN0;
176 1.1 kleink u->L[1] = u->L[3] = d_QNAN1;
177 1.1 kleink }
178 1.1 kleink if (rv & STRTOG_Neg) {
179 1.1 kleink u->L[ _0] |= 0x80000000L;
180 1.1 kleink u->L[2+_0] |= 0x80000000L;
181 1.1 kleink }
182 1.1 kleink return rv;
183 1.1 kleink }
184