]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/gdtoa/strtopdd.c
This commit was generated by cvs2svn to compensate for changes in r159764,
[FreeBSD/FreeBSD.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
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  int
41 #ifdef KR_headers
42 strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd;
43 #else
44 strtopdd(CONST char *s, char **sp, double *dd)
45 #endif
46 {
47 #ifdef Sudden_Underflow
48         static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 };
49 #else
50         static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 };
51 #endif
52         ULong bits[4];
53         Long exp;
54         int i, j, rv;
55         typedef union {
56                 double d[2];
57                 ULong L[4];
58                 } U;
59         U *u;
60
61         rv = strtodg(s, sp, &fpi, &exp, bits);
62         u = (U*)dd;
63         switch(rv & STRTOG_Retmask) {
64           case STRTOG_NoNumber:
65           case STRTOG_Zero:
66                 u->d[0] = u->d[1] = 0.;
67                 break;
68
69           case STRTOG_Normal:
70                 u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL;
71                 u->L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff
72                           | exp + 0x3ff + 105 << 20;
73                 exp += 0x3ff + 52;
74                 if (bits[1] &= 0x1fffff) {
75                         i = hi0bits(bits[1]) - 11;
76                         if (i >= exp) {
77                                 i = exp - 1;
78                                 exp = 0;
79                                 }
80                         else
81                                 exp -= i;
82                         if (i > 0) {
83                                 bits[1] = bits[1] << i | bits[0] >> 32-i;
84                                 bits[0] = bits[0] << i & 0xffffffffL;
85                                 }
86                         }
87                 else if (bits[0]) {
88                         i = hi0bits(bits[0]) + 21;
89                         if (i >= exp) {
90                                 i = exp - 1;
91                                 exp = 0;
92                                 }
93                         else
94                                 exp -= i;
95                         if (i < 32) {
96                                 bits[1] = bits[0] >> 32 - i;
97                                 bits[0] = bits[0] << i & 0xffffffffL;
98                                 }
99                         else {
100                                 bits[1] = bits[0] << i - 32;
101                                 bits[0] = 0;
102                                 }
103                         }
104                 else {
105                         u->L[2] = u->L[3] = 0;
106                         break;
107                         }
108                 u->L[2+_1] = bits[0];
109                 u->L[2+_0] = bits[1] & 0xfffff | exp << 20;
110                 break;
111
112           case STRTOG_Denormal:
113                 if (bits[3])
114                         goto nearly_normal;
115                 if (bits[2])
116                         goto partly_normal;
117                 if (bits[1] & 0xffe00000)
118                         goto hardly_normal;
119                 /* completely denormal */
120                 u->L[2] = u->L[3] = 0;
121                 u->L[_1] = bits[0];
122                 u->L[_0] = bits[1];
123                 break;
124
125           nearly_normal:
126                 i = hi0bits(bits[3]) - 11;      /* i >= 12 */
127                 j = 32 - i;
128                 u->L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff
129                         | 65 - i << 20;
130                 u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
131                 u->L[2+_0] = bits[1] & (1L << j) - 1;
132                 u->L[2+_1] = bits[0];
133                 break;
134
135           partly_normal:
136                 i = hi0bits(bits[2]) - 11;
137                 if (i < 0) {
138                         j = -i;
139                         i += 32;
140                         u->L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20;
141                         u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
142                         u->L[2+_0] = bits[1] & (1L << j) - 1;
143                         u->L[2+_1] = bits[0];
144                         break;
145                         }
146                 if (i == 0) {
147                         u->L[_0] = bits[2] & 0xfffff | 33 << 20;
148                         u->L[_1] = bits[1];
149                         u->L[2+_0] = 0;
150                         u->L[2+_1] = bits[0];
151                         break;
152                         }
153                 j = 32 - i;
154                 u->L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff
155                                 | j + 1 << 20;
156                 u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
157                 u->L[2+_0] = 0;
158                 u->L[2+_1] = bits[0] & (1L << j) - 1;
159                 break;
160
161           hardly_normal:
162                 j = 11 - hi0bits(bits[1]);
163                 i = 32 - j;
164                 u->L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20;
165                 u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
166                 u->L[2+_0] = 0;
167                 u->L[2+_1] = bits[0] & (1L << j) - 1;
168                 break;
169
170           case STRTOG_Infinite:
171                 u->L[_0] = u->L[2+_0] = 0x7ff00000;
172                 u->L[_1] = u->L[2+_1] = 0;
173                 break;
174
175           case STRTOG_NaN:
176                 u->L[_0] = u->L[2+_0] = 0x7fffffff;
177                 u->L[_1] = u->L[2+_1] = (ULong)-1;
178           }
179         if (rv & STRTOG_Neg) {
180                 u->L[  _0] |= 0x80000000L;
181                 u->L[2+_0] |= 0x80000000L;
182                 }
183         return rv;
184         }