xref: /netbsd-src/external/lgpl3/mpc/dist/tests/tpow_ui.c (revision 367b82799ab709709d3c3b541df56a2a14644d3e)
1 /* tpow_ui -- test file for mpc_pow_ui.
2 
3 Copyright (C) 2009, 2010, 2012, 2013 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
compare_mpc_pow(mpfr_prec_t pmax,int iter,unsigned long nbits)25 compare_mpc_pow (mpfr_prec_t pmax, int iter, unsigned long nbits)
26 {
27   mpfr_prec_t p;
28   mpc_t x, y, z, t;
29   unsigned long n;
30   int i, inex_pow, inex_pow_ui;
31   mpc_rnd_t rnd1, rnd2, rnd;
32 
33   mpc_init3 (y, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN);
34   for (p = MPFR_PREC_MIN; p <= pmax; p++)
35     for (i = 0; i < iter; i++)
36       {
37         mpc_init2 (x, p);
38         mpc_init2 (z, p);
39         mpc_init2 (t, p);
40         mpc_urandom (x, rands);
41         n = gmp_urandomb_ui (rands, nbits); /* 0 <= n < 2^nbits */
42         mpc_set_ui (y, n, MPC_RNDNN);
43         for (rnd1 = 0; rnd1 < 4; rnd1 ++)
44           for (rnd2 = 0; rnd2 < 4; rnd2 ++)
45           {
46             rnd = MPC_RND (rnd1, rnd2);
47             inex_pow = mpc_pow (z, x, y, rnd);
48             inex_pow_ui = mpc_pow_ui (t, x, n, rnd);
49             if (mpc_cmp (z, t) != 0)
50               {
51                 printf ("mpc_pow and mpc_pow_ui differ for x=");
52                 mpc_out_str (stdout, 16, 0, x, MPC_RNDNN);
53                 printf (" n=%li p=%lu rnd=%d\n", n, p, rnd);
54                 printf ("mpc_pow gives ");
55                 mpc_out_str (stdout, 16, 0, z, MPC_RNDNN);
56                 printf ("\nmpc_pow_ui gives ");
57                 mpc_out_str (stdout, 16, 0, t, MPC_RNDNN);
58                 printf ("\n");
59                 exit (1);
60               }
61             if (inex_pow != inex_pow_ui)
62               {
63                 printf ("mpc_pow and mpc_pow_ui give different flags for x=");
64                 mpc_out_str (stdout, 16, 0, x, MPC_RNDNN);
65                 printf (" n=%li p=%lu rnd=%d\n", n, p, rnd);
66                 printf ("mpc_pow gives %d\n", inex_pow);
67                 printf ("mpc_pow_ui gives %d\n", inex_pow_ui);
68                 exit (1);
69               }
70           }
71         mpc_clear (x);
72         mpc_clear (z);
73         mpc_clear (t);
74       }
75   mpc_clear (y);
76 }
77 
78 #define MPC_FUNCTION_CALL                                               \
79   P[0].mpc_inex = mpc_pow_ui (P[1].mpc, P[2].mpc, P[3].ui, P[4].mpc_rnd)
80 #define MPC_FUNCTION_CALL_REUSE_OP1                                     \
81   P[0].mpc_inex = mpc_pow_ui (P[1].mpc, P[1].mpc, P[3].ui, P[4].mpc_rnd)
82 
83 #include "data_check.tpl"
84 
85 int
main(int argc,char * argv[])86 main (int argc, char *argv[])
87 {
88   mpc_t z;
89 
90   if (argc != 1)
91     {
92       mpfr_prec_t p;
93       long int n, k;
94       mpc_t res;
95       if (argc != 3 && argc != 4)
96         {
97           printf ("Usage: tpow_ui precision exponent [k]\n");
98           exit (1);
99         }
100       p = atoi (argv[1]);
101       n = atoi (argv[2]);
102       MPC_ASSERT (n >= 0);
103       k = (argc > 3) ? atoi (argv[3]) : 1;
104       MPC_ASSERT (k >= 0);
105       mpc_init2 (z, p);
106       mpc_init2 (res, p);
107       mpfr_const_pi (mpc_realref (z), MPFR_RNDN);
108       mpfr_div_2exp (mpc_realref (z), mpc_realref (z), 2, MPFR_RNDN);
109       mpfr_const_log2 (mpc_imagref (z), MPFR_RNDN);
110       while (k--)
111         mpc_pow_ui (res, z, (unsigned long int) n, MPC_RNDNN);
112       mpc_clear (z);
113       mpc_clear (res);
114       return 0;
115     }
116 
117   test_start ();
118 
119   data_check_template ("pow_ui.dsc", "pow_ui.dat");
120 
121   compare_mpc_pow (100, 5, 19);
122 
123   test_end ();
124 
125   return 0;
126 }
127