dfsub.c 16 KB

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