home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / builtin.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  7.2 KB  |  380 lines

  1. /* 
  2. Copyright (C) 1988 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #include <builtin.h>
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include "libconfig.h"
  28. #include <Obstack.h>
  29.  
  30. /*
  31.  common functions on built-in types
  32. */
  33.  
  34. long gcd(long x, long y)        // euclid's algorithm
  35. {
  36.   long a = abs(x);
  37.   long b = abs(y);
  38.  
  39.   long tmp;
  40.   
  41.   if (b > a)
  42.   {
  43.     tmp = a; a = b; b = a;
  44.   }
  45.   for(;;)
  46.   {
  47.     if (b == 0)
  48.       return a;
  49.     else if (b == 1)
  50.       return b;
  51.     else
  52.     {
  53.       tmp = b;
  54.       b = a % b;
  55.       a = tmp;
  56.     }
  57.   }
  58. }
  59.  
  60. double pow(double x, long p)
  61. {
  62.   if (p == 0)
  63.     return (x < 0 && (p & 1)) ? -1.0 : 1.0;
  64.   else if (x == 0.0)
  65.     return 0.0;
  66.   else
  67.   {
  68.     double b;
  69.     if (p < 0)
  70.     {
  71.       p = -p;
  72.       b = 1.0 / x;
  73.     }
  74.     else
  75.       b = x;
  76.     double r = 1.0;
  77.     for(;;)
  78.     {
  79.       if (p & 1)
  80.         r *= b;
  81.       if ((p >>= 1) == 0)
  82.         return r;
  83.       else
  84.         b *= b;
  85.     }
  86.   }
  87. }
  88.  
  89. long lg(long x)
  90. {
  91.   long l = 0;
  92.   unsigned long a = abs(x);
  93.  
  94.   while (a > 1)
  95.   {
  96.     a = a >> 1;
  97.     ++l;
  98.   }
  99.   return l;
  100. }
  101.  
  102. long  pow(long  x, long y)
  103. {
  104.   if (x == 0)
  105.     return (y == 0)? 1 : 0;
  106.   else if (y < 0)
  107.     return 0;
  108.   else if (y == 0 || x == 1)
  109.     return  1;
  110.   else if (x == -1)
  111.     return (y & 1)? -1 : 1;
  112.   else
  113.   {
  114.     long r = 1;
  115.     for(;;)
  116.     {
  117.       if (y & 1)
  118.         r *= x;
  119.       if ((y >>= 1) == 0)
  120.         return r;
  121.       else
  122.         x *= x;
  123.     }
  124.   }
  125. }
  126.  
  127. long sqrt(long x)
  128. {
  129.   if (x <= 0)
  130.     return 0;                   // no int error handler, so ...
  131.   else if (x == 1)
  132.     return 1;
  133.   else
  134.   {
  135.     long r = x >> 1;
  136.     long q;
  137.     for(;;)
  138.     {
  139.       q = x / r;
  140.       if (q >= r)
  141.         return r;
  142.       else
  143.         r = (r + q) >> 1;
  144.     }
  145.   }
  146. }
  147.  
  148.  
  149. // Obstacks are used as an easy way to allocate enough space
  150. // for various input and output & conversion operations
  151. // (they are a real natural for input; we might as well use
  152. // them for output too rather than creating yet another allocation
  153. // mechanism.)
  154. // 
  155. // We guarantee that ONLY the most recently constructed
  156. // obstack object is intact. There is no queuing mechanism.
  157. // This is a firmer policy than using a queue that may or may
  158. // not have enough space to hold several objects.
  159.  
  160. Obstack _libgxx_io_ob;
  161. char* _libgxx_io_oblast = 0;
  162.  
  163.     
  164.  
  165. char* form(const char* fmt ...)
  166. {
  167.   va_list args;
  168.   va_start(args, fmt);
  169.   if (_libgxx_io_oblast) _libgxx_io_ob.free(_libgxx_io_oblast);
  170.   _libgxx_io_ob.blank(BUFSIZ);
  171.   _libgxx_io_oblast = (char*)(_libgxx_io_ob.finish());
  172.   char* obbase = _libgxx_io_oblast;
  173. #ifndef HAVE_VPRINTF
  174.   FILE b;
  175.   b._flag = _IOWRT|_IOSTRG;
  176.   b._ptr = obbase;
  177.   b._cnt = BUFSIZ;
  178.   _doprnt(fmt, args, &b);
  179.   putc('\0', &b);
  180. #else
  181.   vsprintf(obbase, fmt, args);
  182. #endif
  183.   va_end(args);
  184.   return obbase;
  185. }
  186.  
  187. char* itoa(long x, int base = 10, int width = 0)
  188. {
  189.   if (_libgxx_io_oblast) _libgxx_io_ob.free(_libgxx_io_oblast);
  190.   int wrksiz = 100 + width;
  191.   _libgxx_io_ob.blank(wrksiz);
  192.   _libgxx_io_oblast = (char*)(_libgxx_io_ob.finish());
  193.   char* obbase = _libgxx_io_oblast;
  194.   char* e = obbase + wrksiz - 1;
  195.   char* s = e;
  196.   *--s = 0;
  197.   char sgn = 0;
  198.  
  199.   if (x == 0)
  200.     *--s = '0';
  201.   else
  202.   {
  203.     int z;
  204.     if (x < 0)
  205.     {
  206.       sgn = '-';
  207.       z = -x;
  208.     }
  209.     else
  210.       z = x;
  211.     while (z != 0)
  212.     {
  213.       char ch = z % base;
  214.       z = z / base;
  215.       if (ch >= 10)
  216.         ch += 'a' - 10;
  217.       else
  218.         ch += '0';
  219.       *--s = ch;
  220.     }
  221.   }
  222.  
  223.   if (sgn) *--s = sgn;
  224.   int w = e - s - 1;
  225.   while (w++ < width)
  226.     *--s = ' ';
  227.   return s;
  228. }
  229.  
  230. char* hex(long i, int width = 0)
  231. {
  232.   return itoa(i, 16, width);
  233. }
  234.  
  235. char* oct(long i, int width = 0)
  236. {
  237.   return itoa(i, 8, width);
  238. }
  239.  
  240. char* dec(long i, int width = 0)
  241. {
  242.   return itoa(i, 10, width);
  243. }
  244.  
  245. static char chr_buf[2];   // fixed slot to hold chr(ch)
  246.  
  247. char* chr(char ch)
  248. {
  249.   chr_buf[0] = ch;
  250.   chr_buf[1] = 0;
  251.   return chr_buf;
  252. }
  253.  
  254. /*
  255.  some useful hash functions
  256. */
  257.  
  258. unsigned int hashpjw(const char* x) // From Dragon book, p436
  259. {
  260.   unsigned int h = 0;
  261.   unsigned int g;
  262.  
  263.   while (*x != 0)
  264.   {
  265.     h = (h << 4) + *x++;
  266.     if ((g = h & 0xf0000000) != 0)
  267.       h = (h ^ (g >> 24)) ^ g;
  268.   }
  269.   return h;
  270. }
  271.  
  272. unsigned int multiplicativehash(int x)
  273. {
  274.   // uses a const close to golden ratio * pow(2,32)
  275.   return ((unsigned)x) * 2654435767;
  276. }
  277.  
  278.  
  279. unsigned int foldhash(double x)
  280. {
  281.   union { unsigned int i[2]; double d; } u;
  282.   u.d = x;
  283.   unsigned int u0 = u.i[0];
  284.   unsigned int u1 = u.i[1]; 
  285.   return u0 ^ u1;
  286. }
  287.  
  288. void default_one_arg_error_handler(const char* msg)
  289. {
  290.   fputs("Error: ", stderr);
  291.   fputs(msg, stderr);
  292.   fputs("\n", stderr);
  293.   abort();
  294. }
  295.  
  296.  
  297. void default_two_arg_error_handler(const char* kind, const char* msg)
  298. {
  299.   fputs(kind, stderr);
  300.   fputs(" Error: ", stderr);
  301.   fputs(msg, stderr);
  302.   fputs("\n", stderr);
  303.   abort();
  304. }
  305.  
  306. two_arg_error_handler_t lib_error_handler = default_two_arg_error_handler;
  307.  
  308. two_arg_error_handler_t set_lib_error_handler(two_arg_error_handler_t f)
  309. {
  310.   two_arg_error_handler_t old = lib_error_handler;
  311.   lib_error_handler = f;
  312.   return old;
  313. }
  314.  
  315.  
  316. // from Doug Schmidt...
  317.  
  318. /* no such thing as "negative time"! */
  319. #define  TIMER_ERROR_VALUE -1.0   
  320.  
  321. // surely OK for these machines...
  322.  
  323. #if defined(BSD) || defined(vax) || defined(sun)
  324.  
  325. extern "C" {
  326. #include <sys/time.h>
  327. #include <sys/resource.h>
  328. int   getrusage(int, struct rusage*);
  329. }
  330.  
  331. static struct rusage Old_Time;
  332. static struct rusage New_Time;
  333. static int    Timer_Set = 0;
  334.  
  335. double start_timer()
  336. {
  337.    Timer_Set = 1;
  338.    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
  339.    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
  340. }
  341.  
  342. /* returns process time since Last_Time (if parameter is not DEFAULT_TIME, */
  343. /* i.e., (double) 0.0 ),otherwise, if parameter == DEFAULT_TIME then       */
  344. /* the time since the Old_Time was set is returned. */
  345. /* Returns TIMER_ERROR_VALUE   */
  346. /* if Start_Timer() is not called first */
  347.  
  348. double return_elapsed_time(double Last_Time)
  349. {
  350.    if (!Timer_Set) {
  351.       return(TIMER_ERROR_VALUE);
  352.    }   
  353.    else {
  354.     /* get process time */
  355.       getrusage(RUSAGE_SELF,&New_Time);
  356.       if (Last_Time == 0.0) {
  357.          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) + 
  358.                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec) 
  359.                 / 1000000.0));
  360.       }
  361.       else {
  362.          return((New_Time.ru_utime.tv_sec + 
  363.                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
  364.       }
  365.    }
  366. }
  367.  
  368. #else /* dummy them out */
  369. double start_timer()
  370. {
  371.   return TIMER_ERROR_VALUE;
  372. }
  373.  
  374. double return_elapsed_time(double)
  375. {
  376.   return TIMER_ERROR_VALUE;
  377. }
  378.  
  379. #endif
  380.