1 /* $NetBSD: strtodI.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) 1998, 2000 by Lucent Technologies 8 All Rights Reserved 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 /* Please send bug reports to David M. Gay (dmg at acm dot org, 32 * with " at " changed at "@" and " dot " changed to "."). */ 33 34 #include "gdtoaimp.h" 35 36 static double 37 #ifdef KR_headers 38 ulpdown(d) double *d; 39 #else 40 ulpdown(double *d) 41 #endif 42 { 43 double u; 44 ULong *L = (ULong*)d; 45 46 u = ulp(*d); 47 if (!(L[_1] | L[_0] & 0xfffff) 48 && (L[_0] & 0x7ff00000) > 0x00100000) 49 u *= 0.5; 50 return u; 51 } 52 53 int 54 #ifdef KR_headers 55 strtodI(s, sp, dd) CONST char *s; char **sp; double *dd; 56 #else 57 strtodI(CONST char *s, char **sp, double *dd) 58 #endif 59 { 60 static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI }; 61 ULong bits[2], sign; 62 Long exp; 63 int j, k; 64 typedef union { 65 double d[2]; 66 ULong L[4]; 67 } U; 68 U *u; 69 70 k = strtodg(s, sp, &fpi, &exp, bits); 71 if (k == STRTOG_NoMemory) 72 return k; 73 u = (U*)dd; 74 sign = k & STRTOG_Neg ? 0x80000000L : 0; 75 switch(k & STRTOG_Retmask) { 76 case STRTOG_NoNumber: 77 u->d[0] = u->d[1] = 0.; 78 break; 79 80 case STRTOG_Zero: 81 u->d[0] = u->d[1] = 0.; 82 #ifdef Sudden_Underflow 83 if (k & STRTOG_Inexact) { 84 if (sign) 85 u->L[_0] = 0x80100000L; 86 else 87 u->L[2+_0] = 0x100000L; 88 } 89 break; 90 #else 91 goto contain; 92 #endif 93 94 case STRTOG_Denormal: 95 u->L[_1] = bits[0]; 96 u->L[_0] = bits[1]; 97 goto contain; 98 99 case STRTOG_Normal: 100 u->L[_1] = bits[0]; 101 u->L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); 102 contain: 103 j = k & STRTOG_Inexact; 104 if (sign) { 105 u->L[_0] |= sign; 106 j = STRTOG_Inexact - j; 107 } 108 switch(j) { 109 case STRTOG_Inexlo: 110 #ifdef Sudden_Underflow 111 if ((u->L[_0] & 0x7ff00000) < 0x3500000) { 112 u->L[2+_0] = u->L[_0] + 0x3500000; 113 u->L[2+_1] = u->L[_1]; 114 u->d[1] += ulp(u->d[1]); 115 u->L[2+_0] -= 0x3500000; 116 if (!(u->L[2+_0] & 0x7ff00000)) { 117 u->L[2+_0] = sign; 118 u->L[2+_1] = 0; 119 } 120 } 121 else 122 #endif 123 u->d[1] = u->d[0] + ulp(u->d[0]); 124 break; 125 case STRTOG_Inexhi: 126 u->d[1] = u->d[0]; 127 #ifdef Sudden_Underflow 128 if ((u->L[_0] & 0x7ff00000) < 0x3500000) { 129 u->L[_0] += 0x3500000; 130 u->d[0] -= ulpdown(u->d); 131 u->L[_0] -= 0x3500000; 132 if (!(u->L[_0] & 0x7ff00000)) { 133 u->L[_0] = sign; 134 u->L[_1] = 0; 135 } 136 } 137 else 138 #endif 139 u->d[0] -= ulpdown(u->d); 140 break; 141 default: 142 u->d[1] = u->d[0]; 143 } 144 break; 145 146 case STRTOG_Infinite: 147 u->L[_0] = u->L[2+_0] = sign | 0x7ff00000; 148 u->L[_1] = u->L[2+_1] = 0; 149 if (k & STRTOG_Inexact) { 150 if (sign) { 151 u->L[2+_0] = 0xffefffffL; 152 u->L[2+_1] = 0xffffffffL; 153 } 154 else { 155 u->L[_0] = 0x7fefffffL; 156 u->L[_1] = 0xffffffffL; 157 } 158 } 159 break; 160 161 case STRTOG_NaN: 162 u->L[0] = u->L[2] = d_QNAN0; 163 u->L[1] = u->L[3] = d_QNAN1; 164 break; 165 166 case STRTOG_NaNbits: 167 u->L[_0] = u->L[2+_0] = 0x7ff00000 | sign | bits[1]; 168 u->L[_1] = u->L[2+_1] = bits[0]; 169 } 170 return k; 171 } 172