xref: /freebsd/contrib/gdtoa/strtodnrp.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /****************************************************************
2 
3 The author of this software is David M. Gay.
4 
5 Copyright (C) 2004 by David M. Gay.
6 All Rights Reserved
7 Based on material in the rest of /netlib/fp/gdota.tar.gz,
8 which is copyright (C) 1998, 2000 by Lucent Technologies.
9 
10 Permission to use, copy, modify, and distribute this software and
11 its documentation for any purpose and without fee is hereby
12 granted, provided that the above copyright notice appear in all
13 copies and that both that the copyright notice and this
14 permission notice and warranty disclaimer appear in supporting
15 documentation, and that the name of Lucent or any of its entities
16 not be used in advertising or publicity pertaining to
17 distribution of the software without specific, written prior
18 permission.
19 
20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 THIS SOFTWARE.
28 
29 ****************************************************************/
30 
31 /* This is a variant of strtod that works on Intel ia32 systems */
32 /* with the default extended-precision arithmetic -- it does not */
33 /* require setting the precision control to 53 bits.  */
34 
35 /* Please send bug reports to David M. Gay (dmg at acm dot org,
36  * with " at " changed at "@" and " dot " changed to ".").	*/
37 
38 #include "gdtoaimp.h"
39 
40  double
41 #ifdef KR_headers
42 strtod(s, sp) CONST char *s; char **sp;
43 #else
44 strtod(CONST char *s, char **sp)
45 #endif
46 {
47 	static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI };
48 	ULong bits[2];
49 	Long exp;
50 	int k;
51 	union { ULong L[2]; double d; } u;
52 
53 	k = strtodg(s, sp, &fpi, &exp, bits);
54 	switch(k & STRTOG_Retmask) {
55 	  case STRTOG_NoNumber:
56 	  case STRTOG_Zero:
57 		u.L[0] = u.L[1] = 0;
58 		break;
59 
60 	  case STRTOG_Normal:
61 		u.L[_1] = bits[0];
62 		u.L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20);
63 		break;
64 
65 	  case STRTOG_Denormal:
66 		u.L[_1] = bits[0];
67 		u.L[_0] = bits[1];
68 		break;
69 
70 	  case STRTOG_Infinite:
71 		u.L[_0] = 0x7ff00000;
72 		u.L[_1] = 0;
73 		break;
74 
75 	  case STRTOG_NaN:
76 		u.L[0] = d_QNAN0;
77 		u.L[1] = d_QNAN1;
78 		break;
79 
80 	  case STRTOG_NaNbits:
81 		u.L[_0] = 0x7ff00000 | bits[1];
82 		u.L[_1] = bits[0];
83 	  }
84 	if (k & STRTOG_Neg)
85 		u.L[_0] |= 0x80000000L;
86 	return u.d;
87 	}
88