home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / longlong.h < prev    next >
Text File  |  1991-06-03  |  14KB  |  427 lines

  1. /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4.    This definition file is free software; you can redistribute it
  5.    and/or modify it under the terms of the GNU General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2, or (at your option) any later version.
  8.  
  9.    This definition file is distributed in the hope that it will be
  10.    useful, but WITHOUT ANY WARRANTY; without even the implied
  11.    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.    See the GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #define B (1L << (8 * sizeof (unsigned long) / 2))
  19. #define lowpart(t) ((unsigned long) (t) % B)
  20. #define highpart(t) ((unsigned long) (t) / B)
  21.  
  22. /* Define auxilliary asm macros.
  23.  
  24.    1) umul_ppmm(high_prod, low_prod, multipler, multiplicand)
  25.    multiplies two unsigned long integers MULTIPLER and MULTIPLICAND,
  26.    and generates a two unsigned word product in HIGH_PROD and
  27.    LOW_PROD.
  28.  
  29.    2) __umulsidi3(a,b) multiplies two unsigned long integers A and B,
  30.    and returns a long long product.
  31.  
  32.    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  33.    denominator) divides a two-word unsigned integer, composed by the
  34.    integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and
  35.    places the quotient in QUOTIENT and the remainder in REMAINDER.
  36.    The most significant bit of DENOMINATOR must be 1, and
  37.    HIGH_NUMERATOR must be less than DENOMINATOR for correct operation.
  38.  
  39.    4) count_leading_zeros(count,x) counts the number of zero-bits from
  40.    the msb to the first non-zero bit.  This is the number of steps X
  41.    needs to be shifted left to set the msb.
  42.  
  43.    5) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  44.    high_addend_2, low_addend_2) adds two two-word unsigned integers,
  45.    composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and
  46.    LOW_ADDEND_2 respectively.  The result is placed in HIGH_SUM and
  47.    LOW_SUM.  Overflow (i.e. carry out) is not stored anywhere, and is
  48.    lost.
  49.  
  50.    6) sub_ddmmss(high_difference, low_difference, high_minuend,
  51.    low_minuend, high_subtrahend, low_subtrahend) subtracts two
  52.    two-word unsigned integers, composed by HIGH_MINUEND_1 and
  53.    LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2
  54.    respectively.  The result is placed in HIGH_DIFFERENCE and
  55.    LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
  56.    and is lost.
  57.  
  58.    If any of these macors are left undefined for a particular CPU,
  59.    C macros are used.
  60.  
  61.    There are simple machine instructions for these operations on many
  62.    CPUs.  Use them!  */
  63.  
  64. /* Please add support for more CPUs here!
  65.  
  66.    rs6000:        signed mul with adjustment
  67.    vax:            ??
  68.    we32100:        ??
  69.    i860:        ??
  70.    */
  71.  
  72. #if defined (__GNUC__) && !defined (NO_ASM)
  73.  
  74. #if defined (mc68020) || defined (__NeXT__)
  75.  
  76. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  77.   asm ("add%.l %5,%1
  78.     addx%.l %3,%0"                            \
  79.        : "=d" (sh), "=&d" (sl)                        \
  80.        : "%0" (ah), "d" (bh), "%1" (al), "g" (bl))
  81. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  82.   asm ("sub%.l %5,%1
  83.     subx%.l %3,%0"                            \
  84.        : "=d" (sh), "=&d" (sl)                        \
  85.        : "0" (ah), "d" (bh), "1" (al), "g" (bl))
  86. #define __umulsidi3(u, v) \
  87.   ({long_long __w;                            \
  88.     asm ("mulu%.l %3,%1:%0"                        \
  89.      : "=d" (__w.s.low), "=d" (__w.s.high) : "%0" (u), "dmi" (v));    \
  90.     __w.ll; })
  91. #define umul_ppmm(w1, w0, u, v) \
  92.   asm ("mulu%.l %3,%1:%0" : "=d" (w0), "=d" (w1) : "%0" (u), "dmi" (v))
  93. #define udiv_qrnnd(q, r, n1, n0, d) \
  94.   asm ("divu%.l %4,%1:%0" : "=d" (q), "=d" (r) : "0" (n0), "1" (n1), "dmi" (d))
  95. #define count_leading_zeros(count, x) \
  96.   asm ("bfffo %1{#0:#0},%0" : "=d" (count) : "od" (x))
  97.  
  98. #elif defined (__i386__) || defined (__i486__)
  99.  
  100. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  101.   asm ("addl %5,%1
  102.     adcl %3,%0"                            \
  103.        : "=r" (sh), "=&r" (sl)                        \
  104.        : "%0" (ah), "g" (bh), "%1" (al), "g" (bl))
  105. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  106.   asm ("subl %5,%1
  107.     sbbl %3,%0"                            \
  108.        : "=r" (sh), "=&r" (sl)                        \
  109.        : "0" (ah), "g" (bh), "1" (al), "g" (bl))
  110. #define __umulsidi3(u, v) \
  111.   ({long_long __w;                            \
  112.       asm ("mull %3"                            \
  113.        : "=a" (__w.s.low), "=d" (__w.s.high)            \
  114.        : "%0" (u), "rm" (v));                    \
  115.     __w.ll; })
  116. #define umul_ppmm(w1, w0, u, v) \
  117.   asm ("mull %3" : "=a" (w0), "=d" (w1) : "%0" (u), "rm" (v))
  118. #define udiv_qrnnd(q, r, n1, n0, d) \
  119.   asm ("divl %4" : "=a" (q), "=d" (r) : "0" (n0), "1" (n1), "rm" (d))
  120. #define count_leading_zeros(count, x) \
  121.   do {                                    \
  122.     unsigned long __cbtmp;                        \
  123.     asm ("bsrl %1,%0" : "=r" (__cbtmp) : "rm" (x));            \
  124.     (count) = __cbtmp ^ 31;                        \
  125.   } while (0)
  126.  
  127. #elif defined (__sparc__)
  128.  
  129. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  130.   asm ("addcc %4,%5,%1
  131.     addx %2,%3,%0"                            \
  132.        : "=r" (sh), "=&r" (sl)                        \
  133.        : "%r" (ah), "rI" (bh), "%r" (al), "rI" (bl))
  134. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  135.   asm ("subcc %4,%5,%1
  136.     subx %2,%3,%0"                            \
  137.        : "=r" (sh), "=&r" (sl)                        \
  138.        : "r" (ah), "rI" (bh), "r" (al), "rI" (bl))
  139.  
  140. #if defined (__sparc8__)    /* How do we recog. version 8 SPARC?  */
  141. #define umul_ppmm(w1, w0, u, v) \
  142.   asm ("umul %2,%3,%1;rd %%y,%0" : "=r" (w1), "=r" (w0) : "r" (u), "r" (v))
  143. #define udiv_qrnnd(q, r, n1, n0, d) \
  144.   asm ("wr %2,0,%%y;udiv %3,%4,%0;umul %0,%4,%1;sub %3,%1,%1"        \
  145.        : "=&r" (q), "=&r" (r) : "r" (n1), "r" (n0), "r" (d))
  146. #else
  147. /* SPARC without integer multiplication and divide instructions.
  148.    (i.e. at least Sun4/60,65,75,110,260,280,330,360,380,470,490) */
  149. /* Go via __call_umulsidi3 to make "defined (__umulsidi3)" true.  */
  150. /* SPARC .umul routine returns the value in WORD_LITTLE_ENDIAN form. Swap it */
  151. #define __umulsidi3(u, v)                        \
  152.   ({ unsigned long __arg1 = (u);                    \
  153.      unsigned long __arg2 = (v);                    \
  154.                                          \
  155.      union {                                \
  156.     struct {                            \
  157.         unsigned long high;                        \
  158.         unsigned long low;                        \
  159.     } l;                                \
  160.     unsigned long long ll;                        \
  161.     } __u, __tem;                            \
  162.                                         \
  163.     __tem.ll = __call_umulsidi3 (__arg1, __arg2);            \
  164.     __u.l.low = __tem.l.high;                        \
  165.     __u.l.high = __tem.l.low;                        \
  166.                                     \
  167.     __u.ll; })
  168.  
  169. unsigned long long __call_umulsidi3 (unsigned, unsigned) asm (".umul");
  170.  
  171. #define umul_ppmm(w1, w0, u, v)                     \
  172.   do {                                    \
  173.     union {long long ll; struct {int h, l;} i;} __ww;            \
  174.     __ww.ll = __umulsidi3 (u, v);                    \
  175.     (w1) = __ww.i.h;                            \
  176.     (w0) = __ww.i.l;                            \
  177.   } while (0)
  178.  
  179. /* It's quite necessary to add this much assembler for the sparc.
  180.    The default udiv_qrnnd (in C) is more than 10 times slower!  */
  181. #define udiv_qrnnd(q, r, n1, n0, d) \
  182.   asm ("! Inlined udiv_qrnnd\n"                        \
  183. "    mov    32,%%g1\n"                        \
  184. "    subcc    %1,%2,%%g0\n"                        \
  185. "1:    bcs    5f\n"                            \
  186. "     addxcc %0,%0,%0    ! shift n1n0 and a q-bit in lsb\n"    \
  187. "    sub    %1,%2,%1    ! this kills msb of n\n"        \
  188. "    addx    %1,%1,%1    ! so this can't give carry\n"        \
  189. "    subcc    %%g1,1,%%g1\n"                        \
  190. "2:    bne    1b\n"                            \
  191. "     subcc    %1,%2,%%g0\n"                        \
  192.                                     \
  193. "    bcs    3f\n"                            \
  194. "     addxcc %0,%0,%0    ! shift n1n0 and a q-bit in lsb\n"    \
  195. "    b    3f\n"                            \
  196. "     sub    %1,%2,%1    ! this kills msb of n\n"        \
  197.                                     \
  198. "4:    sub    %1,%2,%1\n"                        \
  199. "5:    addxcc    %1,%1,%1\n"                        \
  200. "    bcc    2b\n"                            \
  201. "     subcc    %%g1,1,%%g1\n"                        \
  202. "! Got carry from n.  Subtract next step to cancel this carry.\n"    \
  203. "    bne    4b\n"                            \
  204. "     addcc    %0,%0,%0    ! shift n1n0 and a 0-bit in lsb\n"    \
  205.                                     \
  206. "    sub    %1,%2,%1\n"                        \
  207. "3:    xnor    %0,0,%0\n"                        \
  208.        : "=r&" (q), "=r&" (r) : "r" (d), "1" (n1), "0" (n0) : "%g1")
  209. #endif
  210.  
  211. #elif defined (__mips__)
  212.  
  213. #define umul_ppmm(w1, w0, u, v) \
  214.   asm ("multu %2,%3
  215.     mflo %0
  216.     mfhi %1"                            \
  217.        : "=r" (w0), "=r" (w1) : "r" (u), "r" (v))
  218.  
  219. #elif defined (__m88000__)
  220.  
  221. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  222.   asm ("addu.co %1,%r4,%r5
  223.     addu.ci %0,%r2,%r3"                        \
  224.      : "=r" (sh), "=&r" (sl)                    \
  225.      : "%rJ" (ah), "rJ" (bh), "%rJ" (al), "rJ" (bl))
  226. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  227.   asm ("subu.co %1,%r4,%r5
  228.     subu.ci %0,%r2,%r3"                        \
  229.      : "=r" (sh), "=&r" (sl)                    \
  230.      : "rJ" (ah), "rJ" (bh), "rJ" (al), "rJ" (bl))
  231. #define count_leading_zeros(count, x) \
  232.   do {                                    \
  233.     unsigned long __cbtmp;                        \
  234.     asm ("ff1 %0,%1" : "=r" (__cbtmp) : "r" (x));            \
  235.     (count) = __cbtmp ^ 31;                        \
  236.   } while (0)
  237.  
  238. #elif defined (__pyr__)
  239.  
  240. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  241.   asm ("addw    %5,%1
  242.     addwc    %3,%0"                            \
  243.        : "=r" (sh), "=&r" (sl)                        \
  244.        : "%0" (ah), "g" (bh), "%1" (al), "g" (bl))
  245. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  246.   asm ("subw    %5,%1
  247.     subwb    %3,%0"                            \
  248.        : "=r" (sh), "=&r" (sl)                        \
  249.        : "0" (ah), "g" (bh), "1" (al), "g" (bl))
  250. /* This insn doesn't work on ancient pyramids.  */
  251. #define umul_ppmm(w1, w0, u, v) \
  252.   asm ("movw %2,tr11
  253.     uemul %3,tr10
  254.     movw tr10,%0
  255.     movw tr11,%1"                            \
  256.        : "=r" (w1), "=r" (w0) : "r" (u), "r" (v) : "tr10", "tr11")
  257.  
  258. #elif defined (__a29k__) || defined (___AM29K__)
  259.  
  260. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  261.   asm ("add %1,%4,%5
  262.     addc %0,%2,%3"                            \
  263.      : "=r" (sh), "=&r" (sl)                    \
  264.      : "%r" (ah), "rI" (bh), "%r" (al), "rI" (bl))
  265. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  266.   asm ("sub %1,%4,%5
  267.     subc %0,%2,%3"                            \
  268.      : "=r" (sh), "=&r" (sl)                    \
  269.      : "r" (ah), "rI" (bh), "r" (al), "rI" (bl))
  270. #define umul_ppmm(w1, w0, u, v) \
  271.   asm ("multiplu %1,%2,%3
  272.     multmu %0,%2,%3"                        \
  273.        : "=r" (w1), "=&r" (w0) : "r" (u), "r" (v))
  274. #define udiv_qrnnd(q, r, n1, n0, d) \
  275.   asm ("dividu %0,%3,%4"                        \
  276.        : "=r" (q), "=q" (r) : "q" (n1), "r" (n0), "r" (d))
  277. #define udiv_qrnnd(q, r, n1, n0, d) \
  278.   asm ("dividu %0,%3,%4"                        \
  279.        : "=r" (q), "=q" (r) : "q" (n1), "r" (n0), "r" (d))
  280. #define count_leading_zeros(count, x) \
  281.     asm ("clz %0,%1" : "=r" (count) : "r" (x))
  282.  
  283. #elif defined (___IBMR2__)
  284.  
  285. #define count_leading_zeros(count, x) \
  286.     asm ("cntlz %0,%1" : "=r" (count) : "r" (x))
  287.  
  288. #elif defined (__ns32000__)
  289. #define __umulsidi3(u, v) \
  290.   ({long long __w;                            \
  291.       asm ("meid %2,%0" : "=g" (__w) : "%0" (u), "g" (v));        \
  292.       __w; })
  293. #define div_qrnnd(q, r, n1, n0, d) \
  294.   asm ("movd %2,r0
  295.     movd %3,r1
  296.     deid %4,r0
  297.     movd r1,%0
  298.     movd r0,%1"                            \
  299.        : "=g" (q), "=g" (r)                        \
  300.        : "g" (n0), "g" (n1), "g" (d) : "r0", "r1")
  301.  
  302. #endif
  303.  
  304. #endif /* __GNUC__ */
  305.  
  306. /* If this machine has no inline assembler, use C macros.  */
  307.  
  308. #if !defined (add_ssaaaa)
  309. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  310.   do {                                    \
  311.     unsigned __x;                            \
  312.     __x = (al) + (bl);                            \
  313.     (sh) = (ah) + (bh) + (__x < (al));                    \
  314.     (sl) = __x;                                \
  315.   } while (0)
  316. #endif
  317.  
  318. #if !defined (sub_ddmmss)
  319. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  320.   do {                                    \
  321.     unsigned __x;                            \
  322.     __x = (al) - (bl);                            \
  323.     (sh) = (ah) - (bh) - (__x > (al));                    \
  324.     (sl) = __x;                                \
  325.   } while (0)
  326. #endif
  327.  
  328. #if !defined (umul_ppmm)
  329. #define umul_ppmm(w1, w0, u, v)                        \
  330.   do {                                    \
  331.     unsigned long __x0, __x1, __x2, __x3;                \
  332.     unsigned long __ul, __vl, __uh, __vh;                \
  333.                                     \
  334.     __ul = lowpart (u);                            \
  335.     __uh = highpart (u);                        \
  336.     __vl = lowpart (v);                            \
  337.     __vh = highpart (v);                        \
  338.                                     \
  339.     __x0 = __ul * __vl;                            \
  340.     __x1 = __ul * __vh;                            \
  341.     __x2 = __uh * __vl;                            \
  342.     __x3 = __uh * __vh;                            \
  343.                                     \
  344.     __x1 += highpart (__x0);    /* this can't give carry */        \
  345.     __x1 += __x2;        /* but this indeed can */        \
  346.     if (__x1 < __x2)        /* did we get it? */            \
  347.       __x3 += B;        /* yes, add it in the proper pos. */    \
  348.                                     \
  349.     (w1) = __x3 + highpart (__x1);                    \
  350.     (w0) = lowpart (__x1) * B + lowpart (__x0);                \
  351.   } while (0)
  352. #endif
  353.  
  354. #if !defined (__umulsidi3)
  355. #define __umulsidi3(u, v) \
  356.   ({long_long __w;                            \
  357.     umul_ppmm (__w.s.high, __w.s.low, u, v);                \
  358.     __w.ll; })
  359. #endif
  360.  
  361. #if !defined (udiv_qrnnd)
  362. #define UDIV_NEEDS_NORMALIZATION 1
  363. #define udiv_qrnnd(q, r, n1, n0, d) \
  364.   do {                                    \
  365.     unsigned int __d1, __d0, __q1, __q0;                \
  366.     unsigned long __r1, __r0, __m;                    \
  367.     __d1 = highpart (d);                        \
  368.     __d0 = lowpart (d);                            \
  369.                                     \
  370.     __r1 = (n1) % __d1;                            \
  371.     __q1 = (n1) / __d1;                            \
  372.     __m = (unsigned long) __q1 * __d0;                    \
  373.     __r1 = __r1 * B | highpart (n0);                    \
  374.     if (__r1 < __m)                            \
  375.       {                                    \
  376.     __q1--, __r1 += (d);                        \
  377.     if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
  378.       if (__r1 < __m)                        \
  379.         __q1--, __r1 += (d);                    \
  380.       }                                    \
  381.     __r1 -= __m;                            \
  382.                                     \
  383.     __r0 = __r1 % __d1;                            \
  384.     __q0 = __r1 / __d1;                            \
  385.     __m = (unsigned long) __q0 * __d0;                    \
  386.     __r0 = __r0 * B | lowpart (n0);                    \
  387.     if (__r0 < __m)                            \
  388.       {                                    \
  389.     __q0--, __r0 += (d);                        \
  390.     if (__r0 >= (d))                        \
  391.       if (__r0 < __m)                        \
  392.         __q0--, __r0 += (d);                    \
  393.       }                                    \
  394.     __r0 -= __m;                            \
  395.                                     \
  396.     (q) = (unsigned long) __q1 * B | __q0;                \
  397.     (r) = __r0;                                \
  398.   } while (0)
  399. #endif
  400.  
  401. #if !defined (count_leading_zeros)
  402. #define count_leading_zeros(count, x) \
  403.   do {                                    \
  404.     unsigned long xr = (x);                        \
  405.     static const unsigned char t[] =                    \
  406.       {                                    \
  407.     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,\
  408.     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,\
  409.     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,\
  410.     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,\
  411.     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,\
  412.     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,\
  413.     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,\
  414.     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    \
  415.       };                                \
  416.     unsigned a;                                \
  417.                                     \
  418.     a = xr <= 0xffff ? (xr <= 0xff ? 0 : 8) : (xr <= 0xffffff ?  16 : 24);\
  419.                                     \
  420.     (count) =  32 - (t[xr >> a] + a);                \
  421.   } while (0)
  422. #endif
  423.  
  424. #ifndef UDIV_NEEDS_NORMALIZATION
  425. #define UDIV_NEEDS_NORMALIZATION 0
  426. #endif
  427.