home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / mntlib16.lzh / MNTLIB16 / GNULIB2.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  23KB  |  1,045 lines

  1. /* WARNING: compile this in 32 bit int mode even for short library */
  2. #include <string.h>
  3. #include <stddef.h>
  4. #include <stdlib.h>
  5.  
  6. #ifdef __DEF_ALL__    /* this def'ed when making on the ST */
  7.  
  8. #define L_adddi3
  9. #define L_subdi3
  10. #define L_muldi3
  11. #define L_divdi3
  12. #define L_moddi3
  13. #define L_udivdi3
  14. #define L_umoddi3
  15. #define L_negdi2
  16. #define L_anddi3
  17. #define L_iordi3
  18. #define L_xordi3
  19. #define L_lshrdi3
  20. #define L_lshldi3
  21. #define L_ashldi3
  22. #define L_ashrdi3
  23. #define L_one_cmpldi2
  24. #define L_bdiv
  25. #define L_cmpdi2
  26. #define L_ucmpdi2
  27. #define L_fixunsdfdi
  28. #define L_fixdfdi
  29. #define L_floatdidf
  30.  
  31. #endif /* __DEF_ALL__ */
  32.  
  33. /* More subroutines needed by GCC output code on some machines.  */
  34. /* Compile this one with gcc.  */
  35.  
  36. #if 0
  37. #include "config.h"    /* dont drag this in, just define relevant
  38.                stuff from xm/tm-atari.h & xm/tm-m68k.h here */
  39. #else
  40.  
  41. /* #defines that need visibility everywhere.  */
  42. #define FALSE 0
  43. #define TRUE 1
  44.  
  45. /* This describes the machine the compiler is hosted on.  */
  46. #define HOST_BITS_PER_CHAR 8
  47. #define HOST_BITS_PER_SHORT 16
  48. #define HOST_BITS_PER_INT 32
  49. #define HOST_BITS_PER_LONG 32
  50.  
  51. #ifdef __GNUC__
  52. #define alloca __builtin_alloca
  53. #endif
  54.  
  55. /* Define this if most significant bit is lowest numbered
  56.    in instructions that operate on numbered bit-fields.
  57.    This is true for 68020 insns such as bfins and bfexts.
  58.    We make it true always by avoiding using the single-bit insns
  59.    except in special cases with constant bit numbers.  */
  60. #define BITS_BIG_ENDIAN
  61.  
  62. /* Define this if most significant byte of a word is the lowest numbered.  */
  63. /* That is true on the 68000.  */
  64. #define BYTES_BIG_ENDIAN
  65.  
  66. /* Define this if most significant word of a multiword number is numbered.  */
  67. /* For 68000 we can decide arbitrarily
  68.    since there are no machine instructions for them.  */
  69. /* #define WORDS_BIG_ENDIAN */
  70.  
  71. /* number of bits in an addressible storage unit */
  72. #define BITS_PER_UNIT 8
  73.  
  74. /* Width in bits of a "word", which is the contents of a machine register.
  75.    Note that this is not necessarily the width of data type `int';
  76.    if using 16-bit ints on a 68000, this would still be 32.
  77.    But on a machine with 16-bit registers, this would be 16.  */
  78. #define BITS_PER_WORD 32
  79.  
  80. /* Width of a word, in units (bytes).  */
  81. #define UNITS_PER_WORD 4
  82.  
  83. /* Width in bits of a pointer.
  84.    See also the macro `Pmode' defined below.  */
  85. #define POINTER_SIZE 32
  86.  
  87. /* Allocation boundary (in *bits*) for storing pointers in memory.  */
  88. #define POINTER_BOUNDARY 16
  89.  
  90. /* Allocation boundary (in *bits*) for storing arguments in argument list.  */
  91. #define PARM_BOUNDARY (TARGET_SHORT ? 16 : 32)
  92.  
  93. /* Boundary (in *bits*) on which stack pointer should be aligned.  */
  94. #define STACK_BOUNDARY 16
  95.  
  96. /* Allocation boundary (in *bits*) for the code of a function.  */
  97. #define FUNCTION_BOUNDARY 16
  98.  
  99. /* Alignment of field after `int : 0' in a structure.  */
  100. #define EMPTY_FIELD_BOUNDARY 16
  101.  
  102. /* No data type wants to be aligned rounder than this.  */
  103. #define BIGGEST_ALIGNMENT 16
  104.  
  105. /* Define this if move instructions will actually fail to work
  106.    when given unaligned data.  */
  107. #define STRICT_ALIGNMENT
  108.  
  109. /* Define number of bits in most basic integer type.
  110.    (If undefined, default is BITS_PER_WORD).  */
  111. #ifdef __MSHORT__
  112. #define INT_TYPE_SIZE 16
  113. #else
  114. #define INT_TYPE_SIZE 32
  115. #endif
  116.  
  117. #endif
  118.  
  119. #ifndef SItype
  120. #define SItype long int
  121. #endif
  122.  
  123. /* long long ints are pairs of long ints in the order determined by
  124.    WORDS_BIG_ENDIAN.  */
  125.  
  126. #ifdef WORDS_BIG_ENDIAN
  127.   struct longlong {long high, low;};
  128. #else
  129.   struct longlong {long low, high;};
  130. #endif
  131.  
  132. /* We need this union to unpack/pack longlongs, since we don't have
  133.    any arithmetic yet.  Incoming long long parameters are stored
  134.    into the `ll' field, and the unpacked result is read from the struct
  135.    longlong.  */
  136.  
  137. typedef union
  138. {
  139.   struct longlong s;
  140.   long long ll;
  141.   SItype i[2];
  142.   unsigned SItype ui[2];
  143. } long_long;
  144.  
  145. /* Internally, long long ints are strings of unsigned shorts in the
  146.    order determined by BYTES_BIG_ENDIAN.  */
  147.  
  148. #define B 0x10000
  149. #define low16 (B - 1)
  150.  
  151. #ifdef BYTES_BIG_ENDIAN
  152.  
  153. /* Note that HIGH and LOW do not describe the order
  154.    of words in a long long.  They describe the order of words
  155.    in vectors ordered according to the byte order.  */
  156.  
  157. #define HIGH 0
  158. #define LOW 1
  159.  
  160. #define big_end(n)    0 
  161. #define little_end(n)    ((n) - 1)
  162. #define next_msd(i)    ((i) - 1)
  163. #define next_lsd(i)    ((i) + 1)
  164. #define is_not_msd(i,n)    ((i) >= 0)
  165. #define is_not_lsd(i,n)    ((i) < (n))
  166.  
  167. #else
  168.  
  169. #define LOW 0
  170. #define HIGH 1
  171.  
  172. #define big_end(n)    ((n) - 1)
  173. #define little_end(n)    0 
  174. #define next_msd(i)    ((i) + 1)
  175. #define next_lsd(i)    ((i) - 1)
  176. #define is_not_msd(i,n)    ((i) < (n))
  177. #define is_not_lsd(i,n)    ((i) >= 0)
  178.  
  179. #endif
  180.  
  181. /* These algorithms are all straight out of Knuth, vol. 2, sec. 4.3.1. */
  182. #if __STDC__
  183. #define P_(x) x
  184. #else
  185. #define P_(x) ()
  186. #endif
  187.  
  188. long long __adddi3 P_((long long u, long long v));
  189. #ifdef L_adddi3
  190. static int badd P_((unsigned short *a, unsigned short *b, unsigned short *c, size_t n));
  191. #endif
  192. long long __anddi3 P_((long long u, long long v));
  193. long long __iordi3 P_((long long u, long long v));
  194. long long __xordi3 P_((long long u, long long v));
  195. long long __one_cmpldi2 P_((long long u));
  196. long long __lshldi3 P_((long long u, long int b1));
  197. long long __lshrdi3 P_((long long u, long int b1));
  198. long long __ashldi3 P_((long long u, long int b1));
  199. long long __ashrdi3 P_((long long u, long int b1));
  200. long long __subdi3 P_((long long u, long long v));
  201. #ifdef L_subdi3
  202. static int bsub P_((unsigned short *a, unsigned short *b, unsigned short *c, size_t n));
  203. #endif
  204. long long __muldi3 P_((long long u, long long v));
  205. #ifdef L_muldi3
  206. static void bmul P_((unsigned short *a, unsigned short *b, unsigned short *c, size_t m, size_t n));
  207. #endif
  208. long long __divdi3 P_((long long u, long long v));
  209. long long __moddi3 P_((long long u, long long v));
  210. long long __udivdi3 P_((long long u, long long v));
  211. long long __umoddi3 P_((long long u, long long v));
  212. long long __negdi2 P_((long long u));
  213. #ifdef L_negdi2
  214. static int bneg P_((unsigned short *a, unsigned short *b, size_t n));
  215. #endif
  216. void __bdiv P_((unsigned short *a, unsigned short *b, unsigned short *q, unsigned short *r, size_t m, size_t n));
  217. #ifdef L_bdiv
  218. static int bshift P_((unsigned short *u, int k, unsigned short *w, unsigned int carry_in, int n));
  219. #endif
  220. SItype __cmpdi2 P_((long long a, long long b));
  221. SItype __ucmpdi2 P_((long long a, long long b));
  222. long long __fixunsdfdi P_((double a));
  223. long long __fixdfdi P_((double a));
  224. double __floatdidf P_((long long u));
  225. #if 0
  226. /* gcc -Wall doesn't like seeing builtin functions redeclared */
  227. int __builtin_saveregs P_((void));
  228. #endif
  229.  
  230. #undef P_
  231.  
  232.  
  233. #ifdef L_adddi3
  234. long long 
  235. __adddi3 (u, v)
  236.      long long u, v;
  237. {
  238.   long a[2], b[2], c[2];
  239.   long_long w;
  240.   long_long uu, vv;
  241.  
  242.   uu.ll = u;
  243.   vv.ll = v;
  244.  
  245.   a[HIGH] = uu.s.high;
  246.   a[LOW] = uu.s.low;
  247.   b[HIGH] = vv.s.high;
  248.   b[LOW] = vv.s.low;
  249.  
  250.   badd ((unsigned short *)a, (unsigned short *)b, (unsigned short *)c, sizeof c);
  251.  
  252.   w.s.high = c[HIGH];
  253.   w.s.low = c[LOW];
  254.   return w.ll;
  255. }
  256.  
  257. static int 
  258. badd (a, b, c, n)
  259.      unsigned short *a, *b, *c;
  260.      size_t n;
  261. {
  262.   unsigned long acc;
  263.   int i;
  264.  
  265.   n /= sizeof *c;
  266.  
  267.   acc = 0;
  268.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  269.     {
  270.       /* Widen before adding to avoid loss of high bits.  */
  271.       acc += (unsigned long) a[i] + b[i];
  272.       c[i] = acc & low16;
  273.       acc = acc >> 16;
  274.     }
  275.   return acc;
  276. }
  277. #endif
  278.  
  279. #ifdef L_anddi3
  280. long long 
  281. __anddi3 (u, v)
  282.      long long u, v;
  283. {
  284.   long_long w;
  285.   long_long uu, vv;
  286.  
  287.   uu.ll = u;
  288.   vv.ll = v;
  289.  
  290.   w.s.high = uu.s.high & vv.s.high;
  291.   w.s.low = uu.s.low & vv.s.low;
  292.  
  293.   return w.ll;
  294. }
  295. #endif
  296.  
  297. #ifdef L_iordi3
  298. long long 
  299. __iordi3 (u, v)
  300.      long long u, v;
  301. {
  302.   long_long w;
  303.   long_long uu, vv;
  304.  
  305.   uu.ll = u;
  306.   vv.ll = v;
  307.  
  308.   w.s.high = uu.s.high | vv.s.high;
  309.   w.s.low = uu.s.low | vv.s.low;
  310.  
  311.   return w.ll;
  312. }
  313. #endif
  314.  
  315. #ifdef L_xordi3
  316. long long 
  317. __xordi3 (u, v)
  318.      long long u, v;
  319. {
  320.   long_long w;
  321.   long_long uu, vv;
  322.  
  323.   uu.ll = u;
  324.   vv.ll = v;
  325.  
  326.   w.s.high = uu.s.high ^ vv.s.high;
  327.   w.s.low = uu.s.low ^ vv.s.low;
  328.  
  329.   return w.ll;
  330. }
  331. #endif
  332.  
  333. #ifdef L_one_cmpldi2
  334. long long
  335. __one_cmpldi2 (u)
  336.      long long u;
  337. {
  338.   long_long w;
  339.   long_long uu;
  340.  
  341.   uu.ll = u;
  342.  
  343.   w.s.high = ~uu.s.high;
  344.   w.s.low = ~uu.s.low;
  345.  
  346.   return w.ll;
  347. }
  348. #endif
  349.  
  350. #ifdef L_lshldi3
  351. long long
  352. __lshldi3 (u, b1)
  353.      long long u;
  354.      long int b1;
  355. {
  356.   long_long w;
  357.   unsigned long carries;
  358.   int bm;
  359.   long_long uu;
  360.   int b = b1;
  361.   
  362.   if (b == 0)
  363.     return u;
  364.  
  365.   uu.ll = u;
  366.  
  367.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  368.   if (bm <= 0)
  369.     {
  370.       w.s.low = 0;
  371.       w.s.high = (unsigned long)uu.s.low << -bm;
  372.     }
  373.   else
  374.     {
  375.       carries = (unsigned long)uu.s.low >> bm;
  376.       w.s.low = (unsigned long)uu.s.low << b;
  377.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  378.     }
  379.  
  380.   return w.ll;
  381. }
  382. #endif
  383.  
  384. #ifdef L_lshrdi3
  385. long long
  386. __lshrdi3 (u, b1)
  387.      long long u;
  388.      long int b1;
  389. {
  390.   long_long w;
  391.   unsigned long carries;
  392.   int bm;
  393.   long_long uu;
  394.   int b = b1;
  395.   
  396.   if (b == 0)
  397.     return u;
  398.  
  399.   uu.ll = u;
  400.  
  401.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  402.   if (bm <= 0)
  403.     {
  404.       w.s.high = 0;
  405.       w.s.low = (unsigned long)uu.s.high >> -bm;
  406.     }
  407.   else
  408.     {
  409.       carries = (unsigned long)uu.s.high << bm;
  410.       w.s.high = (unsigned long)uu.s.high >> b;
  411.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  412.     }
  413.  
  414.   return w.ll;
  415. }
  416. #endif
  417.  
  418. #ifdef L_ashldi3
  419. long long
  420. __ashldi3 (u, b1)
  421.      long long u;
  422.      long int b1;
  423. {
  424.   long_long w;
  425.   unsigned long carries;
  426.   int bm;
  427.   long_long uu;
  428.   int b = b1;
  429.   
  430.   if (b == 0)
  431.     return u;
  432.  
  433.   uu.ll = u;
  434.  
  435.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  436.   if (bm <= 0)
  437.     {
  438.       w.s.low = 0;
  439.       w.s.high = (unsigned long)uu.s.low << -bm;
  440.     }
  441.   else
  442.     {
  443.       carries = (unsigned long)uu.s.low >> bm;
  444.       w.s.low = (unsigned long)uu.s.low << b;
  445.       w.s.high = ((unsigned long)uu.s.high << b) | carries;
  446.     }
  447.  
  448.   return w.ll;
  449. }
  450. #endif
  451.  
  452. #ifdef L_ashrdi3
  453. long long
  454. __ashrdi3 (u, b1)
  455.      long long u;
  456.      long int b1;
  457. {
  458.   long_long w;
  459.   unsigned long carries;
  460.   int bm;
  461.   long_long uu;
  462.   int b = b1;
  463.   
  464.   if (b == 0)
  465.     return u;
  466.  
  467.   uu.ll = u;
  468.  
  469.   bm = (sizeof (int) * BITS_PER_UNIT) - b;
  470.   if (bm <= 0)
  471.     {
  472.       w.s.high = uu.s.high >> 31; /* just to make w.s.high 1..1 or 0..0 */
  473.       w.s.low = uu.s.high >> -bm;
  474.     }
  475.   else
  476.     {
  477.       carries = (unsigned long)uu.s.high << bm;
  478.       w.s.high = uu.s.high >> b;
  479.       w.s.low = ((unsigned long)uu.s.low >> b) | carries;
  480.     }
  481.  
  482.   return w.ll;
  483. }
  484. #endif
  485.  
  486. #ifdef L_subdi3
  487. long long 
  488. __subdi3 (u, v)
  489.      long long u, v;
  490. {
  491.   long a[2], b[2], c[2];
  492.   long_long w;
  493.   long_long uu, vv;
  494.  
  495.   uu.ll = u;
  496.   vv.ll = v;
  497.  
  498.   a[HIGH] = uu.s.high;
  499.   a[LOW] = uu.s.low;
  500.   b[HIGH] = vv.s.high;
  501.   b[LOW] = vv.s.low;
  502.  
  503.   bsub ((unsigned short *)a, (unsigned short *)b, (unsigned short *)c, sizeof c);
  504.  
  505.   w.s.high = c[HIGH];
  506.   w.s.low = c[LOW];
  507.   return w.ll;
  508. }
  509.  
  510. static int 
  511. bsub (a, b, c, n)
  512.      unsigned short *a, *b, *c;
  513.      size_t n;
  514. {
  515.   signed long acc;
  516.   int i;
  517.  
  518.   n /= sizeof *c;
  519.  
  520.   acc = 0;
  521.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  522.     {
  523.       /* Widen before subtracting to avoid loss of high bits.  */
  524.       acc += (long) a[i] - b[i];
  525.       c[i] = acc & low16;
  526.       acc = acc >> 16;
  527.     }
  528.   return acc;
  529. }
  530. #endif
  531.  
  532. #ifdef L_muldi3
  533. long long 
  534. __muldi3 (u, v)
  535.      long long u, v;
  536. {
  537.   long a[2], b[2], c[2][2];
  538.   long_long w;
  539.   long_long uu, vv;
  540.  
  541.   uu.ll = u;
  542.   vv.ll = v;
  543.  
  544.   a[HIGH] = uu.s.high;
  545.   a[LOW] = uu.s.low;
  546.   b[HIGH] = vv.s.high;
  547.   b[LOW] = vv.s.low;
  548.  
  549.   bmul ((unsigned short *)a, (unsigned short *)b, (unsigned short *)c, sizeof a, sizeof b);
  550.  
  551.   w.s.high = c[LOW][HIGH];
  552.   w.s.low = c[LOW][LOW];
  553.   return w.ll;
  554. }
  555.  
  556. static void 
  557. bmul (a, b, c, m, n)
  558.     unsigned short *a, *b, *c;
  559.     size_t m, n;
  560. {
  561.   int i, j;
  562.   unsigned long acc;
  563.  
  564.   (void)bzero (c, m + n);
  565.  
  566.   m /= sizeof *a;
  567.   n /= sizeof *b;
  568.  
  569.   for (j = little_end (n); is_not_msd (j, n); j = next_msd (j))
  570.     {
  571.       unsigned short *c1 = c + j + little_end (2);
  572.       acc = 0;
  573.       for (i = little_end (m); is_not_msd (i, m); i = next_msd (i))
  574.     {
  575.       /* Widen before arithmetic to avoid loss of high bits.  */
  576.       acc += (unsigned long) a[i] * b[j] + c1[i];
  577.       c1[i] = acc & low16;
  578.       acc = acc >> 16;
  579.     }
  580.       c1[i] = acc;
  581.     }
  582. }
  583. #endif
  584.  
  585. #ifdef L_divdi3
  586. long long
  587. __divdi3 (u, v)
  588.      long long u, v;
  589. {
  590.   if (u < 0)
  591.     if (v < 0)
  592.       return (unsigned long long) -u / (unsigned long long) -v;
  593.     else
  594.       return - ((unsigned long long) -u / (unsigned long long) v);
  595.   else
  596.     if (v < 0)
  597.       return - ((unsigned long long) u / (unsigned long long) -v);
  598.     else
  599.       return (unsigned long long) u / (unsigned long long) v;
  600. }
  601. #endif
  602.  
  603. #ifdef L_moddi3
  604. long long
  605. __moddi3 (u, v)
  606.      long long u, v;
  607. {
  608.   if (u < 0)
  609.     if (v < 0)
  610.       return - ((unsigned long long) -u % (unsigned long long) -v);
  611.     else
  612.       return - ((unsigned long long) -u % (unsigned long long) v);
  613.   else
  614.     if (v < 0)
  615.       return (unsigned long long) u % (unsigned long long) -v;
  616.     else
  617.       return (unsigned long long) u % (unsigned long long) v;
  618. }
  619. #endif
  620.  
  621. #ifdef L_udivdi3
  622. long long 
  623. __udivdi3 (u, v)
  624.      long long u, v;
  625. {
  626.   unsigned long a[2][2], b[2], q[2], r[2];
  627.   long_long w;
  628.   long_long uu, vv;
  629.  
  630.   uu.ll = u;
  631.   vv.ll = v;
  632.  
  633.   a[HIGH][HIGH] = 0;
  634.   a[HIGH][LOW] = 0;
  635.   a[LOW][HIGH] = uu.s.high;
  636.   a[LOW][LOW] = uu.s.low;
  637.   b[HIGH] = vv.s.high;
  638.   b[LOW] = vv.s.low;
  639.  
  640.   __bdiv ((unsigned short *)a, (unsigned short *)b, (unsigned short *)q,
  641.       (unsigned short *)r, sizeof a, sizeof b);
  642.  
  643.   w.s.high = q[HIGH];
  644.   w.s.low = q[LOW];
  645.   return w.ll;
  646. }
  647. #endif
  648.  
  649. #ifdef L_umoddi3
  650. long long 
  651. __umoddi3 (u, v)
  652.      long long u, v;
  653. {
  654.   unsigned long a[2][2], b[2], q[2], r[2];
  655.   long_long w;
  656.   long_long uu, vv;
  657.  
  658.   uu.ll = u;
  659.   vv.ll = v;
  660.  
  661.   a[HIGH][HIGH] = 0;
  662.   a[HIGH][LOW] = 0;
  663.   a[LOW][HIGH] = uu.s.high;
  664.   a[LOW][LOW] = uu.s.low;
  665.   b[HIGH] = vv.s.high;
  666.   b[LOW] = vv.s.low;
  667.  
  668.   __bdiv ((unsigned short *)a, (unsigned short *)b, (unsigned short *)q,
  669.       (unsigned short *)r, sizeof a, sizeof b);
  670.  
  671.   w.s.high = r[HIGH];
  672.   w.s.low = r[LOW];
  673.   return w.ll;
  674. }
  675. #endif
  676.  
  677. #ifdef L_negdi2
  678. long long 
  679. __negdi2 (u)
  680.      long long u;
  681. {
  682.   unsigned long a[2], b[2];
  683.   long_long w;
  684.   long_long uu;
  685.  
  686.   uu.ll = u;
  687.  
  688.   a[HIGH] = uu.s.high;
  689.   a[LOW] = uu.s.low;
  690.  
  691.   bneg ((unsigned short *)a, (unsigned short *)b, sizeof b);
  692.  
  693.   w.s.high = b[HIGH];
  694.   w.s.low = b[LOW];
  695.   return w.ll;
  696. }
  697.  
  698. static int
  699. bneg (a, b, n)
  700.      unsigned short *a, *b;
  701.      size_t n;
  702. {
  703.   signed long acc;
  704.   int i;
  705.  
  706.   n /= sizeof (short);
  707.  
  708.   acc = 0;
  709.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  710.     {
  711.       acc -= a[i];
  712.       b[i] = acc & low16;
  713.       acc = acc >> 16;
  714.     }
  715.   return acc;
  716. }
  717. #endif
  718.  
  719. /* Divide a by b, producing quotient q and remainder r.
  720.  
  721.        sizeof a is m
  722.        sizeof b is n
  723.        sizeof q is m - n
  724.        sizeof r is n
  725.  
  726.    The quotient must fit in m - n bytes, i.e., the most significant
  727.    n digits of a must be less than b, and m must be greater than n.  */
  728.  
  729. /* The name of this used to be __div_internal,
  730.    but that is too long for SYSV.  */
  731.  
  732. #ifdef L_bdiv
  733. void 
  734. __bdiv (a, b, q, r, m, n)
  735.      unsigned short *a, *b, *q, *r;
  736.      size_t m, n;
  737. {
  738.   unsigned long qhat, rhat;
  739.   unsigned long acc;
  740.   unsigned short *u = (unsigned short *) alloca (m);
  741.   unsigned short *v = (unsigned short *) alloca (n);
  742.   unsigned short *u0, *u1, *u2;
  743.   unsigned short *v0;
  744.   int d, qn;
  745.   int i, j;
  746.  
  747.   m /= sizeof *a;
  748.   n /= sizeof *b;
  749.   qn = m - n;
  750.  
  751.   /* Remove leading zero digits from divisor, and the same number of
  752.      digits (which must be zero) from dividend.  */
  753.  
  754.   while (b[big_end (n)] == 0)
  755.     {
  756.       r[big_end (n)] = 0;
  757.  
  758.       a += little_end (2);
  759.       b += little_end (2);
  760.       r += little_end (2);
  761.       m--;
  762.       n--;
  763.  
  764.       /* Check for zero divisor.  */
  765.       if (n == 0)
  766.     abort ();
  767.     }
  768.       
  769.   /* If divisor is a single digit, do short division.  */
  770.  
  771.   if (n == 1)
  772.     {
  773.       acc = a[big_end (m)];
  774.       a += little_end (2);
  775.       for (j = big_end (qn); is_not_lsd (j, qn); j = next_lsd (j))
  776.     {
  777.       acc = (acc << 16) | a[j];
  778.       q[j] = acc / *b;
  779.       acc = acc % *b;
  780.     }
  781.       *r = acc;
  782.       return;
  783.     }
  784.  
  785.   /* No such luck, must do long division. Shift divisor and dividend
  786.      left until the high bit of the divisor is 1.  */
  787.  
  788.   for (d = 0; d < 16; d++)
  789.     if (b[big_end (n)] & (1 << (16 - 1 - d)))
  790.       break;
  791.  
  792.   bshift (a, d, u, 0, m);
  793.   bshift (b, d, v, 0, n);
  794.  
  795.   /* Get pointers to the high dividend and divisor digits.  */
  796.  
  797.   u0 = u + big_end (m) - big_end (qn);
  798.   u1 = next_lsd (u0);
  799.   u2 = next_lsd (u1);
  800.   u += little_end (2);
  801.  
  802.   v0 = v + big_end (n);
  803.  
  804.   /* Main loop: find a quotient digit, multiply it by the divisor,
  805.      and subtract that from the dividend, shifted over the right amount. */
  806.  
  807.   for (j = big_end (qn); is_not_lsd (j, qn); j = next_lsd (j))
  808.     {
  809.       /* Quotient digit initial guess: high 2 dividend digits over high
  810.      divisor digit.  */
  811.  
  812.       if (u0[j] == *v0)
  813.     {
  814.       qhat = B - 1;
  815.       rhat = (unsigned long) *v0 + u1[j];
  816.     }
  817.       else
  818.     {
  819.       unsigned long numerator = ((unsigned long) u0[j] << 16) | u1[j];
  820.       qhat = numerator / *v0;
  821.       rhat = numerator % *v0;
  822.     }
  823.  
  824.       /* Now get the quotient right for high 3 dividend digits over
  825.      high 2 divisor digits.  */
  826.  
  827.       while (rhat < B && qhat * *next_lsd (v0) > ((rhat << 16) | u2[j]))
  828.     {
  829.       qhat -= 1;
  830.       rhat += *v0;
  831.     }
  832.         
  833.       /* Multiply quotient by divisor, subtract from dividend.  */
  834.  
  835.       acc = 0;
  836.       for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  837.     {
  838.       acc += (unsigned long) (u + j)[i] - v[i] * qhat;
  839.       (u + j)[i] = acc & low16;
  840.       if (acc < B)
  841.         acc = 0;
  842.       else
  843.         acc = (acc >> 16) | -B;
  844.     }
  845.  
  846.       q[j] = qhat;
  847.  
  848.       /* Quotient may have been too high by 1.  If dividend went negative,
  849.      decrement the quotient by 1 and add the divisor back.  */
  850.  
  851.       if ((signed long) (acc + u0[j]) < 0)
  852.     {
  853.       q[j] -= 1;
  854.       acc = 0;
  855.       for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  856.         {
  857.           acc += (unsigned long) (u + j)[i] + v[i];
  858.           (u + j)[i] = acc & low16;
  859.           acc = acc >> 16;
  860.         }
  861.     }
  862.     }
  863.  
  864.   /* Now the remainder is what's left of the dividend, shifted right
  865.      by the amount of the normalizing left shift at the top.  */
  866.  
  867.   r[big_end (n)] = bshift (u + 1 + little_end (j - 1),
  868.                16 - d,
  869.                r + little_end (2),
  870.                u[little_end (m - 1)] >> d,
  871.                n - 1);
  872. }
  873.  
  874. /* Left shift U by K giving W; fill the introduced low-order bits with
  875.    CARRY_IN.  Length of U and W is N.  Return carry out.  K must be
  876.    in 0 .. 16.  */
  877.  
  878. static int
  879. bshift (u, k, w, carry_in, n)
  880.      unsigned short *u, *w;
  881.      unsigned int carry_in;
  882.      int k, n;
  883. {
  884.   unsigned long acc;
  885.   int i;
  886.  
  887.   if (k == 0)
  888.     {
  889.       bcopy (u, w, n * sizeof *u);
  890.       return 0;
  891.     }
  892.  
  893.   acc = carry_in;
  894.   for (i = little_end (n); is_not_msd (i, n); i = next_msd (i))
  895.     {
  896.       acc |= (unsigned long) u[i] << k;
  897.       w[i] = acc & low16;
  898.       acc = acc >> 16;
  899.     }
  900.   return acc;
  901. }
  902. #endif
  903.  
  904. #ifdef L_cmpdi2
  905. SItype
  906. __cmpdi2 (a, b)
  907.      long long a, b;
  908. {
  909.   long_long au, bu;
  910.  
  911.   au.ll = a, bu.ll = b;
  912.  
  913.   if (au.s.high < bu.s.high)
  914.     return 0;
  915.   else if (au.s.high > bu.s.high)
  916.     return 2;
  917.   if ((unsigned) au.s.low < (unsigned) bu.s.low)
  918.     return 0;
  919.   else if ((unsigned) au.s.low > (unsigned) bu.s.low)
  920.     return 2;
  921.   return 1;
  922. }
  923. #endif
  924.  
  925. #ifdef L_ucmpdi2
  926. SItype
  927. __ucmpdi2 (a, b)
  928.      long long a, b;
  929. {
  930.   long_long au, bu;
  931.  
  932.   au.ll = a, bu.ll = b;
  933.  
  934.   if ((unsigned) au.s.high < (unsigned) bu.s.high)
  935.     return 0;
  936.   else if ((unsigned) au.s.high > (unsigned) bu.s.high)
  937.     return 2;
  938.   if ((unsigned) au.s.low < (unsigned) bu.s.low)
  939.     return 0;
  940.   else if ((unsigned) au.s.low > (unsigned) bu.s.low)
  941.     return 2;
  942.   return 1;
  943. }
  944. #endif
  945.  
  946. #ifdef L_fixunsdfdi
  947. #define HIGH_WORD_COEFF (((long long) 1) << BITS_PER_WORD)
  948.  
  949. long long
  950. __fixunsdfdi (a)
  951.      double a;
  952. {
  953.   double b;
  954.   unsigned long long v;
  955.  
  956.   if (a < 0)
  957.     return 0;
  958.  
  959.   /* Compute high word of result, as a flonum.  */
  960.   b = (a / HIGH_WORD_COEFF);
  961.   /* Convert that to fixed (but not to long long!),
  962.      and shift it into the high word.  */
  963.   v = (unsigned long int) b;
  964.   v <<= BITS_PER_WORD;
  965.   /* Remove high part from the double, leaving the low part as flonum.  */
  966.   a -= (double)v;
  967.   /* Convert that to fixed (but not to long long!) and add it in.
  968.      Sometimes A comes out negative.  This is significant, since
  969.      A has more bits than a long int does.  */
  970.   if (a < 0)
  971.     v -= (unsigned long int) (- a);
  972.   else
  973.     v += (unsigned long int) a;
  974.   return v;
  975. }
  976. #endif
  977.  
  978. #ifdef L_fixdfdi
  979. long long
  980. __fixdfdi (a)
  981.      double a;
  982. {
  983.   long long __fixunsdfdi (double a);
  984.  
  985.   if (a < 0)
  986.     return - __fixunsdfdi (-a);
  987.   return __fixunsdfdi (a);
  988. }
  989. #endif
  990.  
  991. #ifdef L_floatdidf
  992. #define HIGH_HALFWORD_COEFF (((long long) 1) << (BITS_PER_WORD / 2))
  993. #define HIGH_WORD_COEFF (((long long) 1) << BITS_PER_WORD)
  994.  
  995. double
  996. __floatdidf (u)
  997.      long long u;
  998. {
  999.   double d;
  1000.   int negate = 0;
  1001.  
  1002.   if (u < 0)  /* was : if (d < 0)     */
  1003.     u = -u, negate = 1;
  1004.  
  1005.   d = (unsigned int) (u >> BITS_PER_WORD);
  1006.   d *= HIGH_HALFWORD_COEFF;
  1007.   d *= HIGH_HALFWORD_COEFF;
  1008.   d += (unsigned int) (u & (HIGH_WORD_COEFF - 1));
  1009.  
  1010.   return (negate ? -d : d);
  1011. }
  1012. #endif
  1013.  
  1014. #ifdef L_varargs
  1015. #ifdef sparc
  1016.     asm (".global ___builtin_saveregs");
  1017.     asm ("___builtin_saveregs:");
  1018.     asm ("st %i0,[%fp+68]");
  1019.     asm ("st %i1,[%fp+72]");
  1020.     asm ("st %i2,[%fp+76]");
  1021.     asm ("st %i3,[%fp+80]");
  1022.     asm ("st %i4,[%fp+84]");
  1023.     asm ("retl");
  1024.     asm ("st %i5,[%fp+88]");
  1025. #else /* not sparc */
  1026. #if defined(MIPSEL) | defined(R3000) | defined(R2000) | defined(mips)
  1027.  
  1028.   asm ("    .ent __builtin_saveregs");
  1029.   asm ("    .globl __builtin_saveregs");
  1030.   asm ("__builtin_saveregs:");
  1031.   asm ("    sw    $4,0($30)");
  1032.   asm ("    sw    $5,4($30)");
  1033.   asm ("    sw    $6,8($30)");
  1034.   asm ("    sw    $7,12($30)");
  1035.   asm ("    j    $31");
  1036.   asm ("    .end __builtin_saveregs");
  1037. #else /* not mips */
  1038. __builtin_saveregs ()
  1039. {
  1040.   abort ();
  1041. }
  1042. #endif /* not mips */
  1043. #endif /* not sparc */
  1044. #endif
  1045.