1/* $NetBSD: strtordd.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */
2
3/****************************************************************
4
5The author of this software is David M. Gay.
6
7Copyright (C) 1998, 2000 by Lucent Technologies
8All Rights Reserved
9
10Permission to use, copy, modify, and distribute this software and
11its documentation for any purpose and without fee is hereby
12granted, provided that the above copyright notice appear in all
13copies and that both that the copyright notice and this
14permission notice and warranty disclaimer appear in supporting
15documentation, and that the name of Lucent or any of its entities
16not be used in advertising or publicity pertaining to
17distribution of the software without specific, written prior
18permission.
19
20LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27THIS 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 void
37#ifdef KR_headers
38ULtodd(L, bits, expt, k) ULong *L; ULong *bits; Long expt; int k;
39#else
40ULtodd(ULong *L, ULong *bits, Long expt, 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			  | ((expt + 0x3ff + 105) << 20);
55		expt += 0x3ff + 52;
56		if (bits[1] &= 0x1fffff) {
57			i = hi0bits(bits[1]) - 11;
58			if (i >= expt) {
59				i = expt - 1;
60				expt = 0;
61				}
62			else
63				expt -= 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 >= expt) {
72				i = expt - 1;
73				expt = 0;
74				}
75			else
76				expt -= 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) | (expt << 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[2] = d_QNAN0;
159		L[1] = L[3] = d_QNAN1;
160		break;
161
162	  case STRTOG_NaNbits:
163		L[_1] = (bits[1] >> 21 | bits[2] << 11) & (ULong)0xffffffffL;
164		L[_0] = bits[2] >> 21 | bits[3] << 11
165			  | (ULong)0x7ff00000L;
166		L[2+_1] = bits[0];
167		L[2+_0] = bits[1] | (ULong)0x7ff00000L;
168	  }
169	if (k & STRTOG_Neg) {
170		L[_0] |= 0x80000000L;
171		L[2+_0] |= 0x80000000L;
172		}
173	}
174
175 int
176#ifdef KR_headers
177strtordd(s, sp, rounding, dd) CONST char *s; char **sp; int rounding; double *dd;
178#else
179strtordd(CONST char *s, char **sp, int rounding, double *dd)
180#endif
181{
182#ifdef Sudden_Underflow
183	static CONST FPI fpi0 = { 106, 1-1023, 2046-1023-106+1, 1, 1 };
184#else
185	static CONST FPI fpi0 = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 };
186#endif
187	CONST FPI *fpi;
188	FPI fpi1;
189	ULong bits[4];
190	Long expt;
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, &expt, bits);
200	if (k == STRTOG_NoMemory)
201		return k;
202	ULtodd((ULong*)dd, bits, expt, k);
203	return k;
204	}
205