fmpyfadd.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:78k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21. /*
  22.  * BEGIN_DESC
  23.  *
  24.  *  File:
  25.  * @(#) pa/spmath/fmpyfadd.c $Revision: 1.1 $
  26.  *
  27.  *  Purpose:
  28.  * Double Floating-point Multiply Fused Add
  29.  * Double Floating-point Multiply Negate Fused Add
  30.  * Single Floating-point Multiply Fused Add
  31.  * Single Floating-point Multiply Negate Fused Add
  32.  *
  33.  *  External Interfaces:
  34.  * dbl_fmpyfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  35.  * dbl_fmpynfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  36.  * sgl_fmpyfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  37.  * sgl_fmpynfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  38.  *
  39.  *  Internal Interfaces:
  40.  *
  41.  *  Theory:
  42.  * <<please update with a overview of the operation of this file>>
  43.  *
  44.  * END_DESC
  45. */
  46. #include "float.h"
  47. #include "sgl_float.h"
  48. #include "dbl_float.h"
  49. /*
  50.  *  Double Floating-point Multiply Fused Add
  51.  */
  52. int
  53. dbl_fmpyfadd(
  54.     dbl_floating_point *src1ptr,
  55.     dbl_floating_point *src2ptr,
  56.     dbl_floating_point *src3ptr,
  57.     unsigned int *status,
  58.     dbl_floating_point *dstptr)
  59. {
  60. unsigned int opnd1p1, opnd1p2, opnd2p1, opnd2p2, opnd3p1, opnd3p2;
  61. register unsigned int tmpresp1, tmpresp2, tmpresp3, tmpresp4;
  62. unsigned int rightp1, rightp2, rightp3, rightp4;
  63. unsigned int resultp1, resultp2 = 0, resultp3 = 0, resultp4 = 0;
  64. register int mpy_exponent, add_exponent, count;
  65. boolean inexact = FALSE, is_tiny = FALSE;
  66. unsigned int signlessleft1, signlessright1, save;
  67. register int result_exponent, diff_exponent;
  68. int sign_save, jumpsize;
  69. Dbl_copyfromptr(src1ptr,opnd1p1,opnd1p2);
  70. Dbl_copyfromptr(src2ptr,opnd2p1,opnd2p2);
  71. Dbl_copyfromptr(src3ptr,opnd3p1,opnd3p2);
  72. /* 
  73.  * set sign bit of result of multiply
  74.  */
  75. if (Dbl_sign(opnd1p1) ^ Dbl_sign(opnd2p1)) 
  76. Dbl_setnegativezerop1(resultp1); 
  77. else Dbl_setzerop1(resultp1);
  78. /*
  79.  * Generate multiply exponent 
  80.  */
  81. mpy_exponent = Dbl_exponent(opnd1p1) + Dbl_exponent(opnd2p1) - DBL_BIAS;
  82. /*
  83.  * check first operand for NaN's or infinity
  84.  */
  85. if (Dbl_isinfinity_exponent(opnd1p1)) {
  86. if (Dbl_iszero_mantissa(opnd1p1,opnd1p2)) {
  87. if (Dbl_isnotnan(opnd2p1,opnd2p2) &&
  88.     Dbl_isnotnan(opnd3p1,opnd3p2)) {
  89. if (Dbl_iszero_exponentmantissa(opnd2p1,opnd2p2)) {
  90. /* 
  91.  * invalid since operands are infinity 
  92.  * and zero 
  93.  */
  94. if (Is_invalidtrap_enabled())
  95. return(OPC_2E_INVALIDEXCEPTION);
  96. Set_invalidflag();
  97. Dbl_makequietnan(resultp1,resultp2);
  98. Dbl_copytoptr(resultp1,resultp2,dstptr);
  99. return(NOEXCEPTION);
  100. }
  101. /*
  102.  * Check third operand for infinity with a
  103.  *  sign opposite of the multiply result
  104.  */
  105. if (Dbl_isinfinity(opnd3p1,opnd3p2) &&
  106.     (Dbl_sign(resultp1) ^ Dbl_sign(opnd3p1))) {
  107. /* 
  108.  * invalid since attempting a magnitude
  109.  * subtraction of infinities
  110.  */
  111. if (Is_invalidtrap_enabled())
  112. return(OPC_2E_INVALIDEXCEPTION);
  113. Set_invalidflag();
  114. Dbl_makequietnan(resultp1,resultp2);
  115. Dbl_copytoptr(resultp1,resultp2,dstptr);
  116. return(NOEXCEPTION);
  117. }
  118. /*
  119.    * return infinity
  120.    */
  121. Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
  122. Dbl_copytoptr(resultp1,resultp2,dstptr);
  123. return(NOEXCEPTION);
  124. }
  125. }
  126. else {
  127. /*
  128.    * is NaN; signaling or quiet?
  129.    */
  130. if (Dbl_isone_signaling(opnd1p1)) {
  131. /* trap if INVALIDTRAP enabled */
  132. if (Is_invalidtrap_enabled()) 
  133.      return(OPC_2E_INVALIDEXCEPTION);
  134. /* make NaN quiet */
  135. Set_invalidflag();
  136. Dbl_set_quiet(opnd1p1);
  137. }
  138. /* 
  139.  * is second operand a signaling NaN? 
  140.  */
  141. else if (Dbl_is_signalingnan(opnd2p1)) {
  142. /* trap if INVALIDTRAP enabled */
  143. if (Is_invalidtrap_enabled())
  144.      return(OPC_2E_INVALIDEXCEPTION);
  145. /* make NaN quiet */
  146. Set_invalidflag();
  147. Dbl_set_quiet(opnd2p1);
  148. Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
  149. return(NOEXCEPTION);
  150. }
  151. /* 
  152.  * is third operand a signaling NaN? 
  153.  */
  154. else if (Dbl_is_signalingnan(opnd3p1)) {
  155. /* trap if INVALIDTRAP enabled */
  156. if (Is_invalidtrap_enabled())
  157.      return(OPC_2E_INVALIDEXCEPTION);
  158. /* make NaN quiet */
  159. Set_invalidflag();
  160. Dbl_set_quiet(opnd3p1);
  161. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  162. return(NOEXCEPTION);
  163. }
  164. /*
  165.    * return quiet NaN
  166.    */
  167. Dbl_copytoptr(opnd1p1,opnd1p2,dstptr);
  168. return(NOEXCEPTION);
  169. }
  170. }
  171. /*
  172.  * check second operand for NaN's or infinity
  173.  */
  174. if (Dbl_isinfinity_exponent(opnd2p1)) {
  175. if (Dbl_iszero_mantissa(opnd2p1,opnd2p2)) {
  176. if (Dbl_isnotnan(opnd3p1,opnd3p2)) {
  177. if (Dbl_iszero_exponentmantissa(opnd1p1,opnd1p2)) {
  178. /* 
  179.  * invalid since multiply operands are
  180.  * zero & infinity
  181.  */
  182. if (Is_invalidtrap_enabled())
  183. return(OPC_2E_INVALIDEXCEPTION);
  184. Set_invalidflag();
  185. Dbl_makequietnan(opnd2p1,opnd2p2);
  186. Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
  187. return(NOEXCEPTION);
  188. }
  189. /*
  190.  * Check third operand for infinity with a
  191.  *  sign opposite of the multiply result
  192.  */
  193. if (Dbl_isinfinity(opnd3p1,opnd3p2) &&
  194.     (Dbl_sign(resultp1) ^ Dbl_sign(opnd3p1))) {
  195. /* 
  196.  * invalid since attempting a magnitude
  197.  * subtraction of infinities
  198.  */
  199. if (Is_invalidtrap_enabled())
  200.         return(OPC_2E_INVALIDEXCEPTION);
  201.         Set_invalidflag();
  202.         Dbl_makequietnan(resultp1,resultp2);
  203. Dbl_copytoptr(resultp1,resultp2,dstptr);
  204. return(NOEXCEPTION);
  205. }
  206. /*
  207.  * return infinity
  208.  */
  209. Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
  210. Dbl_copytoptr(resultp1,resultp2,dstptr);
  211. return(NOEXCEPTION);
  212. }
  213. }
  214. else {
  215. /*
  216.  * is NaN; signaling or quiet?
  217.  */
  218. if (Dbl_isone_signaling(opnd2p1)) {
  219. /* trap if INVALIDTRAP enabled */
  220. if (Is_invalidtrap_enabled())
  221. return(OPC_2E_INVALIDEXCEPTION);
  222. /* make NaN quiet */
  223. Set_invalidflag();
  224. Dbl_set_quiet(opnd2p1);
  225. }
  226. /* 
  227.  * is third operand a signaling NaN? 
  228.  */
  229. else if (Dbl_is_signalingnan(opnd3p1)) {
  230.         /* trap if INVALIDTRAP enabled */
  231.         if (Is_invalidtrap_enabled())
  232.     return(OPC_2E_INVALIDEXCEPTION);
  233.         /* make NaN quiet */
  234.         Set_invalidflag();
  235.         Dbl_set_quiet(opnd3p1);
  236. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  237.         return(NOEXCEPTION);
  238. }
  239. /*
  240.  * return quiet NaN
  241.  */
  242. Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
  243. return(NOEXCEPTION);
  244. }
  245. }
  246. /*
  247.  * check third operand for NaN's or infinity
  248.  */
  249. if (Dbl_isinfinity_exponent(opnd3p1)) {
  250. if (Dbl_iszero_mantissa(opnd3p1,opnd3p2)) {
  251. /* return infinity */
  252. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  253. return(NOEXCEPTION);
  254. } else {
  255. /*
  256.  * is NaN; signaling or quiet?
  257.  */
  258. if (Dbl_isone_signaling(opnd3p1)) {
  259. /* trap if INVALIDTRAP enabled */
  260. if (Is_invalidtrap_enabled())
  261. return(OPC_2E_INVALIDEXCEPTION);
  262. /* make NaN quiet */
  263. Set_invalidflag();
  264. Dbl_set_quiet(opnd3p1);
  265. }
  266. /*
  267.  * return quiet NaN
  268.    */
  269. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  270. return(NOEXCEPTION);
  271. }
  272.      }
  273. /*
  274.  * Generate multiply mantissa
  275.  */
  276. if (Dbl_isnotzero_exponent(opnd1p1)) {
  277. /* set hidden bit */
  278. Dbl_clear_signexponent_set_hidden(opnd1p1);
  279. }
  280. else {
  281. /* check for zero */
  282. if (Dbl_iszero_mantissa(opnd1p1,opnd1p2)) {
  283. /*
  284.  * Perform the add opnd3 with zero here.
  285.  */
  286. if (Dbl_iszero_exponentmantissa(opnd3p1,opnd3p2)) {
  287. if (Is_rounding_mode(ROUNDMINUS)) {
  288. Dbl_or_signs(opnd3p1,resultp1);
  289. } else {
  290. Dbl_and_signs(opnd3p1,resultp1);
  291. }
  292. }
  293. /*
  294.  * Now let's check for trapped underflow case.
  295.  */
  296. else if (Dbl_iszero_exponent(opnd3p1) &&
  297.          Is_underflowtrap_enabled()) {
  298.                      /* need to normalize results mantissa */
  299.                      sign_save = Dbl_signextendedsign(opnd3p1);
  300. result_exponent = 0;
  301.                      Dbl_leftshiftby1(opnd3p1,opnd3p2);
  302.                      Dbl_normalize(opnd3p1,opnd3p2,result_exponent);
  303.                      Dbl_set_sign(opnd3p1,/*using*/sign_save);
  304.                      Dbl_setwrapped_exponent(opnd3p1,result_exponent,
  305. unfl);
  306.                      Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  307.                      /* inexact = FALSE */
  308.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  309. }
  310. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  311. return(NOEXCEPTION);
  312. }
  313. /* is denormalized, adjust exponent */
  314. Dbl_clear_signexponent(opnd1p1);
  315. Dbl_leftshiftby1(opnd1p1,opnd1p2);
  316. Dbl_normalize(opnd1p1,opnd1p2,mpy_exponent);
  317. }
  318. /* opnd2 needs to have hidden bit set with msb in hidden bit */
  319. if (Dbl_isnotzero_exponent(opnd2p1)) {
  320. Dbl_clear_signexponent_set_hidden(opnd2p1);
  321. }
  322. else {
  323. /* check for zero */
  324. if (Dbl_iszero_mantissa(opnd2p1,opnd2p2)) {
  325. /*
  326.  * Perform the add opnd3 with zero here.
  327.  */
  328. if (Dbl_iszero_exponentmantissa(opnd3p1,opnd3p2)) {
  329. if (Is_rounding_mode(ROUNDMINUS)) {
  330. Dbl_or_signs(opnd3p1,resultp1);
  331. } else {
  332. Dbl_and_signs(opnd3p1,resultp1);
  333. }
  334. }
  335. /*
  336.  * Now let's check for trapped underflow case.
  337.  */
  338. else if (Dbl_iszero_exponent(opnd3p1) &&
  339.     Is_underflowtrap_enabled()) {
  340.                      /* need to normalize results mantissa */
  341.                      sign_save = Dbl_signextendedsign(opnd3p1);
  342. result_exponent = 0;
  343.                      Dbl_leftshiftby1(opnd3p1,opnd3p2);
  344.                      Dbl_normalize(opnd3p1,opnd3p2,result_exponent);
  345.                      Dbl_set_sign(opnd3p1,/*using*/sign_save);
  346.                      Dbl_setwrapped_exponent(opnd3p1,result_exponent,
  347. unfl);
  348.                      Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  349.                      /* inexact = FALSE */
  350. return(OPC_2E_UNDERFLOWEXCEPTION);
  351. }
  352. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  353. return(NOEXCEPTION);
  354. }
  355. /* is denormalized; want to normalize */
  356. Dbl_clear_signexponent(opnd2p1);
  357. Dbl_leftshiftby1(opnd2p1,opnd2p2);
  358. Dbl_normalize(opnd2p1,opnd2p2,mpy_exponent);
  359. }
  360. /* Multiply the first two source mantissas together */
  361. /* 
  362.  * The intermediate result will be kept in tmpres,
  363.  * which needs enough room for 106 bits of mantissa,
  364.  * so lets call it a Double extended.
  365.  */
  366. Dblext_setzero(tmpresp1,tmpresp2,tmpresp3,tmpresp4);
  367. /* 
  368.  * Four bits at a time are inspected in each loop, and a 
  369.  * simple shift and add multiply algorithm is used. 
  370.  */ 
  371. for (count = DBL_P-1; count >= 0; count -= 4) {
  372. Dblext_rightshiftby4(tmpresp1,tmpresp2,tmpresp3,tmpresp4);
  373. if (Dbit28p2(opnd1p2)) {
  374.   /* Fourword_add should be an ADD followed by 3 ADDC's */
  375. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4, 
  376.  opnd2p1<<3 | opnd2p2>>29, opnd2p2<<3, 0, 0);
  377. }
  378. if (Dbit29p2(opnd1p2)) {
  379. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4,
  380.  opnd2p1<<2 | opnd2p2>>30, opnd2p2<<2, 0, 0);
  381. }
  382. if (Dbit30p2(opnd1p2)) {
  383. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4,
  384.  opnd2p1<<1 | opnd2p2>>31, opnd2p2<<1, 0, 0);
  385. }
  386. if (Dbit31p2(opnd1p2)) {
  387. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4,
  388.  opnd2p1, opnd2p2, 0, 0);
  389. }
  390. Dbl_rightshiftby4(opnd1p1,opnd1p2);
  391. }
  392. if (Is_dexthiddenoverflow(tmpresp1)) {
  393. /* result mantissa >= 2 (mantissa overflow) */
  394. mpy_exponent++;
  395. Dblext_rightshiftby1(tmpresp1,tmpresp2,tmpresp3,tmpresp4);
  396. }
  397. /*
  398.  * Restore the sign of the mpy result which was saved in resultp1.
  399.  * The exponent will continue to be kept in mpy_exponent.
  400.  */
  401. Dblext_set_sign(tmpresp1,Dbl_sign(resultp1));
  402. /* 
  403.  * No rounding is required, since the result of the multiply
  404.  * is exact in the extended format.
  405.  */
  406. /*
  407.  * Now we are ready to perform the add portion of the operation.
  408.  *
  409.  * The exponents need to be kept as integers for now, since the
  410.  * multiply result might not fit into the exponent field.  We
  411.  * can't overflow or underflow because of this yet, since the
  412.  * add could bring the final result back into range.
  413.  */
  414. add_exponent = Dbl_exponent(opnd3p1);
  415. /*
  416.  * Check for denormalized or zero add operand.
  417.  */
  418. if (add_exponent == 0) {
  419. /* check for zero */
  420. if (Dbl_iszero_mantissa(opnd3p1,opnd3p2)) {
  421. /* right is zero */
  422. /* Left can't be zero and must be result.
  423.  *
  424.  * The final result is now in tmpres and mpy_exponent,
  425.  * and needs to be rounded and squeezed back into
  426.  * double precision format from double extended.
  427.  */
  428. result_exponent = mpy_exponent;
  429. Dblext_copy(tmpresp1,tmpresp2,tmpresp3,tmpresp4,
  430. resultp1,resultp2,resultp3,resultp4);
  431. sign_save = Dbl_signextendedsign(resultp1);/*save sign*/
  432. goto round;
  433. }
  434. /* 
  435.  * Neither are zeroes.  
  436.  * Adjust exponent and normalize add operand.
  437.  */
  438. sign_save = Dbl_signextendedsign(opnd3p1); /* save sign */
  439. Dbl_clear_signexponent(opnd3p1);
  440. Dbl_leftshiftby1(opnd3p1,opnd3p2);
  441. Dbl_normalize(opnd3p1,opnd3p2,add_exponent);
  442. Dbl_set_sign(opnd3p1,sign_save); /* restore sign */
  443. } else {
  444. Dbl_clear_exponent_set_hidden(opnd3p1);
  445. }
  446. /*
  447.  * Copy opnd3 to the double extended variable called right.
  448.  */
  449. Dbl_copyto_dblext(opnd3p1,opnd3p2,rightp1,rightp2,rightp3,rightp4);
  450. /*
  451.  * A zero "save" helps discover equal operands (for later),
  452.  * and is used in swapping operands (if needed).
  453.  */
  454. Dblext_xortointp1(tmpresp1,rightp1,/*to*/save);
  455. /*
  456.  * Compare magnitude of operands.
  457.  */
  458. Dblext_copytoint_exponentmantissap1(tmpresp1,signlessleft1);
  459. Dblext_copytoint_exponentmantissap1(rightp1,signlessright1);
  460. if (mpy_exponent < add_exponent || mpy_exponent == add_exponent &&
  461.     Dblext_ismagnitudeless(tmpresp2,rightp2,signlessleft1,signlessright1)){
  462. /*
  463.  * Set the left operand to the larger one by XOR swap.
  464.  * First finish the first word "save".
  465.  */
  466. Dblext_xorfromintp1(save,rightp1,/*to*/rightp1);
  467. Dblext_xorfromintp1(save,tmpresp1,/*to*/tmpresp1);
  468. Dblext_swap_lower(tmpresp2,tmpresp3,tmpresp4,
  469. rightp2,rightp3,rightp4);
  470. /* also setup exponents used in rest of routine */
  471. diff_exponent = add_exponent - mpy_exponent;
  472. result_exponent = add_exponent;
  473. } else {
  474. /* also setup exponents used in rest of routine */
  475. diff_exponent = mpy_exponent - add_exponent;
  476. result_exponent = mpy_exponent;
  477. }
  478. /* Invariant: left is not smaller than right. */
  479. /*
  480.  * Special case alignment of operands that would force alignment
  481.  * beyond the extent of the extension.  A further optimization
  482.  * could special case this but only reduces the path length for
  483.  * this infrequent case.
  484.  */
  485. if (diff_exponent > DBLEXT_THRESHOLD) {
  486. diff_exponent = DBLEXT_THRESHOLD;
  487. }
  488. /* Align right operand by shifting it to the right */
  489. Dblext_clear_sign(rightp1);
  490. Dblext_right_align(rightp1,rightp2,rightp3,rightp4,
  491. /*shifted by*/diff_exponent);
  492. /* Treat sum and difference of the operands separately. */
  493. if ((int)save < 0) {
  494. /*
  495.  * Difference of the two operands.  Overflow can occur if the
  496.  * multiply overflowed.  A borrow can occur out of the hidden
  497.  * bit and force a post normalization phase.
  498.  */
  499. Dblext_subtract(tmpresp1,tmpresp2,tmpresp3,tmpresp4,
  500. rightp1,rightp2,rightp3,rightp4,
  501. resultp1,resultp2,resultp3,resultp4);
  502. sign_save = Dbl_signextendedsign(resultp1);
  503. if (Dbl_iszero_hidden(resultp1)) {
  504. /* Handle normalization */
  505. /* A straight foward algorithm would now shift the
  506.  * result and extension left until the hidden bit
  507.  * becomes one.  Not all of the extension bits need
  508.  * participate in the shift.  Only the two most 
  509.  * significant bits (round and guard) are needed.
  510.  * If only a single shift is needed then the guard
  511.  * bit becomes a significant low order bit and the
  512.  * extension must participate in the rounding.
  513.  * If more than a single shift is needed, then all
  514.  * bits to the right of the guard bit are zeros, 
  515.  * and the guard bit may or may not be zero. */
  516. Dblext_leftshiftby1(resultp1,resultp2,resultp3,
  517. resultp4);
  518. /* Need to check for a zero result.  The sign and
  519.  * exponent fields have already been zeroed.  The more
  520.  * efficient test of the full object can be used.
  521.  */
  522.  if(Dblext_iszero(resultp1,resultp2,resultp3,resultp4)){
  523. /* Must have been "x-x" or "x+(-x)". */
  524. if (Is_rounding_mode(ROUNDMINUS))
  525. Dbl_setone_sign(resultp1);
  526. Dbl_copytoptr(resultp1,resultp2,dstptr);
  527. return(NOEXCEPTION);
  528. }
  529. result_exponent--;
  530. /* Look to see if normalization is finished. */
  531. if (Dbl_isone_hidden(resultp1)) {
  532. /* No further normalization is needed */
  533. goto round;
  534. }
  535. /* Discover first one bit to determine shift amount.
  536.  * Use a modified binary search.  We have already
  537.  * shifted the result one position right and still
  538.  * not found a one so the remainder of the extension
  539.  * must be zero and simplifies rounding. */
  540. /* Scan bytes */
  541. while (Dbl_iszero_hiddenhigh7mantissa(resultp1)) {
  542. Dblext_leftshiftby8(resultp1,resultp2,resultp3,resultp4);
  543. result_exponent -= 8;
  544. }
  545. /* Now narrow it down to the nibble */
  546. if (Dbl_iszero_hiddenhigh3mantissa(resultp1)) {
  547. /* The lower nibble contains the
  548.  * normalizing one */
  549. Dblext_leftshiftby4(resultp1,resultp2,resultp3,resultp4);
  550. result_exponent -= 4;
  551. }
  552. /* Select case where first bit is set (already
  553.  * normalized) otherwise select the proper shift. */
  554. jumpsize = Dbl_hiddenhigh3mantissa(resultp1);
  555. if (jumpsize <= 7) switch(jumpsize) {
  556. case 1:
  557. Dblext_leftshiftby3(resultp1,resultp2,resultp3,
  558. resultp4);
  559. result_exponent -= 3;
  560. break;
  561. case 2:
  562. case 3:
  563. Dblext_leftshiftby2(resultp1,resultp2,resultp3,
  564. resultp4);
  565. result_exponent -= 2;
  566. break;
  567. case 4:
  568. case 5:
  569. case 6:
  570. case 7:
  571. Dblext_leftshiftby1(resultp1,resultp2,resultp3,
  572. resultp4);
  573. result_exponent -= 1;
  574. break;
  575. }
  576. } /* end if (hidden...)... */
  577. /* Fall through and round */
  578. } /* end if (save < 0)... */
  579. else {
  580. /* Add magnitudes */
  581. Dblext_addition(tmpresp1,tmpresp2,tmpresp3,tmpresp4,
  582. rightp1,rightp2,rightp3,rightp4,
  583. /*to*/resultp1,resultp2,resultp3,resultp4);
  584. sign_save = Dbl_signextendedsign(resultp1);
  585. if (Dbl_isone_hiddenoverflow(resultp1)) {
  586.      /* Prenormalization required. */
  587.      Dblext_arithrightshiftby1(resultp1,resultp2,resultp3,
  588. resultp4);
  589.      result_exponent++;
  590. } /* end if hiddenoverflow... */
  591. } /* end else ...add magnitudes... */
  592. /* Round the result.  If the extension and lower two words are
  593.  * all zeros, then the result is exact.  Otherwise round in the
  594.  * correct direction.  Underflow is possible. If a postnormalization
  595.  * is necessary, then the mantissa is all zeros so no shift is needed.
  596.  */
  597.   round:
  598. if (result_exponent <= 0 && !Is_underflowtrap_enabled()) {
  599. Dblext_denormalize(resultp1,resultp2,resultp3,resultp4,
  600. result_exponent,is_tiny);
  601. }
  602. Dbl_set_sign(resultp1,/*using*/sign_save);
  603. if (Dblext_isnotzero_mantissap3(resultp3) || 
  604.     Dblext_isnotzero_mantissap4(resultp4)) {
  605. inexact = TRUE;
  606. switch(Rounding_mode()) {
  607. case ROUNDNEAREST: /* The default. */
  608. if (Dblext_isone_highp3(resultp3)) {
  609. /* at least 1/2 ulp */
  610. if (Dblext_isnotzero_low31p3(resultp3) ||
  611.     Dblext_isnotzero_mantissap4(resultp4) ||
  612.     Dblext_isone_lowp2(resultp2)) {
  613. /* either exactly half way and odd or
  614.  * more than 1/2ulp */
  615. Dbl_increment(resultp1,resultp2);
  616. }
  617. }
  618.      break;
  619. case ROUNDPLUS:
  620.      if (Dbl_iszero_sign(resultp1)) {
  621. /* Round up positive results */
  622. Dbl_increment(resultp1,resultp2);
  623. }
  624. break;
  625.     
  626. case ROUNDMINUS:
  627.      if (Dbl_isone_sign(resultp1)) {
  628. /* Round down negative results */
  629. Dbl_increment(resultp1,resultp2);
  630. }
  631.     
  632. case ROUNDZERO:;
  633. /* truncate is simple */
  634. } /* end switch... */
  635. if (Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
  636. }
  637. if (result_exponent >= DBL_INFINITY_EXPONENT) {
  638.                 /* trap if OVERFLOWTRAP enabled */
  639.                 if (Is_overflowtrap_enabled()) {
  640.                         /*
  641.                          * Adjust bias of result
  642.                          */
  643.                         Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
  644.                         Dbl_copytoptr(resultp1,resultp2,dstptr);
  645.                         if (inexact)
  646.                             if (Is_inexacttrap_enabled())
  647.                                 return (OPC_2E_OVERFLOWEXCEPTION |
  648. OPC_2E_INEXACTEXCEPTION);
  649.                             else Set_inexactflag();
  650.                         return (OPC_2E_OVERFLOWEXCEPTION);
  651.                 }
  652.                 inexact = TRUE;
  653.                 Set_overflowflag();
  654.                 /* set result to infinity or largest number */
  655.                 Dbl_setoverflow(resultp1,resultp2);
  656. } else if (result_exponent <= 0) { /* underflow case */
  657. if (Is_underflowtrap_enabled()) {
  658.                         /*
  659.                          * Adjust bias of result
  660.                          */
  661.                  Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
  662. Dbl_copytoptr(resultp1,resultp2,dstptr);
  663.                         if (inexact)
  664.                             if (Is_inexacttrap_enabled())
  665.                                 return (OPC_2E_UNDERFLOWEXCEPTION |
  666. OPC_2E_INEXACTEXCEPTION);
  667.                             else Set_inexactflag();
  668.      return(OPC_2E_UNDERFLOWEXCEPTION);
  669. }
  670. else if (inexact && is_tiny) Set_underflowflag();
  671. }
  672. else Dbl_set_exponent(resultp1,result_exponent);
  673. Dbl_copytoptr(resultp1,resultp2,dstptr);
  674. if (inexact) 
  675. if (Is_inexacttrap_enabled()) return(OPC_2E_INEXACTEXCEPTION);
  676. else Set_inexactflag();
  677.      return(NOEXCEPTION);
  678. }
  679. /*
  680.  *  Double Floating-point Multiply Negate Fused Add
  681.  */
  682. dbl_fmpynfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  683. dbl_floating_point *src1ptr, *src2ptr, *src3ptr, *dstptr;
  684. unsigned int *status;
  685. {
  686. unsigned int opnd1p1, opnd1p2, opnd2p1, opnd2p2, opnd3p1, opnd3p2;
  687. register unsigned int tmpresp1, tmpresp2, tmpresp3, tmpresp4;
  688. unsigned int rightp1, rightp2, rightp3, rightp4;
  689. unsigned int resultp1, resultp2 = 0, resultp3 = 0, resultp4 = 0;
  690. register int mpy_exponent, add_exponent, count;
  691. boolean inexact = FALSE, is_tiny = FALSE;
  692. unsigned int signlessleft1, signlessright1, save;
  693. register int result_exponent, diff_exponent;
  694. int sign_save, jumpsize;
  695. Dbl_copyfromptr(src1ptr,opnd1p1,opnd1p2);
  696. Dbl_copyfromptr(src2ptr,opnd2p1,opnd2p2);
  697. Dbl_copyfromptr(src3ptr,opnd3p1,opnd3p2);
  698. /* 
  699.  * set sign bit of result of multiply
  700.  */
  701. if (Dbl_sign(opnd1p1) ^ Dbl_sign(opnd2p1)) 
  702. Dbl_setzerop1(resultp1);
  703. else
  704. Dbl_setnegativezerop1(resultp1); 
  705. /*
  706.  * Generate multiply exponent 
  707.  */
  708. mpy_exponent = Dbl_exponent(opnd1p1) + Dbl_exponent(opnd2p1) - DBL_BIAS;
  709. /*
  710.  * check first operand for NaN's or infinity
  711.  */
  712. if (Dbl_isinfinity_exponent(opnd1p1)) {
  713. if (Dbl_iszero_mantissa(opnd1p1,opnd1p2)) {
  714. if (Dbl_isnotnan(opnd2p1,opnd2p2) &&
  715.     Dbl_isnotnan(opnd3p1,opnd3p2)) {
  716. if (Dbl_iszero_exponentmantissa(opnd2p1,opnd2p2)) {
  717. /* 
  718.  * invalid since operands are infinity 
  719.  * and zero 
  720.  */
  721. if (Is_invalidtrap_enabled())
  722. return(OPC_2E_INVALIDEXCEPTION);
  723. Set_invalidflag();
  724. Dbl_makequietnan(resultp1,resultp2);
  725. Dbl_copytoptr(resultp1,resultp2,dstptr);
  726. return(NOEXCEPTION);
  727. }
  728. /*
  729.  * Check third operand for infinity with a
  730.  *  sign opposite of the multiply result
  731.  */
  732. if (Dbl_isinfinity(opnd3p1,opnd3p2) &&
  733.     (Dbl_sign(resultp1) ^ Dbl_sign(opnd3p1))) {
  734. /* 
  735.  * invalid since attempting a magnitude
  736.  * subtraction of infinities
  737.  */
  738. if (Is_invalidtrap_enabled())
  739. return(OPC_2E_INVALIDEXCEPTION);
  740. Set_invalidflag();
  741. Dbl_makequietnan(resultp1,resultp2);
  742. Dbl_copytoptr(resultp1,resultp2,dstptr);
  743. return(NOEXCEPTION);
  744. }
  745. /*
  746.    * return infinity
  747.    */
  748. Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
  749. Dbl_copytoptr(resultp1,resultp2,dstptr);
  750. return(NOEXCEPTION);
  751. }
  752. }
  753. else {
  754. /*
  755.    * is NaN; signaling or quiet?
  756.    */
  757. if (Dbl_isone_signaling(opnd1p1)) {
  758. /* trap if INVALIDTRAP enabled */
  759. if (Is_invalidtrap_enabled()) 
  760.      return(OPC_2E_INVALIDEXCEPTION);
  761. /* make NaN quiet */
  762. Set_invalidflag();
  763. Dbl_set_quiet(opnd1p1);
  764. }
  765. /* 
  766.  * is second operand a signaling NaN? 
  767.  */
  768. else if (Dbl_is_signalingnan(opnd2p1)) {
  769. /* trap if INVALIDTRAP enabled */
  770. if (Is_invalidtrap_enabled())
  771.      return(OPC_2E_INVALIDEXCEPTION);
  772. /* make NaN quiet */
  773. Set_invalidflag();
  774. Dbl_set_quiet(opnd2p1);
  775. Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
  776. return(NOEXCEPTION);
  777. }
  778. /* 
  779.  * is third operand a signaling NaN? 
  780.  */
  781. else if (Dbl_is_signalingnan(opnd3p1)) {
  782. /* trap if INVALIDTRAP enabled */
  783. if (Is_invalidtrap_enabled())
  784.      return(OPC_2E_INVALIDEXCEPTION);
  785. /* make NaN quiet */
  786. Set_invalidflag();
  787. Dbl_set_quiet(opnd3p1);
  788. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  789. return(NOEXCEPTION);
  790. }
  791. /*
  792.    * return quiet NaN
  793.    */
  794. Dbl_copytoptr(opnd1p1,opnd1p2,dstptr);
  795. return(NOEXCEPTION);
  796. }
  797. }
  798. /*
  799.  * check second operand for NaN's or infinity
  800.  */
  801. if (Dbl_isinfinity_exponent(opnd2p1)) {
  802. if (Dbl_iszero_mantissa(opnd2p1,opnd2p2)) {
  803. if (Dbl_isnotnan(opnd3p1,opnd3p2)) {
  804. if (Dbl_iszero_exponentmantissa(opnd1p1,opnd1p2)) {
  805. /* 
  806.  * invalid since multiply operands are
  807.  * zero & infinity
  808.  */
  809. if (Is_invalidtrap_enabled())
  810. return(OPC_2E_INVALIDEXCEPTION);
  811. Set_invalidflag();
  812. Dbl_makequietnan(opnd2p1,opnd2p2);
  813. Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
  814. return(NOEXCEPTION);
  815. }
  816. /*
  817.  * Check third operand for infinity with a
  818.  *  sign opposite of the multiply result
  819.  */
  820. if (Dbl_isinfinity(opnd3p1,opnd3p2) &&
  821.     (Dbl_sign(resultp1) ^ Dbl_sign(opnd3p1))) {
  822. /* 
  823.  * invalid since attempting a magnitude
  824.  * subtraction of infinities
  825.  */
  826. if (Is_invalidtrap_enabled())
  827.         return(OPC_2E_INVALIDEXCEPTION);
  828.         Set_invalidflag();
  829.         Dbl_makequietnan(resultp1,resultp2);
  830. Dbl_copytoptr(resultp1,resultp2,dstptr);
  831. return(NOEXCEPTION);
  832. }
  833. /*
  834.  * return infinity
  835.  */
  836. Dbl_setinfinity_exponentmantissa(resultp1,resultp2);
  837. Dbl_copytoptr(resultp1,resultp2,dstptr);
  838. return(NOEXCEPTION);
  839. }
  840. }
  841. else {
  842. /*
  843.  * is NaN; signaling or quiet?
  844.  */
  845. if (Dbl_isone_signaling(opnd2p1)) {
  846. /* trap if INVALIDTRAP enabled */
  847. if (Is_invalidtrap_enabled())
  848. return(OPC_2E_INVALIDEXCEPTION);
  849. /* make NaN quiet */
  850. Set_invalidflag();
  851. Dbl_set_quiet(opnd2p1);
  852. }
  853. /* 
  854.  * is third operand a signaling NaN? 
  855.  */
  856. else if (Dbl_is_signalingnan(opnd3p1)) {
  857.         /* trap if INVALIDTRAP enabled */
  858.         if (Is_invalidtrap_enabled())
  859.     return(OPC_2E_INVALIDEXCEPTION);
  860.         /* make NaN quiet */
  861.         Set_invalidflag();
  862.         Dbl_set_quiet(opnd3p1);
  863. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  864.         return(NOEXCEPTION);
  865. }
  866. /*
  867.  * return quiet NaN
  868.  */
  869. Dbl_copytoptr(opnd2p1,opnd2p2,dstptr);
  870. return(NOEXCEPTION);
  871. }
  872. }
  873. /*
  874.  * check third operand for NaN's or infinity
  875.  */
  876. if (Dbl_isinfinity_exponent(opnd3p1)) {
  877. if (Dbl_iszero_mantissa(opnd3p1,opnd3p2)) {
  878. /* return infinity */
  879. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  880. return(NOEXCEPTION);
  881. } else {
  882. /*
  883.  * is NaN; signaling or quiet?
  884.  */
  885. if (Dbl_isone_signaling(opnd3p1)) {
  886. /* trap if INVALIDTRAP enabled */
  887. if (Is_invalidtrap_enabled())
  888. return(OPC_2E_INVALIDEXCEPTION);
  889. /* make NaN quiet */
  890. Set_invalidflag();
  891. Dbl_set_quiet(opnd3p1);
  892. }
  893. /*
  894.  * return quiet NaN
  895.    */
  896. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  897. return(NOEXCEPTION);
  898. }
  899.      }
  900. /*
  901.  * Generate multiply mantissa
  902.  */
  903. if (Dbl_isnotzero_exponent(opnd1p1)) {
  904. /* set hidden bit */
  905. Dbl_clear_signexponent_set_hidden(opnd1p1);
  906. }
  907. else {
  908. /* check for zero */
  909. if (Dbl_iszero_mantissa(opnd1p1,opnd1p2)) {
  910. /*
  911.  * Perform the add opnd3 with zero here.
  912.  */
  913. if (Dbl_iszero_exponentmantissa(opnd3p1,opnd3p2)) {
  914. if (Is_rounding_mode(ROUNDMINUS)) {
  915. Dbl_or_signs(opnd3p1,resultp1);
  916. } else {
  917. Dbl_and_signs(opnd3p1,resultp1);
  918. }
  919. }
  920. /*
  921.  * Now let's check for trapped underflow case.
  922.  */
  923. else if (Dbl_iszero_exponent(opnd3p1) &&
  924.          Is_underflowtrap_enabled()) {
  925.                      /* need to normalize results mantissa */
  926.                      sign_save = Dbl_signextendedsign(opnd3p1);
  927. result_exponent = 0;
  928.                      Dbl_leftshiftby1(opnd3p1,opnd3p2);
  929.                      Dbl_normalize(opnd3p1,opnd3p2,result_exponent);
  930.                      Dbl_set_sign(opnd3p1,/*using*/sign_save);
  931.                      Dbl_setwrapped_exponent(opnd3p1,result_exponent,
  932. unfl);
  933.                      Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  934.                      /* inexact = FALSE */
  935.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  936. }
  937. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  938. return(NOEXCEPTION);
  939. }
  940. /* is denormalized, adjust exponent */
  941. Dbl_clear_signexponent(opnd1p1);
  942. Dbl_leftshiftby1(opnd1p1,opnd1p2);
  943. Dbl_normalize(opnd1p1,opnd1p2,mpy_exponent);
  944. }
  945. /* opnd2 needs to have hidden bit set with msb in hidden bit */
  946. if (Dbl_isnotzero_exponent(opnd2p1)) {
  947. Dbl_clear_signexponent_set_hidden(opnd2p1);
  948. }
  949. else {
  950. /* check for zero */
  951. if (Dbl_iszero_mantissa(opnd2p1,opnd2p2)) {
  952. /*
  953.  * Perform the add opnd3 with zero here.
  954.  */
  955. if (Dbl_iszero_exponentmantissa(opnd3p1,opnd3p2)) {
  956. if (Is_rounding_mode(ROUNDMINUS)) {
  957. Dbl_or_signs(opnd3p1,resultp1);
  958. } else {
  959. Dbl_and_signs(opnd3p1,resultp1);
  960. }
  961. }
  962. /*
  963.  * Now let's check for trapped underflow case.
  964.  */
  965. else if (Dbl_iszero_exponent(opnd3p1) &&
  966.     Is_underflowtrap_enabled()) {
  967.                      /* need to normalize results mantissa */
  968.                      sign_save = Dbl_signextendedsign(opnd3p1);
  969. result_exponent = 0;
  970.                      Dbl_leftshiftby1(opnd3p1,opnd3p2);
  971.                      Dbl_normalize(opnd3p1,opnd3p2,result_exponent);
  972.                      Dbl_set_sign(opnd3p1,/*using*/sign_save);
  973.                      Dbl_setwrapped_exponent(opnd3p1,result_exponent,
  974. unfl);
  975.                      Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  976.                      /* inexact = FALSE */
  977.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  978. }
  979. Dbl_copytoptr(opnd3p1,opnd3p2,dstptr);
  980. return(NOEXCEPTION);
  981. }
  982. /* is denormalized; want to normalize */
  983. Dbl_clear_signexponent(opnd2p1);
  984. Dbl_leftshiftby1(opnd2p1,opnd2p2);
  985. Dbl_normalize(opnd2p1,opnd2p2,mpy_exponent);
  986. }
  987. /* Multiply the first two source mantissas together */
  988. /* 
  989.  * The intermediate result will be kept in tmpres,
  990.  * which needs enough room for 106 bits of mantissa,
  991.  * so lets call it a Double extended.
  992.  */
  993. Dblext_setzero(tmpresp1,tmpresp2,tmpresp3,tmpresp4);
  994. /* 
  995.  * Four bits at a time are inspected in each loop, and a 
  996.  * simple shift and add multiply algorithm is used. 
  997.  */ 
  998. for (count = DBL_P-1; count >= 0; count -= 4) {
  999. Dblext_rightshiftby4(tmpresp1,tmpresp2,tmpresp3,tmpresp4);
  1000. if (Dbit28p2(opnd1p2)) {
  1001.   /* Fourword_add should be an ADD followed by 3 ADDC's */
  1002. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4, 
  1003.  opnd2p1<<3 | opnd2p2>>29, opnd2p2<<3, 0, 0);
  1004. }
  1005. if (Dbit29p2(opnd1p2)) {
  1006. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4,
  1007.  opnd2p1<<2 | opnd2p2>>30, opnd2p2<<2, 0, 0);
  1008. }
  1009. if (Dbit30p2(opnd1p2)) {
  1010. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4,
  1011.  opnd2p1<<1 | opnd2p2>>31, opnd2p2<<1, 0, 0);
  1012. }
  1013. if (Dbit31p2(opnd1p2)) {
  1014. Fourword_add(tmpresp1, tmpresp2, tmpresp3, tmpresp4,
  1015.  opnd2p1, opnd2p2, 0, 0);
  1016. }
  1017. Dbl_rightshiftby4(opnd1p1,opnd1p2);
  1018. }
  1019. if (Is_dexthiddenoverflow(tmpresp1)) {
  1020. /* result mantissa >= 2 (mantissa overflow) */
  1021. mpy_exponent++;
  1022. Dblext_rightshiftby1(tmpresp1,tmpresp2,tmpresp3,tmpresp4);
  1023. }
  1024. /*
  1025.  * Restore the sign of the mpy result which was saved in resultp1.
  1026.  * The exponent will continue to be kept in mpy_exponent.
  1027.  */
  1028. Dblext_set_sign(tmpresp1,Dbl_sign(resultp1));
  1029. /* 
  1030.  * No rounding is required, since the result of the multiply
  1031.  * is exact in the extended format.
  1032.  */
  1033. /*
  1034.  * Now we are ready to perform the add portion of the operation.
  1035.  *
  1036.  * The exponents need to be kept as integers for now, since the
  1037.  * multiply result might not fit into the exponent field.  We
  1038.  * can't overflow or underflow because of this yet, since the
  1039.  * add could bring the final result back into range.
  1040.  */
  1041. add_exponent = Dbl_exponent(opnd3p1);
  1042. /*
  1043.  * Check for denormalized or zero add operand.
  1044.  */
  1045. if (add_exponent == 0) {
  1046. /* check for zero */
  1047. if (Dbl_iszero_mantissa(opnd3p1,opnd3p2)) {
  1048. /* right is zero */
  1049. /* Left can't be zero and must be result.
  1050.  *
  1051.  * The final result is now in tmpres and mpy_exponent,
  1052.  * and needs to be rounded and squeezed back into
  1053.  * double precision format from double extended.
  1054.  */
  1055. result_exponent = mpy_exponent;
  1056. Dblext_copy(tmpresp1,tmpresp2,tmpresp3,tmpresp4,
  1057. resultp1,resultp2,resultp3,resultp4);
  1058. sign_save = Dbl_signextendedsign(resultp1);/*save sign*/
  1059. goto round;
  1060. }
  1061. /* 
  1062.  * Neither are zeroes.  
  1063.  * Adjust exponent and normalize add operand.
  1064.  */
  1065. sign_save = Dbl_signextendedsign(opnd3p1); /* save sign */
  1066. Dbl_clear_signexponent(opnd3p1);
  1067. Dbl_leftshiftby1(opnd3p1,opnd3p2);
  1068. Dbl_normalize(opnd3p1,opnd3p2,add_exponent);
  1069. Dbl_set_sign(opnd3p1,sign_save); /* restore sign */
  1070. } else {
  1071. Dbl_clear_exponent_set_hidden(opnd3p1);
  1072. }
  1073. /*
  1074.  * Copy opnd3 to the double extended variable called right.
  1075.  */
  1076. Dbl_copyto_dblext(opnd3p1,opnd3p2,rightp1,rightp2,rightp3,rightp4);
  1077. /*
  1078.  * A zero "save" helps discover equal operands (for later),
  1079.  * and is used in swapping operands (if needed).
  1080.  */
  1081. Dblext_xortointp1(tmpresp1,rightp1,/*to*/save);
  1082. /*
  1083.  * Compare magnitude of operands.
  1084.  */
  1085. Dblext_copytoint_exponentmantissap1(tmpresp1,signlessleft1);
  1086. Dblext_copytoint_exponentmantissap1(rightp1,signlessright1);
  1087. if (mpy_exponent < add_exponent || mpy_exponent == add_exponent &&
  1088.     Dblext_ismagnitudeless(tmpresp2,rightp2,signlessleft1,signlessright1)){
  1089. /*
  1090.  * Set the left operand to the larger one by XOR swap.
  1091.  * First finish the first word "save".
  1092.  */
  1093. Dblext_xorfromintp1(save,rightp1,/*to*/rightp1);
  1094. Dblext_xorfromintp1(save,tmpresp1,/*to*/tmpresp1);
  1095. Dblext_swap_lower(tmpresp2,tmpresp3,tmpresp4,
  1096. rightp2,rightp3,rightp4);
  1097. /* also setup exponents used in rest of routine */
  1098. diff_exponent = add_exponent - mpy_exponent;
  1099. result_exponent = add_exponent;
  1100. } else {
  1101. /* also setup exponents used in rest of routine */
  1102. diff_exponent = mpy_exponent - add_exponent;
  1103. result_exponent = mpy_exponent;
  1104. }
  1105. /* Invariant: left is not smaller than right. */
  1106. /*
  1107.  * Special case alignment of operands that would force alignment
  1108.  * beyond the extent of the extension.  A further optimization
  1109.  * could special case this but only reduces the path length for
  1110.  * this infrequent case.
  1111.  */
  1112. if (diff_exponent > DBLEXT_THRESHOLD) {
  1113. diff_exponent = DBLEXT_THRESHOLD;
  1114. }
  1115. /* Align right operand by shifting it to the right */
  1116. Dblext_clear_sign(rightp1);
  1117. Dblext_right_align(rightp1,rightp2,rightp3,rightp4,
  1118. /*shifted by*/diff_exponent);
  1119. /* Treat sum and difference of the operands separately. */
  1120. if ((int)save < 0) {
  1121. /*
  1122.  * Difference of the two operands.  Overflow can occur if the
  1123.  * multiply overflowed.  A borrow can occur out of the hidden
  1124.  * bit and force a post normalization phase.
  1125.  */
  1126. Dblext_subtract(tmpresp1,tmpresp2,tmpresp3,tmpresp4,
  1127. rightp1,rightp2,rightp3,rightp4,
  1128. resultp1,resultp2,resultp3,resultp4);
  1129. sign_save = Dbl_signextendedsign(resultp1);
  1130. if (Dbl_iszero_hidden(resultp1)) {
  1131. /* Handle normalization */
  1132. /* A straight foward algorithm would now shift the
  1133.  * result and extension left until the hidden bit
  1134.  * becomes one.  Not all of the extension bits need
  1135.  * participate in the shift.  Only the two most 
  1136.  * significant bits (round and guard) are needed.
  1137.  * If only a single shift is needed then the guard
  1138.  * bit becomes a significant low order bit and the
  1139.  * extension must participate in the rounding.
  1140.  * If more than a single shift is needed, then all
  1141.  * bits to the right of the guard bit are zeros, 
  1142.  * and the guard bit may or may not be zero. */
  1143. Dblext_leftshiftby1(resultp1,resultp2,resultp3,
  1144. resultp4);
  1145. /* Need to check for a zero result.  The sign and
  1146.  * exponent fields have already been zeroed.  The more
  1147.  * efficient test of the full object can be used.
  1148.  */
  1149.  if (Dblext_iszero(resultp1,resultp2,resultp3,resultp4)) {
  1150. /* Must have been "x-x" or "x+(-x)". */
  1151. if (Is_rounding_mode(ROUNDMINUS))
  1152. Dbl_setone_sign(resultp1);
  1153. Dbl_copytoptr(resultp1,resultp2,dstptr);
  1154. return(NOEXCEPTION);
  1155. }
  1156. result_exponent--;
  1157. /* Look to see if normalization is finished. */
  1158. if (Dbl_isone_hidden(resultp1)) {
  1159. /* No further normalization is needed */
  1160. goto round;
  1161. }
  1162. /* Discover first one bit to determine shift amount.
  1163.  * Use a modified binary search.  We have already
  1164.  * shifted the result one position right and still
  1165.  * not found a one so the remainder of the extension
  1166.  * must be zero and simplifies rounding. */
  1167. /* Scan bytes */
  1168. while (Dbl_iszero_hiddenhigh7mantissa(resultp1)) {
  1169. Dblext_leftshiftby8(resultp1,resultp2,resultp3,resultp4);
  1170. result_exponent -= 8;
  1171. }
  1172. /* Now narrow it down to the nibble */
  1173. if (Dbl_iszero_hiddenhigh3mantissa(resultp1)) {
  1174. /* The lower nibble contains the
  1175.  * normalizing one */
  1176. Dblext_leftshiftby4(resultp1,resultp2,resultp3,resultp4);
  1177. result_exponent -= 4;
  1178. }
  1179. /* Select case where first bit is set (already
  1180.  * normalized) otherwise select the proper shift. */
  1181. jumpsize = Dbl_hiddenhigh3mantissa(resultp1);
  1182. if (jumpsize <= 7) switch(jumpsize) {
  1183. case 1:
  1184. Dblext_leftshiftby3(resultp1,resultp2,resultp3,
  1185. resultp4);
  1186. result_exponent -= 3;
  1187. break;
  1188. case 2:
  1189. case 3:
  1190. Dblext_leftshiftby2(resultp1,resultp2,resultp3,
  1191. resultp4);
  1192. result_exponent -= 2;
  1193. break;
  1194. case 4:
  1195. case 5:
  1196. case 6:
  1197. case 7:
  1198. Dblext_leftshiftby1(resultp1,resultp2,resultp3,
  1199. resultp4);
  1200. result_exponent -= 1;
  1201. break;
  1202. }
  1203. } /* end if (hidden...)... */
  1204. /* Fall through and round */
  1205. } /* end if (save < 0)... */
  1206. else {
  1207. /* Add magnitudes */
  1208. Dblext_addition(tmpresp1,tmpresp2,tmpresp3,tmpresp4,
  1209. rightp1,rightp2,rightp3,rightp4,
  1210. /*to*/resultp1,resultp2,resultp3,resultp4);
  1211. sign_save = Dbl_signextendedsign(resultp1);
  1212. if (Dbl_isone_hiddenoverflow(resultp1)) {
  1213.      /* Prenormalization required. */
  1214.      Dblext_arithrightshiftby1(resultp1,resultp2,resultp3,
  1215. resultp4);
  1216.      result_exponent++;
  1217. } /* end if hiddenoverflow... */
  1218. } /* end else ...add magnitudes... */
  1219. /* Round the result.  If the extension and lower two words are
  1220.  * all zeros, then the result is exact.  Otherwise round in the
  1221.  * correct direction.  Underflow is possible. If a postnormalization
  1222.  * is necessary, then the mantissa is all zeros so no shift is needed.
  1223.  */
  1224.   round:
  1225. if (result_exponent <= 0 && !Is_underflowtrap_enabled()) {
  1226. Dblext_denormalize(resultp1,resultp2,resultp3,resultp4,
  1227. result_exponent,is_tiny);
  1228. }
  1229. Dbl_set_sign(resultp1,/*using*/sign_save);
  1230. if (Dblext_isnotzero_mantissap3(resultp3) || 
  1231.     Dblext_isnotzero_mantissap4(resultp4)) {
  1232. inexact = TRUE;
  1233. switch(Rounding_mode()) {
  1234. case ROUNDNEAREST: /* The default. */
  1235. if (Dblext_isone_highp3(resultp3)) {
  1236. /* at least 1/2 ulp */
  1237. if (Dblext_isnotzero_low31p3(resultp3) ||
  1238.     Dblext_isnotzero_mantissap4(resultp4) ||
  1239.     Dblext_isone_lowp2(resultp2)) {
  1240. /* either exactly half way and odd or
  1241.  * more than 1/2ulp */
  1242. Dbl_increment(resultp1,resultp2);
  1243. }
  1244. }
  1245.      break;
  1246. case ROUNDPLUS:
  1247.      if (Dbl_iszero_sign(resultp1)) {
  1248. /* Round up positive results */
  1249. Dbl_increment(resultp1,resultp2);
  1250. }
  1251. break;
  1252.     
  1253. case ROUNDMINUS:
  1254.      if (Dbl_isone_sign(resultp1)) {
  1255. /* Round down negative results */
  1256. Dbl_increment(resultp1,resultp2);
  1257. }
  1258.     
  1259. case ROUNDZERO:;
  1260. /* truncate is simple */
  1261. } /* end switch... */
  1262. if (Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
  1263. }
  1264. if (result_exponent >= DBL_INFINITY_EXPONENT) {
  1265. /* Overflow */
  1266. if (Is_overflowtrap_enabled()) {
  1267.                         /*
  1268.                          * Adjust bias of result
  1269.                          */
  1270.                         Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
  1271.                         Dbl_copytoptr(resultp1,resultp2,dstptr);
  1272.                         if (inexact)
  1273.                             if (Is_inexacttrap_enabled())
  1274.                                 return (OPC_2E_OVERFLOWEXCEPTION |
  1275. OPC_2E_INEXACTEXCEPTION);
  1276.                             else Set_inexactflag();
  1277.                         return (OPC_2E_OVERFLOWEXCEPTION);
  1278. }
  1279. inexact = TRUE;
  1280. Set_overflowflag();
  1281. Dbl_setoverflow(resultp1,resultp2);
  1282. } else if (result_exponent <= 0) { /* underflow case */
  1283. if (Is_underflowtrap_enabled()) {
  1284.                         /*
  1285.                          * Adjust bias of result
  1286.                          */
  1287.                  Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
  1288. Dbl_copytoptr(resultp1,resultp2,dstptr);
  1289.                         if (inexact)
  1290.                             if (Is_inexacttrap_enabled())
  1291.                                 return (OPC_2E_UNDERFLOWEXCEPTION |
  1292. OPC_2E_INEXACTEXCEPTION);
  1293.                             else Set_inexactflag();
  1294.      return(OPC_2E_UNDERFLOWEXCEPTION);
  1295. }
  1296. else if (inexact && is_tiny) Set_underflowflag();
  1297. }
  1298. else Dbl_set_exponent(resultp1,result_exponent);
  1299. Dbl_copytoptr(resultp1,resultp2,dstptr);
  1300. if (inexact) 
  1301. if (Is_inexacttrap_enabled()) return(OPC_2E_INEXACTEXCEPTION);
  1302. else Set_inexactflag();
  1303.      return(NOEXCEPTION);
  1304. }
  1305. /*
  1306.  *  Single Floating-point Multiply Fused Add
  1307.  */
  1308. sgl_fmpyfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  1309. sgl_floating_point *src1ptr, *src2ptr, *src3ptr, *dstptr;
  1310. unsigned int *status;
  1311. {
  1312. unsigned int opnd1, opnd2, opnd3;
  1313. register unsigned int tmpresp1, tmpresp2;
  1314. unsigned int rightp1, rightp2;
  1315. unsigned int resultp1, resultp2 = 0;
  1316. register int mpy_exponent, add_exponent, count;
  1317. boolean inexact = FALSE, is_tiny = FALSE;
  1318. unsigned int signlessleft1, signlessright1, save;
  1319. register int result_exponent, diff_exponent;
  1320. int sign_save, jumpsize;
  1321. Sgl_copyfromptr(src1ptr,opnd1);
  1322. Sgl_copyfromptr(src2ptr,opnd2);
  1323. Sgl_copyfromptr(src3ptr,opnd3);
  1324. /* 
  1325.  * set sign bit of result of multiply
  1326.  */
  1327. if (Sgl_sign(opnd1) ^ Sgl_sign(opnd2)) 
  1328. Sgl_setnegativezero(resultp1); 
  1329. else Sgl_setzero(resultp1);
  1330. /*
  1331.  * Generate multiply exponent 
  1332.  */
  1333. mpy_exponent = Sgl_exponent(opnd1) + Sgl_exponent(opnd2) - SGL_BIAS;
  1334. /*
  1335.  * check first operand for NaN's or infinity
  1336.  */
  1337. if (Sgl_isinfinity_exponent(opnd1)) {
  1338. if (Sgl_iszero_mantissa(opnd1)) {
  1339. if (Sgl_isnotnan(opnd2) && Sgl_isnotnan(opnd3)) {
  1340. if (Sgl_iszero_exponentmantissa(opnd2)) {
  1341. /* 
  1342.  * invalid since operands are infinity 
  1343.  * and zero 
  1344.  */
  1345. if (Is_invalidtrap_enabled())
  1346. return(OPC_2E_INVALIDEXCEPTION);
  1347. Set_invalidflag();
  1348. Sgl_makequietnan(resultp1);
  1349. Sgl_copytoptr(resultp1,dstptr);
  1350. return(NOEXCEPTION);
  1351. }
  1352. /*
  1353.  * Check third operand for infinity with a
  1354.  *  sign opposite of the multiply result
  1355.  */
  1356. if (Sgl_isinfinity(opnd3) &&
  1357.     (Sgl_sign(resultp1) ^ Sgl_sign(opnd3))) {
  1358. /* 
  1359.  * invalid since attempting a magnitude
  1360.  * subtraction of infinities
  1361.  */
  1362. if (Is_invalidtrap_enabled())
  1363. return(OPC_2E_INVALIDEXCEPTION);
  1364. Set_invalidflag();
  1365. Sgl_makequietnan(resultp1);
  1366. Sgl_copytoptr(resultp1,dstptr);
  1367. return(NOEXCEPTION);
  1368. }
  1369. /*
  1370.    * return infinity
  1371.    */
  1372. Sgl_setinfinity_exponentmantissa(resultp1);
  1373. Sgl_copytoptr(resultp1,dstptr);
  1374. return(NOEXCEPTION);
  1375. }
  1376. }
  1377. else {
  1378. /*
  1379.    * is NaN; signaling or quiet?
  1380.    */
  1381. if (Sgl_isone_signaling(opnd1)) {
  1382. /* trap if INVALIDTRAP enabled */
  1383. if (Is_invalidtrap_enabled()) 
  1384.      return(OPC_2E_INVALIDEXCEPTION);
  1385. /* make NaN quiet */
  1386. Set_invalidflag();
  1387. Sgl_set_quiet(opnd1);
  1388. }
  1389. /* 
  1390.  * is second operand a signaling NaN? 
  1391.  */
  1392. else if (Sgl_is_signalingnan(opnd2)) {
  1393. /* trap if INVALIDTRAP enabled */
  1394. if (Is_invalidtrap_enabled())
  1395.      return(OPC_2E_INVALIDEXCEPTION);
  1396. /* make NaN quiet */
  1397. Set_invalidflag();
  1398. Sgl_set_quiet(opnd2);
  1399. Sgl_copytoptr(opnd2,dstptr);
  1400. return(NOEXCEPTION);
  1401. }
  1402. /* 
  1403.  * is third operand a signaling NaN? 
  1404.  */
  1405. else if (Sgl_is_signalingnan(opnd3)) {
  1406. /* trap if INVALIDTRAP enabled */
  1407. if (Is_invalidtrap_enabled())
  1408.      return(OPC_2E_INVALIDEXCEPTION);
  1409. /* make NaN quiet */
  1410. Set_invalidflag();
  1411. Sgl_set_quiet(opnd3);
  1412. Sgl_copytoptr(opnd3,dstptr);
  1413. return(NOEXCEPTION);
  1414. }
  1415. /*
  1416.    * return quiet NaN
  1417.    */
  1418. Sgl_copytoptr(opnd1,dstptr);
  1419. return(NOEXCEPTION);
  1420. }
  1421. }
  1422. /*
  1423.  * check second operand for NaN's or infinity
  1424.  */
  1425. if (Sgl_isinfinity_exponent(opnd2)) {
  1426. if (Sgl_iszero_mantissa(opnd2)) {
  1427. if (Sgl_isnotnan(opnd3)) {
  1428. if (Sgl_iszero_exponentmantissa(opnd1)) {
  1429. /* 
  1430.  * invalid since multiply operands are
  1431.  * zero & infinity
  1432.  */
  1433. if (Is_invalidtrap_enabled())
  1434. return(OPC_2E_INVALIDEXCEPTION);
  1435. Set_invalidflag();
  1436. Sgl_makequietnan(opnd2);
  1437. Sgl_copytoptr(opnd2,dstptr);
  1438. return(NOEXCEPTION);
  1439. }
  1440. /*
  1441.  * Check third operand for infinity with a
  1442.  *  sign opposite of the multiply result
  1443.  */
  1444. if (Sgl_isinfinity(opnd3) &&
  1445.     (Sgl_sign(resultp1) ^ Sgl_sign(opnd3))) {
  1446. /* 
  1447.  * invalid since attempting a magnitude
  1448.  * subtraction of infinities
  1449.  */
  1450. if (Is_invalidtrap_enabled())
  1451.         return(OPC_2E_INVALIDEXCEPTION);
  1452.         Set_invalidflag();
  1453.         Sgl_makequietnan(resultp1);
  1454. Sgl_copytoptr(resultp1,dstptr);
  1455. return(NOEXCEPTION);
  1456. }
  1457. /*
  1458.  * return infinity
  1459.  */
  1460. Sgl_setinfinity_exponentmantissa(resultp1);
  1461. Sgl_copytoptr(resultp1,dstptr);
  1462. return(NOEXCEPTION);
  1463. }
  1464. }
  1465. else {
  1466. /*
  1467.  * is NaN; signaling or quiet?
  1468.  */
  1469. if (Sgl_isone_signaling(opnd2)) {
  1470. /* trap if INVALIDTRAP enabled */
  1471. if (Is_invalidtrap_enabled())
  1472. return(OPC_2E_INVALIDEXCEPTION);
  1473. /* make NaN quiet */
  1474. Set_invalidflag();
  1475. Sgl_set_quiet(opnd2);
  1476. }
  1477. /* 
  1478.  * is third operand a signaling NaN? 
  1479.  */
  1480. else if (Sgl_is_signalingnan(opnd3)) {
  1481.         /* trap if INVALIDTRAP enabled */
  1482.         if (Is_invalidtrap_enabled())
  1483.     return(OPC_2E_INVALIDEXCEPTION);
  1484.         /* make NaN quiet */
  1485.         Set_invalidflag();
  1486.         Sgl_set_quiet(opnd3);
  1487. Sgl_copytoptr(opnd3,dstptr);
  1488.         return(NOEXCEPTION);
  1489. }
  1490. /*
  1491.  * return quiet NaN
  1492.  */
  1493. Sgl_copytoptr(opnd2,dstptr);
  1494. return(NOEXCEPTION);
  1495. }
  1496. }
  1497. /*
  1498.  * check third operand for NaN's or infinity
  1499.  */
  1500. if (Sgl_isinfinity_exponent(opnd3)) {
  1501. if (Sgl_iszero_mantissa(opnd3)) {
  1502. /* return infinity */
  1503. Sgl_copytoptr(opnd3,dstptr);
  1504. return(NOEXCEPTION);
  1505. } else {
  1506. /*
  1507.  * is NaN; signaling or quiet?
  1508.  */
  1509. if (Sgl_isone_signaling(opnd3)) {
  1510. /* trap if INVALIDTRAP enabled */
  1511. if (Is_invalidtrap_enabled())
  1512. return(OPC_2E_INVALIDEXCEPTION);
  1513. /* make NaN quiet */
  1514. Set_invalidflag();
  1515. Sgl_set_quiet(opnd3);
  1516. }
  1517. /*
  1518.  * return quiet NaN
  1519.    */
  1520. Sgl_copytoptr(opnd3,dstptr);
  1521. return(NOEXCEPTION);
  1522. }
  1523.      }
  1524. /*
  1525.  * Generate multiply mantissa
  1526.  */
  1527. if (Sgl_isnotzero_exponent(opnd1)) {
  1528. /* set hidden bit */
  1529. Sgl_clear_signexponent_set_hidden(opnd1);
  1530. }
  1531. else {
  1532. /* check for zero */
  1533. if (Sgl_iszero_mantissa(opnd1)) {
  1534. /*
  1535.  * Perform the add opnd3 with zero here.
  1536.  */
  1537. if (Sgl_iszero_exponentmantissa(opnd3)) {
  1538. if (Is_rounding_mode(ROUNDMINUS)) {
  1539. Sgl_or_signs(opnd3,resultp1);
  1540. } else {
  1541. Sgl_and_signs(opnd3,resultp1);
  1542. }
  1543. }
  1544. /*
  1545.  * Now let's check for trapped underflow case.
  1546.  */
  1547. else if (Sgl_iszero_exponent(opnd3) &&
  1548.          Is_underflowtrap_enabled()) {
  1549.                      /* need to normalize results mantissa */
  1550.                      sign_save = Sgl_signextendedsign(opnd3);
  1551. result_exponent = 0;
  1552.                      Sgl_leftshiftby1(opnd3);
  1553.                      Sgl_normalize(opnd3,result_exponent);
  1554.                      Sgl_set_sign(opnd3,/*using*/sign_save);
  1555.                      Sgl_setwrapped_exponent(opnd3,result_exponent,
  1556. unfl);
  1557.                      Sgl_copytoptr(opnd3,dstptr);
  1558.                      /* inexact = FALSE */
  1559.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  1560. }
  1561. Sgl_copytoptr(opnd3,dstptr);
  1562. return(NOEXCEPTION);
  1563. }
  1564. /* is denormalized, adjust exponent */
  1565. Sgl_clear_signexponent(opnd1);
  1566. Sgl_leftshiftby1(opnd1);
  1567. Sgl_normalize(opnd1,mpy_exponent);
  1568. }
  1569. /* opnd2 needs to have hidden bit set with msb in hidden bit */
  1570. if (Sgl_isnotzero_exponent(opnd2)) {
  1571. Sgl_clear_signexponent_set_hidden(opnd2);
  1572. }
  1573. else {
  1574. /* check for zero */
  1575. if (Sgl_iszero_mantissa(opnd2)) {
  1576. /*
  1577.  * Perform the add opnd3 with zero here.
  1578.  */
  1579. if (Sgl_iszero_exponentmantissa(opnd3)) {
  1580. if (Is_rounding_mode(ROUNDMINUS)) {
  1581. Sgl_or_signs(opnd3,resultp1);
  1582. } else {
  1583. Sgl_and_signs(opnd3,resultp1);
  1584. }
  1585. }
  1586. /*
  1587.  * Now let's check for trapped underflow case.
  1588.  */
  1589. else if (Sgl_iszero_exponent(opnd3) &&
  1590.     Is_underflowtrap_enabled()) {
  1591.                      /* need to normalize results mantissa */
  1592.                      sign_save = Sgl_signextendedsign(opnd3);
  1593. result_exponent = 0;
  1594.                      Sgl_leftshiftby1(opnd3);
  1595.                      Sgl_normalize(opnd3,result_exponent);
  1596.                      Sgl_set_sign(opnd3,/*using*/sign_save);
  1597.                      Sgl_setwrapped_exponent(opnd3,result_exponent,
  1598. unfl);
  1599.                      Sgl_copytoptr(opnd3,dstptr);
  1600.                      /* inexact = FALSE */
  1601.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  1602. }
  1603. Sgl_copytoptr(opnd3,dstptr);
  1604. return(NOEXCEPTION);
  1605. }
  1606. /* is denormalized; want to normalize */
  1607. Sgl_clear_signexponent(opnd2);
  1608. Sgl_leftshiftby1(opnd2);
  1609. Sgl_normalize(opnd2,mpy_exponent);
  1610. }
  1611. /* Multiply the first two source mantissas together */
  1612. /* 
  1613.  * The intermediate result will be kept in tmpres,
  1614.  * which needs enough room for 106 bits of mantissa,
  1615.  * so lets call it a Double extended.
  1616.  */
  1617. Sglext_setzero(tmpresp1,tmpresp2);
  1618. /* 
  1619.  * Four bits at a time are inspected in each loop, and a 
  1620.  * simple shift and add multiply algorithm is used. 
  1621.  */ 
  1622. for (count = SGL_P-1; count >= 0; count -= 4) {
  1623. Sglext_rightshiftby4(tmpresp1,tmpresp2);
  1624. if (Sbit28(opnd1)) {
  1625.   /* Twoword_add should be an ADD followed by 2 ADDC's */
  1626. Twoword_add(tmpresp1, tmpresp2, opnd2<<3, 0);
  1627. }
  1628. if (Sbit29(opnd1)) {
  1629. Twoword_add(tmpresp1, tmpresp2, opnd2<<2, 0);
  1630. }
  1631. if (Sbit30(opnd1)) {
  1632. Twoword_add(tmpresp1, tmpresp2, opnd2<<1, 0);
  1633. }
  1634. if (Sbit31(opnd1)) {
  1635. Twoword_add(tmpresp1, tmpresp2, opnd2, 0);
  1636. }
  1637. Sgl_rightshiftby4(opnd1);
  1638. }
  1639. if (Is_sexthiddenoverflow(tmpresp1)) {
  1640. /* result mantissa >= 2 (mantissa overflow) */
  1641. mpy_exponent++;
  1642. Sglext_rightshiftby4(tmpresp1,tmpresp2);
  1643. } else {
  1644. Sglext_rightshiftby3(tmpresp1,tmpresp2);
  1645. }
  1646. /*
  1647.  * Restore the sign of the mpy result which was saved in resultp1.
  1648.  * The exponent will continue to be kept in mpy_exponent.
  1649.  */
  1650. Sglext_set_sign(tmpresp1,Sgl_sign(resultp1));
  1651. /* 
  1652.  * No rounding is required, since the result of the multiply
  1653.  * is exact in the extended format.
  1654.  */
  1655. /*
  1656.  * Now we are ready to perform the add portion of the operation.
  1657.  *
  1658.  * The exponents need to be kept as integers for now, since the
  1659.  * multiply result might not fit into the exponent field.  We
  1660.  * can't overflow or underflow because of this yet, since the
  1661.  * add could bring the final result back into range.
  1662.  */
  1663. add_exponent = Sgl_exponent(opnd3);
  1664. /*
  1665.  * Check for denormalized or zero add operand.
  1666.  */
  1667. if (add_exponent == 0) {
  1668. /* check for zero */
  1669. if (Sgl_iszero_mantissa(opnd3)) {
  1670. /* right is zero */
  1671. /* Left can't be zero and must be result.
  1672.  *
  1673.  * The final result is now in tmpres and mpy_exponent,
  1674.  * and needs to be rounded and squeezed back into
  1675.  * double precision format from double extended.
  1676.  */
  1677. result_exponent = mpy_exponent;
  1678. Sglext_copy(tmpresp1,tmpresp2,resultp1,resultp2);
  1679. sign_save = Sgl_signextendedsign(resultp1);/*save sign*/
  1680. goto round;
  1681. }
  1682. /* 
  1683.  * Neither are zeroes.  
  1684.  * Adjust exponent and normalize add operand.
  1685.  */
  1686. sign_save = Sgl_signextendedsign(opnd3); /* save sign */
  1687. Sgl_clear_signexponent(opnd3);
  1688. Sgl_leftshiftby1(opnd3);
  1689. Sgl_normalize(opnd3,add_exponent);
  1690. Sgl_set_sign(opnd3,sign_save); /* restore sign */
  1691. } else {
  1692. Sgl_clear_exponent_set_hidden(opnd3);
  1693. }
  1694. /*
  1695.  * Copy opnd3 to the double extended variable called right.
  1696.  */
  1697. Sgl_copyto_sglext(opnd3,rightp1,rightp2);
  1698. /*
  1699.  * A zero "save" helps discover equal operands (for later),
  1700.  * and is used in swapping operands (if needed).
  1701.  */
  1702. Sglext_xortointp1(tmpresp1,rightp1,/*to*/save);
  1703. /*
  1704.  * Compare magnitude of operands.
  1705.  */
  1706. Sglext_copytoint_exponentmantissa(tmpresp1,signlessleft1);
  1707. Sglext_copytoint_exponentmantissa(rightp1,signlessright1);
  1708. if (mpy_exponent < add_exponent || mpy_exponent == add_exponent &&
  1709.     Sglext_ismagnitudeless(signlessleft1,signlessright1)) {
  1710. /*
  1711.  * Set the left operand to the larger one by XOR swap.
  1712.  * First finish the first word "save".
  1713.  */
  1714. Sglext_xorfromintp1(save,rightp1,/*to*/rightp1);
  1715. Sglext_xorfromintp1(save,tmpresp1,/*to*/tmpresp1);
  1716. Sglext_swap_lower(tmpresp2,rightp2);
  1717. /* also setup exponents used in rest of routine */
  1718. diff_exponent = add_exponent - mpy_exponent;
  1719. result_exponent = add_exponent;
  1720. } else {
  1721. /* also setup exponents used in rest of routine */
  1722. diff_exponent = mpy_exponent - add_exponent;
  1723. result_exponent = mpy_exponent;
  1724. }
  1725. /* Invariant: left is not smaller than right. */
  1726. /*
  1727.  * Special case alignment of operands that would force alignment
  1728.  * beyond the extent of the extension.  A further optimization
  1729.  * could special case this but only reduces the path length for
  1730.  * this infrequent case.
  1731.  */
  1732. if (diff_exponent > SGLEXT_THRESHOLD) {
  1733. diff_exponent = SGLEXT_THRESHOLD;
  1734. }
  1735. /* Align right operand by shifting it to the right */
  1736. Sglext_clear_sign(rightp1);
  1737. Sglext_right_align(rightp1,rightp2,/*shifted by*/diff_exponent);
  1738. /* Treat sum and difference of the operands separately. */
  1739. if ((int)save < 0) {
  1740. /*
  1741.  * Difference of the two operands.  Overflow can occur if the
  1742.  * multiply overflowed.  A borrow can occur out of the hidden
  1743.  * bit and force a post normalization phase.
  1744.  */
  1745. Sglext_subtract(tmpresp1,tmpresp2, rightp1,rightp2,
  1746. resultp1,resultp2);
  1747. sign_save = Sgl_signextendedsign(resultp1);
  1748. if (Sgl_iszero_hidden(resultp1)) {
  1749. /* Handle normalization */
  1750. /* A straight foward algorithm would now shift the
  1751.  * result and extension left until the hidden bit
  1752.  * becomes one.  Not all of the extension bits need
  1753.  * participate in the shift.  Only the two most 
  1754.  * significant bits (round and guard) are needed.
  1755.  * If only a single shift is needed then the guard
  1756.  * bit becomes a significant low order bit and the
  1757.  * extension must participate in the rounding.
  1758.  * If more than a single shift is needed, then all
  1759.  * bits to the right of the guard bit are zeros, 
  1760.  * and the guard bit may or may not be zero. */
  1761. Sglext_leftshiftby1(resultp1,resultp2);
  1762. /* Need to check for a zero result.  The sign and
  1763.  * exponent fields have already been zeroed.  The more
  1764.  * efficient test of the full object can be used.
  1765.  */
  1766.  if (Sglext_iszero(resultp1,resultp2)) {
  1767. /* Must have been "x-x" or "x+(-x)". */
  1768. if (Is_rounding_mode(ROUNDMINUS))
  1769. Sgl_setone_sign(resultp1);
  1770. Sgl_copytoptr(resultp1,dstptr);
  1771. return(NOEXCEPTION);
  1772. }
  1773. result_exponent--;
  1774. /* Look to see if normalization is finished. */
  1775. if (Sgl_isone_hidden(resultp1)) {
  1776. /* No further normalization is needed */
  1777. goto round;
  1778. }
  1779. /* Discover first one bit to determine shift amount.
  1780.  * Use a modified binary search.  We have already
  1781.  * shifted the result one position right and still
  1782.  * not found a one so the remainder of the extension
  1783.  * must be zero and simplifies rounding. */
  1784. /* Scan bytes */
  1785. while (Sgl_iszero_hiddenhigh7mantissa(resultp1)) {
  1786. Sglext_leftshiftby8(resultp1,resultp2);
  1787. result_exponent -= 8;
  1788. }
  1789. /* Now narrow it down to the nibble */
  1790. if (Sgl_iszero_hiddenhigh3mantissa(resultp1)) {
  1791. /* The lower nibble contains the
  1792.  * normalizing one */
  1793. Sglext_leftshiftby4(resultp1,resultp2);
  1794. result_exponent -= 4;
  1795. }
  1796. /* Select case where first bit is set (already
  1797.  * normalized) otherwise select the proper shift. */
  1798. jumpsize = Sgl_hiddenhigh3mantissa(resultp1);
  1799. if (jumpsize <= 7) switch(jumpsize) {
  1800. case 1:
  1801. Sglext_leftshiftby3(resultp1,resultp2);
  1802. result_exponent -= 3;
  1803. break;
  1804. case 2:
  1805. case 3:
  1806. Sglext_leftshiftby2(resultp1,resultp2);
  1807. result_exponent -= 2;
  1808. break;
  1809. case 4:
  1810. case 5:
  1811. case 6:
  1812. case 7:
  1813. Sglext_leftshiftby1(resultp1,resultp2);
  1814. result_exponent -= 1;
  1815. break;
  1816. }
  1817. } /* end if (hidden...)... */
  1818. /* Fall through and round */
  1819. } /* end if (save < 0)... */
  1820. else {
  1821. /* Add magnitudes */
  1822. Sglext_addition(tmpresp1,tmpresp2,
  1823. rightp1,rightp2, /*to*/resultp1,resultp2);
  1824. sign_save = Sgl_signextendedsign(resultp1);
  1825. if (Sgl_isone_hiddenoverflow(resultp1)) {
  1826.      /* Prenormalization required. */
  1827.      Sglext_arithrightshiftby1(resultp1,resultp2);
  1828.      result_exponent++;
  1829. } /* end if hiddenoverflow... */
  1830. } /* end else ...add magnitudes... */
  1831. /* Round the result.  If the extension and lower two words are
  1832.  * all zeros, then the result is exact.  Otherwise round in the
  1833.  * correct direction.  Underflow is possible. If a postnormalization
  1834.  * is necessary, then the mantissa is all zeros so no shift is needed.
  1835.  */
  1836.   round:
  1837. if (result_exponent <= 0 && !Is_underflowtrap_enabled()) {
  1838. Sglext_denormalize(resultp1,resultp2,result_exponent,is_tiny);
  1839. }
  1840. Sgl_set_sign(resultp1,/*using*/sign_save);
  1841. if (Sglext_isnotzero_mantissap2(resultp2)) {
  1842. inexact = TRUE;
  1843. switch(Rounding_mode()) {
  1844. case ROUNDNEAREST: /* The default. */
  1845. if (Sglext_isone_highp2(resultp2)) {
  1846. /* at least 1/2 ulp */
  1847. if (Sglext_isnotzero_low31p2(resultp2) ||
  1848.     Sglext_isone_lowp1(resultp1)) {
  1849. /* either exactly half way and odd or
  1850.  * more than 1/2ulp */
  1851. Sgl_increment(resultp1);
  1852. }
  1853. }
  1854.      break;
  1855. case ROUNDPLUS:
  1856.      if (Sgl_iszero_sign(resultp1)) {
  1857. /* Round up positive results */
  1858. Sgl_increment(resultp1);
  1859. }
  1860. break;
  1861.     
  1862. case ROUNDMINUS:
  1863.      if (Sgl_isone_sign(resultp1)) {
  1864. /* Round down negative results */
  1865. Sgl_increment(resultp1);
  1866. }
  1867.     
  1868. case ROUNDZERO:;
  1869. /* truncate is simple */
  1870. } /* end switch... */
  1871. if (Sgl_isone_hiddenoverflow(resultp1)) result_exponent++;
  1872. }
  1873. if (result_exponent >= SGL_INFINITY_EXPONENT) {
  1874. /* Overflow */
  1875. if (Is_overflowtrap_enabled()) {
  1876.                         /*
  1877.                          * Adjust bias of result
  1878.                          */
  1879.                         Sgl_setwrapped_exponent(resultp1,result_exponent,ovfl);
  1880.                         Sgl_copytoptr(resultp1,dstptr);
  1881.                         if (inexact)
  1882.                             if (Is_inexacttrap_enabled())
  1883.                                 return (OPC_2E_OVERFLOWEXCEPTION |
  1884. OPC_2E_INEXACTEXCEPTION);
  1885.                             else Set_inexactflag();
  1886.                         return (OPC_2E_OVERFLOWEXCEPTION);
  1887. }
  1888. inexact = TRUE;
  1889. Set_overflowflag();
  1890. Sgl_setoverflow(resultp1);
  1891. } else if (result_exponent <= 0) { /* underflow case */
  1892. if (Is_underflowtrap_enabled()) {
  1893.                         /*
  1894.                          * Adjust bias of result
  1895.                          */
  1896.                  Sgl_setwrapped_exponent(resultp1,result_exponent,unfl);
  1897. Sgl_copytoptr(resultp1,dstptr);
  1898.                         if (inexact)
  1899.                             if (Is_inexacttrap_enabled())
  1900.                                 return (OPC_2E_UNDERFLOWEXCEPTION |
  1901. OPC_2E_INEXACTEXCEPTION);
  1902.                             else Set_inexactflag();
  1903.      return(OPC_2E_UNDERFLOWEXCEPTION);
  1904. }
  1905. else if (inexact && is_tiny) Set_underflowflag();
  1906. }
  1907. else Sgl_set_exponent(resultp1,result_exponent);
  1908. Sgl_copytoptr(resultp1,dstptr);
  1909. if (inexact) 
  1910. if (Is_inexacttrap_enabled()) return(OPC_2E_INEXACTEXCEPTION);
  1911. else Set_inexactflag();
  1912.      return(NOEXCEPTION);
  1913. }
  1914. /*
  1915.  *  Single Floating-point Multiply Negate Fused Add
  1916.  */
  1917. sgl_fmpynfadd(src1ptr,src2ptr,src3ptr,status,dstptr)
  1918. sgl_floating_point *src1ptr, *src2ptr, *src3ptr, *dstptr;
  1919. unsigned int *status;
  1920. {
  1921. unsigned int opnd1, opnd2, opnd3;
  1922. register unsigned int tmpresp1, tmpresp2;
  1923. unsigned int rightp1, rightp2;
  1924. unsigned int resultp1, resultp2 = 0;
  1925. register int mpy_exponent, add_exponent, count;
  1926. boolean inexact = FALSE, is_tiny = FALSE;
  1927. unsigned int signlessleft1, signlessright1, save;
  1928. register int result_exponent, diff_exponent;
  1929. int sign_save, jumpsize;
  1930. Sgl_copyfromptr(src1ptr,opnd1);
  1931. Sgl_copyfromptr(src2ptr,opnd2);
  1932. Sgl_copyfromptr(src3ptr,opnd3);
  1933. /* 
  1934.  * set sign bit of result of multiply
  1935.  */
  1936. if (Sgl_sign(opnd1) ^ Sgl_sign(opnd2)) 
  1937. Sgl_setzero(resultp1);
  1938. else 
  1939. Sgl_setnegativezero(resultp1); 
  1940. /*
  1941.  * Generate multiply exponent 
  1942.  */
  1943. mpy_exponent = Sgl_exponent(opnd1) + Sgl_exponent(opnd2) - SGL_BIAS;
  1944. /*
  1945.  * check first operand for NaN's or infinity
  1946.  */
  1947. if (Sgl_isinfinity_exponent(opnd1)) {
  1948. if (Sgl_iszero_mantissa(opnd1)) {
  1949. if (Sgl_isnotnan(opnd2) && Sgl_isnotnan(opnd3)) {
  1950. if (Sgl_iszero_exponentmantissa(opnd2)) {
  1951. /* 
  1952.  * invalid since operands are infinity 
  1953.  * and zero 
  1954.  */
  1955. if (Is_invalidtrap_enabled())
  1956. return(OPC_2E_INVALIDEXCEPTION);
  1957. Set_invalidflag();
  1958. Sgl_makequietnan(resultp1);
  1959. Sgl_copytoptr(resultp1,dstptr);
  1960. return(NOEXCEPTION);
  1961. }
  1962. /*
  1963.  * Check third operand for infinity with a
  1964.  *  sign opposite of the multiply result
  1965.  */
  1966. if (Sgl_isinfinity(opnd3) &&
  1967.     (Sgl_sign(resultp1) ^ Sgl_sign(opnd3))) {
  1968. /* 
  1969.  * invalid since attempting a magnitude
  1970.  * subtraction of infinities
  1971.  */
  1972. if (Is_invalidtrap_enabled())
  1973. return(OPC_2E_INVALIDEXCEPTION);
  1974. Set_invalidflag();
  1975. Sgl_makequietnan(resultp1);
  1976. Sgl_copytoptr(resultp1,dstptr);
  1977. return(NOEXCEPTION);
  1978. }
  1979. /*
  1980.    * return infinity
  1981.    */
  1982. Sgl_setinfinity_exponentmantissa(resultp1);
  1983. Sgl_copytoptr(resultp1,dstptr);
  1984. return(NOEXCEPTION);
  1985. }
  1986. }
  1987. else {
  1988. /*
  1989.    * is NaN; signaling or quiet?
  1990.    */
  1991. if (Sgl_isone_signaling(opnd1)) {
  1992. /* trap if INVALIDTRAP enabled */
  1993. if (Is_invalidtrap_enabled()) 
  1994.      return(OPC_2E_INVALIDEXCEPTION);
  1995. /* make NaN quiet */
  1996. Set_invalidflag();
  1997. Sgl_set_quiet(opnd1);
  1998. }
  1999. /* 
  2000.  * is second operand a signaling NaN? 
  2001.  */
  2002. else if (Sgl_is_signalingnan(opnd2)) {
  2003. /* trap if INVALIDTRAP enabled */
  2004. if (Is_invalidtrap_enabled())
  2005.      return(OPC_2E_INVALIDEXCEPTION);
  2006. /* make NaN quiet */
  2007. Set_invalidflag();
  2008. Sgl_set_quiet(opnd2);
  2009. Sgl_copytoptr(opnd2,dstptr);
  2010. return(NOEXCEPTION);
  2011. }
  2012. /* 
  2013.  * is third operand a signaling NaN? 
  2014.  */
  2015. else if (Sgl_is_signalingnan(opnd3)) {
  2016. /* trap if INVALIDTRAP enabled */
  2017. if (Is_invalidtrap_enabled())
  2018.      return(OPC_2E_INVALIDEXCEPTION);
  2019. /* make NaN quiet */
  2020. Set_invalidflag();
  2021. Sgl_set_quiet(opnd3);
  2022. Sgl_copytoptr(opnd3,dstptr);
  2023. return(NOEXCEPTION);
  2024. }
  2025. /*
  2026.    * return quiet NaN
  2027.    */
  2028. Sgl_copytoptr(opnd1,dstptr);
  2029. return(NOEXCEPTION);
  2030. }
  2031. }
  2032. /*
  2033.  * check second operand for NaN's or infinity
  2034.  */
  2035. if (Sgl_isinfinity_exponent(opnd2)) {
  2036. if (Sgl_iszero_mantissa(opnd2)) {
  2037. if (Sgl_isnotnan(opnd3)) {
  2038. if (Sgl_iszero_exponentmantissa(opnd1)) {
  2039. /* 
  2040.  * invalid since multiply operands are
  2041.  * zero & infinity
  2042.  */
  2043. if (Is_invalidtrap_enabled())
  2044. return(OPC_2E_INVALIDEXCEPTION);
  2045. Set_invalidflag();
  2046. Sgl_makequietnan(opnd2);
  2047. Sgl_copytoptr(opnd2,dstptr);
  2048. return(NOEXCEPTION);
  2049. }
  2050. /*
  2051.  * Check third operand for infinity with a
  2052.  *  sign opposite of the multiply result
  2053.  */
  2054. if (Sgl_isinfinity(opnd3) &&
  2055.     (Sgl_sign(resultp1) ^ Sgl_sign(opnd3))) {
  2056. /* 
  2057.  * invalid since attempting a magnitude
  2058.  * subtraction of infinities
  2059.  */
  2060. if (Is_invalidtrap_enabled())
  2061.         return(OPC_2E_INVALIDEXCEPTION);
  2062.         Set_invalidflag();
  2063.         Sgl_makequietnan(resultp1);
  2064. Sgl_copytoptr(resultp1,dstptr);
  2065. return(NOEXCEPTION);
  2066. }
  2067. /*
  2068.  * return infinity
  2069.  */
  2070. Sgl_setinfinity_exponentmantissa(resultp1);
  2071. Sgl_copytoptr(resultp1,dstptr);
  2072. return(NOEXCEPTION);
  2073. }
  2074. }
  2075. else {
  2076. /*
  2077.  * is NaN; signaling or quiet?
  2078.  */
  2079. if (Sgl_isone_signaling(opnd2)) {
  2080. /* trap if INVALIDTRAP enabled */
  2081. if (Is_invalidtrap_enabled())
  2082. return(OPC_2E_INVALIDEXCEPTION);
  2083. /* make NaN quiet */
  2084. Set_invalidflag();
  2085. Sgl_set_quiet(opnd2);
  2086. }
  2087. /* 
  2088.  * is third operand a signaling NaN? 
  2089.  */
  2090. else if (Sgl_is_signalingnan(opnd3)) {
  2091.         /* trap if INVALIDTRAP enabled */
  2092.         if (Is_invalidtrap_enabled())
  2093.     return(OPC_2E_INVALIDEXCEPTION);
  2094.         /* make NaN quiet */
  2095.         Set_invalidflag();
  2096.         Sgl_set_quiet(opnd3);
  2097. Sgl_copytoptr(opnd3,dstptr);
  2098.         return(NOEXCEPTION);
  2099. }
  2100. /*
  2101.  * return quiet NaN
  2102.  */
  2103. Sgl_copytoptr(opnd2,dstptr);
  2104. return(NOEXCEPTION);
  2105. }
  2106. }
  2107. /*
  2108.  * check third operand for NaN's or infinity
  2109.  */
  2110. if (Sgl_isinfinity_exponent(opnd3)) {
  2111. if (Sgl_iszero_mantissa(opnd3)) {
  2112. /* return infinity */
  2113. Sgl_copytoptr(opnd3,dstptr);
  2114. return(NOEXCEPTION);
  2115. } else {
  2116. /*
  2117.  * is NaN; signaling or quiet?
  2118.  */
  2119. if (Sgl_isone_signaling(opnd3)) {
  2120. /* trap if INVALIDTRAP enabled */
  2121. if (Is_invalidtrap_enabled())
  2122. return(OPC_2E_INVALIDEXCEPTION);
  2123. /* make NaN quiet */
  2124. Set_invalidflag();
  2125. Sgl_set_quiet(opnd3);
  2126. }
  2127. /*
  2128.  * return quiet NaN
  2129.    */
  2130. Sgl_copytoptr(opnd3,dstptr);
  2131. return(NOEXCEPTION);
  2132. }
  2133.      }
  2134. /*
  2135.  * Generate multiply mantissa
  2136.  */
  2137. if (Sgl_isnotzero_exponent(opnd1)) {
  2138. /* set hidden bit */
  2139. Sgl_clear_signexponent_set_hidden(opnd1);
  2140. }
  2141. else {
  2142. /* check for zero */
  2143. if (Sgl_iszero_mantissa(opnd1)) {
  2144. /*
  2145.  * Perform the add opnd3 with zero here.
  2146.  */
  2147. if (Sgl_iszero_exponentmantissa(opnd3)) {
  2148. if (Is_rounding_mode(ROUNDMINUS)) {
  2149. Sgl_or_signs(opnd3,resultp1);
  2150. } else {
  2151. Sgl_and_signs(opnd3,resultp1);
  2152. }
  2153. }
  2154. /*
  2155.  * Now let's check for trapped underflow case.
  2156.  */
  2157. else if (Sgl_iszero_exponent(opnd3) &&
  2158.          Is_underflowtrap_enabled()) {
  2159.                      /* need to normalize results mantissa */
  2160.                      sign_save = Sgl_signextendedsign(opnd3);
  2161. result_exponent = 0;
  2162.                      Sgl_leftshiftby1(opnd3);
  2163.                      Sgl_normalize(opnd3,result_exponent);
  2164.                      Sgl_set_sign(opnd3,/*using*/sign_save);
  2165.                      Sgl_setwrapped_exponent(opnd3,result_exponent,
  2166. unfl);
  2167.                      Sgl_copytoptr(opnd3,dstptr);
  2168.                      /* inexact = FALSE */
  2169.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  2170. }
  2171. Sgl_copytoptr(opnd3,dstptr);
  2172. return(NOEXCEPTION);
  2173. }
  2174. /* is denormalized, adjust exponent */
  2175. Sgl_clear_signexponent(opnd1);
  2176. Sgl_leftshiftby1(opnd1);
  2177. Sgl_normalize(opnd1,mpy_exponent);
  2178. }
  2179. /* opnd2 needs to have hidden bit set with msb in hidden bit */
  2180. if (Sgl_isnotzero_exponent(opnd2)) {
  2181. Sgl_clear_signexponent_set_hidden(opnd2);
  2182. }
  2183. else {
  2184. /* check for zero */
  2185. if (Sgl_iszero_mantissa(opnd2)) {
  2186. /*
  2187.  * Perform the add opnd3 with zero here.
  2188.  */
  2189. if (Sgl_iszero_exponentmantissa(opnd3)) {
  2190. if (Is_rounding_mode(ROUNDMINUS)) {
  2191. Sgl_or_signs(opnd3,resultp1);
  2192. } else {
  2193. Sgl_and_signs(opnd3,resultp1);
  2194. }
  2195. }
  2196. /*
  2197.  * Now let's check for trapped underflow case.
  2198.  */
  2199. else if (Sgl_iszero_exponent(opnd3) &&
  2200.     Is_underflowtrap_enabled()) {
  2201.                      /* need to normalize results mantissa */
  2202.                      sign_save = Sgl_signextendedsign(opnd3);
  2203. result_exponent = 0;
  2204.                      Sgl_leftshiftby1(opnd3);
  2205.                      Sgl_normalize(opnd3,result_exponent);
  2206.                      Sgl_set_sign(opnd3,/*using*/sign_save);
  2207.                      Sgl_setwrapped_exponent(opnd3,result_exponent,
  2208. unfl);
  2209.                      Sgl_copytoptr(opnd3,dstptr);
  2210.                      /* inexact = FALSE */
  2211.                      return(OPC_2E_UNDERFLOWEXCEPTION);
  2212. }
  2213. Sgl_copytoptr(opnd3,dstptr);
  2214. return(NOEXCEPTION);
  2215. }
  2216. /* is denormalized; want to normalize */
  2217. Sgl_clear_signexponent(opnd2);
  2218. Sgl_leftshiftby1(opnd2);
  2219. Sgl_normalize(opnd2,mpy_exponent);
  2220. }
  2221. /* Multiply the first two source mantissas together */
  2222. /* 
  2223.  * The intermediate result will be kept in tmpres,
  2224.  * which needs enough room for 106 bits of mantissa,
  2225.  * so lets call it a Double extended.
  2226.  */
  2227. Sglext_setzero(tmpresp1,tmpresp2);
  2228. /* 
  2229.  * Four bits at a time are inspected in each loop, and a 
  2230.  * simple shift and add multiply algorithm is used. 
  2231.  */ 
  2232. for (count = SGL_P-1; count >= 0; count -= 4) {
  2233. Sglext_rightshiftby4(tmpresp1,tmpresp2);
  2234. if (Sbit28(opnd1)) {
  2235.   /* Twoword_add should be an ADD followed by 2 ADDC's */
  2236. Twoword_add(tmpresp1, tmpresp2, opnd2<<3, 0);
  2237. }
  2238. if (Sbit29(opnd1)) {
  2239. Twoword_add(tmpresp1, tmpresp2, opnd2<<2, 0);
  2240. }
  2241. if (Sbit30(opnd1)) {
  2242. Twoword_add(tmpresp1, tmpresp2, opnd2<<1, 0);
  2243. }
  2244. if (Sbit31(opnd1)) {
  2245. Twoword_add(tmpresp1, tmpresp2, opnd2, 0);
  2246. }
  2247. Sgl_rightshiftby4(opnd1);
  2248. }
  2249. if (Is_sexthiddenoverflow(tmpresp1)) {
  2250. /* result mantissa >= 2 (mantissa overflow) */
  2251. mpy_exponent++;
  2252. Sglext_rightshiftby4(tmpresp1,tmpresp2);
  2253. } else {
  2254. Sglext_rightshiftby3(tmpresp1,tmpresp2);
  2255. }
  2256. /*
  2257.  * Restore the sign of the mpy result which was saved in resultp1.
  2258.  * The exponent will continue to be kept in mpy_exponent.
  2259.  */
  2260. Sglext_set_sign(tmpresp1,Sgl_sign(resultp1));
  2261. /* 
  2262.  * No rounding is required, since the result of the multiply
  2263.  * is exact in the extended format.
  2264.  */
  2265. /*
  2266.  * Now we are ready to perform the add portion of the operation.
  2267.  *
  2268.  * The exponents need to be kept as integers for now, since the
  2269.  * multiply result might not fit into the exponent field.  We
  2270.  * can't overflow or underflow because of this yet, since the
  2271.  * add could bring the final result back into range.
  2272.  */
  2273. add_exponent = Sgl_exponent(opnd3);
  2274. /*
  2275.  * Check for denormalized or zero add operand.
  2276.  */
  2277. if (add_exponent == 0) {
  2278. /* check for zero */
  2279. if (Sgl_iszero_mantissa(opnd3)) {
  2280. /* right is zero */
  2281. /* Left can't be zero and must be result.
  2282.  *
  2283.  * The final result is now in tmpres and mpy_exponent,
  2284.  * and needs to be rounded and squeezed back into
  2285.  * double precision format from double extended.
  2286.  */
  2287. result_exponent = mpy_exponent;
  2288. Sglext_copy(tmpresp1,tmpresp2,resultp1,resultp2);
  2289. sign_save = Sgl_signextendedsign(resultp1);/*save sign*/
  2290. goto round;
  2291. }
  2292. /* 
  2293.  * Neither are zeroes.  
  2294.  * Adjust exponent and normalize add operand.
  2295.  */
  2296. sign_save = Sgl_signextendedsign(opnd3); /* save sign */
  2297. Sgl_clear_signexponent(opnd3);
  2298. Sgl_leftshiftby1(opnd3);
  2299. Sgl_normalize(opnd3,add_exponent);
  2300. Sgl_set_sign(opnd3,sign_save); /* restore sign */
  2301. } else {
  2302. Sgl_clear_exponent_set_hidden(opnd3);
  2303. }
  2304. /*
  2305.  * Copy opnd3 to the double extended variable called right.
  2306.  */
  2307. Sgl_copyto_sglext(opnd3,rightp1,rightp2);
  2308. /*
  2309.  * A zero "save" helps discover equal operands (for later),
  2310.  * and is used in swapping operands (if needed).
  2311.  */
  2312. Sglext_xortointp1(tmpresp1,rightp1,/*to*/save);
  2313. /*
  2314.  * Compare magnitude of operands.
  2315.  */
  2316. Sglext_copytoint_exponentmantissa(tmpresp1,signlessleft1);
  2317. Sglext_copytoint_exponentmantissa(rightp1,signlessright1);
  2318. if (mpy_exponent < add_exponent || mpy_exponent == add_exponent &&
  2319.     Sglext_ismagnitudeless(signlessleft1,signlessright1)) {
  2320. /*
  2321.  * Set the left operand to the larger one by XOR swap.
  2322.  * First finish the first word "save".
  2323.  */
  2324. Sglext_xorfromintp1(save,rightp1,/*to*/rightp1);
  2325. Sglext_xorfromintp1(save,tmpresp1,/*to*/tmpresp1);
  2326. Sglext_swap_lower(tmpresp2,rightp2);
  2327. /* also setup exponents used in rest of routine */
  2328. diff_exponent = add_exponent - mpy_exponent;
  2329. result_exponent = add_exponent;
  2330. } else {
  2331. /* also setup exponents used in rest of routine */
  2332. diff_exponent = mpy_exponent - add_exponent;
  2333. result_exponent = mpy_exponent;
  2334. }
  2335. /* Invariant: left is not smaller than right. */
  2336. /*
  2337.  * Special case alignment of operands that would force alignment
  2338.  * beyond the extent of the extension.  A further optimization
  2339.  * could special case this but only reduces the path length for
  2340.  * this infrequent case.
  2341.  */
  2342. if (diff_exponent > SGLEXT_THRESHOLD) {
  2343. diff_exponent = SGLEXT_THRESHOLD;
  2344. }
  2345. /* Align right operand by shifting it to the right */
  2346. Sglext_clear_sign(rightp1);
  2347. Sglext_right_align(rightp1,rightp2,/*shifted by*/diff_exponent);
  2348. /* Treat sum and difference of the operands separately. */
  2349. if ((int)save < 0) {
  2350. /*
  2351.  * Difference of the two operands.  Overflow can occur if the
  2352.  * multiply overflowed.  A borrow can occur out of the hidden
  2353.  * bit and force a post normalization phase.
  2354.  */
  2355. Sglext_subtract(tmpresp1,tmpresp2, rightp1,rightp2,
  2356. resultp1,resultp2);
  2357. sign_save = Sgl_signextendedsign(resultp1);
  2358. if (Sgl_iszero_hidden(resultp1)) {
  2359. /* Handle normalization */
  2360. /* A straight foward algorithm would now shift the
  2361.  * result and extension left until the hidden bit
  2362.  * becomes one.  Not all of the extension bits need
  2363.  * participate in the shift.  Only the two most 
  2364.  * significant bits (round and guard) are needed.
  2365.  * If only a single shift is needed then the guard
  2366.  * bit becomes a significant low order bit and the
  2367.  * extension must participate in the rounding.
  2368.  * If more than a single shift is needed, then all
  2369.  * bits to the right of the guard bit are zeros, 
  2370.  * and the guard bit may or may not be zero. */
  2371. Sglext_leftshiftby1(resultp1,resultp2);
  2372. /* Need to check for a zero result.  The sign and
  2373.  * exponent fields have already been zeroed.  The more
  2374.  * efficient test of the full object can be used.
  2375.  */
  2376.  if (Sglext_iszero(resultp1,resultp2)) {
  2377. /* Must have been "x-x" or "x+(-x)". */
  2378. if (Is_rounding_mode(ROUNDMINUS))
  2379. Sgl_setone_sign(resultp1);
  2380. Sgl_copytoptr(resultp1,dstptr);
  2381. return(NOEXCEPTION);
  2382. }
  2383. result_exponent--;
  2384. /* Look to see if normalization is finished. */
  2385. if (Sgl_isone_hidden(resultp1)) {
  2386. /* No further normalization is needed */
  2387. goto round;
  2388. }
  2389. /* Discover first one bit to determine shift amount.
  2390.  * Use a modified binary search.  We have already
  2391.  * shifted the result one position right and still
  2392.  * not found a one so the remainder of the extension
  2393.  * must be zero and simplifies rounding. */
  2394. /* Scan bytes */
  2395. while (Sgl_iszero_hiddenhigh7mantissa(resultp1)) {
  2396. Sglext_leftshiftby8(resultp1,resultp2);
  2397. result_exponent -= 8;
  2398. }
  2399. /* Now narrow it down to the nibble */
  2400. if (Sgl_iszero_hiddenhigh3mantissa(resultp1)) {
  2401. /* The lower nibble contains the
  2402.  * normalizing one */
  2403. Sglext_leftshiftby4(resultp1,resultp2);
  2404. result_exponent -= 4;
  2405. }
  2406. /* Select case where first bit is set (already
  2407.  * normalized) otherwise select the proper shift. */
  2408. jumpsize = Sgl_hiddenhigh3mantissa(resultp1);
  2409. if (jumpsize <= 7) switch(jumpsize) {
  2410. case 1:
  2411. Sglext_leftshiftby3(resultp1,resultp2);
  2412. result_exponent -= 3;
  2413. break;
  2414. case 2:
  2415. case 3:
  2416. Sglext_leftshiftby2(resultp1,resultp2);
  2417. result_exponent -= 2;
  2418. break;
  2419. case 4:
  2420. case 5:
  2421. case 6:
  2422. case 7:
  2423. Sglext_leftshiftby1(resultp1,resultp2);
  2424. result_exponent -= 1;
  2425. break;
  2426. }
  2427. } /* end if (hidden...)... */
  2428. /* Fall through and round */
  2429. } /* end if (save < 0)... */
  2430. else {
  2431. /* Add magnitudes */
  2432. Sglext_addition(tmpresp1,tmpresp2,
  2433. rightp1,rightp2, /*to*/resultp1,resultp2);
  2434. sign_save = Sgl_signextendedsign(resultp1);
  2435. if (Sgl_isone_hiddenoverflow(resultp1)) {
  2436.      /* Prenormalization required. */
  2437.      Sglext_arithrightshiftby1(resultp1,resultp2);
  2438.      result_exponent++;
  2439. } /* end if hiddenoverflow... */
  2440. } /* end else ...add magnitudes... */
  2441. /* Round the result.  If the extension and lower two words are
  2442.  * all zeros, then the result is exact.  Otherwise round in the
  2443.  * correct direction.  Underflow is possible. If a postnormalization
  2444.  * is necessary, then the mantissa is all zeros so no shift is needed.
  2445.  */
  2446.   round:
  2447. if (result_exponent <= 0 && !Is_underflowtrap_enabled()) {
  2448. Sglext_denormalize(resultp1,resultp2,result_exponent,is_tiny);
  2449. }
  2450. Sgl_set_sign(resultp1,/*using*/sign_save);
  2451. if (Sglext_isnotzero_mantissap2(resultp2)) {
  2452. inexact = TRUE;
  2453. switch(Rounding_mode()) {
  2454. case ROUNDNEAREST: /* The default. */
  2455. if (Sglext_isone_highp2(resultp2)) {
  2456. /* at least 1/2 ulp */
  2457. if (Sglext_isnotzero_low31p2(resultp2) ||
  2458.     Sglext_isone_lowp1(resultp1)) {
  2459. /* either exactly half way and odd or
  2460.  * more than 1/2ulp */
  2461. Sgl_increment(resultp1);
  2462. }
  2463. }
  2464.      break;
  2465. case ROUNDPLUS:
  2466.      if (Sgl_iszero_sign(resultp1)) {
  2467. /* Round up positive results */
  2468. Sgl_increment(resultp1);
  2469. }
  2470. break;
  2471.     
  2472. case ROUNDMINUS:
  2473.      if (Sgl_isone_sign(resultp1)) {
  2474. /* Round down negative results */
  2475. Sgl_increment(resultp1);
  2476. }
  2477.     
  2478. case ROUNDZERO:;
  2479. /* truncate is simple */
  2480. } /* end switch... */
  2481. if (Sgl_isone_hiddenoverflow(resultp1)) result_exponent++;
  2482. }
  2483. if (result_exponent >= SGL_INFINITY_EXPONENT) {
  2484. /* Overflow */
  2485. if (Is_overflowtrap_enabled()) {
  2486.                         /*
  2487.                          * Adjust bias of result
  2488.                          */
  2489.                         Sgl_setwrapped_exponent(resultp1,result_exponent,ovfl);
  2490.                         Sgl_copytoptr(resultp1,dstptr);
  2491.                         if (inexact)
  2492.                             if (Is_inexacttrap_enabled())
  2493.                                 return (OPC_2E_OVERFLOWEXCEPTION |
  2494. OPC_2E_INEXACTEXCEPTION);
  2495.                             else Set_inexactflag();
  2496.                         return (OPC_2E_OVERFLOWEXCEPTION);
  2497. }
  2498. inexact = TRUE;
  2499. Set_overflowflag();
  2500. Sgl_setoverflow(resultp1);
  2501. } else if (result_exponent <= 0) { /* underflow case */
  2502. if (Is_underflowtrap_enabled()) {
  2503.                         /*
  2504.                          * Adjust bias of result
  2505.                          */
  2506.                  Sgl_setwrapped_exponent(resultp1,result_exponent,unfl);
  2507. Sgl_copytoptr(resultp1,dstptr);
  2508.                         if (inexact)
  2509.                             if (Is_inexacttrap_enabled())
  2510.                                 return (OPC_2E_UNDERFLOWEXCEPTION |
  2511. OPC_2E_INEXACTEXCEPTION);
  2512.                             else Set_inexactflag();
  2513.      return(OPC_2E_UNDERFLOWEXCEPTION);
  2514. }
  2515. else if (inexact && is_tiny) Set_underflowflag();
  2516. }
  2517. else Sgl_set_exponent(resultp1,result_exponent);
  2518. Sgl_copytoptr(resultp1,dstptr);
  2519. if (inexact) 
  2520. if (Is_inexacttrap_enabled()) return(OPC_2E_INEXACTEXCEPTION);
  2521. else Set_inexactflag();
  2522.      return(NOEXCEPTION);
  2523. }