xref: /freebsd/contrib/gdtoa/strtodI.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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
30 	David M. Gay
31 	Bell Laboratories, Room 2C-463
32 	600 Mountain Avenue
33 	Murray Hill, NJ 07974-0636
34 	U.S.A.
35 	dmg@bell-labs.com
36  */
37 
38 #include "gdtoaimp.h"
39 
40  static double
41 #ifdef KR_headers
42 ulpdown(d) double *d;
43 #else
44 ulpdown(double *d)
45 #endif
46 {
47 	double u;
48 	ULong *L = (ULong*)d;
49 
50 	u = ulp(*d);
51 	if (!(L[_1] | L[_0] & 0xfffff)
52 	 && (L[_0] & 0x7ff00000) > 0x00100000)
53 		u *= 0.5;
54 	return u;
55 	}
56 
57  int
58 #ifdef KR_headers
59 strtodI(s, sp, dd) CONST char *s; char **sp; double *dd;
60 #else
61 strtodI(CONST char *s, char **sp, double *dd)
62 #endif
63 {
64 #ifdef Sudden_Underflow
65 	static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, 1 };
66 #else
67 	static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, 0 };
68 #endif
69 	ULong bits[2], sign;
70 	Long exp;
71 	int j, k;
72 	typedef union {
73 		double d[2];
74 		ULong L[4];
75 		} U;
76 	U *u;
77 
78 	k = strtodg(s, sp, &fpi, &exp, bits);
79 	u = (U*)dd;
80 	sign = k & STRTOG_Neg ? 0x80000000L : 0;
81 	switch(k & STRTOG_Retmask) {
82 	  case STRTOG_NoNumber:
83 		u->d[0] = u->d[1] = 0.;
84 		break;
85 
86 	  case STRTOG_Zero:
87 		u->d[0] = u->d[1] = 0.;
88 #ifdef Sudden_Underflow
89 		if (k & STRTOG_Inexact) {
90 			if (sign)
91 				u->L[_0] = 0x80100000L;
92 			else
93 				u->L[2+_0] = 0x100000L;
94 			}
95 		break;
96 #else
97 		goto contain;
98 #endif
99 
100 	  case STRTOG_Denormal:
101 		u->L[_1] = bits[0];
102 		u->L[_0] = bits[1];
103 		goto contain;
104 
105 	  case STRTOG_Normal:
106 		u->L[_1] = bits[0];
107 		u->L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20);
108 	  contain:
109 		j = k & STRTOG_Inexact;
110 		if (sign) {
111 			u->L[_0] |= sign;
112 			j = STRTOG_Inexact - j;
113 			}
114 		switch(j) {
115 		  case STRTOG_Inexlo:
116 #ifdef Sudden_Underflow
117 			if ((u->L[_0] & 0x7ff00000) < 0x3500000) {
118 				u->L[2+_0] = u->L[_0] + 0x3500000;
119 				u->L[2+_1] = u->L[_1];
120 				u->d[1] += ulp(u->d[1]);
121 				u->L[2+_0] -= 0x3500000;
122 				if (!(u->L[2+_0] & 0x7ff00000)) {
123 					u->L[2+_0] = sign;
124 					u->L[2+_1] = 0;
125 					}
126 				}
127 			else
128 #endif
129 			u->d[1] = u->d[0] + ulp(u->d[0]);
130 			break;
131 		  case STRTOG_Inexhi:
132 			u->d[1] = u->d[0];
133 #ifdef Sudden_Underflow
134 			if ((u->L[_0] & 0x7ff00000) < 0x3500000) {
135 				u->L[_0] += 0x3500000;
136 				u->d[0] -= ulpdown(u->d);
137 				u->L[_0] -= 0x3500000;
138 				if (!(u->L[_0] & 0x7ff00000)) {
139 					u->L[_0] = sign;
140 					u->L[_1] = 0;
141 					}
142 				}
143 			else
144 #endif
145 			u->d[0] -= ulpdown(u->d);
146 			break;
147 		  default:
148 			u->d[1] = u->d[0];
149 		  }
150 		break;
151 
152 	  case STRTOG_Infinite:
153 		u->L[_0] = u->L[2+_0] = sign | 0x7ff00000;
154 		u->L[_1] = u->L[2+_1] = 0;
155 		if (k & STRTOG_Inexact) {
156 			if (sign) {
157 				u->L[2+_0] = 0xffefffffL;
158 				u->L[2+_1] = 0xffffffffL;
159 				}
160 			else {
161 				u->L[_0] = 0x7fefffffL;
162 				u->L[_1] = 0xffffffffL;
163 				}
164 			}
165 		break;
166 
167 	  case STRTOG_NaN:
168 		u->L[_0] = u->L[2+_0] = 0x7fffffff | sign;
169 		u->L[_1] = u->L[2+_1] = (ULong)-1;
170 		break;
171 
172 	  case STRTOG_NaNbits:
173 		u->L[_0] = u->L[2+_0] = 0x7ff00000 | sign | bits[1];
174 		u->L[_1] = u->L[2+_1] = bits[0];
175 	  }
176 	return k;
177 	}
178