1 /* Test mpn_addaddmul_1msb0.
2
3 Copyright 2021 Free Software Foundation,
4 Inc.
5
6 This file is part of the GNU MP Library test suite.
7
8 The GNU MP Library test suite is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 3 of the License,
11 or (at your option) any later version.
12
13 The GNU MP Library test suite is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "gmp-impl.h"
25 #include "tests.h"
26
27 #if !HAVE_NATIVE_mpn_addaddmul_1msb0
28 int main(int argc, char **argv) {
29 return 77; /* Test driver "SKIP" */
30 }
31 #else
32
33 static void
34 one_test (int i, mp_srcptr a, mp_srcptr b, mp_size_t n, mp_limb_t u, mp_limb_t v)
35 {
36 mp_ptr r = refmpn_malloc_limbs (n + 1);
37 mp_ptr ref = refmpn_malloc_limbs (n + 1);
38
39 u &= ~GMP_NUMB_HIGHBIT;
40 v &= ~GMP_NUMB_HIGHBIT;
41 ref[n] = mpn_mul_1 (ref, a, n, u);
42 ref[n] += mpn_addmul_1 (ref, b, n, v);
43 r[n] = mpn_addaddmul_1msb0 (r, a, b, n, u, v);
44
45 if (mpn_cmp (r, ref, n+1) != 0)
46 {
47 fprintf (stderr, "ERROR in test %d\n", i);
48 fprintf (stderr, "Bad result from addaddmul_1msb0\n");
49 gmp_fprintf (stderr, "op1=%Nx\n", a, n);
50 gmp_fprintf (stderr, "op2=%Nx\n", b, n);
51 gmp_fprintf (stderr, "u = %Mx, v = %Mx\n", u, v);
52 gmp_fprintf (stderr, "res=%Nx\n", r, n + 1);
53 gmp_fprintf (stderr, "ref=%Nx\n", ref, n + 1);
54
55 abort();
56 }
57 }
58
59 int main (int argc, char **argv)
60 {
61 mpz_t op1, op2;
62 int i;
63 gmp_randstate_ptr rands;
64 mpz_t bs;
65
66 tests_start ();
67 rands = RANDS;
68
69 mpz_inits (bs, op1, op2, NULL);
70
71 for (i = 0; i < 10000; i++)
72 {
73 unsigned long size_range;
74 mp_size_t bit_size;
75 mp_size_t limb_size;
76 mp_limb_t u, v;
77
78 mpz_urandomb (bs, rands, 32);
79 size_range = mpz_get_ui (bs) % 10 + 2;
80 mpz_urandomb (bs, rands, size_range);
81
82 bit_size = mpz_get_ui (bs) + 10;
83 mpz_rrandomb (op1, rands, bit_size);
84 mpz_rrandomb (op2, rands, bit_size);
85
86 mpz_rrandomb (bs, rands, GMP_NUMB_BITS - 1);
87 u = mpz_getlimbn (bs, 0);
88
89 mpz_rrandomb (bs, rands, GMP_NUMB_BITS - 1);
90 v = mpz_getlimbn (bs, 0);
91
92 limb_size = mpz_size (op1);
93 one_test (i, mpz_limbs_read (op1), mpz_limbs_read(op2), limb_size, u, v);
94 }
95 mpz_clears (bs, op1, op2, NULL);
96 return 0;
97 }
98 #endif