Imported File#ftype spec from rubyspecs.
[rbx.git] / shotgun / external_libs / libgdtoa / strtopdd.c
blobe569480fe072be9d20c0930cbcef02011e8faa86
1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998, 2000 by Lucent Technologies
6 All Rights Reserved
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.
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.
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
32 #include "gdtoaimp.h"
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
41 #ifdef Sudden_Underflow
42 static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 };
43 #else
44 static FPI fpi = { 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;
55 rv = strtodg(s, sp, &fpi, &exp, bits);
56 u = (U*)dd;
57 switch(rv & STRTOG_Retmask) {
58 case STRTOG_NoNumber:
59 case STRTOG_Zero:
60 u->d[0] = u->d[1] = 0.;
61 break;
63 case STRTOG_Normal:
64 u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL;
65 u->L[_0] = (bits[2] >> 21) | ((bits[3] << 11) & 0xfffff)
66 | ((exp + 0x3ff + 105) << 20);
67 exp += 0x3ff + 52;
68 if (bits[1] &= 0x1fffff) {
69 i = hi0bits(bits[1]) - 11;
70 if (i >= exp) {
71 i = exp - 1;
72 exp = 0;
74 else
75 exp -= i;
76 if (i > 0) {
77 bits[1] = bits[1] << i | bits[0] >> (32-i);
78 bits[0] = bits[0] << i & 0xffffffffL;
81 else if (bits[0]) {
82 i = hi0bits(bits[0]) + 21;
83 if (i >= exp) {
84 i = exp - 1;
85 exp = 0;
87 else
88 exp -= i;
89 if (i < 32) {
90 bits[1] = bits[0] >> (32 - i);
91 bits[0] = bits[0] << i & 0xffffffffL;
93 else {
94 bits[1] = bits[0] << (i - 32);
95 bits[0] = 0;
98 else {
99 u->L[2] = u->L[3] = 0;
100 break;
102 u->L[2+_1] = bits[0];
103 u->L[2+_0] = (bits[1] & 0xfffff) | (exp << 20);
104 break;
106 case STRTOG_Denormal:
107 if (bits[3])
108 goto nearly_normal;
109 if (bits[2])
110 goto partly_normal;
111 if (bits[1] & 0xffe00000)
112 goto hardly_normal;
113 /* completely denormal */
114 u->L[2] = u->L[3] = 0;
115 u->L[_1] = bits[0];
116 u->L[_0] = bits[1];
117 break;
119 nearly_normal:
120 i = hi0bits(bits[3]) - 11; /* i >= 12 */
121 j = 32 - i;
122 u->L[_0] = (((bits[3] << i) | (bits[2] >> j)) & 0xfffff)
123 | ((65 - i) << 20);
124 u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
125 u->L[2+_0] = bits[1] & ((1L << j) - 1);
126 u->L[2+_1] = bits[0];
127 break;
129 partly_normal:
130 i = hi0bits(bits[2]) - 11;
131 if (i < 0) {
132 j = -i;
133 i += 32;
134 u->L[_0] = ((bits[2] >> j) & 0xfffff) | ((33 + j) << 20);
135 u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
136 u->L[2+_0] = bits[1] & ((1L << j) - 1);
137 u->L[2+_1] = bits[0];
138 break;
140 if (i == 0) {
141 u->L[_0] = (bits[2] & 0xfffff) | (33 << 20);
142 u->L[_1] = bits[1];
143 u->L[2+_0] = 0;
144 u->L[2+_1] = bits[0];
145 break;
147 j = 32 - i;
148 u->L[_0] = ((bits[2] << i | bits[1] >> j) & 0xfffff)
149 | ((j + 1) << 20);
150 u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
151 u->L[2+_0] = 0;
152 u->L[2+_1] = bits[0] & ((1L << j) - 1);
153 break;
155 hardly_normal:
156 j = 11 - hi0bits(bits[1]);
157 i = 32 - j;
158 u->L[_0] = ((bits[1] >> j) & 0xfffff) | ((j + 1) << 20);
159 u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
160 u->L[2+_0] = 0;
161 u->L[2+_1] = bits[0] & ((1L << j) - 1);
162 break;
164 case STRTOG_Infinite:
165 u->L[_0] = u->L[2+_0] = 0x7ff00000;
166 u->L[_1] = u->L[2+_1] = 0;
167 break;
169 case STRTOG_NaN:
170 u->L[0] = u->L[2] = d_QNAN0;
171 u->L[1] = u->L[3] = d_QNAN1;
173 if (rv & STRTOG_Neg) {
174 u->L[ _0] |= 0x80000000L;
175 u->L[2+_0] |= 0x80000000L;
177 return rv;