dfadd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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/dfadd.c $Revision: 1.1 $
  26. *
  27. * Purpose:
  28. * Double_add: add two double precision values.
  29. *
  30. * External Interfaces:
  31. * dbl_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 "dbl_float.h"
  42. /*
  43. * Double_add: add two double precision values.
  44. */
  45. dbl_fadd(
  46. dbl_floating_point *leftptr,
  47. dbl_floating_point *rightptr,
  48. dbl_floating_point *dstptr,
  49. unsigned int *status)
  50. {
  51. register unsigned int signless_upper_left, signless_upper_right, save;
  52. register unsigned int leftp1, leftp2, rightp1, rightp2, extent;
  53. register unsigned int resultp1 = 0, resultp2 = 0;
  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. 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 opposite 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_copytoptr(rightp1,rightp2,dstptr);
  133. return(NOEXCEPTION);
  134. }
  135. /*
  136. * is NaN; signaling or quiet?
  137. */
  138. if (Dbl_isone_signaling(rightp1))
  139. {
  140. /* trap if INVALIDTRAP enabled */
  141. if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
  142. /* make NaN quiet */
  143. Set_invalidflag();
  144. Dbl_set_quiet(rightp1);
  145. }
  146. /*
  147. * return quiet NaN
  148. */
  149. Dbl_copytoptr(rightp1,rightp2,dstptr);
  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. Dbl_copytoint_exponentmantissap1(leftp1,signless_upper_left);
  155. Dbl_copytoint_exponentmantissap1(rightp1,signless_upper_right);
  156. /* sign difference selects add or sub operation. */
  157. if(Dbl_ismagnitudeless(leftp2,rightp2,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. Dbl_xorfromintp1(save,rightp1,/*to*/rightp1);
  162. Dbl_xorfromintp1(save,leftp1,/*to*/leftp1);
  163. Dbl_swap_lower(leftp2,rightp2);
  164. result_exponent = Dbl_exponent(leftp1);
  165. }
  166. /* Invariant: left is not smaller than right. */
  167. if((right_exponent = Dbl_exponent(rightp1)) == 0)
  168. {
  169. /* Denormalized operands. First look for zeroes */
  170. if(Dbl_iszero_mantissa(rightp1,rightp2))
  171. {
  172. /* right is zero */
  173. if(Dbl_iszero_exponentmantissa(leftp1,leftp2))
  174. {
  175. /* Both operands are zeros */
  176. if(Is_rounding_mode(ROUNDMINUS))
  177. {
  178. Dbl_or_signs(leftp1,/*with*/rightp1);
  179. }
  180. else
  181. {
  182. Dbl_and_signs(leftp1,/*with*/rightp1);
  183. }
  184. }
  185. else
  186. {
  187. /* Left is not a zero and must be the result. Trapped
  188. * underflows are signaled if left is denormalized. Result
  189. * is always exact. */
  190. if( (result_exponent == 0) && Is_underflowtrap_enabled() )
  191. {
  192. /* need to normalize results mantissa */
  193. sign_save = Dbl_signextendedsign(leftp1);
  194. Dbl_leftshiftby1(leftp1,leftp2);
  195. Dbl_normalize(leftp1,leftp2,result_exponent);
  196. Dbl_set_sign(leftp1,/*using*/sign_save);
  197. Dbl_setwrapped_exponent(leftp1,result_exponent,unfl);
  198. Dbl_copytoptr(leftp1,leftp2,dstptr);
  199. /* inexact = FALSE */
  200. return(UNDERFLOWEXCEPTION);
  201. }
  202. }
  203. Dbl_copytoptr(leftp1,leftp2,dstptr);
  204. return(NOEXCEPTION);
  205. }
  206. /* Neither are zeroes */
  207. Dbl_clear_sign(rightp1); /* Exponent is already cleared */
  208. if(result_exponent == 0 )
  209. {
  210. /* Both operands are denormalized. The result must be exact
  211. * and is simply calculated. A sum could become normalized and a
  212. * difference could cancel to a true zero. */
  213. if( (/*signed*/int) save < 0 )
  214. {
  215. Dbl_subtract(leftp1,leftp2,/*minus*/rightp1,rightp2,
  216. /*into*/resultp1,resultp2);
  217. if(Dbl_iszero_mantissa(resultp1,resultp2))
  218. {
  219. if(Is_rounding_mode(ROUNDMINUS))
  220. {
  221. Dbl_setone_sign(resultp1);
  222. }
  223. else
  224. {
  225. Dbl_setzero_sign(resultp1);
  226. }
  227. Dbl_copytoptr(resultp1,resultp2,dstptr);
  228. return(NOEXCEPTION);
  229. }
  230. }
  231. else
  232. {
  233. Dbl_addition(leftp1,leftp2,rightp1,rightp2,
  234. /*into*/resultp1,resultp2);
  235. if(Dbl_isone_hidden(resultp1))
  236. {
  237. Dbl_copytoptr(resultp1,resultp2,dstptr);
  238. return(NOEXCEPTION);
  239. }
  240. }
  241. if(Is_underflowtrap_enabled())
  242. {
  243. /* need to normalize result */
  244. sign_save = Dbl_signextendedsign(resultp1);
  245. Dbl_leftshiftby1(resultp1,resultp2);
  246. Dbl_normalize(resultp1,resultp2,result_exponent);
  247. Dbl_set_sign(resultp1,/*using*/sign_save);
  248. Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
  249. Dbl_copytoptr(resultp1,resultp2,dstptr);
  250. /* inexact = FALSE */
  251. return(UNDERFLOWEXCEPTION);
  252. }
  253. Dbl_copytoptr(resultp1,resultp2,dstptr);
  254. return(NOEXCEPTION);
  255. }
  256. right_exponent = 1; /* Set exponent to reflect different bias
  257. * with denomalized numbers. */
  258. }
  259. else
  260. {
  261. Dbl_clear_signexponent_set_hidden(rightp1);
  262. }
  263. Dbl_clear_exponent_set_hidden(leftp1);
  264. diff_exponent = result_exponent - right_exponent;
  265. /*
  266. * Special case alignment of operands that would force alignment
  267. * beyond the extent of the extension. A further optimization
  268. * could special case this but only reduces the path length for this
  269. * infrequent case.
  270. */
  271. if(diff_exponent > DBL_THRESHOLD)
  272. {
  273. diff_exponent = DBL_THRESHOLD;
  274. }
  275. /* Align right operand by shifting to right */
  276. Dbl_right_align(/*operand*/rightp1,rightp2,/*shifted by*/diff_exponent,
  277. /*and lower to*/extent);
  278. /* Treat sum and difference of the operands separately. */
  279. if( (/*signed*/int) save < 0 )
  280. {
  281. /*
  282. * Difference of the two operands. Their can be no overflow. A
  283. * borrow can occur out of the hidden bit and force a post
  284. * normalization phase.
  285. */
  286. Dbl_subtract_withextension(leftp1,leftp2,/*minus*/rightp1,rightp2,
  287. /*with*/extent,/*into*/resultp1,resultp2);
  288. if(Dbl_iszero_hidden(resultp1))
  289. {
  290. /* Handle normalization */
  291. /* A straight forward algorithm would now shift the result
  292. * and extension left until the hidden bit becomes one. Not
  293. * all of the extension bits need participate in the shift.
  294. * Only the two most significant bits (round and guard) are
  295. * needed. If only a single shift is needed then the guard
  296. * bit becomes a significant low order bit and the extension
  297. * must participate in the rounding. If more than a single
  298. * shift is needed, then all bits to the right of the guard
  299. * bit are zeros, and the guard bit may or may not be zero. */
  300. sign_save = Dbl_signextendedsign(resultp1);
  301. Dbl_leftshiftby1_withextent(resultp1,resultp2,extent,resultp1,resultp2);
  302. /* Need to check for a zero result. The sign and exponent
  303. * fields have already been zeroed. The more efficient test
  304. * of the full object can be used.
  305. */
  306. if(Dbl_iszero(resultp1,resultp2))
  307. /* Must have been "x-x" or "x+(-x)". */
  308. {
  309. if(Is_rounding_mode(ROUNDMINUS)) Dbl_setone_sign(resultp1);
  310. Dbl_copytoptr(resultp1,resultp2,dstptr);
  311. return(NOEXCEPTION);
  312. }
  313. result_exponent--;
  314. /* Look to see if normalization is finished. */
  315. if(Dbl_isone_hidden(resultp1))
  316. {
  317. if(result_exponent==0)
  318. {
  319. /* Denormalized, exponent should be zero. Left operand *
  320. * was normalized, so extent (guard, round) was zero */
  321. goto underflow;
  322. }
  323. else
  324. {
  325. /* No further normalization is needed. */
  326. Dbl_set_sign(resultp1,/*using*/sign_save);
  327. Ext_leftshiftby1(extent);
  328. goto round;
  329. }
  330. }
  331. /* Check for denormalized, exponent should be zero. Left *
  332. * operand was normalized, so extent (guard, round) was zero */
  333. if(!(underflowtrap = Is_underflowtrap_enabled()) &&
  334. result_exponent==0) goto underflow;
  335. /* Shift extension to complete one bit of normalization and
  336. * update exponent. */
  337. Ext_leftshiftby1(extent);
  338. /* Discover first one bit to determine shift amount. Use a
  339. * modified binary search. We have already shifted the result
  340. * one position right and still not found a one so the remainder
  341. * of the extension must be zero and simplifies rounding. */
  342. /* Scan bytes */
  343. while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
  344. {
  345. Dbl_leftshiftby8(resultp1,resultp2);
  346. if((result_exponent -= 8) <= 0 && !underflowtrap)
  347. goto underflow;
  348. }
  349. /* Now narrow it down to the nibble */
  350. if(Dbl_iszero_hiddenhigh3mantissa(resultp1))
  351. {
  352. /* The lower nibble contains the normalizing one */
  353. Dbl_leftshiftby4(resultp1,resultp2);
  354. if((result_exponent -= 4) <= 0 && !underflowtrap)
  355. goto underflow;
  356. }
  357. /* Select case were first bit is set (already normalized)
  358. * otherwise select the proper shift. */
  359. if((jumpsize = Dbl_hiddenhigh3mantissa(resultp1)) > 7)
  360. {
  361. /* Already normalized */
  362. if(result_exponent <= 0) goto underflow;
  363. Dbl_set_sign(resultp1,/*using*/sign_save);
  364. Dbl_set_exponent(resultp1,/*using*/result_exponent);
  365. Dbl_copytoptr(resultp1,resultp2,dstptr);
  366. return(NOEXCEPTION);
  367. }
  368. Dbl_sethigh4bits(resultp1,/*using*/sign_save);
  369. switch(jumpsize)
  370. {
  371. case 1:
  372. {
  373. Dbl_leftshiftby3(resultp1,resultp2);
  374. result_exponent -= 3;
  375. break;
  376. }
  377. case 2:
  378. case 3:
  379. {
  380. Dbl_leftshiftby2(resultp1,resultp2);
  381. result_exponent -= 2;
  382. break;
  383. }
  384. case 4:
  385. case 5:
  386. case 6:
  387. case 7:
  388. {
  389. Dbl_leftshiftby1(resultp1,resultp2);
  390. result_exponent -= 1;
  391. break;
  392. }
  393. }
  394. if(result_exponent > 0)
  395. {
  396. Dbl_set_exponent(resultp1,/*using*/result_exponent);
  397. Dbl_copytoptr(resultp1,resultp2,dstptr);
  398. return(NOEXCEPTION); /* Sign bit is already set */
  399. }
  400. /* Fixup potential underflows */
  401. underflow:
  402. if(Is_underflowtrap_enabled())
  403. {
  404. Dbl_set_sign(resultp1,sign_save);
  405. Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
  406. Dbl_copytoptr(resultp1,resultp2,dstptr);
  407. /* inexact = FALSE */
  408. return(UNDERFLOWEXCEPTION);
  409. }
  410. /*
  411. * Since we cannot get an inexact denormalized result,
  412. * we can now return.
  413. */
  414. Dbl_fix_overshift(resultp1,resultp2,(1-result_exponent),extent);
  415. Dbl_clear_signexponent(resultp1);
  416. Dbl_set_sign(resultp1,sign_save);
  417. Dbl_copytoptr(resultp1,resultp2,dstptr);
  418. return(NOEXCEPTION);
  419. } /* end if(hidden...)... */
  420. /* Fall through and round */
  421. } /* end if(save < 0)... */
  422. else
  423. {
  424. /* Add magnitudes */
  425. Dbl_addition(leftp1,leftp2,rightp1,rightp2,/*to*/resultp1,resultp2);
  426. if(Dbl_isone_hiddenoverflow(resultp1))
  427. {
  428. /* Prenormalization required. */
  429. Dbl_rightshiftby1_withextent(resultp2,extent,extent);
  430. Dbl_arithrightshiftby1(resultp1,resultp2);
  431. result_exponent++;
  432. } /* end if hiddenoverflow... */
  433. } /* end else ...add magnitudes... */
  434. /* Round the result. If the extension is all zeros,then the result is
  435. * exact. Otherwise round in the correct direction. No underflow is
  436. * possible. If a postnormalization is necessary, then the mantissa is
  437. * all zeros so no shift is needed. */
  438. round:
  439. if(Ext_isnotzero(extent))
  440. {
  441. inexact = TRUE;
  442. switch(Rounding_mode())
  443. {
  444. case ROUNDNEAREST: /* The default. */
  445. if(Ext_isone_sign(extent))
  446. {
  447. /* at least 1/2 ulp */
  448. if(Ext_isnotzero_lower(extent) ||
  449. Dbl_isone_lowmantissap2(resultp2))
  450. {
  451. /* either exactly half way and odd or more than 1/2ulp */
  452. Dbl_increment(resultp1,resultp2);
  453. }
  454. }
  455. break;
  456. case ROUNDPLUS:
  457. if(Dbl_iszero_sign(resultp1))
  458. {
  459. /* Round up positive results */
  460. Dbl_increment(resultp1,resultp2);
  461. }
  462. break;
  463. case ROUNDMINUS:
  464. if(Dbl_isone_sign(resultp1))
  465. {
  466. /* Round down negative results */
  467. Dbl_increment(resultp1,resultp2);
  468. }
  469. case ROUNDZERO:;
  470. /* truncate is simple */
  471. } /* end switch... */
  472. if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
  473. }
  474. if(result_exponent == DBL_INFINITY_EXPONENT)
  475. {
  476. /* Overflow */
  477. if(Is_overflowtrap_enabled())
  478. {
  479. Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
  480. Dbl_copytoptr(resultp1,resultp2,dstptr);
  481. if (inexact)
  482. if (Is_inexacttrap_enabled())
  483. return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
  484. else Set_inexactflag();
  485. return(OVERFLOWEXCEPTION);
  486. }
  487. else
  488. {
  489. inexact = TRUE;
  490. Set_overflowflag();
  491. Dbl_setoverflow(resultp1,resultp2);
  492. }
  493. }
  494. else Dbl_set_exponent(resultp1,result_exponent);
  495. Dbl_copytoptr(resultp1,resultp2,dstptr);
  496. if(inexact)
  497. if(Is_inexacttrap_enabled())
  498. return(INEXACTEXCEPTION);
  499. else Set_inexactflag();
  500. return(NOEXCEPTION);
  501. }