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 >
Wrap
Text File
|
1991-06-03
|
14KB
|
427 lines
/* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
Copyright (C) 1991 Free Software Foundation, Inc.
This definition file is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2, or (at your option) any later version.
This definition file is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#define B (1L << (8 * sizeof (unsigned long) / 2))
#define lowpart(t) ((unsigned long) (t) % B)
#define highpart(t) ((unsigned long) (t) / B)
/* Define auxilliary asm macros.
1) umul_ppmm(high_prod, low_prod, multipler, multiplicand)
multiplies two unsigned long integers MULTIPLER and MULTIPLICAND,
and generates a two unsigned word product in HIGH_PROD and
LOW_PROD.
2) __umulsidi3(a,b) multiplies two unsigned long integers A and B,
and returns a long long product.
3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
denominator) divides a two-word unsigned integer, composed by the
integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and
places the quotient in QUOTIENT and the remainder in REMAINDER.
The most significant bit of DENOMINATOR must be 1, and
HIGH_NUMERATOR must be less than DENOMINATOR for correct operation.
4) count_leading_zeros(count,x) counts the number of zero-bits from
the msb to the first non-zero bit. This is the number of steps X
needs to be shifted left to set the msb.
5) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
high_addend_2, low_addend_2) adds two two-word unsigned integers,
composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and
LOW_ADDEND_2 respectively. The result is placed in HIGH_SUM and
LOW_SUM. Overflow (i.e. carry out) is not stored anywhere, and is
lost.
6) sub_ddmmss(high_difference, low_difference, high_minuend,
low_minuend, high_subtrahend, low_subtrahend) subtracts two
two-word unsigned integers, composed by HIGH_MINUEND_1 and
LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2
respectively. The result is placed in HIGH_DIFFERENCE and
LOW_DIFFERENCE. Overflow (i.e. carry out) is not stored anywhere,
and is lost.
If any of these macors are left undefined for a particular CPU,
C macros are used.
There are simple machine instructions for these operations on many
CPUs. Use them! */
/* Please add support for more CPUs here!
rs6000: signed mul with adjustment
vax: ??
we32100: ??
i860: ??
*/
#if defined (__GNUC__) && !defined (NO_ASM)
#if defined (mc68020) || defined (__NeXT__)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
asm ("add%.l %5,%1
addx%.l %3,%0" \
: "=d" (sh), "=&d" (sl) \
: "%0" (ah), "d" (bh), "%1" (al), "g" (bl))
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
asm ("sub%.l %5,%1
subx%.l %3,%0" \
: "=d" (sh), "=&d" (sl) \
: "0" (ah), "d" (bh), "1" (al), "g" (bl))
#define __umulsidi3(u, v) \
({long_long __w; \
asm ("mulu%.l %3,%1:%0" \
: "=d" (__w.s.low), "=d" (__w.s.high) : "%0" (u), "dmi" (v)); \
__w.ll; })
#define umul_ppmm(w1, w0, u, v) \
asm ("mulu%.l %3,%1:%0" : "=d" (w0), "=d" (w1) : "%0" (u), "dmi" (v))
#define udiv_qrnnd(q, r, n1, n0, d) \
asm ("divu%.l %4,%1:%0" : "=d" (q), "=d" (r) : "0" (n0), "1" (n1), "dmi" (d))
#define count_leading_zeros(count, x) \
asm ("bfffo %1{#0:#0},%0" : "=d" (count) : "od" (x))
#elif defined (__i386__) || defined (__i486__)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
asm ("addl %5,%1
adcl %3,%0" \
: "=r" (sh), "=&r" (sl) \
: "%0" (ah), "g" (bh), "%1" (al), "g" (bl))
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
asm ("subl %5,%1
sbbl %3,%0" \
: "=r" (sh), "=&r" (sl) \
: "0" (ah), "g" (bh), "1" (al), "g" (bl))
#define __umulsidi3(u, v) \
({long_long __w; \
asm ("mull %3" \
: "=a" (__w.s.low), "=d" (__w.s.high) \
: "%0" (u), "rm" (v)); \
__w.ll; })
#define umul_ppmm(w1, w0, u, v) \
asm ("mull %3" : "=a" (w0), "=d" (w1) : "%0" (u), "rm" (v))
#define udiv_qrnnd(q, r, n1, n0, d) \
asm ("divl %4" : "=a" (q), "=d" (r) : "0" (n0), "1" (n1), "rm" (d))
#define count_leading_zeros(count, x) \
do { \
unsigned long __cbtmp; \
asm ("bsrl %1,%0" : "=r" (__cbtmp) : "rm" (x)); \
(count) = __cbtmp ^ 31; \
} while (0)
#elif defined (__sparc__)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
asm ("addcc %4,%5,%1
addx %2,%3,%0" \
: "=r" (sh), "=&r" (sl) \
: "%r" (ah), "rI" (bh), "%r" (al), "rI" (bl))
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
asm ("subcc %4,%5,%1
subx %2,%3,%0" \
: "=r" (sh), "=&r" (sl) \
: "r" (ah), "rI" (bh), "r" (al), "rI" (bl))
#if defined (__sparc8__) /* How do we recog. version 8 SPARC? */
#define umul_ppmm(w1, w0, u, v) \
asm ("umul %2,%3,%1;rd %%y,%0" : "=r" (w1), "=r" (w0) : "r" (u), "r" (v))
#define udiv_qrnnd(q, r, n1, n0, d) \
asm ("wr %2,0,%%y;udiv %3,%4,%0;umul %0,%4,%1;sub %3,%1,%1" \
: "=&r" (q), "=&r" (r) : "r" (n1), "r" (n0), "r" (d))
#else
/* SPARC without integer multiplication and divide instructions.
(i.e. at least Sun4/60,65,75,110,260,280,330,360,380,470,490) */
/* Go via __call_umulsidi3 to make "defined (__umulsidi3)" true. */
/* SPARC .umul routine returns the value in WORD_LITTLE_ENDIAN form. Swap it */
#define __umulsidi3(u, v) \
({ unsigned long __arg1 = (u); \
unsigned long __arg2 = (v); \
\
union { \
struct { \
unsigned long high; \
unsigned long low; \
} l; \
unsigned long long ll; \
} __u, __tem; \
\
__tem.ll = __call_umulsidi3 (__arg1, __arg2); \
__u.l.low = __tem.l.high; \
__u.l.high = __tem.l.low; \
\
__u.ll; })
unsigned long long __call_umulsidi3 (unsigned, unsigned) asm (".umul");
#define umul_ppmm(w1, w0, u, v) \
do { \
union {long long ll; struct {int h, l;} i;} __ww; \
__ww.ll = __umulsidi3 (u, v); \
(w1) = __ww.i.h; \
(w0) = __ww.i.l; \
} while (0)
/* It's quite necessary to add this much assembler for the sparc.
The default udiv_qrnnd (in C) is more than 10 times slower! */
#define udiv_qrnnd(q, r, n1, n0, d) \
asm ("! Inlined udiv_qrnnd\n" \
" mov 32,%%g1\n" \
" subcc %1,%2,%%g0\n" \
"1: bcs 5f\n" \
" addxcc %0,%0,%0 ! shift n1n0 and a q-bit in lsb\n" \
" sub %1,%2,%1 ! this kills msb of n\n" \
" addx %1,%1,%1 ! so this can't give carry\n" \
" subcc %%g1,1,%%g1\n" \
"2: bne 1b\n" \
" subcc %1,%2,%%g0\n" \
\
" bcs 3f\n" \
" addxcc %0,%0,%0 ! shift n1n0 and a q-bit in lsb\n" \
" b 3f\n" \
" sub %1,%2,%1 ! this kills msb of n\n" \
\
"4: sub %1,%2,%1\n" \
"5: addxcc %1,%1,%1\n" \
" bcc 2b\n" \
" subcc %%g1,1,%%g1\n" \
"! Got carry from n. Subtract next step to cancel this carry.\n" \
" bne 4b\n" \
" addcc %0,%0,%0 ! shift n1n0 and a 0-bit in lsb\n" \
\
" sub %1,%2,%1\n" \
"3: xnor %0,0,%0\n" \
: "=r&" (q), "=r&" (r) : "r" (d), "1" (n1), "0" (n0) : "%g1")
#endif
#elif defined (__mips__)
#define umul_ppmm(w1, w0, u, v) \
asm ("multu %2,%3
mflo %0
mfhi %1" \
: "=r" (w0), "=r" (w1) : "r" (u), "r" (v))
#elif defined (__m88000__)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
asm ("addu.co %1,%r4,%r5
addu.ci %0,%r2,%r3" \
: "=r" (sh), "=&r" (sl) \
: "%rJ" (ah), "rJ" (bh), "%rJ" (al), "rJ" (bl))
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
asm ("subu.co %1,%r4,%r5
subu.ci %0,%r2,%r3" \
: "=r" (sh), "=&r" (sl) \
: "rJ" (ah), "rJ" (bh), "rJ" (al), "rJ" (bl))
#define count_leading_zeros(count, x) \
do { \
unsigned long __cbtmp; \
asm ("ff1 %0,%1" : "=r" (__cbtmp) : "r" (x)); \
(count) = __cbtmp ^ 31; \
} while (0)
#elif defined (__pyr__)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
asm ("addw %5,%1
addwc %3,%0" \
: "=r" (sh), "=&r" (sl) \
: "%0" (ah), "g" (bh), "%1" (al), "g" (bl))
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
asm ("subw %5,%1
subwb %3,%0" \
: "=r" (sh), "=&r" (sl) \
: "0" (ah), "g" (bh), "1" (al), "g" (bl))
/* This insn doesn't work on ancient pyramids. */
#define umul_ppmm(w1, w0, u, v) \
asm ("movw %2,tr11
uemul %3,tr10
movw tr10,%0
movw tr11,%1" \
: "=r" (w1), "=r" (w0) : "r" (u), "r" (v) : "tr10", "tr11")
#elif defined (__a29k__) || defined (___AM29K__)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
asm ("add %1,%4,%5
addc %0,%2,%3" \
: "=r" (sh), "=&r" (sl) \
: "%r" (ah), "rI" (bh), "%r" (al), "rI" (bl))
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
asm ("sub %1,%4,%5
subc %0,%2,%3" \
: "=r" (sh), "=&r" (sl) \
: "r" (ah), "rI" (bh), "r" (al), "rI" (bl))
#define umul_ppmm(w1, w0, u, v) \
asm ("multiplu %1,%2,%3
multmu %0,%2,%3" \
: "=r" (w1), "=&r" (w0) : "r" (u), "r" (v))
#define udiv_qrnnd(q, r, n1, n0, d) \
asm ("dividu %0,%3,%4" \
: "=r" (q), "=q" (r) : "q" (n1), "r" (n0), "r" (d))
#define udiv_qrnnd(q, r, n1, n0, d) \
asm ("dividu %0,%3,%4" \
: "=r" (q), "=q" (r) : "q" (n1), "r" (n0), "r" (d))
#define count_leading_zeros(count, x) \
asm ("clz %0,%1" : "=r" (count) : "r" (x))
#elif defined (___IBMR2__)
#define count_leading_zeros(count, x) \
asm ("cntlz %0,%1" : "=r" (count) : "r" (x))
#elif defined (__ns32000__)
#define __umulsidi3(u, v) \
({long long __w; \
asm ("meid %2,%0" : "=g" (__w) : "%0" (u), "g" (v)); \
__w; })
#define div_qrnnd(q, r, n1, n0, d) \
asm ("movd %2,r0
movd %3,r1
deid %4,r0
movd r1,%0
movd r0,%1" \
: "=g" (q), "=g" (r) \
: "g" (n0), "g" (n1), "g" (d) : "r0", "r1")
#endif
#endif /* __GNUC__ */
/* If this machine has no inline assembler, use C macros. */
#if !defined (add_ssaaaa)
#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
do { \
unsigned __x; \
__x = (al) + (bl); \
(sh) = (ah) + (bh) + (__x < (al)); \
(sl) = __x; \
} while (0)
#endif
#if !defined (sub_ddmmss)
#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
do { \
unsigned __x; \
__x = (al) - (bl); \
(sh) = (ah) - (bh) - (__x > (al)); \
(sl) = __x; \
} while (0)
#endif
#if !defined (umul_ppmm)
#define umul_ppmm(w1, w0, u, v) \
do { \
unsigned long __x0, __x1, __x2, __x3; \
unsigned long __ul, __vl, __uh, __vh; \
\
__ul = lowpart (u); \
__uh = highpart (u); \
__vl = lowpart (v); \
__vh = highpart (v); \
\
__x0 = __ul * __vl; \
__x1 = __ul * __vh; \
__x2 = __uh * __vl; \
__x3 = __uh * __vh; \
\
__x1 += highpart (__x0); /* this can't give carry */ \
__x1 += __x2; /* but this indeed can */ \
if (__x1 < __x2) /* did we get it? */ \
__x3 += B; /* yes, add it in the proper pos. */ \
\
(w1) = __x3 + highpart (__x1); \
(w0) = lowpart (__x1) * B + lowpart (__x0); \
} while (0)
#endif
#if !defined (__umulsidi3)
#define __umulsidi3(u, v) \
({long_long __w; \
umul_ppmm (__w.s.high, __w.s.low, u, v); \
__w.ll; })
#endif
#if !defined (udiv_qrnnd)
#define UDIV_NEEDS_NORMALIZATION 1
#define udiv_qrnnd(q, r, n1, n0, d) \
do { \
unsigned int __d1, __d0, __q1, __q0; \
unsigned long __r1, __r0, __m; \
__d1 = highpart (d); \
__d0 = lowpart (d); \
\
__r1 = (n1) % __d1; \
__q1 = (n1) / __d1; \
__m = (unsigned long) __q1 * __d0; \
__r1 = __r1 * B | highpart (n0); \
if (__r1 < __m) \
{ \
__q1--, __r1 += (d); \
if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
if (__r1 < __m) \
__q1--, __r1 += (d); \
} \
__r1 -= __m; \
\
__r0 = __r1 % __d1; \
__q0 = __r1 / __d1; \
__m = (unsigned long) __q0 * __d0; \
__r0 = __r0 * B | lowpart (n0); \
if (__r0 < __m) \
{ \
__q0--, __r0 += (d); \
if (__r0 >= (d)) \
if (__r0 < __m) \
__q0--, __r0 += (d); \
} \
__r0 -= __m; \
\
(q) = (unsigned long) __q1 * B | __q0; \
(r) = __r0; \
} while (0)
#endif
#if !defined (count_leading_zeros)
#define count_leading_zeros(count, x) \
do { \
unsigned long xr = (x); \
static const unsigned char t[] = \
{ \
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,\
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,\
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,\
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,\
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,\
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,\
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,\
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 \
}; \
unsigned a; \
\
a = xr <= 0xffff ? (xr <= 0xff ? 0 : 8) : (xr <= 0xffffff ? 16 : 24);\
\
(count) = 32 - (t[xr >> a] + a); \
} while (0)
#endif
#ifndef UDIV_NEEDS_NORMALIZATION
#define UDIV_NEEDS_NORMALIZATION 0
#endif