xref: /netbsd-src/lib/libc/gdtoa/strtopdd.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: strtopdd.c,v 1.1.1.1 2006/01/25 15:18:53 kleink 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
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 fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 };
45 #else
46 	static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 };
47 #endif
48 	ULong bits[4];
49 	Long exp;
50 	int i, j, rv;
51 	typedef union {
52 		double d[2];
53 		ULong L[4];
54 		} U;
55 	U *u;
56 
57 	rv = strtodg(s, sp, &fpi, &exp, bits);
58 	u = (U*)dd;
59 	switch(rv & STRTOG_Retmask) {
60 	  case STRTOG_NoNumber:
61 	  case STRTOG_Zero:
62 		u->d[0] = u->d[1] = 0.;
63 		break;
64 
65 	  case STRTOG_Normal:
66 		u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL;
67 		u->L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff
68 			  | exp + 0x3ff + 105 << 20;
69 		exp += 0x3ff + 52;
70 		if (bits[1] &= 0x1fffff) {
71 			i = hi0bits(bits[1]) - 11;
72 			if (i >= exp) {
73 				i = exp - 1;
74 				exp = 0;
75 				}
76 			else
77 				exp -= i;
78 			if (i > 0) {
79 				bits[1] = bits[1] << i | bits[0] >> 32-i;
80 				bits[0] = bits[0] << i & 0xffffffffL;
81 				}
82 			}
83 		else if (bits[0]) {
84 			i = hi0bits(bits[0]) + 21;
85 			if (i >= exp) {
86 				i = exp - 1;
87 				exp = 0;
88 				}
89 			else
90 				exp -= i;
91 			if (i < 32) {
92 				bits[1] = bits[0] >> 32 - i;
93 				bits[0] = bits[0] << i & 0xffffffffL;
94 				}
95 			else {
96 				bits[1] = bits[0] << i - 32;
97 				bits[0] = 0;
98 				}
99 			}
100 		else {
101 			u->L[2] = u->L[3] = 0;
102 			break;
103 			}
104 		u->L[2+_1] = bits[0];
105 		u->L[2+_0] = bits[1] & 0xfffff | exp << 20;
106 		break;
107 
108 	  case STRTOG_Denormal:
109 		if (bits[3])
110 			goto nearly_normal;
111 		if (bits[2])
112 			goto partly_normal;
113 		if (bits[1] & 0xffe00000)
114 			goto hardly_normal;
115 		/* completely denormal */
116 		u->L[2] = u->L[3] = 0;
117 		u->L[_1] = bits[0];
118 		u->L[_0] = bits[1];
119 		break;
120 
121 	  nearly_normal:
122 		i = hi0bits(bits[3]) - 11;	/* i >= 12 */
123 		j = 32 - i;
124 		u->L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff
125 			| 65 - i << 20;
126 		u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
127 		u->L[2+_0] = bits[1] & (1L << j) - 1;
128 		u->L[2+_1] = bits[0];
129 		break;
130 
131 	  partly_normal:
132 		i = hi0bits(bits[2]) - 11;
133 		if (i < 0) {
134 			j = -i;
135 			i += 32;
136 			u->L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20;
137 			u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
138 			u->L[2+_0] = bits[1] & (1L << j) - 1;
139 			u->L[2+_1] = bits[0];
140 			break;
141 			}
142 		if (i == 0) {
143 			u->L[_0] = bits[2] & 0xfffff | 33 << 20;
144 			u->L[_1] = bits[1];
145 			u->L[2+_0] = 0;
146 			u->L[2+_1] = bits[0];
147 			break;
148 			}
149 		j = 32 - i;
150 		u->L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff
151 				| j + 1 << 20;
152 		u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
153 		u->L[2+_0] = 0;
154 		u->L[2+_1] = bits[0] & (1L << j) - 1;
155 		break;
156 
157 	  hardly_normal:
158 		j = 11 - hi0bits(bits[1]);
159 		i = 32 - j;
160 		u->L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20;
161 		u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
162 		u->L[2+_0] = 0;
163 		u->L[2+_1] = bits[0] & (1L << j) - 1;
164 		break;
165 
166 	  case STRTOG_Infinite:
167 		u->L[_0] = u->L[2+_0] = 0x7ff00000;
168 		u->L[_1] = u->L[2+_1] = 0;
169 		break;
170 
171 	  case STRTOG_NaN:
172 		u->L[0] = u->L[2] = d_QNAN0;
173 		u->L[1] = u->L[3] = d_QNAN1;
174 	  }
175 	if (rv & STRTOG_Neg) {
176 		u->L[  _0] |= 0x80000000L;
177 		u->L[2+_0] |= 0x80000000L;
178 		}
179 	return rv;
180 	}
181