int main (void) { unsigned long n; gmp_randstate_t rands; int j, k, i, i1; mp_limb_t sp[10000], tp[10000], xp[10000], yp[10000], zp[10000]; tests_start (); gmp_randinit_default(rands); for (i1 = 0; i1 < 2; i1++) { for (n = 1; n < 100; n++) { for (j = 1; j < 5; j++) { if (i1 == 0) { mpn_randomb (xp, rands, n); mpn_randomb (yp, rands, n); mpn_randomb (zp, rands, n); } else { mpn_rrandom (xp, rands, n); mpn_rrandom (yp, rands, n); mpn_rrandom (zp, rands, n); } k = mpn_addadd_n (sp, xp, yp, zp, n); i = mpn_add_n (tp, xp, yp, n); i += mpn_add_n (tp, tp, zp, n); if (k != i) { printf ("mpn_addadd_n ret wrong\n"); abort (); } if (mpn_cmp (sp, tp, n) != 0) { printf ("mpn_addadd_n sum wrong\n"); abort (); } } } } gmp_randclear(rands); tests_end (); exit (0); }
int main (void) { unsigned long bp, xn, n, b, zn, c; mp_limb_t xp[1000], yp[1000], mp[1000], lp[1000], hp[1000]; gmp_randstate_t rands; int qpn, j, k, i, l, i1, k1, j1, i2, k2, j2; tests_start (); gmp_randinit_default(rands); for (n = 1; n < 100; n++) { for (c = 0; c < 10; c++) { mpn_randomb (xp, rands, n); mpn_randomb (yp, rands, n); mpn_mul_n (mp, xp, yp, n); mpn_mullow_n (lp, xp, yp, n); mpn_mulhigh_n (hp, xp, yp, n); if (mpn_cmp (mp, lp, n) != 0) { printf ("mpn_mullow_n error %ld\n", n); abort (); } if (mpn_cmp (mp + n, hp + n, n) != 0) { printf ("mpn_mulhigh_n error %ld\n", n); abort (); } } } gmp_randclear(rands); tests_end (); exit (0); }
void check_rand (void) { gmp_randstate_t rands; int rep, i; unsigned long mant_bits; long exp, exp_min, exp_max; double got, want, d; mp_size_t nalloc, nsize, sign; mp_limb_t nhigh_mask; mp_ptr np; gmp_randinit_default(rands); mant_bits = tests_dbl_mant_bits (); if (mant_bits == 0) return; /* Allow for vax D format with exponent 127 to -128 only. FIXME: Do something to probe for a valid exponent range. */ exp_min = -100 - mant_bits; exp_max = 100 - mant_bits; /* space for mant_bits */ nalloc = BITS_TO_LIMBS (mant_bits); np = refmpn_malloc_limbs (nalloc); nhigh_mask = MP_LIMB_T_MAX >> (GMP_NAIL_BITS + nalloc * GMP_NUMB_BITS - mant_bits); for (rep = 0; rep < 200; rep++) { /* random exp_min to exp_max, inclusive */ exp = exp_min + (long) gmp_urandomm_ui (rands, exp_max - exp_min + 1); /* mant_bits worth of random at np */ if (rep & 1) mpn_randomb (np, rands, nalloc); else mpn_rrandom (np, rands,nalloc); nsize = nalloc; np[nsize-1] &= nhigh_mask; MPN_NORMALIZE (np, nsize); if (nsize == 0) continue; sign = (mp_size_t) gmp_urandomb_ui (rands, 1L) - 1; /* want = {np,nsize}, converting one bit at a time */ want = 0.0; for (i = 0, d = 1.0; i < mant_bits; i++, d *= 2.0) if (np[i/GMP_NUMB_BITS] & (CNST_LIMB(1) << (i%GMP_NUMB_BITS))) want += d; if (sign < 0) want = -want; /* want = want * 2^exp */ for (i = 0; i < exp; i++) want *= 2.0; for (i = 0; i > exp; i--) want *= 0.5; got = mpn_get_d (np, nsize, sign, exp); if (got != want) { printf ("mpn_get_d wrong on random data\n"); printf (" sign %ld\n", (long) sign); mpn_trace (" n ", np, nsize); printf (" nsize %ld\n", (long) nsize); printf (" exp %ld\n", exp); d_trace (" want ", want); d_trace (" got ", got); abort(); } } free (np); gmp_randclear(rands); }
int main (void) { unsigned long n, c; gmp_randstate_t rands; mp_limb_t xp[10000], qp[10000], tp[10000], r1, r2; tests_start (); gmp_randinit_default(rands); // where (xp,n) = (qp,n)*(B-1) -ret*B^n and 0 <= ret < B-1 B=2^GMP_NUMB_BITS // this assumes we use a divexact algorithm , a bi-directional algorithm would give different results for (n = 1; n < 100; n++) { for (c = 0; c < 10; c++) { mpn_randomb (xp, rands, n); r1 = mpn_divexact_byff (qp, xp, n); r2 = mpn_mul_1 (tp, qp, n, GMP_NUMB_MAX); if (r1 != r2) { printf ("mpn_divexact_byff ret error\n"); abort (); } if (mpn_cmp (xp, tp, n) != 0) { printf ("mpn_divexact_byff error\n"); abort (); } } } for (n = 2; n < 100; n++) { for (c = 0; c < 10; c++) { mpn_randomb (xp, rands, n); xp[n] = mpn_mul_1 (xp, xp, n - 1, GMP_NUMB_MAX); r1 = mpn_divexact_byff (qp, xp, n); r2 = mpn_mul_1 (tp, qp, n, GMP_NUMB_MAX); if (r1 != r2) { printf ("mpn_divexact_byff ret error\n"); abort (); } if (mpn_cmp (xp, tp, n) != 0) { printf ("mpn_divexact_byff error\n"); abort (); } } } for (n = 0; n < 100; n++) { umul_ppmm (xp[1], xp[0], GMP_NUMB_MAX, n); r1 = mpn_divexact_byff (qp, xp, 2); r2 = mpn_mul_1 (tp, qp, 2, GMP_NUMB_MAX); if (r1 != r2) { printf ("mpn_divexact_byff ret error\n"); abort (); } if (mpn_cmp (xp, tp, 2) != 0) { printf ("mpn_divexact_byff error\n"); abort (); } } gmp_randclear(rands); tests_end (); exit (0); }
int main (void) { gmp_randstate_t rands; int j, n; mp_limb_t cp1[1000], cp2[1000], mp[1000], tp1[1000], tp2[1000], inv; tests_start (); gmp_randinit_default (rands); for (n = 1; n < 100; n++) { for (j = 1; j < 100; j++) { mpn_randomb (mp, rands, n); mp[0] |= 1; modlimb_invert (inv, mp[0]); inv = -inv; mpn_randomb (tp1, rands, 2 * n); MPN_COPY (tp2, tp1, 2 * n); ref_redc_1 (cp1, tp1, mp, n, inv); mpn_redc_1 (cp2, tp2, mp, n, inv); if (mpn_cmp (cp1, cp2, n) != 0) { printf ("mpn_redc_1 error %d\n", n); abort (); } if (n != 1 && mpn_cmp (tp1, tp2, 2 * n) != 0) { printf ("mpn_redc_1 possible error\n"); abort (); } /* we dont require the above to be the same but it could be a useful test */ } } for (n = 1; n < 100; n++) { for (j = 1; j < 100; j++) { mpn_rrandom (mp, rands, n); mp[0] |= 1; modlimb_invert (inv, mp[0]); inv = -inv; mpn_rrandom (tp1, rands, 2 * n); MPN_COPY (tp2, tp1, 2 * n); ref_redc_1 (cp1, tp1, mp, n, inv); mpn_redc_1 (cp2, tp2, mp, n, inv); if (mpn_cmp (cp1, cp2, n) != 0) { printf ("mpn_redc_1 error %d\n", n); abort (); } if (n != 1 && mpn_cmp (tp1, tp2, 2 * n) != 0) { printf ("mpn_redc_1 possible error\n"); abort (); } /* we dont require the above to be the same but it could be a useful test */ } } gmp_randclear (rands); tests_end (); exit (0); }