/math/test-tgmath2.c

https://github.com/cmatthew/Lind-GlibC · C · 488 lines · 425 code · 39 blank · 24 comment · 17 complexity · 7d2977ae60c555aa6a3c33df7a8f49a0 MD5 · raw file

  1. /* Test compilation of tgmath macros.
  2. Copyright (C) 2007 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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef HAVE_MAIN
  18. #undef __NO_MATH_INLINES
  19. #define __NO_MATH_INLINES 1
  20. #include <math.h>
  21. #include <complex.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <tgmath.h>
  25. //#define DEBUG
  26. typedef complex float cfloat;
  27. typedef complex double cdouble;
  28. #ifndef NO_LONG_DOUBLE
  29. typedef long double ldouble;
  30. typedef complex long double cldouble;
  31. #else
  32. typedef double ldouble;
  33. typedef complex double cldouble;
  34. #endif
  35. float vfloat1, vfloat2, vfloat3;
  36. double vdouble1, vdouble2, vdouble3;
  37. ldouble vldouble1, vldouble2, vldouble3;
  38. cfloat vcfloat1, vcfloat2, vcfloat3;
  39. cdouble vcdouble1, vcdouble2, vcdouble3;
  40. cldouble vcldouble1, vcldouble2, vcldouble4;
  41. int vint1, vint2, vint3;
  42. long int vlong1, vlong2, vlong3;
  43. long long int vllong1, vllong2, vllong3;
  44. const float Vfloat1 = 1, Vfloat2 = 2, Vfloat3 = 3;
  45. const double Vdouble1 = 1, Vdouble2 = 2, Vdouble3 = 3;
  46. const ldouble Vldouble1 = 1, Vldouble2 = 2, Vldouble3 = 3;
  47. const cfloat Vcfloat1 = 1, Vcfloat2 = 2, Vcfloat3 = 3;
  48. const cdouble Vcdouble1 = 1, Vcdouble2 = 2, Vcdouble3 = 3;
  49. const cldouble Vcldouble1 = 1, Vcldouble2 = 2, Vcldouble4 = 3;
  50. const int Vint1 = 1, Vint2 = 2, Vint3 = 3;
  51. const long int Vlong1 = 1, Vlong2 = 2, Vlong3 = 3;
  52. const long long int Vllong1 = 1, Vllong2 = 2, Vllong3 = 3;
  53. enum
  54. {
  55. Tfloat = 0,
  56. Tcfloat,
  57. Tdouble,
  58. Tcdouble,
  59. #ifndef NO_LONG_DOUBLE
  60. Tldouble,
  61. Tcldouble,
  62. #else
  63. Tldouble = Tdouble,
  64. Tcldouble = Tcdouble,
  65. #endif
  66. Tlast
  67. };
  68. enum
  69. {
  70. C_cos = 0,
  71. C_fabs,
  72. C_cabs,
  73. C_conj,
  74. C_expm1,
  75. C_lrint,
  76. C_ldexp,
  77. C_atan2,
  78. C_remquo,
  79. C_pow,
  80. C_fma,
  81. C_last
  82. };
  83. int count;
  84. int counts[Tlast][C_last];
  85. int
  86. test (const int Vint4, const long long int Vllong4)
  87. {
  88. int result = 0;
  89. int quo = 0;
  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"); \
  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. TEST (cos (vfloat1), float, cos);
  133. TEST (cos (vdouble1), double, cos);
  134. TEST (cos (vldouble1), ldouble, cos);
  135. TEST (cos (vint1), double, cos);
  136. TEST (cos (vllong1), double, cos);
  137. TEST (cos (vcfloat1), cfloat, cos);
  138. TEST (cos (vcdouble1), cdouble, cos);
  139. TEST (cos (vcldouble1), cldouble, cos);
  140. TEST (cos (Vfloat1), float, cos);
  141. TEST (cos (Vdouble1), double, cos);
  142. TEST (cos (Vldouble1), ldouble, cos);
  143. TEST (cos (Vint1), double, cos);
  144. TEST (cos (Vllong1), double, cos);
  145. TEST (cos (Vcfloat1), cfloat, cos);
  146. TEST (cos (Vcdouble1), cdouble, cos);
  147. TEST (cos (Vcldouble1), cldouble, cos);
  148. TEST (fabs (vfloat1), float, fabs);
  149. TEST (fabs (vdouble1), double, fabs);
  150. TEST (fabs (vldouble1), ldouble, fabs);
  151. TEST (fabs (vint1), double, fabs);
  152. TEST (fabs (vllong1), double, fabs);
  153. TEST (fabs (vcfloat1), float, cabs);
  154. TEST (fabs (vcdouble1), double, cabs);
  155. TEST (fabs (vcldouble1), ldouble, cabs);
  156. TEST (fabs (Vfloat1), float, fabs);
  157. TEST (fabs (Vdouble1), double, fabs);
  158. TEST (fabs (Vldouble1), ldouble, fabs);
  159. #ifndef __OPTIMIZE__
  160. /* GCC is too smart to optimize these out. */
  161. TEST (fabs (Vint1), double, fabs);
  162. TEST (fabs (Vllong1), double, fabs);
  163. #else
  164. TEST_TYPE_ONLY (fabs (vllong1), double);
  165. TEST_TYPE_ONLY (fabs (vllong1), double);
  166. #endif
  167. TEST (fabs (Vint4), double, fabs);
  168. TEST (fabs (Vllong4), double, fabs);
  169. TEST (fabs (Vcfloat1), float, cabs);
  170. TEST (fabs (Vcdouble1), double, cabs);
  171. TEST (fabs (Vcldouble1), ldouble, cabs);
  172. TEST (conj (vfloat1), cfloat, conj);
  173. TEST (conj (vdouble1), cdouble, conj);
  174. TEST (conj (vldouble1), cldouble, conj);
  175. TEST (conj (vint1), cdouble, conj);
  176. TEST (conj (vllong1), cdouble, conj);
  177. TEST (conj (vcfloat1), cfloat, conj);
  178. TEST (conj (vcdouble1), cdouble, conj);
  179. TEST (conj (vcldouble1), cldouble, conj);
  180. TEST (conj (Vfloat1), cfloat, conj);
  181. TEST (conj (Vdouble1), cdouble, conj);
  182. TEST (conj (Vldouble1), cldouble, conj);
  183. TEST (conj (Vint1), cdouble, conj);
  184. TEST (conj (Vllong1), cdouble, conj);
  185. TEST (conj (Vcfloat1), cfloat, conj);
  186. TEST (conj (Vcdouble1), cdouble, conj);
  187. TEST (conj (Vcldouble1), cldouble, conj);
  188. TEST (expm1 (vfloat1), float, expm1);
  189. TEST (expm1 (vdouble1), double, expm1);
  190. TEST (expm1 (vldouble1), ldouble, expm1);
  191. TEST (expm1 (vint1), double, expm1);
  192. TEST (expm1 (vllong1), double, expm1);
  193. TEST (expm1 (Vfloat1), float, expm1);
  194. TEST (expm1 (Vdouble1), double, expm1);
  195. TEST (expm1 (Vldouble1), ldouble, expm1);
  196. TEST (expm1 (Vint1), double, expm1);
  197. TEST (expm1 (Vllong1), double, expm1);
  198. TEST2 (lrint (vfloat1), float, long int, lrint);
  199. TEST2 (lrint (vdouble1), double, long int, lrint);
  200. TEST2 (lrint (vldouble1), ldouble, long int, lrint);
  201. TEST2 (lrint (vint1), double, long int, lrint);
  202. TEST2 (lrint (vllong1), double, long int, lrint);
  203. TEST2 (lrint (Vfloat1), float, long int, lrint);
  204. TEST2 (lrint (Vdouble1), double, long int, lrint);
  205. TEST2 (lrint (Vldouble1), ldouble, long int, lrint);
  206. TEST2 (lrint (Vint1), double, long int, lrint);
  207. TEST2 (lrint (Vllong1), double, long int, lrint);
  208. TEST (ldexp (vfloat1, 6), float, ldexp);
  209. TEST (ldexp (vdouble1, 6), double, ldexp);
  210. TEST (ldexp (vldouble1, 6), ldouble, ldexp);
  211. TEST (ldexp (vint1, 6), double, ldexp);
  212. TEST (ldexp (vllong1, 6), double, ldexp);
  213. TEST (ldexp (Vfloat1, 6), float, ldexp);
  214. TEST (ldexp (Vdouble1, 6), double, ldexp);
  215. TEST (ldexp (Vldouble1, 6), ldouble, ldexp);
  216. TEST (ldexp (Vint1, 6), double, ldexp);
  217. TEST (ldexp (Vllong1, 6), double, ldexp);
  218. #define FIRST(x, y) (y, x)
  219. #define SECOND(x, y) (x, y)
  220. #define NON_LDBL_TEST(fn, argm, arg, type, fnt) \
  221. TEST (fn argm (arg, vfloat1), type, fnt); \
  222. TEST (fn argm (arg, vdouble1), type, fnt); \
  223. TEST (fn argm (arg, vint1), type, fnt); \
  224. TEST (fn argm (arg, vllong1), type, fnt); \
  225. TEST (fn argm (arg, Vfloat1), type, fnt); \
  226. TEST (fn argm (arg, Vdouble1), type, fnt); \
  227. TEST (fn argm (arg, Vint1), type, fnt); \
  228. TEST (fn argm (arg, Vllong1), type, fnt);
  229. #define NON_LDBL_CTEST(fn, argm, arg, type, fnt) \
  230. NON_LDBL_TEST(fn, argm, arg, type, fnt); \
  231. TEST (fn argm (arg, vcfloat1), type, fnt); \
  232. TEST (fn argm (arg, vcdouble1), type, fnt); \
  233. TEST (fn argm (arg, Vcfloat1), type, fnt); \
  234. TEST (fn argm (arg, Vcdouble1), type, fnt);
  235. #define BINARY_TEST(fn, 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 (Vfloat1, Vfloat2), float, fnt); \
  240. TEST (fn (vldouble1, vldouble2), ldouble, fnt); \
  241. TEST (fn (Vldouble1, vldouble2), ldouble, fnt); \
  242. TEST (fn (vldouble1, Vldouble2), ldouble, fnt); \
  243. TEST (fn (Vldouble1, Vldouble2), ldouble, fnt); \
  244. NON_LDBL_TEST (fn, FIRST, vldouble2, ldouble, fnt); \
  245. NON_LDBL_TEST (fn, SECOND, vldouble2, ldouble, fnt); \
  246. NON_LDBL_TEST (fn, FIRST, Vldouble2, ldouble, fnt); \
  247. NON_LDBL_TEST (fn, SECOND, Vldouble2, ldouble, fnt); \
  248. NON_LDBL_TEST (fn, FIRST, vdouble2, double, fnt); \
  249. NON_LDBL_TEST (fn, SECOND, vdouble2, double, fnt); \
  250. NON_LDBL_TEST (fn, FIRST, Vdouble2, double, fnt); \
  251. NON_LDBL_TEST (fn, SECOND, Vdouble2, double, fnt); \
  252. NON_LDBL_TEST (fn, FIRST, vint2, double, fnt); \
  253. NON_LDBL_TEST (fn, SECOND, vint2, double, fnt); \
  254. NON_LDBL_TEST (fn, FIRST, Vint2, double, fnt); \
  255. NON_LDBL_TEST (fn, SECOND, Vint2, double, fnt); \
  256. NON_LDBL_TEST (fn, FIRST, vllong2, double, fnt); \
  257. NON_LDBL_TEST (fn, SECOND, vllong2, double, fnt); \
  258. NON_LDBL_TEST (fn, FIRST, Vllong2, double, fnt); \
  259. NON_LDBL_TEST (fn, SECOND, Vllong2, double, fnt);
  260. #define BINARY_CTEST(fn, fnt) \
  261. BINARY_TEST (fn, 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 (Vcfloat1, Vfloat2), cfloat, 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 (Vcldouble1, Vldouble2), cldouble, 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 (Vcfloat1, Vfloat2), cfloat, 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 (Vcldouble1, Vldouble2), cldouble, 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 (Vcfloat1, Vcfloat2), cfloat, fnt); \
  282. TEST (fn (vcldouble1, vcldouble2), cldouble, fnt); \
  283. TEST (fn (Vcldouble1, vcldouble2), cldouble, fnt); \
  284. TEST (fn (vcldouble1, Vcldouble2), cldouble, fnt); \
  285. TEST (fn (Vcldouble1, Vcldouble2), cldouble, fnt); \
  286. NON_LDBL_CTEST (fn, FIRST, vcldouble2, cldouble, fnt); \
  287. NON_LDBL_CTEST (fn, SECOND, vcldouble2, cldouble, fnt); \
  288. NON_LDBL_CTEST (fn, FIRST, Vcldouble2, cldouble, fnt); \
  289. NON_LDBL_CTEST (fn, SECOND, Vcldouble2, cldouble, fnt); \
  290. NON_LDBL_CTEST (fn, FIRST, vcdouble2, cdouble, fnt); \
  291. NON_LDBL_CTEST (fn, SECOND, vcdouble2, cdouble, fnt); \
  292. NON_LDBL_CTEST (fn, FIRST, Vcdouble2, cdouble, fnt); \
  293. NON_LDBL_CTEST (fn, SECOND, Vcdouble2, cdouble, fnt);
  294. BINARY_TEST (atan2, atan2);
  295. #define my_remquo(x, y) remquo (x, y, &quo)
  296. BINARY_TEST (my_remquo, remquo);
  297. #undef my_remquo
  298. BINARY_CTEST (pow, pow);
  299. /* Testing all arguments of fma would be just too expensive,
  300. so test just some. */
  301. #define my_fma(x, y) fma (x, y, vfloat3)
  302. BINARY_TEST (my_fma, fma);
  303. #undef my_fma
  304. #define my_fma(x, y) fma (x, vfloat3, y)
  305. BINARY_TEST (my_fma, fma);
  306. #undef my_fma
  307. #define my_fma(x, y) fma (Vfloat3, x, y)
  308. BINARY_TEST (my_fma, fma);
  309. #undef my_fma
  310. TEST (fma (vdouble1, Vdouble2, vllong3), double, fma);
  311. TEST (fma (vint1, Vint2, vint3), double, fma);
  312. TEST (fma (Vldouble1, vldouble2, Vldouble3), ldouble, fma);
  313. TEST (fma (vldouble1, vint2, Vdouble3), ldouble, fma);
  314. return result;
  315. }
  316. int
  317. main (void)
  318. {
  319. return test (vint1, vllong1);
  320. }
  321. /* Now generate the three functions. */
  322. #define HAVE_MAIN
  323. #define F(name) name
  324. #define TYPE double
  325. #define CTYPE cdouble
  326. #define T Tdouble
  327. #define C Tcdouble
  328. #include "test-tgmath2.c"
  329. #define F(name) name##f
  330. #define TYPE float
  331. #define CTYPE cfloat
  332. #define T Tfloat
  333. #define C Tcfloat
  334. #include "test-tgmath2.c"
  335. #ifndef NO_LONG_DOUBLE
  336. #define F(name) name##l
  337. #define TYPE ldouble
  338. #define CTYPE cldouble
  339. #define T Tldouble
  340. #define C Tcldouble
  341. #include "test-tgmath2.c"
  342. #endif
  343. #else
  344. #ifdef DEBUG
  345. #define P() puts (__FUNCTION__); count++
  346. #else
  347. #define P() count++;
  348. #endif
  349. TYPE
  350. (F(cos)) (TYPE x)
  351. {
  352. counts[T][C_cos]++;
  353. P ();
  354. return x;
  355. }
  356. CTYPE
  357. (F(ccos)) (CTYPE x)
  358. {
  359. counts[C][C_cos]++;
  360. P ();
  361. return x;
  362. }
  363. TYPE
  364. (F(fabs)) (TYPE x)
  365. {
  366. counts[T][C_fabs]++;
  367. P ();
  368. return x;
  369. }
  370. TYPE
  371. (F(cabs)) (CTYPE x)
  372. {
  373. counts[T][C_cabs]++;
  374. P ();
  375. return x;
  376. }
  377. CTYPE
  378. (F(conj)) (CTYPE x)
  379. {
  380. counts[C][C_conj]++;
  381. P ();
  382. return x;
  383. }
  384. TYPE
  385. (F(expm1)) (TYPE x)
  386. {
  387. counts[T][C_expm1]++;
  388. P ();
  389. return x;
  390. }
  391. long int
  392. (F(lrint)) (TYPE x)
  393. {
  394. counts[T][C_lrint]++;
  395. P ();
  396. return x;
  397. }
  398. TYPE
  399. (F(ldexp)) (TYPE x, int y)
  400. {
  401. counts[T][C_ldexp]++;
  402. P ();
  403. return x + y;
  404. }
  405. TYPE
  406. (F(atan2)) (TYPE x, TYPE y)
  407. {
  408. counts[T][C_atan2]++;
  409. P ();
  410. return x + y;
  411. }
  412. TYPE
  413. (F(remquo)) (TYPE x, TYPE y, int *z)
  414. {
  415. counts[T][C_remquo]++;
  416. P ();
  417. return x + y + *z;
  418. }
  419. TYPE
  420. (F(pow)) (TYPE x, TYPE y)
  421. {
  422. counts[T][C_pow]++;
  423. P ();
  424. return x + y;
  425. }
  426. CTYPE
  427. (F(cpow)) (CTYPE x, CTYPE y)
  428. {
  429. counts[C][C_pow]++;
  430. P ();
  431. return x + y;
  432. }
  433. TYPE
  434. (F(fma)) (TYPE x, TYPE y, TYPE z)
  435. {
  436. counts[T][C_fma]++;
  437. P ();
  438. return x + y + z;
  439. }
  440. #undef F
  441. #undef TYPE
  442. #undef CTYPE
  443. #undef T
  444. #undef C
  445. #undef P
  446. #endif