summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2021-10-08 16:05:05 +0200
committerNiels Möller <nisse@lysator.liu.se>2021-10-08 16:05:05 +0200
commitfcaa7231f9b26044ba21cf06bdb59ccfbdbf7807 (patch)
tree26c1a3de0c822efb64b11d475dc9bfe7d70ef5a2 /tests
parent2d4d5a474a3b056bba4a4166b4f2fbdb271f59ce (diff)
downloadgmp-fcaa7231f9b26044ba21cf06bdb59ccfbdbf7807.tar.gz
Unit test for mpn_addaddmul_1msb0.
Diffstat (limited to 'tests')
-rw-r--r--tests/mpn/Makefile.am2
-rw-r--r--tests/mpn/t-addaddmul.c98
2 files changed, 99 insertions, 1 deletions
diff --git a/tests/mpn/Makefile.am b/tests/mpn/Makefile.am
index dafeea8d5..7e370b68c 100644
--- a/tests/mpn/Makefile.am
+++ b/tests/mpn/Makefile.am
@@ -28,7 +28,7 @@ check_PROGRAMS = t-asmtype t-aors_1 t-divrem_1 t-mod_1 t-fat t-get_d \
t-toom52 t-toom53 t-toom54 t-toom62 t-toom63 t-toom6h t-toom8h \
t-toom2-sqr t-toom3-sqr t-toom4-sqr t-toom6-sqr t-toom8-sqr \
t-div t-mul t-mullo t-sqrlo t-mulmod_bnm1 t-sqrmod_bnm1 t-mulmid \
- t-hgcd t-hgcd_appr t-matrix22 t-invert t-bdiv t-fib2m \
+ t-addaddmul t-hgcd t-hgcd_appr t-matrix22 t-invert t-bdiv t-fib2m \
t-broot t-brootinv t-minvert t-sizeinbase t-gcd_11 t-gcd_22 t-gcdext_1
EXTRA_DIST = toom-shared.h toom-sqr-shared.h
diff --git a/tests/mpn/t-addaddmul.c b/tests/mpn/t-addaddmul.c
new file mode 100644
index 000000000..8d3b0da2c
--- /dev/null
+++ b/tests/mpn/t-addaddmul.c
@@ -0,0 +1,98 @@
+/* Test mpn_addaddmul_1msb0.
+
+Copyright 2021 Free Software Foundation,
+Inc.
+
+This file is part of the GNU MP Library test suite.
+
+The GNU MP Library test suite 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 3 of the License,
+or (at your option) any later version.
+
+The GNU MP Library test suite 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
+the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "gmp-impl.h"
+#include "tests.h"
+
+#if !HAVE_NATIVE_mpn_addaddmul_1msb0
+int main(int argc, char **argv) {
+ return 77; /* Test driver "SKIP" */
+}
+#else
+
+static void
+one_test (int i, mp_srcptr a, mp_srcptr b, mp_size_t n, mp_limb_t u, mp_limb_t v)
+{
+ mp_ptr r = refmpn_malloc_limbs (n + 1);
+ mp_ptr ref = refmpn_malloc_limbs (n + 1);
+
+ u &= ~GMP_NUMB_HIGHBIT;
+ v &= ~GMP_NUMB_HIGHBIT;
+ ref[n] = mpn_mul_1 (ref, a, n, u);
+ ref[n] += mpn_addmul_1 (ref, b, n, v);
+ r[n] = mpn_addaddmul_1msb0 (r, a, b, n, u, v);
+
+ if (mpn_cmp (r, ref, n+1) != 0)
+ {
+ fprintf (stderr, "ERROR in test %d\n", i);
+ fprintf (stderr, "Bad result from addaddmul_1msb0\n");
+ gmp_fprintf (stderr, "op1=%Nx\n", a, n);
+ gmp_fprintf (stderr, "op2=%Nx\n", b, n);
+ gmp_fprintf (stderr, "u = %Mx, v = %Mx\n", u, v);
+ gmp_fprintf (stderr, "res=%Nx\n", r, n + 1);
+ gmp_fprintf (stderr, "ref=%Nx\n", ref, n + 1);
+
+ abort();
+ }
+}
+
+int main (int argc, char **argv)
+{
+ mpz_t op1, op2;
+ int i;
+ gmp_randstate_ptr rands;
+ mpz_t bs;
+
+ tests_start ();
+ rands = RANDS;
+
+ mpz_inits (bs, op1, op2, NULL);
+
+ for (i = 0; i < 10000; i++)
+ {
+ unsigned long size_range;
+ mp_size_t bit_size;
+ mp_size_t limb_size;
+ mp_limb_t u, v;
+
+ mpz_urandomb (bs, rands, 32);
+ size_range = mpz_get_ui (bs) % 10 + 2;
+ mpz_urandomb (bs, rands, size_range);
+
+ bit_size = mpz_get_ui (bs) + 10;
+ mpz_rrandomb (op1, rands, bit_size);
+ mpz_rrandomb (op2, rands, bit_size);
+
+ mpz_rrandomb (bs, rands, GMP_NUMB_BITS - 1);
+ u = mpz_getlimbn (bs, 0);
+
+ mpz_rrandomb (bs, rands, GMP_NUMB_BITS - 1);
+ v = mpz_getlimbn (bs, 0);
+
+ limb_size = mpz_size (op1);
+ one_test (i, mpz_limbs_read (op1), mpz_limbs_read(op2), limb_size, u, v);
+ }
+ mpz_clears (bs, op1, op2, NULL);
+ return 0;
+}
+#endif