Back to index

glibc  2.9
test-tgmath2.c
Go to the documentation of this file.
00001 /* Test compilation of tgmath macros.
00002    Copyright (C) 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef HAVE_MAIN
00022 #undef __NO_MATH_INLINES
00023 #define __NO_MATH_INLINES 1
00024 #include <math.h>
00025 #include <complex.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <tgmath.h>
00029 
00030 //#define DEBUG
00031 
00032 typedef complex float cfloat;
00033 typedef complex double cdouble;
00034 #ifndef NO_LONG_DOUBLE
00035 typedef long double ldouble;
00036 typedef complex long double cldouble;
00037 #else
00038 typedef double ldouble;
00039 typedef complex double cldouble;
00040 #endif
00041 
00042 float vfloat1, vfloat2, vfloat3;
00043 double vdouble1, vdouble2, vdouble3;
00044 ldouble vldouble1, vldouble2, vldouble3;
00045 cfloat vcfloat1, vcfloat2, vcfloat3;
00046 cdouble vcdouble1, vcdouble2, vcdouble3;
00047 cldouble vcldouble1, vcldouble2, vcldouble4;
00048 int vint1, vint2, vint3;
00049 long int vlong1, vlong2, vlong3;
00050 long long int vllong1, vllong2, vllong3;
00051 const float Vfloat1 = 1, Vfloat2 = 2, Vfloat3 = 3;
00052 const double Vdouble1 = 1, Vdouble2 = 2, Vdouble3 = 3;
00053 const ldouble Vldouble1 = 1, Vldouble2 = 2, Vldouble3 = 3;
00054 const cfloat Vcfloat1 = 1, Vcfloat2 = 2, Vcfloat3 = 3;
00055 const cdouble Vcdouble1 = 1, Vcdouble2 = 2, Vcdouble3 = 3;
00056 const cldouble Vcldouble1 = 1, Vcldouble2 = 2, Vcldouble4 = 3;
00057 const int Vint1 = 1, Vint2 = 2, Vint3 = 3;
00058 const long int Vlong1 = 1, Vlong2 = 2, Vlong3 = 3;
00059 const long long int Vllong1 = 1, Vllong2 = 2, Vllong3 = 3;
00060 enum
00061   {
00062     Tfloat = 0,
00063     Tcfloat,
00064     Tdouble,
00065     Tcdouble,
00066 #ifndef NO_LONG_DOUBLE
00067     Tldouble,
00068     Tcldouble,
00069 #else
00070     Tldouble = Tdouble,
00071     Tcldouble = Tcdouble,
00072 #endif
00073     Tlast
00074   };
00075 enum
00076   {
00077     C_cos = 0,
00078     C_fabs,
00079     C_cabs,
00080     C_conj,
00081     C_expm1,
00082     C_lrint,
00083     C_ldexp,
00084     C_atan2,
00085     C_remquo,
00086     C_pow,
00087     C_fma,
00088     C_last
00089   };
00090 int count;
00091 int counts[Tlast][C_last];
00092 
00093 int
00094 test (const int Vint4, const long long int Vllong4)
00095 {
00096   int result = 0;
00097   int quo = 0;
00098 
00099 #define FAIL(str) \
00100   do                                                    \
00101     {                                                   \
00102       printf ("%s failure on line %d\n", (str), __LINE__);     \
00103       result = 1;                                       \
00104     }                                                   \
00105   while (0)
00106 #define TEST_TYPE_ONLY(expr, rettype) \
00107   do                                                    \
00108     {                                                   \
00109       __typeof__ (expr) texpr = 0;                      \
00110       __typeof__ (rettype) ttype = 0, *ptype;                  \
00111       if (sizeof (expr) != sizeof (rettype))                   \
00112        FAIL ("type");                                          \
00113       if (__alignof__ (expr) != __alignof__ (rettype))         \
00114        FAIL ("type");                                          \
00115       __asm ("" : "=r" (ptype) : "0" (&ttype), "r" (&texpr));  \
00116       if (&texpr == ptype)                              \
00117        FAIL ("type");                                          \
00118     }                                                   \
00119   while (0)
00120 #define TEST2(expr, type, rettype, fn) \
00121   do                                                    \
00122     {                                                   \
00123       __typeof__ (expr) texpr = 0;                      \
00124       TEST_TYPE_ONLY (expr, rettype);                          \
00125       if (count != 0)                                          \
00126        FAIL ("internal error");                         \
00127       if (counts[T##type][C_##fn] != 0)                        \
00128        FAIL ("internal error");                         \
00129       texpr = expr;                                     \
00130       __asm __volatile ("" : : "r" (&texpr));                  \
00131       if (count != 1 || counts[T##type][C_##fn] != 1)          \
00132        {                                                \
00133          FAIL ("wrong function called");                \
00134          memset (counts, 0, sizeof (counts));                  \
00135        }                                                \
00136       count = 0;                                        \
00137       counts[T##type][C_##fn] = 0;                      \
00138     }                                                   \
00139   while (0)
00140 #define TEST(expr, type, fn) TEST2(expr, type, type, fn)
00141 
00142   TEST (cos (vfloat1), float, cos);
00143   TEST (cos (vdouble1), double, cos);
00144   TEST (cos (vldouble1), ldouble, cos);
00145   TEST (cos (vint1), double, cos);
00146   TEST (cos (vllong1), double, cos);
00147   TEST (cos (vcfloat1), cfloat, cos);
00148   TEST (cos (vcdouble1), cdouble, cos);
00149   TEST (cos (vcldouble1), cldouble, cos);
00150   TEST (cos (Vfloat1), float, cos);
00151   TEST (cos (Vdouble1), double, cos);
00152   TEST (cos (Vldouble1), ldouble, cos);
00153   TEST (cos (Vint1), double, cos);
00154   TEST (cos (Vllong1), double, cos);
00155   TEST (cos (Vcfloat1), cfloat, cos);
00156   TEST (cos (Vcdouble1), cdouble, cos);
00157   TEST (cos (Vcldouble1), cldouble, cos);
00158 
00159   TEST (fabs (vfloat1), float, fabs);
00160   TEST (fabs (vdouble1), double, fabs);
00161   TEST (fabs (vldouble1), ldouble, fabs);
00162   TEST (fabs (vint1), double, fabs);
00163   TEST (fabs (vllong1), double, fabs);
00164   TEST (fabs (vcfloat1), float, cabs);
00165   TEST (fabs (vcdouble1), double, cabs);
00166   TEST (fabs (vcldouble1), ldouble, cabs);
00167   TEST (fabs (Vfloat1), float, fabs);
00168   TEST (fabs (Vdouble1), double, fabs);
00169   TEST (fabs (Vldouble1), ldouble, fabs);
00170 #ifndef __OPTIMIZE__
00171   /* GCC is too smart to optimize these out.  */
00172   TEST (fabs (Vint1), double, fabs);
00173   TEST (fabs (Vllong1), double, fabs);
00174 #else
00175   TEST_TYPE_ONLY (fabs (vllong1), double);
00176   TEST_TYPE_ONLY (fabs (vllong1), double);
00177 #endif
00178   TEST (fabs (Vint4), double, fabs);
00179   TEST (fabs (Vllong4), double, fabs);
00180   TEST (fabs (Vcfloat1), float, cabs);
00181   TEST (fabs (Vcdouble1), double, cabs);
00182   TEST (fabs (Vcldouble1), ldouble, cabs);
00183 
00184   TEST (conj (vfloat1), cfloat, conj);
00185   TEST (conj (vdouble1), cdouble, conj);
00186   TEST (conj (vldouble1), cldouble, conj);
00187   TEST (conj (vint1), cdouble, conj);
00188   TEST (conj (vllong1), cdouble, conj);
00189   TEST (conj (vcfloat1), cfloat, conj);
00190   TEST (conj (vcdouble1), cdouble, conj);
00191   TEST (conj (vcldouble1), cldouble, conj);
00192   TEST (conj (Vfloat1), cfloat, conj);
00193   TEST (conj (Vdouble1), cdouble, conj);
00194   TEST (conj (Vldouble1), cldouble, conj);
00195   TEST (conj (Vint1), cdouble, conj);
00196   TEST (conj (Vllong1), cdouble, conj);
00197   TEST (conj (Vcfloat1), cfloat, conj);
00198   TEST (conj (Vcdouble1), cdouble, conj);
00199   TEST (conj (Vcldouble1), cldouble, conj);
00200 
00201   TEST (expm1 (vfloat1), float, expm1);
00202   TEST (expm1 (vdouble1), double, expm1);
00203   TEST (expm1 (vldouble1), ldouble, expm1);
00204   TEST (expm1 (vint1), double, expm1);
00205   TEST (expm1 (vllong1), double, expm1);
00206   TEST (expm1 (Vfloat1), float, expm1);
00207   TEST (expm1 (Vdouble1), double, expm1);
00208   TEST (expm1 (Vldouble1), ldouble, expm1);
00209   TEST (expm1 (Vint1), double, expm1);
00210   TEST (expm1 (Vllong1), double, expm1);
00211 
00212   TEST2 (lrint (vfloat1), float, long int, lrint);
00213   TEST2 (lrint (vdouble1), double, long int, lrint);
00214   TEST2 (lrint (vldouble1), ldouble, long int, lrint);
00215   TEST2 (lrint (vint1), double, long int, lrint);
00216   TEST2 (lrint (vllong1), double, long int, lrint);
00217   TEST2 (lrint (Vfloat1), float, long int, lrint);
00218   TEST2 (lrint (Vdouble1), double, long int, lrint);
00219   TEST2 (lrint (Vldouble1), ldouble, long int, lrint);
00220   TEST2 (lrint (Vint1), double, long int, lrint);
00221   TEST2 (lrint (Vllong1), double, long int, lrint);
00222 
00223   TEST (ldexp (vfloat1, 6), float, ldexp);
00224   TEST (ldexp (vdouble1, 6), double, ldexp);
00225   TEST (ldexp (vldouble1, 6), ldouble, ldexp);
00226   TEST (ldexp (vint1, 6), double, ldexp);
00227   TEST (ldexp (vllong1, 6), double, ldexp);
00228   TEST (ldexp (Vfloat1, 6), float, ldexp);
00229   TEST (ldexp (Vdouble1, 6), double, ldexp);
00230   TEST (ldexp (Vldouble1, 6), ldouble, ldexp);
00231   TEST (ldexp (Vint1, 6), double, ldexp);
00232   TEST (ldexp (Vllong1, 6), double, ldexp);
00233 
00234 #define FIRST(x, y) (y, x)
00235 #define SECOND(x, y) (x, y)
00236 #define NON_LDBL_TEST(fn, argm, arg, type, fnt) \
00237   TEST (fn argm (arg, vfloat1), type, fnt); \
00238   TEST (fn argm (arg, vdouble1), type, fnt); \
00239   TEST (fn argm (arg, vint1), type, fnt); \
00240   TEST (fn argm (arg, vllong1), type, fnt); \
00241   TEST (fn argm (arg, Vfloat1), type, fnt); \
00242   TEST (fn argm (arg, Vdouble1), type, fnt); \
00243   TEST (fn argm (arg, Vint1), type, fnt); \
00244   TEST (fn argm (arg, Vllong1), type, fnt);
00245 #define NON_LDBL_CTEST(fn, argm, arg, type, fnt) \
00246   NON_LDBL_TEST(fn, argm, arg, type, fnt); \
00247   TEST (fn argm (arg, vcfloat1), type, fnt); \
00248   TEST (fn argm (arg, vcdouble1), type, fnt); \
00249   TEST (fn argm (arg, Vcfloat1), type, fnt); \
00250   TEST (fn argm (arg, Vcdouble1), type, fnt);
00251 #define BINARY_TEST(fn, fnt) \
00252   TEST (fn (vfloat1, vfloat2), float, fnt); \
00253   TEST (fn (Vfloat1, vfloat2), float, fnt); \
00254   TEST (fn (vfloat1, Vfloat2), float, fnt); \
00255   TEST (fn (Vfloat1, Vfloat2), float, fnt); \
00256   TEST (fn (vldouble1, vldouble2), ldouble, fnt); \
00257   TEST (fn (Vldouble1, vldouble2), ldouble, fnt); \
00258   TEST (fn (vldouble1, Vldouble2), ldouble, fnt); \
00259   TEST (fn (Vldouble1, Vldouble2), ldouble, fnt); \
00260   NON_LDBL_TEST (fn, FIRST, vldouble2, ldouble, fnt); \
00261   NON_LDBL_TEST (fn, SECOND, vldouble2, ldouble, fnt); \
00262   NON_LDBL_TEST (fn, FIRST, Vldouble2, ldouble, fnt); \
00263   NON_LDBL_TEST (fn, SECOND, Vldouble2, ldouble, fnt); \
00264   NON_LDBL_TEST (fn, FIRST, vdouble2, double, fnt); \
00265   NON_LDBL_TEST (fn, SECOND, vdouble2, double, fnt); \
00266   NON_LDBL_TEST (fn, FIRST, Vdouble2, double, fnt); \
00267   NON_LDBL_TEST (fn, SECOND, Vdouble2, double, fnt); \
00268   NON_LDBL_TEST (fn, FIRST, vint2, double, fnt); \
00269   NON_LDBL_TEST (fn, SECOND, vint2, double, fnt); \
00270   NON_LDBL_TEST (fn, FIRST, Vint2, double, fnt); \
00271   NON_LDBL_TEST (fn, SECOND, Vint2, double, fnt); \
00272   NON_LDBL_TEST (fn, FIRST, vllong2, double, fnt); \
00273   NON_LDBL_TEST (fn, SECOND, vllong2, double, fnt); \
00274   NON_LDBL_TEST (fn, FIRST, Vllong2, double, fnt); \
00275   NON_LDBL_TEST (fn, SECOND, Vllong2, double, fnt);
00276 #define BINARY_CTEST(fn, fnt) \
00277   BINARY_TEST (fn, fnt); \
00278   TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
00279   TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
00280   TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
00281   TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
00282   TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
00283   TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
00284   TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
00285   TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
00286   TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
00287   TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
00288   TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
00289   TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
00290   TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
00291   TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
00292   TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
00293   TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
00294   TEST (fn (vcfloat1, vcfloat2), cfloat, fnt); \
00295   TEST (fn (Vcfloat1, vcfloat2), cfloat, fnt); \
00296   TEST (fn (vcfloat1, Vcfloat2), cfloat, fnt); \
00297   TEST (fn (Vcfloat1, Vcfloat2), cfloat, fnt); \
00298   TEST (fn (vcldouble1, vcldouble2), cldouble, fnt); \
00299   TEST (fn (Vcldouble1, vcldouble2), cldouble, fnt); \
00300   TEST (fn (vcldouble1, Vcldouble2), cldouble, fnt); \
00301   TEST (fn (Vcldouble1, Vcldouble2), cldouble, fnt); \
00302   NON_LDBL_CTEST (fn, FIRST, vcldouble2, cldouble, fnt); \
00303   NON_LDBL_CTEST (fn, SECOND, vcldouble2, cldouble, fnt); \
00304   NON_LDBL_CTEST (fn, FIRST, Vcldouble2, cldouble, fnt); \
00305   NON_LDBL_CTEST (fn, SECOND, Vcldouble2, cldouble, fnt); \
00306   NON_LDBL_CTEST (fn, FIRST, vcdouble2, cdouble, fnt); \
00307   NON_LDBL_CTEST (fn, SECOND, vcdouble2, cdouble, fnt); \
00308   NON_LDBL_CTEST (fn, FIRST, Vcdouble2, cdouble, fnt); \
00309   NON_LDBL_CTEST (fn, SECOND, Vcdouble2, cdouble, fnt);
00310 
00311   BINARY_TEST (atan2, atan2);
00312 
00313 #define my_remquo(x, y) remquo (x, y, &quo)
00314   BINARY_TEST (my_remquo, remquo);
00315 #undef my_remquo
00316 
00317   BINARY_CTEST (pow, pow);
00318 
00319   /* Testing all arguments of fma would be just too expensive,
00320      so test just some.  */
00321 #define my_fma(x, y) fma (x, y, vfloat3)
00322   BINARY_TEST (my_fma, fma);
00323 #undef my_fma
00324 #define my_fma(x, y) fma (x, vfloat3, y)
00325   BINARY_TEST (my_fma, fma);
00326 #undef my_fma
00327 #define my_fma(x, y) fma (Vfloat3, x, y)
00328   BINARY_TEST (my_fma, fma);
00329 #undef my_fma
00330   TEST (fma (vdouble1, Vdouble2, vllong3), double, fma);
00331   TEST (fma (vint1, Vint2, vint3), double, fma);
00332   TEST (fma (Vldouble1, vldouble2, Vldouble3), ldouble, fma);
00333   TEST (fma (vldouble1, vint2, Vdouble3), ldouble, fma);
00334 
00335   return result;
00336 }
00337 
00338 int
00339 main (void)
00340 {
00341   return test (vint1, vllong1);
00342 }
00343 
00344 /* Now generate the three functions.  */
00345 #define HAVE_MAIN
00346 
00347 #define F(name) name
00348 #define TYPE double
00349 #define CTYPE cdouble
00350 #define T Tdouble
00351 #define C Tcdouble
00352 #include "test-tgmath2.c"
00353 
00354 #define F(name) name##f
00355 #define TYPE float
00356 #define CTYPE cfloat
00357 #define T Tfloat
00358 #define C Tcfloat
00359 #include "test-tgmath2.c"
00360 
00361 #ifndef NO_LONG_DOUBLE
00362 #define F(name) name##l
00363 #define TYPE ldouble
00364 #define CTYPE cldouble
00365 #define T Tldouble
00366 #define C Tcldouble
00367 #include "test-tgmath2.c"
00368 #endif
00369 
00370 #else
00371 
00372 #ifdef DEBUG
00373 #define P() puts (__FUNCTION__); count++
00374 #else
00375 #define P() count++;
00376 #endif
00377 
00378 TYPE
00379 (F(cos)) (TYPE x)
00380 {
00381   counts[T][C_cos]++;
00382   P ();
00383   return x;
00384 }
00385 
00386 CTYPE
00387 (F(ccos)) (CTYPE x)
00388 {
00389   counts[C][C_cos]++;
00390   P ();
00391   return x;
00392 }
00393 
00394 TYPE
00395 (F(fabs)) (TYPE x)
00396 {
00397   counts[T][C_fabs]++;
00398   P ();
00399   return x;
00400 }
00401 
00402 TYPE
00403 (F(cabs)) (CTYPE x)
00404 {
00405   counts[T][C_cabs]++;
00406   P ();
00407   return x;
00408 }
00409 
00410 CTYPE
00411 (F(conj)) (CTYPE x)
00412 {
00413   counts[C][C_conj]++;
00414   P ();
00415   return x;
00416 }
00417 
00418 TYPE
00419 (F(expm1)) (TYPE x)
00420 {
00421   counts[T][C_expm1]++;
00422   P ();
00423   return x;
00424 }
00425 
00426 long int
00427 (F(lrint)) (TYPE x)
00428 {
00429   counts[T][C_lrint]++;
00430   P ();
00431   return x;
00432 }
00433 
00434 TYPE
00435 (F(ldexp)) (TYPE x, int y)
00436 {
00437   counts[T][C_ldexp]++;
00438   P ();
00439   return x + y;
00440 }
00441 
00442 TYPE
00443 (F(atan2)) (TYPE x, TYPE y)
00444 {
00445   counts[T][C_atan2]++;
00446   P ();
00447   return x + y;
00448 }
00449 
00450 TYPE
00451 (F(remquo)) (TYPE x, TYPE y, int *z)
00452 {
00453   counts[T][C_remquo]++;
00454   P ();
00455   return x + y + *z;
00456 }
00457 
00458 TYPE
00459 (F(pow)) (TYPE x, TYPE y)
00460 {
00461   counts[T][C_pow]++;
00462   P ();
00463   return x + y;
00464 }
00465 
00466 CTYPE
00467 (F(cpow)) (CTYPE x, CTYPE y)
00468 {
00469   counts[C][C_pow]++;
00470   P ();
00471   return x + y;
00472 }
00473 
00474 TYPE
00475 (F(fma)) (TYPE x, TYPE y, TYPE z)
00476 {
00477   counts[T][C_fma]++;
00478   P ();
00479   return x + y + z;
00480 }
00481 
00482 #undef F
00483 #undef TYPE
00484 #undef CTYPE
00485 #undef T
00486 #undef C
00487 #undef P
00488 #endif