home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / stdlib / rcs / atof.c,v < prev    next >
Encoding:
Text File  |  1992-07-04  |  3.5 KB  |  147 lines

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