strtodnrp.c revision 1.2
11.2Schristos/* $NetBSD: strtodnrp.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */
21.1Skleink
31.1Skleink/****************************************************************
41.1Skleink
51.1SkleinkThe author of this software is David M. Gay.
61.1Skleink
71.1SkleinkCopyright (C) 2004 by David M. Gay.
81.1SkleinkAll Rights Reserved
91.1SkleinkBased on material in the rest of /netlib/fp/gdota.tar.gz,
101.1Skleinkwhich is copyright (C) 1998, 2000 by Lucent Technologies.
111.1Skleink
121.1SkleinkPermission to use, copy, modify, and distribute this software and
131.1Skleinkits documentation for any purpose and without fee is hereby
141.1Skleinkgranted, provided that the above copyright notice appear in all
151.1Skleinkcopies and that both that the copyright notice and this
161.1Skleinkpermission notice and warranty disclaimer appear in supporting
171.1Skleinkdocumentation, and that the name of Lucent or any of its entities
181.1Skleinknot be used in advertising or publicity pertaining to
191.1Skleinkdistribution of the software without specific, written prior
201.1Skleinkpermission.
211.1Skleink
221.1SkleinkLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
231.1SkleinkINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
241.1SkleinkIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
251.1SkleinkSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
261.1SkleinkWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
271.1SkleinkIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
281.1SkleinkARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
291.1SkleinkTHIS SOFTWARE.
301.1Skleink
311.1Skleink****************************************************************/
321.1Skleink
331.1Skleink/* This is a variant of strtod that works on Intel ia32 systems */
341.1Skleink/* with the default extended-precision arithmetic -- it does not */
351.1Skleink/* require setting the precision control to 53 bits.  */
361.1Skleink
371.1Skleink/* Please send bug reports to David M. Gay (dmg at acm dot org,
381.1Skleink * with " at " changed at "@" and " dot " changed to ".").	*/
391.1Skleink
401.1Skleink#include "gdtoaimp.h"
411.1Skleink
421.1Skleink double
431.1Skleink#ifdef KR_headers
441.1Skleinkstrtod(s, sp) CONST char *s; char **sp;
451.1Skleink#else
461.1Skleinkstrtod(CONST char *s, char **sp)
471.1Skleink#endif
481.1Skleink{
491.1Skleink	static FPI fpi = { 53, 1-1023-53+1, 2046-1023-53+1, 1, SI };
501.1Skleink	ULong bits[2];
511.1Skleink	Long exp;
521.1Skleink	int k;
531.1Skleink	union { ULong L[2]; double d; } u;
541.1Skleink
551.1Skleink	k = strtodg(s, sp, &fpi, &exp, bits);
561.2Schristos	if (k == STRTOG_NoMemory) {
571.2Schristos		errno = ERANGE;
581.2Schristos		u.L[0] = Big0;
591.2Schristos		u.L[1] = Big1;
601.2Schristos		return u.d;
611.2Schristos	}
621.1Skleink	switch(k & STRTOG_Retmask) {
631.1Skleink	  case STRTOG_NoNumber:
641.1Skleink	  case STRTOG_Zero:
651.1Skleink		u.L[0] = u.L[1] = 0;
661.1Skleink		break;
671.1Skleink
681.1Skleink	  case STRTOG_Normal:
691.1Skleink		u.L[_1] = bits[0];
701.1Skleink		u.L[_0] = (bits[1] & ~0x100000) | ((exp + 0x3ff + 52) << 20);
711.1Skleink		break;
721.1Skleink
731.1Skleink	  case STRTOG_Denormal:
741.1Skleink		u.L[_1] = bits[0];
751.1Skleink		u.L[_0] = bits[1];
761.1Skleink		break;
771.1Skleink
781.1Skleink	  case STRTOG_Infinite:
791.1Skleink		u.L[_0] = 0x7ff00000;
801.1Skleink		u.L[_1] = 0;
811.1Skleink		break;
821.1Skleink
831.1Skleink	  case STRTOG_NaN:
841.1Skleink		u.L[0] = d_QNAN0;
851.1Skleink		u.L[1] = d_QNAN1;
861.1Skleink		break;
871.1Skleink
881.1Skleink	  case STRTOG_NaNbits:
891.1Skleink		u.L[_0] = 0x7ff00000 | bits[1];
901.1Skleink		u.L[_1] = bits[0];
911.1Skleink	  }
921.1Skleink	if (k & STRTOG_Neg)
931.1Skleink		u.L[_0] |= 0x80000000L;
941.1Skleink	return u.d;
951.1Skleink	}
96