xref: /netbsd-src/lib/libc/gdtoa/strtodI.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: strtodI.c,v 1.1.1.1 2006/01/25 15:18:51 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  static double
37 #ifdef KR_headers
38 ulpdown(d) double *d;
39 #else
40 ulpdown(double *d)
41 #endif
42 {
43 	double u;
44 	ULong *L = (ULong*)d;
45 
46 	u = ulp(*d);
47 	if (!(L[_1] | L[_0] & 0xfffff)
48 	 && (L[_0] & 0x7ff00000) > 0x00100000)
49 		u *= 0.5;
50 	return u;
51 	}
52 
53  int
54 #ifdef KR_headers
55 strtodI(s, sp, dd) CONST char *s; char **sp; double *dd;
56 #else
57 strtodI(CONST char *s, char **sp, double *dd)
58 #endif
59 {
60 	static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI };
61 	ULong bits[2], sign;
62 	Long exp;
63 	int j, k;
64 	typedef union {
65 		double d[2];
66 		ULong L[4];
67 		} U;
68 	U *u;
69 
70 	k = strtodg(s, sp, &fpi, &exp, bits);
71 	u = (U*)dd;
72 	sign = k & STRTOG_Neg ? 0x80000000L : 0;
73 	switch(k & STRTOG_Retmask) {
74 	  case STRTOG_NoNumber:
75 		u->d[0] = u->d[1] = 0.;
76 		break;
77 
78 	  case STRTOG_Zero:
79 		u->d[0] = u->d[1] = 0.;
80 #ifdef Sudden_Underflow
81 		if (k & STRTOG_Inexact) {
82 			if (sign)
83 				u->L[_0] = 0x80100000L;
84 			else
85 				u->L[2+_0] = 0x100000L;
86 			}
87 		break;
88 #else
89 		goto contain;
90 #endif
91 
92 	  case STRTOG_Denormal:
93 		u->L[_1] = bits[0];
94 		u->L[_0] = bits[1];
95 		goto contain;
96 
97 	  case STRTOG_Normal:
98 		u->L[_1] = bits[0];
99 		u->L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20);
100 	  contain:
101 		j = k & STRTOG_Inexact;
102 		if (sign) {
103 			u->L[_0] |= sign;
104 			j = STRTOG_Inexact - j;
105 			}
106 		switch(j) {
107 		  case STRTOG_Inexlo:
108 #ifdef Sudden_Underflow
109 			if ((u->L[_0] & 0x7ff00000) < 0x3500000) {
110 				u->L[2+_0] = u->L[_0] + 0x3500000;
111 				u->L[2+_1] = u->L[_1];
112 				u->d[1] += ulp(u->d[1]);
113 				u->L[2+_0] -= 0x3500000;
114 				if (!(u->L[2+_0] & 0x7ff00000)) {
115 					u->L[2+_0] = sign;
116 					u->L[2+_1] = 0;
117 					}
118 				}
119 			else
120 #endif
121 			u->d[1] = u->d[0] + ulp(u->d[0]);
122 			break;
123 		  case STRTOG_Inexhi:
124 			u->d[1] = u->d[0];
125 #ifdef Sudden_Underflow
126 			if ((u->L[_0] & 0x7ff00000) < 0x3500000) {
127 				u->L[_0] += 0x3500000;
128 				u->d[0] -= ulpdown(u->d);
129 				u->L[_0] -= 0x3500000;
130 				if (!(u->L[_0] & 0x7ff00000)) {
131 					u->L[_0] = sign;
132 					u->L[_1] = 0;
133 					}
134 				}
135 			else
136 #endif
137 			u->d[0] -= ulpdown(u->d);
138 			break;
139 		  default:
140 			u->d[1] = u->d[0];
141 		  }
142 		break;
143 
144 	  case STRTOG_Infinite:
145 		u->L[_0] = u->L[2+_0] = sign | 0x7ff00000;
146 		u->L[_1] = u->L[2+_1] = 0;
147 		if (k & STRTOG_Inexact) {
148 			if (sign) {
149 				u->L[2+_0] = 0xffefffffL;
150 				u->L[2+_1] = 0xffffffffL;
151 				}
152 			else {
153 				u->L[_0] = 0x7fefffffL;
154 				u->L[_1] = 0xffffffffL;
155 				}
156 			}
157 		break;
158 
159 	  case STRTOG_NaN:
160 		u->L[0] = u->L[2] = d_QNAN0;
161 		u->L[1] = u->L[3] = d_QNAN1;
162 		break;
163 
164 	  case STRTOG_NaNbits:
165 		u->L[_0] = u->L[2+_0] = 0x7ff00000 | sign | bits[1];
166 		u->L[_1] = u->L[2+_1] = bits[0];
167 	  }
168 	return k;
169 	}
170