stdlib.c

Go to the documentation of this file.
00001 #include <unistd.h>
00002 
00003 #include "BUILD/bitc-runtime.h"
00004 
00010 
00011 /* (proclaim atoi: (fn (string) int32) external bitc_stdlib_atoi) */
00012 bitc_int32_t
00013 bitc_stdlib_atoi(bitc_string_t *str)
00014 {
00015   bitc_int32_t sign = 1;
00016   bitc_int32_t val = 0;
00017   bitc_int32_t base = 10;
00018   size_t i = 0;
00019 
00020   if(str->s[i] == '-') {
00021     i++;
00022     sign = -1;
00023   }
00024 
00025   /* Deal with optional leading radix: */
00026   if (str->s[i] == 0) {
00027     i++;
00028 
00029     switch (str->s[i]) {
00030     case 'x':
00031     case 'X':
00032       {
00033         base = 16;
00034         i++;
00035         break;
00036       }
00037     case 'b':
00038     case 'B':
00039       {
00040         // BitC library extension to handle base 2 using 0b:
00041         base = 2;
00042         i++;
00043         break;
00044       }
00045     default:
00046       // In absence of qualifier it must be base 8 (octal):
00047       base = 8;
00048       break;
00049     }
00050   }
00051 
00052   for ( ; i < str->length; i++) {
00053     // Note: We are dealing with a UTF8-encoded string.  All legal
00054     // ASCII digits, including hexadecimal digits, fall within the
00055     // leading 128 code points.  Any code point whose encoding
00056     // requires more than 8 bits will have a prefix marker that is not
00057     // a digit, and we will abandon the loop in that case. In light of
00058     // which, it is okay here to use an 8-bit C character to hold the
00059     // code point, and it is okay NOT to use the utf_decode() code
00060     // point extraction functions.
00061     unsigned char c = str->s[i];
00062     bitc_int32_t digit = (c - '0'); // presumptively
00063 
00064     if (c < '0')
00065       break;
00066 
00067     if ('0' <= c && c <= '9') {
00068       digit = (c - '0');
00069       if (digit >= base)
00070         break;                  // out of range
00071     }
00072     else if (base == 16) {
00073       if ('a' <= c && c <= 'f')
00074         digit = (c - 'a' + 10);
00075       else if ('A' <= c && c <= 'F')
00076         digit = (c - 'A' + 10);
00077       else 
00078         break;                  // out of range
00079     }
00080     else
00081       break;                    // out of range
00082 
00083     val = val * base + digit;
00084   }
00085 
00086   return (val * sign);
00087 }
00088 
00089 #if defined(__i386__)
00094 
00095 /* (proclaim getTimeStamp: (fn () uint64) external bitc_stdlib_rdtsc) */
00096 bitc_uns64_t
00097 bitc_stdlib_getTimeStamp()
00098 {
00099   unsigned long temp1=0;
00100   unsigned long temp2=0;  
00101   unsigned long long ts=0;
00102   __asm__ volatile("rdtsc\t\n"
00103                "movl %%eax, %0\t\n"
00104                "movl %%edx, %1\t\n"
00105                : "=m" (temp1), "=m" (temp2)
00106                : 
00107                : "%eax", "%edx");
00108   
00109   ts = (((unsigned long long)temp2) << 32) + temp1;
00110   return ts;
00111 }
00112 #else
00113 bitc_uns64_t
00114 bitc_stdlib_getTimeStamp()
00115 {
00116   return 0;
00117 }
00118 #endif

Generated on Sat Feb 4 23:59:30 2012 for BitC Runtime Library by  doxygen 1.4.7