| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935 |
- /*
- * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #include "mpdecimal.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <math.h>
- #include "basearith.h"
- #include "bits.h"
- #include "convolute.h"
- #include "crt.h"
- #include "errno.h"
- #include "memory.h"
- #include "typearith.h"
- #include "umodarith.h"
- #include "mptest.h"
- #include "mptypes.h"
- #ifdef PPRO
- #if defined(_MSC_VER)
- #include <float.h>
- #pragma fenv_access(on)
- #elif !defined(__OpenBSD__) && !defined(__NetBSD__)
- /* C99 */
- #include <fenv.h>
- #pragma STDC FENV_ACCESS ON
- #endif
- #endif
- #if defined(__x86_64__) && defined(__GLIBC__) && !defined(__INTEL_COMPILER)
- #define USE_80BIT_LONG_DOUBLE
- #endif
- #if defined(_MSC_VER)
- #define ALWAYS_INLINE __forceinline
- #elif defined(LEGACY_COMPILER)
- #define ALWAYS_INLINE
- #undef inline
- #define inline
- #else
- #ifdef TEST_COVERAGE
- #define ALWAYS_INLINE
- #else
- #define ALWAYS_INLINE inline __attribute__ ((always_inline))
- #endif
- #endif
- #define MPD_NEWTONDIV_CUTOFF 1024L
- #define MPD_NEW_STATIC(name, flags, exp, digits, len) \
- mpd_uint_t name##_data[MPD_MINALLOC_MAX]; \
- mpd_t name = {flags|MPD_STATIC|MPD_STATIC_DATA, exp, digits, \
- len, MPD_MINALLOC_MAX, name##_data}
- #define MPD_NEW_CONST(name, flags, exp, digits, len, alloc, initval) \
- mpd_uint_t name##_data[alloc] = {initval}; \
- mpd_t name = {flags|MPD_STATIC|MPD_CONST_DATA, exp, digits, \
- len, alloc, name##_data}
- #define MPD_NEW_SHARED(name, a) \
- mpd_t name = {(a->flags&~MPD_DATAFLAGS)|MPD_STATIC|MPD_SHARED_DATA, \
- a->exp, a->digits, a->len, a->alloc, a->data}
- static mpd_uint_t data_one[1] = {1};
- static mpd_uint_t data_zero[1] = {0};
- static const mpd_t one = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_one};
- static const mpd_t minus_one = {MPD_NEG|MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1,
- data_one};
- static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero};
- static inline void _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx,
- uint32_t *status);
- static void _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a,
- mpd_ssize_t exp);
- static inline mpd_ssize_t _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size);
- static void _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status);
- static inline void _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status);
- static void _mpd_qbarrett_divmod(mpd_t *q, mpd_t *r, const mpd_t *a,
- const mpd_t *b, uint32_t *status);
- static inline void _mpd_qpow_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp,
- uint8_t resultsign, const mpd_context_t *ctx, uint32_t *status);
- mpd_uint_t mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n);
- /******************************************************************************/
- /* Performance critical inline functions */
- /******************************************************************************/
- #ifdef CONFIG_64
- /* Digits in a word, primarily useful for the most significant word. */
- ALWAYS_INLINE int
- mpd_word_digits(mpd_uint_t word)
- {
- if (word < mpd_pow10[9]) {
- if (word < mpd_pow10[4]) {
- if (word < mpd_pow10[2]) {
- return (word < mpd_pow10[1]) ? 1 : 2;
- }
- return (word < mpd_pow10[3]) ? 3 : 4;
- }
- if (word < mpd_pow10[6]) {
- return (word < mpd_pow10[5]) ? 5 : 6;
- }
- if (word < mpd_pow10[8]) {
- return (word < mpd_pow10[7]) ? 7 : 8;
- }
- return 9;
- }
- if (word < mpd_pow10[14]) {
- if (word < mpd_pow10[11]) {
- return (word < mpd_pow10[10]) ? 10 : 11;
- }
- if (word < mpd_pow10[13]) {
- return (word < mpd_pow10[12]) ? 12 : 13;
- }
- return 14;
- }
- if (word < mpd_pow10[17]) {
- if (word < mpd_pow10[16]) {
- return (word < mpd_pow10[15]) ? 15 : 16;
- }
- return 17;
- }
- return (word < mpd_pow10[18]) ? 18 : 19;
- }
- #else
- ALWAYS_INLINE int
- mpd_word_digits(mpd_uint_t word)
- {
- if (word < mpd_pow10[4]) {
- if (word < mpd_pow10[2]) {
- return (word < mpd_pow10[1]) ? 1 : 2;
- }
- return (word < mpd_pow10[3]) ? 3 : 4;
- }
- if (word < mpd_pow10[6]) {
- return (word < mpd_pow10[5]) ? 5 : 6;
- }
- if (word < mpd_pow10[8]) {
- return (word < mpd_pow10[7]) ? 7 : 8;
- }
- return (word < mpd_pow10[9]) ? 9 : 10;
- }
- #endif
- /* Adjusted exponent */
- ALWAYS_INLINE mpd_ssize_t
- mpd_adjexp(const mpd_t *dec)
- {
- return (dec->exp + dec->digits) - 1;
- }
- /* Etiny */
- ALWAYS_INLINE mpd_ssize_t
- mpd_etiny(const mpd_context_t *ctx)
- {
- return ctx->emin - (ctx->prec - 1);
- }
- /* Etop: used for folding down in IEEE clamping */
- ALWAYS_INLINE mpd_ssize_t
- mpd_etop(const mpd_context_t *ctx)
- {
- return ctx->emax - (ctx->prec - 1);
- }
- /* Most significant word */
- ALWAYS_INLINE mpd_uint_t
- mpd_msword(const mpd_t *dec)
- {
- assert(dec->len > 0);
- return dec->data[dec->len-1];
- }
- /* Most significant digit of a word */
- inline mpd_uint_t
- mpd_msd(mpd_uint_t word)
- {
- int n;
- n = mpd_word_digits(word);
- return word / mpd_pow10[n-1];
- }
- /* Least significant digit of a word */
- ALWAYS_INLINE mpd_uint_t
- mpd_lsd(mpd_uint_t word)
- {
- return word % 10;
- }
- /* Coefficient size needed to store 'digits' */
- ALWAYS_INLINE mpd_ssize_t
- mpd_digits_to_size(mpd_ssize_t digits)
- {
- mpd_ssize_t q, r;
- _mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
- return (r == 0) ? q : q+1;
- }
- /* Number of digits in the exponent. Not defined for MPD_SSIZE_MIN. */
- inline int
- mpd_exp_digits(mpd_ssize_t exp)
- {
- exp = (exp < 0) ? -exp : exp;
- return mpd_word_digits(exp);
- }
- /* Canonical */
- ALWAYS_INLINE int
- mpd_iscanonical(const mpd_t *dec UNUSED)
- {
- return 1;
- }
- /* Finite */
- ALWAYS_INLINE int
- mpd_isfinite(const mpd_t *dec)
- {
- return !(dec->flags & MPD_SPECIAL);
- }
- /* Infinite */
- ALWAYS_INLINE int
- mpd_isinfinite(const mpd_t *dec)
- {
- return dec->flags & MPD_INF;
- }
- /* NaN */
- ALWAYS_INLINE int
- mpd_isnan(const mpd_t *dec)
- {
- return dec->flags & (MPD_NAN|MPD_SNAN);
- }
- /* Negative */
- ALWAYS_INLINE int
- mpd_isnegative(const mpd_t *dec)
- {
- return dec->flags & MPD_NEG;
- }
- /* Positive */
- ALWAYS_INLINE int
- mpd_ispositive(const mpd_t *dec)
- {
- return !(dec->flags & MPD_NEG);
- }
- /* qNaN */
- ALWAYS_INLINE int
- mpd_isqnan(const mpd_t *dec)
- {
- return dec->flags & MPD_NAN;
- }
- /* Signed */
- ALWAYS_INLINE int
- mpd_issigned(const mpd_t *dec)
- {
- return dec->flags & MPD_NEG;
- }
- /* sNaN */
- ALWAYS_INLINE int
- mpd_issnan(const mpd_t *dec)
- {
- return dec->flags & MPD_SNAN;
- }
- /* Special */
- ALWAYS_INLINE int
- mpd_isspecial(const mpd_t *dec)
- {
- return dec->flags & MPD_SPECIAL;
- }
- /* Zero */
- ALWAYS_INLINE int
- mpd_iszero(const mpd_t *dec)
- {
- return !mpd_isspecial(dec) && mpd_msword(dec) == 0;
- }
- /* Test for zero when specials have been ruled out already */
- ALWAYS_INLINE int
- mpd_iszerocoeff(const mpd_t *dec)
- {
- return mpd_msword(dec) == 0;
- }
- /* Normal */
- inline int
- mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx)
- {
- if (mpd_isspecial(dec)) return 0;
- if (mpd_iszerocoeff(dec)) return 0;
- return mpd_adjexp(dec) >= ctx->emin;
- }
- /* Subnormal */
- inline int
- mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx)
- {
- if (mpd_isspecial(dec)) return 0;
- if (mpd_iszerocoeff(dec)) return 0;
- return mpd_adjexp(dec) < ctx->emin;
- }
- /* Odd word */
- ALWAYS_INLINE int
- mpd_isoddword(mpd_uint_t word)
- {
- return word & 1;
- }
- /* Odd coefficient */
- ALWAYS_INLINE int
- mpd_isoddcoeff(const mpd_t *dec)
- {
- return mpd_isoddword(dec->data[0]);
- }
- /* 0 if dec is positive, 1 if dec is negative */
- ALWAYS_INLINE uint8_t
- mpd_sign(const mpd_t *dec)
- {
- return dec->flags & MPD_NEG;
- }
- /* 1 if dec is positive, -1 if dec is negative */
- ALWAYS_INLINE int
- mpd_arith_sign(const mpd_t *dec)
- {
- return 1 - 2 * mpd_isnegative(dec);
- }
- /* Radix */
- ALWAYS_INLINE long
- mpd_radix(void)
- {
- return 10;
- }
- /* Dynamic decimal */
- ALWAYS_INLINE int
- mpd_isdynamic(mpd_t *dec)
- {
- return !(dec->flags & MPD_STATIC);
- }
- /* Static decimal */
- ALWAYS_INLINE int
- mpd_isstatic(mpd_t *dec)
- {
- return dec->flags & MPD_STATIC;
- }
- /* Data of decimal is dynamic */
- ALWAYS_INLINE int
- mpd_isdynamic_data(mpd_t *dec)
- {
- return !(dec->flags & MPD_DATAFLAGS);
- }
- /* Data of decimal is static */
- ALWAYS_INLINE int
- mpd_isstatic_data(mpd_t *dec)
- {
- return dec->flags & MPD_STATIC_DATA;
- }
- /* Data of decimal is shared */
- ALWAYS_INLINE int
- mpd_isshared_data(mpd_t *dec)
- {
- return dec->flags & MPD_SHARED_DATA;
- }
- /* Data of decimal is const */
- ALWAYS_INLINE int
- mpd_isconst_data(mpd_t *dec)
- {
- return dec->flags & MPD_CONST_DATA;
- }
- /******************************************************************************/
- /* Inline memory handling */
- /******************************************************************************/
- /* Fill destination with zeros */
- ALWAYS_INLINE void
- mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len)
- {
- mpd_size_t i;
- for (i = 0; i < len; i++) {
- dest[i] = 0;
- }
- }
- /* Free a decimal */
- ALWAYS_INLINE void
- mpd_del(mpd_t *dec)
- {
- if (mpd_isdynamic_data(dec)) {
- mpd_free(dec->data);
- }
- if (mpd_isdynamic(dec)) {
- mpd_free(dec);
- }
- }
- /*
- * Update the memory size for the coefficient. Existing data up to size is
- * left untouched.
- *
- * Error handling: When relloc fails, result->data will still be a valid pointer
- * to the old memory area of size result->len. If the requested size is less than
- * result->len, we can continue normally, so we treat the failure as a soft error.
- * If the requested size is greater than the old area, MPD_Malloc_error is
- * set and the result will be a NaN.
- */
- ALWAYS_INLINE int
- mpd_qresize(mpd_t *result, mpd_ssize_t size, uint32_t *status)
- {
- assert(!mpd_isconst_data(result)); /* illegal operation for a const */
- assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
- if (mpd_isstatic_data(result)) {
- if (size > result->alloc) {
- return mpd_switch_to_dyn(result, size, status);
- }
- }
- else if (size != result->alloc && size >= MPD_MINALLOC) {
- return mpd_realloc_dyn(result, size, status);
- }
- return 1;
- }
- /* Same as mpd_qresize, but the complete coefficient (including the old
- * memory area!) is initialized to zero. */
- ALWAYS_INLINE int
- mpd_qresize_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status)
- {
- assert(!mpd_isconst_data(result)); /* illegal operation for a const */
- assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
- if (mpd_isstatic_data(result)) {
- if (size > result->alloc) {
- return mpd_switch_to_dyn_zero(result, size, status);
- }
- }
- else if (size != result->alloc && size >= MPD_MINALLOC) {
- if (!mpd_realloc_dyn(result, size, status)) {
- return 0;
- }
- }
- mpd_uint_zero(result->data, size);
- return 1;
- }
- /*
- * Reduce memory size for the coefficient to MPD_MINALLOC. In theory,
- * realloc may fail even when reducing the memory size. But in that case
- * the old memory area is always big enough, so checking for MPD_Malloc_error
- * is not imperative.
- */
- ALWAYS_INLINE void
- mpd_minalloc(mpd_t *result)
- {
- assert(!mpd_isconst_data(result)); /* illegal operation for a const */
- assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
- if (!mpd_isstatic_data(result) && result->alloc > MPD_MINALLOC) {
- uint8_t err = 0;
- result->data = mpd_realloc(result->data, MPD_MINALLOC,
- sizeof *result->data, &err);
- if (!err) {
- result->alloc = MPD_MINALLOC;
- }
- }
- }
- int
- mpd_resize(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx)
- {
- uint32_t status = 0;
- if (!mpd_qresize(result, size, &status)) {
- mpd_addstatus_raise(ctx, status);
- return 0;
- }
- return 1;
- }
- int
- mpd_resize_zero(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx)
- {
- uint32_t status = 0;
- if (!mpd_qresize_zero(result, size, &status)) {
- mpd_addstatus_raise(ctx, status);
- return 0;
- }
- return 1;
- }
- /******************************************************************************/
- /* Set attributes of a decimal */
- /******************************************************************************/
- /* Set digits. result->len is assumed to be correct. */
- inline void
- mpd_setdigits(mpd_t *result)
- {
- mpd_ssize_t wdigits = mpd_word_digits(mpd_msword(result));
- result->digits = wdigits + (result->len-1) * MPD_RDIGITS;
- }
- /* Set sign */
- ALWAYS_INLINE void
- mpd_set_sign(mpd_t *result, uint8_t sign)
- {
- result->flags &= ~MPD_NEG;
- result->flags |= sign;
- }
- /* Copy sign from another decimal */
- ALWAYS_INLINE void
- mpd_signcpy(mpd_t *result, mpd_t *a)
- {
- uint8_t sign = a->flags&MPD_NEG;
- result->flags &= ~MPD_NEG;
- result->flags |= sign;
- }
- /* Set infinity */
- ALWAYS_INLINE void
- mpd_set_infinity(mpd_t *result)
- {
- result->flags &= ~MPD_SPECIAL;
- result->flags |= MPD_INF;
- }
- /* Set qNaN */
- ALWAYS_INLINE void
- mpd_set_qnan(mpd_t *result)
- {
- result->flags &= ~MPD_SPECIAL;
- result->flags |= MPD_NAN;
- }
- /* Set sNaN */
- ALWAYS_INLINE void
- mpd_set_snan(mpd_t *result)
- {
- result->flags &= ~MPD_SPECIAL;
- result->flags |= MPD_SNAN;
- }
- /* Set to negative */
- ALWAYS_INLINE void
- mpd_set_negative(mpd_t *result)
- {
- result->flags |= MPD_NEG;
- }
- /* Set to positive */
- ALWAYS_INLINE void
- mpd_set_positive(mpd_t *result)
- {
- result->flags &= ~MPD_NEG;
- }
- /* Set to dynamic */
- ALWAYS_INLINE void
- mpd_set_dynamic(mpd_t *result)
- {
- result->flags &= ~MPD_STATIC;
- }
- /* Set to static */
- ALWAYS_INLINE void
- mpd_set_static(mpd_t *result)
- {
- result->flags |= MPD_STATIC;
- }
- /* Set data to dynamic */
- ALWAYS_INLINE void
- mpd_set_dynamic_data(mpd_t *result)
- {
- result->flags &= ~MPD_DATAFLAGS;
- }
- /* Set data to static */
- ALWAYS_INLINE void
- mpd_set_static_data(mpd_t *result)
- {
- result->flags &= ~MPD_DATAFLAGS;
- result->flags |= MPD_STATIC_DATA;
- }
- /* Set data to shared */
- ALWAYS_INLINE void
- mpd_set_shared_data(mpd_t *result)
- {
- result->flags &= ~MPD_DATAFLAGS;
- result->flags |= MPD_SHARED_DATA;
- }
- /* Set data to const */
- ALWAYS_INLINE void
- mpd_set_const_data(mpd_t *result)
- {
- result->flags &= ~MPD_DATAFLAGS;
- result->flags |= MPD_CONST_DATA;
- }
- /* Clear flags, preserving memory attributes. */
- ALWAYS_INLINE void
- mpd_clear_flags(mpd_t *result)
- {
- result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
- }
- /* Set flags, preserving memory attributes. */
- ALWAYS_INLINE void
- mpd_set_flags(mpd_t *result, uint8_t flags)
- {
- result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
- result->flags |= flags;
- }
- /* Copy flags, preserving memory attributes of result. */
- ALWAYS_INLINE void
- mpd_copy_flags(mpd_t *result, const mpd_t *a)
- {
- uint8_t aflags = a->flags;
- result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
- result->flags |= (aflags & ~(MPD_STATIC|MPD_DATAFLAGS));
- }
- /* Make a work context */
- static inline void
- mpd_workcontext(mpd_context_t *workctx, const mpd_context_t *ctx)
- {
- workctx->prec = ctx->prec;
- workctx->emax = ctx->emax;
- workctx->emin = ctx->emin;
- workctx->round = ctx->round;
- workctx->traps = 0;
- workctx->status= 0;
- workctx->newtrap= 0;
- workctx->clamp = ctx->clamp;
- workctx->allcr = ctx->allcr;
- }
- /******************************************************************************/
- /* Getting and setting parts of decimals */
- /******************************************************************************/
- /* Flip the sign of a decimal */
- static inline void
- _mpd_negate(mpd_t *dec)
- {
- dec->flags ^= MPD_NEG;
- }
- /* Set coefficient to zero */
- void
- mpd_zerocoeff(mpd_t *result)
- {
- mpd_minalloc(result);
- result->digits = 1;
- result->len = 1;
- result->data[0] = 0;
- }
- /* Set the coefficient to all nines. */
- void
- mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_ssize_t len, r;
- _mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
- len = (r == 0) ? len : len+1;
- if (!mpd_qresize(result, len, status)) {
- return;
- }
- result->len = len;
- result->digits = ctx->prec;
- --len;
- if (r > 0) {
- result->data[len--] = mpd_pow10[r]-1;
- }
- for (; len >= 0; --len) {
- result->data[len] = MPD_RADIX-1;
- }
- }
- /*
- * Cut off the most significant digits so that the rest fits in ctx->prec.
- * Cannot fail.
- */
- static void
- _mpd_cap(mpd_t *result, const mpd_context_t *ctx)
- {
- uint32_t dummy;
- mpd_ssize_t len, r;
- if (result->len > 0 && result->digits > ctx->prec) {
- _mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
- len = (r == 0) ? len : len+1;
- if (r != 0) {
- result->data[len-1] %= mpd_pow10[r];
- }
- len = _mpd_real_size(result->data, len);
- /* resize to fewer words cannot fail */
- mpd_qresize(result, len, &dummy);
- result->len = len;
- mpd_setdigits(result);
- }
- if (mpd_iszero(result)) {
- _settriple(result, mpd_sign(result), 0, result->exp);
- }
- }
- /*
- * Cut off the most significant digits of a NaN payload so that the rest
- * fits in ctx->prec - ctx->clamp. Cannot fail.
- */
- static void
- _mpd_fix_nan(mpd_t *result, const mpd_context_t *ctx)
- {
- uint32_t dummy;
- mpd_ssize_t prec;
- mpd_ssize_t len, r;
- prec = ctx->prec - ctx->clamp;
- if (result->len > 0 && result->digits > prec) {
- if (prec == 0) {
- mpd_minalloc(result);
- result->len = result->digits = 0;
- }
- else {
- _mpd_idiv_word(&len, &r, prec, MPD_RDIGITS);
- len = (r == 0) ? len : len+1;
- if (r != 0) {
- result->data[len-1] %= mpd_pow10[r];
- }
- len = _mpd_real_size(result->data, len);
- /* resize to fewer words cannot fail */
- mpd_qresize(result, len, &dummy);
- result->len = len;
- mpd_setdigits(result);
- if (mpd_iszerocoeff(result)) {
- /* NaN0 is not a valid representation */
- result->len = result->digits = 0;
- }
- }
- }
- }
- /*
- * Get n most significant digits from a decimal, where 0 < n <= MPD_UINT_DIGITS.
- * Assumes MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for 32 and 64 bit
- * machines.
- *
- * The result of the operation will be in lo. If the operation is impossible,
- * hi will be nonzero. This is used to indicate an error.
- */
- static inline void
- _mpd_get_msdigits(mpd_uint_t *hi, mpd_uint_t *lo, const mpd_t *dec,
- unsigned int n)
- {
- mpd_uint_t r, tmp;
- assert(0 < n && n <= MPD_RDIGITS+1);
- _mpd_div_word(&tmp, &r, dec->digits, MPD_RDIGITS);
- r = (r == 0) ? MPD_RDIGITS : r; /* digits in the most significant word */
- *hi = 0;
- *lo = dec->data[dec->len-1];
- if (n <= r) {
- *lo /= mpd_pow10[r-n];
- }
- else if (dec->len > 1) {
- /* at this point 1 <= r < n <= MPD_RDIGITS+1 */
- _mpd_mul_words(hi, lo, *lo, mpd_pow10[n-r]);
- tmp = dec->data[dec->len-2] / mpd_pow10[MPD_RDIGITS-(n-r)];
- *lo = *lo + tmp;
- if (*lo < tmp) (*hi)++;
- }
- }
- /******************************************************************************/
- /* Gathering information about a decimal */
- /******************************************************************************/
- /* The real size of the coefficient without leading zero words. */
- static inline mpd_ssize_t
- _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size)
- {
- while (size > 1 && data[size-1] == 0) {
- size--;
- }
- return size;
- }
- /* Return number of trailing zeros. No errors are possible. */
- mpd_ssize_t
- mpd_trail_zeros(const mpd_t *dec)
- {
- mpd_uint_t word;
- mpd_ssize_t i, tz = 0;
- for (i=0; i < dec->len; ++i) {
- if (dec->data[i] != 0) {
- word = dec->data[i];
- tz = i * MPD_RDIGITS;
- while (word % 10 == 0) {
- word /= 10;
- tz++;
- }
- break;
- }
- }
- return tz;
- }
- /* Integer: Undefined for specials */
- static int
- _mpd_isint(const mpd_t *dec)
- {
- mpd_ssize_t tz;
- if (mpd_iszerocoeff(dec)) {
- return 1;
- }
- tz = mpd_trail_zeros(dec);
- return (dec->exp + tz >= 0);
- }
- /* Integer */
- int
- mpd_isinteger(const mpd_t *dec)
- {
- if (mpd_isspecial(dec)) {
- return 0;
- }
- return _mpd_isint(dec);
- }
- /* Word is a power of 10 */
- static int
- mpd_word_ispow10(mpd_uint_t word)
- {
- int n;
- n = mpd_word_digits(word);
- if (word == mpd_pow10[n-1]) {
- return 1;
- }
- return 0;
- }
- /* Coefficient is a power of 10 */
- static int
- mpd_coeff_ispow10(const mpd_t *dec)
- {
- if (mpd_word_ispow10(mpd_msword(dec))) {
- if (_mpd_isallzero(dec->data, dec->len-1)) {
- return 1;
- }
- }
- return 0;
- }
- /* All digits of a word are nines */
- static int
- mpd_word_isallnine(mpd_uint_t word)
- {
- int n;
- n = mpd_word_digits(word);
- if (word == mpd_pow10[n]-1) {
- return 1;
- }
- return 0;
- }
- /* All digits of the coefficient are nines */
- static int
- mpd_coeff_isallnine(const mpd_t *dec)
- {
- if (mpd_word_isallnine(mpd_msword(dec))) {
- if (_mpd_isallnine(dec->data, dec->len-1)) {
- return 1;
- }
- }
- return 0;
- }
- /* Odd decimal: Undefined for non-integers! */
- int
- mpd_isodd(const mpd_t *dec)
- {
- mpd_uint_t q, r;
- assert(mpd_isinteger(dec));
- if (mpd_iszerocoeff(dec)) return 0;
- if (dec->exp < 0) {
- _mpd_div_word(&q, &r, -dec->exp, MPD_RDIGITS);
- q = dec->data[q] / mpd_pow10[r];
- return mpd_isoddword(q);
- }
- return dec->exp == 0 && mpd_isoddword(dec->data[0]);
- }
- /* Even: Undefined for non-integers! */
- int
- mpd_iseven(const mpd_t *dec)
- {
- return !mpd_isodd(dec);
- }
- /******************************************************************************/
- /* Getting and setting decimals */
- /******************************************************************************/
- /* Internal function: Set a static decimal from a triple, no error checking. */
- static void
- _ssettriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
- {
- mpd_set_flags(result, sign);
- result->exp = exp;
- _mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
- result->len = (result->data[1] == 0) ? 1 : 2;
- mpd_setdigits(result);
- }
- /* Internal function: Set a decimal from a triple, no error checking. */
- static void
- _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
- {
- mpd_minalloc(result);
- mpd_set_flags(result, sign);
- result->exp = exp;
- _mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
- result->len = (result->data[1] == 0) ? 1 : 2;
- mpd_setdigits(result);
- }
- /* Set a special number from a triple */
- void
- mpd_setspecial(mpd_t *result, uint8_t sign, uint8_t type)
- {
- mpd_minalloc(result);
- result->flags &= ~(MPD_NEG|MPD_SPECIAL);
- result->flags |= (sign|type);
- result->exp = result->digits = result->len = 0;
- }
- /* Set result of NaN with an error status */
- void
- mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status)
- {
- mpd_minalloc(result);
- mpd_set_qnan(result);
- mpd_set_positive(result);
- result->exp = result->digits = result->len = 0;
- *status |= flags;
- }
- /* quietly set a static decimal from an mpd_ssize_t */
- void
- mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_uint_t u;
- uint8_t sign = MPD_POS;
- if (a < 0) {
- if (a == MPD_SSIZE_MIN) {
- u = (mpd_uint_t)MPD_SSIZE_MAX +
- (-(MPD_SSIZE_MIN+MPD_SSIZE_MAX));
- }
- else {
- u = -a;
- }
- sign = MPD_NEG;
- }
- else {
- u = a;
- }
- _ssettriple(result, sign, u, 0);
- mpd_qfinalize(result, ctx, status);
- }
- /* quietly set a static decimal from an mpd_uint_t */
- void
- mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- _ssettriple(result, MPD_POS, a, 0);
- mpd_qfinalize(result, ctx, status);
- }
- /* quietly set a static decimal from an int32_t */
- void
- mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_qsset_ssize(result, a, ctx, status);
- }
- /* quietly set a static decimal from a uint32_t */
- void
- mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_qsset_uint(result, a, ctx, status);
- }
- #ifdef CONFIG_64
- /* quietly set a static decimal from an int64_t */
- void
- mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_qsset_ssize(result, a, ctx, status);
- }
- /* quietly set a static decimal from a uint64_t */
- void
- mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_qsset_uint(result, a, ctx, status);
- }
- #endif
- /* quietly set a decimal from an mpd_ssize_t */
- void
- mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_minalloc(result);
- mpd_qsset_ssize(result, a, ctx, status);
- }
- /* quietly set a decimal from an mpd_uint_t */
- void
- mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- _settriple(result, MPD_POS, a, 0);
- mpd_qfinalize(result, ctx, status);
- }
- /* quietly set a decimal from an int32_t */
- void
- mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_qset_ssize(result, a, ctx, status);
- }
- /* quietly set a decimal from a uint32_t */
- void
- mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_qset_uint(result, a, ctx, status);
- }
- #if defined(CONFIG_32) && !defined(LEGACY_COMPILER)
- /* set a decimal from a uint64_t */
- static void
- _c32setu64(mpd_t *result, uint64_t u, uint8_t sign, uint32_t *status)
- {
- mpd_uint_t w[3];
- uint64_t q;
- int i, len;
- len = 0;
- do {
- q = u / MPD_RADIX;
- w[len] = (mpd_uint_t)(u - q * MPD_RADIX);
- u = q; len++;
- } while (u != 0);
- if (!mpd_qresize(result, len, status)) {
- return;
- }
- for (i = 0; i < len; i++) {
- result->data[i] = w[i];
- }
- mpd_set_sign(result, sign);
- result->exp = 0;
- result->len = len;
- mpd_setdigits(result);
- }
- static void
- _c32_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- _c32setu64(result, a, MPD_POS, status);
- mpd_qfinalize(result, ctx, status);
- }
- /* set a decimal from an int64_t */
- static void
- _c32_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- uint64_t u;
- uint8_t sign = MPD_POS;
- if (a < 0) {
- if (a == INT64_MIN) {
- u = (uint64_t)INT64_MAX + (-(INT64_MIN+INT64_MAX));
- }
- else {
- u = -a;
- }
- sign = MPD_NEG;
- }
- else {
- u = a;
- }
- _c32setu64(result, u, sign, status);
- mpd_qfinalize(result, ctx, status);
- }
- #endif /* CONFIG_32 && !LEGACY_COMPILER */
- #ifndef LEGACY_COMPILER
- /* quietly set a decimal from an int64_t */
- void
- mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- #ifdef CONFIG_64
- mpd_qset_ssize(result, a, ctx, status);
- #else
- _c32_qset_i64(result, a, ctx, status);
- #endif
- }
- /* quietly set a decimal from a uint64_t */
- void
- mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- #ifdef CONFIG_64
- mpd_qset_uint(result, a, ctx, status);
- #else
- _c32_qset_u64(result, a, ctx, status);
- #endif
- }
- #endif /* !LEGACY_COMPILER */
- /*
- * Quietly get an mpd_uint_t from a decimal. Assumes
- * MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for
- * 32 and 64 bit machines.
- *
- * If the operation is impossible, MPD_Invalid_operation is set.
- */
- static mpd_uint_t
- _mpd_qget_uint(int use_sign, const mpd_t *a, uint32_t *status)
- {
- mpd_t tmp;
- mpd_uint_t tmp_data[2];
- mpd_uint_t lo, hi;
- if (mpd_isspecial(a)) {
- *status |= MPD_Invalid_operation;
- return MPD_UINT_MAX;
- }
- if (mpd_iszero(a)) {
- return 0;
- }
- if (use_sign && mpd_isnegative(a)) {
- *status |= MPD_Invalid_operation;
- return MPD_UINT_MAX;
- }
- if (a->digits+a->exp > MPD_RDIGITS+1) {
- *status |= MPD_Invalid_operation;
- return MPD_UINT_MAX;
- }
- if (a->exp < 0) {
- if (!_mpd_isint(a)) {
- *status |= MPD_Invalid_operation;
- return MPD_UINT_MAX;
- }
- /* At this point a->digits+a->exp <= MPD_RDIGITS+1,
- * so the shift fits. */
- tmp.data = tmp_data;
- tmp.flags = MPD_STATIC|MPD_CONST_DATA;
- mpd_qsshiftr(&tmp, a, -a->exp);
- tmp.exp = 0;
- a = &tmp;
- }
- _mpd_get_msdigits(&hi, &lo, a, MPD_RDIGITS+1);
- if (hi) {
- *status |= MPD_Invalid_operation;
- return MPD_UINT_MAX;
- }
- if (a->exp > 0) {
- _mpd_mul_words(&hi, &lo, lo, mpd_pow10[a->exp]);
- if (hi) {
- *status |= MPD_Invalid_operation;
- return MPD_UINT_MAX;
- }
- }
- return lo;
- }
- /*
- * Sets Invalid_operation for:
- * - specials
- * - negative numbers (except negative zero)
- * - non-integers
- * - overflow
- */
- mpd_uint_t
- mpd_qget_uint(const mpd_t *a, uint32_t *status)
- {
- return _mpd_qget_uint(1, a, status);
- }
- /* Same as above, but gets the absolute value, i.e. the sign is ignored. */
- mpd_uint_t
- mpd_qabs_uint(const mpd_t *a, uint32_t *status)
- {
- return _mpd_qget_uint(0, a, status);
- }
- /* quietly get an mpd_ssize_t from a decimal */
- mpd_ssize_t
- mpd_qget_ssize(const mpd_t *a, uint32_t *status)
- {
- mpd_uint_t u;
- int isneg;
- u = mpd_qabs_uint(a, status);
- if (*status&MPD_Invalid_operation) {
- return MPD_SSIZE_MAX;
- }
- isneg = mpd_isnegative(a);
- if (u <= MPD_SSIZE_MAX) {
- return isneg ? -((mpd_ssize_t)u) : (mpd_ssize_t)u;
- }
- else if (isneg && u-1 == MPD_SSIZE_MAX) {
- return MPD_SSIZE_MIN;
- }
- *status |= MPD_Invalid_operation;
- return MPD_SSIZE_MAX;
- }
- #ifdef CONFIG_64
- /* quietly get a uint64_t from a decimal */
- uint64_t
- mpd_qget_u64(const mpd_t *a, uint32_t *status)
- {
- return mpd_qget_uint(a, status);
- }
- /* quietly get an int64_t from a decimal */
- int64_t
- mpd_qget_i64(const mpd_t *a, uint32_t *status)
- {
- return mpd_qget_ssize(a, status);
- }
- #else
- /* quietly get a uint32_t from a decimal */
- uint32_t
- mpd_qget_u32(const mpd_t *a, uint32_t *status)
- {
- return mpd_qget_uint(a, status);
- }
- /* quietly get an int32_t from a decimal */
- int32_t
- mpd_qget_i32(const mpd_t *a, uint32_t *status)
- {
- return mpd_qget_ssize(a, status);
- }
- #endif
- /******************************************************************************/
- /* Filtering input of functions, finalizing output of functions */
- /******************************************************************************/
- /*
- * Check if the operand is NaN, copy to result and return 1 if this is
- * the case. Copying can fail since NaNs are allowed to have a payload that
- * does not fit in MPD_MINALLOC.
- */
- int
- mpd_qcheck_nan(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isnan(a)) {
- *status |= mpd_issnan(a) ? MPD_Invalid_operation : 0;
- mpd_qcopy(result, a, status);
- mpd_set_qnan(result);
- _mpd_fix_nan(result, ctx);
- return 1;
- }
- return 0;
- }
- /*
- * Check if either operand is NaN, copy to result and return 1 if this
- * is the case. Copying can fail since NaNs are allowed to have a payload
- * that does not fit in MPD_MINALLOC.
- */
- int
- mpd_qcheck_nans(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- if ((a->flags|b->flags)&(MPD_NAN|MPD_SNAN)) {
- const mpd_t *choice = b;
- if (mpd_issnan(a)) {
- choice = a;
- *status |= MPD_Invalid_operation;
- }
- else if (mpd_issnan(b)) {
- *status |= MPD_Invalid_operation;
- }
- else if (mpd_isqnan(a)) {
- choice = a;
- }
- mpd_qcopy(result, choice, status);
- mpd_set_qnan(result);
- _mpd_fix_nan(result, ctx);
- return 1;
- }
- return 0;
- }
- /*
- * Check if one of the operands is NaN, copy to result and return 1 if this
- * is the case. Copying can fail since NaNs are allowed to have a payload
- * that does not fit in MPD_MINALLOC.
- */
- static int
- mpd_qcheck_3nans(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c,
- const mpd_context_t *ctx, uint32_t *status)
- {
- if ((a->flags|b->flags|c->flags)&(MPD_NAN|MPD_SNAN)) {
- const mpd_t *choice = c;
- if (mpd_issnan(a)) {
- choice = a;
- *status |= MPD_Invalid_operation;
- }
- else if (mpd_issnan(b)) {
- choice = b;
- *status |= MPD_Invalid_operation;
- }
- else if (mpd_issnan(c)) {
- *status |= MPD_Invalid_operation;
- }
- else if (mpd_isqnan(a)) {
- choice = a;
- }
- else if (mpd_isqnan(b)) {
- choice = b;
- }
- mpd_qcopy(result, choice, status);
- mpd_set_qnan(result);
- _mpd_fix_nan(result, ctx);
- return 1;
- }
- return 0;
- }
- /* Check if rounding digit 'rnd' leads to an increment. */
- static inline int
- _mpd_rnd_incr(const mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx)
- {
- int ld;
- switch (ctx->round) {
- case MPD_ROUND_DOWN: case MPD_ROUND_TRUNC:
- return 0;
- case MPD_ROUND_HALF_UP:
- return (rnd >= 5);
- case MPD_ROUND_HALF_EVEN:
- return (rnd > 5) || ((rnd == 5) && mpd_isoddcoeff(dec));
- case MPD_ROUND_CEILING:
- return !(rnd == 0 || mpd_isnegative(dec));
- case MPD_ROUND_FLOOR:
- return !(rnd == 0 || mpd_ispositive(dec));
- case MPD_ROUND_HALF_DOWN:
- return (rnd > 5);
- case MPD_ROUND_UP:
- return !(rnd == 0);
- case MPD_ROUND_05UP:
- ld = (int)mpd_lsd(dec->data[0]);
- return (!(rnd == 0) && (ld == 0 || ld == 5));
- default:
- /* Without a valid context, further results will be undefined. */
- return 0; /* GCOV_NOT_REACHED */
- }
- }
- /*
- * Apply rounding to a decimal that has been right-shifted into a full
- * precision decimal. If an increment leads to an overflow of the precision,
- * adjust the coefficient and the exponent and check the new exponent for
- * overflow.
- */
- static inline void
- _mpd_apply_round(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (_mpd_rnd_incr(dec, rnd, ctx)) {
- /* We have a number with exactly ctx->prec digits. The increment
- * can only lead to an overflow if the decimal is all nines. In
- * that case, the result is a power of ten with prec+1 digits.
- *
- * If the precision is a multiple of MPD_RDIGITS, this situation is
- * detected by _mpd_baseincr returning a carry.
- * If the precision is not a multiple of MPD_RDIGITS, we have to
- * check if the result has one digit too many.
- */
- mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
- if (carry) {
- dec->data[dec->len-1] = mpd_pow10[MPD_RDIGITS-1];
- dec->exp += 1;
- _mpd_check_exp(dec, ctx, status);
- return;
- }
- mpd_setdigits(dec);
- if (dec->digits > ctx->prec) {
- mpd_qshiftr_inplace(dec, 1);
- dec->exp += 1;
- dec->digits = ctx->prec;
- _mpd_check_exp(dec, ctx, status);
- }
- }
- }
- /*
- * Apply rounding to a decimal. Allow overflow of the precision.
- */
- static inline void
- _mpd_apply_round_excess(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (_mpd_rnd_incr(dec, rnd, ctx)) {
- mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
- if (carry) {
- if (!mpd_qresize(dec, dec->len+1, status)) {
- return;
- }
- dec->data[dec->len] = 1;
- dec->len += 1;
- }
- mpd_setdigits(dec);
- }
- }
- /*
- * Apply rounding to a decimal that has been right-shifted into a decimal
- * with full precision or less. Return failure if an increment would
- * overflow the precision.
- */
- static inline int
- _mpd_apply_round_fit(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (_mpd_rnd_incr(dec, rnd, ctx)) {
- mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
- if (carry) {
- if (!mpd_qresize(dec, dec->len+1, status)) {
- return 0;
- }
- dec->data[dec->len] = 1;
- dec->len += 1;
- }
- mpd_setdigits(dec);
- if (dec->digits > ctx->prec) {
- mpd_seterror(dec, MPD_Invalid_operation, status);
- return 0;
- }
- }
- return 1;
- }
- /* Check a normal number for overflow, underflow, clamping. If the operand
- is modified, it will be zero, special or (sub)normal with a coefficient
- that fits into the current context precision. */
- static inline void
- _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_ssize_t adjexp, etiny, shift;
- int rnd;
- adjexp = mpd_adjexp(dec);
- if (adjexp > ctx->emax) {
- if (mpd_iszerocoeff(dec)) {
- dec->exp = ctx->emax;
- if (ctx->clamp) {
- dec->exp -= (ctx->prec-1);
- }
- mpd_zerocoeff(dec);
- *status |= MPD_Clamped;
- return;
- }
- switch (ctx->round) {
- case MPD_ROUND_HALF_UP: case MPD_ROUND_HALF_EVEN:
- case MPD_ROUND_HALF_DOWN: case MPD_ROUND_UP:
- case MPD_ROUND_TRUNC:
- mpd_setspecial(dec, mpd_sign(dec), MPD_INF);
- break;
- case MPD_ROUND_DOWN: case MPD_ROUND_05UP:
- mpd_qmaxcoeff(dec, ctx, status);
- dec->exp = ctx->emax - ctx->prec + 1;
- break;
- case MPD_ROUND_CEILING:
- if (mpd_isnegative(dec)) {
- mpd_qmaxcoeff(dec, ctx, status);
- dec->exp = ctx->emax - ctx->prec + 1;
- }
- else {
- mpd_setspecial(dec, MPD_POS, MPD_INF);
- }
- break;
- case MPD_ROUND_FLOOR:
- if (mpd_ispositive(dec)) {
- mpd_qmaxcoeff(dec, ctx, status);
- dec->exp = ctx->emax - ctx->prec + 1;
- }
- else {
- mpd_setspecial(dec, MPD_NEG, MPD_INF);
- }
- break;
- default: /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
- } /* fold down */
- else if (ctx->clamp && dec->exp > mpd_etop(ctx)) {
- /* At this point adjexp=exp+digits-1 <= emax and exp > etop=emax-prec+1:
- * (1) shift = exp -emax+prec-1 > 0
- * (2) digits+shift = exp+digits-1 - emax + prec <= prec */
- shift = dec->exp - mpd_etop(ctx);
- if (!mpd_qshiftl(dec, dec, shift, status)) {
- return;
- }
- dec->exp -= shift;
- *status |= MPD_Clamped;
- if (!mpd_iszerocoeff(dec) && adjexp < ctx->emin) {
- /* Underflow is impossible, since exp < etiny=emin-prec+1
- * and exp > etop=emax-prec+1 would imply emax < emin. */
- *status |= MPD_Subnormal;
- }
- }
- else if (adjexp < ctx->emin) {
- etiny = mpd_etiny(ctx);
- if (mpd_iszerocoeff(dec)) {
- if (dec->exp < etiny) {
- dec->exp = etiny;
- mpd_zerocoeff(dec);
- *status |= MPD_Clamped;
- }
- return;
- }
- *status |= MPD_Subnormal;
- if (dec->exp < etiny) {
- /* At this point adjexp=exp+digits-1 < emin and exp < etiny=emin-prec+1:
- * (1) shift = emin-prec+1 - exp > 0
- * (2) digits-shift = exp+digits-1 - emin + prec < prec */
- shift = etiny - dec->exp;
- rnd = (int)mpd_qshiftr_inplace(dec, shift);
- dec->exp = etiny;
- /* We always have a spare digit in case of an increment. */
- _mpd_apply_round_excess(dec, rnd, ctx, status);
- *status |= MPD_Rounded;
- if (rnd) {
- *status |= (MPD_Inexact|MPD_Underflow);
- if (mpd_iszerocoeff(dec)) {
- mpd_zerocoeff(dec);
- *status |= MPD_Clamped;
- }
- }
- }
- /* Case exp >= etiny=emin-prec+1:
- * (1) adjexp=exp+digits-1 < emin
- * (2) digits < emin-exp+1 <= prec */
- }
- }
- /* Transcendental functions do not always set Underflow reliably,
- * since they only use as much precision as is necessary for correct
- * rounding. If a result like 1.0000000000e-101 is finalized, there
- * is no rounding digit that would trigger Underflow. But we can
- * assume Inexact, so a short check suffices. */
- static inline void
- mpd_check_underflow(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
- {
- if (mpd_adjexp(dec) < ctx->emin && !mpd_iszero(dec) &&
- dec->exp < mpd_etiny(ctx)) {
- *status |= MPD_Underflow;
- }
- }
- /* Check if a normal number must be rounded after the exponent has been checked. */
- static inline void
- _mpd_check_round(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_uint_t rnd;
- mpd_ssize_t shift;
- /* must handle specials: _mpd_check_exp() can produce infinities or NaNs */
- if (mpd_isspecial(dec)) {
- return;
- }
- if (dec->digits > ctx->prec) {
- shift = dec->digits - ctx->prec;
- rnd = mpd_qshiftr_inplace(dec, shift);
- dec->exp += shift;
- _mpd_apply_round(dec, rnd, ctx, status);
- *status |= MPD_Rounded;
- if (rnd) {
- *status |= MPD_Inexact;
- }
- }
- }
- /* Finalize all operations. */
- void
- mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
- {
- if (mpd_isspecial(result)) {
- if (mpd_isnan(result)) {
- _mpd_fix_nan(result, ctx);
- }
- return;
- }
- _mpd_check_exp(result, ctx, status);
- _mpd_check_round(result, ctx, status);
- }
- /******************************************************************************/
- /* Copying */
- /******************************************************************************/
- /* Internal function: Copy a decimal, share data with src: USE WITH CARE! */
- static inline void
- _mpd_copy_shared(mpd_t *dest, const mpd_t *src)
- {
- dest->flags = src->flags;
- dest->exp = src->exp;
- dest->digits = src->digits;
- dest->len = src->len;
- dest->alloc = src->alloc;
- dest->data = src->data;
- mpd_set_shared_data(dest);
- }
- /*
- * Copy a decimal. In case of an error, status is set to MPD_Malloc_error.
- */
- int
- mpd_qcopy(mpd_t *result, const mpd_t *a, uint32_t *status)
- {
- if (result == a) return 1;
- if (!mpd_qresize(result, a->len, status)) {
- return 0;
- }
- mpd_copy_flags(result, a);
- result->exp = a->exp;
- result->digits = a->digits;
- result->len = a->len;
- memcpy(result->data, a->data, a->len * (sizeof *result->data));
- return 1;
- }
- /*
- * Copy to a decimal with a static buffer. The caller has to make sure that
- * the buffer is big enough. Cannot fail.
- */
- static void
- mpd_qcopy_static(mpd_t *result, const mpd_t *a)
- {
- if (result == a) return;
- memcpy(result->data, a->data, a->len * (sizeof *result->data));
- mpd_copy_flags(result, a);
- result->exp = a->exp;
- result->digits = a->digits;
- result->len = a->len;
- }
- /*
- * Return a newly allocated copy of the operand. In case of an error,
- * status is set to MPD_Malloc_error and the return value is NULL.
- */
- mpd_t *
- mpd_qncopy(const mpd_t *a)
- {
- mpd_t *result;
- if ((result = mpd_qnew_size(a->len)) == NULL) {
- return NULL;
- }
- memcpy(result->data, a->data, a->len * (sizeof *result->data));
- mpd_copy_flags(result, a);
- result->exp = a->exp;
- result->digits = a->digits;
- result->len = a->len;
- return result;
- }
- /*
- * Copy a decimal and set the sign to positive. In case of an error, the
- * status is set to MPD_Malloc_error.
- */
- int
- mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status)
- {
- if (!mpd_qcopy(result, a, status)) {
- return 0;
- }
- mpd_set_positive(result);
- return 1;
- }
- /*
- * Copy a decimal and negate the sign. In case of an error, the
- * status is set to MPD_Malloc_error.
- */
- int
- mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status)
- {
- if (!mpd_qcopy(result, a, status)) {
- return 0;
- }
- _mpd_negate(result);
- return 1;
- }
- /*
- * Copy a decimal, setting the sign of the first operand to the sign of the
- * second operand. In case of an error, the status is set to MPD_Malloc_error.
- */
- int
- mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status)
- {
- uint8_t sign_b = mpd_sign(b); /* result may equal b! */
- if (!mpd_qcopy(result, a, status)) {
- return 0;
- }
- mpd_set_sign(result, sign_b);
- return 1;
- }
- /******************************************************************************/
- /* Comparisons */
- /******************************************************************************/
- /*
- * For all functions that compare two operands and return an int the usual
- * convention applies to the return value:
- *
- * -1 if op1 < op2
- * 0 if op1 == op2
- * 1 if op1 > op2
- *
- * INT_MAX for error
- */
- /* Convenience macro. If a and b are not equal, return from the calling
- * function with the correct comparison value. */
- #define CMP_EQUAL_OR_RETURN(a, b) \
- if (a != b) { \
- if (a < b) { \
- return -1; \
- } \
- return 1; \
- }
- /*
- * Compare the data of big and small. This function does the equivalent
- * of first shifting small to the left and then comparing the data of
- * big and small, except that no allocation for the left shift is needed.
- */
- static int
- _mpd_basecmp(mpd_uint_t *big, mpd_uint_t *small, mpd_size_t n, mpd_size_t m,
- mpd_size_t shift)
- {
- #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
- /* spurious uninitialized warnings */
- mpd_uint_t l=l, lprev=lprev, h=h;
- #else
- mpd_uint_t l, lprev, h;
- #endif
- mpd_uint_t q, r;
- mpd_uint_t ph, x;
- assert(m > 0 && n >= m && shift > 0);
- _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS);
- if (r != 0) {
- ph = mpd_pow10[r];
- --m; --n;
- _mpd_divmod_pow10(&h, &lprev, small[m--], MPD_RDIGITS-r);
- if (h != 0) {
- CMP_EQUAL_OR_RETURN(big[n], h)
- --n;
- }
- for (; m != MPD_SIZE_MAX; m--,n--) {
- _mpd_divmod_pow10(&h, &l, small[m], MPD_RDIGITS-r);
- x = ph * lprev + h;
- CMP_EQUAL_OR_RETURN(big[n], x)
- lprev = l;
- }
- x = ph * lprev;
- CMP_EQUAL_OR_RETURN(big[q], x)
- }
- else {
- while (--m != MPD_SIZE_MAX) {
- CMP_EQUAL_OR_RETURN(big[m+q], small[m])
- }
- }
- return !_mpd_isallzero(big, q);
- }
- /* Compare two decimals with the same adjusted exponent. */
- static int
- _mpd_cmp_same_adjexp(const mpd_t *a, const mpd_t *b)
- {
- mpd_ssize_t shift, i;
- if (a->exp != b->exp) {
- /* Cannot wrap: a->exp + a->digits = b->exp + b->digits, so
- * a->exp - b->exp = b->digits - a->digits. */
- shift = a->exp - b->exp;
- if (shift > 0) {
- return -1 * _mpd_basecmp(b->data, a->data, b->len, a->len, shift);
- }
- else {
- return _mpd_basecmp(a->data, b->data, a->len, b->len, -shift);
- }
- }
- /*
- * At this point adjexp(a) == adjexp(b) and a->exp == b->exp,
- * so a->digits == b->digits, therefore a->len == b->len.
- */
- for (i = a->len-1; i >= 0; --i) {
- CMP_EQUAL_OR_RETURN(a->data[i], b->data[i])
- }
- return 0;
- }
- /* Compare two numerical values. */
- static int
- _mpd_cmp(const mpd_t *a, const mpd_t *b)
- {
- mpd_ssize_t adjexp_a, adjexp_b;
- /* equal pointers */
- if (a == b) {
- return 0;
- }
- /* infinities */
- if (mpd_isinfinite(a)) {
- if (mpd_isinfinite(b)) {
- return mpd_isnegative(b) - mpd_isnegative(a);
- }
- return mpd_arith_sign(a);
- }
- if (mpd_isinfinite(b)) {
- return -mpd_arith_sign(b);
- }
- /* zeros */
- if (mpd_iszerocoeff(a)) {
- if (mpd_iszerocoeff(b)) {
- return 0;
- }
- return -mpd_arith_sign(b);
- }
- if (mpd_iszerocoeff(b)) {
- return mpd_arith_sign(a);
- }
- /* different signs */
- if (mpd_sign(a) != mpd_sign(b)) {
- return mpd_sign(b) - mpd_sign(a);
- }
- /* different adjusted exponents */
- adjexp_a = mpd_adjexp(a);
- adjexp_b = mpd_adjexp(b);
- if (adjexp_a != adjexp_b) {
- if (adjexp_a < adjexp_b) {
- return -1 * mpd_arith_sign(a);
- }
- return mpd_arith_sign(a);
- }
- /* same adjusted exponents */
- return _mpd_cmp_same_adjexp(a, b) * mpd_arith_sign(a);
- }
- /* Compare the absolutes of two numerical values. */
- static int
- _mpd_cmp_abs(const mpd_t *a, const mpd_t *b)
- {
- mpd_ssize_t adjexp_a, adjexp_b;
- /* equal pointers */
- if (a == b) {
- return 0;
- }
- /* infinities */
- if (mpd_isinfinite(a)) {
- if (mpd_isinfinite(b)) {
- return 0;
- }
- return 1;
- }
- if (mpd_isinfinite(b)) {
- return -1;
- }
- /* zeros */
- if (mpd_iszerocoeff(a)) {
- if (mpd_iszerocoeff(b)) {
- return 0;
- }
- return -1;
- }
- if (mpd_iszerocoeff(b)) {
- return 1;
- }
- /* different adjusted exponents */
- adjexp_a = mpd_adjexp(a);
- adjexp_b = mpd_adjexp(b);
- if (adjexp_a != adjexp_b) {
- if (adjexp_a < adjexp_b) {
- return -1;
- }
- return 1;
- }
- /* same adjusted exponents */
- return _mpd_cmp_same_adjexp(a, b);
- }
- /* Compare two values and return an integer result. */
- int
- mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status)
- {
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_isnan(a) || mpd_isnan(b)) {
- *status |= MPD_Invalid_operation;
- return INT_MAX;
- }
- }
- return _mpd_cmp(a, b);
- }
- /*
- * Compare a and b, convert the the usual integer result to a decimal and
- * store it in 'result'. For convenience, the integer result of the comparison
- * is returned. Comparisons involving NaNs return NaN/INT_MAX.
- */
- int
- mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return INT_MAX;
- }
- }
- c = _mpd_cmp(a, b);
- _settriple(result, (c < 0), (c != 0), 0);
- return c;
- }
- /* Same as mpd_compare(), but signal for all NaNs, i.e. also for quiet NaNs. */
- int
- mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- *status |= MPD_Invalid_operation;
- return INT_MAX;
- }
- }
- c = _mpd_cmp(a, b);
- _settriple(result, (c < 0), (c != 0), 0);
- return c;
- }
- /* Compare the operands using a total order. */
- int
- mpd_cmp_total(const mpd_t *a, const mpd_t *b)
- {
- mpd_t aa, bb;
- int nan_a, nan_b;
- int c;
- if (mpd_sign(a) != mpd_sign(b)) {
- return mpd_sign(b) - mpd_sign(a);
- }
- if (mpd_isnan(a)) {
- c = 1;
- if (mpd_isnan(b)) {
- nan_a = (mpd_isqnan(a)) ? 1 : 0;
- nan_b = (mpd_isqnan(b)) ? 1 : 0;
- if (nan_b == nan_a) {
- if (a->len > 0 && b->len > 0) {
- _mpd_copy_shared(&aa, a);
- _mpd_copy_shared(&bb, b);
- aa.exp = bb.exp = 0;
- /* compare payload */
- c = _mpd_cmp_abs(&aa, &bb);
- }
- else {
- c = (a->len > 0) - (b->len > 0);
- }
- }
- else {
- c = nan_a - nan_b;
- }
- }
- }
- else if (mpd_isnan(b)) {
- c = -1;
- }
- else {
- c = _mpd_cmp_abs(a, b);
- if (c == 0 && a->exp != b->exp) {
- c = (a->exp < b->exp) ? -1 : 1;
- }
- }
- return c * mpd_arith_sign(a);
- }
- /*
- * Compare a and b according to a total order, convert the usual integer result
- * to a decimal and store it in 'result'. For convenience, the integer result
- * of the comparison is returned.
- */
- int
- mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b)
- {
- int c;
- c = mpd_cmp_total(a, b);
- _settriple(result, (c < 0), (c != 0), 0);
- return c;
- }
- /* Compare the magnitude of the operands using a total order. */
- int
- mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b)
- {
- mpd_t aa, bb;
- _mpd_copy_shared(&aa, a);
- _mpd_copy_shared(&bb, b);
- mpd_set_positive(&aa);
- mpd_set_positive(&bb);
- return mpd_cmp_total(&aa, &bb);
- }
- /*
- * Compare the magnitude of a and b according to a total order, convert the
- * the usual integer result to a decimal and store it in 'result'.
- * For convenience, the integer result of the comparison is returned.
- */
- int
- mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b)
- {
- int c;
- c = mpd_cmp_total_mag(a, b);
- _settriple(result, (c < 0), (c != 0), 0);
- return c;
- }
- /* Determine an ordering for operands that are numerically equal. */
- static inline int
- _mpd_cmp_numequal(const mpd_t *a, const mpd_t *b)
- {
- int sign_a, sign_b;
- int c;
- sign_a = mpd_sign(a);
- sign_b = mpd_sign(b);
- if (sign_a != sign_b) {
- c = sign_b - sign_a;
- }
- else {
- c = (a->exp < b->exp) ? -1 : 1;
- c *= mpd_arith_sign(a);
- }
- return c;
- }
- /******************************************************************************/
- /* Shifting the coefficient */
- /******************************************************************************/
- /*
- * Shift the coefficient of the operand to the left, no check for specials.
- * Both operands may be the same pointer. If the result length has to be
- * increased, mpd_qresize() might fail with MPD_Malloc_error.
- */
- int
- mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status)
- {
- mpd_ssize_t size;
- assert(n >= 0);
- if (mpd_iszerocoeff(a) || n == 0) {
- return mpd_qcopy(result, a, status);
- }
- size = mpd_digits_to_size(a->digits+n);
- if (!mpd_qresize(result, size, status)) {
- return 0; /* result is NaN */
- }
- _mpd_baseshiftl(result->data, a->data, size, a->len, n);
- mpd_copy_flags(result, a);
- result->len = size;
- result->exp = a->exp;
- result->digits = a->digits+n;
- return 1;
- }
- /* Determine the rounding indicator if all digits of the coefficient are shifted
- * out of the picture. */
- static mpd_uint_t
- _mpd_get_rnd(const mpd_uint_t *data, mpd_ssize_t len, int use_msd)
- {
- mpd_uint_t rnd = 0, rest = 0, word;
- word = data[len-1];
- /* special treatment for the most significant digit if shift == digits */
- if (use_msd) {
- _mpd_divmod_pow10(&rnd, &rest, word, mpd_word_digits(word)-1);
- if (len > 1 && rest == 0) {
- rest = !_mpd_isallzero(data, len-1);
- }
- }
- else {
- rest = !_mpd_isallzero(data, len);
- }
- return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd;
- }
- /*
- * Same as mpd_qshiftr(), but 'result' is a static array. It is the
- * caller's responsibility to make sure that the array is big enough.
- * The function cannot fail.
- */
- mpd_uint_t
- mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n)
- {
- mpd_uint_t rnd;
- mpd_ssize_t size;
- assert(n >= 0);
- if (mpd_iszerocoeff(a) || n == 0) {
- mpd_qcopy_static(result, a);
- return 0;
- }
- if (n >= a->digits) {
- rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits));
- mpd_zerocoeff(result);
- result->digits = 1;
- size = 1;
- }
- else {
- result->digits = a->digits-n;
- size = mpd_digits_to_size(result->digits);
- rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
- }
- mpd_copy_flags(result, a);
- result->exp = a->exp;
- result->len = size;
- return rnd;
- }
- /*
- * Inplace shift of the coefficient to the right, no check for specials.
- * Returns the rounding indicator for mpd_rnd_incr().
- * The function cannot fail.
- */
- mpd_uint_t
- mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n)
- {
- uint32_t dummy;
- mpd_uint_t rnd;
- mpd_ssize_t size;
- assert(n >= 0);
- if (mpd_iszerocoeff(result) || n == 0) {
- return 0;
- }
- if (n >= result->digits) {
- rnd = _mpd_get_rnd(result->data, result->len, (n==result->digits));
- mpd_zerocoeff(result);
- result->digits = 1;
- size = 1;
- }
- else {
- rnd = _mpd_baseshiftr(result->data, result->data, result->len, n);
- result->digits -= n;
- size = mpd_digits_to_size(result->digits);
- /* reducing the size cannot fail */
- mpd_qresize(result, size, &dummy);
- }
- result->len = size;
- return rnd;
- }
- /*
- * Shift the coefficient of the operand to the right, no check for specials.
- * Both operands may be the same pointer. Returns the rounding indicator to
- * be used by mpd_rnd_incr(). If the result length has to be increased,
- * mpd_qcopy() or mpd_qresize() might fail with MPD_Malloc_error. In those
- * cases, MPD_UINT_MAX is returned.
- */
- mpd_uint_t
- mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status)
- {
- mpd_uint_t rnd;
- mpd_ssize_t size;
- assert(n >= 0);
- if (mpd_iszerocoeff(a) || n == 0) {
- if (!mpd_qcopy(result, a, status)) {
- return MPD_UINT_MAX;
- }
- return 0;
- }
- if (n >= a->digits) {
- rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits));
- mpd_zerocoeff(result);
- result->digits = 1;
- size = 1;
- }
- else {
- result->digits = a->digits-n;
- size = mpd_digits_to_size(result->digits);
- if (result == a) {
- rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
- /* reducing the size cannot fail */
- mpd_qresize(result, size, status);
- }
- else {
- if (!mpd_qresize(result, size, status)) {
- return MPD_UINT_MAX;
- }
- rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
- }
- }
- mpd_copy_flags(result, a);
- result->exp = a->exp;
- result->len = size;
- return rnd;
- }
- /******************************************************************************/
- /* Miscellaneous operations */
- /******************************************************************************/
- /* Logical And */
- void
- mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- const mpd_t *big = a, *small = b;
- mpd_uint_t x, y, z, xbit, ybit;
- int k, mswdigits;
- mpd_ssize_t i;
- if (mpd_isspecial(a) || mpd_isspecial(b) ||
- mpd_isnegative(a) || mpd_isnegative(b) ||
- a->exp != 0 || b->exp != 0) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (b->digits > a->digits) {
- big = b;
- small = a;
- }
- if (!mpd_qresize(result, big->len, status)) {
- return;
- }
- /* full words */
- for (i = 0; i < small->len-1; i++) {
- x = small->data[i];
- y = big->data[i];
- z = 0;
- for (k = 0; k < MPD_RDIGITS; k++) {
- xbit = x % 10;
- x /= 10;
- ybit = y % 10;
- y /= 10;
- if (xbit > 1 || ybit > 1) {
- goto invalid_operation;
- }
- z += (xbit&ybit) ? mpd_pow10[k] : 0;
- }
- result->data[i] = z;
- }
- /* most significant word of small */
- x = small->data[i];
- y = big->data[i];
- z = 0;
- mswdigits = mpd_word_digits(x);
- for (k = 0; k < mswdigits; k++) {
- xbit = x % 10;
- x /= 10;
- ybit = y % 10;
- y /= 10;
- if (xbit > 1 || ybit > 1) {
- goto invalid_operation;
- }
- z += (xbit&ybit) ? mpd_pow10[k] : 0;
- }
- result->data[i++] = z;
- /* scan the rest of y for digit > 1 */
- for (; k < MPD_RDIGITS; k++) {
- ybit = y % 10;
- y /= 10;
- if (ybit > 1) {
- goto invalid_operation;
- }
- }
- /* scan the rest of big for digit > 1 */
- for (; i < big->len; i++) {
- y = big->data[i];
- for (k = 0; k < MPD_RDIGITS; k++) {
- ybit = y % 10;
- y /= 10;
- if (ybit > 1) {
- goto invalid_operation;
- }
- }
- }
- mpd_clear_flags(result);
- result->exp = 0;
- result->len = _mpd_real_size(result->data, small->len);
- mpd_qresize(result, result->len, status);
- mpd_setdigits(result);
- _mpd_cap(result, ctx);
- return;
- invalid_operation:
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- /* Class of an operand. Returns a pointer to the constant name. */
- const char *
- mpd_class(const mpd_t *a, const mpd_context_t *ctx)
- {
- if (mpd_isnan(a)) {
- if (mpd_isqnan(a))
- return "NaN";
- else
- return "sNaN";
- }
- else if (mpd_ispositive(a)) {
- if (mpd_isinfinite(a))
- return "+Infinity";
- else if (mpd_iszero(a))
- return "+Zero";
- else if (mpd_isnormal(a, ctx))
- return "+Normal";
- else
- return "+Subnormal";
- }
- else {
- if (mpd_isinfinite(a))
- return "-Infinity";
- else if (mpd_iszero(a))
- return "-Zero";
- else if (mpd_isnormal(a, ctx))
- return "-Normal";
- else
- return "-Subnormal";
- }
- }
- /* Logical Xor */
- void
- mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_uint_t x, z, xbit;
- mpd_ssize_t i, digits, len;
- mpd_ssize_t q, r;
- int k;
- if (mpd_isspecial(a) || mpd_isnegative(a) || a->exp != 0) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- digits = (a->digits < ctx->prec) ? ctx->prec : a->digits;
- _mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
- len = (r == 0) ? q : q+1;
- if (!mpd_qresize(result, len, status)) {
- return;
- }
- for (i = 0; i < len; i++) {
- x = (i < a->len) ? a->data[i] : 0;
- z = 0;
- for (k = 0; k < MPD_RDIGITS; k++) {
- xbit = x % 10;
- x /= 10;
- if (xbit > 1) {
- goto invalid_operation;
- }
- z += !xbit ? mpd_pow10[k] : 0;
- }
- result->data[i] = z;
- }
- mpd_clear_flags(result);
- result->exp = 0;
- result->len = _mpd_real_size(result->data, len);
- mpd_qresize(result, result->len, status);
- mpd_setdigits(result);
- _mpd_cap(result, ctx);
- return;
- invalid_operation:
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- /* Exponent of the magnitude of the most significant digit of the operand. */
- void
- mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- mpd_setspecial(result, MPD_POS, MPD_INF);
- }
- else if (mpd_iszerocoeff(a)) {
- mpd_setspecial(result, MPD_NEG, MPD_INF);
- *status |= MPD_Division_by_zero;
- }
- else {
- mpd_qset_ssize(result, mpd_adjexp(a), ctx, status);
- }
- }
- /* Logical Or */
- void
- mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- const mpd_t *big = a, *small = b;
- mpd_uint_t x, y, z, xbit, ybit;
- int k, mswdigits;
- mpd_ssize_t i;
- if (mpd_isspecial(a) || mpd_isspecial(b) ||
- mpd_isnegative(a) || mpd_isnegative(b) ||
- a->exp != 0 || b->exp != 0) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (b->digits > a->digits) {
- big = b;
- small = a;
- }
- if (!mpd_qresize(result, big->len, status)) {
- return;
- }
- /* full words */
- for (i = 0; i < small->len-1; i++) {
- x = small->data[i];
- y = big->data[i];
- z = 0;
- for (k = 0; k < MPD_RDIGITS; k++) {
- xbit = x % 10;
- x /= 10;
- ybit = y % 10;
- y /= 10;
- if (xbit > 1 || ybit > 1) {
- goto invalid_operation;
- }
- z += (xbit|ybit) ? mpd_pow10[k] : 0;
- }
- result->data[i] = z;
- }
- /* most significant word of small */
- x = small->data[i];
- y = big->data[i];
- z = 0;
- mswdigits = mpd_word_digits(x);
- for (k = 0; k < mswdigits; k++) {
- xbit = x % 10;
- x /= 10;
- ybit = y % 10;
- y /= 10;
- if (xbit > 1 || ybit > 1) {
- goto invalid_operation;
- }
- z += (xbit|ybit) ? mpd_pow10[k] : 0;
- }
- /* scan and copy the rest of y for digit > 1 */
- for (; k < MPD_RDIGITS; k++) {
- ybit = y % 10;
- y /= 10;
- if (ybit > 1) {
- goto invalid_operation;
- }
- z += ybit*mpd_pow10[k];
- }
- result->data[i++] = z;
- /* scan and copy the rest of big for digit > 1 */
- for (; i < big->len; i++) {
- y = big->data[i];
- for (k = 0; k < MPD_RDIGITS; k++) {
- ybit = y % 10;
- y /= 10;
- if (ybit > 1) {
- goto invalid_operation;
- }
- }
- result->data[i] = big->data[i];
- }
- mpd_clear_flags(result);
- result->exp = 0;
- result->len = _mpd_real_size(result->data, big->len);
- mpd_qresize(result, result->len, status);
- mpd_setdigits(result);
- _mpd_cap(result, ctx);
- return;
- invalid_operation:
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- /*
- * Rotate the coefficient of a by b->data digits. b must be an integer with
- * exponent 0.
- */
- void
- mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- MPD_NEW_STATIC(tmp,0,0,0,0);
- MPD_NEW_STATIC(big,0,0,0,0);
- MPD_NEW_STATIC(small,0,0,0,0);
- mpd_ssize_t n, lshift, rshift;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- }
- if (b->exp != 0 || mpd_isinfinite(b)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- n = mpd_qget_ssize(b, &workstatus);
- if (workstatus&MPD_Invalid_operation) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (n > ctx->prec || n < -ctx->prec) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_qcopy(result, a, status);
- return;
- }
- if (n >= 0) {
- lshift = n;
- rshift = ctx->prec-n;
- }
- else {
- lshift = ctx->prec+n;
- rshift = -n;
- }
- if (a->digits > ctx->prec) {
- if (!mpd_qcopy(&tmp, a, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- goto finish;
- }
- _mpd_cap(&tmp, ctx);
- a = &tmp;
- }
- if (!mpd_qshiftl(&big, a, lshift, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- goto finish;
- }
- _mpd_cap(&big, ctx);
- if (mpd_qshiftr(&small, a, rshift, status) == MPD_UINT_MAX) {
- mpd_seterror(result, MPD_Malloc_error, status);
- goto finish;
- }
- _mpd_qadd(result, &big, &small, ctx, status);
- finish:
- mpd_del(&tmp);
- mpd_del(&big);
- mpd_del(&small);
- }
- /*
- * b must be an integer with exponent 0 and in the range +-2*(emax + prec).
- * XXX: In my opinion +-(2*emax + prec) would be more sensible.
- * The result is a with the value of b added to its exponent.
- */
- void
- mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_uint_t n, maxjump;
- #ifndef LEGACY_COMPILER
- int64_t exp;
- #else
- mpd_uint_t x;
- int x_sign, n_sign;
- mpd_ssize_t exp;
- #endif
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- }
- if (b->exp != 0 || mpd_isinfinite(b)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- n = mpd_qabs_uint(b, &workstatus);
- /* the spec demands this */
- maxjump = 2 * (mpd_uint_t)(ctx->emax + ctx->prec);
- if (n > maxjump || workstatus&MPD_Invalid_operation) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_qcopy(result, a, status);
- return;
- }
- #ifndef LEGACY_COMPILER
- exp = a->exp + (int64_t)n * mpd_arith_sign(b);
- exp = (exp > MPD_EXP_INF) ? MPD_EXP_INF : exp;
- exp = (exp < MPD_EXP_CLAMP) ? MPD_EXP_CLAMP : exp;
- #else
- x = (a->exp < 0) ? -a->exp : a->exp;
- x_sign = (a->exp < 0) ? 1 : 0;
- n_sign = mpd_isnegative(b) ? 1 : 0;
- if (x_sign == n_sign) {
- x = x + n;
- if (x < n) x = MPD_UINT_MAX;
- }
- else {
- x_sign = (x >= n) ? x_sign : n_sign;
- x = (x >= n) ? x - n : n - x;
- }
- if (!x_sign && x > MPD_EXP_INF) x = MPD_EXP_INF;
- if (x_sign && x > -MPD_EXP_CLAMP) x = -MPD_EXP_CLAMP;
- exp = x_sign ? -((mpd_ssize_t)x) : (mpd_ssize_t)x;
- #endif
- mpd_qcopy(result, a, status);
- result->exp = (mpd_ssize_t)exp;
- mpd_qfinalize(result, ctx, status);
- }
- /*
- * Shift the coefficient by n digits, positive n is a left shift. In the case
- * of a left shift, the result is decapitated to fit the context precision. If
- * you don't want that, use mpd_shiftl().
- */
- void
- mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- mpd_qcopy(result, a, status);
- return;
- }
- if (n >= 0 && n <= ctx->prec) {
- mpd_qshiftl(result, a, n, status);
- _mpd_cap(result, ctx);
- }
- else if (n < 0 && n >= -ctx->prec) {
- if (!mpd_qcopy(result, a, status)) {
- return;
- }
- _mpd_cap(result, ctx);
- mpd_qshiftr_inplace(result, -n);
- }
- else {
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- }
- /*
- * Same as mpd_shiftn(), but the shift is specified by the decimal b, which
- * must be an integer with a zero exponent. Infinities remain infinities.
- */
- void
- mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx,
- uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_ssize_t n;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- }
- if (b->exp != 0 || mpd_isinfinite(b)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- n = mpd_qget_ssize(b, &workstatus);
- if (workstatus&MPD_Invalid_operation) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (n > ctx->prec || n < -ctx->prec) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_qcopy(result, a, status);
- return;
- }
- if (n >= 0) {
- mpd_qshiftl(result, a, n, status);
- _mpd_cap(result, ctx);
- }
- else {
- if (!mpd_qcopy(result, a, status)) {
- return;
- }
- _mpd_cap(result, ctx);
- mpd_qshiftr_inplace(result, -n);
- }
- }
- /* Logical Xor */
- void
- mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- const mpd_t *big = a, *small = b;
- mpd_uint_t x, y, z, xbit, ybit;
- int k, mswdigits;
- mpd_ssize_t i;
- if (mpd_isspecial(a) || mpd_isspecial(b) ||
- mpd_isnegative(a) || mpd_isnegative(b) ||
- a->exp != 0 || b->exp != 0) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (b->digits > a->digits) {
- big = b;
- small = a;
- }
- if (!mpd_qresize(result, big->len, status)) {
- return;
- }
- /* full words */
- for (i = 0; i < small->len-1; i++) {
- x = small->data[i];
- y = big->data[i];
- z = 0;
- for (k = 0; k < MPD_RDIGITS; k++) {
- xbit = x % 10;
- x /= 10;
- ybit = y % 10;
- y /= 10;
- if (xbit > 1 || ybit > 1) {
- goto invalid_operation;
- }
- z += (xbit^ybit) ? mpd_pow10[k] : 0;
- }
- result->data[i] = z;
- }
- /* most significant word of small */
- x = small->data[i];
- y = big->data[i];
- z = 0;
- mswdigits = mpd_word_digits(x);
- for (k = 0; k < mswdigits; k++) {
- xbit = x % 10;
- x /= 10;
- ybit = y % 10;
- y /= 10;
- if (xbit > 1 || ybit > 1) {
- goto invalid_operation;
- }
- z += (xbit^ybit) ? mpd_pow10[k] : 0;
- }
- /* scan and copy the rest of y for digit > 1 */
- for (; k < MPD_RDIGITS; k++) {
- ybit = y % 10;
- y /= 10;
- if (ybit > 1) {
- goto invalid_operation;
- }
- z += ybit*mpd_pow10[k];
- }
- result->data[i++] = z;
- /* scan and copy the rest of big for digit > 1 */
- for (; i < big->len; i++) {
- y = big->data[i];
- for (k = 0; k < MPD_RDIGITS; k++) {
- ybit = y % 10;
- y /= 10;
- if (ybit > 1) {
- goto invalid_operation;
- }
- }
- result->data[i] = big->data[i];
- }
- mpd_clear_flags(result);
- result->exp = 0;
- result->len = _mpd_real_size(result->data, big->len);
- mpd_qresize(result, result->len, status);
- mpd_setdigits(result);
- _mpd_cap(result, ctx);
- return;
- invalid_operation:
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- /******************************************************************************/
- /* Arithmetic operations */
- /******************************************************************************/
- /*
- * The absolute value of a. If a is negative, the result is the same
- * as the result of the minus operation. Otherwise, the result is the
- * result of the plus operation.
- */
- void
- mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- }
- if (mpd_isnegative(a)) {
- mpd_qminus(result, a, ctx, status);
- }
- else {
- mpd_qplus(result, a, ctx, status);
- }
- mpd_qfinalize(result, ctx, status);
- }
- static inline void
- _mpd_ptrswap(mpd_t **a, mpd_t **b)
- {
- mpd_t *t = *a;
- *a = *b;
- *b = t;
- }
- /* Add or subtract infinities. */
- static void
- _mpd_qaddsub_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b,
- uint32_t *status)
- {
- if (mpd_isinfinite(a)) {
- if (mpd_sign(a) != sign_b && mpd_isinfinite(b)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- else {
- mpd_setspecial(result, mpd_sign(a), MPD_INF);
- }
- return;
- }
- assert(mpd_isinfinite(b));
- mpd_setspecial(result, sign_b, MPD_INF);
- }
- /* Add or subtract non-special numbers. */
- static void
- _mpd_qaddsub(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_t *big, *small;
- MPD_NEW_STATIC(big_aligned,0,0,0,0);
- MPD_NEW_CONST(tiny,0,0,0,1,1,1);
- mpd_uint_t carry;
- mpd_ssize_t newsize, shift;
- mpd_ssize_t exp, i;
- int swap = 0;
- /* compare exponents */
- big = (mpd_t *)a; small = (mpd_t *)b;
- if (big->exp != small->exp) {
- if (small->exp > big->exp) {
- _mpd_ptrswap(&big, &small);
- swap++;
- }
- if (!mpd_iszerocoeff(big)) {
- /* Test for adjexp(small) + big->digits < adjexp(big), if big-digits > prec
- * Test for adjexp(small) + prec + 1 < adjexp(big), if big-digits <= prec
- * If true, the magnitudes of the numbers are so far apart that one can as
- * well add or subtract 1*10**big->exp. */
- exp = big->exp - 1;
- exp += (big->digits > ctx->prec) ? 0 : big->digits-ctx->prec-1;
- if (mpd_adjexp(small) < exp) {
- mpd_copy_flags(&tiny, small);
- tiny.exp = exp;
- tiny.digits = 1;
- tiny.len = 1;
- tiny.data[0] = mpd_iszerocoeff(small) ? 0 : 1;
- small = &tiny;
- }
- /* this cannot wrap: the difference is positive and <= maxprec+1 */
- shift = big->exp - small->exp;
- if (!mpd_qshiftl(&big_aligned, big, shift, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- goto finish;
- }
- big = &big_aligned;
- }
- }
- result->exp = small->exp;
- /* compare length of coefficients */
- if (big->len < small->len) {
- _mpd_ptrswap(&big, &small);
- swap++;
- }
- newsize = big->len;
- if (!mpd_qresize(result, newsize, status)) {
- goto finish;
- }
- if (mpd_sign(a) == sign_b) {
- carry = _mpd_baseadd(result->data, big->data, small->data,
- big->len, small->len);
- if (carry) {
- newsize = big->len + 1;
- if (!mpd_qresize(result, newsize, status)) {
- goto finish;
- }
- result->data[newsize-1] = carry;
- }
- result->len = newsize;
- mpd_set_flags(result, sign_b);
- }
- else {
- if (big->len == small->len) {
- for (i=big->len-1; i >= 0; --i) {
- if (big->data[i] != small->data[i]) {
- if (big->data[i] < small->data[i]) {
- _mpd_ptrswap(&big, &small);
- swap++;
- }
- break;
- }
- }
- }
- _mpd_basesub(result->data, big->data, small->data,
- big->len, small->len);
- newsize = _mpd_real_size(result->data, big->len);
- /* resize to smaller cannot fail */
- (void)mpd_qresize(result, newsize, status);
- result->len = newsize;
- sign_b = (swap & 1) ? sign_b : mpd_sign(a);
- mpd_set_flags(result, sign_b);
- if (mpd_iszerocoeff(result)) {
- mpd_set_positive(result);
- if (ctx->round == MPD_ROUND_FLOOR) {
- mpd_set_negative(result);
- }
- }
- }
- mpd_setdigits(result);
- finish:
- mpd_del(&big_aligned);
- }
- /* Add a and b. No specials, no finalizing. */
- static void
- _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- _mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status);
- }
- /* Subtract b from a. No specials, no finalizing. */
- static void
- _mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- _mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status);
- }
- /* Add a and b. */
- void
- mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- _mpd_qaddsub_inf(result, a, b, mpd_sign(b), status);
- return;
- }
- _mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status);
- mpd_qfinalize(result, ctx, status);
- }
- /* Subtract b from a. */
- void
- mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- _mpd_qaddsub_inf(result, a, b, !mpd_sign(b), status);
- return;
- }
- _mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status);
- mpd_qfinalize(result, ctx, status);
- }
- /* Add decimal and mpd_ssize_t. */
- void
- mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_ssize(&bb, b, &maxcontext, status);
- mpd_qadd(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Add decimal and mpd_uint_t. */
- void
- mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_uint(&bb, b, &maxcontext, status);
- mpd_qadd(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Subtract mpd_ssize_t from decimal. */
- void
- mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_ssize(&bb, b, &maxcontext, status);
- mpd_qsub(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Subtract mpd_uint_t from decimal. */
- void
- mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_uint(&bb, b, &maxcontext, status);
- mpd_qsub(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Add decimal and int32_t. */
- void
- mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qadd_ssize(result, a, b, ctx, status);
- }
- /* Add decimal and uint32_t. */
- void
- mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qadd_uint(result, a, b, ctx, status);
- }
- #ifdef CONFIG_64
- /* Add decimal and int64_t. */
- void
- mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qadd_ssize(result, a, b, ctx, status);
- }
- /* Add decimal and uint64_t. */
- void
- mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qadd_uint(result, a, b, ctx, status);
- }
- #endif
- /* Subtract int32_t from decimal. */
- void
- mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qsub_ssize(result, a, b, ctx, status);
- }
- /* Subtract uint32_t from decimal. */
- void
- mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qsub_uint(result, a, b, ctx, status);
- }
- #ifdef CONFIG_64
- /* Subtract int64_t from decimal. */
- void
- mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qsub_ssize(result, a, b, ctx, status);
- }
- /* Subtract uint64_t from decimal. */
- void
- mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qsub_uint(result, a, b, ctx, status);
- }
- #endif
- /* Divide infinities. */
- static void
- _mpd_qdiv_inf(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- if (mpd_isinfinite(a)) {
- if (mpd_isinfinite(b)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
- return;
- }
- assert(mpd_isinfinite(b));
- _settriple(result, mpd_sign(a)^mpd_sign(b), 0, mpd_etiny(ctx));
- *status |= MPD_Clamped;
- }
- enum {NO_IDEAL_EXP, SET_IDEAL_EXP};
- /* Divide a by b. */
- static void
- _mpd_qdiv(int action, mpd_t *q, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(aligned,0,0,0,0);
- mpd_uint_t ld;
- mpd_ssize_t shift, exp, tz;
- mpd_ssize_t newsize;
- mpd_ssize_t ideal_exp;
- mpd_uint_t rem;
- uint8_t sign_a = mpd_sign(a);
- uint8_t sign_b = mpd_sign(b);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(q, a, b, ctx, status)) {
- return;
- }
- _mpd_qdiv_inf(q, a, b, ctx, status);
- return;
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(q, MPD_Division_undefined, status);
- }
- else {
- mpd_setspecial(q, sign_a^sign_b, MPD_INF);
- *status |= MPD_Division_by_zero;
- }
- return;
- }
- if (mpd_iszerocoeff(a)) {
- exp = a->exp - b->exp;
- _settriple(q, sign_a^sign_b, 0, exp);
- mpd_qfinalize(q, ctx, status);
- return;
- }
- shift = (b->digits - a->digits) + ctx->prec + 1;
- ideal_exp = a->exp - b->exp;
- exp = ideal_exp - shift;
- if (shift > 0) {
- if (!mpd_qshiftl(&aligned, a, shift, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- a = &aligned;
- }
- else if (shift < 0) {
- shift = -shift;
- if (!mpd_qshiftl(&aligned, b, shift, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- b = &aligned;
- }
- newsize = a->len - b->len + 1;
- if ((q != b && q != a) || (q == b && newsize > b->len)) {
- if (!mpd_qresize(q, newsize, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- }
- if (b->len == 1) {
- rem = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
- }
- else if (a->len < 2*MPD_NEWTONDIV_CUTOFF &&
- b->len < MPD_NEWTONDIV_CUTOFF) {
- int ret = _mpd_basedivmod(q->data, NULL, a->data, b->data,
- a->len, b->len);
- if (ret < 0) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- rem = ret;
- }
- else {
- MPD_NEW_STATIC(r,0,0,0,0);
- _mpd_qbarrett_divmod(q, &r, a, b, status);
- if (mpd_isspecial(q) || mpd_isspecial(&r)) {
- mpd_del(&r);
- goto finish;
- }
- rem = !mpd_iszerocoeff(&r);
- mpd_del(&r);
- newsize = q->len;
- }
- newsize = _mpd_real_size(q->data, newsize);
- /* resize to smaller cannot fail */
- mpd_qresize(q, newsize, status);
- q->len = newsize;
- mpd_setdigits(q);
- shift = ideal_exp - exp;
- if (rem) {
- ld = mpd_lsd(q->data[0]);
- if (ld == 0 || ld == 5) {
- q->data[0] += 1;
- }
- }
- else if (action == SET_IDEAL_EXP && shift > 0) {
- tz = mpd_trail_zeros(q);
- shift = (tz > shift) ? shift : tz;
- mpd_qshiftr_inplace(q, shift);
- exp += shift;
- }
- mpd_set_flags(q, sign_a^sign_b);
- q->exp = exp;
- finish:
- mpd_del(&aligned);
- mpd_qfinalize(q, ctx, status);
- }
- /* Divide a by b. */
- void
- mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- _mpd_qdiv(SET_IDEAL_EXP, q, a, b, ctx, status);
- }
- /* Internal function. */
- static void
- _mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(aligned,0,0,0,0);
- mpd_ssize_t qsize, rsize;
- mpd_ssize_t ideal_exp, expdiff, shift;
- uint8_t sign_a = mpd_sign(a);
- uint8_t sign_ab = mpd_sign(a)^mpd_sign(b);
- ideal_exp = (a->exp > b->exp) ? b->exp : a->exp;
- if (mpd_iszerocoeff(a)) {
- if (!mpd_qcopy(r, a, status)) {
- goto nanresult; /* GCOV_NOT_REACHED */
- }
- r->exp = ideal_exp;
- _settriple(q, sign_ab, 0, 0);
- return;
- }
- expdiff = mpd_adjexp(a) - mpd_adjexp(b);
- if (expdiff < 0) {
- if (a->exp > b->exp) {
- /* positive and less than b->digits - a->digits */
- shift = a->exp - b->exp;
- if (!mpd_qshiftl(r, a, shift, status)) {
- goto nanresult;
- }
- r->exp = ideal_exp;
- }
- else {
- if (!mpd_qcopy(r, a, status)) {
- goto nanresult;
- }
- }
- _settriple(q, sign_ab, 0, 0);
- return;
- }
- if (expdiff > ctx->prec) {
- *status |= MPD_Division_impossible;
- goto nanresult;
- }
- /*
- * At this point we have:
- * (1) 0 <= a->exp + a->digits - b->exp - b->digits <= prec
- * (2) a->exp - b->exp >= b->digits - a->digits
- * (3) a->exp - b->exp <= prec + b->digits - a->digits
- */
- if (a->exp != b->exp) {
- shift = a->exp - b->exp;
- if (shift > 0) {
- /* by (3), after the shift a->digits <= prec + b->digits */
- if (!mpd_qshiftl(&aligned, a, shift, status)) {
- goto nanresult;
- }
- a = &aligned;
- }
- else {
- shift = -shift;
- /* by (2), after the shift b->digits <= a->digits */
- if (!mpd_qshiftl(&aligned, b, shift, status)) {
- goto nanresult;
- }
- b = &aligned;
- }
- }
- qsize = a->len - b->len + 1;
- if (!(q == a && qsize < a->len) && !(q == b && qsize < b->len)) {
- if (!mpd_qresize(q, qsize, status)) {
- goto nanresult;
- }
- }
- rsize = b->len;
- if (!(r == a && rsize < a->len)) {
- if (!mpd_qresize(r, rsize, status)) {
- goto nanresult;
- }
- }
- if (b->len == 1) {
- if (a->len == 1) {
- _mpd_div_word(&q->data[0], &r->data[0], a->data[0], b->data[0]);
- }
- else {
- r->data[0] = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
- }
- }
- else if (a->len < 2*MPD_NEWTONDIV_CUTOFF &&
- b->len < MPD_NEWTONDIV_CUTOFF) {
- int ret;
- ret = _mpd_basedivmod(q->data, r->data, a->data, b->data,
- a->len, b->len);
- if (ret == -1) {
- *status |= MPD_Malloc_error;
- goto nanresult;
- }
- }
- else {
- _mpd_qbarrett_divmod(q, r, a, b, status);
- if (mpd_isspecial(q) || mpd_isspecial(r)) {
- goto nanresult;
- }
- if (mpd_isinfinite(q) || q->digits > ctx->prec) {
- *status |= MPD_Division_impossible;
- goto nanresult;
- }
- qsize = q->len;
- rsize = r->len;
- }
- qsize = _mpd_real_size(q->data, qsize);
- /* resize to smaller cannot fail */
- mpd_qresize(q, qsize, status);
- q->len = qsize;
- mpd_setdigits(q);
- mpd_set_flags(q, sign_ab);
- q->exp = 0;
- if (q->digits > ctx->prec) {
- *status |= MPD_Division_impossible;
- goto nanresult;
- }
- rsize = _mpd_real_size(r->data, rsize);
- /* resize to smaller cannot fail */
- mpd_qresize(r, rsize, status);
- r->len = rsize;
- mpd_setdigits(r);
- mpd_set_flags(r, sign_a);
- r->exp = ideal_exp;
- out:
- mpd_del(&aligned);
- return;
- nanresult:
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- goto out;
- }
- /* Integer division with remainder. */
- void
- mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint8_t sign = mpd_sign(a)^mpd_sign(b);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(q, a, b, ctx, status)) {
- mpd_qcopy(r, q, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- if (mpd_isinfinite(b)) {
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- }
- else {
- mpd_setspecial(q, sign, MPD_INF);
- }
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- *status |= MPD_Invalid_operation;
- return;
- }
- if (mpd_isinfinite(b)) {
- if (!mpd_qcopy(r, a, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- return;
- }
- mpd_qfinalize(r, ctx, status);
- _settriple(q, sign, 0, 0);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- *status |= MPD_Division_undefined;
- }
- else {
- mpd_setspecial(q, sign, MPD_INF);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- *status |= (MPD_Division_by_zero|MPD_Invalid_operation);
- }
- return;
- }
- _mpd_qdivmod(q, r, a, b, ctx, status);
- mpd_qfinalize(q, ctx, status);
- mpd_qfinalize(r, ctx, status);
- }
- void
- mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(r,0,0,0,0);
- uint8_t sign = mpd_sign(a)^mpd_sign(b);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(q, a, b, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a) && mpd_isinfinite(b)) {
- mpd_seterror(q, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_setspecial(q, sign, MPD_INF);
- return;
- }
- if (mpd_isinfinite(b)) {
- _settriple(q, sign, 0, 0);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(q, MPD_Division_undefined, status);
- }
- else {
- mpd_setspecial(q, sign, MPD_INF);
- *status |= MPD_Division_by_zero;
- }
- return;
- }
- _mpd_qdivmod(q, &r, a, b, ctx, status);
- mpd_del(&r);
- mpd_qfinalize(q, ctx, status);
- }
- /* Divide decimal by mpd_ssize_t. */
- void
- mpd_qdiv_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_ssize(&bb, b, &maxcontext, status);
- mpd_qdiv(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Divide decimal by mpd_uint_t. */
- void
- mpd_qdiv_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_uint(&bb, b, &maxcontext, status);
- mpd_qdiv(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Divide decimal by int32_t. */
- void
- mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qdiv_ssize(result, a, b, ctx, status);
- }
- /* Divide decimal by uint32_t. */
- void
- mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qdiv_uint(result, a, b, ctx, status);
- }
- #ifdef CONFIG_64
- /* Divide decimal by int64_t. */
- void
- mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qdiv_ssize(result, a, b, ctx, status);
- }
- /* Divide decimal by uint64_t. */
- void
- mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qdiv_uint(result, a, b, ctx, status);
- }
- #endif
- #if defined(_MSC_VER)
- /* conversion from 'double' to 'mpd_ssize_t', possible loss of data */
- #pragma warning(disable:4244)
- #endif
- /*
- * Get the number of iterations for the Horner scheme in _mpd_qexp().
- */
- static inline mpd_ssize_t
- _mpd_get_exp_iterations(const mpd_t *a, mpd_ssize_t prec)
- {
- mpd_uint_t dummy;
- mpd_uint_t msdigits;
- double f;
- /* 9 is MPD_RDIGITS for 32 bit platforms */
- _mpd_get_msdigits(&dummy, &msdigits, a, 9);
- f = ((double)msdigits + 1) / mpd_pow10[mpd_word_digits(msdigits)];
- #ifdef CONFIG_64
- #ifdef USE_80BIT_LONG_DOUBLE
- return ceill((1.435*(long double)prec - 1.182)
- / log10l((long double)prec/f));
- #else
- /* prec > floor((1ULL<<53) / 1.435) */
- if (prec > 6276793905742851LL) {
- return MPD_SSIZE_MAX;
- }
- return ceil((1.435*(double)prec - 1.182) / log10((double)prec/f));
- #endif
- #else /* CONFIG_32 */
- return ceil((1.435*(double)prec - 1.182) / log10((double)prec/f));
- #if defined(_MSC_VER)
- #pragma warning(default:4244)
- #endif
- #endif
- }
- /*
- * Internal function, specials have been dealt with.
- *
- * The algorithm is from Hull&Abrham, Variable Precision Exponential Function,
- * ACM Transactions on Mathematical Software, Vol. 12, No. 2, June 1986.
- *
- * Main differences:
- *
- * - The number of iterations for the Horner scheme is calculated using the
- * C log10() function.
- *
- * - The analysis for early abortion has been adapted for the mpd_t
- * ranges.
- */
- static void
- _mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx;
- MPD_NEW_STATIC(tmp,0,0,0,0);
- MPD_NEW_STATIC(sum,0,0,0,0);
- MPD_NEW_CONST(word,0,0,0,1,1,1);
- mpd_ssize_t j, n, t;
- assert(!mpd_isspecial(a));
- /*
- * We are calculating e^x = e^(r*10^t) = (e^r)^(10^t), where r < 1 and t >= 0.
- *
- * If t > 0, we have:
- *
- * (1) 0.1 <= r < 1, so e^r >= e^0.1. Overflow in the final power operation
- * will occur when (e^0.1)^(10^t) > 10^(emax+1). If we consider MAX_EMAX,
- * this will happen for t > 10 (32 bit) or (t > 19) (64 bit).
- *
- * (2) -1 < r <= -0.1, so e^r > e^-1. Underflow in the final power operation
- * will occur when (e^-1)^(10^t) < 10^(etiny-1). If we consider MIN_ETINY,
- * this will also happen for t > 10 (32 bit) or (t > 19) (64 bit).
- */
- #if defined(CONFIG_64)
- #define MPD_EXP_MAX_T 19
- #elif defined(CONFIG_32)
- #define MPD_EXP_MAX_T 10
- #endif
- t = a->digits + a->exp;
- t = (t > 0) ? t : 0;
- if (t > MPD_EXP_MAX_T) {
- if (mpd_ispositive(a)) {
- mpd_setspecial(result, MPD_POS, MPD_INF);
- *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
- }
- else {
- _settriple(result, MPD_POS, 0, mpd_etiny(ctx));
- *status |= (MPD_Inexact|MPD_Rounded|MPD_Subnormal|
- MPD_Underflow|MPD_Clamped);
- }
- return;
- }
- mpd_maxcontext(&workctx);
- workctx.prec = ctx->prec + t + 2;
- workctx.prec = (workctx.prec < 9) ? 9 : workctx.prec;
- workctx.round = MPD_ROUND_HALF_EVEN;
- if ((n = _mpd_get_exp_iterations(a, workctx.prec)) == MPD_SSIZE_MAX) {
- mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_UNLIKELY */
- goto finish; /* GCOV_UNLIKELY */
- }
- if (!mpd_qcopy(result, a, status)) {
- goto finish;
- }
- result->exp -= t;
- _settriple(&sum, MPD_POS, 1, 0);
- for (j = n-1; j >= 1; j--) {
- word.data[0] = j;
- mpd_setdigits(&word);
- mpd_qdiv(&tmp, result, &word, &workctx, &workctx.status);
- mpd_qmul(&sum, &sum, &tmp, &workctx, &workctx.status);
- mpd_qadd(&sum, &sum, &one, &workctx, &workctx.status);
- }
- #ifdef CONFIG_64
- _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status);
- #else
- if (t <= MPD_MAX_POW10) {
- _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status);
- }
- else {
- t -= MPD_MAX_POW10;
- _mpd_qpow_uint(&tmp, &sum, mpd_pow10[MPD_MAX_POW10], MPD_POS,
- &workctx, status);
- _mpd_qpow_uint(result, &tmp, mpd_pow10[t], MPD_POS, &workctx, status);
- }
- #endif
- finish:
- mpd_del(&tmp);
- mpd_del(&sum);
- *status |= (workctx.status&MPD_Errors);
- *status |= (MPD_Inexact|MPD_Rounded);
- }
- /* exp(a) */
- void
- mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx;
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isnegative(a)) {
- _settriple(result, MPD_POS, 0, 0);
- }
- else {
- mpd_setspecial(result, MPD_POS, MPD_INF);
- }
- return;
- }
- if (mpd_iszerocoeff(a)) {
- _settriple(result, MPD_POS, 1, 0);
- return;
- }
- workctx = *ctx;
- workctx.round = MPD_ROUND_HALF_EVEN;
- if (ctx->allcr) {
- MPD_NEW_STATIC(t1, 0,0,0,0);
- MPD_NEW_STATIC(t2, 0,0,0,0);
- MPD_NEW_STATIC(ulp, 0,0,0,0);
- MPD_NEW_STATIC(aa, 0,0,0,0);
- mpd_ssize_t prec;
- if (result == a) {
- if (!mpd_qcopy(&aa, a, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- a = &aa;
- }
- workctx.clamp = 0;
- prec = ctx->prec + 3;
- while (1) {
- workctx.prec = prec;
- _mpd_qexp(result, a, &workctx, status);
- _ssettriple(&ulp, MPD_POS, 1,
- result->exp + result->digits-workctx.prec-1);
- workctx.prec = ctx->prec;
- mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status);
- mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status);
- if (mpd_isspecial(result) || mpd_iszerocoeff(result) ||
- mpd_qcmp(&t1, &t2, status) == 0) {
- workctx.clamp = ctx->clamp;
- mpd_check_underflow(result, &workctx, status);
- mpd_qfinalize(result, &workctx, status);
- break;
- }
- prec += MPD_RDIGITS;
- }
- mpd_del(&t1);
- mpd_del(&t2);
- mpd_del(&ulp);
- mpd_del(&aa);
- }
- else {
- _mpd_qexp(result, a, &workctx, status);
- mpd_check_underflow(result, &workctx, status);
- mpd_qfinalize(result, &workctx, status);
- }
- }
- /* Fused multiply-add: (a * b) + c, with a single final rounding. */
- void
- mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_t *cc = (mpd_t *)c;
- if (result == c) {
- if ((cc = mpd_qncopy(c)) == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- }
- _mpd_qmul(result, a, b, ctx, &workstatus);
- if (!(workstatus&MPD_Invalid_operation)) {
- mpd_qadd(result, result, cc, ctx, &workstatus);
- }
- if (cc != c) mpd_del(cc);
- *status |= workstatus;
- }
- static inline int
- ln_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], mpd_ssize_t maxprec,
- mpd_ssize_t initprec)
- {
- mpd_ssize_t k;
- int i;
- assert(maxprec >= 2 && initprec >= 2);
- if (maxprec <= initprec) return -1;
- i = 0; k = maxprec;
- do {
- k = (k+2) / 2;
- klist[i++] = k;
- } while (k > initprec);
- return i-1;
- }
- /* Two word initial approximations for ln(10) */
- #ifdef CONFIG_64
- #if MPD_RDIGITS != 19
- #error "mpdecimal.c: MPD_RDIGITS must be 19."
- #endif
- static mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = {
- 179914546843642076, 2302585092994045684
- };
- static mpd_uint_t mpd_ln10_init[2] = {
- 179914546843642076, 2302585092994045684
- };
- #else
- #if MPD_RDIGITS != 9
- #error "mpdecimal.c: MPD_RDIGITS must be 9."
- #endif
- static mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = {299404568, 230258509};
- static mpd_uint_t mpd_ln10_init[2] = {299404568, 230258509};
- #endif
- /* mpd_ln10 is cached in order to speed up computations */
- mpd_t mpd_ln10 = {MPD_STATIC|MPD_STATIC_DATA, -(2*MPD_RDIGITS-1),
- 2*MPD_RDIGITS, 2, MPD_MINALLOC_MAX, mpd_ln10_data};
- static void
- mpd_reset_ln10(void)
- {
- if (mpd_isdynamic_data(&mpd_ln10)) {
- mpd_free(mpd_ln10.data);
- }
- mpd_ln10.data = mpd_ln10_data;
- mpd_ln10_data[0] = mpd_ln10_init[0];
- mpd_ln10_data[1] = mpd_ln10_init[1];
- mpd_ln10.flags = MPD_STATIC|MPD_STATIC_DATA;
- mpd_ln10.exp = -(2*MPD_RDIGITS-1);
- mpd_ln10.digits = 2*MPD_RDIGITS;
- mpd_ln10.len = 2;
- mpd_ln10.alloc = MPD_MINALLOC_MAX;
- }
- /*
- * Initializes or updates mpd_ln10. If mpd_ln10 is cached and has exactly the
- * requested precision, the function returns. If the cached precision is greater
- * than the requested precision, mpd_ln10 is shifted to the requested precision.
- *
- * The function can fail with MPD_Malloc_error.
- */
- void
- mpd_update_ln10(mpd_ssize_t maxprec, uint32_t *status)
- {
- mpd_context_t varcontext, maxcontext;
- MPD_NEW_STATIC(tmp, 0,0,0,0);
- MPD_NEW_CONST(static10, 0,0,2,1,1,10);
- mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
- int i;
- if (mpd_isspecial(&mpd_ln10)) {
- mpd_reset_ln10();
- }
- if (mpd_ln10.digits > maxprec) {
- /* shift to smaller cannot fail */
- mpd_qshiftr_inplace(&mpd_ln10, mpd_ln10.digits-maxprec);
- mpd_ln10.exp = -(mpd_ln10.digits-1);
- return;
- }
- else if (mpd_ln10.digits == maxprec) {
- return;
- }
- mpd_maxcontext(&maxcontext);
- mpd_maxcontext(&varcontext);
- varcontext.round = MPD_ROUND_TRUNC;
- i = ln_schedule_prec(klist, maxprec+2, mpd_ln10.digits);
- for (; i >= 0; i--) {
- varcontext.prec = 2*klist[i]+3;
- mpd_ln10.flags ^= MPD_NEG;
- _mpd_qexp(&tmp, &mpd_ln10, &varcontext, status);
- mpd_ln10.flags ^= MPD_NEG;
- mpd_qmul(&tmp, &static10, &tmp, &varcontext, status);
- mpd_qsub(&tmp, &tmp, &one, &maxcontext, status);
- mpd_qadd(&mpd_ln10, &mpd_ln10, &tmp, &maxcontext, status);
- if (mpd_isspecial(&mpd_ln10)) {
- break;
- }
- }
- mpd_del(&tmp);
- varcontext.prec = maxprec;
- varcontext.round = MPD_ROUND_HALF_EVEN;
- mpd_qfinalize(&mpd_ln10, &varcontext, status);
- }
- /* Initial approximations for the ln() iteration */
- static const uint16_t lnapprox[900] = {
- /* index 0 - 400: log((i+100)/100) * 1000 */
- 0, 10, 20, 30, 39, 49, 58, 68, 77, 86, 95, 104, 113, 122, 131, 140, 148, 157,
- 166, 174, 182, 191, 199, 207, 215, 223, 231, 239, 247, 255, 262, 270, 278,
- 285, 293, 300, 308, 315, 322, 329, 336, 344, 351, 358, 365, 372, 378, 385,
- 392, 399, 406, 412, 419, 425, 432, 438, 445, 451, 457, 464, 470, 476, 482,
- 489, 495, 501, 507, 513, 519, 525, 531, 536, 542, 548, 554, 560, 565, 571,
- 577, 582, 588, 593, 599, 604, 610, 615, 621, 626, 631, 637, 642, 647, 652,
- 658, 663, 668, 673, 678, 683, 688, 693, 698, 703, 708, 713, 718, 723, 728,
- 732, 737, 742, 747, 751, 756, 761, 766, 770, 775, 779, 784, 788, 793, 798,
- 802, 806, 811, 815, 820, 824, 829, 833, 837, 842, 846, 850, 854, 859, 863,
- 867, 871, 876, 880, 884, 888, 892, 896, 900, 904, 908, 912, 916, 920, 924,
- 928, 932, 936, 940, 944, 948, 952, 956, 959, 963, 967, 971, 975, 978, 982,
- 986, 990, 993, 997, 1001, 1004, 1008, 1012, 1015, 1019, 1022, 1026, 1030,
- 1033, 1037, 1040, 1044, 1047, 1051, 1054, 1058, 1061, 1065, 1068, 1072, 1075,
- 1078, 1082, 1085, 1089, 1092, 1095, 1099, 1102, 1105, 1109, 1112, 1115, 1118,
- 1122, 1125, 1128, 1131, 1135, 1138, 1141, 1144, 1147, 1151, 1154, 1157, 1160,
- 1163, 1166, 1169, 1172, 1176, 1179, 1182, 1185, 1188, 1191, 1194, 1197, 1200,
- 1203, 1206, 1209, 1212, 1215, 1218, 1221, 1224, 1227, 1230, 1233, 1235, 1238,
- 1241, 1244, 1247, 1250, 1253, 1256, 1258, 1261, 1264, 1267, 1270, 1273, 1275,
- 1278, 1281, 1284, 1286, 1289, 1292, 1295, 1297, 1300, 1303, 1306, 1308, 1311,
- 1314, 1316, 1319, 1322, 1324, 1327, 1330, 1332, 1335, 1338, 1340, 1343, 1345,
- 1348, 1351, 1353, 1356, 1358, 1361, 1364, 1366, 1369, 1371, 1374, 1376, 1379,
- 1381, 1384, 1386, 1389, 1391, 1394, 1396, 1399, 1401, 1404, 1406, 1409, 1411,
- 1413, 1416, 1418, 1421, 1423, 1426, 1428, 1430, 1433, 1435, 1437, 1440, 1442,
- 1445, 1447, 1449, 1452, 1454, 1456, 1459, 1461, 1463, 1466, 1468, 1470, 1472,
- 1475, 1477, 1479, 1482, 1484, 1486, 1488, 1491, 1493, 1495, 1497, 1500, 1502,
- 1504, 1506, 1509, 1511, 1513, 1515, 1517, 1520, 1522, 1524, 1526, 1528, 1530,
- 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1548, 1550, 1552, 1554, 1556, 1558,
- 1560, 1562, 1564, 1567, 1569, 1571, 1573, 1575, 1577, 1579, 1581, 1583, 1585,
- 1587, 1589, 1591, 1593, 1595, 1597, 1599, 1601, 1603, 1605, 1607, 1609,
- /* index 401 - 899: -log((i+100)/1000) * 1000 */
- 691, 689, 687, 685, 683, 681, 679, 677, 675, 673, 671, 669, 668, 666, 664,
- 662, 660, 658, 656, 654, 652, 650, 648, 646, 644, 642, 641, 639, 637, 635,
- 633, 631, 629, 627, 626, 624, 622, 620, 618, 616, 614, 612, 611, 609, 607,
- 605, 603, 602, 600, 598, 596, 594, 592, 591, 589, 587, 585, 583, 582, 580,
- 578, 576, 574, 573, 571, 569, 567, 566, 564, 562, 560, 559, 557, 555, 553,
- 552, 550, 548, 546, 545, 543, 541, 540, 538, 536, 534, 533, 531, 529, 528,
- 526, 524, 523, 521, 519, 518, 516, 514, 512, 511, 509, 508, 506, 504, 502,
- 501, 499, 498, 496, 494, 493, 491, 489, 488, 486, 484, 483, 481, 480, 478,
- 476, 475, 473, 472, 470, 468, 467, 465, 464, 462, 460, 459, 457, 456, 454,
- 453, 451, 449, 448, 446, 445, 443, 442, 440, 438, 437, 435, 434, 432, 431,
- 429, 428, 426, 425, 423, 422, 420, 419, 417, 416, 414, 412, 411, 410, 408,
- 406, 405, 404, 402, 400, 399, 398, 396, 394, 393, 392, 390, 389, 387, 386,
- 384, 383, 381, 380, 378, 377, 375, 374, 372, 371, 370, 368, 367, 365, 364,
- 362, 361, 360, 358, 357, 355, 354, 352, 351, 350, 348, 347, 345, 344, 342,
- 341, 340, 338, 337, 336, 334, 333, 331, 330, 328, 327, 326, 324, 323, 322,
- 320, 319, 318, 316, 315, 313, 312, 311, 309, 308, 306, 305, 304, 302, 301,
- 300, 298, 297, 296, 294, 293, 292, 290, 289, 288, 286, 285, 284, 282, 281,
- 280, 278, 277, 276, 274, 273, 272, 270, 269, 268, 267, 265, 264, 263, 261,
- 260, 259, 258, 256, 255, 254, 252, 251, 250, 248, 247, 246, 245, 243, 242,
- 241, 240, 238, 237, 236, 234, 233, 232, 231, 229, 228, 227, 226, 224, 223,
- 222, 221, 219, 218, 217, 216, 214, 213, 212, 211, 210, 208, 207, 206, 205,
- 203, 202, 201, 200, 198, 197, 196, 195, 194, 192, 191, 190, 189, 188, 186,
- 185, 184, 183, 182, 180, 179, 178, 177, 176, 174, 173, 172, 171, 170, 168,
- 167, 166, 165, 164, 162, 161, 160, 159, 158, 157, 156, 154, 153, 152, 151,
- 150, 148, 147, 146, 145, 144, 143, 142, 140, 139, 138, 137, 136, 135, 134,
- 132, 131, 130, 129, 128, 127, 126, 124, 123, 122, 121, 120, 119, 118, 116,
- 115, 114, 113, 112, 111, 110, 109, 108, 106, 105, 104, 103, 102, 101, 100,
- 99, 98, 97, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79,
- 78, 77, 76, 75, 74, 73, 72, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59,
- 58, 57, 56, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39,
- 38, 37, 36, 35, 34, 33, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19,
- 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
- };
- /* Internal ln() function that does not check for specials, zero or one. */
- static void
- _mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t varcontext, maxcontext;
- mpd_t *z = (mpd_t *) result;
- MPD_NEW_STATIC(v,0,0,0,0);
- MPD_NEW_STATIC(vtmp,0,0,0,0);
- MPD_NEW_STATIC(tmp,0,0,0,0);
- mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
- mpd_ssize_t maxprec, shift, t;
- mpd_ssize_t a_digits, a_exp;
- mpd_uint_t dummy, x;
- int i;
- assert(!mpd_isspecial(a) && !mpd_iszerocoeff(a));
- /*
- * We are calculating ln(a) = ln(v * 10^t) = ln(v) + t*ln(10),
- * where 0.5 < v <= 5.
- */
- if (!mpd_qcopy(&v, a, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- goto finish;
- }
- /* Initial approximation: we have at least one non-zero digit */
- _mpd_get_msdigits(&dummy, &x, &v, 3);
- if (x < 10) x *= 10;
- if (x < 100) x *= 10;
- x -= 100;
- /* a may equal z */
- a_digits = a->digits;
- a_exp = a->exp;
- mpd_minalloc(z);
- mpd_clear_flags(z);
- z->data[0] = lnapprox[x];
- z->len = 1;
- z->exp = -3;
- mpd_setdigits(z);
- if (x <= 400) {
- v.exp = -(a_digits - 1);
- t = a_exp + a_digits - 1;
- }
- else {
- v.exp = -a_digits;
- t = a_exp + a_digits;
- mpd_set_negative(z);
- }
- mpd_maxcontext(&maxcontext);
- mpd_maxcontext(&varcontext);
- varcontext.round = MPD_ROUND_TRUNC;
- maxprec = ctx->prec + 2;
- if (x <= 10 || x >= 805) {
- /* v is close to 1: Estimate the magnitude of the logarithm.
- * If v = 1 or ln(v) will underflow, skip the loop. Otherwise,
- * adjust the precision upwards in order to obtain a sufficient
- * number of significant digits.
- *
- * 1) x/(1+x) < ln(1+x) < x, for x > -1, x != 0
- *
- * 2) (v-1)/v < ln(v) < v-1
- */
- mpd_t *lower = &tmp;
- mpd_t *upper = &vtmp;
- int cmp = _mpd_cmp(&v, &one);
- varcontext.round = MPD_ROUND_CEILING;
- varcontext.prec = maxprec;
- mpd_qsub(upper, &v, &one, &varcontext, &varcontext.status);
- varcontext.round = MPD_ROUND_FLOOR;
- mpd_qdiv(lower, upper, &v, &varcontext, &varcontext.status);
- varcontext.round = MPD_ROUND_TRUNC;
- if (cmp < 0) {
- _mpd_ptrswap(&upper, &lower);
- }
- if (mpd_adjexp(upper) < mpd_etiny(ctx)) {
- _settriple(z, (cmp<0), 1, mpd_etiny(ctx)-1);
- goto postloop;
- }
- if (mpd_adjexp(lower) < 0) {
- maxprec = maxprec - mpd_adjexp(lower);
- }
- }
- i = ln_schedule_prec(klist, maxprec, 2);
- for (; i >= 0; i--) {
- varcontext.prec = 2*klist[i]+3;
- z->flags ^= MPD_NEG;
- _mpd_qexp(&tmp, z, &varcontext, status);
- z->flags ^= MPD_NEG;
- if (v.digits > varcontext.prec) {
- shift = v.digits - varcontext.prec;
- mpd_qshiftr(&vtmp, &v, shift, status);
- vtmp.exp += shift;
- mpd_qmul(&tmp, &vtmp, &tmp, &varcontext, status);
- }
- else {
- mpd_qmul(&tmp, &v, &tmp, &varcontext, status);
- }
- mpd_qsub(&tmp, &tmp, &one, &maxcontext, status);
- mpd_qadd(z, z, &tmp, &maxcontext, status);
- if (mpd_isspecial(z)) {
- break;
- }
- }
- postloop:
- mpd_update_ln10(maxprec+2, status);
- mpd_qmul_ssize(&tmp, &mpd_ln10, t, &maxcontext, status);
- varcontext.prec = maxprec+2;
- mpd_qadd(result, &tmp, z, &varcontext, status);
- finish:
- mpd_del(&v);
- mpd_del(&vtmp);
- mpd_del(&tmp);
- }
- /* ln(a) */
- void
- mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx;
- mpd_ssize_t adjexp, t;
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- mpd_setspecial(result, MPD_POS, MPD_INF);
- return;
- }
- if (mpd_iszerocoeff(a)) {
- mpd_setspecial(result, MPD_NEG, MPD_INF);
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (_mpd_cmp(a, &one) == 0) {
- _settriple(result, MPD_POS, 0, 0);
- return;
- }
- /* Check if the result will overflow.
- *
- * 1) adjexp(a) + 1 > log10(a) >= adjexp(a)
- *
- * 2) |log10(a)| >= adjexp(a), if adjexp(a) >= 0
- * |log10(a)| > -adjexp(a)-1, if adjexp(a) < 0
- *
- * 3) |log(a)| > 2*|log10(a)|
- */
- adjexp = mpd_adjexp(a);
- t = (adjexp < 0) ? -adjexp-1 : adjexp;
- t *= 2;
- if (mpd_exp_digits(t)-1 > ctx->emax) {
- *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
- mpd_setspecial(result, (adjexp<0), MPD_INF);
- return;
- }
- workctx = *ctx;
- workctx.round = MPD_ROUND_HALF_EVEN;
- if (ctx->allcr) {
- MPD_NEW_STATIC(t1, 0,0,0,0);
- MPD_NEW_STATIC(t2, 0,0,0,0);
- MPD_NEW_STATIC(ulp, 0,0,0,0);
- MPD_NEW_STATIC(aa, 0,0,0,0);
- mpd_ssize_t prec;
- if (result == a) {
- if (!mpd_qcopy(&aa, a, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- a = &aa;
- }
- workctx.clamp = 0;
- prec = ctx->prec + 3;
- while (1) {
- workctx.prec = prec;
- _mpd_qln(result, a, &workctx, status);
- _ssettriple(&ulp, MPD_POS, 1,
- result->exp + result->digits-workctx.prec-1);
- workctx.prec = ctx->prec;
- mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status);
- mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status);
- if (mpd_isspecial(result) || mpd_iszerocoeff(result) ||
- mpd_qcmp(&t1, &t2, status) == 0) {
- workctx.clamp = ctx->clamp;
- mpd_check_underflow(result, &workctx, status);
- mpd_qfinalize(result, &workctx, status);
- break;
- }
- prec += MPD_RDIGITS;
- }
- mpd_del(&t1);
- mpd_del(&t2);
- mpd_del(&ulp);
- mpd_del(&aa);
- }
- else {
- _mpd_qln(result, a, &workctx, status);
- mpd_check_underflow(result, &workctx, status);
- mpd_qfinalize(result, &workctx, status);
- }
- }
- /* Internal log10() function that does not check for specials, zero, ... */
- static void
- _mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx;
- mpd_maxcontext(&workctx);
- workctx.prec = ctx->prec + 3;
- _mpd_qln(result, a, &workctx, status);
- mpd_update_ln10(workctx.prec, status);
- workctx = *ctx;
- workctx.round = MPD_ROUND_HALF_EVEN;
- _mpd_qdiv(NO_IDEAL_EXP, result, result, &mpd_ln10, &workctx, status);
- }
- /* log10(a) */
- void
- mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx;
- mpd_ssize_t adjexp, t;
- workctx = *ctx;
- workctx.round = MPD_ROUND_HALF_EVEN;
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- mpd_setspecial(result, MPD_POS, MPD_INF);
- return;
- }
- if (mpd_iszerocoeff(a)) {
- mpd_setspecial(result, MPD_NEG, MPD_INF);
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_coeff_ispow10(a)) {
- uint8_t sign = 0;
- adjexp = mpd_adjexp(a);
- if (adjexp < 0) {
- sign = 1;
- adjexp = -adjexp;
- }
- _settriple(result, sign, adjexp, 0);
- mpd_qfinalize(result, &workctx, status);
- return;
- }
- /* Check if the result will overflow.
- *
- * 1) adjexp(a) + 1 > log10(a) >= adjexp(a)
- *
- * 2) |log10(a)| >= adjexp(a), if adjexp(a) >= 0
- * |log10(a)| > -adjexp(a)-1, if adjexp(a) < 0
- */
- adjexp = mpd_adjexp(a);
- t = (adjexp < 0) ? -adjexp-1 : adjexp;
- if (mpd_exp_digits(t)-1 > ctx->emax) {
- *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
- mpd_setspecial(result, (adjexp<0), MPD_INF);
- return;
- }
- if (ctx->allcr) {
- MPD_NEW_STATIC(t1, 0,0,0,0);
- MPD_NEW_STATIC(t2, 0,0,0,0);
- MPD_NEW_STATIC(ulp, 0,0,0,0);
- MPD_NEW_STATIC(aa, 0,0,0,0);
- mpd_ssize_t prec;
- if (result == a) {
- if (!mpd_qcopy(&aa, a, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- a = &aa;
- }
- workctx.clamp = 0;
- prec = ctx->prec + 3;
- while (1) {
- workctx.prec = prec;
- _mpd_qlog10(result, a, &workctx, status);
- _ssettriple(&ulp, MPD_POS, 1,
- result->exp + result->digits-workctx.prec-1);
- workctx.prec = ctx->prec;
- mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status);
- mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status);
- if (mpd_isspecial(result) || mpd_iszerocoeff(result) ||
- mpd_qcmp(&t1, &t2, status) == 0) {
- workctx.clamp = ctx->clamp;
- mpd_check_underflow(result, &workctx, status);
- mpd_qfinalize(result, &workctx, status);
- break;
- }
- prec += MPD_RDIGITS;
- }
- mpd_del(&t1);
- mpd_del(&t2);
- mpd_del(&ulp);
- mpd_del(&aa);
- }
- else {
- _mpd_qlog10(result, a, &workctx, status);
- mpd_check_underflow(result, &workctx, status);
- }
- }
- /*
- * Maximum of the two operands. Attention: If one operand is a quiet NaN and the
- * other is numeric, the numeric operand is returned. This may not be what one
- * expects.
- */
- void
- mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isqnan(a) && !mpd_isnan(b)) {
- mpd_qcopy(result, b, status);
- }
- else if (mpd_isqnan(b) && !mpd_isnan(a)) {
- mpd_qcopy(result, a, status);
- }
- else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- else {
- c = _mpd_cmp(a, b);
- if (c == 0) {
- c = _mpd_cmp_numequal(a, b);
- }
- if (c < 0) {
- mpd_qcopy(result, b, status);
- }
- else {
- mpd_qcopy(result, a, status);
- }
- }
- mpd_qfinalize(result, ctx, status);
- }
- /*
- * Maximum magnitude: Same as mpd_max(), but compares the operands with their
- * sign ignored.
- */
- void
- mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isqnan(a) && !mpd_isnan(b)) {
- mpd_qcopy(result, b, status);
- }
- else if (mpd_isqnan(b) && !mpd_isnan(a)) {
- mpd_qcopy(result, a, status);
- }
- else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- else {
- c = _mpd_cmp_abs(a, b);
- if (c == 0) {
- c = _mpd_cmp_numequal(a, b);
- }
- if (c < 0) {
- mpd_qcopy(result, b, status);
- }
- else {
- mpd_qcopy(result, a, status);
- }
- }
- mpd_qfinalize(result, ctx, status);
- }
- /*
- * Minimum of the two operands. Attention: If one operand is a quiet NaN and the
- * other is numeric, the numeric operand is returned. This may not be what one
- * expects.
- */
- void
- mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isqnan(a) && !mpd_isnan(b)) {
- mpd_qcopy(result, b, status);
- }
- else if (mpd_isqnan(b) && !mpd_isnan(a)) {
- mpd_qcopy(result, a, status);
- }
- else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- else {
- c = _mpd_cmp(a, b);
- if (c == 0) {
- c = _mpd_cmp_numequal(a, b);
- }
- if (c < 0) {
- mpd_qcopy(result, a, status);
- }
- else {
- mpd_qcopy(result, b, status);
- }
- }
- mpd_qfinalize(result, ctx, status);
- }
- /*
- * Minimum magnitude: Same as mpd_min(), but compares the operands with their
- * sign ignored.
- */
- void
- mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isqnan(a) && !mpd_isnan(b)) {
- mpd_qcopy(result, b, status);
- }
- else if (mpd_isqnan(b) && !mpd_isnan(a)) {
- mpd_qcopy(result, a, status);
- }
- else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- else {
- c = _mpd_cmp_abs(a, b);
- if (c == 0) {
- c = _mpd_cmp_numequal(a, b);
- }
- if (c < 0) {
- mpd_qcopy(result, a, status);
- }
- else {
- mpd_qcopy(result, b, status);
- }
- }
- mpd_qfinalize(result, ctx, status);
- }
- /* Minimum space needed for the result array in _karatsuba_rec(). */
- static inline mpd_size_t
- _kmul_resultsize(mpd_size_t la, mpd_size_t lb)
- {
- mpd_size_t n, m;
- n = add_size_t(la, lb);
- n = add_size_t(n, 1);
- m = (la+1)/2 + 1;
- m = mul_size_t(m, 3);
- return (m > n) ? m : n;
- }
- /* Work space needed in _karatsuba_rec(). lim >= 4 */
- static inline mpd_size_t
- _kmul_worksize(mpd_size_t n, mpd_size_t lim)
- {
- mpd_size_t m;
- if (n <= lim) {
- return 0;
- }
- m = (n+1)/2 + 1;
- return add_size_t(mul_size_t(m, 2), _kmul_worksize(m, lim));
- }
- #define MPD_KARATSUBA_BASECASE 16 /* must be >= 4 */
- /*
- * Add the product of a and b to c.
- * c must be _kmul_resultsize(la, lb) in size.
- * w is used as a work array and must be _kmul_worksize(a, lim) in size.
- * Roman E. Maeder, Storage Allocation for the Karatsuba Integer Multiplication
- * Algorithm. In "Design and implementation of symbolic computation systems",
- * Springer, 1993, ISBN 354057235X, 9783540572350.
- */
- static void
- _karatsuba_rec(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b,
- mpd_uint_t *w, mpd_size_t la, mpd_size_t lb)
- {
- mpd_size_t m, lt;
- assert (la >= lb && lb > 0);
- if (la <= MPD_KARATSUBA_BASECASE) {
- _mpd_basemul(c, a, b, la, lb);
- return;
- }
- m = (la+1)/2; // ceil(la/2)
- /* lb <= m < la */
- if (lb <= m) {
- /* lb can now be larger than la-m */
- if (lb > la-m) {
- lt = lb + lb + 1; // space needed for result array
- mpd_uint_zero(w, lt); // clear result array
- _karatsuba_rec(w, b, a+m, w+lt, lb, la-m); // b*ah
- }
- else {
- lt = (la-m) + (la-m) + 1; // space needed for result array
- mpd_uint_zero(w, lt); // clear result array
- _karatsuba_rec(w, a+m, b, w+lt, la-m, lb); // ah*b
- }
- _mpd_baseaddto(c+m, w, (la-m)+lb); // add ah*b*B**m
- lt = m + m + 1; // space needed for the result array
- mpd_uint_zero(w, lt); // clear result array
- _karatsuba_rec(w, a, b, w+lt, m, lb); // al*b
- _mpd_baseaddto(c, w, m+lb); // add al*b
- return;
- }
- /* la >= lb > m */
- memcpy(w, a, m * sizeof *w);
- w[m] = 0;
- _mpd_baseaddto(w, a+m, la-m);
- memcpy(w+(m+1), b, m * sizeof *w);
- w[m+1+m] = 0;
- _mpd_baseaddto(w+(m+1), b+m, lb-m);
- _karatsuba_rec(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1);
- lt = (la-m) + (la-m) + 1;
- mpd_uint_zero(w, lt);
- _karatsuba_rec(w, a+m, b+m, w+lt, la-m, lb-m);
- _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m));
- _mpd_basesubfrom(c+m, w, (la-m) + (lb-m));
- lt = m + m + 1;
- mpd_uint_zero(w, lt);
- _karatsuba_rec(w, a, b, w+lt, m, m);
- _mpd_baseaddto(c, w, m+m);
- _mpd_basesubfrom(c+m, w, m+m);
- return;
- }
- /*
- * Multiply u and v, using Karatsuba multiplication. Returns a pointer
- * to the result or NULL in case of failure (malloc error).
- * Conditions: ulen >= vlen, ulen >= 4
- */
- mpd_uint_t *
- _mpd_kmul(const mpd_uint_t *u, const mpd_uint_t *v,
- mpd_size_t ulen, mpd_size_t vlen,
- mpd_size_t *rsize)
- {
- mpd_uint_t *result = NULL, *w = NULL;
- mpd_size_t m;
- assert(ulen >= 4);
- assert(ulen >= vlen);
- *rsize = _kmul_resultsize(ulen, vlen);
- if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) {
- return NULL;
- }
- m = _kmul_worksize(ulen, MPD_KARATSUBA_BASECASE);
- if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) {
- mpd_free(result);
- return NULL;
- }
- _karatsuba_rec(result, u, v, w, ulen, vlen);
- if (w) mpd_free(w);
- return result;
- }
- /* Determine the minimum length for the number theoretic transform. */
- static inline mpd_size_t
- _mpd_get_transform_len(mpd_size_t rsize)
- {
- mpd_size_t log2rsize;
- mpd_size_t x, step;
- assert(rsize >= 4);
- log2rsize = mpd_bsr(rsize);
- if (rsize <= 1024) {
- x = ONE_UM<<log2rsize;
- return (rsize == x) ? x : x<<1;
- }
- else if (rsize <= MPD_MAXTRANSFORM_2N) {
- x = ONE_UM<<log2rsize;
- if (rsize == x) return x;
- step = x>>1;
- x += step;
- return (rsize <= x) ? x : x + step;
- }
- else if (rsize <= MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2) {
- return MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2;
- }
- else if (rsize <= 3*MPD_MAXTRANSFORM_2N) {
- return 3*MPD_MAXTRANSFORM_2N;
- }
- else {
- return MPD_SIZE_MAX;
- }
- }
- #ifdef PPRO
- #ifndef _MSC_VER
- static inline unsigned short
- _mpd_get_control87(void)
- {
- unsigned short cw;
- __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
- return cw;
- }
- static inline void
- _mpd_set_control87(unsigned short cw)
- {
- __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
- }
- #endif
- unsigned int
- mpd_set_fenv(void)
- {
- unsigned int cw;
- #ifdef _MSC_VER
- cw = _control87(0, 0);
- _control87((_RC_CHOP|_PC_64), (_MCW_RC|_MCW_PC));
- #else
- cw = _mpd_get_control87();
- _mpd_set_control87(cw|0x780);
- #endif
- return cw;
- }
- void
- mpd_restore_fenv(unsigned int cw)
- {
- #ifdef _MSC_VER
- _control87(cw, (_MCW_RC|_MCW_PC));
- #else
- _mpd_set_control87((unsigned short)cw);
- #endif
- }
- #endif /* PPRO */
- /*
- * Multiply u and v, using the fast number theoretic transform. Returns
- * a pointer to the result or NULL in case of failure (malloc error).
- */
- mpd_uint_t *
- _mpd_fntmul(const mpd_uint_t *u, const mpd_uint_t *v,
- mpd_size_t ulen, mpd_size_t vlen,
- mpd_size_t *rsize)
- {
- mpd_uint_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *vtmp = NULL;
- mpd_size_t n;
- #ifdef PPRO
- unsigned int cw;
- cw = mpd_set_fenv();
- #endif
- *rsize = add_size_t(ulen, vlen);
- if ((n = _mpd_get_transform_len(*rsize)) == MPD_SIZE_MAX) {
- goto malloc_error;
- }
- if ((c1 = mpd_calloc(sizeof *c1, n)) == NULL) {
- goto malloc_error;
- }
- if ((c2 = mpd_calloc(sizeof *c2, n)) == NULL) {
- goto malloc_error;
- }
- if ((c3 = mpd_calloc(sizeof *c3, n)) == NULL) {
- goto malloc_error;
- }
- memcpy(c1, u, ulen * (sizeof *c1));
- memcpy(c2, u, ulen * (sizeof *c2));
- memcpy(c3, u, ulen * (sizeof *c3));
- if (u == v) {
- if (!fnt_autoconvolute(c1, n, P1) ||
- !fnt_autoconvolute(c2, n, P2) ||
- !fnt_autoconvolute(c3, n, P3)) {
- goto malloc_error;
- }
- }
- else {
- if ((vtmp = mpd_calloc(sizeof *vtmp, n)) == NULL) {
- goto malloc_error;
- }
- memcpy(vtmp, v, vlen * (sizeof *vtmp));
- if (!fnt_convolute(c1, vtmp, n, P1)) {
- mpd_free(vtmp);
- goto malloc_error;
- }
- memcpy(vtmp, v, vlen * (sizeof *vtmp));
- mpd_uint_zero(vtmp+vlen, n-vlen);
- if (!fnt_convolute(c2, vtmp, n, P2)) {
- mpd_free(vtmp);
- goto malloc_error;
- }
- memcpy(vtmp, v, vlen * (sizeof *vtmp));
- mpd_uint_zero(vtmp+vlen, n-vlen);
- if (!fnt_convolute(c3, vtmp, n, P3)) {
- mpd_free(vtmp);
- goto malloc_error;
- }
- mpd_free(vtmp);
- }
- crt3(c1, c2, c3, *rsize);
- out:
- #ifdef PPRO
- mpd_restore_fenv(cw);
- #endif
- if (c2) mpd_free(c2);
- if (c3) mpd_free(c3);
- return c1;
- malloc_error:
- if (c1) mpd_free(c1);
- c1 = NULL;
- goto out;
- }
- /*
- * Karatsuba multiplication with FNT/basemul as the base case.
- */
- static int
- _karatsuba_rec_fnt(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b,
- mpd_uint_t *w, mpd_size_t la, mpd_size_t lb)
- {
- mpd_size_t m, lt;
- assert (la >= lb && lb > 0);
- if (la <= 3*(MPD_MAXTRANSFORM_2N/2)) {
- if (lb <= 192) {
- _mpd_basemul(c, b, a, lb, la);
- }
- else {
- mpd_uint_t *result;
- mpd_size_t dummy;
- if ((result = _mpd_fntmul(a, b, la, lb, &dummy)) == NULL) {
- return 0;
- }
- memcpy(c, result, (la+lb) * (sizeof *result));
- mpd_free(result);
- }
- return 1;
- }
- m = (la+1)/2; // ceil(la/2)
- /* lb <= m < la */
- if (lb <= m) {
- /* lb can now be larger than la-m */
- if (lb > la-m) {
- lt = lb + lb + 1; // space needed for result array
- mpd_uint_zero(w, lt); // clear result array
- if (!_karatsuba_rec_fnt(w, b, a+m, w+lt, lb, la-m)) { // b*ah
- return 0; /* GCOV_UNLIKELY */
- }
- }
- else {
- lt = (la-m) + (la-m) + 1; // space needed for result array
- mpd_uint_zero(w, lt); // clear result array
- if (!_karatsuba_rec_fnt(w, a+m, b, w+lt, la-m, lb)) { // ah*b
- return 0; /* GCOV_UNLIKELY */
- }
- }
- _mpd_baseaddto(c+m, w, (la-m)+lb); // add ah*b*B**m
- lt = m + m + 1; // space needed for the result array
- mpd_uint_zero(w, lt); // clear result array
- if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, lb)) { // al*b
- return 0; /* GCOV_UNLIKELY */
- }
- _mpd_baseaddto(c, w, m+lb); // add al*b
- return 1;
- }
- /* la >= lb > m */
- memcpy(w, a, m * sizeof *w);
- w[m] = 0;
- _mpd_baseaddto(w, a+m, la-m);
- memcpy(w+(m+1), b, m * sizeof *w);
- w[m+1+m] = 0;
- _mpd_baseaddto(w+(m+1), b+m, lb-m);
- if (!_karatsuba_rec_fnt(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1)) {
- return 0; /* GCOV_UNLIKELY */
- }
- lt = (la-m) + (la-m) + 1;
- mpd_uint_zero(w, lt);
- if (!_karatsuba_rec_fnt(w, a+m, b+m, w+lt, la-m, lb-m)) {
- return 0; /* GCOV_UNLIKELY */
- }
- _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m));
- _mpd_basesubfrom(c+m, w, (la-m) + (lb-m));
- lt = m + m + 1;
- mpd_uint_zero(w, lt);
- if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, m)) {
- return 0; /* GCOV_UNLIKELY */
- }
- _mpd_baseaddto(c, w, m+m);
- _mpd_basesubfrom(c+m, w, m+m);
- return 1;
- }
- /*
- * Multiply u and v, using Karatsuba multiplication with the FNT as the
- * base case. Returns a pointer to the result or NULL in case of failure
- * (malloc error). Conditions: ulen >= vlen, ulen >= 4.
- */
- mpd_uint_t *
- _mpd_kmul_fnt(const mpd_uint_t *u, const mpd_uint_t *v,
- mpd_size_t ulen, mpd_size_t vlen,
- mpd_size_t *rsize)
- {
- mpd_uint_t *result = NULL, *w = NULL;
- mpd_size_t m;
- assert(ulen >= 4);
- assert(ulen >= vlen);
- *rsize = _kmul_resultsize(ulen, vlen);
- if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) {
- return NULL;
- }
- m = _kmul_worksize(ulen, 3*(MPD_MAXTRANSFORM_2N/2));
- if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) {
- mpd_free(result); /* GCOV_UNLIKELY */
- return NULL; /* GCOV_UNLIKELY */
- }
- if (!_karatsuba_rec_fnt(result, u, v, w, ulen, vlen)) {
- mpd_free(result);
- result = NULL;
- }
- if (w) mpd_free(w);
- return result;
- }
- /* Deal with the special cases of multiplying infinities. */
- static void
- _mpd_qmul_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status)
- {
- if (mpd_isinfinite(a)) {
- if (mpd_iszero(b)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- else {
- mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
- }
- return;
- }
- assert(mpd_isinfinite(b));
- if (mpd_iszero(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- else {
- mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
- }
- }
- /*
- * Internal function: Multiply a and b. _mpd_qmul deals with specials but
- * does NOT finalize the result. This is for use in mpd_fma().
- */
- static inline void
- _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_t *big = (mpd_t *)a, *small = (mpd_t *)b;
- mpd_uint_t *rdata = NULL;
- mpd_uint_t rbuf[MPD_MINALLOC_MAX];
- mpd_size_t rsize, i;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- _mpd_qmul_inf(result, a, b, status);
- return;
- }
- if (small->len > big->len) {
- _mpd_ptrswap(&big, &small);
- }
- rsize = big->len + small->len;
- if (big->len == 1) {
- _mpd_singlemul(result->data, big->data[0], small->data[0]);
- goto finish;
- }
- if (rsize <= (mpd_size_t)MPD_MINALLOC_MAX) {
- if (big->len == 2) {
- _mpd_mul_2_le2(rbuf, big->data, small->data, small->len);
- }
- else {
- mpd_uint_zero(rbuf, rsize);
- if (small->len == 1) {
- _mpd_shortmul(rbuf, big->data, big->len, small->data[0]);
- }
- else {
- _mpd_basemul(rbuf, small->data, big->data, small->len, big->len);
- }
- }
- if (!mpd_qresize(result, rsize, status)) {
- return;
- }
- for(i = 0; i < rsize; i++) {
- result->data[i] = rbuf[i];
- }
- goto finish;
- }
- if (small->len == 1) {
- if ((rdata = mpd_calloc(rsize, sizeof *rdata)) == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- _mpd_shortmul(rdata, big->data, big->len, small->data[0]);
- }
- else if (rsize <= 1024) {
- rdata = _mpd_kmul(big->data, small->data, big->len, small->len, &rsize);
- if (rdata == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- }
- else if (rsize <= 3*MPD_MAXTRANSFORM_2N) {
- rdata = _mpd_fntmul(big->data, small->data, big->len, small->len, &rsize);
- if (rdata == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- }
- else {
- rdata = _mpd_kmul_fnt(big->data, small->data, big->len, small->len, &rsize);
- if (rdata == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status); /* GCOV_UNLIKELY */
- return; /* GCOV_UNLIKELY */
- }
- }
- if (mpd_isdynamic_data(result)) {
- mpd_free(result->data);
- }
- result->data = rdata;
- result->alloc = rsize;
- mpd_set_dynamic_data(result);
- finish:
- mpd_set_flags(result, mpd_sign(a)^mpd_sign(b));
- result->exp = big->exp + small->exp;
- result->len = _mpd_real_size(result->data, rsize);
- /* resize to smaller cannot fail */
- mpd_qresize(result, result->len, status);
- mpd_setdigits(result);
- }
- /* Multiply a and b. */
- void
- mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- _mpd_qmul(result, a, b, ctx, status);
- mpd_qfinalize(result, ctx, status);
- }
- /* Multiply decimal and mpd_ssize_t. */
- void
- mpd_qmul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_ssize(&bb, b, &maxcontext, status);
- mpd_qmul(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- /* Multiply decimal and mpd_uint_t. */
- void
- mpd_qmul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(bb,0,0,0,0);
- mpd_maxcontext(&maxcontext);
- mpd_qsset_uint(&bb, b, &maxcontext, status);
- mpd_qmul(result, a, &bb, ctx, status);
- mpd_del(&bb);
- }
- void
- mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qmul_ssize(result, a, b, ctx, status);
- }
- void
- mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qmul_uint(result, a, b, ctx, status);
- }
- #ifdef CONFIG_64
- void
- mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qmul_ssize(result, a, b, ctx, status);
- }
- void
- mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_qmul_uint(result, a, b, ctx, status);
- }
- #endif
- /* Like the minus operator. */
- void
- mpd_qminus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- }
- if (mpd_iszero(a) && ctx->round != MPD_ROUND_FLOOR) {
- mpd_qcopy_abs(result, a, status);
- }
- else {
- mpd_qcopy_negate(result, a, status);
- }
- mpd_qfinalize(result, ctx, status);
- }
- /* Like the plus operator. */
- void
- mpd_qplus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- }
- if (mpd_iszero(a) && ctx->round != MPD_ROUND_FLOOR) {
- mpd_qcopy_abs(result, a, status);
- }
- else {
- mpd_qcopy(result, a, status);
- }
- mpd_qfinalize(result, ctx, status);
- }
- /* The largest representable number that is smaller than the operand. */
- void
- mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx; /* function context */
- MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1);
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a)) {
- if (mpd_isnegative(a)) {
- mpd_qcopy(result, a, status);
- return;
- }
- else {
- mpd_clear_flags(result);
- mpd_qmaxcoeff(result, ctx, status);
- if (mpd_isnan(result)) {
- return;
- }
- result->exp = ctx->emax - ctx->prec + 1;
- return;
- }
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- mpd_workcontext(&workctx, ctx);
- workctx.round = MPD_ROUND_FLOOR;
- if (!mpd_qcopy(result, a, status)) {
- return;
- }
- mpd_qfinalize(result, &workctx, &workctx.status);
- if (workctx.status&(MPD_Inexact|MPD_Errors)) {
- *status |= (workctx.status&MPD_Errors);
- return;
- }
- workctx.status = 0;
- mpd_qsub(result, a, &tiny, &workctx, &workctx.status);
- *status |= (workctx.status&MPD_Errors);
- }
- /* The smallest representable number that is larger than the operand. */
- void
- mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t workctx;
- MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1);
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a)) {
- if (mpd_ispositive(a)) {
- mpd_qcopy(result, a, status);
- }
- else {
- mpd_clear_flags(result);
- mpd_qmaxcoeff(result, ctx, status);
- if (mpd_isnan(result)) {
- return;
- }
- mpd_set_flags(result, MPD_NEG);
- result->exp = mpd_etop(ctx);
- }
- return;
- }
- }
- mpd_workcontext(&workctx, ctx);
- workctx.round = MPD_ROUND_CEILING;
- if (!mpd_qcopy(result, a, status)) {
- return;
- }
- mpd_qfinalize(result, &workctx, &workctx.status);
- if (workctx.status & (MPD_Inexact|MPD_Errors)) {
- *status |= (workctx.status&MPD_Errors);
- return;
- }
- workctx.status = 0;
- mpd_qadd(result, a, &tiny, &workctx, &workctx.status);
- *status |= (workctx.status&MPD_Errors);
- }
- /*
- * The number closest to the first operand that is in the direction towards
- * the second operand.
- */
- void
- mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- int c;
- if (mpd_isnan(a) || mpd_isnan(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status))
- return;
- }
- c = _mpd_cmp(a, b);
- if (c == 0) {
- mpd_qcopy_sign(result, a, b, status);
- return;
- }
- if (c < 0) {
- mpd_qnext_plus(result, a, ctx, status);
- }
- else {
- mpd_qnext_minus(result, a, ctx, status);
- }
- if (mpd_isinfinite(result)) {
- *status |= (MPD_Overflow|MPD_Rounded|MPD_Inexact);
- }
- else if (mpd_adjexp(result) < ctx->emin) {
- *status |= (MPD_Underflow|MPD_Subnormal|MPD_Rounded|MPD_Inexact);
- if (mpd_iszero(result)) {
- *status |= MPD_Clamped;
- }
- }
- }
- /*
- * Internal function: Integer power with mpd_uint_t exponent, base is modified!
- * Function can fail with MPD_Malloc_error.
- */
- static inline void
- _mpd_qpow_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp, uint8_t resultsign,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_uint_t n;
- if (exp == 0) {
- _settriple(result, resultsign, 1, 0); /* GCOV_NOT_REACHED */
- return; /* GCOV_NOT_REACHED */
- }
- if (!mpd_qcopy(result, base, status)) {
- return;
- }
- n = mpd_bits[mpd_bsr(exp)];
- while (n >>= 1) {
- mpd_qmul(result, result, result, ctx, &workstatus);
- if (exp & n) {
- mpd_qmul(result, result, base, ctx, &workstatus);
- }
- if (workstatus & (MPD_Overflow|MPD_Clamped)) {
- break;
- }
- }
- *status |= workstatus;
- mpd_set_sign(result, resultsign);
- }
- /*
- * Internal function: Integer power with mpd_t exponent, tbase and texp
- * are modified!! Function can fail with MPD_Malloc_error.
- */
- static inline void
- _mpd_qpow_mpd(mpd_t *result, mpd_t *tbase, mpd_t *texp, uint8_t resultsign,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_context_t maxctx;
- MPD_NEW_CONST(two,0,0,1,1,1,2);
- mpd_maxcontext(&maxctx);
- /* resize to smaller cannot fail */
- mpd_qcopy(result, &one, status);
- while (!mpd_iszero(texp)) {
- if (mpd_isodd(texp)) {
- mpd_qmul(result, result, tbase, ctx, &workstatus);
- *status |= workstatus;
- if (workstatus & (MPD_Overflow|MPD_Clamped)) {
- break;
- }
- }
- mpd_qmul(tbase, tbase, tbase, ctx, &workstatus);
- mpd_qdivint(texp, texp, &two, &maxctx, &workstatus);
- if (mpd_isnan(tbase) || mpd_isnan(texp)) {
- mpd_seterror(result, workstatus&MPD_Errors, status);
- return;
- }
- }
- mpd_set_sign(result, resultsign);
- }
- /*
- * The power function for integer exponents.
- */
- static void
- _mpd_qpow_int(mpd_t *result, const mpd_t *base, const mpd_t *exp,
- uint8_t resultsign,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t workctx;
- MPD_NEW_STATIC(tbase,0,0,0,0);
- MPD_NEW_STATIC(texp,0,0,0,0);
- mpd_ssize_t n;
- mpd_workcontext(&workctx, ctx);
- workctx.prec += (exp->digits + exp->exp + 2);
- workctx.round = MPD_ROUND_HALF_EVEN;
- workctx.clamp = 0;
- if (mpd_isnegative(exp)) {
- mpd_qdiv(&tbase, &one, base, &workctx, status);
- if (*status&MPD_Errors) {
- mpd_setspecial(result, MPD_POS, MPD_NAN);
- goto finish;
- }
- }
- else {
- if (!mpd_qcopy(&tbase, base, status)) {
- mpd_setspecial(result, MPD_POS, MPD_NAN);
- goto finish;
- }
- }
- n = mpd_qabs_uint(exp, &workctx.status);
- if (workctx.status&MPD_Invalid_operation) {
- if (!mpd_qcopy(&texp, exp, status)) {
- mpd_setspecial(result, MPD_POS, MPD_NAN); /* GCOV_UNLIKELY */
- goto finish; /* GCOV_UNLIKELY */
- }
- _mpd_qpow_mpd(result, &tbase, &texp, resultsign, &workctx, status);
- }
- else {
- _mpd_qpow_uint(result, &tbase, n, resultsign, &workctx, status);
- }
- if (mpd_isinfinite(result)) {
- /* for ROUND_DOWN, ROUND_FLOOR, etc. */
- _settriple(result, resultsign, 1, MPD_EXP_INF);
- }
- finish:
- mpd_del(&tbase);
- mpd_del(&texp);
- mpd_qfinalize(result, ctx, status);
- }
- /*
- * This is an internal function that does not check for NaNs.
- */
- static int
- _qcheck_pow_one_inf(mpd_t *result, const mpd_t *base, uint8_t resultsign,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_ssize_t shift;
- int cmp;
- if ((cmp = _mpd_cmp(base, &one)) == 0) {
- shift = ctx->prec-1;
- mpd_qshiftl(result, &one, shift, status);
- result->exp = -shift;
- mpd_set_flags(result, resultsign);
- *status |= (MPD_Inexact|MPD_Rounded);
- }
- return cmp;
- }
- /*
- * If base equals one, calculate the correct power of one result.
- * Otherwise, result is undefined. Return the value of the comparison
- * against 1.
- *
- * This is an internal function that does not check for specials.
- */
- static int
- _qcheck_pow_one(mpd_t *result, const mpd_t *base, const mpd_t *exp,
- uint8_t resultsign,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_ssize_t shift;
- int cmp;
- if ((cmp = _mpd_cmp_abs(base, &one)) == 0) {
- if (_mpd_isint(exp)) {
- if (mpd_isnegative(exp)) {
- _settriple(result, resultsign, 1, 0);
- return 0;
- }
- /* 1.000**3 = 1.000000000 */
- mpd_qmul_ssize(result, exp, -base->exp, ctx, &workstatus);
- if (workstatus&MPD_Errors) {
- *status |= (workstatus&MPD_Errors);
- return 0;
- }
- /* digits-1 after exponentiation */
- shift = mpd_qget_ssize(result, &workstatus);
- /* shift is MPD_SSIZE_MAX if result is too large */
- if (shift > ctx->prec-1) {
- shift = ctx->prec-1;
- *status |= MPD_Rounded;
- }
- }
- else if (mpd_ispositive(base)) {
- shift = ctx->prec-1;
- *status |= (MPD_Inexact|MPD_Rounded);
- }
- else {
- return -2; /* GCOV_NOT_REACHED */
- }
- if (!mpd_qshiftl(result, &one, shift, status)) {
- return 0;
- }
- result->exp = -shift;
- mpd_set_flags(result, resultsign);
- }
- return cmp;
- }
- /*
- * Detect certain over/underflow of x**y.
- * ACL2 proof: pow_bounds.lisp.
- *
- * Symbols:
- *
- * e: EXP_INF or EXP_CLAMP
- * x: base
- * y: exponent
- *
- * omega(e) = log10(abs(e))
- * zeta(x) = log10(abs(log10(x)))
- * theta(y) = log10(abs(y))
- *
- * Upper and lower bounds:
- *
- * ub_omega(e) = ceil(log10(abs(e)))
- * lb_theta(y) = floor(log10(abs(y)))
- *
- * | floor(log10(floor(abs(log10(x))))) if x < 1/10 or x >= 10
- * lb_zeta(x) = | floor(log10(abs(x-1)/10)) if 1/10 <= x < 1
- * | floor(log10(abs((x-1)/100))) if 1 < x < 10
- *
- * ub_omega(e) and lb_theta(y) are obviously upper and lower bounds
- * for omega(e) and theta(y).
- *
- * lb_zeta is a lower bound for zeta(x):
- *
- * x < 1/10 or x >= 10:
- *
- * abs(log10(x)) >= 1, so the outer log10 is well defined. Since log10
- * is strictly increasing, the end result is a lower bound.
- *
- * 1/10 <= x < 1:
- *
- * We use: log10(x) <= (x-1)/log(10)
- * abs(log10(x)) >= abs(x-1)/log(10)
- * abs(log10(x)) >= abs(x-1)/10
- *
- * 1 < x < 10:
- *
- * We use: (x-1)/(x*log(10)) < log10(x)
- * abs((x-1)/100) < abs(log10(x))
- *
- * XXX: abs((x-1)/10) would work, need ACL2 proof.
- *
- *
- * Let (0 < x < 1 and y < 0) or (x > 1 and y > 0). (H1)
- * Let ub_omega(exp_inf) < lb_zeta(x) + lb_theta(y) (H2)
- *
- * Then:
- * log10(abs(exp_inf)) < log10(abs(log10(x))) + log10(abs(y)). (1)
- * exp_inf < log10(x) * y (2)
- * 10**exp_inf < x**y (3)
- *
- * Let (0 < x < 1 and y > 0) or (x > 1 and y < 0). (H3)
- * Let ub_omega(exp_clamp) < lb_zeta(x) + lb_theta(y) (H4)
- *
- * Then:
- * log10(abs(exp_clamp)) < log10(abs(log10(x))) + log10(abs(y)). (4)
- * log10(x) * y < exp_clamp (5)
- * x**y < 10**exp_clamp (6)
- *
- */
- static mpd_ssize_t
- _lower_bound_zeta(const mpd_t *x, uint32_t *status)
- {
- mpd_context_t maxctx;
- MPD_NEW_STATIC(scratch,0,0,0,0);
- mpd_ssize_t t, u;
- t = mpd_adjexp(x);
- if (t > 0) {
- /* x >= 10 -> floor(log10(floor(abs(log10(x))))) */
- return mpd_exp_digits(t) - 1;
- }
- else if (t < -1) {
- /* x < 1/10 -> floor(log10(floor(abs(log10(x))))) */
- return mpd_exp_digits(t+1) - 1;
- }
- else {
- mpd_maxcontext(&maxctx);
- mpd_qsub(&scratch, x, &one, &maxctx, status);
- if (mpd_isspecial(&scratch)) {
- mpd_del(&scratch);
- return MPD_SSIZE_MAX;
- }
- u = mpd_adjexp(&scratch);
- mpd_del(&scratch);
- /* t == -1, 1/10 <= x < 1 -> floor(log10(abs(x-1)/10))
- * t == 0, 1 < x < 10 -> floor(log10(abs(x-1)/100)) */
- return (t == 0) ? u-2 : u-1;
- }
- }
- /*
- * Detect cases of certain overflow/underflow in the power function.
- * Assumptions: x != 1, y != 0. The proof above is for positive x.
- * If x is negative and y is an odd integer, x**y == -(abs(x)**y),
- * so the analysis does not change.
- */
- static int
- _qcheck_pow_bounds(mpd_t *result, const mpd_t *x, const mpd_t *y,
- uint8_t resultsign,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_SHARED(abs_x, x);
- mpd_ssize_t ub_omega, lb_zeta, lb_theta;
- uint8_t sign;
- mpd_set_positive(&abs_x);
- lb_theta = mpd_adjexp(y);
- lb_zeta = _lower_bound_zeta(&abs_x, status);
- if (lb_zeta == MPD_SSIZE_MAX) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return 1;
- }
- sign = (mpd_adjexp(&abs_x) < 0) ^ mpd_sign(y);
- if (sign == 0) {
- /* (0 < |x| < 1 and y < 0) or (|x| > 1 and y > 0) */
- ub_omega = mpd_exp_digits(ctx->emax);
- if (ub_omega < lb_zeta + lb_theta) {
- _settriple(result, resultsign, 1, MPD_EXP_INF);
- mpd_qfinalize(result, ctx, status);
- return 1;
- }
- }
- else {
- /* (0 < |x| < 1 and y > 0) or (|x| > 1 and y < 0). */
- ub_omega = mpd_exp_digits(mpd_etiny(ctx));
- if (ub_omega < lb_zeta + lb_theta) {
- _settriple(result, resultsign, 1, mpd_etiny(ctx)-1);
- mpd_qfinalize(result, ctx, status);
- return 1;
- }
- }
- return 0;
- }
- /*
- * TODO: Implement algorithm for computing exact powers from decimal.py.
- * In order to prevent infinite loops, this has to be called before
- * using Ziv's strategy for correct rounding.
- */
- /*
- static int
- _mpd_qpow_exact(mpd_t *result, const mpd_t *base, const mpd_t *exp,
- const mpd_context_t *ctx, uint32_t *status)
- {
- return 0;
- }
- */
- /* The power function for real exponents */
- static void
- _mpd_qpow_real(mpd_t *result, const mpd_t *base, const mpd_t *exp,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t workctx;
- MPD_NEW_STATIC(texp,0,0,0,0);
- if (!mpd_qcopy(&texp, exp, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- mpd_maxcontext(&workctx);
- workctx.prec = (base->digits > ctx->prec) ? base->digits : ctx->prec;
- workctx.prec += (4 + MPD_EXPDIGITS);
- workctx.round = MPD_ROUND_HALF_EVEN;
- workctx.allcr = ctx->allcr;
- mpd_qln(result, base, &workctx, &workctx.status);
- mpd_qmul(result, result, &texp, &workctx, &workctx.status);
- mpd_qexp(result, result, &workctx, status);
- mpd_del(&texp);
- *status |= (workctx.status&MPD_Errors);
- *status |= (MPD_Inexact|MPD_Rounded);
- }
- /* The power function: base**exp */
- void
- mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint8_t resultsign = 0;
- int intexp = 0;
- int cmp;
- if (mpd_isspecial(base) || mpd_isspecial(exp)) {
- if (mpd_qcheck_nans(result, base, exp, ctx, status)) {
- return;
- }
- }
- if (mpd_isinteger(exp)) {
- intexp = 1;
- resultsign = mpd_isnegative(base) && mpd_isodd(exp);
- }
- if (mpd_iszero(base)) {
- if (mpd_iszero(exp)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- }
- else if (mpd_isnegative(exp)) {
- mpd_setspecial(result, resultsign, MPD_INF);
- }
- else {
- _settriple(result, resultsign, 0, 0);
- }
- return;
- }
- if (mpd_isnegative(base)) {
- if (!intexp || mpd_isinfinite(exp)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- }
- if (mpd_isinfinite(exp)) {
- /* power of one */
- cmp = _qcheck_pow_one_inf(result, base, resultsign, ctx, status);
- if (cmp == 0) {
- return;
- }
- else {
- cmp *= mpd_arith_sign(exp);
- if (cmp < 0) {
- _settriple(result, resultsign, 0, 0);
- }
- else {
- mpd_setspecial(result, resultsign, MPD_INF);
- }
- }
- return;
- }
- if (mpd_isinfinite(base)) {
- if (mpd_iszero(exp)) {
- _settriple(result, resultsign, 1, 0);
- }
- else if (mpd_isnegative(exp)) {
- _settriple(result, resultsign, 0, 0);
- }
- else {
- mpd_setspecial(result, resultsign, MPD_INF);
- }
- return;
- }
- if (mpd_iszero(exp)) {
- _settriple(result, resultsign, 1, 0);
- return;
- }
- if (_qcheck_pow_one(result, base, exp, resultsign, ctx, status) == 0) {
- return;
- }
- if (_qcheck_pow_bounds(result, base, exp, resultsign, ctx, status)) {
- return;
- }
- if (intexp) {
- _mpd_qpow_int(result, base, exp, resultsign, ctx, status);
- }
- else {
- _mpd_qpow_real(result, base, exp, ctx, status);
- if (!mpd_isspecial(result) && _mpd_cmp(result, &one) == 0) {
- mpd_ssize_t shift = ctx->prec-1;
- mpd_qshiftl(result, &one, shift, status);
- result->exp = -shift;
- }
- if (mpd_isinfinite(result)) {
- /* for ROUND_DOWN, ROUND_FLOOR, etc. */
- _settriple(result, MPD_POS, 1, MPD_EXP_INF);
- }
- mpd_qfinalize(result, ctx, status);
- }
- }
- /*
- * Internal function: Integer powmod with mpd_uint_t exponent, base is modified!
- * Function can fail with MPD_Malloc_error.
- */
- static inline void
- _mpd_qpowmod_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp,
- mpd_t *mod, uint32_t *status)
- {
- mpd_context_t maxcontext;
- mpd_maxcontext(&maxcontext);
- /* resize to smaller cannot fail */
- mpd_qcopy(result, &one, status);
- while (exp > 0) {
- if (exp & 1) {
- mpd_qmul(result, result, base, &maxcontext, status);
- mpd_qrem(result, result, mod, &maxcontext, status);
- }
- mpd_qmul(base, base, base, &maxcontext, status);
- mpd_qrem(base, base, mod, &maxcontext, status);
- exp >>= 1;
- }
- }
- /* The powmod function: (base**exp) % mod */
- void
- mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp,
- const mpd_t *mod,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxcontext;
- MPD_NEW_STATIC(tbase,0,0,0,0);
- MPD_NEW_STATIC(texp,0,0,0,0);
- MPD_NEW_STATIC(tmod,0,0,0,0);
- MPD_NEW_STATIC(tmp,0,0,0,0);
- MPD_NEW_CONST(two,0,0,1,1,1,2);
- mpd_ssize_t tbase_exp, texp_exp;
- mpd_ssize_t i;
- mpd_t t;
- mpd_uint_t r;
- uint8_t sign;
- if (mpd_isspecial(base) || mpd_isspecial(exp) || mpd_isspecial(mod)) {
- if (mpd_qcheck_3nans(result, base, exp, mod, ctx, status)) {
- return;
- }
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (!_mpd_isint(base) || !_mpd_isint(exp) || !_mpd_isint(mod)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_iszerocoeff(mod)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- sign = (mpd_isnegative(base)) && (mpd_isodd(exp));
- if (mpd_iszerocoeff(exp)) {
- if (mpd_iszerocoeff(base)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- r = (_mpd_cmp_abs(mod, &one)==0) ? 0 : 1;
- _settriple(result, sign, r, 0);
- return;
- }
- if (mpd_isnegative(exp)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_iszerocoeff(base)) {
- _settriple(result, sign, 0, 0);
- return;
- }
- if (mod->digits+mod->exp > ctx->prec) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (!mpd_qcopy(&tmod, mod, status)) {
- goto mpd_errors;
- }
- mpd_set_positive(&tmod);
- mpd_maxcontext(&maxcontext);
- mpd_qround_to_int(&tbase, base, &maxcontext, status);
- mpd_qround_to_int(&texp, exp, &maxcontext, status);
- mpd_qround_to_int(&tmod, &tmod, &maxcontext, status);
- tbase_exp = tbase.exp;
- tbase.exp = 0;
- texp_exp = texp.exp;
- texp.exp = 0;
- /* base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo */
- mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status);
- _settriple(result, MPD_POS, 1, tbase_exp);
- mpd_qrem(result, result, &tmod, &maxcontext, status);
- mpd_qmul(&tbase, &tbase, result, &maxcontext, status);
- mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status);
- if (mpd_isspecial(&tbase) ||
- mpd_isspecial(&texp) ||
- mpd_isspecial(&tmod)) {
- goto mpd_errors;
- }
- for (i = 0; i < texp_exp; i++) {
- _mpd_qpowmod_uint(&tmp, &tbase, 10, &tmod, status);
- t = tmp;
- tmp = tbase;
- tbase = t;
- }
- if (mpd_isspecial(&tbase)) {
- goto mpd_errors; /* GCOV_UNLIKELY */
- }
- /* resize to smaller cannot fail */
- mpd_qcopy(result, &one, status);
- while (mpd_isfinite(&texp) && !mpd_iszero(&texp)) {
- if (mpd_isodd(&texp)) {
- mpd_qmul(result, result, &tbase, &maxcontext, status);
- mpd_qrem(result, result, &tmod, &maxcontext, status);
- }
- mpd_qmul(&tbase, &tbase, &tbase, &maxcontext, status);
- mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status);
- mpd_qdivint(&texp, &texp, &two, &maxcontext, status);
- }
- if (mpd_isspecial(&texp) || mpd_isspecial(&tbase) ||
- mpd_isspecial(&tmod) || mpd_isspecial(result)) {
- /* MPD_Malloc_error */
- goto mpd_errors;
- }
- else {
- mpd_set_sign(result, sign);
- }
- out:
- mpd_del(&tbase);
- mpd_del(&texp);
- mpd_del(&tmod);
- mpd_del(&tmp);
- mpd_qfinalize(result, ctx, status);
- return;
- mpd_errors:
- mpd_setspecial(result, MPD_POS, MPD_NAN);
- goto out;
- }
- void
- mpd_qquantize(mpd_t *result, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_ssize_t b_exp = b->exp;
- mpd_ssize_t expdiff, shift;
- mpd_uint_t rnd;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(result, a, b, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a) && mpd_isinfinite(b)) {
- mpd_qcopy(result, a, status);
- return;
- }
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (b->exp > ctx->emax || b->exp < mpd_etiny(ctx)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_iszero(a)) {
- _settriple(result, mpd_sign(a), 0, b->exp);
- mpd_qfinalize(result, ctx, status);
- return;
- }
- expdiff = a->exp - b->exp;
- if (a->digits + expdiff > ctx->prec) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (expdiff >= 0) {
- shift = expdiff;
- if (!mpd_qshiftl(result, a, shift, status)) {
- return;
- }
- result->exp = b_exp;
- }
- else {
- /* At this point expdiff < 0 and a->digits+expdiff <= prec,
- * so the shift before an increment will fit in prec. */
- shift = -expdiff;
- rnd = mpd_qshiftr(result, a, shift, status);
- if (rnd == MPD_UINT_MAX) {
- return;
- }
- result->exp = b_exp;
- if (!_mpd_apply_round_fit(result, rnd, ctx, status)) {
- return;
- }
- workstatus |= MPD_Rounded;
- if (rnd) {
- workstatus |= MPD_Inexact;
- }
- }
- if (mpd_adjexp(result) > ctx->emax ||
- mpd_adjexp(result) < mpd_etiny(ctx)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- *status |= workstatus;
- mpd_qfinalize(result, ctx, status);
- }
- void
- mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_ssize_t shift, maxexp, maxshift;
- uint8_t sign_a = mpd_sign(a);
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- mpd_qcopy(result, a, status);
- return;
- }
- if (!mpd_qcopy(result, a, status)) {
- return;
- }
- mpd_qfinalize(result, ctx, status);
- if (mpd_isspecial(result)) {
- return;
- }
- if (mpd_iszero(result)) {
- _settriple(result, sign_a, 0, 0);
- return;
- }
- shift = mpd_trail_zeros(result);
- maxexp = (ctx->clamp) ? mpd_etop(ctx) : ctx->emax;
- /* After the finalizing above result->exp <= maxexp. */
- maxshift = maxexp - result->exp;
- shift = (shift > maxshift) ? maxshift : shift;
- mpd_qshiftr_inplace(result, shift);
- result->exp += shift;
- }
- void
- mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx,
- uint32_t *status)
- {
- MPD_NEW_STATIC(q,0,0,0,0);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(r, a, b, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_seterror(r, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(b)) {
- mpd_qcopy(r, a, status);
- mpd_qfinalize(r, ctx, status);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(r, MPD_Division_undefined, status);
- }
- else {
- mpd_seterror(r, MPD_Invalid_operation, status);
- }
- return;
- }
- _mpd_qdivmod(&q, r, a, b, ctx, status);
- mpd_del(&q);
- mpd_qfinalize(r, ctx, status);
- }
- void
- mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t workctx;
- MPD_NEW_STATIC(btmp,0,0,0,0);
- MPD_NEW_STATIC(q,0,0,0,0);
- mpd_ssize_t expdiff, floordigits;
- int cmp, isodd, allnine;
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(r, a, b, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_seterror(r, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(b)) {
- mpd_qcopy(r, a, status);
- mpd_qfinalize(r, ctx, status);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(r, MPD_Division_undefined, status);
- }
- else {
- mpd_seterror(r, MPD_Invalid_operation, status);
- }
- return;
- }
- if (r == b) {
- if (!mpd_qcopy(&btmp, b, status)) {
- mpd_seterror(r, MPD_Malloc_error, status);
- return;
- }
- b = &btmp;
- }
- workctx = *ctx;
- workctx.prec = a->digits;
- workctx.prec = (workctx.prec > ctx->prec) ? workctx.prec : ctx->prec;
- _mpd_qdivmod(&q, r, a, b, &workctx, status);
- if (mpd_isnan(&q) || mpd_isnan(r) || q.digits > ctx->prec) {
- mpd_seterror(r, MPD_Division_impossible, status);
- goto finish;
- }
- if (mpd_iszerocoeff(r)) {
- goto finish;
- }
- /* Deal with cases like rmnx078:
- * remaindernear 999999999.5 1 -> NaN Division_impossible */
- expdiff = mpd_adjexp(b) - mpd_adjexp(r);
- if (-1 <= expdiff && expdiff <= 1) {
- mpd_qtrunc(&q, &q, &workctx, &workctx.status);
- allnine = mpd_coeff_isallnine(&q);
- floordigits = q.digits;
- isodd = mpd_isodd(&q);
- mpd_maxcontext(&workctx);
- if (mpd_sign(a) == mpd_sign(b)) {
- _mpd_qsub(&q, r, b, &workctx, &workctx.status);
- if (workctx.status&MPD_Errors) {
- mpd_seterror(r, workctx.status&MPD_Errors, status);
- goto finish;
- }
- }
- else {
- _mpd_qadd(&q, r, b, &workctx, &workctx.status);
- if (workctx.status&MPD_Errors) {
- mpd_seterror(r, workctx.status&MPD_Errors, status);
- goto finish;
- }
- }
- cmp = mpd_cmp_total_mag(&q, r);
- if (cmp < 0 || (cmp == 0 && isodd)) {
- if (allnine && floordigits == ctx->prec) {
- mpd_seterror(r, MPD_Division_impossible, status);
- goto finish;
- }
- mpd_qcopy(r, &q, status);
- *status &= ~MPD_Rounded;
- }
- }
- finish:
- mpd_del(&btmp);
- mpd_del(&q);
- mpd_qfinalize(r, ctx, status);
- }
- /*
- * Rescale a number so that it has exponent 'exp'. Does not regard
- * context precision, emax, emin, but uses the rounding mode.
- * Special numbers are quietly copied.
- */
- void
- mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_ssize_t expdiff, shift;
- mpd_uint_t rnd;
- if (mpd_isspecial(a)) {
- mpd_qcopy(result, a, status);
- return;
- }
- if (exp > MPD_MAX_EMAX || exp < MPD_MIN_ETINY) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_iszero(a)) {
- _settriple(result, mpd_sign(a), 0, exp);
- return;
- }
- expdiff = a->exp - exp;
- if (expdiff >= 0) {
- shift = expdiff;
- if (!mpd_qshiftl(result, a, shift, status)) {
- return;
- }
- result->exp = exp;
- }
- else {
- shift = -expdiff;
- rnd = mpd_qshiftr(result, a, shift, status);
- if (rnd == MPD_UINT_MAX) {
- return;
- }
- result->exp = exp;
- _mpd_apply_round_excess(result, rnd, ctx, status);
- *status |= MPD_Rounded;
- if (rnd) {
- *status |= MPD_Inexact;
- }
- }
- if (mpd_issubnormal(result, ctx)) {
- *status |= MPD_Subnormal;
- }
- }
- /* Round to an integer according to 'action' and ctx->round. */
- enum {TO_INT_EXACT, TO_INT_SILENT, TO_INT_TRUNC, TO_INT_FLOOR, TO_INT_CEIL};
- static void
- _mpd_qround_to_integral(int action, mpd_t *result, const mpd_t *a,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_uint_t rnd;
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- mpd_qcopy(result, a, status);
- return;
- }
- if (a->exp >= 0) {
- mpd_qcopy(result, a, status);
- return;
- }
- if (mpd_iszerocoeff(a)) {
- _settriple(result, mpd_sign(a), 0, 0);
- return;
- }
- rnd = mpd_qshiftr(result, a, -a->exp, status);
- if (rnd == MPD_UINT_MAX) {
- return;
- }
- result->exp = 0;
- if (action == TO_INT_EXACT || action == TO_INT_SILENT) {
- _mpd_apply_round_excess(result, rnd, ctx, status);
- if (action == TO_INT_EXACT) {
- *status |= MPD_Rounded;
- if (rnd) {
- *status |= MPD_Inexact;
- }
- }
- }
- else if (action == TO_INT_FLOOR) {
- if (rnd && mpd_isnegative(result)) {
- _mpd_qsub(result, result, &one, ctx, status);
- }
- }
- else if (action == TO_INT_CEIL) {
- if (rnd && mpd_ispositive(result)) {
- _mpd_qadd(result, result, &one, ctx, status);
- }
- }
- }
- void
- mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- (void)_mpd_qround_to_integral(TO_INT_EXACT, result, a, ctx, status);
- }
- void
- mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, ctx, status);
- }
- void
- mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- (void)_mpd_qround_to_integral(TO_INT_TRUNC, result, a, ctx, status);
- }
- void
- mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- (void)_mpd_qround_to_integral(TO_INT_FLOOR, result, a, ctx, status);
- }
- void
- mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- (void)_mpd_qround_to_integral(TO_INT_CEIL, result, a, ctx, status);
- }
- int
- mpd_same_quantum(const mpd_t *a, const mpd_t *b)
- {
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- return ((mpd_isnan(a) && mpd_isnan(b)) ||
- (mpd_isinfinite(a) && mpd_isinfinite(b)));
- }
- return a->exp == b->exp;
- }
- /* Schedule the increase in precision for the Newton iteration. */
- static inline int
- recpr_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2],
- mpd_ssize_t maxprec, mpd_ssize_t initprec)
- {
- mpd_ssize_t k;
- int i;
- assert(maxprec > 0 && initprec > 0);
- if (maxprec <= initprec) return -1;
- i = 0; k = maxprec;
- do {
- k = (k+1) / 2;
- klist[i++] = k;
- } while (k > initprec);
- return i-1;
- }
- /*
- * Initial approximation for the reciprocal. Result has MPD_RDIGITS-2
- * significant digits.
- */
- static void
- _mpd_qreciprocal_approx(mpd_t *z, const mpd_t *v, uint32_t *status)
- {
- mpd_uint_t p10data[2] = {0, mpd_pow10[MPD_RDIGITS-2]}; /* 10**(2*MPD_RDIGITS-2) */
- mpd_uint_t dummy, word;
- int n;
- _mpd_get_msdigits(&dummy, &word, v, MPD_RDIGITS);
- n = mpd_word_digits(word);
- word *= mpd_pow10[MPD_RDIGITS-n];
- mpd_qresize(z, 2, status);
- (void)_mpd_shortdiv(z->data, p10data, 2, word);
- mpd_clear_flags(z);
- z->exp = -(v->exp + v->digits) - (MPD_RDIGITS-2);
- z->len = (z->data[1] == 0) ? 1 : 2;
- mpd_setdigits(z);
- }
- /* Reciprocal, calculated with Newton's Method */
- static void
- _mpd_qreciprocal(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- mpd_context_t varcontext, maxcontext;
- mpd_t *z = result; /* current approximation */
- mpd_t *v; /* a, normalized to a number between 0.1 and 1 */
- MPD_NEW_SHARED(vtmp, a); /* by default v will share data with a */
- MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */
- MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */
- MPD_NEW_CONST(two,0,0,1,1,1,2); /* const 2 */
- mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
- mpd_ssize_t adj, maxprec, initprec;
- uint8_t sign = mpd_sign(a);
- int i;
- v = &vtmp;
- if (result == a) {
- if ((v = mpd_qncopy(a)) == NULL) { /* GCOV_NOT_REACHED */
- mpd_seterror(result, MPD_Malloc_error, status); /* GCOV_NOT_REACHED */
- goto finish; /* GCOV_NOT_REACHED */
- }
- }
- mpd_clear_flags(v);
- adj = v->digits + v->exp;
- v->exp = -v->digits;
- /* initial approximation */
- _mpd_qreciprocal_approx(z, v, status);
- mpd_maxcontext(&varcontext);
- mpd_maxcontext(&maxcontext);
- varcontext.round = MPD_ROUND_TRUNC;
- maxcontext.round = MPD_ROUND_TRUNC;
- maxprec = (v->digits > ctx->prec) ? v->digits : ctx->prec;
- maxprec += 2;
- initprec = MPD_RDIGITS-3;
- i = recpr_schedule_prec(klist, maxprec, initprec);
- for (; i >= 0; i--) {
- mpd_qmul(&s, z, z, &maxcontext, status);
- varcontext.prec = 2*klist[i] + 5;
- if (v->digits > varcontext.prec) {
- mpd_qshiftr(&t, v, v->digits-varcontext.prec, status);
- t.exp = -varcontext.prec;
- mpd_qmul(&t, &t, &s, &varcontext, status);
- }
- else {
- mpd_qmul(&t, v, &s, &varcontext, status);
- }
- mpd_qmul(&s, z, &two, &maxcontext, status);
- mpd_qsub(z, &s, &t, &maxcontext, status);
- }
- if (!mpd_isspecial(z)) {
- z->exp -= adj;
- mpd_set_flags(z, sign);
- }
- finish:
- mpd_del(&s);
- mpd_del(&t);
- if (v != &vtmp) mpd_del(v);
- mpd_qfinalize(z, ctx, status);
- }
- /*
- * Integer division with remainder of the coefficients: coeff(a) / coeff(b).
- * This function is for large numbers where it is faster to divide by
- * multiplying the dividend by the reciprocal of the divisor.
- * The inexact result is fixed by a small loop, which should not take
- * more than 2 iterations.
- */
- static void
- _mpd_qbarrett_divmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
- uint32_t *status)
- {
- mpd_context_t workctx;
- mpd_t *qq = q, *rr = r;
- mpd_t aa, bb;
- int k;
- mpd_maxcontext(&workctx);
- _mpd_copy_shared(&aa, a);
- _mpd_copy_shared(&bb, b);
- mpd_set_positive(&aa);
- mpd_set_positive(&bb);
- aa.exp = 0;
- bb.exp = 0;
- if (q == a || q == b) {
- if ((qq = mpd_qnew()) == NULL) {
- *status |= MPD_Malloc_error;
- goto nanresult;
- }
- }
- if (r == a || r == b) {
- if ((rr = mpd_qnew()) == NULL) {
- *status |= MPD_Malloc_error;
- goto nanresult;
- }
- }
- /* maximum length of q + 3 digits */
- workctx.prec = aa.digits - bb.digits + 1 + 3;
- /* we get the reciprocal with precision maxlen(q) + 3 */
- _mpd_qreciprocal(rr, &bb, &workctx, &workctx.status);
- mpd_qmul(qq, &aa, rr, &workctx, &workctx.status);
- mpd_qtrunc(qq, qq, &workctx, &workctx.status);
- workctx.prec = aa.digits + 3;
- /* get the remainder */
- mpd_qmul(rr, &bb, qq, &workctx, &workctx.status);
- mpd_qsub(rr, &aa, rr, &workctx, &workctx.status);
- /* Fix the result. Algorithm from: Karl Hasselstrom, Fast Division of Large Integers */
- for (k = 0;; k++) {
- if (mpd_isspecial(rr)) {
- *status |= (workctx.status&MPD_Errors);
- goto nanresult;
- }
- if (k > 2) {
- mpd_err_warn("_mpd_barrett_divmod: k > 2 in correcting loop"); /* GCOV_NOT_REACHED */
- abort(); /* GCOV_NOT_REACHED */
- }
- else if (_mpd_cmp(&zero, rr) == 1) {
- mpd_qadd(rr, rr, &bb, &workctx, &workctx.status);
- mpd_qadd(qq, qq, &minus_one, &workctx, &workctx.status);
- }
- else if (_mpd_cmp(rr, &bb) == -1) {
- break;
- }
- else {
- mpd_qsub(rr, rr, &bb, &workctx, &workctx.status);
- mpd_qadd(qq, qq, &one, &workctx, &workctx.status);
- }
- }
- if (qq != q) {
- if (!mpd_qcopy(q, qq, status)) {
- goto nanresult; /* GCOV_UNLIKELY */
- }
- mpd_del(qq);
- }
- if (rr != r) {
- if (!mpd_qcopy(r, rr, status)) {
- goto nanresult; /* GCOV_UNLIKELY */
- }
- mpd_del(rr);
- }
- *status |= (workctx.status&MPD_Errors);
- return;
- nanresult:
- if (qq && qq != q) mpd_del(qq);
- if (rr && rr != r) mpd_del(rr);
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- }
- static inline int
- invroot_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2],
- mpd_ssize_t maxprec, mpd_ssize_t initprec)
- {
- mpd_ssize_t k;
- int i;
- assert(maxprec >= 3 && initprec >= 3);
- if (maxprec <= initprec) return -1;
- i = 0; k = maxprec;
- do {
- k = (k+3) / 2;
- klist[i++] = k;
- } while (k > initprec);
- return i-1;
- }
- /*
- * Initial approximation for the inverse square root.
- *
- * Input:
- * v := 7 or 8 decimal digits with an implicit exponent of 10**-6,
- * representing a number 1 <= x < 100.
- *
- * Output:
- * An approximation to 1/sqrt(v)
- */
- static inline void
- _invroot_init_approx(mpd_t *z, mpd_uint_t v)
- {
- mpd_uint_t lo = 1000;
- mpd_uint_t hi = 10000;
- mpd_uint_t a, sq;
- assert(v >= lo*lo && v < (hi+1)*(hi+1));
- for(;;) {
- a = (lo + hi) / 2;
- sq = a * a;
- if (v >= sq) {
- if (v < sq + 2*a + 1) {
- break;
- }
- lo = a + 1;
- }
- else {
- hi = a - 1;
- }
- }
- /* At this point a/1000 is an approximation to sqrt(v). */
- mpd_minalloc(z);
- mpd_clear_flags(z);
- z->data[0] = 1000000000UL / a;
- z->len = 1;
- z->exp = -6;
- mpd_setdigits(z);
- }
- static void
- _mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_context_t varcontext, maxcontext;
- mpd_t *z = result; /* current approximation */
- mpd_t *v; /* a, normalized to a number between 1 and 100 */
- MPD_NEW_SHARED(vtmp, a); /* by default v will share data with a */
- MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */
- MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */
- MPD_NEW_CONST(one_half,0,-1,1,1,1,5);
- MPD_NEW_CONST(three,0,0,1,1,1,3);
- mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
- mpd_ssize_t ideal_exp, shift;
- mpd_ssize_t adj, tz;
- mpd_ssize_t maxprec, fracdigits;
- mpd_uint_t x, dummy;
- int i, n;
- ideal_exp = -(a->exp - (a->exp & 1)) / 2;
- v = &vtmp;
- if (result == a) {
- if ((v = mpd_qncopy(a)) == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- }
- /* normalize a to 1 <= v < 100 */
- if ((v->digits+v->exp) & 1) {
- fracdigits = v->digits - 1;
- v->exp = -fracdigits;
- n = (v->digits > 7) ? 7 : (int)v->digits;
- _mpd_get_msdigits(&dummy, &x, v, n);
- if (n < 7) {
- x *= mpd_pow10[7-n];
- }
- }
- else {
- fracdigits = v->digits - 2;
- v->exp = -fracdigits;
- n = (v->digits > 8) ? 8 : (int)v->digits;
- _mpd_get_msdigits(&dummy, &x, v, n);
- if (n < 8) {
- x *= mpd_pow10[8-n];
- }
- }
- adj = (a->exp-v->exp) / 2;
- /* initial approximation */
- _invroot_init_approx(z, x);
- mpd_maxcontext(&maxcontext);
- mpd_maxcontext(&varcontext);
- varcontext.round = MPD_ROUND_TRUNC;
- maxprec = ctx->prec + 2;
- i = invroot_schedule_prec(klist, maxprec, 3);
- for (; i >= 0; i--) {
- varcontext.prec = 2*klist[i]+2;
- mpd_qmul(&s, z, z, &maxcontext, &workstatus);
- if (v->digits > varcontext.prec) {
- shift = v->digits - varcontext.prec;
- mpd_qshiftr(&t, v, shift, &workstatus);
- t.exp += shift;
- mpd_qmul(&t, &t, &s, &varcontext, &workstatus);
- }
- else {
- mpd_qmul(&t, v, &s, &varcontext, &workstatus);
- }
- mpd_qsub(&t, &three, &t, &maxcontext, &workstatus);
- mpd_qmul(z, z, &t, &varcontext, &workstatus);
- mpd_qmul(z, z, &one_half, &maxcontext, &workstatus);
- }
- z->exp -= adj;
- tz = mpd_trail_zeros(result);
- shift = ideal_exp - result->exp;
- shift = (tz > shift) ? shift : tz;
- if (shift > 0) {
- mpd_qshiftr_inplace(result, shift);
- result->exp += shift;
- }
- mpd_del(&s);
- mpd_del(&t);
- if (v != &vtmp) mpd_del(v);
- *status |= (workstatus&MPD_Errors);
- varcontext = *ctx;
- varcontext.round = MPD_ROUND_HALF_EVEN;
- mpd_qfinalize(result, &varcontext, status);
- }
- void
- mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- /* positive infinity */
- _settriple(result, MPD_POS, 0, mpd_etiny(ctx));
- *status |= MPD_Clamped;
- return;
- }
- if (mpd_iszero(a)) {
- mpd_setspecial(result, mpd_sign(a), MPD_INF);
- *status |= MPD_Division_by_zero;
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- _mpd_qinvroot(result, a, ctx, status);
- }
- /*
- * Ensure correct rounding. Algorithm after Hull & Abrham, "Properly Rounded
- * Variable Precision Square Root", ACM Transactions on Mathematical Software,
- * Vol. 11, No. 3.
- */
- static void
- _mpd_fix_sqrt(mpd_t *result, const mpd_t *a, mpd_t *tmp,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_context_t maxctx;
- MPD_NEW_CONST(u,0,0,1,1,1,5);
- mpd_maxcontext(&maxctx);
- u.exp = u.digits - ctx->prec + result->exp - 1;
- _mpd_qsub(tmp, result, &u, &maxctx, status);
- if (*status&MPD_Errors) goto nanresult;
- _mpd_qmul(tmp, tmp, tmp, &maxctx, status);
- if (*status&MPD_Errors) goto nanresult;
- if (_mpd_cmp(tmp, a) == 1) {
- u.exp += 1;
- u.data[0] = 1;
- _mpd_qsub(result, result, &u, &maxctx, status);
- }
- else {
- _mpd_qadd(tmp, result, &u, &maxctx, status);
- if (*status&MPD_Errors) goto nanresult;
- _mpd_qmul(tmp, tmp, tmp, &maxctx, status);
- if (*status&MPD_Errors) goto nanresult;
- if (_mpd_cmp(tmp, a) == -1) {
- u.exp += 1;
- u.data[0] = 1;
- _mpd_qadd(result, result, &u, &maxctx, status);
- }
- }
- return;
- nanresult:
- mpd_setspecial(result, MPD_POS, MPD_NAN);
- }
- void
- mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
- uint32_t *status)
- {
- uint32_t workstatus = 0;
- mpd_context_t varcontext;
- mpd_t *z = result; /* current approximation */
- MPD_NEW_STATIC(v,0,0,0,0); /* a, normalized to a number between 1 and 10 */
- MPD_NEW_STATIC(vtmp,0,0,0,0);
- MPD_NEW_STATIC(tmp,0,0,0,0);
- mpd_ssize_t ideal_exp, shift;
- mpd_ssize_t target_prec, fracdigits;
- mpd_ssize_t a_exp, a_digits;
- mpd_ssize_t adj, tz;
- mpd_uint_t dummy, t;
- int exact = 0;
- varcontext = *ctx;
- varcontext.round = MPD_ROUND_HALF_EVEN;
- ideal_exp = (a->exp - (a->exp & 1)) / 2;
- if (mpd_isspecial(a)) {
- if (mpd_qcheck_nan(result, a, ctx, status)) {
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- mpd_setspecial(result, MPD_POS, MPD_INF);
- return;
- }
- if (mpd_iszero(a)) {
- _settriple(result, mpd_sign(a), 0, ideal_exp);
- mpd_qfinalize(result, ctx, status);
- return;
- }
- if (mpd_isnegative(a)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (!mpd_qcopy(&v, a, status)) {
- mpd_seterror(result, MPD_Malloc_error, status);
- goto finish;
- }
- a_exp = a->exp;
- a_digits = a->digits;
- /* normalize a to 1 <= v < 100 */
- if ((v.digits+v.exp) & 1) {
- fracdigits = v.digits - 1;
- v.exp = -fracdigits;
- _mpd_get_msdigits(&dummy, &t, &v, 3);
- t = t < 100 ? t*10 : t;
- t = t < 100 ? t*10 : t;
- }
- else {
- fracdigits = v.digits - 2;
- v.exp = -fracdigits;
- _mpd_get_msdigits(&dummy, &t, &v, 4);
- t = t < 1000 ? t*10 : t;
- t = t < 1000 ? t*10 : t;
- t = t < 1000 ? t*10 : t;
- }
- adj = (a_exp-v.exp) / 2;
- /* use excess digits */
- target_prec = (a_digits > ctx->prec) ? a_digits : ctx->prec;
- target_prec += 2;
- varcontext.prec = target_prec + 3;
- /* invroot is much faster for large numbers */
- _mpd_qinvroot(&tmp, &v, &varcontext, &workstatus);
- varcontext.prec = target_prec;
- _mpd_qdiv(NO_IDEAL_EXP, z, &one, &tmp, &varcontext, &workstatus);
- tz = mpd_trail_zeros(result);
- if ((result->digits-tz)*2-1 <= v.digits) {
- _mpd_qmul(&tmp, result, result, &varcontext, &workstatus);
- if (workstatus&MPD_Errors) {
- mpd_seterror(result, workstatus&MPD_Errors, status);
- goto finish;
- }
- exact = (_mpd_cmp(&tmp, &v) == 0);
- }
- *status |= (workstatus&MPD_Errors);
- if (!exact && !mpd_isspecial(result) && !mpd_iszero(result)) {
- _mpd_fix_sqrt(result, &v, &tmp, &varcontext, status);
- if (mpd_isspecial(result)) goto finish;
- *status |= (MPD_Rounded|MPD_Inexact);
- }
- result->exp += adj;
- if (exact) {
- shift = ideal_exp - result->exp;
- shift = (tz > shift) ? shift : tz;
- if (shift > 0) {
- mpd_qshiftr_inplace(result, shift);
- result->exp += shift;
- }
- }
- finish:
- mpd_del(&v);
- mpd_del(&vtmp);
- mpd_del(&tmp);
- varcontext.prec = ctx->prec;
- mpd_qfinalize(result, &varcontext, status);
- }
- /******************************************************************************/
- /* Base conversions */
- /******************************************************************************/
- /*
- * Returns the space needed to represent an integer mpd_t in base 'base'.
- * The result is undefined for non-integers.
- *
- * Max space needed:
- *
- * base^n >= 10^(digits+exp)
- * n >= log10(10^(digits+exp))/log10(base) = (digits+exp) / log10(base)
- */
- size_t
- mpd_sizeinbase(mpd_t *a, uint32_t base)
- {
- size_t x;
- assert(mpd_isinteger(a));
- if (mpd_iszero(a)) {
- return 1;
- }
- x = a->digits+a->exp;
- #ifdef CONFIG_64
- #ifdef USE_80BIT_LONG_DOUBLE
- return (long double)x / log10(base) + 3;
- #else
- /* x > floor(((1ULL<<53)-3) * log10(2)) */
- if (x > 2711437152599294ULL) {
- return SIZE_MAX;
- }
- return (double)x / log10(base) + 3;
- #endif
- #else /* CONFIG_32 */
- {
- double y = x / log10(base) + 3;
- return (y > SIZE_MAX) ? SIZE_MAX : (size_t)y;
- }
- #endif
- }
- /*
- * Returns the space needed to import a base 'base' integer of length 'srclen'.
- */
- static inline mpd_ssize_t
- _mpd_importsize(size_t srclen, uint32_t base)
- {
- #if SIZE_MAX == UINT64_MAX
- #ifdef USE_80BIT_LONG_DOUBLE
- long double x = (long double)srclen * (log10(base)/MPD_RDIGITS) + 3;
- #else
- double x;
- if (srclen > (1ULL<<53)) {
- return MPD_SSIZE_MAX;
- }
- x = (double)srclen * (log10(base)/MPD_RDIGITS) + 3;
- #endif
- #else
- double x = srclen * (log10(base)/MPD_RDIGITS) + 3;
- #endif
- return (x > MPD_MAXIMPORT) ? MPD_SSIZE_MAX : (mpd_ssize_t)x;
- }
- static inline size_t
- _to_base_u16(uint16_t *w, size_t wlen, mpd_uint_t wbase,
- mpd_uint_t *u, mpd_ssize_t ulen)
- {
- size_t n = 0;
- assert(wlen > 0 && ulen > 0);
- do {
- w[n++] = (uint16_t)_mpd_shortdiv(u, u, ulen, wbase);
- /* ulen will be at least 1. u[ulen-1] can only be zero if ulen == 1 */
- ulen = _mpd_real_size(u, ulen);
- } while (u[ulen-1] != 0 && n < wlen);
- /* proper termination condition */
- assert(u[ulen-1] == 0);
- return n;
- }
- static inline void
- _from_base_u16(mpd_uint_t *w, mpd_ssize_t wlen,
- const mpd_uint_t *u, size_t ulen, uint32_t ubase)
- {
- mpd_ssize_t m = 1;
- mpd_uint_t carry;
- assert(wlen > 0 && ulen > 0);
- w[0] = u[--ulen];
- while (--ulen != SIZE_MAX && m < wlen) {
- _mpd_shortmul(w, w, m, ubase);
- m = _mpd_real_size(w, m+1);
- carry = _mpd_shortadd(w, m, u[ulen]);
- if (carry) w[m++] = carry;
- }
- /* proper termination condition */
- assert(ulen == SIZE_MAX);
- }
- /* target base wbase <= source base ubase */
- static inline size_t
- _baseconv_to_smaller(uint32_t *w, size_t wlen, mpd_uint_t wbase,
- mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase)
- {
- size_t n = 0;
- assert(wlen > 0 && ulen > 0);
- do {
- w[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase);
- /* ulen will be at least 1. u[ulen-1] can only be zero if ulen == 1 */
- ulen = _mpd_real_size(u, ulen);
- } while (u[ulen-1] != 0 && n < wlen);
- /* proper termination condition */
- assert(u[ulen-1] == 0);
- return n;
- }
- /* target base wbase >= source base ubase */
- static inline void
- _baseconv_to_larger(mpd_uint_t *w, mpd_ssize_t wlen, mpd_uint_t wbase,
- const mpd_uint_t *u, size_t ulen, mpd_uint_t ubase)
- {
- mpd_ssize_t m = 1;
- mpd_uint_t carry;
- assert(wlen > 0 && ulen > 0);
- w[0] = u[--ulen];
- while (--ulen != SIZE_MAX && m < wlen) {
- _mpd_shortmul_b(w, w, m, ubase, wbase);
- m = _mpd_real_size(w, m+1);
- carry = _mpd_shortadd_b(w, m, u[ulen], wbase);
- if (carry) w[m++] = carry;
- }
- /* proper termination condition */
- assert(ulen == SIZE_MAX);
- }
- /*
- * Converts an integer mpd_t to a multiprecision integer with
- * base <= UINT16_MAX+1. The least significant word of the result
- * is rdata[0].
- */
- size_t
- mpd_qexport_u16(uint16_t *rdata, size_t rlen, uint32_t rbase,
- const mpd_t *src, uint32_t *status)
- {
- mpd_t *tsrc;
- size_t n;
- assert(rbase <= (1U<<16));
- assert(rlen <= SIZE_MAX/(sizeof *rdata));
- if (mpd_isspecial(src) || !_mpd_isint(src)) {
- *status |= MPD_Invalid_operation;
- return SIZE_MAX;
- }
- memset(rdata, 0, rlen * (sizeof *rdata));
- if (mpd_iszero(src)) {
- return 1;
- }
- if ((tsrc = mpd_qnew()) == NULL) {
- *status |= MPD_Malloc_error;
- return SIZE_MAX;
- }
- if (src->exp >= 0) {
- if (!mpd_qshiftl(tsrc, src, src->exp, status)) {
- mpd_del(tsrc);
- return SIZE_MAX;
- }
- }
- else {
- if (mpd_qshiftr(tsrc, src, -src->exp, status) == MPD_UINT_MAX) {
- mpd_del(tsrc);
- return SIZE_MAX;
- }
- }
- n = _to_base_u16(rdata, rlen, rbase, tsrc->data, tsrc->len);
- mpd_del(tsrc);
- return n;
- }
- /*
- * Converts an integer mpd_t to a multiprecision integer with
- * base <= UINT32_MAX. The least significant word of the result
- * is rdata[0].
- */
- size_t
- mpd_qexport_u32(uint32_t *rdata, size_t rlen, uint32_t rbase,
- const mpd_t *src, uint32_t *status)
- {
- mpd_t *tsrc;
- size_t n;
- if (mpd_isspecial(src) || !_mpd_isint(src)) {
- *status |= MPD_Invalid_operation;
- return SIZE_MAX;
- }
- #if MPD_SIZE_MAX < SIZE_MAX
- if (rlen > MPD_SSIZE_MAX) {
- *status |= MPD_Invalid_operation;
- return SIZE_MAX;
- }
- #endif
- assert(rlen <= SIZE_MAX/(sizeof *rdata));
- memset(rdata, 0, rlen * (sizeof *rdata));
- if (mpd_iszero(src)) {
- return 1;
- }
- if ((tsrc = mpd_qnew()) == NULL) {
- *status |= MPD_Malloc_error;
- return SIZE_MAX;
- }
- if (src->exp >= 0) {
- if (!mpd_qshiftl(tsrc, src, src->exp, status)) {
- mpd_del(tsrc);
- return SIZE_MAX;
- }
- }
- else {
- if (mpd_qshiftr(tsrc, src, -src->exp, status) == MPD_UINT_MAX) {
- mpd_del(tsrc);
- return SIZE_MAX;
- }
- }
- #ifdef CONFIG_64
- n = _baseconv_to_smaller(rdata, rlen, rbase,
- tsrc->data, tsrc->len, MPD_RADIX);
- #else
- if (rbase <= MPD_RADIX) {
- n = _baseconv_to_smaller(rdata, rlen, rbase,
- tsrc->data, tsrc->len, MPD_RADIX);
- }
- else {
- _baseconv_to_larger(rdata, (mpd_ssize_t)rlen, rbase,
- tsrc->data, tsrc->len, MPD_RADIX);
- n = _mpd_real_size(rdata, (mpd_ssize_t)rlen);
- }
- #endif
- mpd_del(tsrc);
- return n;
- }
- /*
- * Converts a multiprecision integer with base <= UINT16_MAX+1 to an mpd_t.
- * The least significant word of the source is srcdata[0].
- */
- void
- mpd_qimport_u16(mpd_t *result,
- const uint16_t *srcdata, size_t srclen,
- uint8_t srcsign, uint32_t srcbase,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_uint_t *usrc; /* uint16_t src copied to an mpd_uint_t array */
- mpd_ssize_t rlen; /* length of the result */
- size_t n = 0;
- assert(srclen > 0);
- assert(srcbase <= (1U<<16));
- if ((rlen = _mpd_importsize(srclen, srcbase)) == MPD_SSIZE_MAX) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (srclen > MPD_SIZE_MAX/(sizeof *usrc)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if ((usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc)) == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- for (n = 0; n < srclen; n++) {
- usrc[n] = srcdata[n];
- }
- /* result->data is initialized to zero */
- if (!mpd_qresize_zero(result, rlen, status)) {
- goto finish;
- }
- _from_base_u16(result->data, rlen, usrc, srclen, srcbase);
- mpd_set_flags(result, srcsign);
- result->exp = 0;
- result->len = _mpd_real_size(result->data, rlen);
- mpd_setdigits(result);
- mpd_qresize(result, result->len, status);
- mpd_qfinalize(result, ctx, status);
- finish:
- mpd_free(usrc);
- }
- /*
- * Converts a multiprecision integer with base <= UINT32_MAX to an mpd_t.
- * The least significant word of the source is srcdata[0].
- */
- void
- mpd_qimport_u32(mpd_t *result,
- const uint32_t *srcdata, size_t srclen,
- uint8_t srcsign, uint32_t srcbase,
- const mpd_context_t *ctx, uint32_t *status)
- {
- mpd_uint_t *usrc; /* uint32_t src copied to an mpd_uint_t array */
- mpd_ssize_t rlen; /* length of the result */
- size_t n = 0;
- assert(srclen > 0);
- if ((rlen = _mpd_importsize(srclen, srcbase)) == MPD_SSIZE_MAX) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if (srclen > MPD_SIZE_MAX/(sizeof *usrc)) {
- mpd_seterror(result, MPD_Invalid_operation, status);
- return;
- }
- if ((usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc)) == NULL) {
- mpd_seterror(result, MPD_Malloc_error, status);
- return;
- }
- for (n = 0; n < srclen; n++) {
- usrc[n] = srcdata[n];
- }
- /* result->data is initialized to zero */
- if (!mpd_qresize_zero(result, rlen, status)) {
- goto finish;
- }
- #ifdef CONFIG_64
- _baseconv_to_larger(result->data, rlen, MPD_RADIX,
- usrc, srclen, srcbase);
- #else
- if (srcbase <= MPD_RADIX) {
- _baseconv_to_larger(result->data, rlen, MPD_RADIX,
- usrc, srclen, srcbase);
- }
- else {
- _baseconv_to_smaller(result->data, rlen, MPD_RADIX,
- usrc, (mpd_ssize_t)srclen, srcbase);
- }
- #endif
- mpd_set_flags(result, srcsign);
- result->exp = 0;
- result->len = _mpd_real_size(result->data, rlen);
- mpd_setdigits(result);
- mpd_qresize(result, result->len, status);
- mpd_qfinalize(result, ctx, status);
- finish:
- mpd_free(usrc);
- }
- /*********************************************************************/
- /* Testcases for Newton Division */
- /*********************************************************************/
- static void
- _mpd_qtest_newtondiv(int action, mpd_t *q, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(aligned,0,0,0,0);
- mpd_uint_t ld;
- mpd_ssize_t shift, exp, tz;
- mpd_ssize_t newsize;
- mpd_ssize_t ideal_exp;
- mpd_uint_t rem;
- uint8_t sign_a = mpd_sign(a);
- uint8_t sign_b = mpd_sign(b);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(q, a, b, ctx, status)) {
- return;
- }
- _mpd_qdiv_inf(q, a, b, ctx, status);
- return;
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(q, MPD_Division_undefined, status);
- }
- else {
- mpd_setspecial(q, sign_a^sign_b, MPD_INF);
- *status |= MPD_Division_by_zero;
- }
- return;
- }
- if (mpd_iszerocoeff(a)) {
- exp = a->exp - b->exp;
- _settriple(q, sign_a^sign_b, 0, exp);
- mpd_qfinalize(q, ctx, status);
- return;
- }
- shift = (b->digits - a->digits) + ctx->prec + 1;
- ideal_exp = a->exp - b->exp;
- exp = ideal_exp - shift;
- if (shift > 0) {
- if (!mpd_qshiftl(&aligned, a, shift, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- a = &aligned;
- }
- else if (shift < 0) {
- shift = -shift;
- if (!mpd_qshiftl(&aligned, b, shift, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- b = &aligned;
- }
- newsize = a->len - b->len + 1;
- if ((q != b && q != a) || (q == b && newsize > b->len)) {
- if (!mpd_qresize(q, newsize, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- goto finish;
- }
- }
- {
- MPD_NEW_STATIC(r,0,0,0,0);
- _mpd_qbarrett_divmod(q, &r, a, b, status);
- if (mpd_isspecial(q) || mpd_isspecial(&r)) {
- mpd_del(&r);
- goto finish;
- }
- rem = !mpd_iszerocoeff(&r);
- mpd_del(&r);
- newsize = q->len;
- }
- newsize = _mpd_real_size(q->data, newsize);
- /* resize to smaller cannot fail */
- mpd_qresize(q, newsize, status);
- q->len = newsize;
- mpd_setdigits(q);
- shift = ideal_exp - exp;
- if (rem) {
- ld = mpd_lsd(q->data[0]);
- if (ld == 0 || ld == 5) {
- q->data[0] += 1;
- }
- }
- else if (action == SET_IDEAL_EXP && shift > 0) {
- tz = mpd_trail_zeros(q);
- shift = (tz > shift) ? shift : tz;
- mpd_qshiftr_inplace(q, shift);
- exp += shift;
- }
- mpd_set_flags(q, sign_a^sign_b);
- q->exp = exp;
- finish:
- mpd_del(&aligned);
- mpd_qfinalize(q, ctx, status);
- }
- static void
- mpd_qtest_newtondiv(mpd_t *q, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- _mpd_qtest_newtondiv(SET_IDEAL_EXP, q, a, b, ctx, status);
- }
- static void
- _mpd_qtest_barrett_divmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(aligned,0,0,0,0);
- mpd_ssize_t qsize, rsize;
- mpd_ssize_t ideal_exp, expdiff, shift;
- uint8_t sign_a = mpd_sign(a);
- uint8_t sign_ab = mpd_sign(a)^mpd_sign(b);
- ideal_exp = (a->exp > b->exp) ? b->exp : a->exp;
- if (mpd_iszerocoeff(a)) {
- if (!mpd_qcopy(r, a, status)) {
- goto nanresult; /* GCOV_NOT_REACHED */
- }
- r->exp = ideal_exp;
- _settriple(q, sign_ab, 0, 0);
- return;
- }
- expdiff = mpd_adjexp(a) - mpd_adjexp(b);
- if (expdiff < 0) {
- if (a->exp > b->exp) {
- /* positive and less than b->digits - a->digits */
- shift = a->exp - b->exp;
- if (!mpd_qshiftl(r, a, shift, status)) {
- goto nanresult;
- }
- r->exp = ideal_exp;
- }
- else {
- if (!mpd_qcopy(r, a, status)) {
- goto nanresult;
- }
- }
- _settriple(q, sign_ab, 0, 0);
- return;
- }
- if (expdiff > ctx->prec) {
- *status |= MPD_Division_impossible;
- goto nanresult;
- }
- /*
- * At this point we have:
- * (1) 0 <= a->exp + a->digits - b->exp - b->digits <= prec
- * (2) a->exp - b->exp >= b->digits - a->digits
- * (3) a->exp - b->exp <= prec + b->digits - a->digits
- */
- if (a->exp != b->exp) {
- shift = a->exp - b->exp;
- if (shift > 0) {
- /* by (3), after the shift a->digits <= prec + b->digits */
- if (!mpd_qshiftl(&aligned, a, shift, status)) {
- goto nanresult;
- }
- a = &aligned;
- }
- else {
- shift = -shift;
- /* by (2), after the shift b->digits <= a->digits */
- if (!mpd_qshiftl(&aligned, b, shift, status)) {
- goto nanresult;
- }
- b = &aligned;
- }
- }
- qsize = a->len - b->len + 1;
- if (!(q == a && qsize < a->len) && !(q == b && qsize < b->len)) {
- if (!mpd_qresize(q, qsize, status)) {
- goto nanresult;
- }
- }
- rsize = b->len;
- if (!(r == a && rsize < a->len)) {
- if (!mpd_qresize(r, rsize, status)) {
- goto nanresult;
- }
- }
- _mpd_qbarrett_divmod(q, r, a, b, status);
- if (mpd_isspecial(q) || mpd_isspecial(r)) {
- goto nanresult;
- }
- if (mpd_isinfinite(q) || q->digits > ctx->prec) {
- *status |= MPD_Division_impossible;
- goto nanresult;
- }
- qsize = q->len;
- rsize = r->len;
- qsize = _mpd_real_size(q->data, qsize);
- /* resize to smaller cannot fail */
- mpd_qresize(q, qsize, status);
- q->len = qsize;
- mpd_setdigits(q);
- mpd_set_flags(q, sign_ab);
- q->exp = 0;
- if (q->digits > ctx->prec) {
- *status |= MPD_Division_impossible; /* GCOV_NOT_REACHED */
- goto nanresult; /* GCOV_NOT_REACHED */
- }
- rsize = _mpd_real_size(r->data, rsize);
- /* resize to smaller cannot fail */
- mpd_qresize(r, rsize, status);
- r->len = rsize;
- mpd_setdigits(r);
- mpd_set_flags(r, sign_a);
- r->exp = ideal_exp;
- out:
- mpd_del(&aligned);
- return;
- nanresult:
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- goto out;
- }
- static void
- mpd_qtest_newtondivint(mpd_t *q, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(r,0,0,0,0);
- uint8_t sign = mpd_sign(a)^mpd_sign(b);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(q, a, b, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a) && mpd_isinfinite(b)) {
- mpd_seterror(q, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_setspecial(q, sign, MPD_INF);
- return;
- }
- if (mpd_isinfinite(b)) {
- _settriple(q, sign, 0, 0);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(q, MPD_Division_undefined, status);
- }
- else {
- mpd_setspecial(q, sign, MPD_INF);
- *status |= MPD_Division_by_zero;
- }
- return;
- }
- _mpd_qtest_barrett_divmod(q, &r, a, b, ctx, status);
- mpd_del(&r);
- mpd_qfinalize(q, ctx, status);
- }
- static void
- mpd_qtest_newtonrem(mpd_t *r, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- MPD_NEW_STATIC(q,0,0,0,0);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(r, a, b, ctx, status)) {
- return;
- }
- if (mpd_isinfinite(a)) {
- mpd_seterror(r, MPD_Invalid_operation, status);
- return;
- }
- if (mpd_isinfinite(b)) {
- mpd_qcopy(r, a, status);
- mpd_qfinalize(r, ctx, status);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_seterror(r, MPD_Division_undefined, status);
- }
- else {
- mpd_seterror(r, MPD_Invalid_operation, status);
- }
- return;
- }
- _mpd_qtest_barrett_divmod(&q, r, a, b, ctx, status);
- mpd_del(&q);
- mpd_qfinalize(r, ctx, status);
- }
- static void
- mpd_qtest_newtondivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
- const mpd_context_t *ctx, uint32_t *status)
- {
- uint8_t sign = mpd_sign(a)^mpd_sign(b);
- if (mpd_isspecial(a) || mpd_isspecial(b)) {
- if (mpd_qcheck_nans(q, a, b, ctx, status)) {
- mpd_qcopy(r, q, status);
- return;
- }
- if (mpd_isinfinite(a)) {
- if (mpd_isinfinite(b)) {
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- }
- else {
- mpd_setspecial(q, sign, MPD_INF);
- }
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- *status |= MPD_Invalid_operation;
- return;
- }
- if (mpd_isinfinite(b)) {
- if (!mpd_qcopy(r, a, status)) {
- mpd_seterror(q, MPD_Malloc_error, status);
- return;
- }
- mpd_qfinalize(r, ctx, status);
- _settriple(q, sign, 0, 0);
- return;
- }
- /* debug */
- abort(); /* GCOV_NOT_REACHED */
- }
- if (mpd_iszerocoeff(b)) {
- if (mpd_iszerocoeff(a)) {
- mpd_setspecial(q, MPD_POS, MPD_NAN);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- *status |= MPD_Division_undefined;
- }
- else {
- mpd_setspecial(q, sign, MPD_INF);
- mpd_setspecial(r, MPD_POS, MPD_NAN);
- *status |= (MPD_Division_by_zero|MPD_Invalid_operation);
- }
- return;
- }
- _mpd_qtest_barrett_divmod(q, r, a, b, ctx, status);
- mpd_qfinalize(q, ctx, status);
- mpd_qfinalize(r, ctx, status);
- }
- void
- mpd_test_newtondiv(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx)
- {
- uint32_t status = 0;
- mpd_qtest_newtondiv(q, a, b, ctx, &status);
- mpd_addstatus_raise(ctx, status);
- }
- void
- mpd_test_newtondivint(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx)
- {
- uint32_t status = 0;
- mpd_qtest_newtondivint(q, a, b, ctx, &status);
- mpd_addstatus_raise(ctx, status);
- }
- void
- mpd_test_newtonrem(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx)
- {
- uint32_t status = 0;
- mpd_qtest_newtonrem(r, a, b, ctx, &status);
- mpd_addstatus_raise(ctx, status);
- }
- void
- mpd_test_newtondivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx)
- {
- uint32_t status = 0;
- mpd_qtest_newtondivmod(q, r, a, b, ctx, &status);
- mpd_addstatus_raise(ctx, status);
- }
|