fmpyfadd.c 78 KB

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