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