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