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