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