xref: /netbsd-src/external/lgpl3/mpc/dist/tests/tpow_ui.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
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
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 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 (rnd = 0; rnd < 16; rnd ++)
44           {
45             inex_pow = mpc_pow (z, x, y, rnd);
46             inex_pow_ui = mpc_pow_ui (t, x, n, rnd);
47             if (mpc_cmp (z, t) != 0)
48               {
49                 printf ("mpc_pow and mpc_pow_ui differ for x=");
50                 mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
51                 printf (" n=%lu\n", n);
52                 printf ("mpc_pow gives ");
53                 mpc_out_str (stdout, 10, 0, z, MPC_RNDNN);
54                 printf ("\nmpc_pow_ui gives ");
55                 mpc_out_str (stdout, 10, 0, t, MPC_RNDNN);
56                 printf ("\n");
57                 exit (1);
58               }
59             if (inex_pow != inex_pow_ui)
60               {
61                 printf ("mpc_pow and mpc_pow_ui give different flags for x=");
62                 mpc_out_str (stdout, 10, 0, x, MPC_RNDNN);
63                 printf (" n=%lu\n", n);
64                 printf ("mpc_pow gives %d\n", inex_pow);
65                 printf ("mpc_pow_ui gives %d\n", inex_pow_ui);
66                 exit (1);
67               }
68           }
69         mpc_clear (x);
70         mpc_clear (z);
71         mpc_clear (t);
72       }
73   mpc_clear (y);
74 }
75 
76 #define MPC_FUNCTION_CALL                                               \
77   P[0].mpc_inex = mpc_pow_ui (P[1].mpc, P[2].mpc, P[3].ui, P[4].mpc_rnd)
78 #define MPC_FUNCTION_CALL_REUSE_OP1                                     \
79   P[0].mpc_inex = mpc_pow_ui (P[1].mpc, P[1].mpc, P[3].ui, P[4].mpc_rnd)
80 
81 #include "data_check.tpl"
82 
83 int
84 main (int argc, char *argv[])
85 {
86   mpc_t z;
87 
88   if (argc != 1)
89     {
90       mpfr_prec_t p;
91       long int n, k;
92       mpc_t res;
93       if (argc != 3 && argc != 4)
94         {
95           printf ("Usage: tpow_ui precision exponent [k]\n");
96           exit (1);
97         }
98       p = atoi (argv[1]);
99       n = atoi (argv[2]);
100       MPC_ASSERT (n >= 0);
101       k = (argc > 3) ? atoi (argv[3]) : 1;
102       MPC_ASSERT (k >= 0);
103       mpc_init2 (z, p);
104       mpc_init2 (res, p);
105       mpfr_const_pi (mpc_realref (z), MPFR_RNDN);
106       mpfr_div_2exp (mpc_realref (z), mpc_realref (z), 2, MPFR_RNDN);
107       mpfr_const_log2 (mpc_imagref (z), MPFR_RNDN);
108       while (k--)
109         mpc_pow_ui (res, z, (unsigned long int) n, MPC_RNDNN);
110       mpc_clear (z);
111       mpc_clear (res);
112       return 0;
113     }
114 
115   test_start ();
116 
117   data_check_template ("pow_ui.dsc", "pow_ui.dat");
118 
119   compare_mpc_pow (100, 5, 19);
120 
121   test_end ();
122 
123   return 0;
124 }
125