]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/gdtoa/strtopdd.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / gdtoa / strtopdd.c
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 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;
54
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;
62
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;
73                                 }
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;
79                                 }
80                         }
81                 else if (bits[0]) {
82                         i = hi0bits(bits[0]) + 21;
83                         if (i >= exp) {
84                                 i = exp - 1;
85                                 exp = 0;
86                                 }
87                         else
88                                 exp -= i;
89                         if (i < 32) {
90                                 bits[1] = bits[0] >> 32 - i;
91                                 bits[0] = bits[0] << i & 0xffffffffL;
92                                 }
93                         else {
94                                 bits[1] = bits[0] << i - 32;
95                                 bits[0] = 0;
96                                 }
97                         }
98                 else {
99                         u->L[2] = u->L[3] = 0;
100                         break;
101                         }
102                 u->L[2+_1] = bits[0];
103                 u->L[2+_0] = bits[1] & 0xfffff | exp << 20;
104                 break;
105
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;
118
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;
128
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;
139                         }
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;
146                         }
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;
154
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;
163
164           case STRTOG_Infinite:
165                 u->L[_0] = u->L[2+_0] = 0x7ff00000;
166                 u->L[_1] = u->L[2+_1] = 0;
167                 break;
168
169           case STRTOG_NaN:
170                 u->L[0] = u->L[2] = d_QNAN0;
171                 u->L[1] = u->L[3] = d_QNAN1;
172           }
173         if (rv & STRTOG_Neg) {
174                 u->L[  _0] |= 0x80000000L;
175                 u->L[2+_0] |= 0x80000000L;
176                 }
177         return rv;
178         }