NPTL is no longer an add-on!
[platform/upstream/glibc.git] / math / test-tgmath2.c
1 /* Test compilation of tgmath macros.
2    Copyright (C) 2007-2014 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 <math.h>
24 #include <complex.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <tgmath.h>
28
29 //#define DEBUG
30
31 typedef complex float cfloat;
32 typedef complex double cdouble;
33 #ifndef NO_LONG_DOUBLE
34 typedef long double ldouble;
35 typedef complex long double cldouble;
36 #else
37 typedef double ldouble;
38 typedef complex double cldouble;
39 #endif
40
41 float vfloat1, vfloat2, vfloat3;
42 double vdouble1, vdouble2, vdouble3;
43 ldouble vldouble1, vldouble2, vldouble3;
44 cfloat vcfloat1, vcfloat2, vcfloat3;
45 cdouble vcdouble1, vcdouble2, vcdouble3;
46 cldouble vcldouble1, vcldouble2, vcldouble4;
47 int vint1, vint2, vint3;
48 long int vlong1, vlong2, vlong3;
49 long long int vllong1, vllong2, vllong3;
50 const float Vfloat1 = 1, Vfloat2 = 2, Vfloat3 = 3;
51 const double Vdouble1 = 1, Vdouble2 = 2, Vdouble3 = 3;
52 const ldouble Vldouble1 = 1, Vldouble2 = 2, Vldouble3 = 3;
53 const cfloat Vcfloat1 = 1, Vcfloat2 = 2, Vcfloat3 = 3;
54 const cdouble Vcdouble1 = 1, Vcdouble2 = 2, Vcdouble3 = 3;
55 const cldouble Vcldouble1 = 1, Vcldouble2 = 2, Vcldouble4 = 3;
56 const int Vint1 = 1, Vint2 = 2, Vint3 = 3;
57 const long int Vlong1 = 1, Vlong2 = 2, Vlong3 = 3;
58 const long long int Vllong1 = 1, Vllong2 = 2, Vllong3 = 3;
59 enum
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   };
74 enum
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   };
89 int count;
90 int counts[Tlast][C_last];
91
92 int
93 test (const int Vint4, const long long int Vllong4)
94 {
95   int result = 0;
96   int quo = 0;
97
98 #define FAIL(str) \
99   do                                                            \
100     {                                                           \
101       printf ("%s failure on line %d\n", (str), __LINE__);      \
102       result = 1;                                               \
103     }                                                           \
104   while (0)
105 #define TEST_TYPE_ONLY(expr, rettype) \
106   do                                                            \
107     {                                                           \
108       __typeof__ (expr) texpr = 0;                              \
109       __typeof__ (rettype) ttype = 0, *ptype;                   \
110       if (sizeof (expr) != sizeof (rettype))                    \
111         FAIL ("type");                                          \
112       if (__alignof__ (expr) != __alignof__ (rettype))          \
113         FAIL ("type");                                          \
114       __asm ("" : "=r" (ptype) : "0" (&ttype), "r" (&texpr));   \
115       if (&texpr == ptype)                                      \
116         FAIL ("type");                                          \
117     }                                                           \
118   while (0)
119 #define TEST2(expr, type, rettype, fn) \
120   do                                                            \
121     {                                                           \
122       __typeof__ (expr) texpr = 0;                              \
123       TEST_TYPE_ONLY (expr, rettype);                           \
124       if (count != 0)                                           \
125         FAIL ("internal error");                                \
126       if (counts[T##type][C_##fn] != 0)                         \
127         FAIL ("internal error");                                \
128       texpr = expr;                                             \
129       __asm __volatile ("" : : "r" (&texpr));                   \
130       if (count != 1 || counts[T##type][C_##fn] != 1)           \
131         {                                                       \
132           FAIL ("wrong function called");                       \
133           memset (counts, 0, sizeof (counts));                  \
134         }                                                       \
135       count = 0;                                                \
136       counts[T##type][C_##fn] = 0;                              \
137     }                                                           \
138   while (0)
139 #define TEST(expr, type, fn) TEST2(expr, type, type, fn)
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   TEST (fabs (vfloat1), float, fabs);
159   TEST (fabs (vdouble1), double, fabs);
160   TEST (fabs (vldouble1), ldouble, fabs);
161   TEST (fabs (vint1), double, fabs);
162   TEST (fabs (vllong1), double, fabs);
163   TEST (fabs (vcfloat1), float, cabs);
164   TEST (fabs (vcdouble1), double, cabs);
165   TEST (fabs (vcldouble1), ldouble, cabs);
166   TEST (fabs (Vfloat1), float, fabs);
167   TEST (fabs (Vdouble1), double, fabs);
168   TEST (fabs (Vldouble1), ldouble, fabs);
169 #ifndef __OPTIMIZE__
170   /* GCC is too smart to optimize these out.  */
171   TEST (fabs (Vint1), double, fabs);
172   TEST (fabs (Vllong1), double, fabs);
173 #else
174   TEST_TYPE_ONLY (fabs (vllong1), double);
175   TEST_TYPE_ONLY (fabs (vllong1), double);
176 #endif
177   TEST (fabs (Vint4), double, fabs);
178   TEST (fabs (Vllong4), double, fabs);
179   TEST (fabs (Vcfloat1), float, cabs);
180   TEST (fabs (Vcdouble1), double, cabs);
181   TEST (fabs (Vcldouble1), ldouble, cabs);
182
183   TEST (conj (vfloat1), cfloat, conj);
184   TEST (conj (vdouble1), cdouble, conj);
185   TEST (conj (vldouble1), cldouble, conj);
186   TEST (conj (vint1), cdouble, conj);
187   TEST (conj (vllong1), cdouble, conj);
188   TEST (conj (vcfloat1), cfloat, conj);
189   TEST (conj (vcdouble1), cdouble, conj);
190   TEST (conj (vcldouble1), cldouble, conj);
191   TEST (conj (Vfloat1), cfloat, conj);
192   TEST (conj (Vdouble1), cdouble, conj);
193   TEST (conj (Vldouble1), cldouble, conj);
194   TEST (conj (Vint1), cdouble, conj);
195   TEST (conj (Vllong1), cdouble, conj);
196   TEST (conj (Vcfloat1), cfloat, conj);
197   TEST (conj (Vcdouble1), cdouble, conj);
198   TEST (conj (Vcldouble1), cldouble, conj);
199
200   TEST (expm1 (vfloat1), float, expm1);
201   TEST (expm1 (vdouble1), double, expm1);
202   TEST (expm1 (vldouble1), ldouble, expm1);
203   TEST (expm1 (vint1), double, expm1);
204   TEST (expm1 (vllong1), double, expm1);
205   TEST (expm1 (Vfloat1), float, expm1);
206   TEST (expm1 (Vdouble1), double, expm1);
207   TEST (expm1 (Vldouble1), ldouble, expm1);
208   TEST (expm1 (Vint1), double, expm1);
209   TEST (expm1 (Vllong1), double, expm1);
210
211   TEST2 (lrint (vfloat1), float, long int, lrint);
212   TEST2 (lrint (vdouble1), double, long int, lrint);
213   TEST2 (lrint (vldouble1), ldouble, long int, lrint);
214   TEST2 (lrint (vint1), double, long int, lrint);
215   TEST2 (lrint (vllong1), double, long int, lrint);
216   TEST2 (lrint (Vfloat1), float, long int, lrint);
217   TEST2 (lrint (Vdouble1), double, long int, lrint);
218   TEST2 (lrint (Vldouble1), ldouble, long int, lrint);
219   TEST2 (lrint (Vint1), double, long int, lrint);
220   TEST2 (lrint (Vllong1), double, long int, lrint);
221
222   TEST (ldexp (vfloat1, 6), float, ldexp);
223   TEST (ldexp (vdouble1, 6), double, ldexp);
224   TEST (ldexp (vldouble1, 6), ldouble, ldexp);
225   TEST (ldexp (vint1, 6), double, ldexp);
226   TEST (ldexp (vllong1, 6), double, ldexp);
227   TEST (ldexp (Vfloat1, 6), float, ldexp);
228   TEST (ldexp (Vdouble1, 6), double, ldexp);
229   TEST (ldexp (Vldouble1, 6), ldouble, ldexp);
230   TEST (ldexp (Vint1, 6), double, ldexp);
231   TEST (ldexp (Vllong1, 6), double, ldexp);
232
233 #define FIRST(x, y) (y, x)
234 #define SECOND(x, y) (x, y)
235 #define NON_LDBL_TEST(fn, argm, arg, type, fnt) \
236   TEST (fn argm (arg, vfloat1), type, fnt); \
237   TEST (fn argm (arg, vdouble1), type, fnt); \
238   TEST (fn argm (arg, vint1), type, fnt); \
239   TEST (fn argm (arg, vllong1), type, fnt); \
240   TEST (fn argm (arg, Vfloat1), type, fnt); \
241   TEST (fn argm (arg, Vdouble1), type, fnt); \
242   TEST (fn argm (arg, Vint1), type, fnt); \
243   TEST (fn argm (arg, Vllong1), type, fnt);
244 #define NON_LDBL_CTEST(fn, argm, arg, type, fnt) \
245   NON_LDBL_TEST(fn, argm, arg, type, fnt); \
246   TEST (fn argm (arg, vcfloat1), type, fnt); \
247   TEST (fn argm (arg, vcdouble1), type, fnt); \
248   TEST (fn argm (arg, Vcfloat1), type, fnt); \
249   TEST (fn argm (arg, Vcdouble1), type, fnt);
250 #define BINARY_TEST(fn, fnt) \
251   TEST (fn (vfloat1, vfloat2), float, fnt); \
252   TEST (fn (Vfloat1, vfloat2), float, fnt); \
253   TEST (fn (vfloat1, Vfloat2), float, fnt); \
254   TEST (fn (Vfloat1, Vfloat2), float, fnt); \
255   TEST (fn (vldouble1, vldouble2), ldouble, fnt); \
256   TEST (fn (Vldouble1, vldouble2), ldouble, fnt); \
257   TEST (fn (vldouble1, Vldouble2), ldouble, fnt); \
258   TEST (fn (Vldouble1, Vldouble2), ldouble, fnt); \
259   NON_LDBL_TEST (fn, FIRST, vldouble2, ldouble, fnt); \
260   NON_LDBL_TEST (fn, SECOND, vldouble2, ldouble, fnt); \
261   NON_LDBL_TEST (fn, FIRST, Vldouble2, ldouble, fnt); \
262   NON_LDBL_TEST (fn, SECOND, Vldouble2, ldouble, fnt); \
263   NON_LDBL_TEST (fn, FIRST, vdouble2, double, fnt); \
264   NON_LDBL_TEST (fn, SECOND, vdouble2, double, fnt); \
265   NON_LDBL_TEST (fn, FIRST, Vdouble2, double, fnt); \
266   NON_LDBL_TEST (fn, SECOND, Vdouble2, double, fnt); \
267   NON_LDBL_TEST (fn, FIRST, vint2, double, fnt); \
268   NON_LDBL_TEST (fn, SECOND, vint2, double, fnt); \
269   NON_LDBL_TEST (fn, FIRST, Vint2, double, fnt); \
270   NON_LDBL_TEST (fn, SECOND, Vint2, double, fnt); \
271   NON_LDBL_TEST (fn, FIRST, vllong2, double, fnt); \
272   NON_LDBL_TEST (fn, SECOND, vllong2, double, fnt); \
273   NON_LDBL_TEST (fn, FIRST, Vllong2, double, fnt); \
274   NON_LDBL_TEST (fn, SECOND, Vllong2, double, fnt);
275 #define BINARY_CTEST(fn, fnt) \
276   BINARY_TEST (fn, fnt); \
277   TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
278   TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
279   TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
280   TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
281   TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
282   TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
283   TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
284   TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
285   TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
286   TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
287   TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
288   TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
289   TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
290   TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
291   TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
292   TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
293   TEST (fn (vcfloat1, vcfloat2), cfloat, fnt); \
294   TEST (fn (Vcfloat1, vcfloat2), cfloat, fnt); \
295   TEST (fn (vcfloat1, Vcfloat2), cfloat, fnt); \
296   TEST (fn (Vcfloat1, Vcfloat2), cfloat, fnt); \
297   TEST (fn (vcldouble1, vcldouble2), cldouble, fnt); \
298   TEST (fn (Vcldouble1, vcldouble2), cldouble, fnt); \
299   TEST (fn (vcldouble1, Vcldouble2), cldouble, fnt); \
300   TEST (fn (Vcldouble1, Vcldouble2), cldouble, fnt); \
301   NON_LDBL_CTEST (fn, FIRST, vcldouble2, cldouble, fnt); \
302   NON_LDBL_CTEST (fn, SECOND, vcldouble2, cldouble, fnt); \
303   NON_LDBL_CTEST (fn, FIRST, Vcldouble2, cldouble, fnt); \
304   NON_LDBL_CTEST (fn, SECOND, Vcldouble2, cldouble, fnt); \
305   NON_LDBL_CTEST (fn, FIRST, vcdouble2, cdouble, fnt); \
306   NON_LDBL_CTEST (fn, SECOND, vcdouble2, cdouble, fnt); \
307   NON_LDBL_CTEST (fn, FIRST, Vcdouble2, cdouble, fnt); \
308   NON_LDBL_CTEST (fn, SECOND, Vcdouble2, cdouble, fnt);
309
310   BINARY_TEST (atan2, atan2);
311
312 #define my_remquo(x, y) remquo (x, y, &quo)
313   BINARY_TEST (my_remquo, remquo);
314 #undef my_remquo
315
316   BINARY_CTEST (pow, pow);
317
318   /* Testing all arguments of fma would be just too expensive,
319      so test just some.  */
320 #define my_fma(x, y) fma (x, y, vfloat3)
321   BINARY_TEST (my_fma, fma);
322 #undef my_fma
323 #define my_fma(x, y) fma (x, vfloat3, y)
324   BINARY_TEST (my_fma, fma);
325 #undef my_fma
326 #define my_fma(x, y) fma (Vfloat3, x, y)
327   BINARY_TEST (my_fma, fma);
328 #undef my_fma
329   TEST (fma (vdouble1, Vdouble2, vllong3), double, fma);
330   TEST (fma (vint1, Vint2, vint3), double, fma);
331   TEST (fma (Vldouble1, vldouble2, Vldouble3), ldouble, fma);
332   TEST (fma (vldouble1, vint2, Vdouble3), ldouble, fma);
333
334   return result;
335 }
336
337 int
338 main (void)
339 {
340   return test (vint1, vllong1);
341 }
342
343 /* Now generate the three functions.  */
344 #define HAVE_MAIN
345
346 #define F(name) name
347 #define TYPE double
348 #define CTYPE cdouble
349 #define T Tdouble
350 #define C Tcdouble
351 #include "test-tgmath2.c"
352
353 #define F(name) name##f
354 #define TYPE float
355 #define CTYPE cfloat
356 #define T Tfloat
357 #define C Tcfloat
358 #include "test-tgmath2.c"
359
360 #ifndef NO_LONG_DOUBLE
361 #define F(name) name##l
362 #define TYPE ldouble
363 #define CTYPE cldouble
364 #define T Tldouble
365 #define C Tcldouble
366 #include "test-tgmath2.c"
367 #endif
368
369 #else
370
371 #ifdef DEBUG
372 #define P() puts (__FUNCTION__); count++
373 #else
374 #define P() count++;
375 #endif
376
377 TYPE
378 (F(cos)) (TYPE x)
379 {
380   counts[T][C_cos]++;
381   P ();
382   return x;
383 }
384
385 CTYPE
386 (F(ccos)) (CTYPE x)
387 {
388   counts[C][C_cos]++;
389   P ();
390   return x;
391 }
392
393 TYPE
394 (F(fabs)) (TYPE x)
395 {
396   counts[T][C_fabs]++;
397   P ();
398   return x;
399 }
400
401 TYPE
402 (F(cabs)) (CTYPE x)
403 {
404   counts[T][C_cabs]++;
405   P ();
406   return x;
407 }
408
409 CTYPE
410 (F(conj)) (CTYPE x)
411 {
412   counts[C][C_conj]++;
413   P ();
414   return x;
415 }
416
417 TYPE
418 (F(expm1)) (TYPE x)
419 {
420   counts[T][C_expm1]++;
421   P ();
422   return x;
423 }
424
425 long int
426 (F(lrint)) (TYPE x)
427 {
428   counts[T][C_lrint]++;
429   P ();
430   return x;
431 }
432
433 TYPE
434 (F(ldexp)) (TYPE x, int y)
435 {
436   counts[T][C_ldexp]++;
437   P ();
438   return x + y;
439 }
440
441 TYPE
442 (F(atan2)) (TYPE x, TYPE y)
443 {
444   counts[T][C_atan2]++;
445   P ();
446   return x + y;
447 }
448
449 TYPE
450 (F(remquo)) (TYPE x, TYPE y, int *z)
451 {
452   counts[T][C_remquo]++;
453   P ();
454   return x + y + *z;
455 }
456
457 TYPE
458 (F(pow)) (TYPE x, TYPE y)
459 {
460   counts[T][C_pow]++;
461   P ();
462   return x + y;
463 }
464
465 CTYPE
466 (F(cpow)) (CTYPE x, CTYPE y)
467 {
468   counts[C][C_pow]++;
469   P ();
470   return x + y;
471 }
472
473 TYPE
474 (F(fma)) (TYPE x, TYPE y, TYPE z)
475 {
476   counts[T][C_fma]++;
477   P ();
478   return x + y + z;
479 }
480
481 #undef F
482 #undef TYPE
483 #undef CTYPE
484 #undef T
485 #undef C
486 #undef P
487 #endif