src/libc/stdlib.c

Go to the documentation of this file.
00001 
00009 #include "tm_macros.h"
00010 #include "libc_internal.h"
00011 #include "tm_macros.h"
00012 #include <ctype.h>
00013 
00014 
00015 int
00016 txc_libc_atoi(char *p)
00017 {
00018         int n, f;
00019 
00020     TM_BEGIN
00021         n = 0;
00022         f = 0;
00023         for(;;p++) {
00024                 switch(*p) {
00025                         case ' ':
00026                         case '\t':
00027                                 continue;
00028                         case '-':
00029                                 f++;
00030                         case '+':
00031                                 p++;
00032                 }
00033                 break;
00034         }
00035         while(*p >= '0' && *p <= '9')
00036                 n = n*10 + *p++ - '0';
00037         return(f? -n: n);
00038         TM_END
00039 }
00040 
00041 TM_WAIVER
00042 int txc_libc_isdigit(int c)
00043 {
00044         return isdigit(c);
00045 }
00046 
00047 double
00048 txc_libc_atof(char *p)
00049 {
00050         register c;
00051         double fl, flexp, exp5;
00052         double big = 72057594037927936.;  /*2^56*/
00053         double ldexp();
00054         int nd;
00055         register eexp, exp, neg, negexp, bexp;
00056 
00057     TM_BEGIN
00058         neg = 1;
00059         while((c = *p++) == ' ')
00060                 ;
00061         if (c == '-')
00062                 neg = -1;
00063         else if (c=='+')
00064                 ;
00065         else
00066                 --p;
00067 
00068         exp = 0;
00069         fl = 0;
00070         nd = 0;
00071         while ((c = *p++), txc_libc_isdigit(c)) {
00072                 if (fl<big)
00073                         fl = 10*fl + (c-'0');
00074                 else
00075                         exp++;
00076                 nd++;
00077         }
00078 
00079         if (c == '.') {
00080                 while ((c = *p++), txc_libc_isdigit(c)) {
00081                         if (fl<big) {
00082                                 fl = 10*fl + (c-'0');
00083                                 exp--;
00084                         }
00085                 nd++;
00086                 }
00087         }
00088 
00089         negexp = 1;
00090         eexp = 0;
00091         if ((c == 'E') || (c == 'e')) {
00092                 if ((c= *p++) == '+')
00093                         ;
00094                 else if (c=='-')
00095                         negexp = -1;
00096                 else
00097                         --p;
00098 
00099                 while ((c = *p++), txc_libc_isdigit(c)) {
00100                         eexp = 10*eexp+(c-'0');
00101                 }
00102                 if (negexp<0)
00103                         eexp = -eexp;
00104                 exp = exp + eexp;
00105         }
00106 
00107         negexp = 1;
00108         if (exp<0) {
00109                 negexp = -1;
00110                 exp = -exp;
00111         }
00112 
00113 
00114         if((nd+exp*negexp) < -LOGHUGE){
00115                 fl = 0;
00116                 exp = 0;
00117         }
00118         flexp = 1;
00119         exp5 = 5;
00120         bexp = exp;
00121         for (;;) {
00122                 if (exp&01)
00123                         flexp *= exp5;
00124                 exp >>= 1;
00125                 if (exp==0)
00126                         break;
00127                 exp5 *= exp5;
00128         }
00129         if (negexp<0)
00130                 fl /= flexp;
00131         else
00132                 fl *= flexp;
00133         fl = ldexp(fl, negexp*bexp);
00134         if (neg<0)
00135                 fl = -fl;
00136         return(fl);
00137         TM_END
00138 }
00139 

Generated on Wed Dec 9 20:32:39 2009 for xCalls by  doxygen 1.4.7