sfsub.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/sfsub.c $Revision: 1.1 $
  26. *
  27. * Purpose:
  28. * Single_subtract: subtract two single precision values.
  29. *
  30. * External Interfaces:
  31. * sgl_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 "sgl_float.h"
  42. /*
  43. * Single_subtract: subtract two single precision values.
  44. */
  45. int
  46. sgl_fsub(
  47. sgl_floating_point *leftptr,
  48. sgl_floating_point *rightptr,
  49. sgl_floating_point *dstptr,
  50. unsigned int *status)
  51. {
  52. register unsigned int left, right, result, extent;
  53. register unsigned int signless_upper_left, signless_upper_right, save;
  54. register int result_exponent, right_exponent, diff_exponent;
  55. register int sign_save, jumpsize;
  56. register boolean inexact = FALSE, underflowtrap;
  57. /* Create local copies of the numbers */
  58. left = *leftptr;
  59. right = *rightptr;
  60. /* A zero "save" helps discover equal operands (for later), *
  61. * and is used in swapping operands (if needed). */
  62. Sgl_xortointp1(left,right,/*to*/save);
  63. /*
  64. * check first operand for NaN's or infinity
  65. */
  66. if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT)
  67. {
  68. if (Sgl_iszero_mantissa(left))
  69. {
  70. if (Sgl_isnotnan(right))
  71. {
  72. if (Sgl_isinfinity(right) && save==0)
  73. {
  74. /*
  75. * invalid since operands are same signed infinity's
  76. */
  77. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  78. Set_invalidflag();
  79. Sgl_makequietnan(result);
  80. *dstptr = result;
  81. return(NOEXCEPTION);
  82. }
  83. /*
  84. * return infinity
  85. */
  86. *dstptr = left;
  87. return(NOEXCEPTION);
  88. }
  89. }
  90. else
  91. {
  92. /*
  93. * is NaN; signaling or quiet?
  94. */
  95. if (Sgl_isone_signaling(left))
  96. {
  97. /* trap if INVALIDTRAP enabled */
  98. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  99. /* make NaN quiet */
  100. Set_invalidflag();
  101. Sgl_set_quiet(left);
  102. }
  103. /*
  104. * is second operand a signaling NaN?
  105. */
  106. else if (Sgl_is_signalingnan(right))
  107. {
  108. /* trap if INVALIDTRAP enabled */
  109. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  110. /* make NaN quiet */
  111. Set_invalidflag();
  112. Sgl_set_quiet(right);
  113. *dstptr = right;
  114. return(NOEXCEPTION);
  115. }
  116. /*
  117. * return quiet NaN
  118. */
  119. *dstptr = left;
  120. return(NOEXCEPTION);
  121. }
  122. } /* End left NaN or Infinity processing */
  123. /*
  124. * check second operand for NaN's or infinity
  125. */
  126. if (Sgl_isinfinity_exponent(right))
  127. {
  128. if (Sgl_iszero_mantissa(right))
  129. {
  130. /* return infinity */
  131. Sgl_invert_sign(right);
  132. *dstptr = right;
  133. return(NOEXCEPTION);
  134. }
  135. /*
  136. * is NaN; signaling or quiet?
  137. */
  138. if (Sgl_isone_signaling(right))
  139. {
  140. /* trap if INVALIDTRAP enabled */
  141. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  142. /* make NaN quiet */
  143. Set_invalidflag();
  144. Sgl_set_quiet(right);
  145. }
  146. /*
  147. * return quiet NaN
  148. */
  149. *dstptr = right;
  150. return(NOEXCEPTION);
  151. } /* End right NaN or Infinity processing */
  152. /* Invariant: Must be dealing with finite numbers */
  153. /* Compare operands by removing the sign */
  154. Sgl_copytoint_exponentmantissa(left,signless_upper_left);
  155. Sgl_copytoint_exponentmantissa(right,signless_upper_right);
  156. /* sign difference selects sub or add operation. */
  157. if(Sgl_ismagnitudeless(signless_upper_left,signless_upper_right))
  158. {
  159. /* Set the left operand to the larger one by XOR swap *
  160. * First finish the first word using "save" */
  161. Sgl_xorfromintp1(save,right,/*to*/right);
  162. Sgl_xorfromintp1(save,left,/*to*/left);
  163. result_exponent = Sgl_exponent(left);
  164. Sgl_invert_sign(left);
  165. }
  166. /* Invariant: left is not smaller than right. */
  167. if((right_exponent = Sgl_exponent(right)) == 0)
  168. {
  169. /* Denormalized operands. First look for zeroes */
  170. if(Sgl_iszero_mantissa(right))
  171. {
  172. /* right is zero */
  173. if(Sgl_iszero_exponentmantissa(left))
  174. {
  175. /* Both operands are zeros */
  176. Sgl_invert_sign(right);
  177. if(Is_rounding_mode(ROUNDMINUS))
  178. {
  179. Sgl_or_signs(left,/*with*/right);
  180. }
  181. else
  182. {
  183. Sgl_and_signs(left,/*with*/right);
  184. }
  185. }
  186. else
  187. {
  188. /* Left is not a zero and must be the result. Trapped
  189. * underflows are signaled if left is denormalized. Result
  190. * is always exact. */
  191. if( (result_exponent == 0) && Is_underflowtrap_enabled() )
  192. {
  193. /* need to normalize results mantissa */
  194. sign_save = Sgl_signextendedsign(left);
  195. Sgl_leftshiftby1(left);
  196. Sgl_normalize(left,result_exponent);
  197. Sgl_set_sign(left,/*using*/sign_save);
  198. Sgl_setwrapped_exponent(left,result_exponent,unfl);
  199. *dstptr = left;
  200. /* inexact = FALSE */
  201. return(UNDERFLOWEXCEPTION);
  202. }
  203. }
  204. *dstptr = left;
  205. return(NOEXCEPTION);
  206. }
  207. /* Neither are zeroes */
  208. Sgl_clear_sign(right); /* Exponent is already cleared */
  209. if(result_exponent == 0 )
  210. {
  211. /* Both operands are denormalized. The result must be exact
  212. * and is simply calculated. A sum could become normalized and a
  213. * difference could cancel to a true zero. */
  214. if( (/*signed*/int) save >= 0 )
  215. {
  216. Sgl_subtract(left,/*minus*/right,/*into*/result);
  217. if(Sgl_iszero_mantissa(result))
  218. {
  219. if(Is_rounding_mode(ROUNDMINUS))
  220. {
  221. Sgl_setone_sign(result);
  222. }
  223. else
  224. {
  225. Sgl_setzero_sign(result);
  226. }
  227. *dstptr = result;
  228. return(NOEXCEPTION);
  229. }
  230. }
  231. else
  232. {
  233. Sgl_addition(left,right,/*into*/result);
  234. if(Sgl_isone_hidden(result))
  235. {
  236. *dstptr = result;
  237. return(NOEXCEPTION);
  238. }
  239. }
  240. if(Is_underflowtrap_enabled())
  241. {
  242. /* need to normalize result */
  243. sign_save = Sgl_signextendedsign(result);
  244. Sgl_leftshiftby1(result);
  245. Sgl_normalize(result,result_exponent);
  246. Sgl_set_sign(result,/*using*/sign_save);
  247. Sgl_setwrapped_exponent(result,result_exponent,unfl);
  248. *dstptr = result;
  249. /* inexact = FALSE */
  250. return(UNDERFLOWEXCEPTION);
  251. }
  252. *dstptr = result;
  253. return(NOEXCEPTION);
  254. }
  255. right_exponent = 1; /* Set exponent to reflect different bias
  256. * with denomalized numbers. */
  257. }
  258. else
  259. {
  260. Sgl_clear_signexponent_set_hidden(right);
  261. }
  262. Sgl_clear_exponent_set_hidden(left);
  263. diff_exponent = result_exponent - right_exponent;
  264. /*
  265. * Special case alignment of operands that would force alignment
  266. * beyond the extent of the extension. A further optimization
  267. * could special case this but only reduces the path length for this
  268. * infrequent case.
  269. */
  270. if(diff_exponent > SGL_THRESHOLD)
  271. {
  272. diff_exponent = SGL_THRESHOLD;
  273. }
  274. /* Align right operand by shifting to right */
  275. Sgl_right_align(/*operand*/right,/*shifted by*/diff_exponent,
  276. /*and lower to*/extent);
  277. /* Treat sum and difference of the operands separately. */
  278. if( (/*signed*/int) save >= 0 )
  279. {
  280. /*
  281. * Difference of the two operands. Their can be no overflow. A
  282. * borrow can occur out of the hidden bit and force a post
  283. * normalization phase.
  284. */
  285. Sgl_subtract_withextension(left,/*minus*/right,/*with*/extent,/*into*/result);
  286. if(Sgl_iszero_hidden(result))
  287. {
  288. /* Handle normalization */
  289. /* A straightforward algorithm would now shift the result
  290. * and extension left until the hidden bit becomes one. Not
  291. * all of the extension bits need participate in the shift.
  292. * Only the two most significant bits (round and guard) are
  293. * needed. If only a single shift is needed then the guard
  294. * bit becomes a significant low order bit and the extension
  295. * must participate in the rounding. If more than a single
  296. * shift is needed, then all bits to the right of the guard
  297. * bit are zeros, and the guard bit may or may not be zero. */
  298. sign_save = Sgl_signextendedsign(result);
  299. Sgl_leftshiftby1_withextent(result,extent,result);
  300. /* Need to check for a zero result. The sign and exponent
  301. * fields have already been zeroed. The more efficient test
  302. * of the full object can be used.
  303. */
  304. if(Sgl_iszero(result))
  305. /* Must have been "x-x" or "x+(-x)". */
  306. {
  307. if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result);
  308. *dstptr = result;
  309. return(NOEXCEPTION);
  310. }
  311. result_exponent--;
  312. /* Look to see if normalization is finished. */
  313. if(Sgl_isone_hidden(result))
  314. {
  315. if(result_exponent==0)
  316. {
  317. /* Denormalized, exponent should be zero. Left operand *
  318. * was normalized, so extent (guard, round) was zero */
  319. goto underflow;
  320. }
  321. else
  322. {
  323. /* No further normalization is needed. */
  324. Sgl_set_sign(result,/*using*/sign_save);
  325. Ext_leftshiftby1(extent);
  326. goto round;
  327. }
  328. }
  329. /* Check for denormalized, exponent should be zero. Left *
  330. * operand was normalized, so extent (guard, round) was zero */
  331. if(!(underflowtrap = Is_underflowtrap_enabled()) &&
  332. result_exponent==0) goto underflow;
  333. /* Shift extension to complete one bit of normalization and
  334. * update exponent. */
  335. Ext_leftshiftby1(extent);
  336. /* Discover first one bit to determine shift amount. Use a
  337. * modified binary search. We have already shifted the result
  338. * one position right and still not found a one so the remainder
  339. * of the extension must be zero and simplifies rounding. */
  340. /* Scan bytes */
  341. while(Sgl_iszero_hiddenhigh7mantissa(result))
  342. {
  343. Sgl_leftshiftby8(result);
  344. if((result_exponent -= 8) <= 0 && !underflowtrap)
  345. goto underflow;
  346. }
  347. /* Now narrow it down to the nibble */
  348. if(Sgl_iszero_hiddenhigh3mantissa(result))
  349. {
  350. /* The lower nibble contains the normalizing one */
  351. Sgl_leftshiftby4(result);
  352. if((result_exponent -= 4) <= 0 && !underflowtrap)
  353. goto underflow;
  354. }
  355. /* Select case were first bit is set (already normalized)
  356. * otherwise select the proper shift. */
  357. if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7)
  358. {
  359. /* Already normalized */
  360. if(result_exponent <= 0) goto underflow;
  361. Sgl_set_sign(result,/*using*/sign_save);
  362. Sgl_set_exponent(result,/*using*/result_exponent);
  363. *dstptr = result;
  364. return(NOEXCEPTION);
  365. }
  366. Sgl_sethigh4bits(result,/*using*/sign_save);
  367. switch(jumpsize)
  368. {
  369. case 1:
  370. {
  371. Sgl_leftshiftby3(result);
  372. result_exponent -= 3;
  373. break;
  374. }
  375. case 2:
  376. case 3:
  377. {
  378. Sgl_leftshiftby2(result);
  379. result_exponent -= 2;
  380. break;
  381. }
  382. case 4:
  383. case 5:
  384. case 6:
  385. case 7:
  386. {
  387. Sgl_leftshiftby1(result);
  388. result_exponent -= 1;
  389. break;
  390. }
  391. }
  392. if(result_exponent > 0)
  393. {
  394. Sgl_set_exponent(result,/*using*/result_exponent);
  395. *dstptr = result; /* Sign bit is already set */
  396. return(NOEXCEPTION);
  397. }
  398. /* Fixup potential underflows */
  399. underflow:
  400. if(Is_underflowtrap_enabled())
  401. {
  402. Sgl_set_sign(result,sign_save);
  403. Sgl_setwrapped_exponent(result,result_exponent,unfl);
  404. *dstptr = result;
  405. /* inexact = FALSE */
  406. return(UNDERFLOWEXCEPTION);
  407. }
  408. /*
  409. * Since we cannot get an inexact denormalized result,
  410. * we can now return.
  411. */
  412. Sgl_right_align(result,/*by*/(1-result_exponent),extent);
  413. Sgl_clear_signexponent(result);
  414. Sgl_set_sign(result,sign_save);
  415. *dstptr = result;
  416. return(NOEXCEPTION);
  417. } /* end if(hidden...)... */
  418. /* Fall through and round */
  419. } /* end if(save >= 0)... */
  420. else
  421. {
  422. /* Add magnitudes */
  423. Sgl_addition(left,right,/*to*/result);
  424. if(Sgl_isone_hiddenoverflow(result))
  425. {
  426. /* Prenormalization required. */
  427. Sgl_rightshiftby1_withextent(result,extent,extent);
  428. Sgl_arithrightshiftby1(result);
  429. result_exponent++;
  430. } /* end if hiddenoverflow... */
  431. } /* end else ...sub magnitudes... */
  432. /* Round the result. If the extension is all zeros,then the result is
  433. * exact. Otherwise round in the correct direction. No underflow is
  434. * possible. If a postnormalization is necessary, then the mantissa is
  435. * all zeros so no shift is needed. */
  436. round:
  437. if(Ext_isnotzero(extent))
  438. {
  439. inexact = TRUE;
  440. switch(Rounding_mode())
  441. {
  442. case ROUNDNEAREST: /* The default. */
  443. if(Ext_isone_sign(extent))
  444. {
  445. /* at least 1/2 ulp */
  446. if(Ext_isnotzero_lower(extent) ||
  447. Sgl_isone_lowmantissa(result))
  448. {
  449. /* either exactly half way and odd or more than 1/2ulp */
  450. Sgl_increment(result);
  451. }
  452. }
  453. break;
  454. case ROUNDPLUS:
  455. if(Sgl_iszero_sign(result))
  456. {
  457. /* Round up positive results */
  458. Sgl_increment(result);
  459. }
  460. break;
  461. case ROUNDMINUS:
  462. if(Sgl_isone_sign(result))
  463. {
  464. /* Round down negative results */
  465. Sgl_increment(result);
  466. }
  467. case ROUNDZERO:;
  468. /* truncate is simple */
  469. } /* end switch... */
  470. if(Sgl_isone_hiddenoverflow(result)) result_exponent++;
  471. }
  472. if(result_exponent == SGL_INFINITY_EXPONENT)
  473. {
  474. /* Overflow */
  475. if(Is_overflowtrap_enabled())
  476. {
  477. Sgl_setwrapped_exponent(result,result_exponent,ovfl);
  478. *dstptr = result;
  479. if (inexact)
  480. if (Is_inexacttrap_enabled())
  481. return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
  482. else Set_inexactflag();
  483. return(OVERFLOWEXCEPTION);
  484. }
  485. else
  486. {
  487. Set_overflowflag();
  488. inexact = TRUE;
  489. Sgl_setoverflow(result);
  490. }
  491. }
  492. else Sgl_set_exponent(result,result_exponent);
  493. *dstptr = result;
  494. if(inexact)
  495. if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
  496. else Set_inexactflag();
  497. return(NOEXCEPTION);
  498. }