test-misc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /* Miscellaneous tests which don't fit anywhere else.
  2. Copyright (C) 2000-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <fenv.h>
  16. #include <float.h>
  17. #include <ieee754.h>
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "math-tests.h"
  22. static int
  23. do_test (void)
  24. {
  25. int result = 0;
  26. #ifndef NO_LONG_DOUBLE
  27. {
  28. long double x = 0x100000001ll + (long double) 0.5;
  29. long double q;
  30. long double r;
  31. r = modfl (x, &q);
  32. if (q != (long double) 0x100000001ll || r != 0.5)
  33. {
  34. printf ("modfl (%Lg, ...) failed\n", x);
  35. result = 1;
  36. }
  37. }
  38. {
  39. long double x;
  40. long double m;
  41. long double r;
  42. int e;
  43. int i;
  44. # if LDBL_MANT_DIG == 64
  45. m = 0xf.fffffffffffffffp-4L;
  46. # elif LDBL_MANT_DIG == 106
  47. /* This has to match the mantissa of LDBL_MAX which actually does have a
  48. missing bit in the middle. */
  49. m = 0x1.fffffffffffff7ffffffffffff8p-1L;
  50. # elif LDBL_MANT_DIG == 113
  51. m = 0x1.ffffffffffffffffffffffffffffp-1L;
  52. # else
  53. # error "Please adjust"
  54. # endif
  55. for (i = LDBL_MAX_EXP, x = LDBL_MAX; i >= LDBL_MIN_EXP; --i, x /= 2.0L)
  56. {
  57. printf ("2^%d: ", i);
  58. r = frexpl (x, &e);
  59. if (r != m)
  60. {
  61. printf ("mantissa incorrect: %.20La\n", r);
  62. result = 1;
  63. continue;
  64. }
  65. if (e != i)
  66. {
  67. printf ("exponent wrong %d (%.20Lg)\n", e, x);
  68. result = 1;
  69. continue;
  70. }
  71. puts ("ok");
  72. }
  73. for (i = LDBL_MIN_EXP, x = LDBL_MIN; i >= LDBL_MIN_EXP - LDBL_MANT_DIG + 1;
  74. --i, x /= 2.0L)
  75. {
  76. printf ("2^%d: ", i);
  77. r = frexpl (x, &e);
  78. if (r != 0.5L)
  79. {
  80. printf ("mantissa incorrect: %.20La\n", r);
  81. result = 1;
  82. continue;
  83. }
  84. if (e != i)
  85. {
  86. printf ("exponent wrong %d (%.20Lg)\n", e, x);
  87. result = 1;
  88. continue;
  89. }
  90. puts ("ok");
  91. }
  92. }
  93. # if 0
  94. {
  95. int e;
  96. long double r = frexpl (LDBL_MIN * LDBL_EPSILON, &e);
  97. if (r != 0.5)
  98. {
  99. printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): mantissa wrong: %Lg\n",
  100. r);
  101. result = 1;
  102. }
  103. else if (e != -16444)
  104. {
  105. printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): exponent wrong: %d\n",
  106. e);
  107. result = 1;
  108. }
  109. }
  110. # endif
  111. #endif
  112. {
  113. double x = 0x100000001ll + (double) 0.5;
  114. double q;
  115. double r;
  116. r = modf (x, &q);
  117. if (q != (double) 0x100000001ll || r != 0.5)
  118. {
  119. printf ("modf (%g, ...) failed\n", x);
  120. result = 1;
  121. }
  122. }
  123. {
  124. union ieee754_float v1;
  125. union ieee754_float v2;
  126. float f;
  127. v1.f = f = FLT_MIN;
  128. if (fpclassify (f) != FP_NORMAL)
  129. {
  130. printf ("fpclassify (FLT_MIN) failed: %d\n", fpclassify (f));
  131. result = 1;
  132. }
  133. f = nextafterf (f, FLT_MIN / 2.0f);
  134. if (fpclassify (f) != FP_SUBNORMAL)
  135. {
  136. printf ("fpclassify (FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
  137. result = 1;
  138. }
  139. v2.f = f = nextafterf (f, FLT_MIN);
  140. if (fpclassify (f) != FP_NORMAL)
  141. {
  142. printf ("fpclassify (FLT_MIN-epsilon+epsilon) failed: %d\n",
  143. fpclassify (f));
  144. result = 1;
  145. }
  146. if (v1.ieee.mantissa != v2.ieee.mantissa)
  147. {
  148. printf ("FLT_MIN: mantissa differs: %8x vs %8x\n",
  149. v1.ieee.mantissa, v2.ieee.mantissa);
  150. result = 1;
  151. }
  152. if (v1.ieee.exponent != v2.ieee.exponent)
  153. {
  154. printf ("FLT_MIN: exponent differs: %4x vs %4x\n",
  155. v1.ieee.exponent, v2.ieee.exponent);
  156. result = 1;
  157. }
  158. if (v1.ieee.negative != v2.ieee.negative)
  159. {
  160. printf ("FLT_MIN: negative differs: %d vs %d\n",
  161. v1.ieee.negative, v2.ieee.negative);
  162. result = 1;
  163. }
  164. v1.f = f = -FLT_MIN;
  165. if (fpclassify (f) != FP_NORMAL)
  166. {
  167. printf ("fpclassify (-FLT_MIN) failed: %d\n", fpclassify (f));
  168. result = 1;
  169. }
  170. f = nextafterf (f, -FLT_MIN / 2.0f);
  171. if (fpclassify (f) != FP_SUBNORMAL)
  172. {
  173. printf ("fpclassify (-FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
  174. result = 1;
  175. }
  176. v2.f = f = nextafterf (f, -FLT_MIN);
  177. if (fpclassify (f) != FP_NORMAL)
  178. {
  179. printf ("fpclassify (-FLT_MIN-epsilon+epsilon) failed: %d\n",
  180. fpclassify (f));
  181. result = 1;
  182. }
  183. if (v1.ieee.mantissa != v2.ieee.mantissa)
  184. {
  185. printf ("-FLT_MIN: mantissa differs: %8x vs %8x\n",
  186. v1.ieee.mantissa, v2.ieee.mantissa);
  187. result = 1;
  188. }
  189. if (v1.ieee.exponent != v2.ieee.exponent)
  190. {
  191. printf ("-FLT_MIN: exponent differs: %4x vs %4x\n",
  192. v1.ieee.exponent, v2.ieee.exponent);
  193. result = 1;
  194. }
  195. if (v1.ieee.negative != v2.ieee.negative)
  196. {
  197. printf ("-FLT_MIN: negative differs: %d vs %d\n",
  198. v1.ieee.negative, v2.ieee.negative);
  199. result = 1;
  200. }
  201. f = FLT_MAX;
  202. if (fpclassify (f) != FP_NORMAL)
  203. {
  204. printf ("fpclassify (FLT_MAX) failed: %d\n", fpclassify (f));
  205. result = 1;
  206. }
  207. f = nextafterf (f, INFINITY);
  208. if (fpclassify (f) != FP_INFINITE)
  209. {
  210. printf ("fpclassify (FLT_MAX+epsilon) failed: %d\n", fpclassify (f));
  211. result = 1;
  212. }
  213. f = -FLT_MAX;
  214. if (fpclassify (f) != FP_NORMAL)
  215. {
  216. printf ("fpclassify (-FLT_MAX) failed: %d\n", fpclassify (f));
  217. result = 1;
  218. }
  219. f = nextafterf (f, -INFINITY);
  220. if (fpclassify (f) != FP_INFINITE)
  221. {
  222. printf ("fpclassify (-FLT_MAX-epsilon) failed: %d\n", fpclassify (f));
  223. result = 1;
  224. }
  225. v1.f = f = 0.0625;
  226. f = nextafterf (f, 0.0);
  227. v2.f = f = nextafterf (f, 1.0);
  228. if (v1.ieee.mantissa != v2.ieee.mantissa)
  229. {
  230. printf ("0.0625f down: mantissa differs: %8x vs %8x\n",
  231. v1.ieee.mantissa, v2.ieee.mantissa);
  232. result = 1;
  233. }
  234. if (v1.ieee.exponent != v2.ieee.exponent)
  235. {
  236. printf ("0.0625f down: exponent differs: %4x vs %4x\n",
  237. v1.ieee.exponent, v2.ieee.exponent);
  238. result = 1;
  239. }
  240. if (v1.ieee.negative != v2.ieee.negative)
  241. {
  242. printf ("0.0625f down: negative differs: %d vs %d\n",
  243. v1.ieee.negative, v2.ieee.negative);
  244. result = 1;
  245. }
  246. v1.f = f = 0.0625;
  247. f = nextafterf (f, 1.0);
  248. v2.f = f = nextafterf (f, 0.0);
  249. if (v1.ieee.mantissa != v2.ieee.mantissa)
  250. {
  251. printf ("0.0625f up: mantissa differs: %8x vs %8x\n",
  252. v1.ieee.mantissa, v2.ieee.mantissa);
  253. result = 1;
  254. }
  255. if (v1.ieee.exponent != v2.ieee.exponent)
  256. {
  257. printf ("0.0625f up: exponent differs: %4x vs %4x\n",
  258. v1.ieee.exponent, v2.ieee.exponent);
  259. result = 1;
  260. }
  261. if (v1.ieee.negative != v2.ieee.negative)
  262. {
  263. printf ("0.0625f up: negative differs: %d vs %d\n",
  264. v1.ieee.negative, v2.ieee.negative);
  265. result = 1;
  266. }
  267. v1.f = f = -0.0625;
  268. f = nextafterf (f, 0.0);
  269. v2.f = f = nextafterf (f, -1.0);
  270. if (v1.ieee.mantissa != v2.ieee.mantissa)
  271. {
  272. printf ("-0.0625f up: mantissa differs: %8x vs %8x\n",
  273. v1.ieee.mantissa, v2.ieee.mantissa);
  274. result = 1;
  275. }
  276. if (v1.ieee.exponent != v2.ieee.exponent)
  277. {
  278. printf ("-0.0625f up: exponent differs: %4x vs %4x\n",
  279. v1.ieee.exponent, v2.ieee.exponent);
  280. result = 1;
  281. }
  282. if (v1.ieee.negative != v2.ieee.negative)
  283. {
  284. printf ("-0.0625f up: negative differs: %d vs %d\n",
  285. v1.ieee.negative, v2.ieee.negative);
  286. result = 1;
  287. }
  288. v1.f = f = -0.0625;
  289. f = nextafterf (f, -1.0);
  290. v2.f = f = nextafterf (f, 0.0);
  291. if (v1.ieee.mantissa != v2.ieee.mantissa)
  292. {
  293. printf ("-0.0625f down: mantissa differs: %8x vs %8x\n",
  294. v1.ieee.mantissa, v2.ieee.mantissa);
  295. result = 1;
  296. }
  297. if (v1.ieee.exponent != v2.ieee.exponent)
  298. {
  299. printf ("-0.0625f down: exponent differs: %4x vs %4x\n",
  300. v1.ieee.exponent, v2.ieee.exponent);
  301. result = 1;
  302. }
  303. if (v1.ieee.negative != v2.ieee.negative)
  304. {
  305. printf ("-0.0625f down: negative differs: %d vs %d\n",
  306. v1.ieee.negative, v2.ieee.negative);
  307. result = 1;
  308. }
  309. v1.f = f = 0.0f;
  310. f = nextafterf (f, 1.0);
  311. v2.f = nextafterf (f, -1.0);
  312. if (v1.ieee.mantissa != v2.ieee.mantissa)
  313. {
  314. printf ("0.0f up: mantissa differs: %8x vs %8x\n",
  315. v1.ieee.mantissa, v2.ieee.mantissa);
  316. result = 1;
  317. }
  318. if (v1.ieee.exponent != v2.ieee.exponent)
  319. {
  320. printf ("0.0f up: exponent differs: %4x vs %4x\n",
  321. v1.ieee.exponent, v2.ieee.exponent);
  322. result = 1;
  323. }
  324. if (0 != v2.ieee.negative)
  325. {
  326. printf ("0.0f up: negative differs: 0 vs %d\n",
  327. v2.ieee.negative);
  328. result = 1;
  329. }
  330. v1.f = f = 0.0f;
  331. f = nextafterf (f, -1.0);
  332. v2.f = nextafterf (f, 1.0);
  333. if (v1.ieee.mantissa != v2.ieee.mantissa)
  334. {
  335. printf ("0.0f down: mantissa differs: %8x vs %8x\n",
  336. v1.ieee.mantissa, v2.ieee.mantissa);
  337. result = 1;
  338. }
  339. if (v1.ieee.exponent != v2.ieee.exponent)
  340. {
  341. printf ("0.0f down: exponent differs: %4x vs %4x\n",
  342. v1.ieee.exponent, v2.ieee.exponent);
  343. result = 1;
  344. }
  345. if (1 != v2.ieee.negative)
  346. {
  347. printf ("0.0f down: negative differs: 1 vs %d\n",
  348. v2.ieee.negative);
  349. result = 1;
  350. }
  351. if (nextafterf (0.0f, INFINITY) != nextafterf (0.0f, 1.0f)
  352. || nextafterf (-0.0f, INFINITY) != nextafterf (-0.0f, 1.0f)
  353. || nextafterf (0.0f, -INFINITY) != nextafterf (0.0f, -1.0f)
  354. || nextafterf (-0.0f, -INFINITY) != nextafterf (-0.0f, -1.0f))
  355. {
  356. printf ("nextafterf (+-0, +-Inf) != nextafterf (+-0, +-1)\n");
  357. result = 1;
  358. }
  359. if (nexttowardf (0.0f, INFINITY) != nexttowardf (0.0f, 1.0f)
  360. || nexttowardf (-0.0f, INFINITY) != nexttowardf (-0.0f, 1.0f)
  361. || nexttowardf (0.0f, -INFINITY) != nexttowardf (0.0f, -1.0f)
  362. || nexttowardf (-0.0f, -INFINITY) != nexttowardf (-0.0f, -1.0f))
  363. {
  364. printf ("nexttowardf (+-0, +-Inf) != nexttowardf (+-0, +-1)\n");
  365. result = 1;
  366. }
  367. }
  368. {
  369. union ieee754_double v1;
  370. union ieee754_double v2;
  371. double d;
  372. v1.d = d = DBL_MIN;
  373. if (fpclassify (d) != FP_NORMAL)
  374. {
  375. printf ("fpclassify (DBL_MIN) failed: %d\n", fpclassify (d));
  376. result = 1;
  377. }
  378. d = nextafter (d, DBL_MIN / 2.0);
  379. if (fpclassify (d) != FP_SUBNORMAL)
  380. {
  381. printf ("fpclassify (DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
  382. result = 1;
  383. }
  384. v2.d = d = nextafter (d, DBL_MIN);
  385. if (fpclassify (d) != FP_NORMAL)
  386. {
  387. printf ("fpclassify (DBL_MIN-epsilon+epsilon) failed: %d\n",
  388. fpclassify (d));
  389. result = 1;
  390. }
  391. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  392. {
  393. printf ("DBL_MIN: mantissa0 differs: %8x vs %8x\n",
  394. v1.ieee.mantissa0, v2.ieee.mantissa0);
  395. result = 1;
  396. }
  397. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  398. {
  399. printf ("DBL_MIN: mantissa1 differs: %8x vs %8x\n",
  400. v1.ieee.mantissa1, v2.ieee.mantissa1);
  401. result = 1;
  402. }
  403. if (v1.ieee.exponent != v2.ieee.exponent)
  404. {
  405. printf ("DBL_MIN: exponent differs: %4x vs %4x\n",
  406. v1.ieee.exponent, v2.ieee.exponent);
  407. result = 1;
  408. }
  409. if (v1.ieee.negative != v2.ieee.negative)
  410. {
  411. printf ("DBL_MIN: negative differs: %d vs %d\n",
  412. v1.ieee.negative, v2.ieee.negative);
  413. result = 1;
  414. }
  415. v1.d = d = -DBL_MIN;
  416. if (fpclassify (d) != FP_NORMAL)
  417. {
  418. printf ("fpclassify (-DBL_MIN) failed: %d\n", fpclassify (d));
  419. result = 1;
  420. }
  421. d = nextafter (d, -DBL_MIN / 2.0);
  422. if (fpclassify (d) != FP_SUBNORMAL)
  423. {
  424. printf ("fpclassify (-DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
  425. result = 1;
  426. }
  427. v2.d = d = nextafter (d, -DBL_MIN);
  428. if (fpclassify (d) != FP_NORMAL)
  429. {
  430. printf ("fpclassify (-DBL_MIN-epsilon+epsilon) failed: %d\n",
  431. fpclassify (d));
  432. result = 1;
  433. }
  434. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  435. {
  436. printf ("-DBL_MIN: mantissa0 differs: %8x vs %8x\n",
  437. v1.ieee.mantissa0, v2.ieee.mantissa0);
  438. result = 1;
  439. }
  440. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  441. {
  442. printf ("-DBL_MIN: mantissa1 differs: %8x vs %8x\n",
  443. v1.ieee.mantissa1, v2.ieee.mantissa1);
  444. result = 1;
  445. }
  446. if (v1.ieee.exponent != v2.ieee.exponent)
  447. {
  448. printf ("-DBL_MIN: exponent differs: %4x vs %4x\n",
  449. v1.ieee.exponent, v2.ieee.exponent);
  450. result = 1;
  451. }
  452. if (v1.ieee.negative != v2.ieee.negative)
  453. {
  454. printf ("-DBL_MIN: negative differs: %d vs %d\n",
  455. v1.ieee.negative, v2.ieee.negative);
  456. result = 1;
  457. }
  458. d = DBL_MAX;
  459. if (fpclassify (d) != FP_NORMAL)
  460. {
  461. printf ("fpclassify (DBL_MAX) failed: %d\n", fpclassify (d));
  462. result = 1;
  463. }
  464. d = nextafter (d, INFINITY);
  465. if (fpclassify (d) != FP_INFINITE)
  466. {
  467. printf ("fpclassify (DBL_MAX+epsilon) failed: %d\n", fpclassify (d));
  468. result = 1;
  469. }
  470. d = -DBL_MAX;
  471. if (fpclassify (d) != FP_NORMAL)
  472. {
  473. printf ("fpclassify (-DBL_MAX) failed: %d\n", fpclassify (d));
  474. result = 1;
  475. }
  476. d = nextafter (d, -INFINITY);
  477. if (fpclassify (d) != FP_INFINITE)
  478. {
  479. printf ("fpclassify (-DBL_MAX-epsilon) failed: %d\n", fpclassify (d));
  480. result = 1;
  481. }
  482. v1.d = d = 0.0625;
  483. d = nextafter (d, 0.0);
  484. v2.d = d = nextafter (d, 1.0);
  485. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  486. {
  487. printf ("0.0625 down: mantissa0 differs: %8x vs %8x\n",
  488. v1.ieee.mantissa0, v2.ieee.mantissa0);
  489. result = 1;
  490. }
  491. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  492. {
  493. printf ("0.0625 down: mantissa1 differs: %8x vs %8x\n",
  494. v1.ieee.mantissa1, v2.ieee.mantissa1);
  495. result = 1;
  496. }
  497. if (v1.ieee.exponent != v2.ieee.exponent)
  498. {
  499. printf ("0.0625 down: exponent differs: %4x vs %4x\n",
  500. v1.ieee.exponent, v2.ieee.exponent);
  501. result = 1;
  502. }
  503. if (v1.ieee.negative != v2.ieee.negative)
  504. {
  505. printf ("0.0625 down: negative differs: %d vs %d\n",
  506. v1.ieee.negative, v2.ieee.negative);
  507. result = 1;
  508. }
  509. v1.d = d = 0.0625;
  510. d = nextafter (d, 1.0);
  511. v2.d = d = nextafter (d, 0.0);
  512. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  513. {
  514. printf ("0.0625 up: mantissa0 differs: %8x vs %8x\n",
  515. v1.ieee.mantissa0, v2.ieee.mantissa0);
  516. result = 1;
  517. }
  518. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  519. {
  520. printf ("0.0625 up: mantissa1 differs: %8x vs %8x\n",
  521. v1.ieee.mantissa1, v2.ieee.mantissa1);
  522. result = 1;
  523. }
  524. if (v1.ieee.exponent != v2.ieee.exponent)
  525. {
  526. printf ("0.0625 up: exponent differs: %4x vs %4x\n",
  527. v1.ieee.exponent, v2.ieee.exponent);
  528. result = 1;
  529. }
  530. if (v1.ieee.negative != v2.ieee.negative)
  531. {
  532. printf ("0.0625 up: negative differs: %d vs %d\n",
  533. v1.ieee.negative, v2.ieee.negative);
  534. result = 1;
  535. }
  536. v1.d = d = -0.0625;
  537. d = nextafter (d, 0.0);
  538. v2.d = d = nextafter (d, -1.0);
  539. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  540. {
  541. printf ("-0.0625 up: mantissa0 differs: %8x vs %8x\n",
  542. v1.ieee.mantissa0, v2.ieee.mantissa0);
  543. result = 1;
  544. }
  545. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  546. {
  547. printf ("-0.0625 up: mantissa1 differs: %8x vs %8x\n",
  548. v1.ieee.mantissa1, v2.ieee.mantissa1);
  549. result = 1;
  550. }
  551. if (v1.ieee.exponent != v2.ieee.exponent)
  552. {
  553. printf ("-0.0625 up: exponent differs: %4x vs %4x\n",
  554. v1.ieee.exponent, v2.ieee.exponent);
  555. result = 1;
  556. }
  557. if (v1.ieee.negative != v2.ieee.negative)
  558. {
  559. printf ("-0.0625 up: negative differs: %d vs %d\n",
  560. v1.ieee.negative, v2.ieee.negative);
  561. result = 1;
  562. }
  563. v1.d = d = -0.0625;
  564. d = nextafter (d, -1.0);
  565. v2.d = d = nextafter (d, 0.0);
  566. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  567. {
  568. printf ("-0.0625 down: mantissa0 differs: %8x vs %8x\n",
  569. v1.ieee.mantissa0, v2.ieee.mantissa0);
  570. result = 1;
  571. }
  572. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  573. {
  574. printf ("-0.0625 down: mantissa1 differs: %8x vs %8x\n",
  575. v1.ieee.mantissa1, v2.ieee.mantissa1);
  576. result = 1;
  577. }
  578. if (v1.ieee.exponent != v2.ieee.exponent)
  579. {
  580. printf ("-0.0625 down: exponent differs: %4x vs %4x\n",
  581. v1.ieee.exponent, v2.ieee.exponent);
  582. result = 1;
  583. }
  584. if (v1.ieee.negative != v2.ieee.negative)
  585. {
  586. printf ("-0.0625 down: negative differs: %d vs %d\n",
  587. v1.ieee.negative, v2.ieee.negative);
  588. result = 1;
  589. }
  590. v1.d = d = 0.0;
  591. d = nextafter (d, 1.0);
  592. v2.d = nextafter (d, -1.0);
  593. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  594. {
  595. printf ("0.0 up: mantissa0 differs: %8x vs %8x\n",
  596. v1.ieee.mantissa0, v2.ieee.mantissa0);
  597. result = 1;
  598. }
  599. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  600. {
  601. printf ("0.0 up: mantissa1 differs: %8x vs %8x\n",
  602. v1.ieee.mantissa1, v2.ieee.mantissa1);
  603. result = 1;
  604. }
  605. if (v1.ieee.exponent != v2.ieee.exponent)
  606. {
  607. printf ("0.0 up: exponent differs: %4x vs %4x\n",
  608. v1.ieee.exponent, v2.ieee.exponent);
  609. result = 1;
  610. }
  611. if (0 != v2.ieee.negative)
  612. {
  613. printf ("0.0 up: negative differs: 0 vs %d\n",
  614. v2.ieee.negative);
  615. result = 1;
  616. }
  617. v1.d = d = 0.0;
  618. d = nextafter (d, -1.0);
  619. v2.d = nextafter (d, 1.0);
  620. if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
  621. {
  622. printf ("0.0 down: mantissa0 differs: %8x vs %8x\n",
  623. v1.ieee.mantissa0, v2.ieee.mantissa0);
  624. result = 1;
  625. }
  626. if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
  627. {
  628. printf ("0.0 down: mantissa1 differs: %8x vs %8x\n",
  629. v1.ieee.mantissa1, v2.ieee.mantissa1);
  630. result = 1;
  631. }
  632. if (v1.ieee.exponent != v2.ieee.exponent)
  633. {
  634. printf ("0.0 down: exponent differs: %4x vs %4x\n",
  635. v1.ieee.exponent, v2.ieee.exponent);
  636. result = 1;
  637. }
  638. if (1 != v2.ieee.negative)
  639. {
  640. printf ("0.0 down: negative differs: 1 vs %d\n",
  641. v2.ieee.negative);
  642. result = 1;
  643. }
  644. if (nextafter (0.0, INFINITY) != nextafter (0.0, 1.0)
  645. || nextafter (-0.0, INFINITY) != nextafter (-0.0, 1.0)
  646. || nextafter (0.0, -INFINITY) != nextafter (0.0, -1.0)
  647. || nextafter (-0.0, -INFINITY) != nextafter (-0.0, -1.0))
  648. {
  649. printf ("nextafter (+-0, +-Inf) != nextafter (+-0, +-1)\n");
  650. result = 1;
  651. }
  652. if (nexttoward (0.0, INFINITY) != nexttoward (0.0, 1.0)
  653. || nexttoward (-0.0, INFINITY) != nexttoward (-0.0, 1.0)
  654. || nexttoward (0.0, -INFINITY) != nexttoward (0.0, -1.0)
  655. || nexttoward (-0.0, -INFINITY) != nexttoward (-0.0, -1.0))
  656. {
  657. printf ("nexttoward (+-0, +-Inf) != nexttoward (+-0, +-1)\n");
  658. result = 1;
  659. }
  660. }
  661. #ifndef NO_LONG_DOUBLE
  662. {
  663. long double v1, v2;
  664. v1 = LDBL_MIN;
  665. if (fpclassify (v1) != FP_NORMAL)
  666. {
  667. printf ("fpclassify (LDBL_MIN) failed: %d (%La)\n",
  668. fpclassify (v1), v1);
  669. result = 1;
  670. }
  671. v2 = nextafterl (v1, LDBL_MIN / 2.0);
  672. if (fpclassify (v2) != FP_SUBNORMAL)
  673. {
  674. printf ("fpclassify (LDBL_MIN-epsilon) failed: %d (%La)\n",
  675. fpclassify (v2), v2);
  676. result = 1;
  677. }
  678. v2 = nextafterl (v2, LDBL_MIN);
  679. if (fpclassify (v2) != FP_NORMAL)
  680. {
  681. printf ("fpclassify (LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
  682. fpclassify (v2), v2);
  683. result = 1;
  684. }
  685. if (v1 != v2)
  686. {
  687. printf ("LDBL_MIN-epsilon+epsilon != LDBL_MIN: %La vs %La\n", v2, v1);
  688. result = 1;
  689. }
  690. v1 = -LDBL_MIN;
  691. if (fpclassify (v1) != FP_NORMAL)
  692. {
  693. printf ("fpclassify (-LDBL_MIN) failed: %d (%La)\n",
  694. fpclassify (v1), v1);
  695. result = 1;
  696. }
  697. v2 = nextafterl (v1, -LDBL_MIN / 2.0);
  698. if (fpclassify (v2) != FP_SUBNORMAL)
  699. {
  700. printf ("fpclassify (-LDBL_MIN-epsilon) failed: %d (%La)\n",
  701. fpclassify (v2), v2);
  702. result = 1;
  703. }
  704. v2 = nextafterl (v2, -LDBL_MIN);
  705. if (fpclassify (v2) != FP_NORMAL)
  706. {
  707. printf ("fpclassify (-LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
  708. fpclassify (v2), v2);
  709. result = 1;
  710. }
  711. if (v1 != v2)
  712. {
  713. printf ("-LDBL_MIN-epsilon+epsilon != -LDBL_MIN: %La vs %La\n", v2, v1);
  714. result = 1;
  715. }
  716. v1 = LDBL_MAX;
  717. if (fpclassify (v1) != FP_NORMAL)
  718. {
  719. printf ("fpclassify (LDBL_MAX) failed: %d (%La)\n",
  720. fpclassify (v1), v1);
  721. result = 1;
  722. }
  723. v2 = nextafterl (v1, INFINITY);
  724. if (fpclassify (v2) != FP_INFINITE)
  725. {
  726. printf ("fpclassify (LDBL_MAX+epsilon) failed: %d (%La)\n",
  727. fpclassify (v2), v2);
  728. result = 1;
  729. }
  730. v1 = -LDBL_MAX;
  731. if (fpclassify (v1) != FP_NORMAL)
  732. {
  733. printf ("fpclassify (-LDBL_MAX) failed: %d (%La)\n",
  734. fpclassify (v1), v1);
  735. result = 1;
  736. }
  737. v2 = nextafterl (v1, -INFINITY);
  738. if (fpclassify (v2) != FP_INFINITE)
  739. {
  740. printf ("fpclassify (-LDBL_MAX-epsilon) failed: %d (%La)\n",
  741. fpclassify (v2), v2);
  742. result = 1;
  743. }
  744. v1 = 0.0625;
  745. v2 = nextafterl (v1, 0.0);
  746. v2 = nextafterl (v2, 1.0);
  747. if (v1 != v2)
  748. {
  749. printf ("0.0625L-epsilon+epsilon != 0.0625L: %La vs %La\n", v2, v1);
  750. result = 1;
  751. }
  752. v1 = 0.0625;
  753. v2 = nextafterl (v1, 1.0);
  754. v2 = nextafterl (v2, 0.0);
  755. if (v1 != v2)
  756. {
  757. printf ("0.0625L+epsilon-epsilon != 0.0625L: %La vs %La\n", v2, v1);
  758. result = 1;
  759. }
  760. v1 = -0.0625;
  761. v2 = nextafterl (v1, 0.0);
  762. v2 = nextafterl (v2, -1.0);
  763. if (v1 != v2)
  764. {
  765. printf ("-0.0625L+epsilon-epsilon != -0.0625L: %La vs %La\n", v2, v1);
  766. result = 1;
  767. }
  768. v1 = -0.0625;
  769. v2 = nextafterl (v1, -1.0);
  770. v2 = nextafterl (v2, 0.0);
  771. if (v1 != v2)
  772. {
  773. printf ("-0.0625L-epsilon+epsilon != -0.0625L: %La vs %La\n", v2, v1);
  774. result = 1;
  775. }
  776. v1 = 0.0;
  777. v2 = nextafterl (v1, 1.0);
  778. v2 = nextafterl (v2, -1.0);
  779. if (v1 != v2)
  780. {
  781. printf ("0.0+epsilon-epsilon != 0.0L: %La vs %La\n", v2, v1);
  782. result = 1;
  783. }
  784. if (signbit (v2))
  785. {
  786. printf ("0.0+epsilon-epsilon is negative\n");
  787. result = 1;
  788. }
  789. v1 = 0.0;
  790. v2 = nextafterl (v1, -1.0);
  791. v2 = nextafterl (v2, 1.0);
  792. if (v1 != v2)
  793. {
  794. printf ("0.0-epsilon+epsilon != 0.0L: %La vs %La\n", v2, v1);
  795. result = 1;
  796. }
  797. if (!signbit (v2))
  798. {
  799. printf ("0.0-epsilon+epsilon is positive\n");
  800. result = 1;
  801. }
  802. if (nextafterl (0.0, INFINITY) != nextafterl (0.0, 1.0)
  803. || nextafterl (-0.0, INFINITY) != nextafterl (-0.0, 1.0)
  804. || nextafterl (0.0, -INFINITY) != nextafterl (0.0, -1.0)
  805. || nextafterl (-0.0, -INFINITY) != nextafterl (-0.0, -1.0))
  806. {
  807. printf ("nextafterl (+-0, +-Inf) != nextafterl (+-0, +-1)\n");
  808. result = 1;
  809. }
  810. if (nexttowardl (0.0L, INFINITY) != nexttowardl (0.0L, 1.0L)
  811. || nexttowardl (-0.0L, INFINITY) != nexttowardl (-0.0L, 1.0L)
  812. || nexttowardl (0.0L, -INFINITY) != nexttowardl (0.0L, -1.0L)
  813. || nexttowardl (-0.0L, -INFINITY) != nexttowardl (-0.0L, -1.0L))
  814. {
  815. printf ("nexttowardl (+-0, +-Inf) != nexttowardl (+-0, +-1)\n");
  816. result = 1;
  817. }
  818. }
  819. #endif
  820. if (! isnormal (FLT_MIN))
  821. {
  822. puts ("isnormal (FLT_MIN) failed");
  823. result = 1;
  824. }
  825. if (! isnormal (DBL_MIN))
  826. {
  827. puts ("isnormal (DBL_MIN) failed");
  828. result = 1;
  829. }
  830. #ifndef NO_LONG_DOUBLE
  831. if (! isnormal (LDBL_MIN))
  832. {
  833. puts ("isnormal (LDBL_MIN) failed");
  834. result = 1;
  835. }
  836. #endif
  837. #if defined (__i386__) || defined (__x86_64__)
  838. /* This is a test for the strange long doubles in x86 FPUs. */
  839. {
  840. union
  841. {
  842. char b[10];
  843. long double d;
  844. } u =
  845. { .b = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0 } };
  846. if (fpclassify (u.d) != FP_NORMAL)
  847. {
  848. printf ("fpclassify (0x00008000000000000000) failed: %d (%Lg)\n",
  849. fpclassify (u.d), u.d);
  850. result = 1;
  851. }
  852. }
  853. /* Special qNaNs in x86 long double. Test for scalbl. */
  854. {
  855. union
  856. {
  857. char b[10];
  858. long double d;
  859. } u =
  860. { .b = { 0, 1, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f } };
  861. long double r;
  862. r = scalbl (u.d, 0.0);
  863. if (!isnan (r))
  864. {
  865. puts ("scalbl (qNaN, 0) does not return NaN");
  866. result = 1;
  867. }
  868. else if (memcmp (&r, &u.d, sizeof (double)) != 0)
  869. {
  870. puts ("scalbl (qNaN, 0) does not return the same NaN");
  871. result = 1;
  872. }
  873. }
  874. #endif
  875. #ifndef NO_LONG_DOUBLE
  876. {
  877. long double r;
  878. feclearexcept (FE_ALL_EXCEPT);
  879. r = scalbl (LDBL_MIN, 2147483647);
  880. if (! isinf (r))
  881. {
  882. puts ("scalbl (LDBL_MIN, 2147483647) does not return Inf");
  883. result = 1;
  884. }
  885. else if (signbit (r) != 0)
  886. {
  887. puts ("scalbl (LDBL_MIN, 2147483647) returns -Inf");
  888. result = 1;
  889. }
  890. else if (fetestexcept (FE_UNDERFLOW))
  891. {
  892. puts ("scalbl (LDBL_MIN, 2147483647) raises underflow exception");
  893. result = 1;
  894. }
  895. feclearexcept (FE_ALL_EXCEPT);
  896. r = scalbl (LDBL_MAX, -2147483647);
  897. if (r != 0.0)
  898. {
  899. puts ("scalbl (LDBL_MAX, -2147483647) does not return 0");
  900. result = 1;
  901. }
  902. else if (signbit (r) != 0)
  903. {
  904. puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf");
  905. result = 1;
  906. }
  907. else if (fetestexcept (FE_OVERFLOW))
  908. {
  909. puts ("scalbl (LDBL_MAX, -2147483647) raises overflow exception");
  910. result = 1;
  911. }
  912. }
  913. #endif
  914. /* The tests here are very similar to tests earlier in this file,
  915. the important difference is just that there are no intervening
  916. union variables that cause some GCC versions to hide possible
  917. bugs in nextafter* implementation. */
  918. if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN)
  919. {
  920. puts ("nextafterf FLT_MIN test failed");
  921. result = 1;
  922. }
  923. if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN)
  924. != -FLT_MIN)
  925. {
  926. puts ("nextafterf -FLT_MIN test failed");
  927. result = 1;
  928. }
  929. if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN)
  930. {
  931. puts ("nextafter DBL_MIN test failed");
  932. result = 1;
  933. }
  934. if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN)
  935. {
  936. puts ("nextafter -DBL_MIN test failed");
  937. result = 1;
  938. }
  939. #ifndef NO_LONG_DOUBLE
  940. if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN)
  941. != LDBL_MIN)
  942. {
  943. puts ("nextafterl LDBL_MIN test failed");
  944. result = 1;
  945. }
  946. if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN)
  947. != -LDBL_MIN)
  948. {
  949. puts ("nextafterl -LDBL_MIN test failed");
  950. result = 1;
  951. }
  952. #endif
  953. volatile float f1 = FLT_MAX;
  954. volatile float f2 = FLT_MAX / 2;
  955. (void) &f1;
  956. (void) &f2;
  957. feclearexcept (FE_ALL_EXCEPT);
  958. f2 += f1;
  959. #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
  960. int fe = fetestexcept (FE_ALL_EXCEPT);
  961. if (EXCEPTION_TESTS (float) && fe != (FE_OVERFLOW | FE_INEXACT))
  962. {
  963. printf ("float overflow test failed: %x\n", fe);
  964. result = 1;
  965. }
  966. #endif
  967. volatile double d1 = DBL_MAX;
  968. volatile double d2 = DBL_MAX / 2;
  969. (void) &d1;
  970. (void) &d2;
  971. feclearexcept (FE_ALL_EXCEPT);
  972. d2 += d1;
  973. #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
  974. fe = fetestexcept (FE_ALL_EXCEPT);
  975. if (EXCEPTION_TESTS (double) && fe != (FE_OVERFLOW | FE_INEXACT))
  976. {
  977. printf ("double overflow test failed: %x\n", fe);
  978. result = 1;
  979. }
  980. #endif
  981. #ifndef NO_LONG_DOUBLE
  982. volatile long double ld1 = LDBL_MAX;
  983. volatile long double ld2 = LDBL_MAX / 2;
  984. (void) &ld1;
  985. (void) &ld2;
  986. feclearexcept (FE_ALL_EXCEPT);
  987. ld2 += ld1;
  988. # if defined(FE_OVERFLOW) && defined(FE_INEXACT)
  989. fe = fetestexcept (FE_ALL_EXCEPT);
  990. if (EXCEPTION_TESTS (long double) && fe != (FE_OVERFLOW | FE_INEXACT))
  991. {
  992. printf ("long double overflow test failed: %x\n", fe);
  993. result = 1;
  994. }
  995. # endif
  996. #endif
  997. #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG == 113
  998. volatile long double ld3 = 0x1.0000000000010000000100000001p+1;
  999. volatile long double ld4 = 0x1.0000000000000000000000000001p+1;
  1000. (void) &ld3;
  1001. (void) &ld4;
  1002. ld3 -= ld4;
  1003. if (ld3 != 0x1.0p-47)
  1004. {
  1005. printf ("long double subtraction test failed %.28La\n", ld3);
  1006. result = 1;
  1007. }
  1008. #endif
  1009. /* Skip testing IBM long double format, for 2 reasons:
  1010. 1) it only supports FE_TONEAREST
  1011. 2) nextafter (0.0, 1.0) == nextafterl (0.0L, 1.0L), so
  1012. nextafter (0.0, 1.0) / 16.0L will be 0.0L. */
  1013. #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG >= DBL_MANT_DIG + 4 \
  1014. && LDBL_MANT_DIG != 106
  1015. int oldmode = fegetround ();
  1016. int j;
  1017. for (j = 0; j < 4; j++)
  1018. {
  1019. int mode;
  1020. int i;
  1021. int k = 0;
  1022. const char *mstr;
  1023. switch (j)
  1024. {
  1025. #ifdef FE_TONEAREST
  1026. case 0:
  1027. mode = FE_TONEAREST;
  1028. mstr = "nearest";
  1029. k = 8;
  1030. break;
  1031. #endif
  1032. #ifdef FE_DOWNWARD
  1033. case 1:
  1034. mode = FE_DOWNWARD;
  1035. mstr = "-inf";
  1036. break;
  1037. #endif
  1038. #ifdef FE_UPWARD
  1039. case 2:
  1040. mode = FE_UPWARD;
  1041. mstr = "+inf";
  1042. k = 15;
  1043. break;
  1044. #endif
  1045. #ifdef FE_TOWARDZERO
  1046. case 3:
  1047. mode = FE_TOWARDZERO;
  1048. mstr = "0";
  1049. break;
  1050. #endif
  1051. default:
  1052. continue;
  1053. }
  1054. volatile long double ld5 = nextafter (0.0, 1.0) / 16.0L;
  1055. volatile double d5;
  1056. (void) &ld5;
  1057. for (i = 0; i <= 32; i++)
  1058. {
  1059. if (fesetround (mode))
  1060. {
  1061. printf ("failed to set rounding mode to %s\n", mstr);
  1062. if (ROUNDING_TESTS (long double, mode)
  1063. && ROUNDING_TESTS (double, mode))
  1064. result = 1;
  1065. else
  1066. puts ("ignoring this failure");
  1067. break;
  1068. }
  1069. d5 = ld5 * i;
  1070. (void) &d5;
  1071. fesetround (oldmode);
  1072. if (d5 != ((j == 0 && i == 8) ? 0 : (i + k) / 16)
  1073. * nextafter (0.0, 1.0))
  1074. {
  1075. printf ("%La incorrectly rounded to %s as %a\n",
  1076. ld5 * i, mstr, d5);
  1077. if (ROUNDING_TESTS (long double, mode)
  1078. && ROUNDING_TESTS (double, mode))
  1079. result = 1;
  1080. else
  1081. puts ("ignoring this failure");
  1082. }
  1083. }
  1084. }
  1085. volatile long double ld7 = nextafterl (0.0L, 1.0L);
  1086. volatile double d7;
  1087. (void) &ld7;
  1088. fesetround (FE_UPWARD);
  1089. d7 = ld7;
  1090. (void) &d7;
  1091. fesetround (oldmode);
  1092. if (d7 != nextafter (0.0, 1.0))
  1093. {
  1094. printf ("%La incorrectly rounded upward to %a\n", ld7, d7);
  1095. if (ROUNDING_TESTS (long double, FE_UPWARD)
  1096. && ROUNDING_TESTS (double, FE_UPWARD))
  1097. result = 1;
  1098. else
  1099. puts ("ignoring this failure");
  1100. }
  1101. #endif
  1102. return result;
  1103. }
  1104. #define TEST_FUNCTION do_test ()
  1105. #include "../test-skeleton.c"