xref: /netbsd-src/external/lgpl3/mpc/dist/tests/tpow_si.c (revision e670fd5c413e99c2f6a37901bb21c537fcd322d2)
1 /* tpow_si -- test file for mpc_pow_si.
2 
3 Copyright (C) 2009, 2010, 2011, 2012 INRIA
4 
5 This file is part of GNU MPC.
6 
7 GNU MPC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see http://www.gnu.org/licenses/ .
19 */
20 
21 #include <limits.h> /* for CHAR_BIT */
22 #include "mpc-tests.h"
23 
24 static void
25 compare_mpc_pow (mpfr_prec_t pmax, int iter, unsigned long nbits)
26    /* copied from tpow_ui.c and replaced unsigned by signed */
27 {
28   mpfr_prec_t p;
29   mpc_t x, y, z, t;
30   long n;
31   int i, inex_pow, inex_pow_si;
32   mpc_rnd_t rnd;
33 
34   mpc_init3 (y, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN);
35   for (p = MPFR_PREC_MIN; p <= pmax; p++)
36     for (i = 0; i < iter; i++)
37       {
38         mpc_init2 (x, p);
39         mpc_init2 (z, p);
40         mpc_init2 (t, p);
41         mpc_urandom (x, rands);
42         n = (signed long) gmp_urandomb_ui (rands, nbits);
43         mpc_set_si (y, n, MPC_RNDNN);
44         for (rnd = 0; rnd < 16; rnd ++)
45           {
46             inex_pow = mpc_pow (z, x, y, rnd);
47             inex_pow_si = mpc_pow_si (t, x, n, rnd);
48             if (mpc_cmp (z, t) != 0)
49               {
50                 printf ("mpc_pow and mpc_pow_si differ for x=");
51                 mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
52                 printf (" n=%li\n", n);
53                 printf ("mpc_pow gives ");
54                 mpc_out_str (stdout, 10, 0, z, MPC_RNDNN);
55                 printf ("\nmpc_pow_si gives ");
56                 mpc_out_str (stdout, 10, 0, t, MPC_RNDNN);
57                 printf ("\n");
58                 exit (1);
59               }
60             if (inex_pow != inex_pow_si)
61               {
62                 printf ("mpc_pow and mpc_pow_si give different flags for x=");
63                 mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
64                 printf (" n=%li\n", n);
65                 printf ("mpc_pow gives %d\n", inex_pow);
66                 printf ("mpc_pow_si gives %d\n", inex_pow_si);
67                 exit (1);
68               }
69           }
70         mpc_clear (x);
71         mpc_clear (z);
72         mpc_clear (t);
73       }
74   mpc_clear (y);
75 }
76 
77 #define MPC_FUNCTION_CALL                                               \
78   P[0].mpc_inex = mpc_pow_si (P[1].mpc, P[2].mpc, P[3].si, P[4].mpc_rnd)
79 #define MPC_FUNCTION_CALL_REUSE_OP1                                     \
80   P[0].mpc_inex = mpc_pow_si (P[1].mpc, P[1].mpc, P[3].si, P[4].mpc_rnd)
81 
82 #include "data_check.tpl"
83 
84 int
85 main (void)
86 {
87   test_start ();
88 
89   data_check_template ("pow_si.dsc", "pow_si.dat");
90 
91   compare_mpc_pow (100, 5, 19);
92 
93   test_end ();
94 
95   return 0;
96 }
97