test-misc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /* Miscellaneous tests which don't fit anywhere else.
  2. Copyright (C) 2000-2019 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. #if LDBL_MANT_DIG > DBL_MANT_DIG
  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 "Unsupported LDBL_MANT_DIG, 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. #if LDBL_MANT_DIG > DBL_MANT_DIG
  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. #if LDBL_MANT_DIG > DBL_MANT_DIG
  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. #if LDBL_MANT_DIG > DBL_MANT_DIG
  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. # ifdef FE_UNDERFLOW
  891. else if (fetestexcept (FE_UNDERFLOW))
  892. {
  893. puts ("scalbl (LDBL_MIN, 2147483647) raises underflow exception");
  894. result = 1;
  895. }
  896. # endif
  897. feclearexcept (FE_ALL_EXCEPT);
  898. r = scalbl (LDBL_MAX, -2147483647);
  899. if (r != 0.0)
  900. {
  901. puts ("scalbl (LDBL_MAX, -2147483647) does not return 0");
  902. result = 1;
  903. }
  904. else if (signbit (r) != 0)
  905. {
  906. puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf");
  907. result = 1;
  908. }
  909. # ifdef FE_OVERFLOW
  910. else if (fetestexcept (FE_OVERFLOW))
  911. {
  912. puts ("scalbl (LDBL_MAX, -2147483647) raises overflow exception");
  913. result = 1;
  914. }
  915. # endif
  916. }
  917. #endif
  918. /* The tests here are very similar to tests earlier in this file,
  919. the important difference is just that there are no intervening
  920. union variables that cause some GCC versions to hide possible
  921. bugs in nextafter* implementation. */
  922. if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN)
  923. {
  924. puts ("nextafterf FLT_MIN test failed");
  925. result = 1;
  926. }
  927. if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN)
  928. != -FLT_MIN)
  929. {
  930. puts ("nextafterf -FLT_MIN test failed");
  931. result = 1;
  932. }
  933. if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN)
  934. {
  935. puts ("nextafter DBL_MIN test failed");
  936. result = 1;
  937. }
  938. if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN)
  939. {
  940. puts ("nextafter -DBL_MIN test failed");
  941. result = 1;
  942. }
  943. #if LDBL_MANT_DIG > DBL_MANT_DIG
  944. if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN)
  945. != LDBL_MIN)
  946. {
  947. puts ("nextafterl LDBL_MIN test failed");
  948. result = 1;
  949. }
  950. if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN)
  951. != -LDBL_MIN)
  952. {
  953. puts ("nextafterl -LDBL_MIN test failed");
  954. result = 1;
  955. }
  956. #endif
  957. volatile float f1 = FLT_MAX;
  958. volatile float f2 = FLT_MAX / 2;
  959. (void) &f1;
  960. (void) &f2;
  961. feclearexcept (FE_ALL_EXCEPT);
  962. f2 += f1;
  963. #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
  964. int fe = fetestexcept (FE_ALL_EXCEPT);
  965. if (EXCEPTION_TESTS (float) && fe != (FE_OVERFLOW | FE_INEXACT))
  966. {
  967. printf ("float overflow test failed: %x\n", fe);
  968. result = 1;
  969. }
  970. #endif
  971. volatile double d1 = DBL_MAX;
  972. volatile double d2 = DBL_MAX / 2;
  973. (void) &d1;
  974. (void) &d2;
  975. feclearexcept (FE_ALL_EXCEPT);
  976. d2 += d1;
  977. #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
  978. fe = fetestexcept (FE_ALL_EXCEPT);
  979. if (EXCEPTION_TESTS (double) && fe != (FE_OVERFLOW | FE_INEXACT))
  980. {
  981. printf ("double overflow test failed: %x\n", fe);
  982. result = 1;
  983. }
  984. #endif
  985. #if LDBL_MANT_DIG > DBL_MANT_DIG
  986. volatile long double ld1 = LDBL_MAX;
  987. volatile long double ld2 = LDBL_MAX / 2;
  988. (void) &ld1;
  989. (void) &ld2;
  990. feclearexcept (FE_ALL_EXCEPT);
  991. ld2 += ld1;
  992. # if defined(FE_OVERFLOW) && defined(FE_INEXACT)
  993. fe = fetestexcept (FE_ALL_EXCEPT);
  994. if (EXCEPTION_TESTS (long double) && fe != (FE_OVERFLOW | FE_INEXACT))
  995. {
  996. printf ("long double overflow test failed: %x\n", fe);
  997. result = 1;
  998. }
  999. # endif
  1000. # if LDBL_MANT_DIG == 113
  1001. volatile long double ld3 = 0x1.0000000000010000000100000001p+1;
  1002. volatile long double ld4 = 0x1.0000000000000000000000000001p+1;
  1003. (void) &ld3;
  1004. (void) &ld4;
  1005. ld3 -= ld4;
  1006. if (ld3 != 0x1.0p-47)
  1007. {
  1008. printf ("long double subtraction test failed %.28La\n", ld3);
  1009. result = 1;
  1010. }
  1011. # endif
  1012. /* Skip testing IBM long double format, for 2 reasons:
  1013. 1) it only supports FE_TONEAREST
  1014. 2) nextafter (0.0, 1.0) == nextafterl (0.0L, 1.0L), so
  1015. nextafter (0.0, 1.0) / 16.0L will be 0.0L. */
  1016. # if LDBL_MANT_DIG >= DBL_MANT_DIG + 4 && LDBL_MANT_DIG != 106
  1017. int oldmode = fegetround ();
  1018. int j;
  1019. for (j = 0; j < 4; j++)
  1020. {
  1021. int mode;
  1022. int i;
  1023. int k = 0;
  1024. const char *mstr;
  1025. switch (j)
  1026. {
  1027. #ifdef FE_TONEAREST
  1028. case 0:
  1029. mode = FE_TONEAREST;
  1030. mstr = "nearest";
  1031. k = 8;
  1032. break;
  1033. #endif
  1034. #ifdef FE_DOWNWARD
  1035. case 1:
  1036. mode = FE_DOWNWARD;
  1037. mstr = "-inf";
  1038. break;
  1039. #endif
  1040. #ifdef FE_UPWARD
  1041. case 2:
  1042. mode = FE_UPWARD;
  1043. mstr = "+inf";
  1044. k = 15;
  1045. break;
  1046. #endif
  1047. #ifdef FE_TOWARDZERO
  1048. case 3:
  1049. mode = FE_TOWARDZERO;
  1050. mstr = "0";
  1051. break;
  1052. #endif
  1053. default:
  1054. continue;
  1055. }
  1056. volatile long double ld5 = nextafter (0.0, 1.0) / 16.0L;
  1057. volatile double d5;
  1058. (void) &ld5;
  1059. for (i = 0; i <= 32; i++)
  1060. {
  1061. if (fesetround (mode))
  1062. {
  1063. printf ("failed to set rounding mode to %s\n", mstr);
  1064. if (ROUNDING_TESTS (long double, mode)
  1065. && ROUNDING_TESTS (double, mode))
  1066. result = 1;
  1067. else
  1068. puts ("ignoring this failure");
  1069. break;
  1070. }
  1071. d5 = ld5 * i;
  1072. (void) &d5;
  1073. fesetround (oldmode);
  1074. if (d5 != ((j == 0 && i == 8) ? 0 : (i + k) / 16)
  1075. * nextafter (0.0, 1.0))
  1076. {
  1077. printf ("%La incorrectly rounded to %s as %a\n",
  1078. ld5 * i, mstr, d5);
  1079. if (ROUNDING_TESTS (long double, mode)
  1080. && ROUNDING_TESTS (double, mode))
  1081. result = 1;
  1082. else
  1083. puts ("ignoring this failure");
  1084. }
  1085. }
  1086. }
  1087. # ifdef FE_UPWARD
  1088. volatile long double ld7 = nextafterl (0.0L, 1.0L);
  1089. volatile double d7;
  1090. (void) &ld7;
  1091. fesetround (FE_UPWARD);
  1092. d7 = ld7;
  1093. (void) &d7;
  1094. fesetround (oldmode);
  1095. if (d7 != nextafter (0.0, 1.0))
  1096. {
  1097. printf ("%La incorrectly rounded upward to %a\n", ld7, d7);
  1098. if (ROUNDING_TESTS (long double, FE_UPWARD)
  1099. && ROUNDING_TESTS (double, FE_UPWARD))
  1100. result = 1;
  1101. else
  1102. puts ("ignoring this failure");
  1103. }
  1104. # endif
  1105. # endif
  1106. #endif
  1107. return result;
  1108. }
  1109. #define TEST_FUNCTION do_test ()
  1110. #include "../test-skeleton.c"