xref: /netbsd-src/external/lgpl3/gmp/dist/mini-gmp/tests/t-div_2exp.c (revision 19ef5b5b0bcb90f63509df6e78769de1b57c2758)
1 /*
2 
3 Copyright 2012, Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see http://www.gnu.org/licenses/.  */
19 
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 
24 #include "testutils.h"
25 
26 #define MAXBITS 400
27 #define COUNT 10000
28 
29 static void
30 dump (const char *label, const mpz_t x)
31 {
32   char *buf = mpz_get_str (NULL, 16, x);
33   fprintf (stderr, "%s: %s\n", label, buf);
34   testfree (buf);
35 }
36 
37 typedef void div_func (mpz_t, const mpz_t, mp_bitcnt_t);
38 
39 void
40 testmain (int argc, char **argv)
41 {
42   unsigned i;
43   mpz_t a, res, ref;
44   mp_bitcnt_t b;
45 
46   mpz_init (a);
47   mpz_init (res);
48   mpz_init (ref);
49 
50   for (i = 0; i < COUNT; i++)
51     {
52       unsigned j;
53       for (j = 0; j < 6; j++)
54 	{
55 	  static const enum hex_random_op ops[6] =
56 	    {
57 	      OP_CDIV_Q_2, OP_CDIV_R_2,
58 	      OP_FDIV_Q_2, OP_FDIV_R_2,
59 	      OP_TDIV_Q_2, OP_TDIV_R_2
60 	    };
61 	  static const char *name[6] =
62 	    {
63 	      "cdiv_q", "cdiv_r",
64 	      "fdiv_q", "fdiv_r",
65 	      "tdiv_q", "tdiv_r"
66 	    };
67 	  static div_func * const div [6] =
68 	    {
69 	      mpz_cdiv_q_2exp, mpz_cdiv_r_2exp,
70 	      mpz_fdiv_q_2exp, mpz_fdiv_r_2exp,
71 	      mpz_tdiv_q_2exp, mpz_tdiv_r_2exp
72 	    };
73 
74 	  mini_random_bit_op (ops[j], MAXBITS, a, &b, ref);
75 	  div[j] (res, a, b);
76 	  if (mpz_cmp (ref, res))
77 	    {
78 	      fprintf (stderr, "mpz_%s_2exp failed:\n", name[j]);
79 	      dump ("a", a);
80 	      fprintf (stderr, "b: %lu\n", b);
81 	      dump ("r", res);
82 	      dump ("ref", ref);
83 	      abort ();
84 	    }
85 	}
86     }
87   mpz_clear (a);
88   mpz_clear (res);
89   mpz_clear (ref);
90 }
91