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