decBasic.c 176 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908
  1. /* ------------------------------------------------------------------ */
  2. /* decBasic.c -- common base code for Basic decimal types */
  3. /* ------------------------------------------------------------------ */
  4. /* Copyright (c) IBM Corporation, 2000, 2010. All rights reserved. */
  5. /* */
  6. /* This software is made available under the terms of the */
  7. /* ICU License -- ICU 1.8.1 and later. */
  8. /* */
  9. /* The description and User's Guide ("The decNumber C Library") for */
  10. /* this software is included in the package as decNumber.pdf. This */
  11. /* document is also available in HTML, together with specifications, */
  12. /* testcases, and Web links, on the General Decimal Arithmetic page. */
  13. /* */
  14. /* Please send comments, suggestions, and corrections to the author: */
  15. /* [email protected] */
  16. /* Mike Cowlishaw, IBM Fellow */
  17. /* IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK */
  18. /* ------------------------------------------------------------------ */
  19. /* This module comprises code that is shared between decDouble and */
  20. /* decQuad (but not decSingle). The main arithmetic operations are */
  21. /* here (Add, Subtract, Multiply, FMA, and Division operators). */
  22. /* */
  23. /* Unlike decNumber, parameterization takes place at compile time */
  24. /* rather than at runtime. The parameters are set in the decDouble.c */
  25. /* (etc.) files, which then include this one to produce the compiled */
  26. /* code. The functions here, therefore, are code shared between */
  27. /* multiple formats. */
  28. /* */
  29. /* This must be included after decCommon.c. */
  30. /* ------------------------------------------------------------------ */
  31. // Names here refer to decFloat rather than to decDouble, etc., and
  32. // the functions are in strict alphabetical order.
  33. // The compile-time flags SINGLE, DOUBLE, and QUAD are set up in
  34. // decCommon.c
  35. #if !defined(QUAD)
  36. #error decBasic.c must be included after decCommon.c
  37. #endif
  38. #if SINGLE
  39. #error Routines in decBasic.c are for decDouble and decQuad only
  40. #endif
  41. /* Private constants */
  42. #define DIVIDE 0x80000000 // Divide operations [as flags]
  43. #define REMAINDER 0x40000000 // ..
  44. #define DIVIDEINT 0x20000000 // ..
  45. #define REMNEAR 0x10000000 // ..
  46. /* Private functions (local, used only by routines in this module) */
  47. static decFloat *decDivide(decFloat *, const decFloat *,
  48. const decFloat *, decContext *, uInt);
  49. static decFloat *decCanonical(decFloat *, const decFloat *);
  50. static void decFiniteMultiply(bcdnum *, uByte *, const decFloat *,
  51. const decFloat *);
  52. static decFloat *decInfinity(decFloat *, const decFloat *);
  53. static decFloat *decInvalid(decFloat *, decContext *);
  54. static decFloat *decNaNs(decFloat *, const decFloat *, const decFloat *,
  55. decContext *);
  56. static Int decNumCompare(const decFloat *, const decFloat *, Flag);
  57. static decFloat *decToIntegral(decFloat *, const decFloat *, decContext *,
  58. enum rounding, Flag);
  59. static uInt decToInt32(const decFloat *, decContext *, enum rounding,
  60. Flag, Flag);
  61. /* ------------------------------------------------------------------ */
  62. /* decCanonical -- copy a decFloat, making canonical */
  63. /* */
  64. /* result gets the canonicalized df */
  65. /* df is the decFloat to copy and make canonical */
  66. /* returns result */
  67. /* */
  68. /* This is exposed via decFloatCanonical for Double and Quad only. */
  69. /* This works on specials, too; no error or exception is possible. */
  70. /* ------------------------------------------------------------------ */
  71. static decFloat * decCanonical(decFloat *result, const decFloat *df) {
  72. uInt encode, precode, dpd; // work
  73. uInt inword, uoff, canon; // ..
  74. Int n; // counter (down)
  75. if (df!=result) *result=*df; // effect copy if needed
  76. if (DFISSPECIAL(result)) {
  77. if (DFISINF(result)) return decInfinity(result, df); // clean Infinity
  78. // is a NaN
  79. DFWORD(result, 0)&=~ECONNANMASK; // clear ECON except selector
  80. if (DFISCCZERO(df)) return result; // coefficient continuation is 0
  81. // drop through to check payload
  82. }
  83. // return quickly if the coefficient continuation is canonical
  84. { // declare block
  85. #if DOUBLE
  86. uInt sourhi=DFWORD(df, 0);
  87. uInt sourlo=DFWORD(df, 1);
  88. if (CANONDPDOFF(sourhi, 8)
  89. && CANONDPDTWO(sourhi, sourlo, 30)
  90. && CANONDPDOFF(sourlo, 20)
  91. && CANONDPDOFF(sourlo, 10)
  92. && CANONDPDOFF(sourlo, 0)) return result;
  93. #elif QUAD
  94. uInt sourhi=DFWORD(df, 0);
  95. uInt sourmh=DFWORD(df, 1);
  96. uInt sourml=DFWORD(df, 2);
  97. uInt sourlo=DFWORD(df, 3);
  98. if (CANONDPDOFF(sourhi, 4)
  99. && CANONDPDTWO(sourhi, sourmh, 26)
  100. && CANONDPDOFF(sourmh, 16)
  101. && CANONDPDOFF(sourmh, 6)
  102. && CANONDPDTWO(sourmh, sourml, 28)
  103. && CANONDPDOFF(sourml, 18)
  104. && CANONDPDOFF(sourml, 8)
  105. && CANONDPDTWO(sourml, sourlo, 30)
  106. && CANONDPDOFF(sourlo, 20)
  107. && CANONDPDOFF(sourlo, 10)
  108. && CANONDPDOFF(sourlo, 0)) return result;
  109. #endif
  110. } // block
  111. // Loop to repair a non-canonical coefficent, as needed
  112. inword=DECWORDS-1; // current input word
  113. uoff=0; // bit offset of declet
  114. encode=DFWORD(result, inword);
  115. for (n=DECLETS-1; n>=0; n--) { // count down declets of 10 bits
  116. dpd=encode>>uoff;
  117. uoff+=10;
  118. if (uoff>32) { // crossed uInt boundary
  119. inword--;
  120. encode=DFWORD(result, inword);
  121. uoff-=32;
  122. dpd|=encode<<(10-uoff); // get pending bits
  123. }
  124. dpd&=0x3ff; // clear uninteresting bits
  125. if (dpd<0x16e) continue; // must be canonical
  126. canon=BIN2DPD[DPD2BIN[dpd]]; // determine canonical declet
  127. if (canon==dpd) continue; // have canonical declet
  128. // need to replace declet
  129. if (uoff>=10) { // all within current word
  130. encode&=~(0x3ff<<(uoff-10)); // clear the 10 bits ready for replace
  131. encode|=canon<<(uoff-10); // insert the canonical form
  132. DFWORD(result, inword)=encode; // .. and save
  133. continue;
  134. }
  135. // straddled words
  136. precode=DFWORD(result, inword+1); // get previous
  137. precode&=0xffffffff>>(10-uoff); // clear top bits
  138. DFWORD(result, inword+1)=precode|(canon<<(32-(10-uoff)));
  139. encode&=0xffffffff<<uoff; // clear bottom bits
  140. encode|=canon>>(10-uoff); // insert canonical
  141. DFWORD(result, inword)=encode; // .. and save
  142. } // n
  143. return result;
  144. } // decCanonical
  145. /* ------------------------------------------------------------------ */
  146. /* decDivide -- divide operations */
  147. /* */
  148. /* result gets the result of dividing dfl by dfr: */
  149. /* dfl is the first decFloat (lhs) */
  150. /* dfr is the second decFloat (rhs) */
  151. /* set is the context */
  152. /* op is the operation selector */
  153. /* returns result */
  154. /* */
  155. /* op is one of DIVIDE, REMAINDER, DIVIDEINT, or REMNEAR. */
  156. /* ------------------------------------------------------------------ */
  157. #define DIVCOUNT 0 // 1 to instrument subtractions counter
  158. #define DIVBASE ((uInt)BILLION) // the base used for divide
  159. #define DIVOPLEN DECPMAX9 // operand length ('digits' base 10**9)
  160. #define DIVACCLEN (DIVOPLEN*3) // accumulator length (ditto)
  161. static decFloat * decDivide(decFloat *result, const decFloat *dfl,
  162. const decFloat *dfr, decContext *set, uInt op) {
  163. decFloat quotient; // for remainders
  164. bcdnum num; // for final conversion
  165. uInt acc[DIVACCLEN]; // coefficent in base-billion ..
  166. uInt div[DIVOPLEN]; // divisor in base-billion ..
  167. uInt quo[DIVOPLEN+1]; // quotient in base-billion ..
  168. uByte bcdacc[(DIVOPLEN+1)*9+2]; // for quotient in BCD, +1, +1
  169. uInt *msua, *msud, *msuq; // -> msu of acc, div, and quo
  170. Int divunits, accunits; // lengths
  171. Int quodigits; // digits in quotient
  172. uInt *lsua, *lsuq; // -> current acc and quo lsus
  173. Int length, multiplier; // work
  174. uInt carry, sign; // ..
  175. uInt *ua, *ud, *uq; // ..
  176. uByte *ub; // ..
  177. uInt uiwork; // for macros
  178. uInt divtop; // top unit of div adjusted for estimating
  179. #if DIVCOUNT
  180. static uInt maxcount=0; // worst-seen subtractions count
  181. uInt divcount=0; // subtractions count [this divide]
  182. #endif
  183. // calculate sign
  184. num.sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
  185. if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { // either is special?
  186. // NaNs are handled as usual
  187. if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  188. // one or two infinities
  189. if (DFISINF(dfl)) {
  190. if (DFISINF(dfr)) return decInvalid(result, set); // Two infinities bad
  191. if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); // as is rem
  192. // Infinity/x is infinite and quiet, even if x=0
  193. DFWORD(result, 0)=num.sign;
  194. return decInfinity(result, result);
  195. }
  196. // must be x/Infinity -- remainders are lhs
  197. if (op&(REMAINDER|REMNEAR)) return decCanonical(result, dfl);
  198. // divides: return zero with correct sign and exponent depending
  199. // on op (Etiny for divide, 0 for divideInt)
  200. decFloatZero(result);
  201. if (op==DIVIDEINT) DFWORD(result, 0)|=num.sign; // add sign
  202. else DFWORD(result, 0)=num.sign; // zeros the exponent, too
  203. return result;
  204. }
  205. // next, handle zero operands (x/0 and 0/x)
  206. if (DFISZERO(dfr)) { // x/0
  207. if (DFISZERO(dfl)) { // 0/0 is undefined
  208. decFloatZero(result);
  209. DFWORD(result, 0)=DECFLOAT_qNaN;
  210. set->status|=DEC_Division_undefined;
  211. return result;
  212. }
  213. if (op&(REMAINDER|REMNEAR)) return decInvalid(result, set); // bad rem
  214. set->status|=DEC_Division_by_zero;
  215. DFWORD(result, 0)=num.sign;
  216. return decInfinity(result, result); // x/0 -> signed Infinity
  217. }
  218. num.exponent=GETEXPUN(dfl)-GETEXPUN(dfr); // ideal exponent
  219. if (DFISZERO(dfl)) { // 0/x (x!=0)
  220. // if divide, result is 0 with ideal exponent; divideInt has
  221. // exponent=0, remainders give zero with lower exponent
  222. if (op&DIVIDEINT) {
  223. decFloatZero(result);
  224. DFWORD(result, 0)|=num.sign; // add sign
  225. return result;
  226. }
  227. if (!(op&DIVIDE)) { // a remainder
  228. // exponent is the minimum of the operands
  229. num.exponent=MINI(GETEXPUN(dfl), GETEXPUN(dfr));
  230. // if the result is zero the sign shall be sign of dfl
  231. num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
  232. }
  233. bcdacc[0]=0;
  234. num.msd=bcdacc; // -> 0
  235. num.lsd=bcdacc; // ..
  236. return decFinalize(result, &num, set); // [divide may clamp exponent]
  237. } // 0/x
  238. // [here, both operands are known to be finite and non-zero]
  239. // extract the operand coefficents into 'units' which are
  240. // base-billion; the lhs is high-aligned in acc and the msu of both
  241. // acc and div is at the right-hand end of array (offset length-1);
  242. // the quotient can need one more unit than the operands as digits
  243. // in it are not necessarily aligned neatly; further, the quotient
  244. // may not start accumulating until after the end of the initial
  245. // operand in acc if that is small (e.g., 1) so the accumulator
  246. // must have at least that number of units extra (at the ls end)
  247. GETCOEFFBILL(dfl, acc+DIVACCLEN-DIVOPLEN);
  248. GETCOEFFBILL(dfr, div);
  249. // zero the low uInts of acc
  250. acc[0]=0;
  251. acc[1]=0;
  252. acc[2]=0;
  253. acc[3]=0;
  254. #if DOUBLE
  255. #if DIVOPLEN!=2
  256. #error Unexpected Double DIVOPLEN
  257. #endif
  258. #elif QUAD
  259. acc[4]=0;
  260. acc[5]=0;
  261. acc[6]=0;
  262. acc[7]=0;
  263. #if DIVOPLEN!=4
  264. #error Unexpected Quad DIVOPLEN
  265. #endif
  266. #endif
  267. // set msu and lsu pointers
  268. msua=acc+DIVACCLEN-1; // [leading zeros removed below]
  269. msuq=quo+DIVOPLEN;
  270. //[loop for div will terminate because operands are non-zero]
  271. for (msud=div+DIVOPLEN-1; *msud==0;) msud--;
  272. // the initial least-significant unit of acc is set so acc appears
  273. // to have the same length as div.
  274. // This moves one position towards the least possible for each
  275. // iteration
  276. divunits=(Int)(msud-div+1); // precalculate
  277. lsua=msua-divunits+1; // initial working lsu of acc
  278. lsuq=msuq; // and of quo
  279. // set up the estimator for the multiplier; this is the msu of div,
  280. // plus two bits from the unit below (if any) rounded up by one if
  281. // there are any non-zero bits or units below that [the extra two
  282. // bits makes for a much better estimate when the top unit is small]
  283. divtop=*msud<<2;
  284. if (divunits>1) {
  285. uInt *um=msud-1;
  286. uInt d=*um;
  287. if (d>=750000000) {divtop+=3; d-=750000000;}
  288. else if (d>=500000000) {divtop+=2; d-=500000000;}
  289. else if (d>=250000000) {divtop++; d-=250000000;}
  290. if (d) divtop++;
  291. else for (um--; um>=div; um--) if (*um) {
  292. divtop++;
  293. break;
  294. }
  295. } // >1 unit
  296. #if DECTRACE
  297. {Int i;
  298. printf("----- div=");
  299. for (i=divunits-1; i>=0; i--) printf("%09ld ", (LI)div[i]);
  300. printf("\n");}
  301. #endif
  302. // now collect up to DECPMAX+1 digits in the quotient (this may
  303. // need OPLEN+1 uInts if unaligned)
  304. quodigits=0; // no digits yet
  305. for (;; lsua--) { // outer loop -- each input position
  306. #if DECCHECK
  307. if (lsua<acc) {
  308. printf("Acc underrun...\n");
  309. break;
  310. }
  311. #endif
  312. #if DECTRACE
  313. printf("Outer: quodigits=%ld acc=", (LI)quodigits);
  314. for (ua=msua; ua>=lsua; ua--) printf("%09ld ", (LI)*ua);
  315. printf("\n");
  316. #endif
  317. *lsuq=0; // default unit result is 0
  318. for (;;) { // inner loop -- calculate quotient unit
  319. // strip leading zero units from acc (either there initially or
  320. // from subtraction below); this may strip all if exactly 0
  321. for (; *msua==0 && msua>=lsua;) msua--;
  322. accunits=(Int)(msua-lsua+1); // [maybe 0]
  323. // subtraction is only necessary and possible if there are as
  324. // least as many units remaining in acc for this iteration as
  325. // there are in div
  326. if (accunits<divunits) {
  327. if (accunits==0) msua++; // restore
  328. break;
  329. }
  330. // If acc is longer than div then subtraction is definitely
  331. // possible (as msu of both is non-zero), but if they are the
  332. // same length a comparison is needed.
  333. // If a subtraction is needed then a good estimate of the
  334. // multiplier for the subtraction is also needed in order to
  335. // minimise the iterations of this inner loop because the
  336. // subtractions needed dominate division performance.
  337. if (accunits==divunits) {
  338. // compare the high divunits of acc and div:
  339. // acc<div: this quotient unit is unchanged; subtraction
  340. // will be possible on the next iteration
  341. // acc==div: quotient gains 1, set acc=0
  342. // acc>div: subtraction necessary at this position
  343. for (ud=msud, ua=msua; ud>div; ud--, ua--) if (*ud!=*ua) break;
  344. // [now at first mismatch or lsu]
  345. if (*ud>*ua) break; // next time...
  346. if (*ud==*ua) { // all compared equal
  347. *lsuq+=1; // increment result
  348. msua=lsua; // collapse acc units
  349. *msua=0; // .. to a zero
  350. break;
  351. }
  352. // subtraction necessary; estimate multiplier [see above]
  353. // if both *msud and *msua are small it is cost-effective to
  354. // bring in part of the following units (if any) to get a
  355. // better estimate (assume some other non-zero in div)
  356. #define DIVLO 1000000U
  357. #define DIVHI (DIVBASE/DIVLO)
  358. #if DECUSE64
  359. if (divunits>1) {
  360. // there cannot be a *(msud-2) for DECDOUBLE so next is
  361. // an exact calculation unless DECQUAD (which needs to
  362. // assume bits out there if divunits>2)
  363. uLong mul=(uLong)*msua * DIVBASE + *(msua-1);
  364. uLong div=(uLong)*msud * DIVBASE + *(msud-1);
  365. #if QUAD
  366. if (divunits>2) div++;
  367. #endif
  368. mul/=div;
  369. multiplier=(Int)mul;
  370. }
  371. else multiplier=*msua/(*msud);
  372. #else
  373. if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
  374. multiplier=(*msua*DIVHI + *(msua-1)/DIVLO)
  375. /(*msud*DIVHI + *(msud-1)/DIVLO +1);
  376. }
  377. else multiplier=(*msua<<2)/divtop;
  378. #endif
  379. }
  380. else { // accunits>divunits
  381. // msud is one unit 'lower' than msua, so estimate differently
  382. #if DECUSE64
  383. uLong mul;
  384. // as before, bring in extra digits if possible
  385. if (divunits>1 && *msua<DIVLO && *msud<DIVLO) {
  386. mul=((uLong)*msua * DIVHI * DIVBASE) + *(msua-1) * DIVHI
  387. + *(msua-2)/DIVLO;
  388. mul/=(*msud*DIVHI + *(msud-1)/DIVLO +1);
  389. }
  390. else if (divunits==1) {
  391. mul=(uLong)*msua * DIVBASE + *(msua-1);
  392. mul/=*msud; // no more to the right
  393. }
  394. else {
  395. mul=(uLong)(*msua) * (uInt)(DIVBASE<<2)
  396. + (*(msua-1)<<2);
  397. mul/=divtop; // [divtop already allows for sticky bits]
  398. }
  399. multiplier=(Int)mul;
  400. #else
  401. multiplier=*msua * ((DIVBASE<<2)/divtop);
  402. #endif
  403. }
  404. if (multiplier==0) multiplier=1; // marginal case
  405. *lsuq+=multiplier;
  406. #if DIVCOUNT
  407. // printf("Multiplier: %ld\n", (LI)multiplier);
  408. divcount++;
  409. #endif
  410. // Carry out the subtraction acc-(div*multiplier); for each
  411. // unit in div, do the multiply, split to units (see
  412. // decFloatMultiply for the algorithm), and subtract from acc
  413. #define DIVMAGIC 2305843009U // 2**61/10**9
  414. #define DIVSHIFTA 29
  415. #define DIVSHIFTB 32
  416. carry=0;
  417. for (ud=div, ua=lsua; ud<=msud; ud++, ua++) {
  418. uInt lo, hop;
  419. #if DECUSE64
  420. uLong sub=(uLong)multiplier*(*ud)+carry;
  421. if (sub<DIVBASE) {
  422. carry=0;
  423. lo=(uInt)sub;
  424. }
  425. else {
  426. hop=(uInt)(sub>>DIVSHIFTA);
  427. carry=(uInt)(((uLong)hop*DIVMAGIC)>>DIVSHIFTB);
  428. // the estimate is now in hi; now calculate sub-hi*10**9
  429. // to get the remainder (which will be <DIVBASE))
  430. lo=(uInt)sub;
  431. lo-=carry*DIVBASE; // low word of result
  432. if (lo>=DIVBASE) {
  433. lo-=DIVBASE; // correct by +1
  434. carry++;
  435. }
  436. }
  437. #else // 32-bit
  438. uInt hi;
  439. // calculate multiplier*(*ud) into hi and lo
  440. LONGMUL32HI(hi, *ud, multiplier); // get the high word
  441. lo=multiplier*(*ud); // .. and the low
  442. lo+=carry; // add the old hi
  443. carry=hi+(lo<carry); // .. with any carry
  444. if (carry || lo>=DIVBASE) { // split is needed
  445. hop=(carry<<3)+(lo>>DIVSHIFTA); // hi:lo/2**29
  446. LONGMUL32HI(carry, hop, DIVMAGIC); // only need the high word
  447. // [DIVSHIFTB is 32, so carry can be used directly]
  448. // the estimate is now in carry; now calculate hi:lo-est*10**9;
  449. // happily the top word of the result is irrelevant because it
  450. // will always be zero so this needs only one multiplication
  451. lo-=(carry*DIVBASE);
  452. // the correction here will be at most +1; do it
  453. if (lo>=DIVBASE) {
  454. lo-=DIVBASE;
  455. carry++;
  456. }
  457. }
  458. #endif
  459. if (lo>*ua) { // borrow needed
  460. *ua+=DIVBASE;
  461. carry++;
  462. }
  463. *ua-=lo;
  464. } // ud loop
  465. if (carry) *ua-=carry; // accdigits>divdigits [cannot borrow]
  466. } // inner loop
  467. // the outer loop terminates when there is either an exact result
  468. // or enough digits; first update the quotient digit count and
  469. // pointer (if any significant digits)
  470. #if DECTRACE
  471. if (*lsuq || quodigits) printf("*lsuq=%09ld\n", (LI)*lsuq);
  472. #endif
  473. if (quodigits) {
  474. quodigits+=9; // had leading unit earlier
  475. lsuq--;
  476. if (quodigits>DECPMAX+1) break; // have enough
  477. }
  478. else if (*lsuq) { // first quotient digits
  479. const uInt *pow;
  480. for (pow=DECPOWERS; *lsuq>=*pow; pow++) quodigits++;
  481. lsuq--;
  482. // [cannot have >DECPMAX+1 on first unit]
  483. }
  484. if (*msua!=0) continue; // not an exact result
  485. // acc is zero iff used all of original units and zero down to lsua
  486. // (must also continue to original lsu for correct quotient length)
  487. if (lsua>acc+DIVACCLEN-DIVOPLEN) continue;
  488. for (; msua>lsua && *msua==0;) msua--;
  489. if (*msua==0 && msua==lsua) break;
  490. } // outer loop
  491. // all of the original operand in acc has been covered at this point
  492. // quotient now has at least DECPMAX+2 digits
  493. // *msua is now non-0 if inexact and sticky bits
  494. // lsuq is one below the last uint of the quotient
  495. lsuq++; // set -> true lsu of quo
  496. if (*msua) *lsuq|=1; // apply sticky bit
  497. // quo now holds the (unrounded) quotient in base-billion; one
  498. // base-billion 'digit' per uInt.
  499. #if DECTRACE
  500. printf("DivQuo:");
  501. for (uq=msuq; uq>=lsuq; uq--) printf(" %09ld", (LI)*uq);
  502. printf("\n");
  503. #endif
  504. // Now convert to BCD for rounding and cleanup, starting from the
  505. // most significant end [offset by one into bcdacc to leave room
  506. // for a possible carry digit if rounding for REMNEAR is needed]
  507. for (uq=msuq, ub=bcdacc+1; uq>=lsuq; uq--, ub+=9) {
  508. uInt top, mid, rem; // work
  509. if (*uq==0) { // no split needed
  510. UBFROMUI(ub, 0); // clear 9 BCD8s
  511. UBFROMUI(ub+4, 0); // ..
  512. *(ub+8)=0; // ..
  513. continue;
  514. }
  515. // *uq is non-zero -- split the base-billion digit into
  516. // hi, mid, and low three-digits
  517. #define divsplit9 1000000 // divisor
  518. #define divsplit6 1000 // divisor
  519. // The splitting is done by simple divides and remainders,
  520. // assuming the compiler will optimize these [GCC does]
  521. top=*uq/divsplit9;
  522. rem=*uq%divsplit9;
  523. mid=rem/divsplit6;
  524. rem=rem%divsplit6;
  525. // lay out the nine BCD digits (plus one unwanted byte)
  526. UBFROMUI(ub, UBTOUI(&BIN2BCD8[top*4]));
  527. UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4]));
  528. UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4]));
  529. } // BCD conversion loop
  530. ub--; // -> lsu
  531. // complete the bcdnum; quodigits is correct, so the position of
  532. // the first non-zero is known
  533. num.msd=bcdacc+1+(msuq-lsuq+1)*9-quodigits;
  534. num.lsd=ub;
  535. // make exponent adjustments, etc
  536. if (lsua<acc+DIVACCLEN-DIVOPLEN) { // used extra digits
  537. num.exponent-=(Int)((acc+DIVACCLEN-DIVOPLEN-lsua)*9);
  538. // if the result was exact then there may be up to 8 extra
  539. // trailing zeros in the overflowed quotient final unit
  540. if (*msua==0) {
  541. for (; *ub==0;) ub--; // drop zeros
  542. num.exponent+=(Int)(num.lsd-ub); // and adjust exponent
  543. num.lsd=ub;
  544. }
  545. } // adjustment needed
  546. #if DIVCOUNT
  547. if (divcount>maxcount) { // new high-water nark
  548. maxcount=divcount;
  549. printf("DivNewMaxCount: %ld\n", (LI)maxcount);
  550. }
  551. #endif
  552. if (op&DIVIDE) return decFinalize(result, &num, set); // all done
  553. // Is DIVIDEINT or a remainder; there is more to do -- first form
  554. // the integer (this is done 'after the fact', unlike as in
  555. // decNumber, so as not to tax DIVIDE)
  556. // The first non-zero digit will be in the first 9 digits, known
  557. // from quodigits and num.msd, so there is always space for DECPMAX
  558. // digits
  559. length=(Int)(num.lsd-num.msd+1);
  560. //printf("Length exp: %ld %ld\n", (LI)length, (LI)num.exponent);
  561. if (length+num.exponent>DECPMAX) { // cannot fit
  562. decFloatZero(result);
  563. DFWORD(result, 0)=DECFLOAT_qNaN;
  564. set->status|=DEC_Division_impossible;
  565. return result;
  566. }
  567. if (num.exponent>=0) { // already an int, or need pad zeros
  568. for (ub=num.lsd+1; ub<=num.lsd+num.exponent; ub++) *ub=0;
  569. num.lsd+=num.exponent;
  570. }
  571. else { // too long: round or truncate needed
  572. Int drop=-num.exponent;
  573. if (!(op&REMNEAR)) { // simple truncate
  574. num.lsd-=drop;
  575. if (num.lsd<num.msd) { // truncated all
  576. num.lsd=num.msd; // make 0
  577. *num.lsd=0; // .. [sign still relevant]
  578. }
  579. }
  580. else { // round to nearest even [sigh]
  581. // round-to-nearest, in-place; msd is at or to right of bcdacc+1
  582. // (this is a special case of Quantize -- q.v. for commentary)
  583. uByte *roundat; // -> re-round digit
  584. uByte reround; // reround value
  585. *(num.msd-1)=0; // in case of left carry, or make 0
  586. if (drop<length) roundat=num.lsd-drop+1;
  587. else if (drop==length) roundat=num.msd;
  588. else roundat=num.msd-1; // [-> 0]
  589. reround=*roundat;
  590. for (ub=roundat+1; ub<=num.lsd; ub++) {
  591. if (*ub!=0) {
  592. reround=DECSTICKYTAB[reround];
  593. break;
  594. }
  595. } // check stickies
  596. if (roundat>num.msd) num.lsd=roundat-1;
  597. else {
  598. num.msd--; // use the 0 ..
  599. num.lsd=num.msd; // .. at the new MSD place
  600. }
  601. if (reround!=0) { // discarding non-zero
  602. uInt bump=0;
  603. // rounding is DEC_ROUND_HALF_EVEN always
  604. if (reround>5) bump=1; // >0.5 goes up
  605. else if (reround==5) // exactly 0.5000 ..
  606. bump=*(num.lsd) & 0x01; // .. up iff [new] lsd is odd
  607. if (bump!=0) { // need increment
  608. // increment the coefficient; this might end up with 1000...
  609. ub=num.lsd;
  610. for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0);
  611. for (; *ub==9; ub--) *ub=0; // at most 3 more
  612. *ub+=1;
  613. if (ub<num.msd) num.msd--; // carried
  614. } // bump needed
  615. } // reround!=0
  616. } // remnear
  617. } // round or truncate needed
  618. num.exponent=0; // all paths
  619. //decShowNum(&num, "int");
  620. if (op&DIVIDEINT) return decFinalize(result, &num, set); // all done
  621. // Have a remainder to calculate
  622. decFinalize(&quotient, &num, set); // lay out the integer so far
  623. DFWORD(&quotient, 0)^=DECFLOAT_Sign; // negate it
  624. sign=DFWORD(dfl, 0); // save sign of dfl
  625. decFloatFMA(result, &quotient, dfr, dfl, set);
  626. if (!DFISZERO(result)) return result;
  627. // if the result is zero the sign shall be sign of dfl
  628. DFWORD(&quotient, 0)=sign; // construct decFloat of sign
  629. return decFloatCopySign(result, result, &quotient);
  630. } // decDivide
  631. /* ------------------------------------------------------------------ */
  632. /* decFiniteMultiply -- multiply two finite decFloats */
  633. /* */
  634. /* num gets the result of multiplying dfl and dfr */
  635. /* bcdacc .. with the coefficient in this array */
  636. /* dfl is the first decFloat (lhs) */
  637. /* dfr is the second decFloat (rhs) */
  638. /* */
  639. /* This effects the multiplication of two decFloats, both known to be */
  640. /* finite, leaving the result in a bcdnum ready for decFinalize (for */
  641. /* use in Multiply) or in a following addition (FMA). */
  642. /* */
  643. /* bcdacc must have space for at least DECPMAX9*18+1 bytes. */
  644. /* No error is possible and no status is set. */
  645. /* ------------------------------------------------------------------ */
  646. // This routine has two separate implementations of the core
  647. // multiplication; both using base-billion. One uses only 32-bit
  648. // variables (Ints and uInts) or smaller; the other uses uLongs (for
  649. // multiplication and addition only). Both implementations cover
  650. // both arithmetic sizes (DOUBLE and QUAD) in order to allow timing
  651. // comparisons. In any one compilation only one implementation for
  652. // each size can be used, and if DECUSE64 is 0 then use of the 32-bit
  653. // version is forced.
  654. //
  655. // Historical note: an earlier version of this code also supported the
  656. // 256-bit format and has been preserved. That is somewhat trickier
  657. // during lazy carry splitting because the initial quotient estimate
  658. // (est) can exceed 32 bits.
  659. #define MULTBASE ((uInt)BILLION) // the base used for multiply
  660. #define MULOPLEN DECPMAX9 // operand length ('digits' base 10**9)
  661. #define MULACCLEN (MULOPLEN*2) // accumulator length (ditto)
  662. #define LEADZEROS (MULACCLEN*9 - DECPMAX*2) // leading zeros always
  663. // Assertions: exponent not too large and MULACCLEN is a multiple of 4
  664. #if DECEMAXD>9
  665. #error Exponent may overflow when doubled for Multiply
  666. #endif
  667. #if MULACCLEN!=(MULACCLEN/4)*4
  668. // This assumption is used below only for initialization
  669. #error MULACCLEN is not a multiple of 4
  670. #endif
  671. static void decFiniteMultiply(bcdnum *num, uByte *bcdacc,
  672. const decFloat *dfl, const decFloat *dfr) {
  673. uInt bufl[MULOPLEN]; // left coefficient (base-billion)
  674. uInt bufr[MULOPLEN]; // right coefficient (base-billion)
  675. uInt *ui, *uj; // work
  676. uByte *ub; // ..
  677. uInt uiwork; // for macros
  678. #if DECUSE64
  679. uLong accl[MULACCLEN]; // lazy accumulator (base-billion+)
  680. uLong *pl; // work -> lazy accumulator
  681. uInt acc[MULACCLEN]; // coefficent in base-billion ..
  682. #else
  683. uInt acc[MULACCLEN*2]; // accumulator in base-billion ..
  684. #endif
  685. uInt *pa; // work -> accumulator
  686. //printf("Base10**9: OpLen=%d MulAcclen=%d\n", OPLEN, MULACCLEN);
  687. /* Calculate sign and exponent */
  688. num->sign=(DFWORD(dfl, 0)^DFWORD(dfr, 0)) & DECFLOAT_Sign;
  689. num->exponent=GETEXPUN(dfl)+GETEXPUN(dfr); // [see assertion above]
  690. /* Extract the coefficients and prepare the accumulator */
  691. // the coefficients of the operands are decoded into base-billion
  692. // numbers in uInt arrays (bufl and bufr, LSD at offset 0) of the
  693. // appropriate size.
  694. GETCOEFFBILL(dfl, bufl);
  695. GETCOEFFBILL(dfr, bufr);
  696. #if DECTRACE && 0
  697. printf("CoeffbL:");
  698. for (ui=bufl+MULOPLEN-1; ui>=bufl; ui--) printf(" %08lx", (LI)*ui);
  699. printf("\n");
  700. printf("CoeffbR:");
  701. for (uj=bufr+MULOPLEN-1; uj>=bufr; uj--) printf(" %08lx", (LI)*uj);
  702. printf("\n");
  703. #endif
  704. // start the 64-bit/32-bit differing paths...
  705. #if DECUSE64
  706. // zero the accumulator
  707. #if MULACCLEN==4
  708. accl[0]=0; accl[1]=0; accl[2]=0; accl[3]=0;
  709. #else // use a loop
  710. // MULACCLEN is a multiple of four, asserted above
  711. for (pl=accl; pl<accl+MULACCLEN; pl+=4) {
  712. *pl=0; *(pl+1)=0; *(pl+2)=0; *(pl+3)=0;// [reduce overhead]
  713. } // pl
  714. #endif
  715. /* Effect the multiplication */
  716. // The multiplcation proceeds using MFC's lazy-carry resolution
  717. // algorithm from decNumber. First, the multiplication is
  718. // effected, allowing accumulation of the partial products (which
  719. // are in base-billion at each column position) into 64 bits
  720. // without resolving back to base=billion after each addition.
  721. // These 64-bit numbers (which may contain up to 19 decimal digits)
  722. // are then split using the Clark & Cowlishaw algorithm (see below).
  723. // [Testing for 0 in the inner loop is not really a 'win']
  724. for (ui=bufr; ui<bufr+MULOPLEN; ui++) { // over each item in rhs
  725. if (*ui==0) continue; // product cannot affect result
  726. pl=accl+(ui-bufr); // where to add the lhs
  727. for (uj=bufl; uj<bufl+MULOPLEN; uj++, pl++) { // over each item in lhs
  728. // if (*uj==0) continue; // product cannot affect result
  729. *pl+=((uLong)*ui)*(*uj);
  730. } // uj
  731. } // ui
  732. // The 64-bit carries must now be resolved; this means that a
  733. // quotient/remainder has to be calculated for base-billion (1E+9).
  734. // For this, Clark & Cowlishaw's quotient estimation approach (also
  735. // used in decNumber) is needed, because 64-bit divide is generally
  736. // extremely slow on 32-bit machines, and may be slower than this
  737. // approach even on 64-bit machines. This algorithm splits X
  738. // using:
  739. //
  740. // magic=2**(A+B)/1E+9; // 'magic number'
  741. // hop=X/2**A; // high order part of X (by shift)
  742. // est=magic*hop/2**B // quotient estimate (may be low by 1)
  743. //
  744. // A and B are quite constrained; hop and magic must fit in 32 bits,
  745. // and 2**(A+B) must be as large as possible (which is 2**61 if
  746. // magic is to fit). Further, maxX increases with the length of
  747. // the operands (and hence the number of partial products
  748. // accumulated); maxX is OPLEN*(10**18), which is up to 19 digits.
  749. //
  750. // It can be shown that when OPLEN is 2 then the maximum error in
  751. // the estimated quotient is <1, but for larger maximum x the
  752. // maximum error is above 1 so a correction that is >1 may be
  753. // needed. Values of A and B are chosen to satisfy the constraints
  754. // just mentioned while minimizing the maximum error (and hence the
  755. // maximum correction), as shown in the following table:
  756. //
  757. // Type OPLEN A B maxX maxError maxCorrection
  758. // ---------------------------------------------------------
  759. // DOUBLE 2 29 32 <2*10**18 0.63 1
  760. // QUAD 4 30 31 <4*10**18 1.17 2
  761. //
  762. // In the OPLEN==2 case there is most choice, but the value for B
  763. // of 32 has a big advantage as then the calculation of the
  764. // estimate requires no shifting; the compiler can extract the high
  765. // word directly after multiplying magic*hop.
  766. #define MULMAGIC 2305843009U // 2**61/10**9 [both cases]
  767. #if DOUBLE
  768. #define MULSHIFTA 29
  769. #define MULSHIFTB 32
  770. #elif QUAD
  771. #define MULSHIFTA 30
  772. #define MULSHIFTB 31
  773. #else
  774. #error Unexpected type
  775. #endif
  776. #if DECTRACE
  777. printf("MulAccl:");
  778. for (pl=accl+MULACCLEN-1; pl>=accl; pl--)
  779. printf(" %08lx:%08lx", (LI)(*pl>>32), (LI)(*pl&0xffffffff));
  780. printf("\n");
  781. #endif
  782. for (pl=accl, pa=acc; pl<accl+MULACCLEN; pl++, pa++) { // each column position
  783. uInt lo, hop; // work
  784. uInt est; // cannot exceed 4E+9
  785. if (*pl>=MULTBASE) {
  786. // *pl holds a binary number which needs to be split
  787. hop=(uInt)(*pl>>MULSHIFTA);
  788. est=(uInt)(((uLong)hop*MULMAGIC)>>MULSHIFTB);
  789. // the estimate is now in est; now calculate hi:lo-est*10**9;
  790. // happily the top word of the result is irrelevant because it
  791. // will always be zero so this needs only one multiplication
  792. lo=(uInt)(*pl-((uLong)est*MULTBASE)); // low word of result
  793. // If QUAD, the correction here could be +2
  794. if (lo>=MULTBASE) {
  795. lo-=MULTBASE; // correct by +1
  796. est++;
  797. #if QUAD
  798. // may need to correct by +2
  799. if (lo>=MULTBASE) {
  800. lo-=MULTBASE;
  801. est++;
  802. }
  803. #endif
  804. }
  805. // finally place lo as the new coefficient 'digit' and add est to
  806. // the next place up [this is safe because this path is never
  807. // taken on the final iteration as *pl will fit]
  808. *pa=lo;
  809. *(pl+1)+=est;
  810. } // *pl needed split
  811. else { // *pl<MULTBASE
  812. *pa=(uInt)*pl; // just copy across
  813. }
  814. } // pl loop
  815. #else // 32-bit
  816. for (pa=acc;; pa+=4) { // zero the accumulator
  817. *pa=0; *(pa+1)=0; *(pa+2)=0; *(pa+3)=0; // [reduce overhead]
  818. if (pa==acc+MULACCLEN*2-4) break; // multiple of 4 asserted
  819. } // pa
  820. /* Effect the multiplication */
  821. // uLongs are not available (and in particular, there is no uLong
  822. // divide) but it is still possible to use MFC's lazy-carry
  823. // resolution algorithm from decNumber. First, the multiplication
  824. // is effected, allowing accumulation of the partial products
  825. // (which are in base-billion at each column position) into 64 bits
  826. // [with the high-order 32 bits in each position being held at
  827. // offset +ACCLEN from the low-order 32 bits in the accumulator].
  828. // These 64-bit numbers (which may contain up to 19 decimal digits)
  829. // are then split using the Clark & Cowlishaw algorithm (see
  830. // below).
  831. for (ui=bufr;; ui++) { // over each item in rhs
  832. uInt hi, lo; // words of exact multiply result
  833. pa=acc+(ui-bufr); // where to add the lhs
  834. for (uj=bufl;; uj++, pa++) { // over each item in lhs
  835. LONGMUL32HI(hi, *ui, *uj); // calculate product of digits
  836. lo=(*ui)*(*uj); // ..
  837. *pa+=lo; // accumulate low bits and ..
  838. *(pa+MULACCLEN)+=hi+(*pa<lo); // .. high bits with any carry
  839. if (uj==bufl+MULOPLEN-1) break;
  840. }
  841. if (ui==bufr+MULOPLEN-1) break;
  842. }
  843. // The 64-bit carries must now be resolved; this means that a
  844. // quotient/remainder has to be calculated for base-billion (1E+9).
  845. // For this, Clark & Cowlishaw's quotient estimation approach (also
  846. // used in decNumber) is needed, because 64-bit divide is generally
  847. // extremely slow on 32-bit machines. This algorithm splits X
  848. // using:
  849. //
  850. // magic=2**(A+B)/1E+9; // 'magic number'
  851. // hop=X/2**A; // high order part of X (by shift)
  852. // est=magic*hop/2**B // quotient estimate (may be low by 1)
  853. //
  854. // A and B are quite constrained; hop and magic must fit in 32 bits,
  855. // and 2**(A+B) must be as large as possible (which is 2**61 if
  856. // magic is to fit). Further, maxX increases with the length of
  857. // the operands (and hence the number of partial products
  858. // accumulated); maxX is OPLEN*(10**18), which is up to 19 digits.
  859. //
  860. // It can be shown that when OPLEN is 2 then the maximum error in
  861. // the estimated quotient is <1, but for larger maximum x the
  862. // maximum error is above 1 so a correction that is >1 may be
  863. // needed. Values of A and B are chosen to satisfy the constraints
  864. // just mentioned while minimizing the maximum error (and hence the
  865. // maximum correction), as shown in the following table:
  866. //
  867. // Type OPLEN A B maxX maxError maxCorrection
  868. // ---------------------------------------------------------
  869. // DOUBLE 2 29 32 <2*10**18 0.63 1
  870. // QUAD 4 30 31 <4*10**18 1.17 2
  871. //
  872. // In the OPLEN==2 case there is most choice, but the value for B
  873. // of 32 has a big advantage as then the calculation of the
  874. // estimate requires no shifting; the high word is simply
  875. // calculated from multiplying magic*hop.
  876. #define MULMAGIC 2305843009U // 2**61/10**9 [both cases]
  877. #if DOUBLE
  878. #define MULSHIFTA 29
  879. #define MULSHIFTB 32
  880. #elif QUAD
  881. #define MULSHIFTA 30
  882. #define MULSHIFTB 31
  883. #else
  884. #error Unexpected type
  885. #endif
  886. #if DECTRACE
  887. printf("MulHiLo:");
  888. for (pa=acc+MULACCLEN-1; pa>=acc; pa--)
  889. printf(" %08lx:%08lx", (LI)*(pa+MULACCLEN), (LI)*pa);
  890. printf("\n");
  891. #endif
  892. for (pa=acc;; pa++) { // each low uInt
  893. uInt hi, lo; // words of exact multiply result
  894. uInt hop, estlo; // work
  895. #if QUAD
  896. uInt esthi; // ..
  897. #endif
  898. lo=*pa;
  899. hi=*(pa+MULACCLEN); // top 32 bits
  900. // hi and lo now hold a binary number which needs to be split
  901. #if DOUBLE
  902. hop=(hi<<3)+(lo>>MULSHIFTA); // hi:lo/2**29
  903. LONGMUL32HI(estlo, hop, MULMAGIC);// only need the high word
  904. // [MULSHIFTB is 32, so estlo can be used directly]
  905. // the estimate is now in estlo; now calculate hi:lo-est*10**9;
  906. // happily the top word of the result is irrelevant because it
  907. // will always be zero so this needs only one multiplication
  908. lo-=(estlo*MULTBASE);
  909. // esthi=0; // high word is ignored below
  910. // the correction here will be at most +1; do it
  911. if (lo>=MULTBASE) {
  912. lo-=MULTBASE;
  913. estlo++;
  914. }
  915. #elif QUAD
  916. hop=(hi<<2)+(lo>>MULSHIFTA); // hi:lo/2**30
  917. LONGMUL32HI(esthi, hop, MULMAGIC);// shift will be 31 ..
  918. estlo=hop*MULMAGIC; // .. so low word needed
  919. estlo=(esthi<<1)+(estlo>>MULSHIFTB); // [just the top bit]
  920. // esthi=0; // high word is ignored below
  921. lo-=(estlo*MULTBASE); // as above
  922. // the correction here could be +1 or +2
  923. if (lo>=MULTBASE) {
  924. lo-=MULTBASE;
  925. estlo++;
  926. }
  927. if (lo>=MULTBASE) {
  928. lo-=MULTBASE;
  929. estlo++;
  930. }
  931. #else
  932. #error Unexpected type
  933. #endif
  934. // finally place lo as the new accumulator digit and add est to
  935. // the next place up; this latter add could cause a carry of 1
  936. // to the high word of the next place
  937. *pa=lo;
  938. *(pa+1)+=estlo;
  939. // esthi is always 0 for DOUBLE and QUAD so this is skipped
  940. // *(pa+1+MULACCLEN)+=esthi;
  941. if (*(pa+1)<estlo) *(pa+1+MULACCLEN)+=1; // carry
  942. if (pa==acc+MULACCLEN-2) break; // [MULACCLEN-1 will never need split]
  943. } // pa loop
  944. #endif
  945. // At this point, whether using the 64-bit or the 32-bit paths, the
  946. // accumulator now holds the (unrounded) result in base-billion;
  947. // one base-billion 'digit' per uInt.
  948. #if DECTRACE
  949. printf("MultAcc:");
  950. for (pa=acc+MULACCLEN-1; pa>=acc; pa--) printf(" %09ld", (LI)*pa);
  951. printf("\n");
  952. #endif
  953. // Now convert to BCD for rounding and cleanup, starting from the
  954. // most significant end
  955. pa=acc+MULACCLEN-1;
  956. if (*pa!=0) num->msd=bcdacc+LEADZEROS;// drop known lead zeros
  957. else { // >=1 word of leading zeros
  958. num->msd=bcdacc; // known leading zeros are gone
  959. pa--; // skip first word ..
  960. for (; *pa==0; pa--) if (pa==acc) break; // .. and any more leading 0s
  961. }
  962. for (ub=bcdacc;; pa--, ub+=9) {
  963. if (*pa!=0) { // split(s) needed
  964. uInt top, mid, rem; // work
  965. // *pa is non-zero -- split the base-billion acc digit into
  966. // hi, mid, and low three-digits
  967. #define mulsplit9 1000000 // divisor
  968. #define mulsplit6 1000 // divisor
  969. // The splitting is done by simple divides and remainders,
  970. // assuming the compiler will optimize these where useful
  971. // [GCC does]
  972. top=*pa/mulsplit9;
  973. rem=*pa%mulsplit9;
  974. mid=rem/mulsplit6;
  975. rem=rem%mulsplit6;
  976. // lay out the nine BCD digits (plus one unwanted byte)
  977. UBFROMUI(ub, UBTOUI(&BIN2BCD8[top*4]));
  978. UBFROMUI(ub+3, UBTOUI(&BIN2BCD8[mid*4]));
  979. UBFROMUI(ub+6, UBTOUI(&BIN2BCD8[rem*4]));
  980. }
  981. else { // *pa==0
  982. UBFROMUI(ub, 0); // clear 9 BCD8s
  983. UBFROMUI(ub+4, 0); // ..
  984. *(ub+8)=0; // ..
  985. }
  986. if (pa==acc) break;
  987. } // BCD conversion loop
  988. num->lsd=ub+8; // complete the bcdnum ..
  989. #if DECTRACE
  990. decShowNum(num, "postmult");
  991. decFloatShow(dfl, "dfl");
  992. decFloatShow(dfr, "dfr");
  993. #endif
  994. return;
  995. } // decFiniteMultiply
  996. /* ------------------------------------------------------------------ */
  997. /* decFloatAbs -- absolute value, heeding NaNs, etc. */
  998. /* */
  999. /* result gets the canonicalized df with sign 0 */
  1000. /* df is the decFloat to abs */
  1001. /* set is the context */
  1002. /* returns result */
  1003. /* */
  1004. /* This has the same effect as decFloatPlus unless df is negative, */
  1005. /* in which case it has the same effect as decFloatMinus. The */
  1006. /* effect is also the same as decFloatCopyAbs except that NaNs are */
  1007. /* handled normally (the sign of a NaN is not affected, and an sNaN */
  1008. /* will signal) and the result will be canonical. */
  1009. /* ------------------------------------------------------------------ */
  1010. decFloat * decFloatAbs(decFloat *result, const decFloat *df,
  1011. decContext *set) {
  1012. if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  1013. decCanonical(result, df); // copy and check
  1014. DFBYTE(result, 0)&=~0x80; // zero sign bit
  1015. return result;
  1016. } // decFloatAbs
  1017. /* ------------------------------------------------------------------ */
  1018. /* decFloatAdd -- add two decFloats */
  1019. /* */
  1020. /* result gets the result of adding dfl and dfr: */
  1021. /* dfl is the first decFloat (lhs) */
  1022. /* dfr is the second decFloat (rhs) */
  1023. /* set is the context */
  1024. /* returns result */
  1025. /* */
  1026. /* ------------------------------------------------------------------ */
  1027. #if QUAD
  1028. // Table for testing MSDs for fastpath elimination; returns the MSD of
  1029. // a decDouble or decQuad (top 6 bits tested) ignoring the sign.
  1030. // Infinities return -32 and NaNs return -128 so that summing the two
  1031. // MSDs also allows rapid tests for the Specials (see code below).
  1032. const Int DECTESTMSD[64]={
  1033. 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
  1034. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128,
  1035. 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
  1036. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, -32, -128};
  1037. #else
  1038. // The table for testing MSDs is shared between the modules
  1039. extern const Int DECTESTMSD[64];
  1040. #endif
  1041. decFloat * decFloatAdd(decFloat *result,
  1042. const decFloat *dfl, const decFloat *dfr,
  1043. decContext *set) {
  1044. bcdnum num; // for final conversion
  1045. Int bexpl, bexpr; // left and right biased exponents
  1046. uByte *ub, *us, *ut; // work
  1047. uInt uiwork; // for macros
  1048. #if QUAD
  1049. uShort uswork; // ..
  1050. #endif
  1051. uInt sourhil, sourhir; // top words from source decFloats
  1052. // [valid only through end of
  1053. // fastpath code -- before swap]
  1054. uInt diffsign; // non-zero if signs differ
  1055. uInt carry; // carry: 0 or 1 before add loop
  1056. Int overlap; // coefficient overlap (if full)
  1057. Int summ; // sum of the MSDs
  1058. // the following buffers hold coefficients with various alignments
  1059. // (see commentary and diagrams below)
  1060. uByte acc[4+2+DECPMAX*3+8];
  1061. uByte buf[4+2+DECPMAX*2];
  1062. uByte *umsd, *ulsd; // local MSD and LSD pointers
  1063. #if DECLITEND
  1064. #define CARRYPAT 0x01000000 // carry=1 pattern
  1065. #else
  1066. #define CARRYPAT 0x00000001 // carry=1 pattern
  1067. #endif
  1068. /* Start decoding the arguments */
  1069. // The initial exponents are placed into the opposite Ints to
  1070. // that which might be expected; there are two sets of data to
  1071. // keep track of (each decFloat and the corresponding exponent),
  1072. // and this scheme means that at the swap point (after comparing
  1073. // exponents) only one pair of words needs to be swapped
  1074. // whichever path is taken (thereby minimising worst-case path).
  1075. // The calculated exponents will be nonsense when the arguments are
  1076. // Special, but are not used in that path
  1077. sourhil=DFWORD(dfl, 0); // LHS top word
  1078. summ=DECTESTMSD[sourhil>>26]; // get first MSD for testing
  1079. bexpr=DECCOMBEXP[sourhil>>26]; // get exponent high bits (in place)
  1080. bexpr+=GETECON(dfl); // .. + continuation
  1081. sourhir=DFWORD(dfr, 0); // RHS top word
  1082. summ+=DECTESTMSD[sourhir>>26]; // sum MSDs for testing
  1083. bexpl=DECCOMBEXP[sourhir>>26];
  1084. bexpl+=GETECON(dfr);
  1085. // here bexpr has biased exponent from lhs, and vice versa
  1086. diffsign=(sourhil^sourhir)&DECFLOAT_Sign;
  1087. // now determine whether to take a fast path or the full-function
  1088. // slow path. The slow path must be taken when:
  1089. // -- both numbers are finite, and:
  1090. // the exponents are different, or
  1091. // the signs are different, or
  1092. // the sum of the MSDs is >8 (hence might overflow)
  1093. // specialness and the sum of the MSDs can be tested at once using
  1094. // the summ value just calculated, so the test for specials is no
  1095. // longer on the worst-case path (as of 3.60)
  1096. if (summ<=8) { // MSD+MSD is good, or there is a special
  1097. if (summ<0) { // there is a special
  1098. // Inf+Inf would give -64; Inf+finite is -32 or higher
  1099. if (summ<-64) return decNaNs(result, dfl, dfr, set); // one or two NaNs
  1100. // two infinities with different signs is invalid
  1101. if (summ==-64 && diffsign) return decInvalid(result, set);
  1102. if (DFISINF(dfl)) return decInfinity(result, dfl); // LHS is infinite
  1103. return decInfinity(result, dfr); // RHS must be Inf
  1104. }
  1105. // Here when both arguments are finite; fast path is possible
  1106. // (currently only for aligned and same-sign)
  1107. if (bexpr==bexpl && !diffsign) {
  1108. uInt tac[DECLETS+1]; // base-1000 coefficient
  1109. uInt encode; // work
  1110. // Get one coefficient as base-1000 and add the other
  1111. GETCOEFFTHOU(dfl, tac); // least-significant goes to [0]
  1112. ADDCOEFFTHOU(dfr, tac);
  1113. // here the sum of the MSDs (plus any carry) will be <10 due to
  1114. // the fastpath test earlier
  1115. // construct the result; low word is the same for both formats
  1116. encode =BIN2DPD[tac[0]];
  1117. encode|=BIN2DPD[tac[1]]<<10;
  1118. encode|=BIN2DPD[tac[2]]<<20;
  1119. encode|=BIN2DPD[tac[3]]<<30;
  1120. DFWORD(result, (DECBYTES/4)-1)=encode;
  1121. // collect next two declets (all that remains, for Double)
  1122. encode =BIN2DPD[tac[3]]>>2;
  1123. encode|=BIN2DPD[tac[4]]<<8;
  1124. #if QUAD
  1125. // complete and lay out middling words
  1126. encode|=BIN2DPD[tac[5]]<<18;
  1127. encode|=BIN2DPD[tac[6]]<<28;
  1128. DFWORD(result, 2)=encode;
  1129. encode =BIN2DPD[tac[6]]>>4;
  1130. encode|=BIN2DPD[tac[7]]<<6;
  1131. encode|=BIN2DPD[tac[8]]<<16;
  1132. encode|=BIN2DPD[tac[9]]<<26;
  1133. DFWORD(result, 1)=encode;
  1134. // and final two declets
  1135. encode =BIN2DPD[tac[9]]>>6;
  1136. encode|=BIN2DPD[tac[10]]<<4;
  1137. #endif
  1138. // add exponent continuation and sign (from either argument)
  1139. encode|=sourhil & (ECONMASK | DECFLOAT_Sign);
  1140. // create lookup index = MSD + top two bits of biased exponent <<4
  1141. tac[DECLETS]|=(bexpl>>DECECONL)<<4;
  1142. encode|=DECCOMBFROM[tac[DECLETS]]; // add constructed combination field
  1143. DFWORD(result, 0)=encode; // complete
  1144. // decFloatShow(result, ">");
  1145. return result;
  1146. } // fast path OK
  1147. // drop through to slow path
  1148. } // low sum or Special(s)
  1149. /* Slow path required -- arguments are finite and might overflow, */
  1150. /* or require alignment, or might have different signs */
  1151. // now swap either exponents or argument pointers
  1152. if (bexpl<=bexpr) {
  1153. // original left is bigger
  1154. Int bexpswap=bexpl;
  1155. bexpl=bexpr;
  1156. bexpr=bexpswap;
  1157. // printf("left bigger\n");
  1158. }
  1159. else {
  1160. const decFloat *dfswap=dfl;
  1161. dfl=dfr;
  1162. dfr=dfswap;
  1163. // printf("right bigger\n");
  1164. }
  1165. // [here dfl and bexpl refer to the datum with the larger exponent,
  1166. // of if the exponents are equal then the original LHS argument]
  1167. // if lhs is zero then result will be the rhs (now known to have
  1168. // the smaller exponent), which also may need to be tested for zero
  1169. // for the weird IEEE 754 sign rules
  1170. if (DFISZERO(dfl)) {
  1171. decCanonical(result, dfr); // clean copy
  1172. // "When the sum of two operands with opposite signs is
  1173. // exactly zero, the sign of that sum shall be '+' in all
  1174. // rounding modes except round toward -Infinity, in which
  1175. // mode that sign shall be '-'."
  1176. if (diffsign && DFISZERO(result)) {
  1177. DFWORD(result, 0)&=~DECFLOAT_Sign; // assume sign 0
  1178. if (set->round==DEC_ROUND_FLOOR) DFWORD(result, 0)|=DECFLOAT_Sign;
  1179. }
  1180. return result;
  1181. } // numfl is zero
  1182. // [here, LHS is non-zero; code below assumes that]
  1183. // Coefficients layout during the calculations to follow:
  1184. //
  1185. // Overlap case:
  1186. // +------------------------------------------------+
  1187. // acc: |0000| coeffa | tail B | |
  1188. // +------------------------------------------------+
  1189. // buf: |0000| pad0s | coeffb | |
  1190. // +------------------------------------------------+
  1191. //
  1192. // Touching coefficients or gap:
  1193. // +------------------------------------------------+
  1194. // acc: |0000| coeffa | gap | coeffb |
  1195. // +------------------------------------------------+
  1196. // [buf not used or needed; gap clamped to Pmax]
  1197. // lay out lhs coefficient into accumulator; this starts at acc+4
  1198. // for decDouble or acc+6 for decQuad so the LSD is word-
  1199. // aligned; the top word gap is there only in case a carry digit
  1200. // is prefixed after the add -- it does not need to be zeroed
  1201. #if DOUBLE
  1202. #define COFF 4 // offset into acc
  1203. #elif QUAD
  1204. UBFROMUS(acc+4, 0); // prefix 00
  1205. #define COFF 6 // offset into acc
  1206. #endif
  1207. GETCOEFF(dfl, acc+COFF); // decode from decFloat
  1208. ulsd=acc+COFF+DECPMAX-1;
  1209. umsd=acc+4; // [having this here avoids
  1210. #if DECTRACE
  1211. {bcdnum tum;
  1212. tum.msd=umsd;
  1213. tum.lsd=ulsd;
  1214. tum.exponent=bexpl-DECBIAS;
  1215. tum.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
  1216. decShowNum(&tum, "dflx");}
  1217. #endif
  1218. // if signs differ, take ten's complement of lhs (here the
  1219. // coefficient is subtracted from all-nines; the 1 is added during
  1220. // the later add cycle -- zeros to the right do not matter because
  1221. // the complement of zero is zero); these are fixed-length inverts
  1222. // where the lsd is known to be at a 4-byte boundary (so no borrow
  1223. // possible)
  1224. carry=0; // assume no carry
  1225. if (diffsign) {
  1226. carry=CARRYPAT; // for +1 during add
  1227. UBFROMUI(acc+ 4, 0x09090909-UBTOUI(acc+ 4));
  1228. UBFROMUI(acc+ 8, 0x09090909-UBTOUI(acc+ 8));
  1229. UBFROMUI(acc+12, 0x09090909-UBTOUI(acc+12));
  1230. UBFROMUI(acc+16, 0x09090909-UBTOUI(acc+16));
  1231. #if QUAD
  1232. UBFROMUI(acc+20, 0x09090909-UBTOUI(acc+20));
  1233. UBFROMUI(acc+24, 0x09090909-UBTOUI(acc+24));
  1234. UBFROMUI(acc+28, 0x09090909-UBTOUI(acc+28));
  1235. UBFROMUI(acc+32, 0x09090909-UBTOUI(acc+32));
  1236. UBFROMUI(acc+36, 0x09090909-UBTOUI(acc+36));
  1237. #endif
  1238. } // diffsign
  1239. // now process the rhs coefficient; if it cannot overlap lhs then
  1240. // it can be put straight into acc (with an appropriate gap, if
  1241. // needed) because no actual addition will be needed (except
  1242. // possibly to complete ten's complement)
  1243. overlap=DECPMAX-(bexpl-bexpr);
  1244. #if DECTRACE
  1245. printf("exps: %ld %ld\n", (LI)(bexpl-DECBIAS), (LI)(bexpr-DECBIAS));
  1246. printf("Overlap=%ld carry=%08lx\n", (LI)overlap, (LI)carry);
  1247. #endif
  1248. if (overlap<=0) { // no overlap possible
  1249. uInt gap; // local work
  1250. // since a full addition is not needed, a ten's complement
  1251. // calculation started above may need to be completed
  1252. if (carry) {
  1253. for (ub=ulsd; *ub==9; ub--) *ub=0;
  1254. *ub+=1;
  1255. carry=0; // taken care of
  1256. }
  1257. // up to DECPMAX-1 digits of the final result can extend down
  1258. // below the LSD of the lhs, so if the gap is >DECPMAX then the
  1259. // rhs will be simply sticky bits. In this case the gap is
  1260. // clamped to DECPMAX and the exponent adjusted to suit [this is
  1261. // safe because the lhs is non-zero].
  1262. gap=-overlap;
  1263. if (gap>DECPMAX) {
  1264. bexpr+=gap-1;
  1265. gap=DECPMAX;
  1266. }
  1267. ub=ulsd+gap+1; // where MSD will go
  1268. // Fill the gap with 0s; note that there is no addition to do
  1269. ut=acc+COFF+DECPMAX; // start of gap
  1270. for (; ut<ub; ut+=4) UBFROMUI(ut, 0); // mind the gap
  1271. if (overlap<-DECPMAX) { // gap was > DECPMAX
  1272. *ub=(uByte)(!DFISZERO(dfr)); // make sticky digit
  1273. }
  1274. else { // need full coefficient
  1275. GETCOEFF(dfr, ub); // decode from decFloat
  1276. ub+=DECPMAX-1; // new LSD...
  1277. }
  1278. ulsd=ub; // save new LSD
  1279. } // no overlap possible
  1280. else { // overlap>0
  1281. // coefficients overlap (perhaps completely, although also
  1282. // perhaps only where zeros)
  1283. if (overlap==DECPMAX) { // aligned
  1284. ub=buf+COFF; // where msd will go
  1285. #if QUAD
  1286. UBFROMUS(buf+4, 0); // clear quad's 00
  1287. #endif
  1288. GETCOEFF(dfr, ub); // decode from decFloat
  1289. }
  1290. else { // unaligned
  1291. ub=buf+COFF+DECPMAX-overlap; // where MSD will go
  1292. // Fill the prefix gap with 0s; 8 will cover most common
  1293. // unalignments, so start with direct assignments (a loop is
  1294. // then used for any remaining -- the loop (and the one in a
  1295. // moment) is not then on the critical path because the number
  1296. // of additions is reduced by (at least) two in this case)
  1297. UBFROMUI(buf+4, 0); // [clears decQuad 00 too]
  1298. UBFROMUI(buf+8, 0);
  1299. if (ub>buf+12) {
  1300. ut=buf+12; // start any remaining
  1301. for (; ut<ub; ut+=4) UBFROMUI(ut, 0); // fill them
  1302. }
  1303. GETCOEFF(dfr, ub); // decode from decFloat
  1304. // now move tail of rhs across to main acc; again use direct
  1305. // copies for 8 digits-worth
  1306. UBFROMUI(acc+COFF+DECPMAX, UBTOUI(buf+COFF+DECPMAX));
  1307. UBFROMUI(acc+COFF+DECPMAX+4, UBTOUI(buf+COFF+DECPMAX+4));
  1308. if (buf+COFF+DECPMAX+8<ub+DECPMAX) {
  1309. us=buf+COFF+DECPMAX+8; // source
  1310. ut=acc+COFF+DECPMAX+8; // target
  1311. for (; us<ub+DECPMAX; us+=4, ut+=4) UBFROMUI(ut, UBTOUI(us));
  1312. }
  1313. } // unaligned
  1314. ulsd=acc+(ub-buf+DECPMAX-1); // update LSD pointer
  1315. // Now do the add of the non-tail; this is all nicely aligned,
  1316. // and is over a multiple of four digits (because for Quad two
  1317. // zero digits were added on the left); words in both acc and
  1318. // buf (buf especially) will often be zero
  1319. // [byte-by-byte add, here, is about 15% slower total effect than
  1320. // the by-fours]
  1321. // Now effect the add; this is harder on a little-endian
  1322. // machine as the inter-digit carry cannot use the usual BCD
  1323. // addition trick because the bytes are loaded in the wrong order
  1324. // [this loop could be unrolled, but probably scarcely worth it]
  1325. ut=acc+COFF+DECPMAX-4; // target LSW (acc)
  1326. us=buf+COFF+DECPMAX-4; // source LSW (buf, to add to acc)
  1327. #if !DECLITEND
  1328. for (; ut>=acc+4; ut-=4, us-=4) { // big-endian add loop
  1329. // bcd8 add
  1330. carry+=UBTOUI(us); // rhs + carry
  1331. if (carry==0) continue; // no-op
  1332. carry+=UBTOUI(ut); // lhs
  1333. // Big-endian BCD adjust (uses internal carry)
  1334. carry+=0x76f6f6f6; // note top nibble not all bits
  1335. // apply BCD adjust and save
  1336. UBFROMUI(ut, (carry & 0x0f0f0f0f) - ((carry & 0x60606060)>>4));
  1337. carry>>=31; // true carry was at far left
  1338. } // add loop
  1339. #else
  1340. for (; ut>=acc+4; ut-=4, us-=4) { // little-endian add loop
  1341. // bcd8 add
  1342. carry+=UBTOUI(us); // rhs + carry
  1343. if (carry==0) continue; // no-op [common if unaligned]
  1344. carry+=UBTOUI(ut); // lhs
  1345. // Little-endian BCD adjust; inter-digit carry must be manual
  1346. // because the lsb from the array will be in the most-significant
  1347. // byte of carry
  1348. carry+=0x76767676; // note no inter-byte carries
  1349. carry+=(carry & 0x80000000)>>15;
  1350. carry+=(carry & 0x00800000)>>15;
  1351. carry+=(carry & 0x00008000)>>15;
  1352. carry-=(carry & 0x60606060)>>4; // BCD adjust back
  1353. UBFROMUI(ut, carry & 0x0f0f0f0f); // clear debris and save
  1354. // here, final carry-out bit is at 0x00000080; move it ready
  1355. // for next word-add (i.e., to 0x01000000)
  1356. carry=(carry & 0x00000080)<<17;
  1357. } // add loop
  1358. #endif
  1359. #if DECTRACE
  1360. {bcdnum tum;
  1361. printf("Add done, carry=%08lx, diffsign=%ld\n", (LI)carry, (LI)diffsign);
  1362. tum.msd=umsd; // acc+4;
  1363. tum.lsd=ulsd;
  1364. tum.exponent=0;
  1365. tum.sign=0;
  1366. decShowNum(&tum, "dfadd");}
  1367. #endif
  1368. } // overlap possible
  1369. // ordering here is a little strange in order to have slowest path
  1370. // first in GCC asm listing
  1371. if (diffsign) { // subtraction
  1372. if (!carry) { // no carry out means RHS<LHS
  1373. // borrowed -- take ten's complement
  1374. // sign is lhs sign
  1375. num.sign=DFWORD(dfl, 0) & DECFLOAT_Sign;
  1376. // invert the coefficient first by fours, then add one; space
  1377. // at the end of the buffer ensures the by-fours is always
  1378. // safe, but lsd+1 must be cleared to prevent a borrow
  1379. // if big-endian
  1380. #if !DECLITEND
  1381. *(ulsd+1)=0;
  1382. #endif
  1383. // there are always at least four coefficient words
  1384. UBFROMUI(umsd, 0x09090909-UBTOUI(umsd));
  1385. UBFROMUI(umsd+4, 0x09090909-UBTOUI(umsd+4));
  1386. UBFROMUI(umsd+8, 0x09090909-UBTOUI(umsd+8));
  1387. UBFROMUI(umsd+12, 0x09090909-UBTOUI(umsd+12));
  1388. #if DOUBLE
  1389. #define BNEXT 16
  1390. #elif QUAD
  1391. UBFROMUI(umsd+16, 0x09090909-UBTOUI(umsd+16));
  1392. UBFROMUI(umsd+20, 0x09090909-UBTOUI(umsd+20));
  1393. UBFROMUI(umsd+24, 0x09090909-UBTOUI(umsd+24));
  1394. UBFROMUI(umsd+28, 0x09090909-UBTOUI(umsd+28));
  1395. UBFROMUI(umsd+32, 0x09090909-UBTOUI(umsd+32));
  1396. #define BNEXT 36
  1397. #endif
  1398. if (ulsd>=umsd+BNEXT) { // unaligned
  1399. // eight will handle most unaligments for Double; 16 for Quad
  1400. UBFROMUI(umsd+BNEXT, 0x09090909-UBTOUI(umsd+BNEXT));
  1401. UBFROMUI(umsd+BNEXT+4, 0x09090909-UBTOUI(umsd+BNEXT+4));
  1402. #if DOUBLE
  1403. #define BNEXTY (BNEXT+8)
  1404. #elif QUAD
  1405. UBFROMUI(umsd+BNEXT+8, 0x09090909-UBTOUI(umsd+BNEXT+8));
  1406. UBFROMUI(umsd+BNEXT+12, 0x09090909-UBTOUI(umsd+BNEXT+12));
  1407. #define BNEXTY (BNEXT+16)
  1408. #endif
  1409. if (ulsd>=umsd+BNEXTY) { // very unaligned
  1410. ut=umsd+BNEXTY; // -> continue
  1411. for (;;ut+=4) {
  1412. UBFROMUI(ut, 0x09090909-UBTOUI(ut)); // invert four digits
  1413. if (ut>=ulsd-3) break; // all done
  1414. }
  1415. }
  1416. }
  1417. // complete the ten's complement by adding 1
  1418. for (ub=ulsd; *ub==9; ub--) *ub=0;
  1419. *ub+=1;
  1420. } // borrowed
  1421. else { // carry out means RHS>=LHS
  1422. num.sign=DFWORD(dfr, 0) & DECFLOAT_Sign;
  1423. // all done except for the special IEEE 754 exact-zero-result
  1424. // rule (see above); while testing for zero, strip leading
  1425. // zeros (which will save decFinalize doing it) (this is in
  1426. // diffsign path, so carry impossible and true umsd is
  1427. // acc+COFF)
  1428. // Check the initial coefficient area using the fast macro;
  1429. // this will often be all that needs to be done (as on the
  1430. // worst-case path when the subtraction was aligned and
  1431. // full-length)
  1432. if (ISCOEFFZERO(acc+COFF)) {
  1433. umsd=acc+COFF+DECPMAX-1; // so far, so zero
  1434. if (ulsd>umsd) { // more to check
  1435. umsd++; // to align after checked area
  1436. for (; UBTOUI(umsd)==0 && umsd+3<ulsd;) umsd+=4;
  1437. for (; *umsd==0 && umsd<ulsd;) umsd++;
  1438. }
  1439. if (*umsd==0) { // must be true zero (and diffsign)
  1440. num.sign=0; // assume +
  1441. if (set->round==DEC_ROUND_FLOOR) num.sign=DECFLOAT_Sign;
  1442. }
  1443. }
  1444. // [else was not zero, might still have leading zeros]
  1445. } // subtraction gave positive result
  1446. } // diffsign
  1447. else { // same-sign addition
  1448. num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
  1449. #if DOUBLE
  1450. if (carry) { // only possible with decDouble
  1451. *(acc+3)=1; // [Quad has leading 00]
  1452. umsd=acc+3;
  1453. }
  1454. #endif
  1455. } // same sign
  1456. num.msd=umsd; // set MSD ..
  1457. num.lsd=ulsd; // .. and LSD
  1458. num.exponent=bexpr-DECBIAS; // set exponent to smaller, unbiassed
  1459. #if DECTRACE
  1460. decFloatShow(dfl, "dfl");
  1461. decFloatShow(dfr, "dfr");
  1462. decShowNum(&num, "postadd");
  1463. #endif
  1464. return decFinalize(result, &num, set); // round, check, and lay out
  1465. } // decFloatAdd
  1466. /* ------------------------------------------------------------------ */
  1467. /* decFloatAnd -- logical digitwise AND of two decFloats */
  1468. /* */
  1469. /* result gets the result of ANDing dfl and dfr */
  1470. /* dfl is the first decFloat (lhs) */
  1471. /* dfr is the second decFloat (rhs) */
  1472. /* set is the context */
  1473. /* returns result, which will be canonical with sign=0 */
  1474. /* */
  1475. /* The operands must be positive, finite with exponent q=0, and */
  1476. /* comprise just zeros and ones; if not, Invalid operation results. */
  1477. /* ------------------------------------------------------------------ */
  1478. decFloat * decFloatAnd(decFloat *result,
  1479. const decFloat *dfl, const decFloat *dfr,
  1480. decContext *set) {
  1481. if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
  1482. || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set);
  1483. // the operands are positive finite integers (q=0) with just 0s and 1s
  1484. #if DOUBLE
  1485. DFWORD(result, 0)=ZEROWORD
  1486. |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04009124);
  1487. DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x49124491;
  1488. #elif QUAD
  1489. DFWORD(result, 0)=ZEROWORD
  1490. |((DFWORD(dfl, 0) & DFWORD(dfr, 0))&0x04000912);
  1491. DFWORD(result, 1)=(DFWORD(dfl, 1) & DFWORD(dfr, 1))&0x44912449;
  1492. DFWORD(result, 2)=(DFWORD(dfl, 2) & DFWORD(dfr, 2))&0x12449124;
  1493. DFWORD(result, 3)=(DFWORD(dfl, 3) & DFWORD(dfr, 3))&0x49124491;
  1494. #endif
  1495. return result;
  1496. } // decFloatAnd
  1497. /* ------------------------------------------------------------------ */
  1498. /* decFloatCanonical -- copy a decFloat, making canonical */
  1499. /* */
  1500. /* result gets the canonicalized df */
  1501. /* df is the decFloat to copy and make canonical */
  1502. /* returns result */
  1503. /* */
  1504. /* This works on specials, too; no error or exception is possible. */
  1505. /* ------------------------------------------------------------------ */
  1506. decFloat * decFloatCanonical(decFloat *result, const decFloat *df) {
  1507. return decCanonical(result, df);
  1508. } // decFloatCanonical
  1509. /* ------------------------------------------------------------------ */
  1510. /* decFloatClass -- return the class of a decFloat */
  1511. /* */
  1512. /* df is the decFloat to test */
  1513. /* returns the decClass that df falls into */
  1514. /* ------------------------------------------------------------------ */
  1515. enum decClass decFloatClass(const decFloat *df) {
  1516. Int exp; // exponent
  1517. if (DFISSPECIAL(df)) {
  1518. if (DFISQNAN(df)) return DEC_CLASS_QNAN;
  1519. if (DFISSNAN(df)) return DEC_CLASS_SNAN;
  1520. // must be an infinity
  1521. if (DFISSIGNED(df)) return DEC_CLASS_NEG_INF;
  1522. return DEC_CLASS_POS_INF;
  1523. }
  1524. if (DFISZERO(df)) { // quite common
  1525. if (DFISSIGNED(df)) return DEC_CLASS_NEG_ZERO;
  1526. return DEC_CLASS_POS_ZERO;
  1527. }
  1528. // is finite and non-zero; similar code to decFloatIsNormal, here
  1529. // [this could be speeded up slightly by in-lining decFloatDigits]
  1530. exp=GETEXPUN(df) // get unbiased exponent ..
  1531. +decFloatDigits(df)-1; // .. and make adjusted exponent
  1532. if (exp>=DECEMIN) { // is normal
  1533. if (DFISSIGNED(df)) return DEC_CLASS_NEG_NORMAL;
  1534. return DEC_CLASS_POS_NORMAL;
  1535. }
  1536. // is subnormal
  1537. if (DFISSIGNED(df)) return DEC_CLASS_NEG_SUBNORMAL;
  1538. return DEC_CLASS_POS_SUBNORMAL;
  1539. } // decFloatClass
  1540. /* ------------------------------------------------------------------ */
  1541. /* decFloatClassString -- return the class of a decFloat as a string */
  1542. /* */
  1543. /* df is the decFloat to test */
  1544. /* returns a constant string describing the class df falls into */
  1545. /* ------------------------------------------------------------------ */
  1546. const char *decFloatClassString(const decFloat *df) {
  1547. enum decClass eclass=decFloatClass(df);
  1548. if (eclass==DEC_CLASS_POS_NORMAL) return DEC_ClassString_PN;
  1549. if (eclass==DEC_CLASS_NEG_NORMAL) return DEC_ClassString_NN;
  1550. if (eclass==DEC_CLASS_POS_ZERO) return DEC_ClassString_PZ;
  1551. if (eclass==DEC_CLASS_NEG_ZERO) return DEC_ClassString_NZ;
  1552. if (eclass==DEC_CLASS_POS_SUBNORMAL) return DEC_ClassString_PS;
  1553. if (eclass==DEC_CLASS_NEG_SUBNORMAL) return DEC_ClassString_NS;
  1554. if (eclass==DEC_CLASS_POS_INF) return DEC_ClassString_PI;
  1555. if (eclass==DEC_CLASS_NEG_INF) return DEC_ClassString_NI;
  1556. if (eclass==DEC_CLASS_QNAN) return DEC_ClassString_QN;
  1557. if (eclass==DEC_CLASS_SNAN) return DEC_ClassString_SN;
  1558. return DEC_ClassString_UN; // Unknown
  1559. } // decFloatClassString
  1560. /* ------------------------------------------------------------------ */
  1561. /* decFloatCompare -- compare two decFloats; quiet NaNs allowed */
  1562. /* */
  1563. /* result gets the result of comparing dfl and dfr */
  1564. /* dfl is the first decFloat (lhs) */
  1565. /* dfr is the second decFloat (rhs) */
  1566. /* set is the context */
  1567. /* returns result, which may be -1, 0, 1, or NaN (Unordered) */
  1568. /* ------------------------------------------------------------------ */
  1569. decFloat * decFloatCompare(decFloat *result,
  1570. const decFloat *dfl, const decFloat *dfr,
  1571. decContext *set) {
  1572. Int comp; // work
  1573. // NaNs are handled as usual
  1574. if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  1575. // numeric comparison needed
  1576. comp=decNumCompare(dfl, dfr, 0);
  1577. decFloatZero(result);
  1578. if (comp==0) return result;
  1579. DFBYTE(result, DECBYTES-1)=0x01; // LSD=1
  1580. if (comp<0) DFBYTE(result, 0)|=0x80; // set sign bit
  1581. return result;
  1582. } // decFloatCompare
  1583. /* ------------------------------------------------------------------ */
  1584. /* decFloatCompareSignal -- compare two decFloats; all NaNs signal */
  1585. /* */
  1586. /* result gets the result of comparing dfl and dfr */
  1587. /* dfl is the first decFloat (lhs) */
  1588. /* dfr is the second decFloat (rhs) */
  1589. /* set is the context */
  1590. /* returns result, which may be -1, 0, 1, or NaN (Unordered) */
  1591. /* ------------------------------------------------------------------ */
  1592. decFloat * decFloatCompareSignal(decFloat *result,
  1593. const decFloat *dfl, const decFloat *dfr,
  1594. decContext *set) {
  1595. Int comp; // work
  1596. // NaNs are handled as usual, except that all NaNs signal
  1597. if (DFISNAN(dfl) || DFISNAN(dfr)) {
  1598. set->status|=DEC_Invalid_operation;
  1599. return decNaNs(result, dfl, dfr, set);
  1600. }
  1601. // numeric comparison needed
  1602. comp=decNumCompare(dfl, dfr, 0);
  1603. decFloatZero(result);
  1604. if (comp==0) return result;
  1605. DFBYTE(result, DECBYTES-1)=0x01; // LSD=1
  1606. if (comp<0) DFBYTE(result, 0)|=0x80; // set sign bit
  1607. return result;
  1608. } // decFloatCompareSignal
  1609. /* ------------------------------------------------------------------ */
  1610. /* decFloatCompareTotal -- compare two decFloats with total ordering */
  1611. /* */
  1612. /* result gets the result of comparing dfl and dfr */
  1613. /* dfl is the first decFloat (lhs) */
  1614. /* dfr is the second decFloat (rhs) */
  1615. /* returns result, which may be -1, 0, or 1 */
  1616. /* ------------------------------------------------------------------ */
  1617. decFloat * decFloatCompareTotal(decFloat *result,
  1618. const decFloat *dfl, const decFloat *dfr) {
  1619. Int comp; // work
  1620. uInt uiwork; // for macros
  1621. #if QUAD
  1622. uShort uswork; // ..
  1623. #endif
  1624. if (DFISNAN(dfl) || DFISNAN(dfr)) {
  1625. Int nanl, nanr; // work
  1626. // morph NaNs to +/- 1 or 2, leave numbers as 0
  1627. nanl=DFISSNAN(dfl)+DFISQNAN(dfl)*2; // quiet > signalling
  1628. if (DFISSIGNED(dfl)) nanl=-nanl;
  1629. nanr=DFISSNAN(dfr)+DFISQNAN(dfr)*2;
  1630. if (DFISSIGNED(dfr)) nanr=-nanr;
  1631. if (nanl>nanr) comp=+1;
  1632. else if (nanl<nanr) comp=-1;
  1633. else { // NaNs are the same type and sign .. must compare payload
  1634. // buffers need +2 for QUAD
  1635. uByte bufl[DECPMAX+4]; // for LHS coefficient + foot
  1636. uByte bufr[DECPMAX+4]; // for RHS coefficient + foot
  1637. uByte *ub, *uc; // work
  1638. Int sigl; // signum of LHS
  1639. sigl=(DFISSIGNED(dfl) ? -1 : +1);
  1640. // decode the coefficients
  1641. // (shift both right two if Quad to make a multiple of four)
  1642. #if QUAD
  1643. UBFROMUS(bufl, 0);
  1644. UBFROMUS(bufr, 0);
  1645. #endif
  1646. GETCOEFF(dfl, bufl+QUAD*2); // decode from decFloat
  1647. GETCOEFF(dfr, bufr+QUAD*2); // ..
  1648. // all multiples of four, here
  1649. comp=0; // assume equal
  1650. for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
  1651. uInt ui=UBTOUI(ub);
  1652. if (ui==UBTOUI(uc)) continue; // so far so same
  1653. // about to find a winner; go by bytes in case little-endian
  1654. for (;; ub++, uc++) {
  1655. if (*ub==*uc) continue;
  1656. if (*ub>*uc) comp=sigl; // difference found
  1657. else comp=-sigl; // ..
  1658. break;
  1659. }
  1660. }
  1661. } // same NaN type and sign
  1662. }
  1663. else {
  1664. // numeric comparison needed
  1665. comp=decNumCompare(dfl, dfr, 1); // total ordering
  1666. }
  1667. decFloatZero(result);
  1668. if (comp==0) return result;
  1669. DFBYTE(result, DECBYTES-1)=0x01; // LSD=1
  1670. if (comp<0) DFBYTE(result, 0)|=0x80; // set sign bit
  1671. return result;
  1672. } // decFloatCompareTotal
  1673. /* ------------------------------------------------------------------ */
  1674. /* decFloatCompareTotalMag -- compare magnitudes with total ordering */
  1675. /* */
  1676. /* result gets the result of comparing abs(dfl) and abs(dfr) */
  1677. /* dfl is the first decFloat (lhs) */
  1678. /* dfr is the second decFloat (rhs) */
  1679. /* returns result, which may be -1, 0, or 1 */
  1680. /* ------------------------------------------------------------------ */
  1681. decFloat * decFloatCompareTotalMag(decFloat *result,
  1682. const decFloat *dfl, const decFloat *dfr) {
  1683. decFloat a, b; // for copy if needed
  1684. // copy and redirect signed operand(s)
  1685. if (DFISSIGNED(dfl)) {
  1686. decFloatCopyAbs(&a, dfl);
  1687. dfl=&a;
  1688. }
  1689. if (DFISSIGNED(dfr)) {
  1690. decFloatCopyAbs(&b, dfr);
  1691. dfr=&b;
  1692. }
  1693. return decFloatCompareTotal(result, dfl, dfr);
  1694. } // decFloatCompareTotalMag
  1695. /* ------------------------------------------------------------------ */
  1696. /* decFloatCopy -- copy a decFloat as-is */
  1697. /* */
  1698. /* result gets the copy of dfl */
  1699. /* dfl is the decFloat to copy */
  1700. /* returns result */
  1701. /* */
  1702. /* This is a bitwise operation; no errors or exceptions are possible. */
  1703. /* ------------------------------------------------------------------ */
  1704. decFloat * decFloatCopy(decFloat *result, const decFloat *dfl) {
  1705. if (dfl!=result) *result=*dfl; // copy needed
  1706. return result;
  1707. } // decFloatCopy
  1708. /* ------------------------------------------------------------------ */
  1709. /* decFloatCopyAbs -- copy a decFloat as-is and set sign bit to 0 */
  1710. /* */
  1711. /* result gets the copy of dfl with sign bit 0 */
  1712. /* dfl is the decFloat to copy */
  1713. /* returns result */
  1714. /* */
  1715. /* This is a bitwise operation; no errors or exceptions are possible. */
  1716. /* ------------------------------------------------------------------ */
  1717. decFloat * decFloatCopyAbs(decFloat *result, const decFloat *dfl) {
  1718. if (dfl!=result) *result=*dfl; // copy needed
  1719. DFBYTE(result, 0)&=~0x80; // zero sign bit
  1720. return result;
  1721. } // decFloatCopyAbs
  1722. /* ------------------------------------------------------------------ */
  1723. /* decFloatCopyNegate -- copy a decFloat as-is with inverted sign bit */
  1724. /* */
  1725. /* result gets the copy of dfl with sign bit inverted */
  1726. /* dfl is the decFloat to copy */
  1727. /* returns result */
  1728. /* */
  1729. /* This is a bitwise operation; no errors or exceptions are possible. */
  1730. /* ------------------------------------------------------------------ */
  1731. decFloat * decFloatCopyNegate(decFloat *result, const decFloat *dfl) {
  1732. if (dfl!=result) *result=*dfl; // copy needed
  1733. DFBYTE(result, 0)^=0x80; // invert sign bit
  1734. return result;
  1735. } // decFloatCopyNegate
  1736. /* ------------------------------------------------------------------ */
  1737. /* decFloatCopySign -- copy a decFloat with the sign of another */
  1738. /* */
  1739. /* result gets the result of copying dfl with the sign of dfr */
  1740. /* dfl is the first decFloat (lhs) */
  1741. /* dfr is the second decFloat (rhs) */
  1742. /* returns result */
  1743. /* */
  1744. /* This is a bitwise operation; no errors or exceptions are possible. */
  1745. /* ------------------------------------------------------------------ */
  1746. decFloat * decFloatCopySign(decFloat *result,
  1747. const decFloat *dfl, const decFloat *dfr) {
  1748. uByte sign=(uByte)(DFBYTE(dfr, 0)&0x80); // save sign bit
  1749. if (dfl!=result) *result=*dfl; // copy needed
  1750. DFBYTE(result, 0)&=~0x80; // clear sign ..
  1751. DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); // .. and set saved
  1752. return result;
  1753. } // decFloatCopySign
  1754. /* ------------------------------------------------------------------ */
  1755. /* decFloatDigits -- return the number of digits in a decFloat */
  1756. /* */
  1757. /* df is the decFloat to investigate */
  1758. /* returns the number of significant digits in the decFloat; a */
  1759. /* zero coefficient returns 1 as does an infinity (a NaN returns */
  1760. /* the number of digits in the payload) */
  1761. /* ------------------------------------------------------------------ */
  1762. // private macro to extract a declet according to provided formula
  1763. // (form), and if it is non-zero then return the calculated digits
  1764. // depending on the declet number (n), where n=0 for the most
  1765. // significant declet; uses uInt dpd for work
  1766. #define dpdlenchk(n, form) dpd=(form)&0x3ff; \
  1767. if (dpd) return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3])
  1768. // next one is used when it is known that the declet must be
  1769. // non-zero, or is the final zero declet
  1770. #define dpdlendun(n, form) dpd=(form)&0x3ff; \
  1771. if (dpd==0) return 1; \
  1772. return (DECPMAX-1-3*(n))-(3-DPD2BCD8[dpd*4+3])
  1773. uInt decFloatDigits(const decFloat *df) {
  1774. uInt dpd; // work
  1775. uInt sourhi=DFWORD(df, 0); // top word from source decFloat
  1776. #if QUAD
  1777. uInt sourmh, sourml;
  1778. #endif
  1779. uInt sourlo;
  1780. if (DFISINF(df)) return 1;
  1781. // A NaN effectively has an MSD of 0; otherwise if non-zero MSD
  1782. // then the coefficient is full-length
  1783. if (!DFISNAN(df) && DECCOMBMSD[sourhi>>26]) return DECPMAX;
  1784. #if DOUBLE
  1785. if (sourhi&0x0003ffff) { // ends in first
  1786. dpdlenchk(0, sourhi>>8);
  1787. sourlo=DFWORD(df, 1);
  1788. dpdlendun(1, (sourhi<<2) | (sourlo>>30));
  1789. } // [cannot drop through]
  1790. sourlo=DFWORD(df, 1); // sourhi not involved now
  1791. if (sourlo&0xfff00000) { // in one of first two
  1792. dpdlenchk(1, sourlo>>30); // very rare
  1793. dpdlendun(2, sourlo>>20);
  1794. } // [cannot drop through]
  1795. dpdlenchk(3, sourlo>>10);
  1796. dpdlendun(4, sourlo);
  1797. // [cannot drop through]
  1798. #elif QUAD
  1799. if (sourhi&0x00003fff) { // ends in first
  1800. dpdlenchk(0, sourhi>>4);
  1801. sourmh=DFWORD(df, 1);
  1802. dpdlendun(1, ((sourhi)<<6) | (sourmh>>26));
  1803. } // [cannot drop through]
  1804. sourmh=DFWORD(df, 1);
  1805. if (sourmh) {
  1806. dpdlenchk(1, sourmh>>26);
  1807. dpdlenchk(2, sourmh>>16);
  1808. dpdlenchk(3, sourmh>>6);
  1809. sourml=DFWORD(df, 2);
  1810. dpdlendun(4, ((sourmh)<<4) | (sourml>>28));
  1811. } // [cannot drop through]
  1812. sourml=DFWORD(df, 2);
  1813. if (sourml) {
  1814. dpdlenchk(4, sourml>>28);
  1815. dpdlenchk(5, sourml>>18);
  1816. dpdlenchk(6, sourml>>8);
  1817. sourlo=DFWORD(df, 3);
  1818. dpdlendun(7, ((sourml)<<2) | (sourlo>>30));
  1819. } // [cannot drop through]
  1820. sourlo=DFWORD(df, 3);
  1821. if (sourlo&0xfff00000) { // in one of first two
  1822. dpdlenchk(7, sourlo>>30); // very rare
  1823. dpdlendun(8, sourlo>>20);
  1824. } // [cannot drop through]
  1825. dpdlenchk(9, sourlo>>10);
  1826. dpdlendun(10, sourlo);
  1827. // [cannot drop through]
  1828. #endif
  1829. } // decFloatDigits
  1830. /* ------------------------------------------------------------------ */
  1831. /* decFloatDivide -- divide a decFloat by another */
  1832. /* */
  1833. /* result gets the result of dividing dfl by dfr: */
  1834. /* dfl is the first decFloat (lhs) */
  1835. /* dfr is the second decFloat (rhs) */
  1836. /* set is the context */
  1837. /* returns result */
  1838. /* */
  1839. /* ------------------------------------------------------------------ */
  1840. // This is just a wrapper.
  1841. decFloat * decFloatDivide(decFloat *result,
  1842. const decFloat *dfl, const decFloat *dfr,
  1843. decContext *set) {
  1844. return decDivide(result, dfl, dfr, set, DIVIDE);
  1845. } // decFloatDivide
  1846. /* ------------------------------------------------------------------ */
  1847. /* decFloatDivideInteger -- integer divide a decFloat by another */
  1848. /* */
  1849. /* result gets the result of dividing dfl by dfr: */
  1850. /* dfl is the first decFloat (lhs) */
  1851. /* dfr is the second decFloat (rhs) */
  1852. /* set is the context */
  1853. /* returns result */
  1854. /* */
  1855. /* ------------------------------------------------------------------ */
  1856. decFloat * decFloatDivideInteger(decFloat *result,
  1857. const decFloat *dfl, const decFloat *dfr,
  1858. decContext *set) {
  1859. return decDivide(result, dfl, dfr, set, DIVIDEINT);
  1860. } // decFloatDivideInteger
  1861. /* ------------------------------------------------------------------ */
  1862. /* decFloatFMA -- multiply and add three decFloats, fused */
  1863. /* */
  1864. /* result gets the result of (dfl*dfr)+dff with a single rounding */
  1865. /* dfl is the first decFloat (lhs) */
  1866. /* dfr is the second decFloat (rhs) */
  1867. /* dff is the final decFloat (fhs) */
  1868. /* set is the context */
  1869. /* returns result */
  1870. /* */
  1871. /* ------------------------------------------------------------------ */
  1872. decFloat * decFloatFMA(decFloat *result, const decFloat *dfl,
  1873. const decFloat *dfr, const decFloat *dff,
  1874. decContext *set) {
  1875. // The accumulator has the bytes needed for FiniteMultiply, plus
  1876. // one byte to the left in case of carry, plus DECPMAX+2 to the
  1877. // right for the final addition (up to full fhs + round & sticky)
  1878. #define FMALEN (ROUNDUP4(1+ (DECPMAX9*18+1) +DECPMAX+2))
  1879. uByte acc[FMALEN]; // for multiplied coefficient in BCD
  1880. // .. and for final result
  1881. bcdnum mul; // for multiplication result
  1882. bcdnum fin; // for final operand, expanded
  1883. uByte coe[ROUNDUP4(DECPMAX)]; // dff coefficient in BCD
  1884. bcdnum *hi, *lo; // bcdnum with higher/lower exponent
  1885. uInt diffsign; // non-zero if signs differ
  1886. uInt hipad; // pad digit for hi if needed
  1887. Int padding; // excess exponent
  1888. uInt carry; // +1 for ten's complement and during add
  1889. uByte *ub, *uh, *ul; // work
  1890. uInt uiwork; // for macros
  1891. // handle all the special values [any special operand leads to a
  1892. // special result]
  1893. if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr) || DFISSPECIAL(dff)) {
  1894. decFloat proxy; // multiplication result proxy
  1895. // NaNs are handled as usual, giving priority to sNaNs
  1896. if (DFISSNAN(dfl) || DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  1897. if (DFISSNAN(dff)) return decNaNs(result, dff, NULL, set);
  1898. if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  1899. if (DFISNAN(dff)) return decNaNs(result, dff, NULL, set);
  1900. // One or more of the three is infinite
  1901. // infinity times zero is bad
  1902. decFloatZero(&proxy);
  1903. if (DFISINF(dfl)) {
  1904. if (DFISZERO(dfr)) return decInvalid(result, set);
  1905. decInfinity(&proxy, &proxy);
  1906. }
  1907. else if (DFISINF(dfr)) {
  1908. if (DFISZERO(dfl)) return decInvalid(result, set);
  1909. decInfinity(&proxy, &proxy);
  1910. }
  1911. // compute sign of multiplication and place in proxy
  1912. DFWORD(&proxy, 0)|=(DFWORD(dfl, 0)^DFWORD(dfr, 0))&DECFLOAT_Sign;
  1913. if (!DFISINF(dff)) return decFloatCopy(result, &proxy);
  1914. // dff is Infinite
  1915. if (!DFISINF(&proxy)) return decInfinity(result, dff);
  1916. // both sides of addition are infinite; different sign is bad
  1917. if ((DFWORD(dff, 0)&DECFLOAT_Sign)!=(DFWORD(&proxy, 0)&DECFLOAT_Sign))
  1918. return decInvalid(result, set);
  1919. return decFloatCopy(result, &proxy);
  1920. }
  1921. /* Here when all operands are finite */
  1922. // First multiply dfl*dfr
  1923. decFiniteMultiply(&mul, acc+1, dfl, dfr);
  1924. // The multiply is complete, exact and unbounded, and described in
  1925. // mul with the coefficient held in acc[1...]
  1926. // now add in dff; the algorithm is essentially the same as
  1927. // decFloatAdd, but the code is different because the code there
  1928. // is highly optimized for adding two numbers of the same size
  1929. fin.exponent=GETEXPUN(dff); // get dff exponent and sign
  1930. fin.sign=DFWORD(dff, 0)&DECFLOAT_Sign;
  1931. diffsign=mul.sign^fin.sign; // note if signs differ
  1932. fin.msd=coe;
  1933. fin.lsd=coe+DECPMAX-1;
  1934. GETCOEFF(dff, coe); // extract the coefficient
  1935. // now set hi and lo so that hi points to whichever of mul and fin
  1936. // has the higher exponent and lo points to the other [don't care,
  1937. // if the same]. One coefficient will be in acc, the other in coe.
  1938. if (mul.exponent>=fin.exponent) {
  1939. hi=&mul;
  1940. lo=&fin;
  1941. }
  1942. else {
  1943. hi=&fin;
  1944. lo=&mul;
  1945. }
  1946. // remove leading zeros on both operands; this will save time later
  1947. // and make testing for zero trivial (tests are safe because acc
  1948. // and coe are rounded up to uInts)
  1949. for (; UBTOUI(hi->msd)==0 && hi->msd+3<hi->lsd;) hi->msd+=4;
  1950. for (; *hi->msd==0 && hi->msd<hi->lsd;) hi->msd++;
  1951. for (; UBTOUI(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4;
  1952. for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++;
  1953. // if hi is zero then result will be lo (which has the smaller
  1954. // exponent), which also may need to be tested for zero for the
  1955. // weird IEEE 754 sign rules
  1956. if (*hi->msd==0) { // hi is zero
  1957. // "When the sum of two operands with opposite signs is
  1958. // exactly zero, the sign of that sum shall be '+' in all
  1959. // rounding modes except round toward -Infinity, in which
  1960. // mode that sign shall be '-'."
  1961. if (diffsign) {
  1962. if (*lo->msd==0) { // lo is zero
  1963. lo->sign=0;
  1964. if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign;
  1965. } // diffsign && lo=0
  1966. } // diffsign
  1967. return decFinalize(result, lo, set); // may need clamping
  1968. } // numfl is zero
  1969. // [here, both are minimal length and hi is non-zero]
  1970. // (if lo is zero then padding with zeros may be needed, below)
  1971. // if signs differ, take the ten's complement of hi (zeros to the
  1972. // right do not matter because the complement of zero is zero); the
  1973. // +1 is done later, as part of the addition, inserted at the
  1974. // correct digit
  1975. hipad=0;
  1976. carry=0;
  1977. if (diffsign) {
  1978. hipad=9;
  1979. carry=1;
  1980. // exactly the correct number of digits must be inverted
  1981. for (uh=hi->msd; uh<hi->lsd-3; uh+=4) UBFROMUI(uh, 0x09090909-UBTOUI(uh));
  1982. for (; uh<=hi->lsd; uh++) *uh=(uByte)(0x09-*uh);
  1983. }
  1984. // ready to add; note that hi has no leading zeros so gap
  1985. // calculation does not have to be as pessimistic as in decFloatAdd
  1986. // (this is much more like the arbitrary-precision algorithm in
  1987. // Rexx and decNumber)
  1988. // padding is the number of zeros that would need to be added to hi
  1989. // for its lsd to be aligned with the lsd of lo
  1990. padding=hi->exponent-lo->exponent;
  1991. // printf("FMA pad %ld\n", (LI)padding);
  1992. // the result of the addition will be built into the accumulator,
  1993. // starting from the far right; this could be either hi or lo, and
  1994. // will be aligned
  1995. ub=acc+FMALEN-1; // where lsd of result will go
  1996. ul=lo->lsd; // lsd of rhs
  1997. if (padding!=0) { // unaligned
  1998. // if the msd of lo is more than DECPMAX+2 digits to the right of
  1999. // the original msd of hi then it can be reduced to a single
  2000. // digit at the right place, as it stays clear of hi digits
  2001. // [it must be DECPMAX+2 because during a subtraction the msd
  2002. // could become 0 after a borrow from 1.000 to 0.9999...]
  2003. Int hilen=(Int)(hi->lsd-hi->msd+1); // length of hi
  2004. Int lolen=(Int)(lo->lsd-lo->msd+1); // and of lo
  2005. if (hilen+padding-lolen > DECPMAX+2) { // can reduce lo to single
  2006. // make sure it is virtually at least DECPMAX from hi->msd, at
  2007. // least to right of hi->lsd (in case of destructive subtract),
  2008. // and separated by at least two digits from either of those
  2009. // (the tricky DOUBLE case is when hi is a 1 that will become a
  2010. // 0.9999... by subtraction:
  2011. // hi: 1 E+16
  2012. // lo: .................1000000000000000 E-16
  2013. // which for the addition pads to:
  2014. // hi: 1000000000000000000 E-16
  2015. // lo: .................1000000000000000 E-16
  2016. Int newexp=MINI(hi->exponent, hi->exponent+hilen-DECPMAX)-3;
  2017. // printf("FMA reduce: %ld\n", (LI)reduce);
  2018. lo->lsd=lo->msd; // to single digit [maybe 0]
  2019. lo->exponent=newexp; // new lowest exponent
  2020. padding=hi->exponent-lo->exponent; // recalculate
  2021. ul=lo->lsd; // .. and repoint
  2022. }
  2023. // padding is still > 0, but will fit in acc (less leading carry slot)
  2024. #if DECCHECK
  2025. if (padding<=0) printf("FMA low padding: %ld\n", (LI)padding);
  2026. if (hilen+padding+1>FMALEN)
  2027. printf("FMA excess hilen+padding: %ld+%ld \n", (LI)hilen, (LI)padding);
  2028. // printf("FMA padding: %ld\n", (LI)padding);
  2029. #endif
  2030. // padding digits can now be set in the result; one or more of
  2031. // these will come from lo; others will be zeros in the gap
  2032. for (; ul-3>=lo->msd && padding>3; padding-=4, ul-=4, ub-=4) {
  2033. UBFROMUI(ub-3, UBTOUI(ul-3)); // [cannot overlap]
  2034. }
  2035. for (; ul>=lo->msd && padding>0; padding--, ul--, ub--) *ub=*ul;
  2036. for (;padding>0; padding--, ub--) *ub=0; // mind the gap
  2037. }
  2038. // addition now complete to the right of the rightmost digit of hi
  2039. uh=hi->lsd;
  2040. // dow do the add from hi->lsd to the left
  2041. // [bytewise, because either operand can run out at any time]
  2042. // carry was set up depending on ten's complement above
  2043. // first assume both operands have some digits
  2044. for (;; ub--) {
  2045. if (uh<hi->msd || ul<lo->msd) break;
  2046. *ub=(uByte)(carry+(*uh--)+(*ul--));
  2047. carry=0;
  2048. if (*ub<10) continue;
  2049. *ub-=10;
  2050. carry=1;
  2051. } // both loop
  2052. if (ul<lo->msd) { // to left of lo
  2053. for (;; ub--) {
  2054. if (uh<hi->msd) break;
  2055. *ub=(uByte)(carry+(*uh--)); // [+0]
  2056. carry=0;
  2057. if (*ub<10) continue;
  2058. *ub-=10;
  2059. carry=1;
  2060. } // hi loop
  2061. }
  2062. else { // to left of hi
  2063. for (;; ub--) {
  2064. if (ul<lo->msd) break;
  2065. *ub=(uByte)(carry+hipad+(*ul--));
  2066. carry=0;
  2067. if (*ub<10) continue;
  2068. *ub-=10;
  2069. carry=1;
  2070. } // lo loop
  2071. }
  2072. // addition complete -- now handle carry, borrow, etc.
  2073. // use lo to set up the num (its exponent is already correct, and
  2074. // sign usually is)
  2075. lo->msd=ub+1;
  2076. lo->lsd=acc+FMALEN-1;
  2077. // decShowNum(lo, "lo");
  2078. if (!diffsign) { // same-sign addition
  2079. if (carry) { // carry out
  2080. *ub=1; // place the 1 ..
  2081. lo->msd--; // .. and update
  2082. }
  2083. } // same sign
  2084. else { // signs differed (subtraction)
  2085. if (!carry) { // no carry out means hi<lo
  2086. // borrowed -- take ten's complement of the right digits
  2087. lo->sign=hi->sign; // sign is lhs sign
  2088. for (ul=lo->msd; ul<lo->lsd-3; ul+=4) UBFROMUI(ul, 0x09090909-UBTOUI(ul));
  2089. for (; ul<=lo->lsd; ul++) *ul=(uByte)(0x09-*ul); // [leaves ul at lsd+1]
  2090. // complete the ten's complement by adding 1 [cannot overrun]
  2091. for (ul--; *ul==9; ul--) *ul=0;
  2092. *ul+=1;
  2093. } // borrowed
  2094. else { // carry out means hi>=lo
  2095. // sign to use is lo->sign
  2096. // all done except for the special IEEE 754 exact-zero-result
  2097. // rule (see above); while testing for zero, strip leading
  2098. // zeros (which will save decFinalize doing it)
  2099. for (; UBTOUI(lo->msd)==0 && lo->msd+3<lo->lsd;) lo->msd+=4;
  2100. for (; *lo->msd==0 && lo->msd<lo->lsd;) lo->msd++;
  2101. if (*lo->msd==0) { // must be true zero (and diffsign)
  2102. lo->sign=0; // assume +
  2103. if (set->round==DEC_ROUND_FLOOR) lo->sign=DECFLOAT_Sign;
  2104. }
  2105. // [else was not zero, might still have leading zeros]
  2106. } // subtraction gave positive result
  2107. } // diffsign
  2108. #if DECCHECK
  2109. // assert no left underrun
  2110. if (lo->msd<acc) {
  2111. printf("FMA underrun by %ld \n", (LI)(acc-lo->msd));
  2112. }
  2113. #endif
  2114. return decFinalize(result, lo, set); // round, check, and lay out
  2115. } // decFloatFMA
  2116. /* ------------------------------------------------------------------ */
  2117. /* decFloatFromInt -- initialise a decFloat from an Int */
  2118. /* */
  2119. /* result gets the converted Int */
  2120. /* n is the Int to convert */
  2121. /* returns result */
  2122. /* */
  2123. /* The result is Exact; no errors or exceptions are possible. */
  2124. /* ------------------------------------------------------------------ */
  2125. decFloat * decFloatFromInt32(decFloat *result, Int n) {
  2126. uInt u=(uInt)n; // copy as bits
  2127. uInt encode; // work
  2128. DFWORD(result, 0)=ZEROWORD; // always
  2129. #if QUAD
  2130. DFWORD(result, 1)=0;
  2131. DFWORD(result, 2)=0;
  2132. #endif
  2133. if (n<0) { // handle -n with care
  2134. // [This can be done without the test, but is then slightly slower]
  2135. u=(~u)+1;
  2136. DFWORD(result, 0)|=DECFLOAT_Sign;
  2137. }
  2138. // Since the maximum value of u now is 2**31, only the low word of
  2139. // result is affected
  2140. encode=BIN2DPD[u%1000];
  2141. u/=1000;
  2142. encode|=BIN2DPD[u%1000]<<10;
  2143. u/=1000;
  2144. encode|=BIN2DPD[u%1000]<<20;
  2145. u/=1000; // now 0, 1, or 2
  2146. encode|=u<<30;
  2147. DFWORD(result, DECWORDS-1)=encode;
  2148. return result;
  2149. } // decFloatFromInt32
  2150. /* ------------------------------------------------------------------ */
  2151. /* decFloatFromUInt -- initialise a decFloat from a uInt */
  2152. /* */
  2153. /* result gets the converted uInt */
  2154. /* n is the uInt to convert */
  2155. /* returns result */
  2156. /* */
  2157. /* The result is Exact; no errors or exceptions are possible. */
  2158. /* ------------------------------------------------------------------ */
  2159. decFloat * decFloatFromUInt32(decFloat *result, uInt u) {
  2160. uInt encode; // work
  2161. DFWORD(result, 0)=ZEROWORD; // always
  2162. #if QUAD
  2163. DFWORD(result, 1)=0;
  2164. DFWORD(result, 2)=0;
  2165. #endif
  2166. encode=BIN2DPD[u%1000];
  2167. u/=1000;
  2168. encode|=BIN2DPD[u%1000]<<10;
  2169. u/=1000;
  2170. encode|=BIN2DPD[u%1000]<<20;
  2171. u/=1000; // now 0 -> 4
  2172. encode|=u<<30;
  2173. DFWORD(result, DECWORDS-1)=encode;
  2174. DFWORD(result, DECWORDS-2)|=u>>2; // rarely non-zero
  2175. return result;
  2176. } // decFloatFromUInt32
  2177. /* ------------------------------------------------------------------ */
  2178. /* decFloatInvert -- logical digitwise INVERT of a decFloat */
  2179. /* */
  2180. /* result gets the result of INVERTing df */
  2181. /* df is the decFloat to invert */
  2182. /* set is the context */
  2183. /* returns result, which will be canonical with sign=0 */
  2184. /* */
  2185. /* The operand must be positive, finite with exponent q=0, and */
  2186. /* comprise just zeros and ones; if not, Invalid operation results. */
  2187. /* ------------------------------------------------------------------ */
  2188. decFloat * decFloatInvert(decFloat *result, const decFloat *df,
  2189. decContext *set) {
  2190. uInt sourhi=DFWORD(df, 0); // top word of dfs
  2191. if (!DFISUINT01(df) || !DFISCC01(df)) return decInvalid(result, set);
  2192. // the operand is a finite integer (q=0)
  2193. #if DOUBLE
  2194. DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04009124);
  2195. DFWORD(result, 1)=(~DFWORD(df, 1)) &0x49124491;
  2196. #elif QUAD
  2197. DFWORD(result, 0)=ZEROWORD|((~sourhi)&0x04000912);
  2198. DFWORD(result, 1)=(~DFWORD(df, 1)) &0x44912449;
  2199. DFWORD(result, 2)=(~DFWORD(df, 2)) &0x12449124;
  2200. DFWORD(result, 3)=(~DFWORD(df, 3)) &0x49124491;
  2201. #endif
  2202. return result;
  2203. } // decFloatInvert
  2204. /* ------------------------------------------------------------------ */
  2205. /* decFloatIs -- decFloat tests (IsSigned, etc.) */
  2206. /* */
  2207. /* df is the decFloat to test */
  2208. /* returns 0 or 1 in a uInt */
  2209. /* */
  2210. /* Many of these could be macros, but having them as real functions */
  2211. /* is a little cleaner (and they can be referred to here by the */
  2212. /* generic names) */
  2213. /* ------------------------------------------------------------------ */
  2214. uInt decFloatIsCanonical(const decFloat *df) {
  2215. if (DFISSPECIAL(df)) {
  2216. if (DFISINF(df)) {
  2217. if (DFWORD(df, 0)&ECONMASK) return 0; // exponent continuation
  2218. if (!DFISCCZERO(df)) return 0; // coefficient continuation
  2219. return 1;
  2220. }
  2221. // is a NaN
  2222. if (DFWORD(df, 0)&ECONNANMASK) return 0; // exponent continuation
  2223. if (DFISCCZERO(df)) return 1; // coefficient continuation
  2224. // drop through to check payload
  2225. }
  2226. { // declare block
  2227. #if DOUBLE
  2228. uInt sourhi=DFWORD(df, 0);
  2229. uInt sourlo=DFWORD(df, 1);
  2230. if (CANONDPDOFF(sourhi, 8)
  2231. && CANONDPDTWO(sourhi, sourlo, 30)
  2232. && CANONDPDOFF(sourlo, 20)
  2233. && CANONDPDOFF(sourlo, 10)
  2234. && CANONDPDOFF(sourlo, 0)) return 1;
  2235. #elif QUAD
  2236. uInt sourhi=DFWORD(df, 0);
  2237. uInt sourmh=DFWORD(df, 1);
  2238. uInt sourml=DFWORD(df, 2);
  2239. uInt sourlo=DFWORD(df, 3);
  2240. if (CANONDPDOFF(sourhi, 4)
  2241. && CANONDPDTWO(sourhi, sourmh, 26)
  2242. && CANONDPDOFF(sourmh, 16)
  2243. && CANONDPDOFF(sourmh, 6)
  2244. && CANONDPDTWO(sourmh, sourml, 28)
  2245. && CANONDPDOFF(sourml, 18)
  2246. && CANONDPDOFF(sourml, 8)
  2247. && CANONDPDTWO(sourml, sourlo, 30)
  2248. && CANONDPDOFF(sourlo, 20)
  2249. && CANONDPDOFF(sourlo, 10)
  2250. && CANONDPDOFF(sourlo, 0)) return 1;
  2251. #endif
  2252. } // block
  2253. return 0; // a declet is non-canonical
  2254. }
  2255. uInt decFloatIsFinite(const decFloat *df) {
  2256. return !DFISSPECIAL(df);
  2257. }
  2258. uInt decFloatIsInfinite(const decFloat *df) {
  2259. return DFISINF(df);
  2260. }
  2261. uInt decFloatIsInteger(const decFloat *df) {
  2262. return DFISINT(df);
  2263. }
  2264. uInt decFloatIsLogical(const decFloat *df) {
  2265. return DFISUINT01(df) & DFISCC01(df);
  2266. }
  2267. uInt decFloatIsNaN(const decFloat *df) {
  2268. return DFISNAN(df);
  2269. }
  2270. uInt decFloatIsNegative(const decFloat *df) {
  2271. return DFISSIGNED(df) && !DFISZERO(df) && !DFISNAN(df);
  2272. }
  2273. uInt decFloatIsNormal(const decFloat *df) {
  2274. Int exp; // exponent
  2275. if (DFISSPECIAL(df)) return 0;
  2276. if (DFISZERO(df)) return 0;
  2277. // is finite and non-zero
  2278. exp=GETEXPUN(df) // get unbiased exponent ..
  2279. +decFloatDigits(df)-1; // .. and make adjusted exponent
  2280. return (exp>=DECEMIN); // < DECEMIN is subnormal
  2281. }
  2282. uInt decFloatIsPositive(const decFloat *df) {
  2283. return !DFISSIGNED(df) && !DFISZERO(df) && !DFISNAN(df);
  2284. }
  2285. uInt decFloatIsSignaling(const decFloat *df) {
  2286. return DFISSNAN(df);
  2287. }
  2288. uInt decFloatIsSignalling(const decFloat *df) {
  2289. return DFISSNAN(df);
  2290. }
  2291. uInt decFloatIsSigned(const decFloat *df) {
  2292. return DFISSIGNED(df);
  2293. }
  2294. uInt decFloatIsSubnormal(const decFloat *df) {
  2295. if (DFISSPECIAL(df)) return 0;
  2296. // is finite
  2297. if (decFloatIsNormal(df)) return 0;
  2298. // it is <Nmin, but could be zero
  2299. if (DFISZERO(df)) return 0;
  2300. return 1; // is subnormal
  2301. }
  2302. uInt decFloatIsZero(const decFloat *df) {
  2303. return DFISZERO(df);
  2304. } // decFloatIs...
  2305. /* ------------------------------------------------------------------ */
  2306. /* decFloatLogB -- return adjusted exponent, by 754 rules */
  2307. /* */
  2308. /* result gets the adjusted exponent as an integer, or a NaN etc. */
  2309. /* df is the decFloat to be examined */
  2310. /* set is the context */
  2311. /* returns result */
  2312. /* */
  2313. /* Notable cases: */
  2314. /* A<0 -> Use |A| */
  2315. /* A=0 -> -Infinity (Division by zero) */
  2316. /* A=Infinite -> +Infinity (Exact) */
  2317. /* A=1 exactly -> 0 (Exact) */
  2318. /* NaNs are propagated as usual */
  2319. /* ------------------------------------------------------------------ */
  2320. decFloat * decFloatLogB(decFloat *result, const decFloat *df,
  2321. decContext *set) {
  2322. Int ae; // adjusted exponent
  2323. if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  2324. if (DFISINF(df)) {
  2325. DFWORD(result, 0)=0; // need +ve
  2326. return decInfinity(result, result); // canonical +Infinity
  2327. }
  2328. if (DFISZERO(df)) {
  2329. set->status|=DEC_Division_by_zero; // as per 754
  2330. DFWORD(result, 0)=DECFLOAT_Sign; // make negative
  2331. return decInfinity(result, result); // canonical -Infinity
  2332. }
  2333. ae=GETEXPUN(df) // get unbiased exponent ..
  2334. +decFloatDigits(df)-1; // .. and make adjusted exponent
  2335. // ae has limited range (3 digits for DOUBLE and 4 for QUAD), so
  2336. // it is worth using a special case of decFloatFromInt32
  2337. DFWORD(result, 0)=ZEROWORD; // always
  2338. if (ae<0) {
  2339. DFWORD(result, 0)|=DECFLOAT_Sign; // -0 so far
  2340. ae=-ae;
  2341. }
  2342. #if DOUBLE
  2343. DFWORD(result, 1)=BIN2DPD[ae]; // a single declet
  2344. #elif QUAD
  2345. DFWORD(result, 1)=0;
  2346. DFWORD(result, 2)=0;
  2347. DFWORD(result, 3)=(ae/1000)<<10; // is <10, so need no DPD encode
  2348. DFWORD(result, 3)|=BIN2DPD[ae%1000];
  2349. #endif
  2350. return result;
  2351. } // decFloatLogB
  2352. /* ------------------------------------------------------------------ */
  2353. /* decFloatMax -- return maxnum of two operands */
  2354. /* */
  2355. /* result gets the chosen decFloat */
  2356. /* dfl is the first decFloat (lhs) */
  2357. /* dfr is the second decFloat (rhs) */
  2358. /* set is the context */
  2359. /* returns result */
  2360. /* */
  2361. /* If just one operand is a quiet NaN it is ignored. */
  2362. /* ------------------------------------------------------------------ */
  2363. decFloat * decFloatMax(decFloat *result,
  2364. const decFloat *dfl, const decFloat *dfr,
  2365. decContext *set) {
  2366. Int comp;
  2367. if (DFISNAN(dfl)) {
  2368. // sNaN or both NaNs leads to normal NaN processing
  2369. if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set);
  2370. return decCanonical(result, dfr); // RHS is numeric
  2371. }
  2372. if (DFISNAN(dfr)) {
  2373. // sNaN leads to normal NaN processing (both NaN handled above)
  2374. if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  2375. return decCanonical(result, dfl); // LHS is numeric
  2376. }
  2377. // Both operands are numeric; numeric comparison needed -- use
  2378. // total order for a well-defined choice (and +0 > -0)
  2379. comp=decNumCompare(dfl, dfr, 1);
  2380. if (comp>=0) return decCanonical(result, dfl);
  2381. return decCanonical(result, dfr);
  2382. } // decFloatMax
  2383. /* ------------------------------------------------------------------ */
  2384. /* decFloatMaxMag -- return maxnummag of two operands */
  2385. /* */
  2386. /* result gets the chosen decFloat */
  2387. /* dfl is the first decFloat (lhs) */
  2388. /* dfr is the second decFloat (rhs) */
  2389. /* set is the context */
  2390. /* returns result */
  2391. /* */
  2392. /* Returns according to the magnitude comparisons if both numeric and */
  2393. /* unequal, otherwise returns maxnum */
  2394. /* ------------------------------------------------------------------ */
  2395. decFloat * decFloatMaxMag(decFloat *result,
  2396. const decFloat *dfl, const decFloat *dfr,
  2397. decContext *set) {
  2398. Int comp;
  2399. decFloat absl, absr;
  2400. if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMax(result, dfl, dfr, set);
  2401. decFloatCopyAbs(&absl, dfl);
  2402. decFloatCopyAbs(&absr, dfr);
  2403. comp=decNumCompare(&absl, &absr, 0);
  2404. if (comp>0) return decCanonical(result, dfl);
  2405. if (comp<0) return decCanonical(result, dfr);
  2406. return decFloatMax(result, dfl, dfr, set);
  2407. } // decFloatMaxMag
  2408. /* ------------------------------------------------------------------ */
  2409. /* decFloatMin -- return minnum of two operands */
  2410. /* */
  2411. /* result gets the chosen decFloat */
  2412. /* dfl is the first decFloat (lhs) */
  2413. /* dfr is the second decFloat (rhs) */
  2414. /* set is the context */
  2415. /* returns result */
  2416. /* */
  2417. /* If just one operand is a quiet NaN it is ignored. */
  2418. /* ------------------------------------------------------------------ */
  2419. decFloat * decFloatMin(decFloat *result,
  2420. const decFloat *dfl, const decFloat *dfr,
  2421. decContext *set) {
  2422. Int comp;
  2423. if (DFISNAN(dfl)) {
  2424. // sNaN or both NaNs leads to normal NaN processing
  2425. if (DFISNAN(dfr) || DFISSNAN(dfl)) return decNaNs(result, dfl, dfr, set);
  2426. return decCanonical(result, dfr); // RHS is numeric
  2427. }
  2428. if (DFISNAN(dfr)) {
  2429. // sNaN leads to normal NaN processing (both NaN handled above)
  2430. if (DFISSNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  2431. return decCanonical(result, dfl); // LHS is numeric
  2432. }
  2433. // Both operands are numeric; numeric comparison needed -- use
  2434. // total order for a well-defined choice (and +0 > -0)
  2435. comp=decNumCompare(dfl, dfr, 1);
  2436. if (comp<=0) return decCanonical(result, dfl);
  2437. return decCanonical(result, dfr);
  2438. } // decFloatMin
  2439. /* ------------------------------------------------------------------ */
  2440. /* decFloatMinMag -- return minnummag of two operands */
  2441. /* */
  2442. /* result gets the chosen decFloat */
  2443. /* dfl is the first decFloat (lhs) */
  2444. /* dfr is the second decFloat (rhs) */
  2445. /* set is the context */
  2446. /* returns result */
  2447. /* */
  2448. /* Returns according to the magnitude comparisons if both numeric and */
  2449. /* unequal, otherwise returns minnum */
  2450. /* ------------------------------------------------------------------ */
  2451. decFloat * decFloatMinMag(decFloat *result,
  2452. const decFloat *dfl, const decFloat *dfr,
  2453. decContext *set) {
  2454. Int comp;
  2455. decFloat absl, absr;
  2456. if (DFISNAN(dfl) || DFISNAN(dfr)) return decFloatMin(result, dfl, dfr, set);
  2457. decFloatCopyAbs(&absl, dfl);
  2458. decFloatCopyAbs(&absr, dfr);
  2459. comp=decNumCompare(&absl, &absr, 0);
  2460. if (comp<0) return decCanonical(result, dfl);
  2461. if (comp>0) return decCanonical(result, dfr);
  2462. return decFloatMin(result, dfl, dfr, set);
  2463. } // decFloatMinMag
  2464. /* ------------------------------------------------------------------ */
  2465. /* decFloatMinus -- negate value, heeding NaNs, etc. */
  2466. /* */
  2467. /* result gets the canonicalized 0-df */
  2468. /* df is the decFloat to minus */
  2469. /* set is the context */
  2470. /* returns result */
  2471. /* */
  2472. /* This has the same effect as 0-df where the exponent of the zero is */
  2473. /* the same as that of df (if df is finite). */
  2474. /* The effect is also the same as decFloatCopyNegate except that NaNs */
  2475. /* are handled normally (the sign of a NaN is not affected, and an */
  2476. /* sNaN will signal), the result is canonical, and zero gets sign 0. */
  2477. /* ------------------------------------------------------------------ */
  2478. decFloat * decFloatMinus(decFloat *result, const decFloat *df,
  2479. decContext *set) {
  2480. if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  2481. decCanonical(result, df); // copy and check
  2482. if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80; // turn off sign bit
  2483. else DFBYTE(result, 0)^=0x80; // flip sign bit
  2484. return result;
  2485. } // decFloatMinus
  2486. /* ------------------------------------------------------------------ */
  2487. /* decFloatMultiply -- multiply two decFloats */
  2488. /* */
  2489. /* result gets the result of multiplying dfl and dfr: */
  2490. /* dfl is the first decFloat (lhs) */
  2491. /* dfr is the second decFloat (rhs) */
  2492. /* set is the context */
  2493. /* returns result */
  2494. /* */
  2495. /* ------------------------------------------------------------------ */
  2496. decFloat * decFloatMultiply(decFloat *result,
  2497. const decFloat *dfl, const decFloat *dfr,
  2498. decContext *set) {
  2499. bcdnum num; // for final conversion
  2500. uByte bcdacc[DECPMAX9*18+1]; // for coefficent in BCD
  2501. if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) { // either is special?
  2502. // NaNs are handled as usual
  2503. if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  2504. // infinity times zero is bad
  2505. if (DFISINF(dfl) && DFISZERO(dfr)) return decInvalid(result, set);
  2506. if (DFISINF(dfr) && DFISZERO(dfl)) return decInvalid(result, set);
  2507. // both infinite; return canonical infinity with computed sign
  2508. DFWORD(result, 0)=DFWORD(dfl, 0)^DFWORD(dfr, 0); // compute sign
  2509. return decInfinity(result, result);
  2510. }
  2511. /* Here when both operands are finite */
  2512. decFiniteMultiply(&num, bcdacc, dfl, dfr);
  2513. return decFinalize(result, &num, set); // round, check, and lay out
  2514. } // decFloatMultiply
  2515. /* ------------------------------------------------------------------ */
  2516. /* decFloatNextMinus -- next towards -Infinity */
  2517. /* */
  2518. /* result gets the next lesser decFloat */
  2519. /* dfl is the decFloat to start with */
  2520. /* set is the context */
  2521. /* returns result */
  2522. /* */
  2523. /* This is 754 nextdown; Invalid is the only status possible (from */
  2524. /* an sNaN). */
  2525. /* ------------------------------------------------------------------ */
  2526. decFloat * decFloatNextMinus(decFloat *result, const decFloat *dfl,
  2527. decContext *set) {
  2528. decFloat delta; // tiny increment
  2529. uInt savestat; // saves status
  2530. enum rounding saveround; // .. and mode
  2531. // +Infinity is the special case
  2532. if (DFISINF(dfl) && !DFISSIGNED(dfl)) {
  2533. DFSETNMAX(result);
  2534. return result; // [no status to set]
  2535. }
  2536. // other cases are effected by sutracting a tiny delta -- this
  2537. // should be done in a wider format as the delta is unrepresentable
  2538. // here (but can be done with normal add if the sign of zero is
  2539. // treated carefully, because no Inexactitude is interesting);
  2540. // rounding to -Infinity then pushes the result to next below
  2541. decFloatZero(&delta); // set up tiny delta
  2542. DFWORD(&delta, DECWORDS-1)=1; // coefficient=1
  2543. DFWORD(&delta, 0)=DECFLOAT_Sign; // Sign=1 + biased exponent=0
  2544. // set up for the directional round
  2545. saveround=set->round; // save mode
  2546. set->round=DEC_ROUND_FLOOR; // .. round towards -Infinity
  2547. savestat=set->status; // save status
  2548. decFloatAdd(result, dfl, &delta, set);
  2549. // Add rules mess up the sign when going from +Ntiny to 0
  2550. if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; // correct
  2551. set->status&=DEC_Invalid_operation; // preserve only sNaN status
  2552. set->status|=savestat; // restore pending flags
  2553. set->round=saveround; // .. and mode
  2554. return result;
  2555. } // decFloatNextMinus
  2556. /* ------------------------------------------------------------------ */
  2557. /* decFloatNextPlus -- next towards +Infinity */
  2558. /* */
  2559. /* result gets the next larger decFloat */
  2560. /* dfl is the decFloat to start with */
  2561. /* set is the context */
  2562. /* returns result */
  2563. /* */
  2564. /* This is 754 nextup; Invalid is the only status possible (from */
  2565. /* an sNaN). */
  2566. /* ------------------------------------------------------------------ */
  2567. decFloat * decFloatNextPlus(decFloat *result, const decFloat *dfl,
  2568. decContext *set) {
  2569. uInt savestat; // saves status
  2570. enum rounding saveround; // .. and mode
  2571. decFloat delta; // tiny increment
  2572. // -Infinity is the special case
  2573. if (DFISINF(dfl) && DFISSIGNED(dfl)) {
  2574. DFSETNMAX(result);
  2575. DFWORD(result, 0)|=DECFLOAT_Sign; // make negative
  2576. return result; // [no status to set]
  2577. }
  2578. // other cases are effected by sutracting a tiny delta -- this
  2579. // should be done in a wider format as the delta is unrepresentable
  2580. // here (but can be done with normal add if the sign of zero is
  2581. // treated carefully, because no Inexactitude is interesting);
  2582. // rounding to +Infinity then pushes the result to next above
  2583. decFloatZero(&delta); // set up tiny delta
  2584. DFWORD(&delta, DECWORDS-1)=1; // coefficient=1
  2585. DFWORD(&delta, 0)=0; // Sign=0 + biased exponent=0
  2586. // set up for the directional round
  2587. saveround=set->round; // save mode
  2588. set->round=DEC_ROUND_CEILING; // .. round towards +Infinity
  2589. savestat=set->status; // save status
  2590. decFloatAdd(result, dfl, &delta, set);
  2591. // Add rules mess up the sign when going from -Ntiny to -0
  2592. if (DFISZERO(result)) DFWORD(result, 0)^=DECFLOAT_Sign; // correct
  2593. set->status&=DEC_Invalid_operation; // preserve only sNaN status
  2594. set->status|=savestat; // restore pending flags
  2595. set->round=saveround; // .. and mode
  2596. return result;
  2597. } // decFloatNextPlus
  2598. /* ------------------------------------------------------------------ */
  2599. /* decFloatNextToward -- next towards a decFloat */
  2600. /* */
  2601. /* result gets the next decFloat */
  2602. /* dfl is the decFloat to start with */
  2603. /* dfr is the decFloat to move toward */
  2604. /* set is the context */
  2605. /* returns result */
  2606. /* */
  2607. /* This is 754-1985 nextafter, as modified during revision (dropped */
  2608. /* from 754-2008); status may be set unless the result is a normal */
  2609. /* number. */
  2610. /* ------------------------------------------------------------------ */
  2611. decFloat * decFloatNextToward(decFloat *result,
  2612. const decFloat *dfl, const decFloat *dfr,
  2613. decContext *set) {
  2614. decFloat delta; // tiny increment or decrement
  2615. decFloat pointone; // 1e-1
  2616. uInt savestat; // saves status
  2617. enum rounding saveround; // .. and mode
  2618. uInt deltatop; // top word for delta
  2619. Int comp; // work
  2620. if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  2621. // Both are numeric, so Invalid no longer a possibility
  2622. comp=decNumCompare(dfl, dfr, 0);
  2623. if (comp==0) return decFloatCopySign(result, dfl, dfr); // equal
  2624. // unequal; do NextPlus or NextMinus but with different status rules
  2625. if (comp<0) { // lhs<rhs, do NextPlus, see above for commentary
  2626. if (DFISINF(dfl) && DFISSIGNED(dfl)) { // -Infinity special case
  2627. DFSETNMAX(result);
  2628. DFWORD(result, 0)|=DECFLOAT_Sign;
  2629. return result;
  2630. }
  2631. saveround=set->round; // save mode
  2632. set->round=DEC_ROUND_CEILING; // .. round towards +Infinity
  2633. deltatop=0; // positive delta
  2634. }
  2635. else { // lhs>rhs, do NextMinus, see above for commentary
  2636. if (DFISINF(dfl) && !DFISSIGNED(dfl)) { // +Infinity special case
  2637. DFSETNMAX(result);
  2638. return result;
  2639. }
  2640. saveround=set->round; // save mode
  2641. set->round=DEC_ROUND_FLOOR; // .. round towards -Infinity
  2642. deltatop=DECFLOAT_Sign; // negative delta
  2643. }
  2644. savestat=set->status; // save status
  2645. // Here, Inexact is needed where appropriate (and hence Underflow,
  2646. // etc.). Therefore the tiny delta which is otherwise
  2647. // unrepresentable (see NextPlus and NextMinus) is constructed
  2648. // using the multiplication of FMA.
  2649. decFloatZero(&delta); // set up tiny delta
  2650. DFWORD(&delta, DECWORDS-1)=1; // coefficient=1
  2651. DFWORD(&delta, 0)=deltatop; // Sign + biased exponent=0
  2652. decFloatFromString(&pointone, "1E-1", set); // set up multiplier
  2653. decFloatFMA(result, &delta, &pointone, dfl, set);
  2654. // [Delta is truly tiny, so no need to correct sign of zero]
  2655. // use new status unless the result is normal
  2656. if (decFloatIsNormal(result)) set->status=savestat; // else goes forward
  2657. set->round=saveround; // restore mode
  2658. return result;
  2659. } // decFloatNextToward
  2660. /* ------------------------------------------------------------------ */
  2661. /* decFloatOr -- logical digitwise OR of two decFloats */
  2662. /* */
  2663. /* result gets the result of ORing dfl and dfr */
  2664. /* dfl is the first decFloat (lhs) */
  2665. /* dfr is the second decFloat (rhs) */
  2666. /* set is the context */
  2667. /* returns result, which will be canonical with sign=0 */
  2668. /* */
  2669. /* The operands must be positive, finite with exponent q=0, and */
  2670. /* comprise just zeros and ones; if not, Invalid operation results. */
  2671. /* ------------------------------------------------------------------ */
  2672. decFloat * decFloatOr(decFloat *result,
  2673. const decFloat *dfl, const decFloat *dfr,
  2674. decContext *set) {
  2675. if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
  2676. || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set);
  2677. // the operands are positive finite integers (q=0) with just 0s and 1s
  2678. #if DOUBLE
  2679. DFWORD(result, 0)=ZEROWORD
  2680. |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04009124);
  2681. DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x49124491;
  2682. #elif QUAD
  2683. DFWORD(result, 0)=ZEROWORD
  2684. |((DFWORD(dfl, 0) | DFWORD(dfr, 0))&0x04000912);
  2685. DFWORD(result, 1)=(DFWORD(dfl, 1) | DFWORD(dfr, 1))&0x44912449;
  2686. DFWORD(result, 2)=(DFWORD(dfl, 2) | DFWORD(dfr, 2))&0x12449124;
  2687. DFWORD(result, 3)=(DFWORD(dfl, 3) | DFWORD(dfr, 3))&0x49124491;
  2688. #endif
  2689. return result;
  2690. } // decFloatOr
  2691. /* ------------------------------------------------------------------ */
  2692. /* decFloatPlus -- add value to 0, heeding NaNs, etc. */
  2693. /* */
  2694. /* result gets the canonicalized 0+df */
  2695. /* df is the decFloat to plus */
  2696. /* set is the context */
  2697. /* returns result */
  2698. /* */
  2699. /* This has the same effect as 0+df where the exponent of the zero is */
  2700. /* the same as that of df (if df is finite). */
  2701. /* The effect is also the same as decFloatCopy except that NaNs */
  2702. /* are handled normally (the sign of a NaN is not affected, and an */
  2703. /* sNaN will signal), the result is canonical, and zero gets sign 0. */
  2704. /* ------------------------------------------------------------------ */
  2705. decFloat * decFloatPlus(decFloat *result, const decFloat *df,
  2706. decContext *set) {
  2707. if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  2708. decCanonical(result, df); // copy and check
  2709. if (DFISZERO(df)) DFBYTE(result, 0)&=~0x80; // turn off sign bit
  2710. return result;
  2711. } // decFloatPlus
  2712. /* ------------------------------------------------------------------ */
  2713. /* decFloatQuantize -- quantize a decFloat */
  2714. /* */
  2715. /* result gets the result of quantizing dfl to match dfr */
  2716. /* dfl is the first decFloat (lhs) */
  2717. /* dfr is the second decFloat (rhs), which sets the exponent */
  2718. /* set is the context */
  2719. /* returns result */
  2720. /* */
  2721. /* Unless there is an error or the result is infinite, the exponent */
  2722. /* of result is guaranteed to be the same as that of dfr. */
  2723. /* ------------------------------------------------------------------ */
  2724. decFloat * decFloatQuantize(decFloat *result,
  2725. const decFloat *dfl, const decFloat *dfr,
  2726. decContext *set) {
  2727. Int explb, exprb; // left and right biased exponents
  2728. uByte *ulsd; // local LSD pointer
  2729. uByte *ub, *uc; // work
  2730. Int drop; // ..
  2731. uInt dpd; // ..
  2732. uInt encode; // encoding accumulator
  2733. uInt sourhil, sourhir; // top words from source decFloats
  2734. uInt uiwork; // for macros
  2735. #if QUAD
  2736. uShort uswork; // ..
  2737. #endif
  2738. // the following buffer holds the coefficient for manipulation
  2739. uByte buf[4+DECPMAX*3+2*QUAD]; // + space for zeros to left or right
  2740. #if DECTRACE
  2741. bcdnum num; // for trace displays
  2742. #endif
  2743. /* Start decoding the arguments */
  2744. sourhil=DFWORD(dfl, 0); // LHS top word
  2745. explb=DECCOMBEXP[sourhil>>26]; // get exponent high bits (in place)
  2746. sourhir=DFWORD(dfr, 0); // RHS top word
  2747. exprb=DECCOMBEXP[sourhir>>26];
  2748. if (EXPISSPECIAL(explb | exprb)) { // either is special?
  2749. // NaNs are handled as usual
  2750. if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  2751. // one infinity but not both is bad
  2752. if (DFISINF(dfl)!=DFISINF(dfr)) return decInvalid(result, set);
  2753. // both infinite; return canonical infinity with sign of LHS
  2754. return decInfinity(result, dfl);
  2755. }
  2756. /* Here when both arguments are finite */
  2757. // complete extraction of the exponents [no need to unbias]
  2758. explb+=GETECON(dfl); // + continuation
  2759. exprb+=GETECON(dfr); // ..
  2760. // calculate the number of digits to drop from the coefficient
  2761. drop=exprb-explb; // 0 if nothing to do
  2762. if (drop==0) return decCanonical(result, dfl); // return canonical
  2763. // the coefficient is needed; lay it out into buf, offset so zeros
  2764. // can be added before or after as needed -- an extra heading is
  2765. // added so can safely pad Quad DECPMAX-1 zeros to the left by
  2766. // fours
  2767. #define BUFOFF (buf+4+DECPMAX)
  2768. GETCOEFF(dfl, BUFOFF); // decode from decFloat
  2769. // [now the msd is at BUFOFF and the lsd is at BUFOFF+DECPMAX-1]
  2770. #if DECTRACE
  2771. num.msd=BUFOFF;
  2772. num.lsd=BUFOFF+DECPMAX-1;
  2773. num.exponent=explb-DECBIAS;
  2774. num.sign=sourhil & DECFLOAT_Sign;
  2775. decShowNum(&num, "dfl");
  2776. #endif
  2777. if (drop>0) { // [most common case]
  2778. // (this code is very similar to that in decFloatFinalize, but
  2779. // has many differences so is duplicated here -- so any changes
  2780. // may need to be made there, too)
  2781. uByte *roundat; // -> re-round digit
  2782. uByte reround; // reround value
  2783. // printf("Rounding; drop=%ld\n", (LI)drop);
  2784. // there is at least one zero needed to the left, in all but one
  2785. // exceptional (all-nines) case, so place four zeros now; this is
  2786. // needed almost always and makes rounding all-nines by fours safe
  2787. UBFROMUI(BUFOFF-4, 0);
  2788. // Three cases here:
  2789. // 1. new LSD is in coefficient (almost always)
  2790. // 2. new LSD is digit to left of coefficient (so MSD is
  2791. // round-for-reround digit)
  2792. // 3. new LSD is to left of case 2 (whole coefficient is sticky)
  2793. // Note that leading zeros can safely be treated as useful digits
  2794. // [duplicate check-stickies code to save a test]
  2795. // [by-digit check for stickies as runs of zeros are rare]
  2796. if (drop<DECPMAX) { // NB lengths not addresses
  2797. roundat=BUFOFF+DECPMAX-drop;
  2798. reround=*roundat;
  2799. for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) {
  2800. if (*ub!=0) { // non-zero to be discarded
  2801. reround=DECSTICKYTAB[reround]; // apply sticky bit
  2802. break; // [remainder don't-care]
  2803. }
  2804. } // check stickies
  2805. ulsd=roundat-1; // set LSD
  2806. }
  2807. else { // edge case
  2808. if (drop==DECPMAX) {
  2809. roundat=BUFOFF;
  2810. reround=*roundat;
  2811. }
  2812. else {
  2813. roundat=BUFOFF-1;
  2814. reround=0;
  2815. }
  2816. for (ub=roundat+1; ub<BUFOFF+DECPMAX; ub++) {
  2817. if (*ub!=0) { // non-zero to be discarded
  2818. reround=DECSTICKYTAB[reround]; // apply sticky bit
  2819. break; // [remainder don't-care]
  2820. }
  2821. } // check stickies
  2822. *BUFOFF=0; // make a coefficient of 0
  2823. ulsd=BUFOFF; // .. at the MSD place
  2824. }
  2825. if (reround!=0) { // discarding non-zero
  2826. uInt bump=0;
  2827. set->status|=DEC_Inexact;
  2828. // next decide whether to increment the coefficient
  2829. if (set->round==DEC_ROUND_HALF_EVEN) { // fastpath slowest case
  2830. if (reround>5) bump=1; // >0.5 goes up
  2831. else if (reround==5) // exactly 0.5000 ..
  2832. bump=*ulsd & 0x01; // .. up iff [new] lsd is odd
  2833. } // r-h-e
  2834. else switch (set->round) {
  2835. case DEC_ROUND_DOWN: {
  2836. // no change
  2837. break;} // r-d
  2838. case DEC_ROUND_HALF_DOWN: {
  2839. if (reround>5) bump=1;
  2840. break;} // r-h-d
  2841. case DEC_ROUND_HALF_UP: {
  2842. if (reround>=5) bump=1;
  2843. break;} // r-h-u
  2844. case DEC_ROUND_UP: {
  2845. if (reround>0) bump=1;
  2846. break;} // r-u
  2847. case DEC_ROUND_CEILING: {
  2848. // same as _UP for positive numbers, and as _DOWN for negatives
  2849. if (!(sourhil&DECFLOAT_Sign) && reround>0) bump=1;
  2850. break;} // r-c
  2851. case DEC_ROUND_FLOOR: {
  2852. // same as _UP for negative numbers, and as _DOWN for positive
  2853. // [negative reround cannot occur on 0]
  2854. if (sourhil&DECFLOAT_Sign && reround>0) bump=1;
  2855. break;} // r-f
  2856. case DEC_ROUND_05UP: {
  2857. if (reround>0) { // anything out there is 'sticky'
  2858. // bump iff lsd=0 or 5; this cannot carry so it could be
  2859. // effected immediately with no bump -- but the code
  2860. // is clearer if this is done the same way as the others
  2861. if (*ulsd==0 || *ulsd==5) bump=1;
  2862. }
  2863. break;} // r-r
  2864. default: { // e.g., DEC_ROUND_MAX
  2865. set->status|=DEC_Invalid_context;
  2866. #if DECCHECK
  2867. printf("Unknown rounding mode: %ld\n", (LI)set->round);
  2868. #endif
  2869. break;}
  2870. } // switch (not r-h-e)
  2871. // printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump);
  2872. if (bump!=0) { // need increment
  2873. // increment the coefficient; this could give 1000... (after
  2874. // the all nines case)
  2875. ub=ulsd;
  2876. for (; UBTOUI(ub-3)==0x09090909; ub-=4) UBFROMUI(ub-3, 0);
  2877. // now at most 3 digits left to non-9 (usually just the one)
  2878. for (; *ub==9; ub--) *ub=0;
  2879. *ub+=1;
  2880. // [the all-nines case will have carried one digit to the
  2881. // left of the original MSD -- just where it is needed]
  2882. } // bump needed
  2883. } // inexact rounding
  2884. // now clear zeros to the left so exactly DECPMAX digits will be
  2885. // available in the coefficent -- the first word to the left was
  2886. // cleared earlier for safe carry; now add any more needed
  2887. if (drop>4) {
  2888. UBFROMUI(BUFOFF-8, 0); // must be at least 5
  2889. for (uc=BUFOFF-12; uc>ulsd-DECPMAX-3; uc-=4) UBFROMUI(uc, 0);
  2890. }
  2891. } // need round (drop>0)
  2892. else { // drop<0; padding with -drop digits is needed
  2893. // This is the case where an error can occur if the padded
  2894. // coefficient will not fit; checking for this can be done in the
  2895. // same loop as padding for zeros if the no-hope and zero cases
  2896. // are checked first
  2897. if (-drop>DECPMAX-1) { // cannot fit unless 0
  2898. if (!ISCOEFFZERO(BUFOFF)) return decInvalid(result, set);
  2899. // a zero can have any exponent; just drop through and use it
  2900. ulsd=BUFOFF+DECPMAX-1;
  2901. }
  2902. else { // padding will fit (but may still be too long)
  2903. // final-word mask depends on endianess
  2904. #if DECLITEND
  2905. static const uInt dmask[]={0, 0x000000ff, 0x0000ffff, 0x00ffffff};
  2906. #else
  2907. static const uInt dmask[]={0, 0xff000000, 0xffff0000, 0xffffff00};
  2908. #endif
  2909. // note that here zeros to the right are added by fours, so in
  2910. // the Quad case this could write 36 zeros if the coefficient has
  2911. // fewer than three significant digits (hence the +2*QUAD for buf)
  2912. for (uc=BUFOFF+DECPMAX;; uc+=4) {
  2913. UBFROMUI(uc, 0);
  2914. if (UBTOUI(uc-DECPMAX)!=0) { // could be bad
  2915. // if all four digits should be zero, definitely bad
  2916. if (uc<=BUFOFF+DECPMAX+(-drop)-4)
  2917. return decInvalid(result, set);
  2918. // must be a 1- to 3-digit sequence; check more carefully
  2919. if ((UBTOUI(uc-DECPMAX)&dmask[(-drop)%4])!=0)
  2920. return decInvalid(result, set);
  2921. break; // no need for loop end test
  2922. }
  2923. if (uc>=BUFOFF+DECPMAX+(-drop)-4) break; // done
  2924. }
  2925. ulsd=BUFOFF+DECPMAX+(-drop)-1;
  2926. } // pad and check leading zeros
  2927. } // drop<0
  2928. #if DECTRACE
  2929. num.msd=ulsd-DECPMAX+1;
  2930. num.lsd=ulsd;
  2931. num.exponent=explb-DECBIAS;
  2932. num.sign=sourhil & DECFLOAT_Sign;
  2933. decShowNum(&num, "res");
  2934. #endif
  2935. /*------------------------------------------------------------------*/
  2936. /* At this point the result is DECPMAX digits, ending at ulsd, so */
  2937. /* fits the encoding exactly; there is no possibility of error */
  2938. /*------------------------------------------------------------------*/
  2939. encode=((exprb>>DECECONL)<<4) + *(ulsd-DECPMAX+1); // make index
  2940. encode=DECCOMBFROM[encode]; // indexed by (0-2)*16+msd
  2941. // the exponent continuation can be extracted from the original RHS
  2942. encode|=sourhir & ECONMASK;
  2943. encode|=sourhil&DECFLOAT_Sign; // add the sign from LHS
  2944. // finally encode the coefficient
  2945. // private macro to encode a declet; this version can be used
  2946. // because all coefficient digits exist
  2947. #define getDPD3q(dpd, n) ub=ulsd-(3*(n))-2; \
  2948. dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
  2949. #if DOUBLE
  2950. getDPD3q(dpd, 4); encode|=dpd<<8;
  2951. getDPD3q(dpd, 3); encode|=dpd>>2;
  2952. DFWORD(result, 0)=encode;
  2953. encode=dpd<<30;
  2954. getDPD3q(dpd, 2); encode|=dpd<<20;
  2955. getDPD3q(dpd, 1); encode|=dpd<<10;
  2956. getDPD3q(dpd, 0); encode|=dpd;
  2957. DFWORD(result, 1)=encode;
  2958. #elif QUAD
  2959. getDPD3q(dpd,10); encode|=dpd<<4;
  2960. getDPD3q(dpd, 9); encode|=dpd>>6;
  2961. DFWORD(result, 0)=encode;
  2962. encode=dpd<<26;
  2963. getDPD3q(dpd, 8); encode|=dpd<<16;
  2964. getDPD3q(dpd, 7); encode|=dpd<<6;
  2965. getDPD3q(dpd, 6); encode|=dpd>>4;
  2966. DFWORD(result, 1)=encode;
  2967. encode=dpd<<28;
  2968. getDPD3q(dpd, 5); encode|=dpd<<18;
  2969. getDPD3q(dpd, 4); encode|=dpd<<8;
  2970. getDPD3q(dpd, 3); encode|=dpd>>2;
  2971. DFWORD(result, 2)=encode;
  2972. encode=dpd<<30;
  2973. getDPD3q(dpd, 2); encode|=dpd<<20;
  2974. getDPD3q(dpd, 1); encode|=dpd<<10;
  2975. getDPD3q(dpd, 0); encode|=dpd;
  2976. DFWORD(result, 3)=encode;
  2977. #endif
  2978. return result;
  2979. } // decFloatQuantize
  2980. /* ------------------------------------------------------------------ */
  2981. /* decFloatReduce -- reduce finite coefficient to minimum length */
  2982. /* */
  2983. /* result gets the reduced decFloat */
  2984. /* df is the source decFloat */
  2985. /* set is the context */
  2986. /* returns result, which will be canonical */
  2987. /* */
  2988. /* This removes all possible trailing zeros from the coefficient; */
  2989. /* some may remain when the number is very close to Nmax. */
  2990. /* Special values are unchanged and no status is set unless df=sNaN. */
  2991. /* Reduced zero has an exponent q=0. */
  2992. /* ------------------------------------------------------------------ */
  2993. decFloat * decFloatReduce(decFloat *result, const decFloat *df,
  2994. decContext *set) {
  2995. bcdnum num; // work
  2996. uByte buf[DECPMAX], *ub; // coefficient and pointer
  2997. if (df!=result) *result=*df; // copy, if needed
  2998. if (DFISNAN(df)) return decNaNs(result, df, NULL, set); // sNaN
  2999. // zeros and infinites propagate too
  3000. if (DFISINF(df)) return decInfinity(result, df); // canonical
  3001. if (DFISZERO(df)) {
  3002. uInt sign=DFWORD(df, 0)&DECFLOAT_Sign;
  3003. decFloatZero(result);
  3004. DFWORD(result, 0)|=sign;
  3005. return result; // exponent dropped, sign OK
  3006. }
  3007. // non-zero finite
  3008. GETCOEFF(df, buf);
  3009. ub=buf+DECPMAX-1; // -> lsd
  3010. if (*ub) return result; // no trailing zeros
  3011. for (ub--; *ub==0;) ub--; // terminates because non-zero
  3012. // *ub is the first non-zero from the right
  3013. num.sign=DFWORD(df, 0)&DECFLOAT_Sign; // set up number...
  3014. num.exponent=GETEXPUN(df)+(Int)(buf+DECPMAX-1-ub); // adjusted exponent
  3015. num.msd=buf;
  3016. num.lsd=ub;
  3017. return decFinalize(result, &num, set);
  3018. } // decFloatReduce
  3019. /* ------------------------------------------------------------------ */
  3020. /* decFloatRemainder -- integer divide and return remainder */
  3021. /* */
  3022. /* result gets the remainder of dividing dfl by dfr: */
  3023. /* dfl is the first decFloat (lhs) */
  3024. /* dfr is the second decFloat (rhs) */
  3025. /* set is the context */
  3026. /* returns result */
  3027. /* */
  3028. /* ------------------------------------------------------------------ */
  3029. decFloat * decFloatRemainder(decFloat *result,
  3030. const decFloat *dfl, const decFloat *dfr,
  3031. decContext *set) {
  3032. return decDivide(result, dfl, dfr, set, REMAINDER);
  3033. } // decFloatRemainder
  3034. /* ------------------------------------------------------------------ */
  3035. /* decFloatRemainderNear -- integer divide to nearest and remainder */
  3036. /* */
  3037. /* result gets the remainder of dividing dfl by dfr: */
  3038. /* dfl is the first decFloat (lhs) */
  3039. /* dfr is the second decFloat (rhs) */
  3040. /* set is the context */
  3041. /* returns result */
  3042. /* */
  3043. /* This is the IEEE remainder, where the nearest integer is used. */
  3044. /* ------------------------------------------------------------------ */
  3045. decFloat * decFloatRemainderNear(decFloat *result,
  3046. const decFloat *dfl, const decFloat *dfr,
  3047. decContext *set) {
  3048. return decDivide(result, dfl, dfr, set, REMNEAR);
  3049. } // decFloatRemainderNear
  3050. /* ------------------------------------------------------------------ */
  3051. /* decFloatRotate -- rotate the coefficient of a decFloat left/right */
  3052. /* */
  3053. /* result gets the result of rotating dfl */
  3054. /* dfl is the source decFloat to rotate */
  3055. /* dfr is the count of digits to rotate, an integer (with q=0) */
  3056. /* set is the context */
  3057. /* returns result */
  3058. /* */
  3059. /* The digits of the coefficient of dfl are rotated to the left (if */
  3060. /* dfr is positive) or to the right (if dfr is negative) without */
  3061. /* adjusting the exponent or the sign of dfl. */
  3062. /* */
  3063. /* dfr must be in the range -DECPMAX through +DECPMAX. */
  3064. /* NaNs are propagated as usual. An infinite dfl is unaffected (but */
  3065. /* dfr must be valid). No status is set unless dfr is invalid or an */
  3066. /* operand is an sNaN. The result is canonical. */
  3067. /* ------------------------------------------------------------------ */
  3068. #define PHALF (ROUNDUP(DECPMAX/2, 4)) // half length, rounded up
  3069. decFloat * decFloatRotate(decFloat *result,
  3070. const decFloat *dfl, const decFloat *dfr,
  3071. decContext *set) {
  3072. Int rotate; // dfr as an Int
  3073. uByte buf[DECPMAX+PHALF]; // coefficient + half
  3074. uInt digits, savestat; // work
  3075. bcdnum num; // ..
  3076. uByte *ub; // ..
  3077. if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  3078. if (!DFISINT(dfr)) return decInvalid(result, set);
  3079. digits=decFloatDigits(dfr); // calculate digits
  3080. if (digits>2) return decInvalid(result, set); // definitely out of range
  3081. rotate=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; // is in bottom declet
  3082. if (rotate>DECPMAX) return decInvalid(result, set); // too big
  3083. // [from here on no error or status change is possible]
  3084. if (DFISINF(dfl)) return decInfinity(result, dfl); // canonical
  3085. // handle no-rotate cases
  3086. if (rotate==0 || rotate==DECPMAX) return decCanonical(result, dfl);
  3087. // a real rotate is needed: 0 < rotate < DECPMAX
  3088. // reduce the rotation to no more than half to reduce copying later
  3089. // (for QUAD in fact half + 2 digits)
  3090. if (DFISSIGNED(dfr)) rotate=-rotate;
  3091. if (abs(rotate)>PHALF) {
  3092. if (rotate<0) rotate=DECPMAX+rotate;
  3093. else rotate=rotate-DECPMAX;
  3094. }
  3095. // now lay out the coefficient, leaving room to the right or the
  3096. // left depending on the direction of rotation
  3097. ub=buf;
  3098. if (rotate<0) ub+=PHALF; // rotate right, so space to left
  3099. GETCOEFF(dfl, ub);
  3100. // copy half the digits to left or right, and set num.msd
  3101. if (rotate<0) {
  3102. memcpy(buf, buf+DECPMAX, PHALF);
  3103. num.msd=buf+PHALF+rotate;
  3104. }
  3105. else {
  3106. memcpy(buf+DECPMAX, buf, PHALF);
  3107. num.msd=buf+rotate;
  3108. }
  3109. // fill in rest of num
  3110. num.lsd=num.msd+DECPMAX-1;
  3111. num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
  3112. num.exponent=GETEXPUN(dfl);
  3113. savestat=set->status; // record
  3114. decFinalize(result, &num, set);
  3115. set->status=savestat; // restore
  3116. return result;
  3117. } // decFloatRotate
  3118. /* ------------------------------------------------------------------ */
  3119. /* decFloatSameQuantum -- test decFloats for same quantum */
  3120. /* */
  3121. /* dfl is the first decFloat (lhs) */
  3122. /* dfr is the second decFloat (rhs) */
  3123. /* returns 1 if the operands have the same quantum, 0 otherwise */
  3124. /* */
  3125. /* No error is possible and no status results. */
  3126. /* ------------------------------------------------------------------ */
  3127. uInt decFloatSameQuantum(const decFloat *dfl, const decFloat *dfr) {
  3128. if (DFISSPECIAL(dfl) || DFISSPECIAL(dfr)) {
  3129. if (DFISNAN(dfl) && DFISNAN(dfr)) return 1;
  3130. if (DFISINF(dfl) && DFISINF(dfr)) return 1;
  3131. return 0; // any other special mixture gives false
  3132. }
  3133. if (GETEXP(dfl)==GETEXP(dfr)) return 1; // biased exponents match
  3134. return 0;
  3135. } // decFloatSameQuantum
  3136. /* ------------------------------------------------------------------ */
  3137. /* decFloatScaleB -- multiply by a power of 10, as per 754 */
  3138. /* */
  3139. /* result gets the result of the operation */
  3140. /* dfl is the first decFloat (lhs) */
  3141. /* dfr is the second decFloat (rhs), am integer (with q=0) */
  3142. /* set is the context */
  3143. /* returns result */
  3144. /* */
  3145. /* This computes result=dfl x 10**dfr where dfr is an integer in the */
  3146. /* range +/-2*(emax+pmax), typically resulting from LogB. */
  3147. /* Underflow and Overflow (with Inexact) may occur. NaNs propagate */
  3148. /* as usual. */
  3149. /* ------------------------------------------------------------------ */
  3150. #define SCALEBMAX 2*(DECEMAX+DECPMAX) // D=800, Q=12356
  3151. decFloat * decFloatScaleB(decFloat *result,
  3152. const decFloat *dfl, const decFloat *dfr,
  3153. decContext *set) {
  3154. uInt digits; // work
  3155. Int expr; // dfr as an Int
  3156. if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  3157. if (!DFISINT(dfr)) return decInvalid(result, set);
  3158. digits=decFloatDigits(dfr); // calculate digits
  3159. #if DOUBLE
  3160. if (digits>3) return decInvalid(result, set); // definitely out of range
  3161. expr=DPD2BIN[DFWORD(dfr, 1)&0x3ff]; // must be in bottom declet
  3162. #elif QUAD
  3163. if (digits>5) return decInvalid(result, set); // definitely out of range
  3164. expr=DPD2BIN[DFWORD(dfr, 3)&0x3ff] // in bottom 2 declets ..
  3165. +DPD2BIN[(DFWORD(dfr, 3)>>10)&0x3ff]*1000; // ..
  3166. #endif
  3167. if (expr>SCALEBMAX) return decInvalid(result, set); // oops
  3168. // [from now on no error possible]
  3169. if (DFISINF(dfl)) return decInfinity(result, dfl); // canonical
  3170. if (DFISSIGNED(dfr)) expr=-expr;
  3171. // dfl is finite and expr is valid
  3172. *result=*dfl; // copy to target
  3173. return decFloatSetExponent(result, set, GETEXPUN(result)+expr);
  3174. } // decFloatScaleB
  3175. /* ------------------------------------------------------------------ */
  3176. /* decFloatShift -- shift the coefficient of a decFloat left or right */
  3177. /* */
  3178. /* result gets the result of shifting dfl */
  3179. /* dfl is the source decFloat to shift */
  3180. /* dfr is the count of digits to shift, an integer (with q=0) */
  3181. /* set is the context */
  3182. /* returns result */
  3183. /* */
  3184. /* The digits of the coefficient of dfl are shifted to the left (if */
  3185. /* dfr is positive) or to the right (if dfr is negative) without */
  3186. /* adjusting the exponent or the sign of dfl. */
  3187. /* */
  3188. /* dfr must be in the range -DECPMAX through +DECPMAX. */
  3189. /* NaNs are propagated as usual. An infinite dfl is unaffected (but */
  3190. /* dfr must be valid). No status is set unless dfr is invalid or an */
  3191. /* operand is an sNaN. The result is canonical. */
  3192. /* ------------------------------------------------------------------ */
  3193. decFloat * decFloatShift(decFloat *result,
  3194. const decFloat *dfl, const decFloat *dfr,
  3195. decContext *set) {
  3196. Int shift; // dfr as an Int
  3197. uByte buf[DECPMAX*2]; // coefficient + padding
  3198. uInt digits, savestat; // work
  3199. bcdnum num; // ..
  3200. uInt uiwork; // for macros
  3201. if (DFISNAN(dfl)||DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set);
  3202. if (!DFISINT(dfr)) return decInvalid(result, set);
  3203. digits=decFloatDigits(dfr); // calculate digits
  3204. if (digits>2) return decInvalid(result, set); // definitely out of range
  3205. shift=DPD2BIN[DFWORD(dfr, DECWORDS-1)&0x3ff]; // is in bottom declet
  3206. if (shift>DECPMAX) return decInvalid(result, set); // too big
  3207. // [from here on no error or status change is possible]
  3208. if (DFISINF(dfl)) return decInfinity(result, dfl); // canonical
  3209. // handle no-shift and all-shift (clear to zero) cases
  3210. if (shift==0) return decCanonical(result, dfl);
  3211. if (shift==DECPMAX) { // zero with sign
  3212. uByte sign=(uByte)(DFBYTE(dfl, 0)&0x80); // save sign bit
  3213. decFloatZero(result); // make +0
  3214. DFBYTE(result, 0)=(uByte)(DFBYTE(result, 0)|sign); // and set sign
  3215. // [cannot safely use CopySign]
  3216. return result;
  3217. }
  3218. // a real shift is needed: 0 < shift < DECPMAX
  3219. num.sign=DFWORD(dfl, 0)&DECFLOAT_Sign;
  3220. num.exponent=GETEXPUN(dfl);
  3221. num.msd=buf;
  3222. GETCOEFF(dfl, buf);
  3223. if (DFISSIGNED(dfr)) { // shift right
  3224. // edge cases are taken care of, so this is easy
  3225. num.lsd=buf+DECPMAX-shift-1;
  3226. }
  3227. else { // shift left -- zero padding needed to right
  3228. UBFROMUI(buf+DECPMAX, 0); // 8 will handle most cases
  3229. UBFROMUI(buf+DECPMAX+4, 0); // ..
  3230. if (shift>8) memset(buf+DECPMAX+8, 0, 8+QUAD*18); // all other cases
  3231. num.msd+=shift;
  3232. num.lsd=num.msd+DECPMAX-1;
  3233. }
  3234. savestat=set->status; // record
  3235. decFinalize(result, &num, set);
  3236. set->status=savestat; // restore
  3237. return result;
  3238. } // decFloatShift
  3239. /* ------------------------------------------------------------------ */
  3240. /* decFloatSubtract -- subtract a decFloat from another */
  3241. /* */
  3242. /* result gets the result of subtracting dfr from dfl: */
  3243. /* dfl is the first decFloat (lhs) */
  3244. /* dfr is the second decFloat (rhs) */
  3245. /* set is the context */
  3246. /* returns result */
  3247. /* */
  3248. /* ------------------------------------------------------------------ */
  3249. decFloat * decFloatSubtract(decFloat *result,
  3250. const decFloat *dfl, const decFloat *dfr,
  3251. decContext *set) {
  3252. decFloat temp;
  3253. // NaNs must propagate without sign change
  3254. if (DFISNAN(dfr)) return decFloatAdd(result, dfl, dfr, set);
  3255. temp=*dfr; // make a copy
  3256. DFBYTE(&temp, 0)^=0x80; // flip sign
  3257. return decFloatAdd(result, dfl, &temp, set); // and add to the lhs
  3258. } // decFloatSubtract
  3259. /* ------------------------------------------------------------------ */
  3260. /* decFloatToInt -- round to 32-bit binary integer (4 flavours) */
  3261. /* */
  3262. /* df is the decFloat to round */
  3263. /* set is the context */
  3264. /* round is the rounding mode to use */
  3265. /* returns a uInt or an Int, rounded according to the name */
  3266. /* */
  3267. /* Invalid will always be signaled if df is a NaN, is Infinite, or is */
  3268. /* outside the range of the target; Inexact will not be signaled for */
  3269. /* simple rounding unless 'Exact' appears in the name. */
  3270. /* ------------------------------------------------------------------ */
  3271. uInt decFloatToUInt32(const decFloat *df, decContext *set,
  3272. enum rounding round) {
  3273. return decToInt32(df, set, round, 0, 1);}
  3274. uInt decFloatToUInt32Exact(const decFloat *df, decContext *set,
  3275. enum rounding round) {
  3276. return decToInt32(df, set, round, 1, 1);}
  3277. Int decFloatToInt32(const decFloat *df, decContext *set,
  3278. enum rounding round) {
  3279. return (Int)decToInt32(df, set, round, 0, 0);}
  3280. Int decFloatToInt32Exact(const decFloat *df, decContext *set,
  3281. enum rounding round) {
  3282. return (Int)decToInt32(df, set, round, 1, 0);}
  3283. /* ------------------------------------------------------------------ */
  3284. /* decFloatToIntegral -- round to integral value (two flavours) */
  3285. /* */
  3286. /* result gets the result */
  3287. /* df is the decFloat to round */
  3288. /* set is the context */
  3289. /* round is the rounding mode to use */
  3290. /* returns result */
  3291. /* */
  3292. /* No exceptions, even Inexact, are raised except for sNaN input, or */
  3293. /* if 'Exact' appears in the name. */
  3294. /* ------------------------------------------------------------------ */
  3295. decFloat * decFloatToIntegralValue(decFloat *result, const decFloat *df,
  3296. decContext *set, enum rounding round) {
  3297. return decToIntegral(result, df, set, round, 0);}
  3298. decFloat * decFloatToIntegralExact(decFloat *result, const decFloat *df,
  3299. decContext *set) {
  3300. return decToIntegral(result, df, set, set->round, 1);}
  3301. /* ------------------------------------------------------------------ */
  3302. /* decFloatXor -- logical digitwise XOR of two decFloats */
  3303. /* */
  3304. /* result gets the result of XORing dfl and dfr */
  3305. /* dfl is the first decFloat (lhs) */
  3306. /* dfr is the second decFloat (rhs) */
  3307. /* set is the context */
  3308. /* returns result, which will be canonical with sign=0 */
  3309. /* */
  3310. /* The operands must be positive, finite with exponent q=0, and */
  3311. /* comprise just zeros and ones; if not, Invalid operation results. */
  3312. /* ------------------------------------------------------------------ */
  3313. decFloat * decFloatXor(decFloat *result,
  3314. const decFloat *dfl, const decFloat *dfr,
  3315. decContext *set) {
  3316. if (!DFISUINT01(dfl) || !DFISUINT01(dfr)
  3317. || !DFISCC01(dfl) || !DFISCC01(dfr)) return decInvalid(result, set);
  3318. // the operands are positive finite integers (q=0) with just 0s and 1s
  3319. #if DOUBLE
  3320. DFWORD(result, 0)=ZEROWORD
  3321. |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04009124);
  3322. DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x49124491;
  3323. #elif QUAD
  3324. DFWORD(result, 0)=ZEROWORD
  3325. |((DFWORD(dfl, 0) ^ DFWORD(dfr, 0))&0x04000912);
  3326. DFWORD(result, 1)=(DFWORD(dfl, 1) ^ DFWORD(dfr, 1))&0x44912449;
  3327. DFWORD(result, 2)=(DFWORD(dfl, 2) ^ DFWORD(dfr, 2))&0x12449124;
  3328. DFWORD(result, 3)=(DFWORD(dfl, 3) ^ DFWORD(dfr, 3))&0x49124491;
  3329. #endif
  3330. return result;
  3331. } // decFloatXor
  3332. /* ------------------------------------------------------------------ */
  3333. /* decInvalid -- set Invalid_operation result */
  3334. /* */
  3335. /* result gets a canonical NaN */
  3336. /* set is the context */
  3337. /* returns result */
  3338. /* */
  3339. /* status has Invalid_operation added */
  3340. /* ------------------------------------------------------------------ */
  3341. static decFloat *decInvalid(decFloat *result, decContext *set) {
  3342. decFloatZero(result);
  3343. DFWORD(result, 0)=DECFLOAT_qNaN;
  3344. set->status|=DEC_Invalid_operation;
  3345. return result;
  3346. } // decInvalid
  3347. /* ------------------------------------------------------------------ */
  3348. /* decInfinity -- set canonical Infinity with sign from a decFloat */
  3349. /* */
  3350. /* result gets a canonical Infinity */
  3351. /* df is source decFloat (only the sign is used) */
  3352. /* returns result */
  3353. /* */
  3354. /* df may be the same as result */
  3355. /* ------------------------------------------------------------------ */
  3356. static decFloat *decInfinity(decFloat *result, const decFloat *df) {
  3357. uInt sign=DFWORD(df, 0); // save source signword
  3358. decFloatZero(result); // clear everything
  3359. DFWORD(result, 0)=DECFLOAT_Inf | (sign & DECFLOAT_Sign);
  3360. return result;
  3361. } // decInfinity
  3362. /* ------------------------------------------------------------------ */
  3363. /* decNaNs -- handle NaN argument(s) */
  3364. /* */
  3365. /* result gets the result of handling dfl and dfr, one or both of */
  3366. /* which is a NaN */
  3367. /* dfl is the first decFloat (lhs) */
  3368. /* dfr is the second decFloat (rhs) -- may be NULL for a single- */
  3369. /* operand operation */
  3370. /* set is the context */
  3371. /* returns result */
  3372. /* */
  3373. /* Called when one or both operands is a NaN, and propagates the */
  3374. /* appropriate result to res. When an sNaN is found, it is changed */
  3375. /* to a qNaN and Invalid operation is set. */
  3376. /* ------------------------------------------------------------------ */
  3377. static decFloat *decNaNs(decFloat *result,
  3378. const decFloat *dfl, const decFloat *dfr,
  3379. decContext *set) {
  3380. // handle sNaNs first
  3381. if (dfr!=NULL && DFISSNAN(dfr) && !DFISSNAN(dfl)) dfl=dfr; // use RHS
  3382. if (DFISSNAN(dfl)) {
  3383. decCanonical(result, dfl); // propagate canonical sNaN
  3384. DFWORD(result, 0)&=~(DECFLOAT_qNaN ^ DECFLOAT_sNaN); // quiet
  3385. set->status|=DEC_Invalid_operation;
  3386. return result;
  3387. }
  3388. // one or both is a quiet NaN
  3389. if (!DFISNAN(dfl)) dfl=dfr; // RHS must be NaN, use it
  3390. return decCanonical(result, dfl); // propagate canonical qNaN
  3391. } // decNaNs
  3392. /* ------------------------------------------------------------------ */
  3393. /* decNumCompare -- numeric comparison of two decFloats */
  3394. /* */
  3395. /* dfl is the left-hand decFloat, which is not a NaN */
  3396. /* dfr is the right-hand decFloat, which is not a NaN */
  3397. /* tot is 1 for total order compare, 0 for simple numeric */
  3398. /* returns -1, 0, or +1 for dfl<dfr, dfl=dfr, dfl>dfr */
  3399. /* */
  3400. /* No error is possible; status and mode are unchanged. */
  3401. /* ------------------------------------------------------------------ */
  3402. static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) {
  3403. Int sigl, sigr; // LHS and RHS non-0 signums
  3404. Int shift; // shift needed to align operands
  3405. uByte *ub, *uc; // work
  3406. uInt uiwork; // for macros
  3407. // buffers +2 if Quad (36 digits), need double plus 4 for safe padding
  3408. uByte bufl[DECPMAX*2+QUAD*2+4]; // for LHS coefficient + padding
  3409. uByte bufr[DECPMAX*2+QUAD*2+4]; // for RHS coefficient + padding
  3410. sigl=1;
  3411. if (DFISSIGNED(dfl)) {
  3412. if (!DFISSIGNED(dfr)) { // -LHS +RHS
  3413. if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
  3414. return -1; // RHS wins
  3415. }
  3416. sigl=-1;
  3417. }
  3418. if (DFISSIGNED(dfr)) {
  3419. if (!DFISSIGNED(dfl)) { // +LHS -RHS
  3420. if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
  3421. return +1; // LHS wins
  3422. }
  3423. }
  3424. // signs are the same; operand(s) could be zero
  3425. sigr=-sigl; // sign to return if abs(RHS) wins
  3426. if (DFISINF(dfl)) {
  3427. if (DFISINF(dfr)) return 0; // both infinite & same sign
  3428. return sigl; // inf > n
  3429. }
  3430. if (DFISINF(dfr)) return sigr; // n < inf [dfl is finite]
  3431. // here, both are same sign and finite; calculate their offset
  3432. shift=GETEXP(dfl)-GETEXP(dfr); // [0 means aligned]
  3433. // [bias can be ignored -- the absolute exponent is not relevant]
  3434. if (DFISZERO(dfl)) {
  3435. if (!DFISZERO(dfr)) return sigr; // LHS=0, RHS!=0
  3436. // both are zero, return 0 if both same exponent or numeric compare
  3437. if (shift==0 || !tot) return 0;
  3438. if (shift>0) return sigl;
  3439. return sigr; // [shift<0]
  3440. }
  3441. else { // LHS!=0
  3442. if (DFISZERO(dfr)) return sigl; // LHS!=0, RHS=0
  3443. }
  3444. // both are known to be non-zero at this point
  3445. // if the exponents are so different that the coefficients do not
  3446. // overlap (by even one digit) then a full comparison is not needed
  3447. if (abs(shift)>=DECPMAX) { // no overlap
  3448. // coefficients are known to be non-zero
  3449. if (shift>0) return sigl;
  3450. return sigr; // [shift<0]
  3451. }
  3452. // decode the coefficients
  3453. // (shift both right two if Quad to make a multiple of four)
  3454. #if QUAD
  3455. UBFROMUI(bufl, 0);
  3456. UBFROMUI(bufr, 0);
  3457. #endif
  3458. GETCOEFF(dfl, bufl+QUAD*2); // decode from decFloat
  3459. GETCOEFF(dfr, bufr+QUAD*2); // ..
  3460. if (shift==0) { // aligned; common and easy
  3461. // all multiples of four, here
  3462. for (ub=bufl, uc=bufr; ub<bufl+DECPMAX+QUAD*2; ub+=4, uc+=4) {
  3463. uInt ui=UBTOUI(ub);
  3464. if (ui==UBTOUI(uc)) continue; // so far so same
  3465. // about to find a winner; go by bytes in case little-endian
  3466. for (;; ub++, uc++) {
  3467. if (*ub>*uc) return sigl; // difference found
  3468. if (*ub<*uc) return sigr; // ..
  3469. }
  3470. }
  3471. } // aligned
  3472. else if (shift>0) { // lhs to left
  3473. ub=bufl; // RHS pointer
  3474. // pad bufl so right-aligned; most shifts will fit in 8
  3475. UBFROMUI(bufl+DECPMAX+QUAD*2, 0); // add eight zeros
  3476. UBFROMUI(bufl+DECPMAX+QUAD*2+4, 0); // ..
  3477. if (shift>8) {
  3478. // more than eight; fill the rest, and also worth doing the
  3479. // lead-in by fours
  3480. uByte *up; // work
  3481. uByte *upend=bufl+DECPMAX+QUAD*2+shift;
  3482. for (up=bufl+DECPMAX+QUAD*2+8; up<upend; up+=4) UBFROMUI(up, 0);
  3483. // [pads up to 36 in all for Quad]
  3484. for (;; ub+=4) {
  3485. if (UBTOUI(ub)!=0) return sigl;
  3486. if (ub+4>bufl+shift-4) break;
  3487. }
  3488. }
  3489. // check remaining leading digits
  3490. for (; ub<bufl+shift; ub++) if (*ub!=0) return sigl;
  3491. // now start the overlapped part; bufl has been padded, so the
  3492. // comparison can go for the full length of bufr, which is a
  3493. // multiple of 4 bytes
  3494. for (uc=bufr; ; uc+=4, ub+=4) {
  3495. uInt ui=UBTOUI(ub);
  3496. if (ui!=UBTOUI(uc)) { // mismatch found
  3497. for (;; uc++, ub++) { // check from left [little-endian?]
  3498. if (*ub>*uc) return sigl; // difference found
  3499. if (*ub<*uc) return sigr; // ..
  3500. }
  3501. } // mismatch
  3502. if (uc==bufr+QUAD*2+DECPMAX-4) break; // all checked
  3503. }
  3504. } // shift>0
  3505. else { // shift<0) .. RHS is to left of LHS; mirror shift>0
  3506. uc=bufr; // RHS pointer
  3507. // pad bufr so right-aligned; most shifts will fit in 8
  3508. UBFROMUI(bufr+DECPMAX+QUAD*2, 0); // add eight zeros
  3509. UBFROMUI(bufr+DECPMAX+QUAD*2+4, 0); // ..
  3510. if (shift<-8) {
  3511. // more than eight; fill the rest, and also worth doing the
  3512. // lead-in by fours
  3513. uByte *up; // work
  3514. uByte *upend=bufr+DECPMAX+QUAD*2-shift;
  3515. for (up=bufr+DECPMAX+QUAD*2+8; up<upend; up+=4) UBFROMUI(up, 0);
  3516. // [pads up to 36 in all for Quad]
  3517. for (;; uc+=4) {
  3518. if (UBTOUI(uc)!=0) return sigr;
  3519. if (uc+4>bufr-shift-4) break;
  3520. }
  3521. }
  3522. // check remaining leading digits
  3523. for (; uc<bufr-shift; uc++) if (*uc!=0) return sigr;
  3524. // now start the overlapped part; bufr has been padded, so the
  3525. // comparison can go for the full length of bufl, which is a
  3526. // multiple of 4 bytes
  3527. for (ub=bufl; ; ub+=4, uc+=4) {
  3528. uInt ui=UBTOUI(ub);
  3529. if (ui!=UBTOUI(uc)) { // mismatch found
  3530. for (;; ub++, uc++) { // check from left [little-endian?]
  3531. if (*ub>*uc) return sigl; // difference found
  3532. if (*ub<*uc) return sigr; // ..
  3533. }
  3534. } // mismatch
  3535. if (ub==bufl+QUAD*2+DECPMAX-4) break; // all checked
  3536. }
  3537. } // shift<0
  3538. // Here when compare equal
  3539. if (!tot) return 0; // numerically equal
  3540. // total ordering .. exponent matters
  3541. if (shift>0) return sigl; // total order by exponent
  3542. if (shift<0) return sigr; // ..
  3543. return 0;
  3544. } // decNumCompare
  3545. /* ------------------------------------------------------------------ */
  3546. /* decToInt32 -- local routine to effect ToInteger conversions */
  3547. /* */
  3548. /* df is the decFloat to convert */
  3549. /* set is the context */
  3550. /* rmode is the rounding mode to use */
  3551. /* exact is 1 if Inexact should be signalled */
  3552. /* unsign is 1 if the result a uInt, 0 if an Int (cast to uInt) */
  3553. /* returns 32-bit result as a uInt */
  3554. /* */
  3555. /* Invalid is set is df is a NaN, is infinite, or is out-of-range; in */
  3556. /* these cases 0 is returned. */
  3557. /* ------------------------------------------------------------------ */
  3558. static uInt decToInt32(const decFloat *df, decContext *set,
  3559. enum rounding rmode, Flag exact, Flag unsign) {
  3560. Int exp; // exponent
  3561. uInt sourhi, sourpen, sourlo; // top word from source decFloat ..
  3562. uInt hi, lo; // .. penultimate, least, etc.
  3563. decFloat zero, result; // work
  3564. Int i; // ..
  3565. /* Start decoding the argument */
  3566. sourhi=DFWORD(df, 0); // top word
  3567. exp=DECCOMBEXP[sourhi>>26]; // get exponent high bits (in place)
  3568. if (EXPISSPECIAL(exp)) { // is special?
  3569. set->status|=DEC_Invalid_operation; // signal
  3570. return 0;
  3571. }
  3572. /* Here when the argument is finite */
  3573. if (GETEXPUN(df)==0) result=*df; // already a true integer
  3574. else { // need to round to integer
  3575. enum rounding saveround; // saver
  3576. uInt savestatus; // ..
  3577. saveround=set->round; // save rounding mode ..
  3578. savestatus=set->status; // .. and status
  3579. set->round=rmode; // set mode
  3580. decFloatZero(&zero); // make 0E+0
  3581. set->status=0; // clear
  3582. decFloatQuantize(&result, df, &zero, set); // [this may fail]
  3583. set->round=saveround; // restore rounding mode ..
  3584. if (exact) set->status|=savestatus; // include Inexact
  3585. else set->status=savestatus; // .. or just original status
  3586. }
  3587. // only the last four declets of the coefficient can contain
  3588. // non-zero; check for others (and also NaN or Infinity from the
  3589. // Quantize) first (see DFISZERO for explanation):
  3590. // decFloatShow(&result, "sofar");
  3591. #if DOUBLE
  3592. if ((DFWORD(&result, 0)&0x1c03ff00)!=0
  3593. || (DFWORD(&result, 0)&0x60000000)==0x60000000) {
  3594. #elif QUAD
  3595. if ((DFWORD(&result, 2)&0xffffff00)!=0
  3596. || DFWORD(&result, 1)!=0
  3597. || (DFWORD(&result, 0)&0x1c003fff)!=0
  3598. || (DFWORD(&result, 0)&0x60000000)==0x60000000) {
  3599. #endif
  3600. set->status|=DEC_Invalid_operation; // Invalid or out of range
  3601. return 0;
  3602. }
  3603. // get last twelve digits of the coefficent into hi & ho, base
  3604. // 10**9 (see GETCOEFFBILL):
  3605. sourlo=DFWORD(&result, DECWORDS-1);
  3606. lo=DPD2BIN0[sourlo&0x3ff]
  3607. +DPD2BINK[(sourlo>>10)&0x3ff]
  3608. +DPD2BINM[(sourlo>>20)&0x3ff];
  3609. sourpen=DFWORD(&result, DECWORDS-2);
  3610. hi=DPD2BIN0[((sourpen<<2) | (sourlo>>30))&0x3ff];
  3611. // according to request, check range carefully
  3612. if (unsign) {
  3613. if (hi>4 || (hi==4 && lo>294967295) || (hi+lo!=0 && DFISSIGNED(&result))) {
  3614. set->status|=DEC_Invalid_operation; // out of range
  3615. return 0;
  3616. }
  3617. return hi*BILLION+lo;
  3618. }
  3619. // signed
  3620. if (hi>2 || (hi==2 && lo>147483647)) {
  3621. // handle the usual edge case
  3622. if (lo==147483648 && hi==2 && DFISSIGNED(&result)) return 0x80000000;
  3623. set->status|=DEC_Invalid_operation; // truly out of range
  3624. return 0;
  3625. }
  3626. i=hi*BILLION+lo;
  3627. if (DFISSIGNED(&result)) i=-i;
  3628. return (uInt)i;
  3629. } // decToInt32
  3630. /* ------------------------------------------------------------------ */
  3631. /* decToIntegral -- local routine to effect ToIntegral value */
  3632. /* */
  3633. /* result gets the result */
  3634. /* df is the decFloat to round */
  3635. /* set is the context */
  3636. /* rmode is the rounding mode to use */
  3637. /* exact is 1 if Inexact should be signalled */
  3638. /* returns result */
  3639. /* ------------------------------------------------------------------ */
  3640. static decFloat * decToIntegral(decFloat *result, const decFloat *df,
  3641. decContext *set, enum rounding rmode,
  3642. Flag exact) {
  3643. Int exp; // exponent
  3644. uInt sourhi; // top word from source decFloat
  3645. enum rounding saveround; // saver
  3646. uInt savestatus; // ..
  3647. decFloat zero; // work
  3648. /* Start decoding the argument */
  3649. sourhi=DFWORD(df, 0); // top word
  3650. exp=DECCOMBEXP[sourhi>>26]; // get exponent high bits (in place)
  3651. if (EXPISSPECIAL(exp)) { // is special?
  3652. // NaNs are handled as usual
  3653. if (DFISNAN(df)) return decNaNs(result, df, NULL, set);
  3654. // must be infinite; return canonical infinity with sign of df
  3655. return decInfinity(result, df);
  3656. }
  3657. /* Here when the argument is finite */
  3658. // complete extraction of the exponent
  3659. exp+=GETECON(df)-DECBIAS; // .. + continuation and unbias
  3660. if (exp>=0) return decCanonical(result, df); // already integral
  3661. saveround=set->round; // save rounding mode ..
  3662. savestatus=set->status; // .. and status
  3663. set->round=rmode; // set mode
  3664. decFloatZero(&zero); // make 0E+0
  3665. decFloatQuantize(result, df, &zero, set); // 'integrate'; cannot fail
  3666. set->round=saveround; // restore rounding mode ..
  3667. if (!exact) set->status=savestatus; // .. and status, unless exact
  3668. return result;
  3669. } // decToIntegral