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