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