test-tgmath2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Test compilation of tgmath macros.
  2. Copyright (C) 2007-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef HAVE_MAIN
  17. #undef __NO_MATH_INLINES
  18. #define __NO_MATH_INLINES 1
  19. #include <math.h>
  20. #include <complex.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <tgmath.h>
  24. //#define DEBUG
  25. typedef complex float cfloat;
  26. typedef complex double cdouble;
  27. #ifndef NO_LONG_DOUBLE
  28. typedef long double ldouble;
  29. typedef complex long double cldouble;
  30. #else
  31. typedef double ldouble;
  32. typedef complex double cldouble;
  33. #endif
  34. float vfloat1, vfloat2, vfloat3;
  35. double vdouble1, vdouble2, vdouble3;
  36. ldouble vldouble1, vldouble2, vldouble3;
  37. cfloat vcfloat1, vcfloat2, vcfloat3;
  38. cdouble vcdouble1, vcdouble2, vcdouble3;
  39. cldouble vcldouble1, vcldouble2, vcldouble4;
  40. int vint1, vint2, vint3;
  41. long int vlong1, vlong2, vlong3;
  42. long long int vllong1, vllong2, vllong3;
  43. const float Vfloat1 = 1, Vfloat2 = 2, Vfloat3 = 3;
  44. const double Vdouble1 = 1, Vdouble2 = 2, Vdouble3 = 3;
  45. const ldouble Vldouble1 = 1, Vldouble2 = 2, Vldouble3 = 3;
  46. const cfloat Vcfloat1 = 1, Vcfloat2 = 2, Vcfloat3 = 3;
  47. const cdouble Vcdouble1 = 1, Vcdouble2 = 2, Vcdouble3 = 3;
  48. const cldouble Vcldouble1 = 1, Vcldouble2 = 2, Vcldouble4 = 3;
  49. const int Vint1 = 1, Vint2 = 2, Vint3 = 3;
  50. const long int Vlong1 = 1, Vlong2 = 2, Vlong3 = 3;
  51. const long long int Vllong1 = 1, Vllong2 = 2, Vllong3 = 3;
  52. enum
  53. {
  54. Tfloat = 0,
  55. Tcfloat,
  56. Tdouble,
  57. Tcdouble,
  58. #ifndef NO_LONG_DOUBLE
  59. Tldouble,
  60. Tcldouble,
  61. #else
  62. Tldouble = Tdouble,
  63. Tcldouble = Tcdouble,
  64. #endif
  65. Tlast
  66. };
  67. enum
  68. {
  69. C_cos = 0,
  70. C_fabs,
  71. C_cabs,
  72. C_conj,
  73. C_expm1,
  74. C_lrint,
  75. C_ldexp,
  76. C_atan2,
  77. C_remquo,
  78. C_pow,
  79. C_fma,
  80. C_last
  81. };
  82. int count;
  83. int counts[Tlast][C_last];
  84. int
  85. test (const int Vint4, const long long int Vllong4)
  86. {
  87. int result = 0;
  88. int quo = 0;
  89. #define FAIL(str) \
  90. do \
  91. { \
  92. printf ("%s failure on line %d\n", (str), __LINE__); \
  93. result = 1; \
  94. } \
  95. while (0)
  96. #define TEST_TYPE_ONLY(expr, rettype) \
  97. do \
  98. { \
  99. __typeof__ (expr) texpr = 0; \
  100. __typeof__ (rettype) ttype = 0, *ptype; \
  101. if (sizeof (expr) != sizeof (rettype)) \
  102. FAIL ("type"); \
  103. if (__alignof__ (expr) != __alignof__ (rettype)) \
  104. FAIL ("type"); \
  105. __asm ("" : "=r" (ptype) : "0" (&ttype), "r" (&texpr)); \
  106. if (&texpr == ptype) \
  107. FAIL ("type"); \
  108. } \
  109. while (0)
  110. #define TEST2(expr, type, rettype, fn) \
  111. do \
  112. { \
  113. __typeof__ (expr) texpr = 0; \
  114. TEST_TYPE_ONLY (expr, rettype); \
  115. if (count != 0) \
  116. FAIL ("internal error"); \
  117. if (counts[T##type][C_##fn] != 0) \
  118. FAIL ("internal error"); \
  119. texpr = expr; \
  120. __asm __volatile ("" : : "r" (&texpr)); \
  121. if (count != 1 || counts[T##type][C_##fn] != 1) \
  122. { \
  123. FAIL ("wrong function called"); \
  124. memset (counts, 0, sizeof (counts)); \
  125. } \
  126. count = 0; \
  127. counts[T##type][C_##fn] = 0; \
  128. } \
  129. while (0)
  130. #define TEST(expr, type, fn) TEST2(expr, type, type, fn)
  131. TEST (cos (vfloat1), float, cos);
  132. TEST (cos (vdouble1), double, cos);
  133. TEST (cos (vldouble1), ldouble, cos);
  134. TEST (cos (vint1), double, cos);
  135. TEST (cos (vllong1), double, cos);
  136. TEST (cos (vcfloat1), cfloat, cos);
  137. TEST (cos (vcdouble1), cdouble, cos);
  138. TEST (cos (vcldouble1), cldouble, cos);
  139. TEST (cos (Vfloat1), float, cos);
  140. TEST (cos (Vdouble1), double, cos);
  141. TEST (cos (Vldouble1), ldouble, cos);
  142. TEST (cos (Vint1), double, cos);
  143. TEST (cos (Vllong1), double, cos);
  144. TEST (cos (Vcfloat1), cfloat, cos);
  145. TEST (cos (Vcdouble1), cdouble, cos);
  146. TEST (cos (Vcldouble1), cldouble, cos);
  147. TEST (fabs (vfloat1), float, fabs);
  148. TEST (fabs (vdouble1), double, fabs);
  149. TEST (fabs (vldouble1), ldouble, fabs);
  150. TEST (fabs (vint1), double, fabs);
  151. TEST (fabs (vllong1), double, fabs);
  152. TEST (fabs (vcfloat1), float, cabs);
  153. TEST (fabs (vcdouble1), double, cabs);
  154. TEST (fabs (vcldouble1), ldouble, cabs);
  155. TEST (fabs (Vfloat1), float, fabs);
  156. TEST (fabs (Vdouble1), double, fabs);
  157. TEST (fabs (Vldouble1), ldouble, fabs);
  158. #ifndef __OPTIMIZE__
  159. /* GCC is too smart to optimize these out. */
  160. TEST (fabs (Vint1), double, fabs);
  161. TEST (fabs (Vllong1), double, fabs);
  162. #else
  163. TEST_TYPE_ONLY (fabs (vllong1), double);
  164. TEST_TYPE_ONLY (fabs (vllong1), double);
  165. #endif
  166. TEST (fabs (Vint4), double, fabs);
  167. TEST (fabs (Vllong4), double, fabs);
  168. TEST (fabs (Vcfloat1), float, cabs);
  169. TEST (fabs (Vcdouble1), double, cabs);
  170. TEST (fabs (Vcldouble1), ldouble, cabs);
  171. TEST (conj (vfloat1), cfloat, conj);
  172. TEST (conj (vdouble1), cdouble, conj);
  173. TEST (conj (vldouble1), cldouble, conj);
  174. TEST (conj (vint1), cdouble, conj);
  175. TEST (conj (vllong1), cdouble, conj);
  176. TEST (conj (vcfloat1), cfloat, conj);
  177. TEST (conj (vcdouble1), cdouble, conj);
  178. TEST (conj (vcldouble1), cldouble, conj);
  179. TEST (conj (Vfloat1), cfloat, conj);
  180. TEST (conj (Vdouble1), cdouble, conj);
  181. TEST (conj (Vldouble1), cldouble, conj);
  182. TEST (conj (Vint1), cdouble, conj);
  183. TEST (conj (Vllong1), cdouble, conj);
  184. TEST (conj (Vcfloat1), cfloat, conj);
  185. TEST (conj (Vcdouble1), cdouble, conj);
  186. TEST (conj (Vcldouble1), cldouble, conj);
  187. TEST (expm1 (vfloat1), float, expm1);
  188. TEST (expm1 (vdouble1), double, expm1);
  189. TEST (expm1 (vldouble1), ldouble, expm1);
  190. TEST (expm1 (vint1), double, expm1);
  191. TEST (expm1 (vllong1), double, expm1);
  192. TEST (expm1 (Vfloat1), float, expm1);
  193. TEST (expm1 (Vdouble1), double, expm1);
  194. TEST (expm1 (Vldouble1), ldouble, expm1);
  195. TEST (expm1 (Vint1), double, expm1);
  196. TEST (expm1 (Vllong1), double, expm1);
  197. TEST2 (lrint (vfloat1), float, long int, lrint);
  198. TEST2 (lrint (vdouble1), double, long int, lrint);
  199. TEST2 (lrint (vldouble1), ldouble, long int, lrint);
  200. TEST2 (lrint (vint1), double, long int, lrint);
  201. TEST2 (lrint (vllong1), double, long int, lrint);
  202. TEST2 (lrint (Vfloat1), float, long int, lrint);
  203. TEST2 (lrint (Vdouble1), double, long int, lrint);
  204. TEST2 (lrint (Vldouble1), ldouble, long int, lrint);
  205. TEST2 (lrint (Vint1), double, long int, lrint);
  206. TEST2 (lrint (Vllong1), double, long int, lrint);
  207. TEST (ldexp (vfloat1, 6), float, ldexp);
  208. TEST (ldexp (vdouble1, 6), double, ldexp);
  209. TEST (ldexp (vldouble1, 6), ldouble, ldexp);
  210. TEST (ldexp (vint1, 6), double, ldexp);
  211. TEST (ldexp (vllong1, 6), double, ldexp);
  212. TEST (ldexp (Vfloat1, 6), float, ldexp);
  213. TEST (ldexp (Vdouble1, 6), double, ldexp);
  214. TEST (ldexp (Vldouble1, 6), ldouble, ldexp);
  215. TEST (ldexp (Vint1, 6), double, ldexp);
  216. TEST (ldexp (Vllong1, 6), double, ldexp);
  217. #define FIRST(x, y) (y, x)
  218. #define SECOND(x, y) (x, y)
  219. #define NON_LDBL_TEST(fn, argm, arg, type, fnt) \
  220. TEST (fn argm (arg, vfloat1), type, fnt); \
  221. TEST (fn argm (arg, vdouble1), type, fnt); \
  222. TEST (fn argm (arg, vint1), type, fnt); \
  223. TEST (fn argm (arg, vllong1), type, fnt); \
  224. TEST (fn argm (arg, Vfloat1), type, fnt); \
  225. TEST (fn argm (arg, Vdouble1), type, fnt); \
  226. TEST (fn argm (arg, Vint1), type, fnt); \
  227. TEST (fn argm (arg, Vllong1), type, fnt);
  228. #define NON_LDBL_CTEST(fn, argm, arg, type, fnt) \
  229. NON_LDBL_TEST(fn, argm, arg, type, fnt); \
  230. TEST (fn argm (arg, vcfloat1), type, fnt); \
  231. TEST (fn argm (arg, vcdouble1), type, fnt); \
  232. TEST (fn argm (arg, Vcfloat1), type, fnt); \
  233. TEST (fn argm (arg, Vcdouble1), type, fnt);
  234. #define BINARY_TEST(fn, fnt) \
  235. TEST (fn (vfloat1, vfloat2), float, fnt); \
  236. TEST (fn (Vfloat1, vfloat2), float, fnt); \
  237. TEST (fn (vfloat1, Vfloat2), float, fnt); \
  238. TEST (fn (Vfloat1, Vfloat2), float, fnt); \
  239. TEST (fn (vldouble1, vldouble2), ldouble, fnt); \
  240. TEST (fn (Vldouble1, vldouble2), ldouble, fnt); \
  241. TEST (fn (vldouble1, Vldouble2), ldouble, fnt); \
  242. TEST (fn (Vldouble1, Vldouble2), ldouble, fnt); \
  243. NON_LDBL_TEST (fn, FIRST, vldouble2, ldouble, fnt); \
  244. NON_LDBL_TEST (fn, SECOND, vldouble2, ldouble, fnt); \
  245. NON_LDBL_TEST (fn, FIRST, Vldouble2, ldouble, fnt); \
  246. NON_LDBL_TEST (fn, SECOND, Vldouble2, ldouble, fnt); \
  247. NON_LDBL_TEST (fn, FIRST, vdouble2, double, fnt); \
  248. NON_LDBL_TEST (fn, SECOND, vdouble2, double, fnt); \
  249. NON_LDBL_TEST (fn, FIRST, Vdouble2, double, fnt); \
  250. NON_LDBL_TEST (fn, SECOND, Vdouble2, double, fnt); \
  251. NON_LDBL_TEST (fn, FIRST, vint2, double, fnt); \
  252. NON_LDBL_TEST (fn, SECOND, vint2, double, fnt); \
  253. NON_LDBL_TEST (fn, FIRST, Vint2, double, fnt); \
  254. NON_LDBL_TEST (fn, SECOND, Vint2, double, fnt); \
  255. NON_LDBL_TEST (fn, FIRST, vllong2, double, fnt); \
  256. NON_LDBL_TEST (fn, SECOND, vllong2, double, fnt); \
  257. NON_LDBL_TEST (fn, FIRST, Vllong2, double, fnt); \
  258. NON_LDBL_TEST (fn, SECOND, Vllong2, double, fnt);
  259. #define BINARY_CTEST(fn, fnt) \
  260. BINARY_TEST (fn, fnt); \
  261. TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
  262. TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
  263. TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
  264. TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
  265. TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
  266. TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
  267. TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
  268. TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
  269. TEST (fn (vcfloat1, vfloat2), cfloat, fnt); \
  270. TEST (fn (Vcfloat1, vfloat2), cfloat, fnt); \
  271. TEST (fn (vcfloat1, Vfloat2), cfloat, fnt); \
  272. TEST (fn (Vcfloat1, Vfloat2), cfloat, fnt); \
  273. TEST (fn (vcldouble1, vldouble2), cldouble, fnt); \
  274. TEST (fn (Vcldouble1, vldouble2), cldouble, fnt); \
  275. TEST (fn (vcldouble1, Vldouble2), cldouble, fnt); \
  276. TEST (fn (Vcldouble1, Vldouble2), cldouble, fnt); \
  277. TEST (fn (vcfloat1, vcfloat2), cfloat, fnt); \
  278. TEST (fn (Vcfloat1, vcfloat2), cfloat, fnt); \
  279. TEST (fn (vcfloat1, Vcfloat2), cfloat, fnt); \
  280. TEST (fn (Vcfloat1, Vcfloat2), cfloat, fnt); \
  281. TEST (fn (vcldouble1, vcldouble2), cldouble, fnt); \
  282. TEST (fn (Vcldouble1, vcldouble2), cldouble, fnt); \
  283. TEST (fn (vcldouble1, Vcldouble2), cldouble, fnt); \
  284. TEST (fn (Vcldouble1, Vcldouble2), cldouble, fnt); \
  285. NON_LDBL_CTEST (fn, FIRST, vcldouble2, cldouble, fnt); \
  286. NON_LDBL_CTEST (fn, SECOND, vcldouble2, cldouble, fnt); \
  287. NON_LDBL_CTEST (fn, FIRST, Vcldouble2, cldouble, fnt); \
  288. NON_LDBL_CTEST (fn, SECOND, Vcldouble2, cldouble, fnt); \
  289. NON_LDBL_CTEST (fn, FIRST, vcdouble2, cdouble, fnt); \
  290. NON_LDBL_CTEST (fn, SECOND, vcdouble2, cdouble, fnt); \
  291. NON_LDBL_CTEST (fn, FIRST, Vcdouble2, cdouble, fnt); \
  292. NON_LDBL_CTEST (fn, SECOND, Vcdouble2, cdouble, fnt);
  293. BINARY_TEST (atan2, atan2);
  294. #define my_remquo(x, y) remquo (x, y, &quo)
  295. BINARY_TEST (my_remquo, remquo);
  296. #undef my_remquo
  297. BINARY_CTEST (pow, pow);
  298. /* Testing all arguments of fma would be just too expensive,
  299. so test just some. */
  300. #define my_fma(x, y) fma (x, y, vfloat3)
  301. BINARY_TEST (my_fma, fma);
  302. #undef my_fma
  303. #define my_fma(x, y) fma (x, vfloat3, y)
  304. BINARY_TEST (my_fma, fma);
  305. #undef my_fma
  306. #define my_fma(x, y) fma (Vfloat3, x, y)
  307. BINARY_TEST (my_fma, fma);
  308. #undef my_fma
  309. TEST (fma (vdouble1, Vdouble2, vllong3), double, fma);
  310. TEST (fma (vint1, Vint2, vint3), double, fma);
  311. TEST (fma (Vldouble1, vldouble2, Vldouble3), ldouble, fma);
  312. TEST (fma (vldouble1, vint2, Vdouble3), ldouble, fma);
  313. return result;
  314. }
  315. static int
  316. do_test (void)
  317. {
  318. return test (vint1, vllong1);
  319. }
  320. /* Now generate the three functions. */
  321. #define HAVE_MAIN
  322. #define F(name) name
  323. #define TYPE double
  324. #define CTYPE cdouble
  325. #define T Tdouble
  326. #define C Tcdouble
  327. #include "test-tgmath2.c"
  328. #define F(name) name##f
  329. #define TYPE float
  330. #define CTYPE cfloat
  331. #define T Tfloat
  332. #define C Tcfloat
  333. #include "test-tgmath2.c"
  334. #ifndef NO_LONG_DOUBLE
  335. #define F(name) name##l
  336. #define TYPE ldouble
  337. #define CTYPE cldouble
  338. #define T Tldouble
  339. #define C Tcldouble
  340. #include "test-tgmath2.c"
  341. #endif
  342. #define TEST_FUNCTION do_test ()
  343. #include "../test-skeleton.c"
  344. #else
  345. #ifdef DEBUG
  346. #define P() puts (__FUNCTION__); count++
  347. #else
  348. #define P() count++;
  349. #endif
  350. TYPE
  351. (F(cos)) (TYPE x)
  352. {
  353. counts[T][C_cos]++;
  354. P ();
  355. return x;
  356. }
  357. CTYPE
  358. (F(ccos)) (CTYPE x)
  359. {
  360. counts[C][C_cos]++;
  361. P ();
  362. return x;
  363. }
  364. TYPE
  365. (F(fabs)) (TYPE x)
  366. {
  367. counts[T][C_fabs]++;
  368. P ();
  369. return x;
  370. }
  371. TYPE
  372. (F(cabs)) (CTYPE x)
  373. {
  374. counts[T][C_cabs]++;
  375. P ();
  376. return x;
  377. }
  378. CTYPE
  379. (F(conj)) (CTYPE x)
  380. {
  381. counts[C][C_conj]++;
  382. P ();
  383. return x;
  384. }
  385. TYPE
  386. (F(expm1)) (TYPE x)
  387. {
  388. counts[T][C_expm1]++;
  389. P ();
  390. return x;
  391. }
  392. long int
  393. (F(lrint)) (TYPE x)
  394. {
  395. counts[T][C_lrint]++;
  396. P ();
  397. return x;
  398. }
  399. TYPE
  400. (F(ldexp)) (TYPE x, int y)
  401. {
  402. counts[T][C_ldexp]++;
  403. P ();
  404. return x + y;
  405. }
  406. TYPE
  407. (F(atan2)) (TYPE x, TYPE y)
  408. {
  409. counts[T][C_atan2]++;
  410. P ();
  411. return x + y;
  412. }
  413. TYPE
  414. (F(remquo)) (TYPE x, TYPE y, int *z)
  415. {
  416. counts[T][C_remquo]++;
  417. P ();
  418. return x + y + *z;
  419. }
  420. TYPE
  421. (F(pow)) (TYPE x, TYPE y)
  422. {
  423. counts[T][C_pow]++;
  424. P ();
  425. return x + y;
  426. }
  427. CTYPE
  428. (F(cpow)) (CTYPE x, CTYPE y)
  429. {
  430. counts[C][C_pow]++;
  431. P ();
  432. return x + y;
  433. }
  434. TYPE
  435. (F(fma)) (TYPE x, TYPE y, TYPE z)
  436. {
  437. counts[T][C_fma]++;
  438. P ();
  439. return x + y + z;
  440. }
  441. #undef F
  442. #undef TYPE
  443. #undef CTYPE
  444. #undef T
  445. #undef C
  446. #undef P
  447. #endif