]> git.ipfire.org Git - thirdparty/glibc.git/blob - math/test-tgmath2.c
Improve the accuracy of tgamma (BZ #26983)
[thirdparty/glibc.git] / math / test-tgmath2.c
1 /* Test compilation of tgmath macros.
2 Copyright (C) 2007-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #ifndef HAVE_MAIN
21 #include <float.h>
22 #include <math.h>
23 #include <complex.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <tgmath.h>
27
28 //#define DEBUG
29
30 typedef complex float cfloat;
31 typedef complex double cdouble;
32 #if LDBL_MANT_DIG > DBL_MANT_DIG
33 typedef long double ldouble;
34 typedef complex long double cldouble;
35 #else
36 typedef double ldouble;
37 typedef complex double cldouble;
38 #endif
39
40 float vfloat1, vfloat2, vfloat3;
41 double vdouble1, vdouble2, vdouble3;
42 ldouble vldouble1, vldouble2, vldouble3;
43 cfloat vcfloat1, vcfloat2, vcfloat3;
44 cdouble vcdouble1, vcdouble2, vcdouble3;
45 cldouble vcldouble1, vcldouble2, vcldouble4;
46 int vint1, vint2, vint3;
47 long int vlong1, vlong2, vlong3;
48 long long int vllong1, vllong2, vllong3;
49 const float Vfloat1 = 1, Vfloat2 = 2, Vfloat3 = 3;
50 const double Vdouble1 = 1, Vdouble2 = 2, Vdouble3 = 3;
51 const ldouble Vldouble1 = 1, Vldouble2 = 2, Vldouble3 = 3;
52 const cfloat Vcfloat1 = 1, Vcfloat2 = 2, Vcfloat3 = 3;
53 const cdouble Vcdouble1 = 1, Vcdouble2 = 2, Vcdouble3 = 3;
54 const cldouble Vcldouble1 = 1, Vcldouble2 = 2, Vcldouble4 = 3;
55 const int Vint1 = 1, Vint2 = 2, Vint3 = 3;
56 const long int Vlong1 = 1, Vlong2 = 2, Vlong3 = 3;
57 const long long int Vllong1 = 1, Vllong2 = 2, Vllong3 = 3;
58 enum
59 {
60 Tfloat = 0,
61 Tcfloat,
62 Tdouble,
63 Tcdouble,
64 #if LDBL_MANT_DIG > DBL_MANT_DIG
65 Tldouble,
66 Tcldouble,
67 #else
68 Tldouble = Tdouble,
69 Tcldouble = Tcdouble,
70 #endif
71 Tlast
72 };
73 enum
74 {
75 C_cos = 0,
76 C_fabs,
77 C_cabs,
78 C_conj,
79 C_expm1,
80 C_lrint,
81 C_ldexp,
82 C_atan2,
83 C_remquo,
84 C_pow,
85 C_fma,
86 C_last
87 };
88 int count;
89 int counts[Tlast][C_last];
90
91 #define FAIL(str) \
92 do \
93 { \
94 printf ("%s failure on line %d\n", (str), __LINE__); \
95 result = 1; \
96 } \
97 while (0)
98 #define TEST_TYPE_ONLY(expr, rettype) \
99 do \
100 { \
101 __typeof__ (expr) texpr = 0; \
102 __typeof__ (rettype) ttype = 0, *ptype; \
103 if (sizeof (expr) != sizeof (rettype)) \
104 FAIL ("type"); \
105 if (__alignof__ (expr) != __alignof__ (rettype)) \
106 FAIL ("type"); \
107 __asm ("" : "=r" (ptype) : "0" (&ttype), "r" (&texpr)); \
108 if (&texpr == ptype) \
109 FAIL ("type"); \
110 } \
111 while (0)
112 #define TEST2(expr, type, rettype, fn) \
113 do \
114 { \
115 __typeof__ (expr) texpr = 0; \
116 TEST_TYPE_ONLY (expr, rettype); \
117 if (count != 0) \
118 FAIL ("internal error"); \
119 if (counts[T##type][C_##fn] != 0) \
120 FAIL ("internal error"); \
121 texpr = expr; \
122 __asm __volatile ("" : : "r" (&texpr)); \
123 if (count != 1 || counts[T##type][C_##fn] != 1) \
124 { \
125 FAIL ("wrong function called, "#fn" ("#type")"); \
126 memset (counts, 0, sizeof (counts)); \
127 } \
128 count = 0; \
129 counts[T##type][C_##fn] = 0; \
130 } \
131 while (0)
132 #define TEST(expr, type, fn) TEST2(expr, type, type, fn)
133
134 int
135 test_cos (const int Vint4, const long long int Vllong4)
136 {
137 int result = 0;
138
139 TEST (cos (vfloat1), float, cos);
140 TEST (cos (vdouble1), double, cos);
141 TEST (cos (vldouble1), ldouble, cos);
142 TEST (cos (vint1), double, cos);
143 TEST (cos (vllong1), double, cos);
144 TEST (cos (vcfloat1), cfloat, cos);
145 TEST (cos (vcdouble1), cdouble, cos);
146 TEST (cos (vcldouble1), cldouble, cos);
147 TEST (cos (Vfloat1), float, cos);
148 TEST (cos (Vdouble1), double, cos);
149 TEST (cos (Vldouble1), ldouble, cos);
150 TEST (cos (Vint1), double, cos);
151 TEST (cos (Vllong1), double, cos);
152 TEST (cos (Vcfloat1), cfloat, cos);
153 TEST (cos (Vcdouble1), cdouble, cos);
154 TEST (cos (Vcldouble1), cldouble, cos);
155
156 return result;
157 }
158
159 int
160 test_fabs (const int Vint4, const long long int Vllong4)
161 {
162 int result = 0;
163
164 TEST (fabs (vfloat1), float, fabs);
165 TEST (fabs (vdouble1), double, fabs);
166 TEST (fabs (vldouble1), ldouble, fabs);
167 TEST (fabs (vint1), double, fabs);
168 TEST (fabs (vllong1), double, fabs);
169 TEST (fabs (vcfloat1), float, cabs);
170 TEST (fabs (vcdouble1), double, cabs);
171 TEST (fabs (vcldouble1), ldouble, cabs);
172 TEST (fabs (Vfloat1), float, fabs);
173 TEST (fabs (Vdouble1), double, fabs);
174 TEST (fabs (Vldouble2), ldouble, fabs);
175 #ifndef __OPTIMIZE__
176 /* GCC is too smart to optimize these out. */
177 TEST (fabs (Vint1), double, fabs);
178 TEST (fabs (Vllong1), double, fabs);
179 #else
180 TEST_TYPE_ONLY (fabs (vllong1), double);
181 TEST_TYPE_ONLY (fabs (vllong1), double);
182 #endif
183 TEST (fabs (Vint4), double, fabs);
184 TEST (fabs (Vllong4), double, fabs);
185 TEST (fabs (Vcfloat1), float, cabs);
186 TEST (fabs (Vcdouble1), double, cabs);
187 TEST (fabs (Vcldouble1), ldouble, cabs);
188
189 return result;
190 }
191
192 int
193 test_conj (const int Vint4, const long long int Vllong4)
194 {
195 int result = 0;
196 TEST (conj (vfloat1), cfloat, conj);
197 TEST (conj (vdouble1), cdouble, conj);
198 TEST (conj (vldouble1), cldouble, conj);
199 TEST (conj (vint1), cdouble, conj);
200 TEST (conj (vllong1), cdouble, conj);
201 TEST (conj (vcfloat1), cfloat, conj);
202 TEST (conj (vcdouble1), cdouble, conj);
203 TEST (conj (vcldouble1), cldouble, conj);
204 TEST (conj (Vfloat1), cfloat, conj);
205 TEST (conj (Vdouble1), cdouble, conj);
206 TEST (conj (Vldouble1), cldouble, conj);
207 TEST (conj (Vint1), cdouble, conj);
208 TEST (conj (Vllong1), cdouble, conj);
209 TEST (conj (Vcfloat1), cfloat, conj);
210 TEST (conj (Vcdouble1), cdouble, conj);
211 TEST (conj (Vcldouble1), cldouble, conj);
212
213 return result;
214 }
215
216 int
217 test_expm1 (const int Vint4, const long long int Vllong4)
218 {
219 int result = 0;
220
221 TEST (expm1 (vfloat1), float, expm1);
222 TEST (expm1 (vdouble1), double, expm1);
223 TEST (expm1 (vldouble1), ldouble, expm1);
224 TEST (expm1 (vint1), double, expm1);
225 TEST (expm1 (vllong1), double, expm1);
226 TEST (expm1 (Vfloat1), float, expm1);
227 TEST (expm1 (Vdouble1), double, expm1);
228 TEST (expm1 (Vldouble1), ldouble, expm1);
229 TEST (expm1 (Vint1), double, expm1);
230 TEST (expm1 (Vllong1), double, expm1);
231
232 return result;
233 }
234
235 int
236 test_lrint (const int Vint4, const long long int Vllong4)
237 {
238 int result = 0;
239 TEST2 (lrint (vfloat1), float, long int, lrint);
240 TEST2 (lrint (vdouble1), double, long int, lrint);
241 TEST2 (lrint (vldouble1), ldouble, long int, lrint);
242 TEST2 (lrint (vint1), double, long int, lrint);
243 TEST2 (lrint (vllong1), double, long int, lrint);
244 TEST2 (lrint (Vfloat1), float, long int, lrint);
245 TEST2 (lrint (Vdouble1), double, long int, lrint);
246 TEST2 (lrint (Vldouble1), ldouble, long int, lrint);
247 TEST2 (lrint (Vint1), double, long int, lrint);
248 TEST2 (lrint (Vllong1), double, long int, lrint);
249
250 return result;
251 }
252
253 int
254 test_ldexp (const int Vint4, const long long int Vllong4)
255 {
256 int result = 0;
257
258 TEST (ldexp (vfloat1, 6), float, ldexp);
259 TEST (ldexp (vdouble1, 6), double, ldexp);
260 TEST (ldexp (vldouble1, 6), ldouble, ldexp);
261 TEST (ldexp (vint1, 6), double, ldexp);
262 TEST (ldexp (vllong1, 6), double, ldexp);
263 TEST (ldexp (Vfloat1, 6), float, ldexp);
264 TEST (ldexp (Vdouble1, 6), double, ldexp);
265 TEST (ldexp (Vldouble1, 6), ldouble, ldexp);
266 TEST (ldexp (Vint1, 6), double, ldexp);
267 TEST (ldexp (Vllong1, 6), double, ldexp);
268
269 return result;
270 }
271
272 #define FIRST(x, y) (y, x)
273 #define SECOND(x, y) (x, y)
274 #define NON_LDBL_TEST(fn, argm, arg, type, fnt) \
275 TEST (fn argm (arg, vfloat1), type, fnt); \
276 TEST (fn argm (arg, vdouble1), type, fnt); \
277 TEST (fn argm (arg, vint1), type, fnt); \
278 TEST (fn argm (arg, vllong1), type, fnt); \
279 TEST (fn argm (arg, Vfloat1), type, fnt); \
280 TEST (fn argm (arg, Vdouble1), type, fnt); \
281 TEST (fn argm (arg, Vint1), type, fnt); \
282 TEST (fn argm (arg, Vllong1), type, fnt);
283 #define NON_LDBL_CTEST(fn, argm, arg, type, fnt) \
284 NON_LDBL_TEST(fn, argm, arg, type, fnt); \
285 TEST (fn argm (arg, vcfloat1), type, fnt); \
286 TEST (fn argm (arg, vcdouble1), type, fnt); \
287 TEST (fn argm (arg, Vcfloat1), type, fnt); \
288 TEST (fn argm (arg, Vcdouble1), type, fnt);
289 #define BINARY_TEST(fn, fnt) \
290 TEST (fn (vfloat1, vfloat2), float, fnt); \
291 TEST (fn (Vfloat1, vfloat2), float, fnt); \
292 TEST (fn (vfloat1, Vfloat2), float, fnt); \
293 TEST (fn (Vfloat1, Vfloat2), float, fnt); \
294 TEST (fn (vldouble1, vldouble2), ldouble, fnt); \
295 TEST (fn (Vldouble1, vldouble2), ldouble, fnt); \
296 TEST (fn (vldouble1, Vldouble2), ldouble, fnt); \
297 TEST (fn (Vldouble1, Vldouble2), ldouble, fnt); \
298 NON_LDBL_TEST (fn, FIRST, vldouble2, ldouble, fnt); \
299 NON_LDBL_TEST (fn, SECOND, vldouble2, ldouble, fnt); \
300 NON_LDBL_TEST (fn, FIRST, Vldouble2, ldouble, fnt); \
301 NON_LDBL_TEST (fn, SECOND, Vldouble2, ldouble, fnt); \
302 NON_LDBL_TEST (fn, FIRST, vdouble2, double, fnt); \
303 NON_LDBL_TEST (fn, SECOND, vdouble2, double, fnt); \
304 NON_LDBL_TEST (fn, FIRST, Vdouble2, double, fnt); \
305 NON_LDBL_TEST (fn, SECOND, Vdouble2, double, fnt); \
306 NON_LDBL_TEST (fn, FIRST, vint2, double, fnt); \
307 NON_LDBL_TEST (fn, SECOND, vint2, double, fnt); \
308 NON_LDBL_TEST (fn, FIRST, Vint2, double, fnt); \
309 NON_LDBL_TEST (fn, SECOND, Vint2, double, fnt); \
310 NON_LDBL_TEST (fn, FIRST, vllong2, double, fnt); \
311 NON_LDBL_TEST (fn, SECOND, vllong2, double, fnt); \
312 NON_LDBL_TEST (fn, FIRST, Vllong2, double, fnt); \
313 NON_LDBL_TEST (fn, SECOND, Vllong2, double, fnt);
314 #define BINARY_CTEST(fn, fnt) \
315 BINARY_TEST (fn, fnt); \
316 TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
317 TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
318 TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
319 TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
320 TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
321 TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
322 TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
323 TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
324 TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
325 TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
326 TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
327 TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
328 TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
329 TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
330 TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
331 TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
332 TEST (fn (vcfloat1, vcfloat2), cfloat, fnt); \
333 TEST (fn (Vcfloat1, vcfloat2), cfloat, fnt); \
334 TEST (fn (vcfloat1, Vcfloat2), cfloat, fnt); \
335 TEST (fn (Vcfloat1, Vcfloat2), cfloat, fnt); \
336 TEST (fn (vcldouble1, vcldouble2), cldouble, fnt); \
337 TEST (fn (Vcldouble1, vcldouble2), cldouble, fnt); \
338 TEST (fn (vcldouble1, Vcldouble2), cldouble, fnt); \
339 TEST (fn (Vcldouble1, Vcldouble2), cldouble, fnt); \
340 NON_LDBL_CTEST (fn, FIRST, vcldouble2, cldouble, fnt); \
341 NON_LDBL_CTEST (fn, SECOND, vcldouble2, cldouble, fnt); \
342 NON_LDBL_CTEST (fn, FIRST, Vcldouble2, cldouble, fnt); \
343 NON_LDBL_CTEST (fn, SECOND, Vcldouble2, cldouble, fnt); \
344 NON_LDBL_CTEST (fn, FIRST, vcdouble2, cdouble, fnt); \
345 NON_LDBL_CTEST (fn, SECOND, vcdouble2, cdouble, fnt); \
346 NON_LDBL_CTEST (fn, FIRST, Vcdouble2, cdouble, fnt); \
347 NON_LDBL_CTEST (fn, SECOND, Vcdouble2, cdouble, fnt);
348
349 int
350 test_atan2 (const int Vint4, const long long int Vllong4)
351 {
352 int result = 0;
353
354 BINARY_TEST (atan2, atan2);
355
356 return result;
357 }
358
359 int
360 test_remquo (const int Vint4, const long long int Vllong4)
361 {
362 int result = 0;
363 int quo = 0;
364
365 #define my_remquo(x, y) remquo (x, y, &quo)
366 BINARY_TEST (my_remquo, remquo);
367 #undef my_remquo
368
369 return result;
370 }
371
372 int
373 test_pow (const int Vint4, const long long int Vllong4)
374 {
375 int result = 0;
376
377 BINARY_CTEST (pow, pow);
378
379 return result;
380 }
381
382 /* Testing all arguments of fma would be just too expensive,
383 so test just some. */
384
385 int
386 test_fma_1 (const int Vint4, const long long int Vllong4)
387 {
388 int result = 0;
389
390 #define my_fma(x, y) fma (x, y, vfloat3)
391 BINARY_TEST (my_fma, fma);
392 #undef my_fma
393
394 return result;
395 }
396
397 int
398 test_fma_2 (const int Vint4, const long long int Vllong4)
399 {
400 int result = 0;
401
402 #define my_fma(x, y) fma (x, vfloat3, y)
403 BINARY_TEST (my_fma, fma);
404 #undef my_fma
405
406 return result;
407 }
408
409 int
410 test_fma_3 (const int Vint4, const long long int Vllong4)
411 {
412 int result = 0;
413
414 #define my_fma(x, y) fma (Vfloat3, x, y)
415 BINARY_TEST (my_fma, fma);
416 #undef my_fma
417
418 return result;
419 }
420
421 int
422 test_fma_4 (const int Vint4, const long long int Vllong4)
423 {
424 int result = 0;
425 TEST (fma (vdouble1, Vdouble2, vllong3), double, fma);
426 TEST (fma (vint1, Vint2, vint3), double, fma);
427 TEST (fma (Vldouble1, vldouble2, Vldouble3), ldouble, fma);
428 TEST (fma (vldouble1, vint2, Vdouble3), ldouble, fma);
429
430 return result;
431 }
432
433 static int
434 do_test (void)
435 {
436 int result;
437
438 result = test_cos (vint1, vllong1);
439 result |= test_fabs (vint1, vllong1);
440 result |= test_conj (vint1, vllong1);
441 result |= test_expm1 (vint1, vllong1);
442 result |= test_lrint (vint1, vllong1);
443 result |= test_ldexp (vint1, vllong1);
444 result |= test_atan2 (vint1, vllong1);
445 result |= test_remquo (vint1, vllong1);
446 result |= test_pow (vint1, vllong1);
447 result |= test_fma_1 (vint1, vllong1);
448 result |= test_fma_2 (vint1, vllong1);
449 result |= test_fma_3 (vint1, vllong1);
450 result |= test_fma_4 (vint1, vllong1);
451
452 return result;
453 }
454
455 /* Now generate the three functions. */
456 #define HAVE_MAIN
457
458 #define F(name) name
459 #define TYPE double
460 #define CTYPE cdouble
461 #define T Tdouble
462 #define C Tcdouble
463 #include "test-tgmath2.c"
464
465 #define F(name) name##f
466 #define TYPE float
467 #define CTYPE cfloat
468 #define T Tfloat
469 #define C Tcfloat
470 #include "test-tgmath2.c"
471
472 #if LDBL_MANT_DIG > DBL_MANT_DIG
473 #define F(name) name##l
474 #define TYPE ldouble
475 #define CTYPE cldouble
476 #define T Tldouble
477 #define C Tcldouble
478 #include "test-tgmath2.c"
479 #endif
480
481 #define TEST_FUNCTION do_test ()
482 #include "../test-skeleton.c"
483
484 #else
485
486 #ifdef DEBUG
487 #define P() puts (__FUNCTION__); count++
488 #else
489 #define P() count++;
490 #endif
491
492 TYPE
493 (F(cos)) (TYPE x)
494 {
495 counts[T][C_cos]++;
496 P ();
497 return x;
498 }
499
500 CTYPE
501 (F(ccos)) (CTYPE x)
502 {
503 counts[C][C_cos]++;
504 P ();
505 return x;
506 }
507
508 TYPE
509 (F(fabs)) (TYPE x)
510 {
511 counts[T][C_fabs]++;
512 P ();
513 return x;
514 }
515
516 TYPE
517 (F(cabs)) (CTYPE x)
518 {
519 counts[T][C_cabs]++;
520 P ();
521 return x;
522 }
523
524 CTYPE
525 (F(conj)) (CTYPE x)
526 {
527 counts[C][C_conj]++;
528 P ();
529 return x;
530 }
531
532 TYPE
533 (F(expm1)) (TYPE x)
534 {
535 counts[T][C_expm1]++;
536 P ();
537 return x;
538 }
539
540 long int
541 (F(lrint)) (TYPE x)
542 {
543 counts[T][C_lrint]++;
544 P ();
545 return x;
546 }
547
548 TYPE
549 (F(ldexp)) (TYPE x, int y)
550 {
551 counts[T][C_ldexp]++;
552 P ();
553 return x + y;
554 }
555
556 TYPE
557 (F(atan2)) (TYPE x, TYPE y)
558 {
559 counts[T][C_atan2]++;
560 P ();
561 return x + y;
562 }
563
564 TYPE
565 (F(remquo)) (TYPE x, TYPE y, int *z)
566 {
567 counts[T][C_remquo]++;
568 P ();
569 return x + y + *z;
570 }
571
572 TYPE
573 (F(pow)) (TYPE x, TYPE y)
574 {
575 counts[T][C_pow]++;
576 P ();
577 return x + y;
578 }
579
580 CTYPE
581 (F(cpow)) (CTYPE x, CTYPE y)
582 {
583 counts[C][C_pow]++;
584 P ();
585 return x + y;
586 }
587
588 TYPE
589 (F(fma)) (TYPE x, TYPE y, TYPE z)
590 {
591 counts[T][C_fma]++;
592 P ();
593 return x + y + z;
594 }
595
596 #undef F
597 #undef TYPE
598 #undef CTYPE
599 #undef T
600 #undef C
601 #undef P
602 #endif