int main (int argc, char **argv) { mp_ptr ap, bp, rp, refp; gmp_randstate_ptr rands; int test; TMP_DECL; TMP_MARK; tests_start (); rands = RANDS; ap = TMP_ALLOC_LIMBS (MAX_N); bp = TMP_ALLOC_LIMBS (MAX_N); rp = TMP_ALLOC_LIMBS (MAX_N + 2); refp = TMP_ALLOC_LIMBS (MAX_N + 2); for (test = 0; test < COUNT; test++) { mp_size_t an, bn, rn; unsigned size_log; size_log = 1 + gmp_urandomm_ui (rands, SIZE_LOG); an = 1 + gmp_urandomm_ui(rands, 1L << size_log); size_log = 1 + gmp_urandomm_ui (rands, SIZE_LOG); bn = 1 + gmp_urandomm_ui(rands, 1L << size_log); /* Make sure an >= bn */ if (an < bn) MP_SIZE_T_SWAP (an, bn); mpn_random2 (ap, an); mpn_random2 (bp, bn); refmpn_mulmid (refp, ap, an, bp, bn); mpn_mulmid (rp, ap, an, bp, bn); rn = an + 3 - bn; if (mpn_cmp (refp, rp, rn)) { printf ("ERROR in test %d, an = %d, bn = %d, rn = %d\n", test, (int) an, (int) bn, (int) rn); printf("a: "); mpn_dump (ap, an); printf("b: "); mpn_dump (bp, bn); printf("r: "); mpn_dump (rp, rn); printf("ref: "); mpn_dump (refp, rn); abort(); } } TMP_FREE; tests_end (); return 0; }
void check_one (mp_srcptr refp, mp_srcptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n, char *funcname) { if (mpn_cmp (refp, rp, n)) { printf ("ERROR in mpn_%s\n", funcname); printf ("a: "); mpn_dump (ap, n); printf ("b: "); mpn_dump (bp, n); printf ("r: "); mpn_dump (rp, n); printf ("ref: "); mpn_dump (refp, n); abort(); } }
static void check_one (mp_srcptr ap, mp_size_t n, mp_limb_t b) { mp_limb_t r_ref = refmpn_mod_1 (ap, n, b); mp_limb_t r; if (n >= 2) { mp_limb_t pre[4]; mpn_mod_1_1p_cps (pre, b); r = mpn_mod_1_1p (ap, n, b << pre[1], pre); if (r != r_ref) { printf ("mpn_mod_1_1p failed\n"); goto fail; } } if ((b & GMP_NUMB_HIGHBIT) == 0) { mp_limb_t pre[5]; mpn_mod_1s_2p_cps (pre, b); r = mpn_mod_1s_2p (ap, n, b << pre[1], pre); if (r != r_ref) { printf ("mpn_mod_1s_2p failed\n"); goto fail; } } if (b <= GMP_NUMB_MASK / 3) { mp_limb_t pre[6]; mpn_mod_1s_3p_cps (pre, b); r = mpn_mod_1s_3p (ap, n, b << pre[1], pre); if (r != r_ref) { printf ("mpn_mod_1s_3p failed\n"); goto fail; } } if (b <= GMP_NUMB_MASK / 4) { mp_limb_t pre[7]; mpn_mod_1s_4p_cps (pre, b); r = mpn_mod_1s_4p (ap, n, b << pre[1], pre); if (r != r_ref) { printf ("mpn_mod_1s_4p failed\n"); goto fail; } } r = mpn_mod_1 (ap, n, b); if (r != r_ref) { printf ("mpn_mod_1 failed\n"); fail: printf ("an = %d, a: ", (int) n); mpn_dump (ap, n); printf ("b : "); mpn_dump (&b, 1); printf ("r (expected): "); mpn_dump (&r_ref, 1); printf ("r (bad) : "); mpn_dump (&r, 1); abort(); } }
int main (int argc, char **argv) { mp_ptr ip, dp, scratch; int count = COUNT; int test; gmp_randstate_ptr rands; TMP_DECL; TMP_MARK; if (argc > 1) { char *end; count = strtol (argv[1], &end, 0); if (*end || count <= 0) { fprintf (stderr, "Invalid test count: %s.\n", argv[1]); return 1; } } tests_start (); rands = RANDS; dp = TMP_ALLOC_LIMBS (MAX_N); ip = 1+TMP_ALLOC_LIMBS (MAX_N + 2); scratch = 1+TMP_ALLOC_LIMBS (mpn_invert_itch (MAX_N) + 2); for (test = 0; test < count; test++) { unsigned size_min; unsigned size_range; mp_size_t n; mp_size_t itch; mp_limb_t i_before, i_after, s_before, s_after; for (size_min = 1; (1L << size_min) < MIN_N; size_min++) ; /* We generate an in the MIN_N <= n <= (1 << size_range). */ size_range = size_min + gmp_urandomm_ui (rands, SIZE_LOG + 1 - size_min); n = MIN_N + gmp_urandomm_ui (rands, (1L << size_range) + 1 - MIN_N); mpn_random2 (dp, n); mpn_random2 (ip-1, n + 2); i_before = ip[-1]; i_after = ip[n]; itch = mpn_invert_itch (n); ASSERT_ALWAYS (itch <= mpn_invert_itch (MAX_N)); mpn_random2 (scratch-1, itch+2); s_before = scratch[-1]; s_after = scratch[itch]; dp[n-1] |= GMP_NUMB_HIGHBIT; mpn_invert (ip, dp, n, scratch); if (ip[-1] != i_before || ip[n] != i_after || scratch[-1] != s_before || scratch[itch] != s_after || ! invert_valid(ip, dp, n)) { printf ("ERROR in test %d, n = %d\n", test, (int) n); if (ip[-1] != i_before) { printf ("before ip:"); mpn_dump (ip -1, 1); printf ("keep: "); mpn_dump (&i_before, 1); } if (ip[n] != i_after) { printf ("after ip:"); mpn_dump (ip + n, 1); printf ("keep: "); mpn_dump (&i_after, 1); } if (scratch[-1] != s_before) { printf ("before scratch:"); mpn_dump (scratch-1, 1); printf ("keep: "); mpn_dump (&s_before, 1); } if (scratch[itch] != s_after) { printf ("after scratch:"); mpn_dump (scratch + itch, 1); printf ("keep: "); mpn_dump (&s_after, 1); } mpn_dump (dp, n); mpn_dump (ip, n); abort(); } } TMP_FREE; tests_end (); return 0; }
int main (int argc, char **argv) { mp_ptr ap, bp, refp, pp, scratch; int count = COUNT; int test; gmp_randstate_ptr rands; TMP_DECL; TMP_MARK; if (argc > 1) { char *end; count = strtol (argv[1], &end, 0); if (*end || count <= 0) { fprintf (stderr, "Invalid test count: %s.\n", argv[1]); return 1; } } tests_start (); rands = RANDS; #define mpn_mullo_itch(n) (0) ap = TMP_ALLOC_LIMBS (MAX_N); bp = TMP_ALLOC_LIMBS (MAX_N); refp = TMP_ALLOC_LIMBS (MAX_N * 2); pp = 1+TMP_ALLOC_LIMBS (MAX_N + 2); scratch = 1+TMP_ALLOC_LIMBS (mpn_mullo_itch (MAX_N) + 2); for (test = 0; test < count; test++) { unsigned size_min; unsigned size_range; mp_size_t n; mp_size_t itch; mp_limb_t p_before, p_after, s_before, s_after; for (size_min = 1; (1L << size_min) < MIN_N; size_min++) ; /* We generate an in the MIN_N <= n <= (1 << size_range). */ size_range = size_min + gmp_urandomm_ui (rands, SIZE_LOG + 1 - size_min); n = MIN_N + gmp_urandomm_ui (rands, (1L << size_range) + 1 - MIN_N); mpn_random2 (ap, n); mpn_random2 (bp, n); mpn_random2 (pp-1, n + 2); p_before = pp[-1]; p_after = pp[n]; itch = mpn_mullo_itch (n); ASSERT_ALWAYS (itch <= mpn_mullo_itch (MAX_N)); mpn_random2 (scratch-1, itch+2); s_before = scratch[-1]; s_after = scratch[itch]; mpn_mullo_n (pp, ap, bp, n); mpn_mul_n (refp, ap, bp, n); if (pp[-1] != p_before || pp[n] != p_after || scratch[-1] != s_before || scratch[itch] != s_after || mpn_cmp (refp, pp, n) != 0) { printf ("ERROR in test %d, n = %d", test, (int) n); if (pp[-1] != p_before) { printf ("before pp:"); mpn_dump (pp -1, 1); printf ("keep: "); mpn_dump (&p_before, 1); } if (pp[n] != p_after) { printf ("after pp:"); mpn_dump (pp + n, 1); printf ("keep: "); mpn_dump (&p_after, 1); } if (scratch[-1] != s_before) { printf ("before scratch:"); mpn_dump (scratch-1, 1); printf ("keep: "); mpn_dump (&s_before, 1); } if (scratch[itch] != s_after) { printf ("after scratch:"); mpn_dump (scratch + itch, 1); printf ("keep: "); mpn_dump (&s_after, 1); } mpn_dump (ap, n); mpn_dump (bp, n); mpn_dump (pp, n); mpn_dump (refp, n); abort(); } } TMP_FREE; tests_end (); return 0; }