1/* $NetBSD: strtodI.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ 2 3/**************************************************************** 4 5The author of this software is David M. Gay. 6 7Copyright (C) 1998, 2000 by Lucent Technologies 8All Rights Reserved 9 10Permission to use, copy, modify, and distribute this software and 11its documentation for any purpose and without fee is hereby 12granted, provided that the above copyright notice appear in all 13copies and that both that the copyright notice and this 14permission notice and warranty disclaimer appear in supporting 15documentation, and that the name of Lucent or any of its entities 16not be used in advertising or publicity pertaining to 17distribution of the software without specific, written prior 18permission. 19 20LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 21INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 22IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 23SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 24WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 25IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 26ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 27THIS 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 38ulpdown(d) U *d; 39#else 40ulpdown(U *d) 41#endif 42{ 43 double u; 44 ULong *L = d->L; 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 55strtodI(s, sp, dd) CONST char *s; char **sp; double *dd; 56#else 57strtodI(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 U *u; 65 66 k = strtodg(s, sp, &fpi, &exp, bits); 67 if (k == STRTOG_NoMemory) 68 return k; 69 u = (U*)dd; 70 sign = k & STRTOG_Neg ? 0x80000000L : 0; 71 switch(k & STRTOG_Retmask) { 72 case STRTOG_NoNumber: 73 dval(&u[0]) = dval(&u[1]) = 0.; 74 break; 75 76 case STRTOG_Zero: 77 dval(&u[0]) = dval(&u[1]) = 0.; 78#ifdef Sudden_Underflow 79 if (k & STRTOG_Inexact) { 80 if (sign) 81 word0(&u[0]) = 0x80100000L; 82 else 83 word0(&u[1]) = 0x100000L; 84 } 85 break; 86#else 87 goto contain; 88#endif 89 90 case STRTOG_Denormal: 91 word1(&u[0]) = bits[0]; 92 word0(&u[0]) = bits[1]; 93 goto contain; 94 95 case STRTOG_Normal: 96 word1(&u[0]) = bits[0]; 97 word0(&u[0]) = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20); 98 contain: 99 j = k & STRTOG_Inexact; 100 if (sign) { 101 word0(&u[0]) |= sign; 102 j = STRTOG_Inexact - j; 103 } 104 switch(j) { 105 case STRTOG_Inexlo: 106#ifdef Sudden_Underflow 107 if ((u->L[_0] & 0x7ff00000) < 0x3500000) { 108 word0(&u[1]) = word0(&u[0]) + 0x3500000; 109 word1(&u[1]) = word1(&u[0]); 110 dval(&u[1]) += ulp(&u[1]); 111 word0(&u[1]) -= 0x3500000; 112 if (!(word0(&u[1]) & 0x7ff00000)) { 113 word0(&u[1]) = sign; 114 word1(&u[1]) = 0; 115 } 116 } 117 else 118#endif 119 dval(&u[1]) = dval(&u[0]) + ulp(&u[0]); 120 break; 121 case STRTOG_Inexhi: 122 dval(&u[1]) = dval(&u[0]); 123#ifdef Sudden_Underflow 124 if ((word0(&u[0]) & 0x7ff00000) < 0x3500000) { 125 word0(&u[0]) += 0x3500000; 126 dval(&u[0]) -= ulpdown(u); 127 word0(&u[0]) -= 0x3500000; 128 if (!(word0(&u[0]) & 0x7ff00000)) { 129 word0(&u[0]) = sign; 130 word1(&u[0]) = 0; 131 } 132 } 133 else 134#endif 135 dval(&u[0]) -= ulpdown(u); 136 break; 137 default: 138 dval(&u[1]) = dval(&u[0]); 139 } 140 break; 141 142 case STRTOG_Infinite: 143 word0(&u[0]) = word0(&u[1]) = sign | 0x7ff00000; 144 word1(&u[0]) = word1(&u[1]) = 0; 145 if (k & STRTOG_Inexact) { 146 if (sign) { 147 word0(&u[1]) = 0xffefffffL; 148 word1(&u[1]) = 0xffffffffL; 149 } 150 else { 151 word0(&u[0]) = 0x7fefffffL; 152 word1(&u[0]) = 0xffffffffL; 153 } 154 } 155 break; 156 157 case STRTOG_NaN: 158 u->L[0] = (u+1)->L[0] = d_QNAN0; 159 u->L[1] = (u+1)->L[1] = d_QNAN1; 160 break; 161 162 case STRTOG_NaNbits: 163 word0(&u[0]) = word0(&u[1]) = 0x7ff00000 | sign | bits[1]; 164 word1(&u[0]) = word1(&u[1]) = bits[0]; 165 } 166 return k; 167 } 168