dfadd.c 15 KB

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