dfsub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  4. *
  5. * Floating-point emulation code
  6. * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <[email protected]>
  7. */
  8. /*
  9. * BEGIN_DESC
  10. *
  11. * File:
  12. * @(#) pa/spmath/dfsub.c $Revision: 1.1 $
  13. *
  14. * Purpose:
  15. * Double_subtract: subtract two double precision values.
  16. *
  17. * External Interfaces:
  18. * dbl_fsub(leftptr, rightptr, dstptr, status)
  19. *
  20. * Internal Interfaces:
  21. *
  22. * Theory:
  23. * <<please update with a overview of the operation of this file>>
  24. *
  25. * END_DESC
  26. */
  27. #include "float.h"
  28. #include "dbl_float.h"
  29. /*
  30. * Double_subtract: subtract two double precision values.
  31. */
  32. int
  33. dbl_fsub(
  34. dbl_floating_point *leftptr,
  35. dbl_floating_point *rightptr,
  36. dbl_floating_point *dstptr,
  37. unsigned int *status)
  38. {
  39. register unsigned int signless_upper_left, signless_upper_right, save;
  40. register unsigned int leftp1, leftp2, rightp1, rightp2, extent;
  41. register unsigned int resultp1 = 0, resultp2 = 0;
  42. register int result_exponent, right_exponent, diff_exponent;
  43. register int sign_save, jumpsize;
  44. register boolean inexact = FALSE, underflowtrap;
  45. /* Create local copies of the numbers */
  46. Dbl_copyfromptr(leftptr,leftp1,leftp2);
  47. Dbl_copyfromptr(rightptr,rightp1,rightp2);
  48. /* A zero "save" helps discover equal operands (for later), *
  49. * and is used in swapping operands (if needed). */
  50. Dbl_xortointp1(leftp1,rightp1,/*to*/save);
  51. /*
  52. * check first operand for NaN's or infinity
  53. */
  54. if ((result_exponent = Dbl_exponent(leftp1)) == DBL_INFINITY_EXPONENT)
  55. {
  56. if (Dbl_iszero_mantissa(leftp1,leftp2))
  57. {
  58. if (Dbl_isnotnan(rightp1,rightp2))
  59. {
  60. if (Dbl_isinfinity(rightp1,rightp2) && save==0)
  61. {
  62. /*
  63. * invalid since operands are same signed infinity's
  64. */
  65. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  66. Set_invalidflag();
  67. Dbl_makequietnan(resultp1,resultp2);
  68. Dbl_copytoptr(resultp1,resultp2,dstptr);
  69. return(NOEXCEPTION);
  70. }
  71. /*
  72. * return infinity
  73. */
  74. Dbl_copytoptr(leftp1,leftp2,dstptr);
  75. return(NOEXCEPTION);
  76. }
  77. }
  78. else
  79. {
  80. /*
  81. * is NaN; signaling or quiet?
  82. */
  83. if (Dbl_isone_signaling(leftp1))
  84. {
  85. /* trap if INVALIDTRAP enabled */
  86. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  87. /* make NaN quiet */
  88. Set_invalidflag();
  89. Dbl_set_quiet(leftp1);
  90. }
  91. /*
  92. * is second operand a signaling NaN?
  93. */
  94. else if (Dbl_is_signalingnan(rightp1))
  95. {
  96. /* trap if INVALIDTRAP enabled */
  97. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  98. /* make NaN quiet */
  99. Set_invalidflag();
  100. Dbl_set_quiet(rightp1);
  101. Dbl_copytoptr(rightp1,rightp2,dstptr);
  102. return(NOEXCEPTION);
  103. }
  104. /*
  105. * return quiet NaN
  106. */
  107. Dbl_copytoptr(leftp1,leftp2,dstptr);
  108. return(NOEXCEPTION);
  109. }
  110. } /* End left NaN or Infinity processing */
  111. /*
  112. * check second operand for NaN's or infinity
  113. */
  114. if (Dbl_isinfinity_exponent(rightp1))
  115. {
  116. if (Dbl_iszero_mantissa(rightp1,rightp2))
  117. {
  118. /* return infinity */
  119. Dbl_invert_sign(rightp1);
  120. Dbl_copytoptr(rightp1,rightp2,dstptr);
  121. return(NOEXCEPTION);
  122. }
  123. /*
  124. * is NaN; signaling or quiet?
  125. */
  126. if (Dbl_isone_signaling(rightp1))
  127. {
  128. /* trap if INVALIDTRAP enabled */
  129. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  130. /* make NaN quiet */
  131. Set_invalidflag();
  132. Dbl_set_quiet(rightp1);
  133. }
  134. /*
  135. * return quiet NaN
  136. */
  137. Dbl_copytoptr(rightp1,rightp2,dstptr);
  138. return(NOEXCEPTION);
  139. } /* End right NaN or Infinity processing */
  140. /* Invariant: Must be dealing with finite numbers */
  141. /* Compare operands by removing the sign */
  142. Dbl_copytoint_exponentmantissap1(leftp1,signless_upper_left);
  143. Dbl_copytoint_exponentmantissap1(rightp1,signless_upper_right);
  144. /* sign difference selects add or sub operation. */
  145. if(Dbl_ismagnitudeless(leftp2,rightp2,signless_upper_left,signless_upper_right))
  146. {
  147. /* Set the left operand to the larger one by XOR swap *
  148. * First finish the first word using "save" */
  149. Dbl_xorfromintp1(save,rightp1,/*to*/rightp1);
  150. Dbl_xorfromintp1(save,leftp1,/*to*/leftp1);
  151. Dbl_swap_lower(leftp2,rightp2);
  152. result_exponent = Dbl_exponent(leftp1);
  153. Dbl_invert_sign(leftp1);
  154. }
  155. /* Invariant: left is not smaller than right. */
  156. if((right_exponent = Dbl_exponent(rightp1)) == 0)
  157. {
  158. /* Denormalized operands. First look for zeroes */
  159. if(Dbl_iszero_mantissa(rightp1,rightp2))
  160. {
  161. /* right is zero */
  162. if(Dbl_iszero_exponentmantissa(leftp1,leftp2))
  163. {
  164. /* Both operands are zeros */
  165. Dbl_invert_sign(rightp1);
  166. if(Is_rounding_mode(ROUNDMINUS))
  167. {
  168. Dbl_or_signs(leftp1,/*with*/rightp1);
  169. }
  170. else
  171. {
  172. Dbl_and_signs(leftp1,/*with*/rightp1);
  173. }
  174. }
  175. else
  176. {
  177. /* Left is not a zero and must be the result. Trapped
  178. * underflows are signaled if left is denormalized. Result
  179. * is always exact. */
  180. if( (result_exponent == 0) && Is_underflowtrap_enabled() )
  181. {
  182. /* need to normalize results mantissa */
  183. sign_save = Dbl_signextendedsign(leftp1);
  184. Dbl_leftshiftby1(leftp1,leftp2);
  185. Dbl_normalize(leftp1,leftp2,result_exponent);
  186. Dbl_set_sign(leftp1,/*using*/sign_save);
  187. Dbl_setwrapped_exponent(leftp1,result_exponent,unfl);
  188. Dbl_copytoptr(leftp1,leftp2,dstptr);
  189. /* inexact = FALSE */
  190. return(UNDERFLOWEXCEPTION);
  191. }
  192. }
  193. Dbl_copytoptr(leftp1,leftp2,dstptr);
  194. return(NOEXCEPTION);
  195. }
  196. /* Neither are zeroes */
  197. Dbl_clear_sign(rightp1); /* Exponent is already cleared */
  198. if(result_exponent == 0 )
  199. {
  200. /* Both operands are denormalized. The result must be exact
  201. * and is simply calculated. A sum could become normalized and a
  202. * difference could cancel to a true zero. */
  203. if( (/*signed*/int) save >= 0 )
  204. {
  205. Dbl_subtract(leftp1,leftp2,/*minus*/rightp1,rightp2,
  206. /*into*/resultp1,resultp2);
  207. if(Dbl_iszero_mantissa(resultp1,resultp2))
  208. {
  209. if(Is_rounding_mode(ROUNDMINUS))
  210. {
  211. Dbl_setone_sign(resultp1);
  212. }
  213. else
  214. {
  215. Dbl_setzero_sign(resultp1);
  216. }
  217. Dbl_copytoptr(resultp1,resultp2,dstptr);
  218. return(NOEXCEPTION);
  219. }
  220. }
  221. else
  222. {
  223. Dbl_addition(leftp1,leftp2,rightp1,rightp2,
  224. /*into*/resultp1,resultp2);
  225. if(Dbl_isone_hidden(resultp1))
  226. {
  227. Dbl_copytoptr(resultp1,resultp2,dstptr);
  228. return(NOEXCEPTION);
  229. }
  230. }
  231. if(Is_underflowtrap_enabled())
  232. {
  233. /* need to normalize result */
  234. sign_save = Dbl_signextendedsign(resultp1);
  235. Dbl_leftshiftby1(resultp1,resultp2);
  236. Dbl_normalize(resultp1,resultp2,result_exponent);
  237. Dbl_set_sign(resultp1,/*using*/sign_save);
  238. Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
  239. Dbl_copytoptr(resultp1,resultp2,dstptr);
  240. /* inexact = FALSE */
  241. return(UNDERFLOWEXCEPTION);
  242. }
  243. Dbl_copytoptr(resultp1,resultp2,dstptr);
  244. return(NOEXCEPTION);
  245. }
  246. right_exponent = 1; /* Set exponent to reflect different bias
  247. * with denormalized numbers. */
  248. }
  249. else
  250. {
  251. Dbl_clear_signexponent_set_hidden(rightp1);
  252. }
  253. Dbl_clear_exponent_set_hidden(leftp1);
  254. diff_exponent = result_exponent - right_exponent;
  255. /*
  256. * Special case alignment of operands that would force alignment
  257. * beyond the extent of the extension. A further optimization
  258. * could special case this but only reduces the path length for this
  259. * infrequent case.
  260. */
  261. if(diff_exponent > DBL_THRESHOLD)
  262. {
  263. diff_exponent = DBL_THRESHOLD;
  264. }
  265. /* Align right operand by shifting to right */
  266. Dbl_right_align(/*operand*/rightp1,rightp2,/*shifted by*/diff_exponent,
  267. /*and lower to*/extent);
  268. /* Treat sum and difference of the operands separately. */
  269. if( (/*signed*/int) save >= 0 )
  270. {
  271. /*
  272. * Difference of the two operands. Their can be no overflow. A
  273. * borrow can occur out of the hidden bit and force a post
  274. * normalization phase.
  275. */
  276. Dbl_subtract_withextension(leftp1,leftp2,/*minus*/rightp1,rightp2,
  277. /*with*/extent,/*into*/resultp1,resultp2);
  278. if(Dbl_iszero_hidden(resultp1))
  279. {
  280. /* Handle normalization */
  281. /* A straight forward algorithm would now shift the result
  282. * and extension left until the hidden bit becomes one. Not
  283. * all of the extension bits need participate in the shift.
  284. * Only the two most significant bits (round and guard) are
  285. * needed. If only a single shift is needed then the guard
  286. * bit becomes a significant low order bit and the extension
  287. * must participate in the rounding. If more than a single
  288. * shift is needed, then all bits to the right of the guard
  289. * bit are zeros, and the guard bit may or may not be zero. */
  290. sign_save = Dbl_signextendedsign(resultp1);
  291. Dbl_leftshiftby1_withextent(resultp1,resultp2,extent,resultp1,resultp2);
  292. /* Need to check for a zero result. The sign and exponent
  293. * fields have already been zeroed. The more efficient test
  294. * of the full object can be used.
  295. */
  296. if(Dbl_iszero(resultp1,resultp2))
  297. /* Must have been "x-x" or "x+(-x)". */
  298. {
  299. if(Is_rounding_mode(ROUNDMINUS)) Dbl_setone_sign(resultp1);
  300. Dbl_copytoptr(resultp1,resultp2,dstptr);
  301. return(NOEXCEPTION);
  302. }
  303. result_exponent--;
  304. /* Look to see if normalization is finished. */
  305. if(Dbl_isone_hidden(resultp1))
  306. {
  307. if(result_exponent==0)
  308. {
  309. /* Denormalized, exponent should be zero. Left operand *
  310. * was normalized, so extent (guard, round) was zero */
  311. goto underflow;
  312. }
  313. else
  314. {
  315. /* No further normalization is needed. */
  316. Dbl_set_sign(resultp1,/*using*/sign_save);
  317. Ext_leftshiftby1(extent);
  318. goto round;
  319. }
  320. }
  321. /* Check for denormalized, exponent should be zero. Left *
  322. * operand was normalized, so extent (guard, round) was zero */
  323. if(!(underflowtrap = Is_underflowtrap_enabled()) &&
  324. result_exponent==0) goto underflow;
  325. /* Shift extension to complete one bit of normalization and
  326. * update exponent. */
  327. Ext_leftshiftby1(extent);
  328. /* Discover first one bit to determine shift amount. Use a
  329. * modified binary search. We have already shifted the result
  330. * one position right and still not found a one so the remainder
  331. * of the extension must be zero and simplifies rounding. */
  332. /* Scan bytes */
  333. while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
  334. {
  335. Dbl_leftshiftby8(resultp1,resultp2);
  336. if((result_exponent -= 8) <= 0 && !underflowtrap)
  337. goto underflow;
  338. }
  339. /* Now narrow it down to the nibble */
  340. if(Dbl_iszero_hiddenhigh3mantissa(resultp1))
  341. {
  342. /* The lower nibble contains the normalizing one */
  343. Dbl_leftshiftby4(resultp1,resultp2);
  344. if((result_exponent -= 4) <= 0 && !underflowtrap)
  345. goto underflow;
  346. }
  347. /* Select case were first bit is set (already normalized)
  348. * otherwise select the proper shift. */
  349. if((jumpsize = Dbl_hiddenhigh3mantissa(resultp1)) > 7)
  350. {
  351. /* Already normalized */
  352. if(result_exponent <= 0) goto underflow;
  353. Dbl_set_sign(resultp1,/*using*/sign_save);
  354. Dbl_set_exponent(resultp1,/*using*/result_exponent);
  355. Dbl_copytoptr(resultp1,resultp2,dstptr);
  356. return(NOEXCEPTION);
  357. }
  358. Dbl_sethigh4bits(resultp1,/*using*/sign_save);
  359. switch(jumpsize)
  360. {
  361. case 1:
  362. {
  363. Dbl_leftshiftby3(resultp1,resultp2);
  364. result_exponent -= 3;
  365. break;
  366. }
  367. case 2:
  368. case 3:
  369. {
  370. Dbl_leftshiftby2(resultp1,resultp2);
  371. result_exponent -= 2;
  372. break;
  373. }
  374. case 4:
  375. case 5:
  376. case 6:
  377. case 7:
  378. {
  379. Dbl_leftshiftby1(resultp1,resultp2);
  380. result_exponent -= 1;
  381. break;
  382. }
  383. }
  384. if(result_exponent > 0)
  385. {
  386. Dbl_set_exponent(resultp1,/*using*/result_exponent);
  387. Dbl_copytoptr(resultp1,resultp2,dstptr);
  388. return(NOEXCEPTION); /* Sign bit is already set */
  389. }
  390. /* Fixup potential underflows */
  391. underflow:
  392. if(Is_underflowtrap_enabled())
  393. {
  394. Dbl_set_sign(resultp1,sign_save);
  395. Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
  396. Dbl_copytoptr(resultp1,resultp2,dstptr);
  397. /* inexact = FALSE */
  398. return(UNDERFLOWEXCEPTION);
  399. }
  400. /*
  401. * Since we cannot get an inexact denormalized result,
  402. * we can now return.
  403. */
  404. Dbl_fix_overshift(resultp1,resultp2,(1-result_exponent),extent);
  405. Dbl_clear_signexponent(resultp1);
  406. Dbl_set_sign(resultp1,sign_save);
  407. Dbl_copytoptr(resultp1,resultp2,dstptr);
  408. return(NOEXCEPTION);
  409. } /* end if(hidden...)... */
  410. /* Fall through and round */
  411. } /* end if(save >= 0)... */
  412. else
  413. {
  414. /* Subtract magnitudes */
  415. Dbl_addition(leftp1,leftp2,rightp1,rightp2,/*to*/resultp1,resultp2);
  416. if(Dbl_isone_hiddenoverflow(resultp1))
  417. {
  418. /* Prenormalization required. */
  419. Dbl_rightshiftby1_withextent(resultp2,extent,extent);
  420. Dbl_arithrightshiftby1(resultp1,resultp2);
  421. result_exponent++;
  422. } /* end if hiddenoverflow... */
  423. } /* end else ...subtract magnitudes... */
  424. /* Round the result. If the extension is all zeros,then the result is
  425. * exact. Otherwise round in the correct direction. No underflow is
  426. * possible. If a postnormalization is necessary, then the mantissa is
  427. * all zeros so no shift is needed. */
  428. round:
  429. if(Ext_isnotzero(extent))
  430. {
  431. inexact = TRUE;
  432. switch(Rounding_mode())
  433. {
  434. case ROUNDNEAREST: /* The default. */
  435. if(Ext_isone_sign(extent))
  436. {
  437. /* at least 1/2 ulp */
  438. if(Ext_isnotzero_lower(extent) ||
  439. Dbl_isone_lowmantissap2(resultp2))
  440. {
  441. /* either exactly half way and odd or more than 1/2ulp */
  442. Dbl_increment(resultp1,resultp2);
  443. }
  444. }
  445. break;
  446. case ROUNDPLUS:
  447. if(Dbl_iszero_sign(resultp1))
  448. {
  449. /* Round up positive results */
  450. Dbl_increment(resultp1,resultp2);
  451. }
  452. break;
  453. case ROUNDMINUS:
  454. if(Dbl_isone_sign(resultp1))
  455. {
  456. /* Round down negative results */
  457. Dbl_increment(resultp1,resultp2);
  458. }
  459. case ROUNDZERO:;
  460. /* truncate is simple */
  461. } /* end switch... */
  462. if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
  463. }
  464. if(result_exponent == DBL_INFINITY_EXPONENT)
  465. {
  466. /* Overflow */
  467. if(Is_overflowtrap_enabled())
  468. {
  469. Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
  470. Dbl_copytoptr(resultp1,resultp2,dstptr);
  471. if (inexact)
  472. if (Is_inexacttrap_enabled())
  473. return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
  474. else Set_inexactflag();
  475. return(OVERFLOWEXCEPTION);
  476. }
  477. else
  478. {
  479. inexact = TRUE;
  480. Set_overflowflag();
  481. Dbl_setoverflow(resultp1,resultp2);
  482. }
  483. }
  484. else Dbl_set_exponent(resultp1,result_exponent);
  485. Dbl_copytoptr(resultp1,resultp2,dstptr);
  486. if(inexact)
  487. if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  488. else Set_inexactflag();
  489. return(NOEXCEPTION);
  490. }