home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / gnulib / normal / gnulib2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  28.0 KB  |  1,345 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 2, 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. /* It is incorrect to include config.h here, because this file is being
  28.    compiled for the target, and hence definitions concerning only the host
  29.    do not apply.  */
  30.  
  31. /* Thu Sep 23 04:05:45 1993 GM: not in our case, we need the functions here ! */
  32.  
  33. #include "config.h"
  34.  
  35. #include "types.h"
  36. #include <stddef.h>
  37.  
  38. /* Don't use `fancy_abort' here even if config.h says to use it.  */
  39. #ifdef abort
  40. #undef abort
  41. #endif
  42.  
  43. /* Need to undef this because LONG_TYPE_SIZE may rely upon GCC's
  44.    internal `target_flags' variable.  */
  45. #undef LONG_TYPE_SIZE
  46.  
  47. #define LONG_TYPE_SIZE (sizeof (long) * BITS_PER_UNIT)
  48.  
  49. #ifndef SItype
  50. #define SItype long int
  51. #endif
  52.  
  53. /* long long ints are pairs of long ints in the order determined by
  54.    WORDS_BIG_ENDIAN.  */
  55.  
  56. #if WORDS_BIG_ENDIAN
  57.   struct longlong {long high, low;};
  58. #else
  59.   struct longlong {long low, high;};
  60. #endif
  61.  
  62. /* We need this union to unpack/pack longlongs, since we don't have
  63.    any arithmetic yet.  Incoming long long parameters are stored
  64.    into the `ll' field, and the unpacked result is read from the struct
  65.    longlong.  */
  66.  
  67. typedef union
  68. {
  69.   struct longlong s;
  70.   long long ll;
  71. } long_long;
  72.  
  73. #if defined (L_udivmoddi4) || defined (L_muldi3)
  74.  
  75. #include "longlong.h"
  76.  
  77. #endif /* udiv or mul */
  78.  
  79. #if defined (L_negdi2) || defined (L_divdi3) || defined (L_moddi3)
  80. #if defined (L_divdi3) || defined (L_moddi3)
  81. /* static inline */
  82. #endif
  83. long long
  84. __negdi2 (u)
  85.      long long u;
  86. {
  87.   long_long w;
  88.   long_long uu;
  89.  
  90.   uu.ll = u;
  91.  
  92.   w.s.low = -uu.s.low;
  93.   w.s.high = -uu.s.high - ((unsigned long) w.s.low > 0);
  94.  
  95.   return w.ll;
  96. }
  97. #endif
  98.  
  99. #ifdef L_lshldi3
  100. long long
  101. __lshldi3 (u, b)
  102.      long long u;
  103.      int b;
  104. {
  105.   long_long w;
  106.   long bm;
  107.   long_long uu;
  108.  
  109.   if (b == 0)
  110.     return u;
  111.  
  112.   uu.ll = u;
  113.  
  114.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  115.   if (bm <= 0)
  116.     {
  117.       w.s.low = 0;
  118.       w.s.high = (unsigned long)uu.s.low << -bm;
  119.     }
  120.   else
  121.     {
  122.       unsigned long carries = (unsigned long)uu.s.low >> bm;
  123.       w.s.low = (unsigned long)uu.s.low << b;
  124.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  125.     }
  126.  
  127.   return w.ll;
  128. }
  129. #endif
  130.  
  131. #ifdef L_lshrdi3
  132. long long
  133. __lshrdi3 (u, b)
  134.      long long u;
  135.      int b;
  136. {
  137.   long_long w;
  138.   long bm;
  139.   long_long uu;
  140.  
  141.   if (b == 0)
  142.     return u;
  143.  
  144.   uu.ll = u;
  145.  
  146.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  147.   if (bm <= 0)
  148.     {
  149.       w.s.high = 0;
  150.       w.s.low = (unsigned long)uu.s.high >> -bm;
  151.     }
  152.   else
  153.     {
  154.       unsigned long carries = (unsigned long)uu.s.high << bm;
  155.       w.s.high = (unsigned long)uu.s.high >> b;
  156.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  157.     }
  158.  
  159.   return w.ll;
  160. }
  161. #endif
  162.  
  163. #ifdef L_ashldi3
  164. long long
  165. __ashldi3 (u, b)
  166.      long long u;
  167.      int b;
  168. {
  169.   long_long w;
  170.   long bm;
  171.   long_long uu;
  172.  
  173.   if (b == 0)
  174.     return u;
  175.  
  176.   uu.ll = u;
  177.  
  178.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  179.   if (bm <= 0)
  180.     {
  181.       w.s.low = 0;
  182.       w.s.high = (unsigned long)uu.s.low << -bm;
  183.     }
  184.   else
  185.     {
  186.       unsigned long carries = (unsigned long)uu.s.low >> bm;
  187.       w.s.low = (unsigned long)uu.s.low << b;
  188.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  189.     }
  190.  
  191.   return w.ll;
  192. }
  193. #endif
  194.  
  195. #ifdef L_ashrdi3
  196. long long
  197. __ashrdi3 (u, b)
  198.      long long u;
  199.      int b;
  200. {
  201.   long_long w;
  202.   long bm;
  203.   long_long uu;
  204.  
  205.   if (b == 0)
  206.     return u;
  207.  
  208.   uu.ll = u;
  209.  
  210.   bm = (sizeof (long) * BITS_PER_UNIT) - b;
  211.   if (bm <= 0)
  212.     {
  213.       /* w.s.high = 1..1 or 0..0 */
  214.       w.s.high = uu.s.high >> (sizeof (long) * BITS_PER_UNIT - 1);
  215.       w.s.low = uu.s.high >> -bm;
  216.     }
  217.   else
  218.     {
  219.       unsigned long carries = (unsigned long)uu.s.high << bm;
  220.       w.s.high = uu.s.high >> b;
  221.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  222.     }
  223.  
  224.   return w.ll;
  225. }
  226. #endif
  227.  
  228. #ifdef L_muldi3
  229. long long
  230. __muldi3 (u, v)
  231.      long long u, v;
  232. {
  233.   long_long w;
  234.   long_long uu, vv;
  235.  
  236.   uu.ll = u,
  237.   vv.ll = v;
  238.  
  239.   w.ll = __umulsidi3 (uu.s.low, vv.s.low);
  240.   w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
  241.            + (unsigned long) uu.s.high * (unsigned long) vv.s.low);
  242.  
  243.   return w.ll;
  244. }
  245. #endif
  246.  
  247. #ifdef L_udivmoddi4
  248. static const unsigned char __clz_tab[] =
  249. {
  250.   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,
  251.   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,
  252.   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,
  253.   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,
  254.   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,
  255.   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,
  256.   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,
  257.   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,
  258. };
  259.  
  260. unsigned long long
  261. __udivmoddi4 (n, d, rp)
  262.      unsigned long long n, d;
  263.      unsigned long long int *rp;
  264. {
  265.   long_long ww;
  266.   long_long nn, dd;
  267.   long_long rr;
  268.   unsigned long d0, d1, n0, n1, n2;
  269.   unsigned long q0, q1;
  270.   unsigned b, bm;
  271.  
  272.   nn.ll = n;
  273.   dd.ll = d;
  274.  
  275.   d0 = dd.s.low;
  276.   d1 = dd.s.high;
  277.   n0 = nn.s.low;
  278.   n1 = nn.s.high;
  279.  
  280. #if !UDIV_NEEDS_NORMALIZATION
  281.   if (d1 == 0)
  282.     {
  283.       if (d0 > n1)
  284.     {
  285.       /* 0q = nn / 0D */
  286.  
  287.       udiv_qrnnd (q0, n0, n1, n0, d0);
  288.       q1 = 0;
  289.  
  290.       /* Remainder in n0.  */
  291.     }
  292.       else
  293.     {
  294.       /* qq = NN / 0d */
  295.  
  296.       if (d0 == 0)
  297.         d0 = 1 / d0;    /* Divide intentionally by zero.  */
  298.  
  299.       udiv_qrnnd (q1, n1, 0, n1, d0);
  300.       udiv_qrnnd (q0, n0, n1, n0, d0);
  301.  
  302.       /* Remainder in n0.  */
  303.     }
  304.  
  305.       if (rp != 0)
  306.     {
  307.       rr.s.low = n0;
  308.       rr.s.high = 0;
  309.       *rp = rr.ll;
  310.     }
  311.     }
  312.  
  313. #else /* UDIV_NEEDS_NORMALIZATION */
  314.  
  315.   if (d1 == 0)
  316.     {
  317.       if (d0 > n1)
  318.     {
  319.       /* 0q = nn / 0D */
  320.  
  321.       count_leading_zeros (bm, d0);
  322.  
  323.       if (bm != 0)
  324.         {
  325.           /* Normalize, i.e. make the most significant bit of the
  326.          denominator set.  */
  327.  
  328.           d0 = d0 << bm;
  329.           n1 = (n1 << bm) | (n0 >> (LONG_TYPE_SIZE - bm));
  330.           n0 = n0 << bm;
  331.         }
  332.  
  333.       udiv_qrnnd (q0, n0, n1, n0, d0);
  334.       q1 = 0;
  335.  
  336.       /* Remainder in n0 >> bm.  */
  337.     }
  338.       else
  339.     {
  340.       /* qq = NN / 0d */
  341.  
  342.       if (d0 == 0)
  343.         d0 = 1 / d0;    /* Divide intentionally by zero.  */
  344.  
  345.       count_leading_zeros (bm, d0);
  346.  
  347.       if (bm == 0)
  348.         {
  349.           /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
  350.          conclude (the most significant bit of n1 is set) /\ (the
  351.          leading quotient digit q1 = 1).
  352.  
  353.          This special case is necessary, not an optimization.
  354.          (Shifts counts of LONG_TYPE_SIZE are undefined.)  */
  355.  
  356.           n1 -= d0;
  357.           q1 = 1;
  358.         }
  359.       else
  360.         {
  361.           /* Normalize.  */
  362.  
  363.           b = LONG_TYPE_SIZE - bm;
  364.  
  365.           d0 = d0 << bm;
  366.           n2 = n1 >> b;
  367.           n1 = (n1 << bm) | (n0 >> b);
  368.           n0 = n0 << bm;
  369.  
  370.           udiv_qrnnd (q1, n1, n2, n1, d0);
  371.         }
  372.  
  373.       /* n1 != d0... */
  374.  
  375.       udiv_qrnnd (q0, n0, n1, n0, d0);
  376.  
  377.       /* Remainder in n0 >> bm.  */
  378.     }
  379.  
  380.       if (rp != 0)
  381.     {
  382.       rr.s.low = n0 >> bm;
  383.       rr.s.high = 0;
  384.       *rp = rr.ll;
  385.     }
  386.     }
  387. #endif /* UDIV_NEEDS_NORMALIZATION */
  388.  
  389.   else
  390.     {
  391.       if (d1 > n1)
  392.     {
  393.       /* 00 = nn / DD */
  394.  
  395.       q0 = 0;
  396.       q1 = 0;
  397.  
  398.       /* Remainder in n1n0.  */
  399.       if (rp != 0)
  400.         {
  401.           rr.s.low = n0;
  402.           rr.s.high = n1;
  403.           *rp = rr.ll;
  404.         }
  405.     }
  406.       else
  407.     {
  408.       /* 0q = NN / dd */
  409.  
  410.       count_leading_zeros (bm, d1);
  411.       if (bm == 0)
  412.         {
  413.           /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
  414.          conclude (the most significant bit of n1 is set) /\ (the
  415.          quotient digit q0 = 0 or 1).
  416.  
  417.          This special case is necessary, not an optimization.  */
  418.  
  419.           /* The condition on the next line takes advantage of that
  420.          n1 >= d1 (true due to program flow).  */
  421.           if (n1 > d1 || n0 >= d0)
  422.         {
  423.           q0 = 1;
  424.           sub_ddmmss (n1, n0, n1, n0, d1, d0);
  425.         }
  426.           else
  427.         q0 = 0;
  428.  
  429.           q1 = 0;
  430.  
  431.           if (rp != 0)
  432.         {
  433.           rr.s.low = n0;
  434.           rr.s.high = n1;
  435.           *rp = rr.ll;
  436.         }
  437.         }
  438.       else
  439.         {
  440.           unsigned long m1, m0;
  441.           /* Normalize.  */
  442.  
  443.           b = LONG_TYPE_SIZE - bm;
  444.  
  445.           d1 = (d1 << bm) | (d0 >> b);
  446.           d0 = d0 << bm;
  447.           n2 = n1 >> b;
  448.           n1 = (n1 << bm) | (n0 >> b);
  449.           n0 = n0 << bm;
  450.  
  451.           udiv_qrnnd (q0, n1, n2, n1, d1);
  452.           umul_ppmm (m1, m0, q0, d0);
  453.  
  454.           if (m1 > n1 || (m1 == n1 && m0 > n0))
  455.         {
  456.           q0--;
  457.           sub_ddmmss (m1, m0, m1, m0, d1, d0);
  458.         }
  459.  
  460.           q1 = 0;
  461.  
  462.           /* Remainder in (n1n0 - m1m0) >> bm.  */
  463.           if (rp != 0)
  464.         {
  465.           sub_ddmmss (n1, n0, n1, n0, m1, m0);
  466.           rr.s.low = (n1 << b) | (n0 >> bm);
  467.           rr.s.high = n1 >> bm;
  468.           *rp = rr.ll;
  469.         }
  470.         }
  471.     }
  472.     }
  473.  
  474.   ww.s.low = q0;
  475.   ww.s.high = q1;
  476.   return ww.ll;
  477. }
  478. #endif
  479.  
  480. #ifdef L_divdi3
  481. unsigned long long __udivmoddi4();
  482. long long
  483. __divdi3 (u, v)
  484.      long long u, v;
  485. {
  486.   int c = 0;
  487.   long_long uu, vv;
  488.   long long w;
  489.  
  490.   uu.ll = u;
  491.   vv.ll = v;
  492.  
  493.   if (uu.s.high < 0)
  494.     c = ~c,
  495.     uu.ll = __negdi2 (uu.ll);
  496.   if (vv.s.high < 0)
  497.     c = ~c,
  498.     vv.ll = __negdi2 (vv.ll);
  499.  
  500.   w = __udivmoddi4 (uu.ll, vv.ll, (unsigned long long *) 0);
  501.   if (c)
  502.     w = __negdi2 (w);
  503.  
  504.   return w;
  505. }
  506. #endif
  507.  
  508. #ifdef L_moddi3
  509. unsigned long long __udivmoddi4();
  510. long long
  511. __moddi3 (u, v)
  512.      long long u, v;
  513. {
  514.   int c = 0;
  515.   long_long uu, vv;
  516.   long long w;
  517.  
  518.   uu.ll = u;
  519.   vv.ll = v;
  520.  
  521.   if (uu.s.high < 0)
  522.     c = ~c,
  523.     uu.ll = __negdi2 (uu.ll);
  524.   if (vv.s.high < 0)
  525.     vv.ll = __negdi2 (vv.ll);
  526.  
  527.   (void) __udivmoddi4 (uu.ll, vv.ll, &w);
  528.   if (c)
  529.     w = __negdi2 (w);
  530.  
  531.   return w;
  532. }
  533. #endif
  534.  
  535. #ifdef L_umoddi3
  536. unsigned long long __udivmoddi4();
  537. unsigned long long
  538. __umoddi3 (u, v)
  539.      unsigned long long u, v;
  540. {
  541.   long long w;
  542.  
  543.   (void) __udivmoddi4 (u, v, &w);
  544.  
  545.   return w;
  546. }
  547. #endif
  548.  
  549. #ifdef L_udivdi3
  550. unsigned long long __udivmoddi4();
  551. unsigned long long
  552. __udivdi3 (n, d)
  553.      unsigned long long n, d;
  554. {
  555.   return __udivmoddi4 (n, d, (unsigned long long *) 0);
  556. }
  557. #endif
  558.  
  559. #ifdef L_cmpdi2
  560. SItype
  561. __cmpdi2 (a, b)
  562.      long long a, b;
  563. {
  564.   long_long au, bu;
  565.  
  566.   au.ll = a, bu.ll = b;
  567.  
  568.   if (au.s.high < bu.s.high)
  569.     return 0;
  570.   else if (au.s.high > bu.s.high)
  571.     return 2;
  572.   if ((unsigned long) au.s.low < (unsigned long) bu.s.low)
  573.     return 0;
  574.   else if ((unsigned long) au.s.low > (unsigned long) bu.s.low)
  575.     return 2;
  576.   return 1;
  577. }
  578. #endif
  579.  
  580. #ifdef L_ucmpdi2
  581. SItype
  582. __ucmpdi2 (a, b)
  583.      long long a, b;
  584. {
  585.   long_long au, bu;
  586.  
  587.   au.ll = a, bu.ll = b;
  588.  
  589.   if ((unsigned long) au.s.high < (unsigned long) bu.s.high)
  590.     return 0;
  591.   else if ((unsigned long) au.s.high > (unsigned long) bu.s.high)
  592.     return 2;
  593.   if ((unsigned long) au.s.low < (unsigned long) bu.s.low)
  594.     return 0;
  595.   else if ((unsigned long) au.s.low > (unsigned long) bu.s.low)
  596.     return 2;
  597.   return 1;
  598. }
  599. #endif
  600.  
  601. #ifdef L_fixunsdfdi
  602. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  603. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  604.  
  605. long long
  606. __fixunsdfdi (a)
  607.      double a;
  608. {
  609.   double b;
  610.   unsigned long long v;
  611.  
  612.   if (a < 0)
  613.     return 0;
  614.  
  615.   /* Compute high word of result, as a flonum.  */
  616.   b = (a / HIGH_WORD_COEFF);
  617.   /* Convert that to fixed (but not to long long!),
  618.      and shift it into the high word.  */
  619.   v = (unsigned long int) b;
  620.   v <<= WORD_SIZE;
  621.   /* Remove high part from the double, leaving the low part as flonum.  */
  622.   a -= (double)v;
  623.   /* Convert that to fixed (but not to long long!) and add it in.
  624.      Sometimes A comes out negative.  This is significant, since
  625.      A has more bits than a long int does.  */
  626.   if (a < 0)
  627.     v -= (unsigned long int) (- a);
  628.   else
  629.     v += (unsigned long int) a;
  630.   return v;
  631. }
  632. #endif
  633.  
  634. #ifdef L_fixdfdi
  635. long long
  636. __fixdfdi (a)
  637.      double a;
  638. {
  639.   long long __fixunsdfdi (double a);
  640.  
  641.   if (a < 0)
  642.     return - __fixunsdfdi (-a);
  643.   return __fixunsdfdi (a);
  644. }
  645. #endif
  646.  
  647. #ifdef L_fixunssfdi
  648. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  649. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  650.  
  651. long long
  652. __fixunssfdi (float original_a)
  653. {
  654.   /* Convert the float to a double, because that is surely not going
  655.      to lose any bits.  Some day someone else can write a faster version
  656.      that avoids converting to double, and verify it really works right.  */
  657.   double a = original_a;
  658.   double b;
  659.   unsigned long long v;
  660.  
  661.   if (a < 0)
  662.     return 0;
  663.  
  664.   /* Compute high word of result, as a flonum.  */
  665.   b = (a / HIGH_WORD_COEFF);
  666.   /* Convert that to fixed (but not to long long!),
  667.      and shift it into the high word.  */
  668.   v = (unsigned long int) b;
  669.   v <<= WORD_SIZE;
  670.   /* Remove high part from the double, leaving the low part as flonum.  */
  671.   a -= (double)v;
  672.   /* Convert that to fixed (but not to long long!) and add it in.
  673.      Sometimes A comes out negative.  This is significant, since
  674.      A has more bits than a long int does.  */
  675.   if (a < 0)
  676.     v -= (unsigned long int) (- a);
  677.   else
  678.     v += (unsigned long int) a;
  679.   return v;
  680. }
  681. #endif
  682.  
  683. #ifdef L_fixsfdi
  684. long long
  685. __fixsfdi (float a)
  686. {
  687.   long long __fixunssfdi (float a);
  688.  
  689.   if (a < 0)
  690.     return - __fixunssfdi (-a);
  691.   return __fixunssfdi (a);
  692. }
  693. #endif
  694.  
  695. #ifdef L_floatdidf
  696. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  697. #define HIGH_HALFWORD_COEFF (((long long) 1) << (WORD_SIZE / 2))
  698. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  699.  
  700. double
  701. __floatdidf (u)
  702.      long long u;
  703. {
  704.   double d;
  705.   int negate = 0;
  706.  
  707.   if (u < 0)
  708.     u = -u, negate = 1;
  709.  
  710.   d = (unsigned int) (u >> WORD_SIZE);
  711.   d *= HIGH_HALFWORD_COEFF;
  712.   d *= HIGH_HALFWORD_COEFF;
  713.   d += (unsigned int) (u & (HIGH_WORD_COEFF - 1));
  714.  
  715.   return (negate ? -d : d);
  716. }
  717. #endif
  718.  
  719. #ifdef L_floatdisf
  720. #define WORD_SIZE (sizeof (long) * BITS_PER_UNIT)
  721. #define HIGH_HALFWORD_COEFF (((long long) 1) << (WORD_SIZE / 2))
  722. #define HIGH_WORD_COEFF (((long long) 1) << WORD_SIZE)
  723.  
  724. float
  725. __floatdisf (u)
  726.      long long u;
  727. {
  728.   float f;
  729.   int negate = 0;
  730.  
  731.   if (u < 0)
  732.     u = -u, negate = 1;
  733.  
  734.   f = (unsigned int) (u >> WORD_SIZE);
  735.   f *= HIGH_HALFWORD_COEFF;
  736.   f *= HIGH_HALFWORD_COEFF;
  737.   f += (unsigned int) (u & (HIGH_WORD_COEFF - 1));
  738.  
  739.   return (negate ? -f : f);
  740. }
  741. #endif
  742.  
  743. #ifdef L_fixunsdfsi
  744. #include "limits.h"
  745.  
  746. unsigned SItype
  747. __fixunsdfsi (a)
  748.      double a;
  749. {
  750.   if (a >= - (double) LONG_MIN)
  751.     return (SItype) (a + LONG_MIN) - LONG_MIN;
  752.   return (SItype) a;
  753. }
  754. #endif
  755.  
  756. #ifdef L_fixunssfsi
  757. #include "limits.h"
  758.  
  759. unsigned SItype
  760. __fixunssfsi (float a)
  761. {
  762.   if (a >= - (float) LONG_MIN)
  763.     return (SItype) (a + LONG_MIN) - LONG_MIN;
  764.   return (SItype) a;
  765. }
  766. #endif
  767.  
  768. #ifdef L_varargs
  769. #ifdef __i860__
  770. #ifdef SVR4
  771.     asm ("    .text");
  772.     asm ("    .align    4");
  773.  
  774.     asm (".globl    __builtin_saveregs");
  775. asm ("__builtin_saveregs:");
  776.     asm ("    andnot    0x0f,%sp,%sp");    /* round down to 16-byte boundary */
  777.     asm ("    adds    -96,%sp,%sp");  /* allocate stack space for reg save
  778.                        area and also for a new va_list
  779.                        structure */
  780.     /* Save all argument registers in the arg reg save area.  The
  781.        arg reg save area must have the following layout (according
  782.        to the svr4 ABI):
  783.  
  784.         struct {
  785.           union  {
  786.             float freg[8];
  787.             double dreg[4];
  788.           } float_regs;
  789.           long    ireg[12];
  790.         };
  791.     */
  792.  
  793.     asm ("    fst.q    %f8,  0(%sp)"); /* save floating regs (f8-f15)  */
  794.     asm ("    fst.q    %f12,16(%sp)"); 
  795.  
  796.     asm ("    st.l    %r16,32(%sp)"); /* save integer regs (r16-r27) */
  797.     asm ("    st.l    %r17,36(%sp)"); 
  798.     asm ("    st.l    %r18,40(%sp)");
  799.     asm ("    st.l    %r19,44(%sp)");
  800.     asm ("    st.l    %r20,48(%sp)");
  801.     asm ("    st.l    %r21,52(%sp)");
  802.     asm ("    st.l    %r22,56(%sp)");
  803.     asm ("    st.l    %r23,60(%sp)");
  804.     asm ("    st.l    %r24,64(%sp)");
  805.     asm ("    st.l    %r25,68(%sp)");
  806.     asm ("    st.l    %r26,72(%sp)");
  807.     asm ("    st.l    %r27,76(%sp)");
  808.  
  809.     asm ("    adds    80,%sp,%r16");  /* compute the address of the new
  810.                        va_list structure.  Put in into
  811.                        r16 so that it will be returned
  812.                        to the caller.  */
  813.  
  814.     /* Initialize all fields of the new va_list structure.  This
  815.        structure looks like:
  816.  
  817.         typedef struct {
  818.             unsigned long    ireg_used;
  819.             unsigned long    freg_used;
  820.             long        *reg_base;
  821.             long        *mem_ptr;
  822.         } va_list;
  823.     */
  824.  
  825.     asm ("    st.l    %r0, 0(%r16)"); /* nfixed */
  826.     asm ("    st.l    %r0, 4(%r16)"); /* nfloating */
  827.     asm ("  st.l    %sp, 8(%r16)"); /* __va_ctl points to __va_struct.  */
  828.     asm ("    bri    %r1");        /* delayed return */
  829.     asm ("    st.l    %r28,12(%r16)"); /* pointer to overflow args */
  830.  
  831. #else /* not SVR4 */
  832.     asm ("    .text");
  833.     asm ("    .align    4");
  834.  
  835.     asm (".globl    ___builtin_saveregs");
  836.     asm ("___builtin_saveregs:");
  837.     asm ("    mov    sp,r30");
  838.     asm ("    andnot    0x0f,sp,sp");
  839.     asm ("    adds    -96,sp,sp");  /* allocate sufficient space on the stack */
  840.  
  841. /* Fill in the __va_struct.  */
  842.     asm ("    st.l    r16, 0(sp)"); /* save integer regs (r16-r27) */
  843.     asm ("    st.l    r17, 4(sp)"); /* int    fixed[12] */
  844.     asm ("    st.l    r18, 8(sp)");
  845.     asm ("    st.l    r19,12(sp)");
  846.     asm ("    st.l    r20,16(sp)");
  847.     asm ("    st.l    r21,20(sp)");
  848.     asm ("    st.l    r22,24(sp)");
  849.     asm ("    st.l    r23,28(sp)");
  850.     asm ("    st.l    r24,32(sp)");
  851.     asm ("    st.l    r25,36(sp)");
  852.     asm ("    st.l    r26,40(sp)");
  853.     asm ("    st.l    r27,44(sp)");
  854.  
  855.     asm ("    fst.q    f8, 48(sp)"); /* save floating regs (f8-f15) */
  856.     asm ("    fst.q    f12,64(sp)"); /* int floating[8] */
  857.  
  858. /* Fill in the __va_ctl.  */
  859.     asm ("  st.l    sp, 80(sp)"); /* __va_ctl points to __va_struct.  */
  860.     asm ("    st.l    r28,84(sp)"); /* pointer to more args */
  861.     asm ("    st.l    r0, 88(sp)"); /* nfixed */
  862.     asm ("    st.l    r0, 92(sp)"); /* nfloating */
  863.  
  864.     asm ("    adds    80,sp,r16");  /* return address of the __va_ctl.  */
  865.     asm ("    bri    r1");
  866.     asm ("    mov    r30,sp");
  867.                 /* recover stack and pass address to start 
  868.                    of data.  */
  869. #endif /* not SVR4 */
  870. #else /* not __i860__ */
  871. #ifdef __sparc__
  872.     asm (".global ___builtin_saveregs");
  873.     asm ("___builtin_saveregs:");
  874.     asm ("st %i0,[%fp+68]");
  875.     asm ("st %i1,[%fp+72]");
  876.     asm ("st %i2,[%fp+76]");
  877.     asm ("st %i3,[%fp+80]");
  878.     asm ("st %i4,[%fp+84]");
  879.     asm ("retl");
  880.     asm ("st %i5,[%fp+88]");
  881. #else /* not __sparc__ */
  882. #if defined(__MIPSEL__) | defined(__R3000__) | defined(__R2000__) | defined(__mips__)
  883.  
  884.   asm ("    .text");
  885.   asm ("    .ent __builtin_saveregs");
  886.   asm ("    .globl __builtin_saveregs");
  887.   asm ("__builtin_saveregs:");
  888.   asm ("    sw    $4,0($30)");
  889.   asm ("    sw    $5,4($30)");
  890.   asm ("    sw    $6,8($30)");
  891.   asm ("    sw    $7,12($30)");
  892.   asm ("    j    $31");
  893.   asm ("    .end __builtin_saveregs");
  894. #else /* not __mips__, etc. */
  895. __builtin_saveregs ()
  896. {
  897.   abort ();
  898. }
  899. #endif /* not __mips__ */
  900. #endif /* not __sparc__ */
  901. #endif /* not __i860__ */
  902. #endif
  903.  
  904. #ifdef L_eprintf
  905. #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch.  */
  906. #include <stdio.h>
  907. /* This is used by the `assert' macro.  */
  908. void
  909. __eprintf (string, expression, line, filename)
  910.      char *string;
  911.      char *expression;
  912.      int line;
  913.      char *filename;
  914. {
  915.   fprintf (stderr, string, expression, line, filename);
  916.   fflush (stderr);
  917.   abort ();
  918. }
  919. #endif
  920.  
  921. #ifdef L_bb
  922. /* Avoid warning from ranlib about empty object file.  */
  923. void
  924. __bb_avoid_warning ()
  925. {}
  926.  
  927. #if defined (__sun__) && defined (__mc68000__)
  928. struct bb
  929. {
  930.   int initialized;
  931.   char *filename;
  932.   int *counts;
  933.   int ncounts;
  934.   int zero_word;
  935.   int *addresses;
  936. };
  937.  
  938. __bb_init_func (blocks)
  939.     struct bb *blocks;
  940. {
  941.   extern int ___tcov_init;
  942.  
  943.   if (! ___tcov_init)
  944.     ___tcov_init_func ();
  945.  
  946.   ___bb_link (blocks->filename, blocks->counts, blocks->ncounts);
  947. }
  948.  
  949. #endif
  950. #endif
  951.  
  952. /* frills for C++ */
  953.  
  954. #ifdef L_builtin_new
  955. typedef void (*vfp)(void);
  956.  
  957. extern vfp __new_handler;
  958.  
  959. void *
  960. __builtin_new (sz)
  961.      long sz;
  962. {
  963.   void *p;
  964.  
  965.   p = (void *) malloc (sz);
  966.   if (p == 0)
  967.     (*__new_handler) ();
  968.   return p;
  969. }
  970. #endif
  971.  
  972. #ifdef L_builtin_New
  973. typedef void (*vfp)(void);
  974.  
  975. static void default_new_handler ();
  976.  
  977. vfp __new_handler = default_new_handler;
  978.  
  979. void *
  980. __builtin_vec_new (p, maxindex, size, ctor)
  981.      void *p;
  982.      int maxindex, size;
  983.      void (*ctor)(void *);
  984. {
  985.   int i, nelts = maxindex + 1;
  986.   void *rval;
  987.  
  988.   if (p == 0)
  989.     p = (void *)__builtin_new (nelts * size);
  990.  
  991.   rval = p;
  992.  
  993.   for (i = 0; i < nelts; i++)
  994.     {
  995.       (*ctor) (p);
  996.       p += size;
  997.     }
  998.  
  999.   return rval;
  1000. }
  1001.  
  1002. vfp
  1003. __set_new_handler (handler)
  1004.      vfp handler;
  1005. {
  1006.   vfp prev_handler;
  1007.  
  1008.   prev_handler = __new_handler;
  1009.   if (handler == 0) handler = default_new_handler;
  1010.   __new_handler = handler;
  1011.   return prev_handler;
  1012. }
  1013.  
  1014. vfp
  1015. set_new_handler (handler)
  1016.      vfp handler;
  1017. {
  1018.   return __set_new_handler (handler);
  1019. }
  1020.  
  1021. static void
  1022. default_new_handler ()
  1023. {
  1024.   /* don't use fprintf (stderr, ...) because it may need to call malloc.  */
  1025.   write (2, "default_new_handler: out of memory... aaaiiiiiieeeeeeeeeeeeee!\n", 65);
  1026.   /* don't call exit () because that may call global destructors which
  1027.      may cause a loop.  */
  1028.   _exit (-1);
  1029. }
  1030. #endif
  1031.  
  1032. #ifdef L_builtin_del
  1033. typedef void (*vfp)(void);
  1034.  
  1035. void
  1036. __builtin_delete (ptr)
  1037.      void *ptr;
  1038. {
  1039.   if (ptr)
  1040.     free (ptr);
  1041. }
  1042.  
  1043. void
  1044. __builtin_vec_delete (ptr, maxindex, size, dtor, auto_delete_vec, auto_delete)
  1045.      void *ptr;
  1046.      int maxindex, size;
  1047.      void (*dtor)();
  1048.      int auto_delete;
  1049. {
  1050.   int i, nelts = maxindex + 1;
  1051.   void *p = ptr;
  1052.  
  1053.   ptr += nelts * size;
  1054.  
  1055.   for (i = 0; i < nelts; i++)
  1056.     {
  1057.       ptr -= size;
  1058.       (*dtor) (ptr, auto_delete);
  1059.     }
  1060.  
  1061.   if (auto_delete_vec)
  1062.     __builtin_delete (p);
  1063. }
  1064.  
  1065. #endif
  1066.  
  1067. #ifdef L_shtab
  1068. unsigned int __shtab[] = {
  1069.     0x00000001, 0x00000002, 0x00000004, 0x00000008,
  1070.     0x00000010, 0x00000020, 0x00000040, 0x00000080,
  1071.     0x00000100, 0x00000200, 0x00000400, 0x00000800,
  1072.     0x00001000, 0x00002000, 0x00004000, 0x00008000,
  1073.     0x00010000, 0x00020000, 0x00040000, 0x00080000,
  1074.     0x00100000, 0x00200000, 0x00400000, 0x00800000,
  1075.     0x01000000, 0x02000000, 0x04000000, 0x08000000,
  1076.     0x10000000, 0x20000000, 0x40000000, 0x80000000
  1077.   };
  1078. #endif
  1079.  
  1080. #ifdef L_clear_cache
  1081. /* Clear part of an instruction cache.  */
  1082.  
  1083. #define INSN_CACHE_PLANE_SIZE (INSN_CACHE_SIZE / INSN_CACHE_DEPTH)
  1084.  
  1085. void
  1086. __clear_cache (beg, end)
  1087.      char *beg, *end;
  1088. {
  1089. #ifdef INSN_CACHE_SIZE
  1090.   static char array[INSN_CACHE_SIZE + INSN_CACHE_PLANE_SIZE + INSN_CACHE_LINE_WIDTH];
  1091.   static int initialized = 0;
  1092.   int offset;
  1093.   unsigned int start_addr, end_addr;
  1094.   typedef (*function_ptr) ();
  1095.  
  1096. #if (INSN_CACHE_SIZE / INSN_CACHE_LINE_WIDTH) < 16
  1097.   /* It's cheaper to clear the whole cache.
  1098.      Put in a series of jump instructions so that calling the beginning
  1099.      of the cache will clear the whole thing.  */
  1100.  
  1101.   if (! initialized)
  1102.     {
  1103.       int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
  1104.          & -INSN_CACHE_LINE_WIDTH);
  1105.       int end_ptr = ptr + INSN_CACHE_SIZE;
  1106.  
  1107.       while (ptr < end_ptr)
  1108.     {
  1109.       *(INSTRUCTION_TYPE *)ptr
  1110.         = JUMP_AHEAD_INSTRUCTION + INSN_CACHE_LINE_WIDTH;
  1111.       ptr += INSN_CACHE_LINE_WIDTH;
  1112.     }
  1113.       *(INSTRUCTION_TYPE *)(ptr - INSN_CACHE_LINE_WIDTH) = RETURN_INSTRUCTION;
  1114.  
  1115.       initialized = 1;
  1116.     }
  1117.  
  1118.   /* Call the beginning of the sequence.  */
  1119.   (((function_ptr) (((int) array + INSN_CACHE_LINE_WIDTH - 1)
  1120.             & -INSN_CACHE_LINE_WIDTH))
  1121.    ());
  1122.  
  1123. #else /* Cache is large.  */
  1124.  
  1125.   if (! initialized)
  1126.     {
  1127.       int ptr = (((int) array + INSN_CACHE_LINE_WIDTH - 1)
  1128.          & -INSN_CACHE_LINE_WIDTH);
  1129.  
  1130.       while (ptr < (int) array + sizeof array)
  1131.     {
  1132.       *(INSTRUCTION_TYPE *)ptr = RETURN_INSTRUCTION;
  1133.       ptr += INSN_CACHE_LINE_WIDTH;
  1134.     }
  1135.  
  1136.       initialized = 1;
  1137.     }
  1138.  
  1139.   /* Find the location in array that occupies the same cache line as BEG.  */
  1140.  
  1141.   offset = ((int) beg & -INSN_CACHE_LINE_WIDTH) & (INSN_CACHE_PLANE_SIZE - 1);
  1142.   start_addr = (((int) (array + INSN_CACHE_PLANE_SIZE - 1)
  1143.          & -INSN_CACHE_PLANE_SIZE)
  1144.         + offset);
  1145.  
  1146.   /* Compute the cache alignment of the place to stop clearing.  */
  1147. #if 0  /* This is not needed for gcc's purposes.  */
  1148.   /* If the block to clear is bigger than a cache plane,
  1149.      we clear the entire cache, and OFFSET is already correct.  */ 
  1150.   if (end < beg + INSN_CACHE_PLANE_SIZE)
  1151. #endif
  1152.     offset = (((int) (end + INSN_CACHE_LINE_WIDTH - 1)
  1153.            & -INSN_CACHE_LINE_WIDTH)
  1154.           & (INSN_CACHE_PLANE_SIZE - 1));
  1155.  
  1156. #if INSN_CACHE_DEPTH > 1
  1157.   end_addr = (start_addr & -INSN_CACHE_PLANE_SIZE) + offset;
  1158.   if (end_addr <= start_addr)
  1159.     end_addr += INSN_CACHE_PLANE_SIZE;
  1160.  
  1161.   for (plane = 0; plane < INSN_CACHE_DEPTH; plane++)
  1162.     {
  1163.       int addr = start_addr + plane * INSN_CACHE_PLANE_SIZE;
  1164.       int stop = end_addr + plane * INSN_CACHE_PLANE_SIZE;
  1165.  
  1166.       while (addr != stop)
  1167.     {
  1168.       /* Call the return instruction at ADDR.  */
  1169.       ((function_ptr) addr) ();
  1170.  
  1171.       addr += INSN_CACHE_LINE_WIDTH;
  1172.     }
  1173.     }
  1174. #else /* just one plane */
  1175.   do
  1176.     {
  1177.       /* Call the return instruction at START_ADDR.  */
  1178.       ((function_ptr) start_addr) ();
  1179.  
  1180.       start_addr += INSN_CACHE_LINE_WIDTH;
  1181.     }
  1182.   while ((start_addr % INSN_CACHE_SIZE) != offset);
  1183. #endif /* just one plane */
  1184. #endif /* Cache is large */
  1185. #endif /* Cache exists */
  1186. }
  1187.  
  1188. #endif /* L_clear_cache */
  1189.  
  1190. #ifdef L_trampoline
  1191.  
  1192. /* Jump to a trampoline, loading the static chain address.  */
  1193.  
  1194. /* asm with operands is permitted only within a function.  */
  1195. void
  1196. __transfer_from_trampoline ()
  1197. {
  1198. #ifdef TRANSFER_FROM_TRAMPOLINE 
  1199.   TRANSFER_FROM_TRAMPOLINE 
  1200. #endif
  1201. }
  1202.  
  1203. #ifdef __convex__
  1204.  
  1205. /* Make stack executable so we can call trampolines on stack.
  1206.    This is called from INITIALIZE_TRAMPOLINE in convex.h.  */
  1207.  
  1208. #include <sys/mman.h>
  1209. #include <sys/vmparam.h>
  1210. #include <machine/machparam.h>
  1211.  
  1212. void
  1213. __enable_execute_stack ()
  1214. {
  1215.   int fp;
  1216.   static unsigned lowest = USRSTACK;
  1217.   unsigned current = (unsigned) &fp & -NBPG;
  1218.  
  1219.   if (lowest > current)
  1220.     {
  1221.       unsigned len = lowest - current;
  1222.       mremap (current, &len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
  1223.       lowest = current;
  1224.     }
  1225.  
  1226.   /* Clear instruction cache in case an old trampoline is in it. */
  1227.   asm ("pich");
  1228. }
  1229. #endif /* __convex__ */
  1230. #endif /* L_trampoline */
  1231.  
  1232. #ifdef L__main
  1233.  
  1234. #include "gbl-ctors.h"
  1235.  
  1236. /* Run all the global destructors on exit from the program.  */
  1237.  
  1238. void
  1239. __do_global_dtors ()
  1240. {
  1241.   int nptrs = *(int *)__DTOR_LIST__;
  1242.  
  1243.   /* There are two formats for the table __DTOR_LIST__:
  1244.      GNU ld: first word is number of pointers, followed by that many pointers.
  1245.      ELF/COFF: first word is -1, followed by pointers up to __DTOR_END__.  */
  1246.  
  1247.   if (nptrs != -1)
  1248.     {
  1249.       /* GNU LD format.  */
  1250.       int i;
  1251.       for (i = nptrs - 1; i >= 1; i--)
  1252.     __DTOR_LIST__[i] ();
  1253.     }
  1254.   else
  1255.     {
  1256.       /* COFF, ELF format.  */
  1257.       func_ptr *p;
  1258.       for (p = __DTOR_END__; p > __DTOR_LIST__ + 1; )
  1259.     (*--p) ();
  1260.     }
  1261. }
  1262.  
  1263. #ifndef INIT_SECTION_ASM_OP
  1264. /* Run all the global constructors on entry to the program.  */
  1265.  
  1266. #ifndef ON_EXIT /* DO_GLOBAL_CTORS_BODY uses ON_EXIT */
  1267. #define ON_EXIT(a, b)
  1268. #endif
  1269.  
  1270. void
  1271. __do_global_ctors ()
  1272. {
  1273.   DO_GLOBAL_CTORS_BODY;
  1274. }
  1275. #endif /* defined(INIT_SECTION_ASM_OP) */
  1276.  
  1277. /* Subroutine called automatically by `main'.
  1278.    Compiling a global function named `main'
  1279.    produces an automatic call to this function at the beginning.
  1280.  
  1281.    For many systems, this routine calls __do_global_ctors.
  1282.    For systems which support a .init section we use the .init section
  1283.    to run __do_global_ctors, so we need not do anything here.  */
  1284.  
  1285. void
  1286. __main ()
  1287. {
  1288. #ifndef INIT_SECTION_ASM_OP
  1289.   /* Support recursive calls to `main': run initializers just once.  */
  1290.   static initialized = 0;
  1291.   if (! initialized)
  1292.     {
  1293.       initialized = 1;
  1294.       __do_global_ctors ();
  1295.     }
  1296. #endif /* not INIT_SECTION_ASM_OP */
  1297. }
  1298.  
  1299. #endif /* L__main */
  1300.  
  1301. #ifdef L_exit
  1302.  
  1303. #include "gbl-ctors.h"
  1304.  
  1305. /* Provide default definitions for the lists of constructors and
  1306.    destructors, so that we don't get linker errors.  These symbols are
  1307.    intentionally bss symbols, so that gld and/or collect will provide
  1308.    the right values.  */
  1309.  
  1310. #ifndef INIT_SECTION_ASM_OP
  1311. func_ptr __CTOR_LIST__[1];
  1312. func_ptr __CTOR_END__[1];
  1313. func_ptr __DTOR_LIST__[1];
  1314. func_ptr __DTOR_END__[1];
  1315. #endif /* INIT_SECTION_ASM_OP */
  1316.  
  1317. #ifndef ON_EXIT
  1318.  
  1319. /* If we have no known way of registering our own __do_global_dtors
  1320.    routine so that it will be invoked at program exit time, then we
  1321.    have to define our own exit routine which will get this to happen.  */
  1322.  
  1323. extern void __do_global_dtors ();
  1324. extern void _cleanup ();
  1325. extern void _exit ();
  1326.  
  1327. void 
  1328. exit (status)
  1329.      int status;
  1330. {
  1331.   __do_global_dtors ();
  1332. #ifdef EXIT_BODY
  1333.   EXIT_BODY;
  1334. #else
  1335.   _cleanup ();
  1336. #endif
  1337.   _exit (status);
  1338. }
  1339.  
  1340. #else
  1341. int _exit_dummy_decl = 0;    /* prevent compiler & linker warnings */
  1342. #endif
  1343.  
  1344. #endif /* L_exit */
  1345.