home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / Bonus / normal / atof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  3.3 KB  |  118 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * the Systems Programming Group of the University of Utah Computer
  7.  * Science Department.
  8.  *
  9.  * Redistribution and use in source and binary forms are permitted
  10.  * provided that: (1) source distributions retain this entire copyright
  11.  * notice and comment, and (2) distributions including binaries display
  12.  * the following acknowledgement:  ``This product includes software
  13.  * developed by the University of California, Berkeley and its contributors''
  14.  * in the documentation or other materials provided with the distribution
  15.  * and in all advertising materials mentioning features or use of this
  16.  * software. Neither the name of the University nor the names of its
  17.  * contributors may be used to endorse or promote products derived
  18.  * from this software without specific prior written permission.
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  */
  23.  
  24. #if defined(LIBC_SCCS) && !defined(lint)
  25. static char sccsid[] = "@@(#)atof.c    5.1 (Berkeley) 5/12/90";
  26. #endif /* LIBC_SCCS and not lint */
  27.  
  28. #include <ctype.h>
  29.  
  30. #ifdef amigados
  31. #define IEEE
  32. /* seems like the amiga stores IEEE in the same byte order as the 300, 
  33.  * lucky we:-)) */
  34. #define hp300
  35. #endif
  36.  
  37. static const double _twoemax =
  38. #ifdef IEEE
  39.     9007199254740992.;    /*2^53*/
  40. #else
  41.     72057594037927936.;    /*2^56*/
  42. #endif
  43.  
  44. #ifdef hp300
  45. /* attempt to be as exact as possible */
  46. static const struct {
  47.     long d_high;
  48.     long d_low;
  49. } _exp5[] = {
  50.     { 0x40140000, 0x00000000 },    /* 5 */
  51.     { 0x40390000, 0x00000000 },    /* 25 */
  52.     { 0x40838800, 0x00000000 },    /* 625 */
  53.     { 0x4117d784, 0x00000000 },    /* 390625 */
  54.     { 0x4241c379, 0x37e08000 },    /* 152587890625 */
  55.     { 0x4493b8b5, 0xb5056e17 },    /* 2.3283064365387e+022 */
  56.     { 0x49384f03, 0xe93ff9f6 },    /* 5.42101086242753e+044 */
  57.     { 0x52827748, 0xf9301d33 },    /* 2.93873587705572e+089 */
  58.     { 0x65154fdd, 0x7f73bf3f }    /* 8.63616855509445e+178 */
  59. };
  60. #else
  61. static const double    _exp5[]    = {
  62.     5.,
  63.     25.,
  64.     625.,
  65.     390625.,
  66.     152587890625.,
  67.     23283064365386962890625.,
  68. #ifdef IEEE
  69.     5.4210108624275231e+044,
  70.     2.9387358770557196e+089,
  71.     8.6361685550944492e+178,
  72. #endif
  73. };
  74. #endif
  75.  
  76. double
  77. atof (const char *p)
  78. {
  79.     extern double ldexp();
  80.     register c, exp = 0, eexp = 0;
  81.     double fl = 0, flexp = 1.0;
  82.     int bexp, neg = 1, negexp = 1;
  83.  
  84.     while((c = *p++) == ' ');
  85.     if (c == '-') neg = -1;    else if (c == '+'); else --p;
  86.  
  87.     while ((c = *p++), isdigit(c))
  88.         if (fl < _twoemax) fl = 10*fl + (c-'0'); else exp++;
  89.     if (c == '.')
  90.     while ((c = *p++), isdigit(c))
  91.         if (fl < _twoemax)
  92.         {
  93.             fl = 10*fl + (c-'0');
  94.             exp--;
  95.         }
  96.     if ((c == 'E') || (c == 'e'))
  97.     {
  98.         if ((c= *p++) == '+'); else if (c=='-') negexp = -1; else --p;
  99.         while ((c = *p++), isdigit(c)) eexp = 10*eexp + (c-'0');
  100.         if (negexp < 0) eexp = -eexp; exp += eexp;
  101.     }
  102.     bexp = exp;
  103.     if (exp < 0) exp = -exp;
  104.  
  105.     for (c = 0; c < sizeof(_exp5)/sizeof(_exp5[0]); c++)
  106.     {
  107. #ifdef hp300
  108.         if (exp & 01) flexp *= *(double *)&_exp5[c];
  109. #else
  110.         if (exp & 01) flexp *= _exp5[c];
  111. #endif
  112.         exp >>= 1; if (exp == 0) break;
  113.     }
  114.  
  115.     if (bexp < 0) fl /= flexp; else fl *= flexp;
  116.     fl = ldexp(fl, bexp);
  117.     if (neg < 0) return(-fl); else return(fl);
  118. }