sfadd.c 14 KB

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