46#ifndef XT_ARITHMETIC_LONG_H
47#define XT_ARITHMETIC_LONG_H
76 return (x >= 0) - (x < 0);
82 return x < 0 ? -x : x;
183 return (a.
hi == b.
hi) & (a.midlo == b.midlo);
200static inline struct Xt_muldiv
233 }
while (q1 < delta || (q1 == delta && r1 == 0));
242static inline XT_INT_DIV_T
243fast_division(
Xt_int num,
struct Xt_muldiv
id)
247 = (
Xt_int)(
id.den == 1 || (
id.den > 0 && M < 0)
248 ? num :
id.den < 0 && M > 0 ? -num : 0);
253 q = (
Xt_int)(q + (q < 0));
256 return (XT_INT_DIV_T){ .quot = q, .rem = r };
259#define Xt_div(num, muldiv, den) fast_division(num, muldiv)
273 return (sign_bit^1) - sign_bit;
289 Xt_long r = { .hi = ((a.
hi + sign_mask) ^ sign_mask) - borrow,
290 .lo = (a.
lo + sign_mask) ^ sign_mask };
328 Xt_long r = { .lo = al + bl, .hi = ah + bh + carry };
338 Xt_long r = { .lo = al + bl, .hi = ah + bh + carry };
348 Xt_long r = { .lo = al + bl, .hi = ah + carry };
357 Xt_long r = { .lo = al + bl, .hi = ah + bh + carry };
367 Xt_long r = { .lo = al - bl, .hi = ah - bh - carry };
376 Xt_long r = { .lo = al - bl, .hi = ah - bh - carry };
397 Xt_long r = { .lo = al - bl, .hi = ah - bh - carry };
468 t = a1*bh + (r.
mid & lo_mask) + k;
487 r.
hi -= a.
hi + borrow;
516 ah = a.
hi, al = a.
lo,
527 enum { int_bits =
sizeof (int) * CHAR_BIT };
537 un0 = un10 & lo_mask;
540 rhat = un32 - q1*vn1;
541 while (q1 >= base || q1*vn0 > base*rhat + un1) {
544 if (rhat >= base)
break;
547 un21 = un32*base + un1 - q1*b;
550 rhat = un21 - q0*vn1;
551 while (q0 >= base || q0*vn0 > base*rhat + un0) {
554 if (rhat >= base)
break;
558 .rem = (
Xt_int)((un21*base + un0 - q0*b) >> s)};
596 if (m < n || n <= 0 || v[n-1] == 0)
601 for (
int j = m - 1; j >= 0; j--) {
602 q[j] = (
Xt_ushort)((k*base + u[j])/v[0]);
616 for (
int i = n - 1; i > 0; i--)
622 for (
int i = m - 1; i > 0; i--)
626 for (
int j = m - n; j >= 0; j--) {
628 Xt_uint qhat = (un[j+n]*base + un[j+n-1])/vn[n-1],
630 rhat = (un[j+n]*base + un[j+n-1]) - qhat*vn[n-1];
631 while (qhat >= base || qhat*vn[n-2] > base*rhat + un[j+n-2]) {
634 if (rhat >= base)
break;
639 for (
int i = 0; i < n; i++) {
641 t = un[i+j] - k - (
Xt_int)(p & lo_mask);
652 for (
int i = 0; i < n; i++) {
653 t = un[i+j] + vn[i] + k;
663 for (
int i = 0; i < n; i++)
703 }
else if (u.
hi < v.
hi || (u.
hi == v.
hi && u.
lo < v.
lo)) {
721 if ((u.
hi >= base) == (v.
hi >= base)) {
725 while (qhat >= base || qhat*(vn.
hi & lo_mask) > base*rhat + (un.
mid >>
xt_hint_bits)) {
728 if (rhat >= base)
break;
740 p = qhat * (vn.
hi & lo_mask);
775 Xt_ushort ua[4], va[4], qa[4] = { 0U }, ra[4] = { 0U };
779 while (ua[m-1] == 0U) --m;
780 while (va[n-1] == 0U) --n;
797 Xt_ldiv r = { { 0, 0 }, { 0, 0 } };
820 int r = ah > bh || (ah == bh && al > bl);
832 int r = ah > bh || (ah == bh && al >= bl);
844 int r = ah < bh || (ah == bh && al <= bl);
856 int r = ah < bh || (ah == bh && al < bl);
863 return ((a.
hi == b.
hi) & (a.
lo == b.
lo));
872#define Xt_div(num, muldiv, den) XT_INT_DIV((num), (den))
static Xt_ldiv xlldivu(Xt_ulong u, Xt_ulong v)
static Xt_long xlisub(Xt_long a, Xt_int b)
static Xt_uint xtlo(Xt_tword x)
static Xt_tword xlimul(Xt_long a, Xt_int b)
static int xlicmp_gt(Xt_long a, Xt_int b)
static Xt_long xlladd(Xt_long a, Xt_long b)
static Xt_long xiisub(Xt_int a, Xt_int b)
static Xt_long xi2l(Xt_int a)
static Xt_ldiv xlldiv(Xt_long a, Xt_long b)
static Xt_uint xlhi(Xt_long x)
static Xt_long xliadd(Xt_long a, Xt_int b)
static int xttcmp_eq(Xt_tword a, Xt_tword b)
static Xt_uint xtmid(Xt_tword x)
static Xt_long xllsub(Xt_long a, Xt_long b)
static Xt_uint xthi(Xt_tword x)
static Xt_uint xllo(Xt_long x)
static void xl2a(Xt_ushort a[4], Xt_long u)
static int xllcmp_eq(Xt_long a, Xt_long b)
unsigned XT_SHORT Xt_ushort
static Xt_idiv xlidivu(Xt_ulong a, Xt_uint b)
static void xdivmnu(Xt_ushort *restrict q, Xt_ushort *restrict r, const Xt_ushort *restrict u, const Xt_ushort *restrict v, int m, int n)
static int xlicmp_lt(Xt_long a, Xt_int b)
static Xt_long xa2l(Xt_ushort a[4])
static Xt_long xiimul(Xt_int a, Xt_int b)
static bool xl_is_in_xt_int_range(Xt_long a)
static Xt_ldiv xtidiv(Xt_tword a, Xt_int b)
static Xt_long xlabs(Xt_long a)
static Xt_long xilsub(Xt_int a, Xt_long b)
static Xt_long xlinc(Xt_long a, bool b)
static Xt_long xlnegate(Xt_long a, bool negate)
static int xlsign(Xt_long x)
static int xlicmp_le(Xt_long a, Xt_int b)
static Xt_long xiiadd(Xt_int a, Xt_int b)
static int xlicmp_ge(Xt_long a, Xt_int b)
static Xt_int Xt_isign_mask(Xt_int x)
static int xinlz(Xt_uint v)
base definitions header file