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