home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / programi / gcc_9112.lzh / gnulib / gnulib2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-09  |  26.1 KB  |  1,270 lines

  1. /* More subroutines needed by GCC output code on some machines.  */
  2. /* Compile this one with gcc.  */
  3. /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* As a special exception, if you link this library with files
  22.    compiled with GCC to produce an executable, this does not cause
  23.    the resulting executable to be covered by the GNU General Public License.
  24.    This exception does not however invalidate any other reasons why
  25.    the executable file might be covered by the GNU General Public License.  */
  26.  
  27. #include "types.h"
  28. #include <stddef.h>
  29.  
  30. /* In case config.h defined it.  */
  31. #undef abort
  32.  
  33. #ifndef LONG_TYPE_SIZE
  34. #define LONG_TYPE_SIZE (sizeof (long) * BITS_PER_UNIT)
  35. #endif
  36.  
  37. #ifndef SItype
  38. #define SItype long int
  39. #endif
  40.  
  41. /* long long ints are pairs of long ints in the order determined by
  42.    WORDS_BIG_ENDIAN.  */
  43.  
  44. #if WORDS_BIG_ENDIAN
  45.   struct longlong {long high, low;};
  46. #else
  47.   struct longlong {long low, high;};
  48. #endif
  49.  
  50. /* We need this union to unpack/pack longlongs, since we don't have
  51.    any arithmetic yet.  Incoming long long parameters are stored
  52.    into the `ll' field, and the unpacked result is read from the struct
  53.    longlong.  */
  54.  
  55. typedef union
  56. {
  57.   struct longlong s;
  58.   long long ll;
  59. } long_long;
  60.  
  61. /* Internally, long long ints are strings of unsigned shorts in the
  62.    order determined by BYTES_BIG_ENDIAN.  */
  63.  
  64. #define B (1 << (LONG_TYPE_SIZE / 2))
  65. #define lowpart(t) ((unsigned long) (t) % B)
  66. #define highpart(t) ((unsigned long) (t) / B)
  67.  
  68.  
  69. /* Define auxilliary functions for multiplication and division.
  70.    1) __umulsidi3(a,b) multiplies two unsigned long integers A and B,
  71.    and returns a long long product.
  72.    2) div_qrnnd(q,r,n1,n0,d) divides a long long integer, composed by the
  73.    long integers N1 and N0, by D and places the quotient in Q and the
  74.    remainder in R.  If N1 >= D the quotient cannot be represented, and
  75.    the result is undefined.
  76.  
  77.    There's a simple machine instruction for these operations on almost
  78.    all CPUs.  Use them!  */
  79.  
  80. #if defined (L_udivdi3) || defined (L_muldi3)
  81.  
  82. #if defined (__mc68020__)
  83. #define __umulsidi3(u, v)                        \
  84.   ({long_long __w;                            \
  85.     asm ("mulul %3,%1:%0" : "=d" (__w.s.low), "=d" (__w.s.high)        \
  86.      : "%0" (u), "dmsK" (v));                    \
  87.     __w.ll;                                \
  88.   })
  89. #define div_qrnnd(q, r, n1, n0, d) \
  90.   asm ("divul %4,%1:%0" : "=d" (q), "=d" (r) : "0" (n0), "1" (n1), "dmsK" (d))
  91.  
  92. #elif defined (sparc)
  93. unsigned long long __umulsidi3 () asm (".umul");
  94.  
  95. #elif defined (ns32000)
  96. /* Try:
  97. #define __umulsidi3(u, v) \
  98.   ({long long __w;                            \
  99.     asm ("meid %2,%0" : "=g" (__w) : "%0" (u), "g" (v)); __w;        \
  100.   })
  101. */
  102. #define __umulsidi3(u, v) \
  103.   ({long_long __w;                            \
  104.     asm ("movd %2,r0;  meid %3,r0;  movd r0,%0;  movd r1,%1"        \
  105.      : "=g" (__w.s.low), "=g" (__w.s.high)                \
  106.      : "g" (u), "g" (v)                        \
  107.      : "r0", "r1"); __w.ll;                        \
  108.   })
  109. #define div_qrnnd(q, r, n1, n0, d) \
  110.   ({                                    \
  111.     asm ("movd %1,r0;  movd %2,r1;  deid %3,r0;  movd r1,%0"        \
  112.      : "=g" (q) : "g" (n0), "g" (n1), "g" (d) : "r0", "r1");    \
  113.     asm ("movd %1,r0;  movd %2,r1;  deid %3,r0;  movd r0,%0"        \
  114.      : "=g" (r) : "g" (n0), "g" (n1), "g" (d) : "r0", "r1");    \
  115.   })
  116.  
  117. #elif defined (__i386__)
  118. #define __umulsidi3(u, v) \
  119.   ({long_long __w;                            \
  120.     asm ("mull %3" : "=a" (__w.s.low), "=d" (__w.s.high)        \
  121.      : "%0" (u), "rm" (v)); __w.ll;                    \
  122.   })
  123. #define div_qrnnd(quo, rem, n1, n0, den) \
  124.   asm ("divl %4" : "=a" (quo), "=d" (rem) : "0" (n0), "1" (n1), "rm" (den))
  125.  
  126. #endif
  127.  
  128. /* If this machine has no inline assembler, use the C function.  */
  129. #if !defined (__umulsidi3)
  130. static inline unsigned long long
  131. __umulsidi3 (u, v)
  132.      unsigned long u, v;
  133. {
  134.   unsigned long x0, x1, x2, x3;
  135.   unsigned long ulow, vlow, uhigh, vhigh;
  136.   long_long w;
  137.  
  138.   ulow = lowpart (u);
  139.   uhigh = highpart (u);
  140.   vlow = lowpart (v);
  141.   vhigh = highpart (v);
  142.  
  143.   x0 = ulow * vlow;
  144.   x1 = ulow * vhigh + highpart (x0);
  145.   x2 = uhigh * vlow;
  146.   x3 = uhigh * vhigh;
  147.  
  148.   x1 += x2;
  149.   if (x1 < x2)
  150.     x3 += B;
  151.  
  152.   w.s.high = x3 + highpart (x1);
  153.   w.s.low = lowpart (x1) * B + lowpart (x0);
  154.  
  155.   return w.ll;
  156. }
  157. #endif
  158.  
  159. #if !defined (div_qrnnd)
  160.  
  161. /* Defined as a macro because of the two output parameters.  */
  162. #define div_qrnnd(q, r, n1, n0, d) \
  163.   ({                                    \
  164.     unsigned int __d1, __d0, __q1, __q0;                \
  165.     unsigned long __r1, __r0, __m;                    \
  166.     __d1 = highpart (d);                        \
  167.     __d0 = lowpart (d);                            \
  168.     __r1 = (n1) % __d1;                            \
  169.     __q1 = (n1) / __d1;                            \
  170.     __m = (unsigned long) __q1 * __d0;                    \
  171.     __r1 = __r1 * B | highpart (n0);                    \
  172.     while (__r1 < __m)                            \
  173.       __q1--, __r1 += (d);                        \
  174.     __r0 = __r1 % __d1;                            \
  175.     __q0 = __r1 / __d1;                            \
  176.     __m = (unsigned long) __q0 * __d0;                    \
  177.     __r0 = __r0 * B | lowpart (n0);                    \
  178.     while (__r0 < __m)                            \
  179.       __q0--, __r0 += (d);                        \
  180.     (q) = (unsigned long) __q1 * B | __q0;                \
  181.     (r) = __r0;                                \
  182.   })
  183. #endif
  184. #endif /* udiv or mul */
  185.  
  186. #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
  187. #if defined (L_divdi3) || defined (L_moddi3)
  188. static inline
  189. #endif
  190. long long
  191. __negdi2 (u)
  192.      long long u;
  193. {
  194.   long_long w;
  195.   long_long uu;
  196.  
  197.   uu.ll = u;
  198.  
  199.   w.s.low = -uu.s.low;
  200.   w.s.high = w.s.low == 0 ? -uu.s.high : ~uu.s.high;
  201.  
  202.   return w.ll;
  203. }
  204. #endif
  205.  
  206. #ifdef L_anddi3
  207. long long 
  208. __anddi3 (u, v)
  209.      long long u, v;
  210. {
  211.   long_long w;
  212.   long_long uu, vv;
  213.  
  214.   uu.ll = u;
  215.   vv.ll = v;
  216.  
  217.   w.s.high = uu.s.high & vv.s.high;
  218.   w.s.low = uu.s.low & vv.s.low;
  219.  
  220.   return w.ll;
  221. }
  222. #endif
  223.  
  224. #ifdef L_iordi3
  225. long long
  226. __iordi3 (u, v)
  227.      long long u, v;
  228. {
  229.   long_long w;
  230.   long_long uu, vv;
  231.  
  232.   uu.ll = u;
  233.   vv.ll = v;
  234.  
  235.   w.s.high = uu.s.high | vv.s.high;
  236.   w.s.low = uu.s.low | vv.s.low;
  237.  
  238.   return w.ll;
  239. }
  240. #endif
  241.  
  242. #ifdef L_xordi3
  243. long long
  244. __xordi3 (u, v)
  245.      long long u, v;
  246. {
  247.   long_long w;
  248.   long_long uu, vv;
  249.  
  250.   uu.ll = u;
  251.   vv.ll = v;
  252.  
  253.   w.s.high = uu.s.high ^ vv.s.high;
  254.   w.s.low = uu.s.low ^ vv.s.low;
  255.  
  256.   return w.ll;
  257. }
  258. #endif
  259.  
  260. #ifdef L_one_cmpldi2
  261. long long
  262. __one_cmpldi2 (u)
  263.      long long u;
  264. {
  265.   long_long w;
  266.   long_long uu;
  267.  
  268.   uu.ll = u;
  269.  
  270.   w.s.high = ~uu.s.high;
  271.   w.s.low = ~uu.s.low;
  272.  
  273.   return w.ll;
  274. }
  275. #endif
  276.  
  277. #ifdef L_lshldi3
  278. long long
  279. __lshldi3 (u, b1)
  280.      long long u;
  281.      long long b1;
  282. {
  283.   long_long w;
  284.   long bm;
  285.   long_long uu;
  286.   int b = b1;
  287.  
  288.   if (b == 0)
  289.     return u;
  290.  
  291.   uu.ll = u;
  292.  
  293.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  294.   if (bm <= 0)
  295.     {
  296.       w.s.low = 0;
  297.       w.s.high = (unsigned long)uu.s.low << -bm;
  298.     }
  299.   else
  300.     {
  301.       unsigned long carries = (unsigned long)uu.s.low >> bm;
  302.       w.s.low = (unsigned long)uu.s.low << b;
  303.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  304.     }
  305.  
  306.   return w.ll;
  307. }
  308. #endif
  309.  
  310. #ifdef L_lshrdi3
  311. long long
  312. __lshrdi3 (u, b1)
  313.      long long u;
  314.      long long b1;
  315. {
  316.   long_long w;
  317.   long bm;
  318.   long_long uu;
  319.   int b = b1;
  320.  
  321.   if (b == 0)
  322.     return u;
  323.  
  324.   uu.ll = u;
  325.  
  326.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  327.   if (bm <= 0)
  328.     {
  329.       w.s.high = 0;
  330.       w.s.low = (unsigned long)uu.s.high >> -bm;
  331.     }
  332.   else
  333.     {
  334.       unsigned long carries = (unsigned long)uu.s.high << bm;
  335.       w.s.high = (unsigned long)uu.s.high >> b;
  336.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  337.     }
  338.  
  339.   return w.ll;
  340. }
  341. #endif
  342.  
  343. #ifdef L_ashldi3
  344. long long
  345. __ashldi3 (u, b1)
  346.      long long u;
  347.      long long b1;
  348. {
  349.   long_long w;
  350.   long bm;
  351.   long_long uu;
  352.   int b = b1;
  353.  
  354.   if (b == 0)
  355.     return u;
  356.  
  357.   uu.ll = u;
  358.  
  359.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  360.   if (bm <= 0)
  361.     {
  362.       w.s.low = 0;
  363.       w.s.high = (unsigned long)uu.s.low << -bm;
  364.     }
  365.   else
  366.     {
  367.       unsigned long carries = (unsigned long)uu.s.low >> bm;
  368.       w.s.low = (unsigned long)uu.s.low << b;
  369.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  370.     }
  371.  
  372.   return w.ll;
  373. }
  374. #endif
  375.  
  376. #ifdef L_ashrdi3
  377. long long
  378. __ashrdi3 (u, b1)
  379.      long long u;
  380.      long long b1;
  381. {
  382.   long_long w;
  383.   long bm;
  384.   long_long uu;
  385.   int b = b1;
  386.  
  387.   if (b == 0)
  388.     return u;
  389.  
  390.   uu.ll = u;
  391.  
  392.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  393.   if (bm <= 0)
  394.     {
  395.       w.s.high = uu.s.high >> 31; /* just to make w.s.high 1..1 or 0..0 */
  396.       w.s.low = uu.s.high >> -bm;
  397.     }
  398.   else
  399.     {
  400.       unsigned long carries = (unsigned long)uu.s.high << bm;
  401.       w.s.high = uu.s.high >> b;
  402.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  403.     }
  404.  
  405.   return w.ll;
  406. }
  407. #endif
  408.  
  409. #ifdef L_adddi3
  410. long long
  411. __adddi3 (u, v)
  412.      long long u, v;
  413. {
  414.   long_long w;
  415.   long_long uu, vv;
  416.  
  417.   uu.ll = u;
  418.   vv.ll = v;
  419.  
  420.   w.s.low = uu.s.low + vv.s.low;
  421.   w.s.high = uu.s.high + vv.s.high + ((unsigned long) w.s.low < uu.s.low);
  422.  
  423.   return w.ll;
  424. }
  425. #endif
  426.  
  427. #ifdef L_subdi3
  428. long long
  429. __subdi3 (u, v)
  430.      long long u, v;
  431. {
  432.   long_long w;
  433.   long_long uu, vv;
  434.  
  435.   uu.ll = u;
  436.   vv.ll = v;
  437.  
  438.   w.s.low = uu.s.low - vv.s.low;
  439.   w.s.high = uu.s.high - vv.s.high - ((unsigned long) w.s.low > uu.s.low);
  440.  
  441.   return w.ll;
  442. }
  443. #endif
  444.  
  445. #ifdef L_divdi3
  446. long long
  447. __divdi3 (u, v)
  448.      long long u, v;
  449. {
  450.   int c = 0;
  451.   long_long uu, vv;
  452.   long_long w;
  453.  
  454.   uu.ll = u, vv.ll = v;
  455.  
  456.   if (uu.s.high < 0)
  457.     c = ~c,
  458.     uu.ll = __negdi2 (uu.ll);
  459.   if (vv.s.high < 0)
  460.     c = ~c,
  461.     vv.ll = __negdi2 (vv.ll);
  462.  
  463.   w.ll = (unsigned long long) uu.ll / vv.ll;
  464.   if (c)
  465.     w.ll = __negdi2 (uu.ll);
  466.  
  467.   return w.ll;
  468. }
  469. #endif
  470.  
  471. #ifdef L_moddi3
  472. long long
  473. __moddi3 (u, v)
  474.      long long u, v;
  475. {
  476.   int c = 0;
  477.   long_long uu, vv;
  478.   long_long w;
  479.  
  480.   uu.ll = u, vv.ll = v;
  481.  
  482.   if (uu.s.high < 0)
  483.     c = ~c,
  484.     uu.ll = __negdi2 (uu.ll);
  485.   if (vv.s.high < 0)
  486.     c = ~c,
  487.     vv.ll = __negdi2 (vv.ll);
  488.  
  489.   w.ll = (unsigned long long) uu.ll % vv.ll;
  490.   if (c)
  491.     w.ll = __negdi2 (w.ll);
  492.  
  493.   return w.ll;
  494. }
  495. #endif
  496.  
  497. #ifdef L_umoddi3
  498. unsigned long long
  499. __umoddi3 (u, v)
  500.      unsigned long long u, v;
  501. {
  502.   /* This ought to be done better.  The division calculates the remainder,
  503.      so we should not make a multiplication here.  */
  504.   return u - (u / v) * v;
  505. }
  506. #endif
  507.  
  508. #ifdef L_muldi3
  509. long long
  510. __muldi3 (u, v)
  511.      long long u, v;
  512. {
  513.   long_long w;
  514.   long_long uu, vv;
  515.  
  516.   uu.ll = u, vv.ll = v;
  517.  
  518.   w.ll = __umulsidi3 (uu.s.low, vv.s.low);
  519.   w.s.high += uu.s.low * vv.s.high + uu.s.high * vv.s.low;
  520.  
  521.   return w.ll;
  522. }
  523. #endif
  524.  
  525. #ifdef L_udivdi3
  526.  
  527. /* Compute the index of the most significant non-zero bit in X.  Range
  528.    0,1,...,32 for 0,1,0x80000000.
  529.    This is for 32 bits machines max.  In my opinion, this function should
  530.    be a __builtin and a standard md code generation name, since it's
  531.    useful even for floating point normalization.  */
  532.  
  533. static inline unsigned long
  534. bits (x)
  535.      unsigned long x;
  536. {
  537.   static unsigned char t[] =
  538.     {
  539.       0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  540.       6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  541.       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  542.       7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  543.       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  544.       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  545.       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  546.       8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  547.     };
  548.   unsigned a;
  549.  
  550.   a = x <= 0xffff ? (x <= 0xff ? 0 : 8) : (x <= 0xffffff ?  16 : 24);
  551.  
  552.   return t[x >> a] + a;
  553. }
  554.  
  555. unsigned long long
  556. __udivdi3 (n, d)
  557.      unsigned long long n, d;
  558. {
  559.   long_long ww;
  560.   long_long nn, dd;
  561.   unsigned long d0, d1, n0, n1, n2;
  562.   unsigned long q0, q1;
  563.   unsigned b, bm;
  564.   long_long mm;
  565.  
  566.   nn.ll = n;
  567.   dd.ll = d;
  568.  
  569.   d0 = dd.s.low;
  570.   d1 = dd.s.high;
  571.   n0 = nn.s.low;
  572.   n1 = nn.s.high;
  573.  
  574.   if (d1 == 0)
  575.     {
  576.       if (d0 > n1)
  577.     {
  578.       /* 0q = nn / 0D */
  579.  
  580.       b = bits (d0);
  581.  
  582.       bm = LONG_TYPE_SIZE - b;
  583.       if (bm != 0)
  584.         {
  585.           /* Normalize, i.e. make the most significant bit of the
  586.          denominator set.  */
  587.  
  588.           d0 = d0 << bm;
  589.           n1 = (n1 << bm) | (n0 >> b);
  590.           n0 = n0 << bm;
  591.         }
  592.       div_qrnnd (q0, n0, n1, n0, d0);
  593.       q1 = 0;
  594.  
  595.       /* Remainder in n0 >> bm.  */
  596.     }
  597.       else
  598.     {
  599.       /* qq = NN / 0d */
  600.  
  601.       b = bits (d0);
  602.  
  603.       if (b == 0)
  604.         b = 1/d0;        /* Divide intentionally by zero.  */
  605.  
  606.       bm = LONG_TYPE_SIZE - b;
  607.       if (bm == 0)
  608.         {
  609.           /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
  610.          conclude (the most significant bit of n1 is set) /\ (the
  611.          leading quotient digit q1 = 1).
  612.  
  613.          This special case is necessary, not an optimization.
  614.          (Shifts counts of LONG_TYPE_SIZE are undefined.)  */
  615.  
  616.           n1 -= d0;
  617.           q1 = 1;
  618.         }
  619.       else
  620.         {
  621.           /* Normalize.  */
  622.           d0 = d0 << bm;
  623.           n2 = n1 >> b;
  624.           n1 = (n1 << bm) | (n0 >> b);
  625.           n0 = n0 << bm;
  626.  
  627.           div_qrnnd (q1, n1, n2, n1, d0);
  628.         }
  629.  
  630.       /* n1 != d0... */
  631.  
  632.       div_qrnnd (q0, n0, n1, n0, d0);
  633.  
  634.       /* Remainder in n0.  */
  635.     }
  636.     }
  637.   else
  638.     {
  639.       if (d1 > n1)
  640.     {
  641.       /* 00 = nn / DD */
  642.  
  643.       q0 = 0;
  644.       q1 = 0;
  645.  
  646.       /* Remainder in n1n0.  */
  647.     }
  648.       else
  649.     {
  650.       /* 0q = NN / dd */
  651.  
  652.       b = bits (d1);
  653.       bm = LONG_TYPE_SIZE - b;
  654.       if (bm == 0)
  655.         {
  656.           /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
  657.          conclude (the most significant bit of n1 is set) /\ (the
  658.          quotient digit q0 = 0 or 1).
  659.  
  660.          This special case is necessary, not an optimization.  */
  661.  
  662.           q0 = (n1 > d1) | (n1 == d1 && n0 >= d0);
  663.           q1 = 0;
  664.         }
  665.       else
  666.         {
  667.           /* Normalize.  */
  668.           d1 = (d1 << bm) | (d0 >> b);
  669.           d0 = d0 << bm;
  670.           n2 = n1 >> b;
  671.           n1 = (n1 << bm) | (n0 >> b);
  672.           n0 = n0 << bm;
  673.  
  674.           div_qrnnd (q0, n1, n2, n1, d1);
  675.  
  676.           mm.ll = __umulsidi3 (q0, d0);
  677.  
  678.           if (mm.s.high > n1 || (mm.s.high == n1 && mm.s.low > n0))
  679.         q0--;
  680.  
  681.           q1 = 0;
  682.  
  683.           /* Remainder in mm - n1n0 (but if q0 was decremented?).  */
  684.         }
  685.     }
  686.     }
  687.  
  688.   ww.s.low = q0;
  689.   ww.s.high = q1;
  690.   return ww.ll;
  691. }
  692. #endif
  693.  
  694. #ifdef L_cmpdi2
  695. SItype
  696. __cmpdi2 (a, b)
  697.      long long a, b;
  698. {
  699.   long_long au, bu;
  700.  
  701.   au.ll = a, bu.ll = b;
  702.  
  703.   if (au.s.high < bu.s.high)
  704.     return 0;
  705.   else if (au.s.high > bu.s.high)
  706.     return 2;
  707.   if ((unsigned) au.s.low < (unsigned) bu.s.low)
  708.     return 0;
  709.   else if ((unsigned) au.s.low > (unsigned) bu.s.low)
  710.     return 2;
  711.   return 1;
  712. }
  713. #endif
  714.  
  715. #ifdef L_ucmpdi2
  716. SItype
  717. __ucmpdi2 (a, b)
  718.      long long a, b;
  719. {
  720.   long_long au, bu;
  721.  
  722.   au.ll = a, bu.ll = b;
  723.  
  724.   if ((unsigned) au.s.high < (unsigned) bu.s.high)
  725.     return 0;
  726.   else if ((unsigned) au.s.high > (unsigned) bu.s.high)
  727.     return 2;
  728.   if ((unsigned) au.s.low < (unsigned) bu.s.low)
  729.     return 0;
  730.   else if ((unsigned) au.s.low > (unsigned) bu.s.low)
  731.     return 2;
  732.   return 1;
  733. }
  734. #endif
  735.  
  736. #ifdef L_fixunsdfdi
  737. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  738. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  739.  
  740. long long
  741. __fixunsdfdi (a)
  742.      double a;
  743. {
  744.   double b;
  745.   unsigned long long v;
  746.  
  747.   if (a < 0)
  748.     return 0;
  749.  
  750.   /* Compute high word of result, as a flonum.  */
  751.   b = (a / HIGH_WORD_COEFF);
  752.   /* Convert that to fixed (but not to long long!),
  753.      and shift it into the high word.  */
  754.   v = (unsigned long int) b;
  755.   v <<= WORD_SIZE;
  756.   /* Remove high part from the double, leaving the low part as flonum.  */
  757.   a -= (double)v;
  758.   /* Convert that to fixed (but not to long long!) and add it in.
  759.      Sometimes A comes out negative.  This is significant, since
  760.      A has more bits than a long int does.  */
  761.   if (a < 0)
  762.     v -= (unsigned long int) (- a);
  763.   else
  764.     v += (unsigned long int) a;
  765.   return v;
  766. }
  767. #endif
  768.  
  769. #ifdef L_fixdfdi
  770. long long
  771. __fixdfdi (a)
  772.      double a;
  773. {
  774.   long long __fixunsdfdi (double a);
  775.  
  776.   if (a < 0)
  777.     return - __fixunsdfdi (-a);
  778.   return __fixunsdfdi (a);
  779. }
  780. #endif
  781.  
  782. #ifdef L_fixunssfdi
  783. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  784. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  785.  
  786. long long
  787. __fixunssfdi (float original_a)
  788. {
  789.   /* Convert the float to a double, because that is surely not going
  790.      to lose any bits.  Some day someone else can write a faster version
  791.      that avoids converting to double, and verify it really works right.  */
  792.   double a = original_a;
  793.   double b;
  794.   unsigned long long v;
  795.  
  796.   if (a < 0)
  797.     return 0;
  798.  
  799.   /* Compute high word of result, as a flonum.  */
  800.   b = (a / HIGH_WORD_COEFF);
  801.   /* Convert that to fixed (but not to long long!),
  802.      and shift it into the high word.  */
  803.   v = (unsigned long int) b;
  804.   v <<= WORD_SIZE;
  805.   /* Remove high part from the double, leaving the low part as flonum.  */
  806.   a -= (double)v;
  807.   /* Convert that to fixed (but not to long long!) and add it in.
  808.      Sometimes A comes out negative.  This is significant, since
  809.      A has more bits than a long int does.  */
  810.   if (a < 0)
  811.     v -= (unsigned long int) (- a);
  812.   else
  813.     v += (unsigned long int) a;
  814.   return v;
  815. }
  816. #endif
  817.  
  818. #ifdef L_fixsfdi
  819. long long
  820. __fixsfdi (float a)
  821. {
  822.   long long __fixunssfdi (float a);
  823.  
  824.   if (a < 0)
  825.     return - __fixunssfdi (-a);
  826.   return __fixunssfdi (a);
  827. }
  828. #endif
  829.  
  830. #ifdef L_floatdidf
  831. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  832. #define HIGH_HALFWORD_COEFF (((long long) 1) << (WORD_SIZE / 2))
  833. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  834.  
  835. double
  836. __floatdidf (u)
  837.      long long u;
  838. {
  839.   double d;
  840.   int negate = 0;
  841.  
  842.   if (u < 0)
  843.     u = -u, negate = 1;
  844.  
  845.   d = (unsigned int) (u >> WORD_SIZE);
  846.   d *= HIGH_HALFWORD_COEFF;
  847.   d *= HIGH_HALFWORD_COEFF;
  848.   d += (unsigned int) (u & (HIGH_WORD_COEFF - 1));
  849.  
  850.   return (negate ? -d : d);
  851. }
  852. #endif
  853.  
  854. #ifdef L_floatdisf
  855. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  856. #define HIGH_HALFWORD_COEFF (((long long) 1) << (WORD_SIZE / 2))
  857. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  858.  
  859. float
  860. __floatdisf (u)
  861.      long long u;
  862. {
  863.   float f;
  864.   int negate = 0;
  865.  
  866.   if (u < 0)
  867.     u = -u, negate = 1;
  868.  
  869.   f = (unsigned int) (u >> WORD_SIZE);
  870.   f *= HIGH_HALFWORD_COEFF;
  871.   f *= HIGH_HALFWORD_COEFF;
  872.   f += (unsigned int) (u & (HIGH_WORD_COEFF - 1));
  873.  
  874.   return (negate ? -f : f);
  875. }
  876. #endif
  877.  
  878. #ifdef L_fixunsdfsi
  879. #include "limits.h"
  880.  
  881. unsigned SItype
  882. __fixunsdfsi (a)
  883.      double a;
  884. {
  885.   if (a >= - (double) LONG_MIN)
  886.     return (SItype) (a + LONG_MIN) - LONG_MIN;
  887.   return (SItype) a;
  888. }
  889. #endif
  890.  
  891. #ifdef L_fixunssfsi
  892. #include "limits.h"
  893.  
  894. unsigned SItype
  895. __fixunssfsi (float a)
  896. {
  897.   if (a >= - (float) LONG_MIN)
  898.     return (SItype) (a + LONG_MIN) - LONG_MIN;
  899.   return (SItype) a;
  900. }
  901. #endif
  902.  
  903. #ifdef L_varargs
  904. #ifdef i860
  905.     asm ("    .text");
  906.     asm ("    .align    4");
  907.  
  908.     asm ("___builtin_saveregs::");
  909.     asm ("    mov    sp,r30");
  910.     asm ("    andnot    0x0f,sp,sp");
  911.     asm ("    adds    -96,sp,sp");  /* allocate sufficient space on the stack */
  912.  
  913.     asm ("    st.l    r16, 0(sp)"); /* save integer regs (r16-r27) */
  914.     asm ("    st.l    r17, 4(sp)"); /* int    fixed[12] */
  915.     asm ("    st.l    r18, 8(sp)");
  916.     asm ("    st.l    r19,12(sp)");
  917.     asm ("    st.l    r20,16(sp)");
  918.     asm ("    st.l    r21,20(sp)");
  919.     asm ("    st.l    r22,24(sp)");
  920.     asm ("    st.l    r23,28(sp)");
  921.     asm ("    st.l    r24,32(sp)");
  922.     asm ("    st.l    r25,36(sp)");
  923.     asm ("    st.l    r26,40(sp)");
  924.     asm ("    st.l    r27,44(sp)");
  925.  
  926.     asm ("    fst.q    f8, 48(sp)"); /* save floating regs (f8-f15) */
  927.     asm ("    fst.q    f12,64(sp)"); /* int floating[8] */
  928.  
  929.     asm ("    st.l    r28,80(sp)"); /* pointer to more args */
  930.     asm ("    st.l    r0, 84(sp)"); /* nfixed */
  931.     asm ("    st.l    r0, 88(sp)"); /* nfloating */
  932.     asm ("    st.l    r0, 92(sp)"); /* pad */
  933.  
  934.     asm ("    mov    sp,r16");
  935.     asm ("    bri    r1");
  936.     asm ("    mov    r30,sp");
  937.                 /* recover stack and pass address to start 
  938.                    of data.  */
  939. #endif
  940. #ifdef __sparc__
  941.     asm (".global ___builtin_saveregs");
  942.     asm ("___builtin_saveregs:");
  943.     asm ("st %i0,[%fp+68]");
  944.     asm ("st %i1,[%fp+72]");
  945.     asm ("st %i2,[%fp+76]");
  946.     asm ("st %i3,[%fp+80]");
  947.     asm ("st %i4,[%fp+84]");
  948.     asm ("retl");
  949.     asm ("st %i5,[%fp+88]");
  950. #else /* not __sparc__ */
  951. #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
  952.  
  953.   asm ("    .ent __builtin_saveregs");
  954.   asm ("    .globl __builtin_saveregs");
  955.   asm ("__builtin_saveregs:");
  956.   asm ("    sw    $4,0($30)");
  957.   asm ("    sw    $5,4($30)");
  958.   asm ("    sw    $6,8($30)");
  959.   asm ("    sw    $7,12($30)");
  960.   asm ("    j    $31");
  961.   asm ("    .end __builtin_saveregs");
  962. #else /* not mips */
  963. __builtin_saveregs ()
  964. {
  965.   abort ();
  966. }
  967. #endif /* not mips */
  968. #endif /* not __sparc__ */
  969. #endif
  970.  
  971. #ifdef L_eprintf
  972. #include <stdio.h>
  973. /* This is used by the `assert' macro.  */
  974. void
  975. __eprintf (string, expression, line, filename)
  976.      char *string;
  977.      char *expression;
  978.      int line;
  979.      char *filename;
  980. {
  981.   fprintf (stderr, string, expression, line, filename);
  982.   fflush (stderr);
  983.   abort ();
  984. }
  985. #endif
  986.  
  987. #ifdef L_bb
  988. /* Avoid warning from ranlib about empty object file.  */
  989. __bb_avoid_warning ()
  990. {}
  991.  
  992. #if defined (sun) && defined (mc68000)
  993. struct bb
  994. {
  995.   int initialized;
  996.   char *filename;
  997.   int *counts;
  998.   int ncounts;
  999.   int zero_word;
  1000.   int *addresses;
  1001. };
  1002.  
  1003. __bb_init_func (blocks)
  1004.     struct bb *blocks;
  1005. {
  1006.   extern int ___tcov_init;
  1007.  
  1008.   if (! ___tcov_init)
  1009.     ___tcov_init_func ();
  1010.  
  1011.   ___bb_link (blocks->filename, blocks->counts, blocks->ncounts);
  1012. }
  1013.  
  1014. #endif
  1015. #endif
  1016.  
  1017. /* frills for C++ */
  1018.  
  1019. #ifdef L_builtin_new
  1020. typedef void (*vfp)();
  1021.  
  1022. extern vfp __new_handler;
  1023.  
  1024. char *
  1025. __builtin_new (sz)
  1026.      long sz;
  1027. {
  1028.   char *p;
  1029.  
  1030.   p = (char *)malloc (sz);
  1031.   if (p == 0)
  1032.     (*__new_handler) ();
  1033.   return p;
  1034. }
  1035. #endif
  1036.  
  1037. #ifdef L_builtin_New
  1038. typedef void (*vfp)();
  1039.  
  1040. static void
  1041. default_new_handler ();
  1042.  
  1043. vfp __new_handler = default_new_handler;
  1044.  
  1045. char *
  1046. __builtin_vec_new (p, maxindex, size, ctor)
  1047.      char *p;
  1048.      int maxindex, size;
  1049.      void (*ctor)();
  1050. {
  1051.   int i, nelts = maxindex + 1;
  1052.   char *rval;
  1053.  
  1054.   if (p == 0)
  1055.     p = (char *)__builtin_new (nelts * size);
  1056.  
  1057.   rval = p;
  1058.  
  1059.   for (i = 0; i < nelts; i++)
  1060.     {
  1061.       (*ctor) (p);
  1062.       p += size;
  1063.     }
  1064.  
  1065.   return rval;
  1066. }
  1067.  
  1068. vfp
  1069. __set_new_handler (handler)
  1070.      vfp handler;
  1071. {
  1072.   vfp prev_handler;
  1073.  
  1074.   prev_handler = __new_handler;
  1075.   if (handler == 0) handler = default_new_handler;
  1076.   __new_handler = handler;
  1077.   return prev_handler;
  1078. }
  1079.  
  1080. vfp
  1081. set_new_handler (handler)
  1082.      vfp handler;
  1083. {
  1084.   return __set_new_handler (handler);
  1085. }
  1086.  
  1087. static void
  1088. default_new_handler ()
  1089. {
  1090.   /* don't use fprintf (stderr, ...) because it may need to call malloc.  */
  1091.   write (2, "default_new_handler: out of memory... aaaiiiiiieeeeeeeeeeeeee!\n", 65);
  1092.   /* don't call exit () because that may call global destructors which
  1093.      may cause a loop.  */
  1094.   _exit (-1);
  1095. }
  1096. #endif
  1097.  
  1098. #ifdef L_builtin_del
  1099. typedef void (*vfp)();
  1100.  
  1101. void
  1102. __builtin_delete (ptr)
  1103.      char *ptr;
  1104. {
  1105.   if (ptr)
  1106.     free (ptr);
  1107. }
  1108.  
  1109. void
  1110. __builtin_vec_delete (ptr, maxindex, size, dtor, auto_delete_vec, auto_delete)
  1111.      char *ptr;
  1112.      int maxindex, size;
  1113.      void (*dtor)();
  1114.      int auto_delete;
  1115. {
  1116.   int i, nelts = maxindex + 1;
  1117.   char *p = ptr;
  1118.  
  1119.   ptr += nelts * size;
  1120.  
  1121.   for (i = 0; i < nelts; i++)
  1122.     {
  1123.       ptr -= size;
  1124.       (*dtor) (ptr, auto_delete);
  1125.     }
  1126.  
  1127.   if (auto_delete_vec)
  1128.     __builtin_delete (p);
  1129. }
  1130.  
  1131. #endif
  1132.  
  1133. #ifdef L_shtab
  1134. unsigned int __shtab[] = {
  1135.     0x00000001, 0x00000002, 0x00000004, 0x00000008,
  1136.     0x00000010, 0x00000020, 0x00000040, 0x00000080,
  1137.     0x00000100, 0x00000200, 0x00000400, 0x00000800,
  1138.     0x00001000, 0x00002000, 0x00004000, 0x00008000,
  1139.     0x00010000, 0x00020000, 0x00040000, 0x00080000,
  1140.     0x00100000, 0x00200000, 0x00400000, 0x00800000,
  1141.     0x01000000, 0x02000000, 0x04000000, 0x08000000,
  1142.     0x10000000, 0x20000000, 0x40000000, 0x80000000
  1143.   };
  1144. #endif
  1145.  
  1146. #ifdef L_clear_cache
  1147. /* Clear part of an instruction cache.  */
  1148.  
  1149. #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
  1150.  
  1151. void
  1152. __clear_cache (beg, end)
  1153.      char *beg, *end;
  1154. {
  1155. #ifdef INSN_CACHE_SIZE
  1156.   static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
  1157.   static int initialized = 0;
  1158.   int offset;
  1159.   unsigned int start_addr, end_addr;
  1160.   typedef (*function_ptr) ();
  1161.  
  1162. #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
  1163.   /* It's cheaper to clear the whole cache.
  1164.      Put in a series of jump instructions so that calling the beginning
  1165.      of the cache will clear the whole thing.  */
  1166.  
  1167.   if (! initialized)
  1168.     {
  1169.       int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
  1170.          & -INSN_CACHE_LINE_WIDTH);
  1171.       int end_ptr = ptr + INSN_CACHE_SIZE;
  1172.  
  1173.       while (ptr < end_ptr)
  1174.     {
  1175.       *(INSTRUCTION_TYPE *)ptr
  1176.         = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
  1177.       ptr += INSN_CACHE_LINE_WIDTH;
  1178.     }
  1179.       *(INSTRUCTION_TYPE *)(ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
  1180.  
  1181.       initialized = 1;
  1182.     }
  1183.  
  1184.   /* Call the beginning of the sequence.  */
  1185.   (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
  1186.             & -INSN_CACHE_LINE_WIDTH))
  1187.    ());
  1188.  
  1189. #else /* Cache is large.  */
  1190.  
  1191.   if (! initialized)
  1192.     {
  1193.       int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
  1194.          & -INSN_CACHE_LINE_WIDTH);
  1195.  
  1196.       while (ptr < (int) array + sizeof array)
  1197.     {
  1198.       *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
  1199.       ptr += INSN_CACHE_LINE_WIDTH;
  1200.     }
  1201.  
  1202.       initialized = 1;
  1203.     }
  1204.  
  1205.   /* Find the location in array that occupies the same cache line as BEG.  */
  1206.  
  1207.   offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
  1208.   start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
  1209.          & -INSN_CACHE_PLANE_SIZE)
  1210.         + offset);
  1211.  
  1212.   /* Compute the cache alignment of the place to stop clearing.  */
  1213. #if 0  /* This is not needed for gcc's purposes.  */
  1214.   /* If the block to clear is bigger than a cache plane,
  1215.      we clear the entire cache, and OFFSET is already correct.  */ 
  1216.   if (end < beg + INSN_CACHE_PLANE_SIZE)
  1217. #endif
  1218.     offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
  1219.            & -INSN_CACHE_LINE_WIDTH)
  1220.           & (INSN_CACHE_PLANE_SIZE - 1));
  1221.  
  1222. #if INSN_CACHE_DEPTH > 1
  1223.   end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
  1224.   if (end_addr <= start_addr)
  1225.     end_addr += INSN_CACHE_PLANE_SIZE;
  1226.  
  1227.   for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
  1228.     {
  1229.       int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
  1230.       int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
  1231.  
  1232.       while (addr != stop)
  1233.     {
  1234.       /* Call the return instruction at ADDR.  */
  1235.       ((function_ptr) addr) ();
  1236.  
  1237.       addr += INSN_CACHE_LINE_WIDTH;
  1238.     }
  1239.     }
  1240. #else /* just one plane */
  1241.   do
  1242.     {
  1243.       /* Call the return instruction at START_ADDR.  */
  1244.       ((function_ptr) start_addr) ();
  1245.  
  1246.       start_addr += INSN_CACHE_LINE_WIDTH;
  1247.     }
  1248.   while ((start_addr % INSN_CACHE_SIZE) != offset);
  1249. #endif /* just one plane */
  1250. #endif /* Cache is large */
  1251. #endif /* Cache exists */
  1252. }
  1253.  
  1254. #endif /* L_clear_cache */
  1255.  
  1256. #ifdef L_trampoline
  1257.  
  1258. /* Jump to a trampoline, loading the static chain address.  */
  1259.  
  1260. #ifdef TRANSFER_FROM_TRAMPOLINE 
  1261.  
  1262. /* asm with operands is permitted only within a function.  */
  1263. static dummy ()
  1264. {
  1265.   TRANSFER_FROM_TRAMPOLINE 
  1266. }
  1267. #endif
  1268.  
  1269. #endif
  1270.