void mpn_trace_file (const char *filename, mp_srcptr ptr, mp_size_t size) { FILE *fp; mpz_t z; fp = fopen (filename, "w"); if (fp == NULL) { perror ("fopen"); abort(); } MPN_NORMALIZE (ptr, size); PTR(z) = (mp_ptr) ptr; SIZ(z) = (int) size; mpz_out_str (fp, mp_trace_base, z); fprintf (fp, "\n"); if (ferror (fp) || fclose (fp) != 0) { printf ("error writing %s\n", filename); abort(); } }
void mpz_realloc2 (mpz_ptr m, mp_bitcnt_t bits) { mp_size_t new_alloc; bits -= (bits != 0); /* Round down, except if 0 */ new_alloc = 1 + bits / GMP_NUMB_BITS; if (sizeof (unsigned long) > sizeof (int)) /* param vs _mp_size field */ { if (UNLIKELY (new_alloc > INT_MAX)) { fprintf (stderr, "gmp: overflow in mpz type\n"); abort (); } } PTR(m) = __GMP_REALLOCATE_FUNC_LIMBS (PTR(m), ALLOC(m), new_alloc); ALLOC(m) = new_alloc; /* Don't create an invalid number; if the current value doesn't fit after reallocation, clear it to 0. */ if (ABSIZ(m) > new_alloc) SIZ(m) = 0; }
unsigned long mpf_get_ui (mpf_srcptr f) { mp_size_t size; mp_exp_t exp; mp_srcptr fp; mp_limb_t fl; exp = EXP (f); size = SIZ (f); fp = PTR (f); fl = 0; if (exp > 0) { /* there are some limbs above the radix point */ size = ABS (size); if (size >= exp) fl = fp[size-exp]; #if BITS_PER_ULONG > GMP_NUMB_BITS if (exp > 1 && size+1 >= exp) fl += (fp[size-exp+1] << GMP_NUMB_BITS); #endif } return (unsigned long) fl; }
void mpq_set_z (mpq_ptr dest, mpz_srcptr src) { mp_size_t num_size; mp_size_t abs_num_size; mp_ptr dp; num_size = SIZ (src); abs_num_size = ABS (num_size); dp = MPZ_NEWALLOC (NUM(dest), abs_num_size); SIZ(NUM(dest)) = num_size; MPN_COPY (dp, PTR(src), abs_num_size); PTR(DEN(dest))[0] = 1; SIZ(DEN(dest)) = 1; }
double mpf_get_d_2exp (mpir_si *exp2, mpf_srcptr src) { mp_size_t size, abs_size; mp_srcptr ptr; int cnt; mpir_si exp; size = SIZ(src); if (UNLIKELY (size == 0)) { *exp2 = 0; return 0.0; } ptr = PTR(src); abs_size = ABS (size); count_leading_zeros (cnt, ptr[abs_size - 1]); cnt -= GMP_NAIL_BITS; exp = EXP(src) * GMP_NUMB_BITS - cnt; *exp2 = exp; return mpn_get_d (ptr, abs_size, size, (long) - (abs_size * GMP_NUMB_BITS - cnt)); }
void mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v) { mp_srcptr up; mp_ptr rp, tp, rtp; mp_size_t usize; mp_size_t rsize, tsize; mp_size_t sign_quotient; mp_size_t prec; mp_limb_t q_limb; mp_exp_t rexp; TMP_DECL; #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ if (v > GMP_NUMB_MAX) { mpf_t vf; mp_limb_t vl[2]; SIZ(vf) = 2; EXP(vf) = 2; PTR(vf) = vl; vl[0] = v & GMP_NUMB_MASK; vl[1] = v >> GMP_NUMB_BITS; mpf_div (r, u, vf); return; }
int mpz_tstbit (mpz_srcptr u, unsigned long bit_index) { mp_srcptr u_ptr = PTR(u); mp_size_t size = SIZ(u); unsigned abs_size = ABS(size); unsigned long limb_index = bit_index / GMP_NUMB_BITS; mp_srcptr p = u_ptr + limb_index; mp_limb_t limb; if (limb_index >= abs_size) return (size < 0); limb = *p; if (size < 0) { limb = -limb; /* twos complement */ while (p != u_ptr) { p--; if (*p != 0) { limb--; /* make it a ones complement instead */ break; } } } return (limb >> (bit_index % GMP_NUMB_BITS)) & 1; }
VOID WeaponCheat ( PLAYERp pp, char *cheat_string ) { PLAYERp p; short pnum; unsigned int i; USERp u; TRAVERSE_CONNECT ( pnum ) { p = &Player[pnum]; u = User[p->PlayerSprite]; // ALL WEAPONS if ( !SW_SHAREWARE ) { p->WpnFlags = 0xFFFFFFFF; } else { p->WpnFlags = 0x0000207F; // Disallows high weapon cheat in shareware } for ( i = 0; i < SIZ ( p->WpnAmmo ); i++ ) { p->WpnAmmo[i] = DamageData[i].max_ammo; } PlayerUpdateWeapon ( p, u->WeaponNum ); } }
/* * Set f to z, choosing the smallest precision for f * so that z = f*(2^BPML)*zs*2^(RetVal) */ static int set_z (mpfr_ptr f, mpz_srcptr z, mp_size_t *zs) { mp_limb_t *p; mp_size_t s; int c; mpfr_prec_t pf; MPFR_ASSERTD (mpz_sgn (z) != 0); /* Remove useless ending 0 */ for (p = PTR (z), s = *zs = ABS (SIZ (z)) ; *p == 0; p++, s--) MPFR_ASSERTD (s >= 0); /* Get working precision */ count_leading_zeros (c, p[s-1]); pf = s * GMP_NUMB_BITS - c; if (pf < MPFR_PREC_MIN) pf = MPFR_PREC_MIN; mpfr_init2 (f, pf); /* Copy Mantissa */ if (MPFR_LIKELY (c)) mpn_lshift (MPFR_MANT (f), p, s, c); else MPN_COPY (MPFR_MANT (f), p, s); MPFR_SET_SIGN (f, mpz_sgn (z)); MPFR_SET_EXP (f, 0); return -c; }
/* Create a fake mpz consisting of just a single 1 bit, with totbits being the total number of bits, inclusive of that 1 bit. */ void mpz_fake_bits (mpz_ptr z, unsigned long totbits) { static mp_limb_t n; unsigned long zero_bits, zero_limbs; zero_bits = totbits - 1; zero_limbs = zero_bits / GMP_NUMB_BITS; zero_bits %= GMP_NUMB_BITS; SIZ(z) = zero_limbs + 1; PTR(z) = (&n) - (SIZ(z) - 1); n = CNST_LIMB(1) << zero_bits; ASSERT_ALWAYS (mpz_sizeinbase (z, 2) == totbits); }
void mpq_init (mpq_t x) { ALLOC(NUM(x)) = 1; PTR(NUM(x)) = __GMP_ALLOCATE_FUNC_LIMBS (1); SIZ(NUM(x)) = 0; ALLOC(DEN(x)) = 1; PTR(DEN(x)) = __GMP_ALLOCATE_FUNC_LIMBS (1); PTR(DEN(x))[0] = 1; SIZ(DEN(x)) = 1; #ifdef __CHECKER__ /* let the low limb look initialized, for the benefit of mpz_get_ui etc */ PTR(NUM(x))[0] = 0; #endif }
/* set f to the integer z multiplied by 2^e */ int mpfr_set_z_2exp (mpfr_ptr f, mpz_srcptr z, mpfr_exp_t e, mpfr_rnd_t rnd_mode) { mp_size_t fn, zn, dif, en; int k, sign_z, inex; mp_limb_t *fp, *zp; mpfr_exp_t exp; sign_z = mpz_sgn (z); if (MPFR_UNLIKELY (sign_z == 0)) /* ignore the exponent for 0 */ { MPFR_SET_ZERO(f); MPFR_SET_POS(f); MPFR_RET(0); } MPFR_ASSERTD (sign_z == MPFR_SIGN_POS || sign_z == MPFR_SIGN_NEG); zn = ABS(SIZ(z)); /* limb size of z */ /* compute en = floor(e/GMP_NUMB_BITS) */ en = (e >= 0) ? e / GMP_NUMB_BITS : (e + 1) / GMP_NUMB_BITS - 1; MPFR_ASSERTD (zn >= 1); if (MPFR_UNLIKELY (zn + en > MPFR_EMAX_MAX / GMP_NUMB_BITS + 1)) return mpfr_overflow (f, rnd_mode, sign_z); /* because zn + en >= MPFR_EMAX_MAX / GMP_NUMB_BITS + 2 implies (zn + en) * GMP_NUMB_BITS >= MPFR_EMAX_MAX + GMP_NUMB_BITS + 1 and exp = zn * GMP_NUMB_BITS + e - k >= (zn + en) * GMP_NUMB_BITS - k > MPFR_EMAX_MAX */ fp = MPFR_MANT (f); fn = MPFR_LIMB_SIZE (f); dif = zn - fn; zp = PTR(z); count_leading_zeros (k, zp[zn-1]); /* now zn + en <= MPFR_EMAX_MAX / GMP_NUMB_BITS + 1 thus (zn + en) * GMP_NUMB_BITS <= MPFR_EMAX_MAX + GMP_NUMB_BITS and exp = zn * GMP_NUMB_BITS + e - k <= (zn + en) * GMP_NUMB_BITS - k + GMP_NUMB_BITS - 1 <= MPFR_EMAX_MAX + 2 * GMP_NUMB_BITS - 1 */ exp = (mpfr_prec_t) zn * GMP_NUMB_BITS + e - k; /* The exponent will be exp or exp + 1 (due to rounding) */ if (MPFR_UNLIKELY (exp > __gmpfr_emax)) return mpfr_overflow (f, rnd_mode, sign_z); if (MPFR_UNLIKELY (exp + 1 < __gmpfr_emin)) return mpfr_underflow (f, rnd_mode == MPFR_RNDN ? MPFR_RNDZ : rnd_mode, sign_z); if (MPFR_LIKELY (dif >= 0)) { mp_limb_t rb, sb, ulp; int sh; /* number has to be truncated */ if (MPFR_LIKELY (k != 0)) { mpn_lshift (fp, &zp[dif], fn, k); if (MPFR_LIKELY (dif > 0)) fp[0] |= zp[dif - 1] >> (GMP_NUMB_BITS - k); }
void mpz_fib2_ui (mpz_ptr fn, mpz_ptr fnsub1, mpir_ui n) { mp_ptr fp, f1p; mp_size_t size; size = MPN_FIB2_SIZE (n); MPZ_REALLOC (fn, size); MPZ_REALLOC (fnsub1, size); fp = PTR (fn); f1p = PTR (fnsub1); size = mpn_fib2_ui (fp, f1p, n); SIZ(fn) = size - (n == 0); SIZ(fnsub1) = size - (f1p[size-1] == 0); }
/* this function is useful in debug mode to print residues */ static void mpres_print (mpres_t x, char* name, mpmod_t n) { mp_size_t m, xn; mpres_t t; mpres_init(t, n); mpz_set_ui(t, 1); mpres_mul (t, x, t, n); xn = SIZ(t); m = ABSIZ(t); MPN_NORMALIZE(PTR(t), m); SIZ(t) = xn >= 0 ? m : -m; gmp_printf ("%s=%Zd\n", name, t); SIZ(t) = xn; mpres_clear (t, n); }
void mpz_limbs_finish (mpz_ptr x, mp_size_t n) { assert (n >= 0); MPN_NORMALIZE (PTR(x), n); SIZ (x) = n; }
void mpz_tdiv_r_2exp (mpz_ptr res, mpz_srcptr in, mp_bitcnt_t cnt) { mp_size_t in_size = ABSIZ (in); mp_size_t res_size; mp_size_t limb_cnt = cnt / GMP_NUMB_BITS; mp_srcptr in_ptr = PTR (in); if (in_size > limb_cnt) { /* The input operand is (probably) greater than 2**CNT. */ mp_limb_t x; x = in_ptr[limb_cnt] & (((mp_limb_t) 1 << cnt % GMP_NUMB_BITS) - 1); if (x != 0) { res_size = limb_cnt + 1; MPZ_REALLOC (res, res_size); PTR (res)[limb_cnt] = x; } else { res_size = limb_cnt; MPN_NORMALIZE (in_ptr, res_size); MPZ_REALLOC (res, res_size); limb_cnt = res_size; } } else { /* The input operand is smaller than 2**CNT. We perform a no-op, apart from that we might need to copy IN to RES. */ res_size = in_size; MPZ_REALLOC (res, res_size); limb_cnt = res_size; } if (res != in) MPN_COPY (PTR (res), PTR (in), limb_cnt); SIZ (res) = SIZ (in) >= 0 ? res_size : -res_size; }
int mpz_invert (mpz_ptr inverse, mpz_srcptr x, mpz_srcptr n) { mpz_t gcd, tmp; mp_size_t xsize, nsize, size; TMP_DECL (marker); xsize = SIZ (x); nsize = SIZ (n); xsize = ABS (xsize); nsize = ABS (nsize); size = MAX (xsize, nsize) + 1; /* No inverse exists if the leftside operand is 0. Likewise, no inverse exists if the mod operand is 1. */ if (xsize == 0 || (nsize == 1 && (PTR (n))[0] == 1)) return 0; TMP_MARK (marker); MPZ_TMP_INIT (gcd, size); MPZ_TMP_INIT (tmp, size); mpz_gcdext (gcd, tmp, (mpz_ptr) 0, x, n); /* If no inverse existed, return with an indication of that. */ if (SIZ (gcd) != 1 || PTR(gcd)[0] != 1) { TMP_FREE (marker); return 0; } /* Make sure we return a positive inverse. */ if (SIZ (tmp) < 0) { if (SIZ (n) < 0) mpz_sub (inverse, tmp, n); else mpz_add (inverse, tmp, n); } else mpz_set (inverse, tmp); TMP_FREE (marker); return 1; }
void mpz_set_n (mpz_ptr z, mp_srcptr p, mp_size_t size) { ASSERT (size >= 0); MPN_NORMALIZE (p, size); MPZ_REALLOC (z, size); MPN_COPY (PTR(z), p, size); SIZ(z) = size; }
void mpq_set (mpq_ptr dest, mpq_srcptr src) { mp_size_t num_size, den_size; mp_size_t abs_num_size; mp_ptr dp; num_size = SIZ(NUM(src)); SIZ(NUM(dest)) = num_size; abs_num_size = ABS (num_size); dp = MPZ_NEWALLOC (NUM(dest), abs_num_size); MPN_COPY (dp, PTR(NUM(src)), abs_num_size); den_size = SIZ(DEN(src)); SIZ(DEN(dest)) = den_size; dp = MPZ_NEWALLOC (DEN(dest), den_size); MPN_COPY (dp, PTR(DEN(src)), den_size); }
Error, error, unsupported BITS_PER_MP_LIMB #endif int mpz_kronecker_ui (mpz_srcptr a, unsigned long b) { mp_srcptr a_ptr = PTR(a); mp_size_t a_size; mp_limb_t a_rem; int result_bit1; a_size = SIZ(a); if (a_size == 0) return JACOBI_0U (b); if (b > GMP_NUMB_MAX) { mp_limb_t blimbs[2]; mpz_t bz; ALLOC(bz) = numberof (blimbs); PTR(bz) = blimbs; mpz_set_ui (bz, b); return mpz_kronecker (a, bz); } if ((b & 1) != 0) { result_bit1 = JACOBI_ASGN_SU_BIT1 (a_size, b); } else { mp_limb_t a_low = a_ptr[0]; int twos; if (b == 0) return JACOBI_LS0 (a_low, a_size); /* (a/0) */ if (! (a_low & 1)) return 0; /* (even/even)=0 */ /* (a/2)=(2/a) for a odd */ count_trailing_zeros (twos, b); b >>= twos; result_bit1 = (JACOBI_TWOS_U_BIT1 (twos, a_low) ^ JACOBI_ASGN_SU_BIT1 (a_size, b)); } if (b == 1) return JACOBI_BIT1_TO_PN (result_bit1); /* (a/1)=1 for any a */ a_size = ABS(a_size); /* (a/b) = (a mod b / b) */ JACOBI_MOD_OR_MODEXACT_1_ODD (result_bit1, a_rem, a_ptr, a_size, b); return mpn_jacobi_base (a_rem, (mp_limb_t) b, result_bit1); }
void check_data (void) { static const struct { int base; const char *f; long want; } data[] = { { 10, "0", 0L }, { 10, "1", 1L }, { 10, "-1", -1L }, { 10, "2", 2L }, { 10, "-2", -2L }, { 10, "12345", 12345L }, { 10, "-12345", -12345L }, /* fraction bits ignored */ { 10, "0.5", 0L }, { 10, "-0.5", 0L }, { 10, "1.1", 1L }, { 10, "-1.1", -1L }, { 10, "1.9", 1L }, { 10, "-1.9", -1L }, { 16, "1.000000000000000000000000000000000000000000000000001", 1L }, { 16, "-1.000000000000000000000000000000000000000000000000001", -1L }, /* low bits extracted (this is undocumented) */ { 16, "1000000000000000000000000000000000000000000000000001", 1L }, { 16, "-1000000000000000000000000000000000000000000000000001", -1L }, }; int i; mpf_t f; long got; mpf_init2 (f, 2000L); for (i = 0; i < numberof (data); i++) { mpf_set_str_or_abort (f, data[i].f, data[i].base); got = mpf_get_si (f); if (got != data[i].want) { printf ("mpf_get_si wrong at data[%d]\n", i); printf (" f \"%s\"\n", data[i].f); printf (" dec "); mpf_out_str (stdout, 10, 0, f); printf ("\n"); printf (" hex "); mpf_out_str (stdout, 16, 0, f); printf ("\n"); printf (" size %ld\n", (long) SIZ(f)); printf (" exp %ld\n", (long) EXP(f)); printf (" got %ld (0x%lX)\n", got, got); printf (" want %ld (0x%lX)\n", data[i].want, data[i].want); abort(); } } mpf_clear (f); }
VOID PreCachePachinko(VOID) { PreCacheRange(618,623); PreCacheRange(618,623); PreCacheRange(4768,4790); PreCacheRange(4792,4814); PreCacheRange(4816,4838); PreCacheRange(4840,4863); PreCacheSoundList(Pachinko_SCTable, SIZ(Pachinko_SCTable)); }
void mpz_cdiv_q (mpz_ptr quot, mpz_srcptr dividend, mpz_srcptr divisor) { mp_size_t dividend_size = SIZ (dividend); mp_size_t divisor_size = SIZ (divisor); mpz_t rem; TMP_DECL; TMP_MARK; MPZ_TMP_INIT (rem, ABS (divisor_size)); mpz_tdiv_qr (quot, rem, dividend, divisor); if ((divisor_size ^ dividend_size) >= 0 && SIZ (rem) != 0) mpz_add_ui (quot, quot, 1L); TMP_FREE; }
void mpfr_rand_raw (mp_ptr mp, gmp_randstate_t rstate, unsigned long int nbits) { mpz_t z; /* To be sure to avoid the potential allocation of mpz_urandomb */ ALLOC(z) = SIZ(z) = (nbits / GMP_NUMB_BITS) + 1; PTR(z) = mp; mpz_urandomb(z, rstate, nbits); }
mpz_cmp (mpz_srcptr u, mpz_srcptr v) #endif { mp_size_t usize, vsize, dsize, asize; mp_srcptr up, vp; int cmp; usize = SIZ(u); vsize = SIZ(v); dsize = usize - vsize; if (dsize != 0) return dsize; asize = ABS (usize); up = PTR(u); vp = PTR(v); MPN_CMP (cmp, up, vp, asize); return (usize >= 0 ? cmp : -cmp); }
void mpz_init_set_n (mpz_ptr z, mp_srcptr p, mp_size_t size) { ASSERT (size >= 0); MPN_NORMALIZE (p, size); ALLOC(z) = MAX (size, 1); PTR(z) = __GMP_ALLOCATE_FUNC_LIMBS (ALLOC(z)); SIZ(z) = size; MPN_COPY (PTR(z), p, size); }
double mpz_get_d (mpz_srcptr z) { mp_size_t size; size = SIZ (z); if (UNLIKELY (size == 0)) return 0.0; return mpn_get_d (PTR (z), ABS (size), size, 0L); }
void mpq_abs (mpq_ptr dst, mpq_srcptr src) { mp_size_t num_abs_size = ABSIZ(NUM(src)); if (dst != src) { mp_size_t den_size = SIZ(DEN(src)); mp_ptr dp; dp = MPZ_NEWALLOC (NUM(dst), num_abs_size); MPN_COPY (dp, PTR(NUM(src)), num_abs_size); dp = MPZ_NEWALLOC (DEN(dst), den_size); SIZ(DEN(dst)) = den_size; MPN_COPY (dp, PTR(DEN(src)), den_size); } SIZ(NUM(dst)) = num_abs_size; }
int mpz_si_kronecker (long a, mpz_srcptr b) { mp_srcptr b_ptr; mp_limb_t b_low; mp_size_t b_size; mp_size_t b_abs_size; mp_limb_t a_limb, b_rem; unsigned twos; int result_bit1; #if GMP_NUMB_BITS < BITS_PER_ULONG if (a > GMP_NUMB_MAX || a < -GMP_NUMB_MAX) { mp_limb_t alimbs[2]; mpz_t az; ALLOC(az) = numberof (alimbs); PTR(az) = alimbs; mpz_set_si (az, a); return mpz_kronecker (az, b); } #endif b_size = SIZ (b); if (b_size == 0) return JACOBI_S0 (a); /* (a/0) */ /* account for the effect of the sign of b, then ignore it */ result_bit1 = JACOBI_BSGN_SS_BIT1 (a, b_size); b_ptr = PTR(b); b_low = b_ptr[0]; b_abs_size = ABS (b_size); if ((b_low & 1) != 0) { /* b odd */ result_bit1 ^= JACOBI_ASGN_SU_BIT1 (a, b_low); a_limb = (unsigned long) ABS(a); if ((a_limb & 1) == 0) { /* (0/b)=1 for b=+/-1, 0 otherwise */ if (a_limb == 0) return (b_abs_size == 1 && b_low == 1); /* a even, b odd */ count_trailing_zeros (twos, a_limb); a_limb >>= twos; /* (a*2^n/b) = (a/b) * twos(n,a) */ result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, b_low); } }
mpz_srcptr mpz_roinit_n (mpz_ptr x, mp_srcptr xp, mp_size_t xs) { mp_size_t xn = ABS(xs); MPN_NORMALIZE (xp, xn); ALLOC (x) = 0; SIZ (x) = xs < 0 ? -xn : xn; PTR (x) = (mp_ptr) xp; return x; }