xref: /netbsd-src/external/lgpl3/mpc/dist/tools/mpcheck/mpcheck-longdouble.c (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1 /* mpcheck-longdouble -- compare mpc functions against "long double complex"
2                          from the GNU libc implementation
3 
4 Copyright (C) 2020 INRIA
5 
6 This file is part of GNU MPC.
7 
8 GNU MPC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with this program. If not, see http://www.gnu.org/licenses/ .
20 */
21 
22 /* the GNU libc provides the following functions (as of 2.31),
23    with 'f' suffix for the float/binary32 version, with no suffix
24    for the double/binary64 version, with 'l' suffix for the long double
25    version, and with 'f128' suffix for the __float128 version:
26 
27    cabs     casinh    cexp      csinh
28    cacos    catan     clog      csqrt
29    cacosh   catanh    clog10    ctan
30    carg     ccos      cpow      ctanh
31    casin    ccosh     csin
32 */
33 
34 #define _GNU_SOURCE /* for clog10 */
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <complex.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 #include <assert.h>
42 #include "mpc.h"
43 #ifdef __GNUC__
44 #include <gnu/libc-version.h>
45 #endif
46 
47 #define PRECISION 64
48 #define EMAX 16384
49 #define TYPE long double
50 #define SUFFIX l
51 
52 #define mpc_get_type mpc_get_ldc
53 #define mpc_set_type mpc_set_ldc
54 #define mpfr_set_type mpfr_set_ld
55 
56 gmp_randstate_t state;
57 mpz_t expz; /* global variable used in mpcheck_random */
58 unsigned long seed = 0;
59 int verbose = 0;
60 mpfr_exp_t emin, emax;
61 
62 #include "mpcheck-common.c"
63 
64 #define FOO add
65 #define CFOO(x,y) (x+y)
66 #include "mpcheck-template3.c"
67 
68 #define FOO sub
69 #define CFOO(x,y) (x-y)
70 #include "mpcheck-template3.c"
71 
72 #define FOO mul
73 #define CFOO(x,y) (x*y)
74 #include "mpcheck-template3.c"
75 
76 #define FOO div
77 #define CFOO(x,y) (x/y)
78 #include "mpcheck-template3.c"
79 
80 #define FOO pow
81 #include "mpcheck-template3.c"
82 
83 #define FOO abs
84 #include "mpcheck-template2.c"
85 
86 #define FOO arg
87 #include "mpcheck-template2.c"
88 
89 #define FOO sqrt
90 #include "mpcheck-template1.c"
91 
92 #define FOO acos
93 #include "mpcheck-template1.c"
94 
95 #define FOO acosh
96 #include "mpcheck-template1.c"
97 
98 #define FOO asin
99 #include "mpcheck-template1.c"
100 
101 #define FOO asinh
102 #include "mpcheck-template1.c"
103 
104 #define FOO atan
105 #include "mpcheck-template1.c"
106 
107 #define FOO atanh
108 #include "mpcheck-template1.c"
109 
110 #define FOO cos
111 #include "mpcheck-template1.c"
112 
113 #define FOO cosh
114 #include "mpcheck-template1.c"
115 
116 #define FOO exp
117 #include "mpcheck-template1.c"
118 
119 #define FOO log
120 #include "mpcheck-template1.c"
121 
122 #define FOO log10
123 #include "mpcheck-template1.c"
124 
125 #define FOO sin
126 #include "mpcheck-template1.c"
127 
128 #define FOO sinh
129 #include "mpcheck-template1.c"
130 
131 #define FOO tan
132 #include "mpcheck-template1.c"
133 
134 #define FOO tanh
135 #include "mpcheck-template1.c"
136 
137 int
138 main (int argc, char *argv[])
139 {
140   mpfr_prec_t p = PRECISION; /* precision of 'double' */
141   unsigned long n = 1000000; /* default number of random tests per function */
142 
143   while (argc >= 2 && argv[1][0] == '-')
144     {
145       if (argc >= 3 && strcmp (argv[1], "-p") == 0)
146         {
147           p = atoi (argv[2]);
148           argc -= 2;
149           argv += 2;
150         }
151       else if (argc >= 3 && strcmp (argv[1], "-seed") == 0)
152         {
153           seed = atoi (argv[2]);
154           argc -= 2;
155           argv += 2;
156         }
157       else if (argc >= 3 && strcmp (argv[1], "-num") == 0)
158         {
159           n = atoi (argv[2]);
160           argc -= 2;
161           argv += 2;
162         }
163       else if (strcmp (argv[1], "-v") == 0)
164         {
165           verbose ++;
166           argc --;
167           argv ++;
168         }
169       else if (strcmp (argv[1], "-check") == 0)
170         {
171           recheck = 1;
172           argc --;
173           argv ++;
174         }
175       else
176         {
177           fprintf (stderr, "Unknown option %s\n", argv[1]);
178           exit (1);
179         }
180     }
181 
182   /* set exponent range */
183   emin = -EMAX - PRECISION + 4; /* should be -16444 */
184   emax = EMAX;
185   mpfr_set_emin (emin);
186   mpfr_set_emax (emax);
187 
188   gmp_randinit_default (state);
189   mpz_init (expz);
190 
191   printf ("Using GMP %s, MPFR %s\n", gmp_version, mpfr_get_version ());
192 
193 #ifdef __GNUC__
194   printf ("GNU libc version: %s\n", gnu_get_libc_version ());
195   printf ("GNU libc release: %s\n", gnu_get_libc_release ());
196 #endif
197 
198   if (seed == 0)
199     seed = getpid ();
200   printf ("Using random seed %lu\n", seed);
201 
202   /* (complex,complex) -> complex */
203   test_add (p, n);
204   test_sub (p, n);
205   test_mul (p, n);
206   test_div (p, n);
207   test_pow (p, n);
208 
209   /* complex -> real */
210   test_abs (p, n);
211   test_arg (p, n);
212 
213   /* complex -> complex */
214   test_sqrt (p, n);
215   test_acos (p, n);
216   test_acosh (p, n);
217   test_asin (p, n);
218   test_asinh (p, n);
219   test_atan (p, n);
220   test_atanh (p, n);
221   test_cos (p, n);
222   test_cosh (p, n);
223   test_exp (p, n);
224   test_log (p, n);
225   test_log10 (p, n);
226   test_sin (p, n);
227   test_sinh (p, n);
228   test_tan (p, n);
229   test_tanh (p, n);
230 
231   gmp_randclear (state);
232   mpz_clear (expz);
233 
234   report_maximal_errors ();
235 
236   return 0;
237 }
238