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