mpdecimal.c 226 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410
  1. /*
  2. * Copyright (c) 2008-2016 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "mpdecimal.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <limits.h>
  32. #include <math.h>
  33. #include "basearith.h"
  34. #include "bits.h"
  35. #include "convolute.h"
  36. #include "crt.h"
  37. #include "memory.h"
  38. #include "typearith.h"
  39. #include "umodarith.h"
  40. #ifdef PPRO
  41. #if defined(_MSC_VER)
  42. #include <float.h>
  43. #pragma fenv_access(on)
  44. #elif !defined(__OpenBSD__) && !defined(__NetBSD__)
  45. /* C99 */
  46. #include <fenv.h>
  47. #pragma STDC FENV_ACCESS ON
  48. #endif
  49. #endif
  50. #if defined(_MSC_VER)
  51. #define ALWAYS_INLINE __forceinline
  52. #elif defined(LEGACY_COMPILER)
  53. #define ALWAYS_INLINE
  54. #undef inline
  55. #define inline
  56. #else
  57. #ifdef TEST_COVERAGE
  58. #define ALWAYS_INLINE
  59. #else
  60. #define ALWAYS_INLINE inline __attribute__ ((always_inline))
  61. #endif
  62. #endif
  63. #define MPD_NEWTONDIV_CUTOFF 1024L
  64. #define MPD_NEW_STATIC(name, flags, exp, digits, len) \
  65. mpd_uint_t name##_data[MPD_MINALLOC_MAX]; \
  66. mpd_t name = {flags|MPD_STATIC|MPD_STATIC_DATA, exp, digits, \
  67. len, MPD_MINALLOC_MAX, name##_data}
  68. #define MPD_NEW_CONST(name, flags, exp, digits, len, alloc, initval) \
  69. mpd_uint_t name##_data[alloc] = {initval}; \
  70. mpd_t name = {flags|MPD_STATIC|MPD_CONST_DATA, exp, digits, \
  71. len, alloc, name##_data}
  72. #define MPD_NEW_SHARED(name, a) \
  73. mpd_t name = {(a->flags&~MPD_DATAFLAGS)|MPD_STATIC|MPD_SHARED_DATA, \
  74. a->exp, a->digits, a->len, a->alloc, a->data}
  75. static mpd_uint_t data_one[1] = {1};
  76. static mpd_uint_t data_zero[1] = {0};
  77. static const mpd_t one = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_one};
  78. static const mpd_t minus_one = {MPD_NEG|MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1,
  79. data_one};
  80. static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero};
  81. static inline void _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx,
  82. uint32_t *status);
  83. static void _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a,
  84. mpd_ssize_t exp);
  85. static inline mpd_ssize_t _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size);
  86. static int _mpd_cmp_abs(const mpd_t *a, const mpd_t *b);
  87. static void _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
  88. const mpd_context_t *ctx, uint32_t *status);
  89. static inline void _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
  90. const mpd_context_t *ctx, uint32_t *status);
  91. static void _mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a,
  92. const mpd_t *b, uint32_t *status);
  93. static inline void _mpd_qpow_uint(mpd_t *result, const mpd_t *base,
  94. mpd_uint_t exp, uint8_t resultsign,
  95. const mpd_context_t *ctx, uint32_t *status);
  96. static mpd_uint_t mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n);
  97. /******************************************************************************/
  98. /* Version */
  99. /******************************************************************************/
  100. const char *
  101. mpd_version(void)
  102. {
  103. return MPD_VERSION;
  104. }
  105. /******************************************************************************/
  106. /* Performance critical inline functions */
  107. /******************************************************************************/
  108. #ifdef CONFIG_64
  109. /* Digits in a word, primarily useful for the most significant word. */
  110. ALWAYS_INLINE int
  111. mpd_word_digits(mpd_uint_t word)
  112. {
  113. if (word < mpd_pow10[9]) {
  114. if (word < mpd_pow10[4]) {
  115. if (word < mpd_pow10[2]) {
  116. return (word < mpd_pow10[1]) ? 1 : 2;
  117. }
  118. return (word < mpd_pow10[3]) ? 3 : 4;
  119. }
  120. if (word < mpd_pow10[6]) {
  121. return (word < mpd_pow10[5]) ? 5 : 6;
  122. }
  123. if (word < mpd_pow10[8]) {
  124. return (word < mpd_pow10[7]) ? 7 : 8;
  125. }
  126. return 9;
  127. }
  128. if (word < mpd_pow10[14]) {
  129. if (word < mpd_pow10[11]) {
  130. return (word < mpd_pow10[10]) ? 10 : 11;
  131. }
  132. if (word < mpd_pow10[13]) {
  133. return (word < mpd_pow10[12]) ? 12 : 13;
  134. }
  135. return 14;
  136. }
  137. if (word < mpd_pow10[18]) {
  138. if (word < mpd_pow10[16]) {
  139. return (word < mpd_pow10[15]) ? 15 : 16;
  140. }
  141. return (word < mpd_pow10[17]) ? 17 : 18;
  142. }
  143. return (word < mpd_pow10[19]) ? 19 : 20;
  144. }
  145. #else
  146. ALWAYS_INLINE int
  147. mpd_word_digits(mpd_uint_t word)
  148. {
  149. if (word < mpd_pow10[4]) {
  150. if (word < mpd_pow10[2]) {
  151. return (word < mpd_pow10[1]) ? 1 : 2;
  152. }
  153. return (word < mpd_pow10[3]) ? 3 : 4;
  154. }
  155. if (word < mpd_pow10[6]) {
  156. return (word < mpd_pow10[5]) ? 5 : 6;
  157. }
  158. if (word < mpd_pow10[8]) {
  159. return (word < mpd_pow10[7]) ? 7 : 8;
  160. }
  161. return (word < mpd_pow10[9]) ? 9 : 10;
  162. }
  163. #endif
  164. /* Adjusted exponent */
  165. ALWAYS_INLINE mpd_ssize_t
  166. mpd_adjexp(const mpd_t *dec)
  167. {
  168. return (dec->exp + dec->digits) - 1;
  169. }
  170. /* Etiny */
  171. ALWAYS_INLINE mpd_ssize_t
  172. mpd_etiny(const mpd_context_t *ctx)
  173. {
  174. return ctx->emin - (ctx->prec - 1);
  175. }
  176. /* Etop: used for folding down in IEEE clamping */
  177. ALWAYS_INLINE mpd_ssize_t
  178. mpd_etop(const mpd_context_t *ctx)
  179. {
  180. return ctx->emax - (ctx->prec - 1);
  181. }
  182. /* Most significant word */
  183. ALWAYS_INLINE mpd_uint_t
  184. mpd_msword(const mpd_t *dec)
  185. {
  186. assert(dec->len > 0);
  187. return dec->data[dec->len-1];
  188. }
  189. /* Most significant digit of a word */
  190. inline mpd_uint_t
  191. mpd_msd(mpd_uint_t word)
  192. {
  193. int n;
  194. n = mpd_word_digits(word);
  195. return word / mpd_pow10[n-1];
  196. }
  197. /* Least significant digit of a word */
  198. ALWAYS_INLINE mpd_uint_t
  199. mpd_lsd(mpd_uint_t word)
  200. {
  201. return word % 10;
  202. }
  203. /* Coefficient size needed to store 'digits' */
  204. ALWAYS_INLINE mpd_ssize_t
  205. mpd_digits_to_size(mpd_ssize_t digits)
  206. {
  207. mpd_ssize_t q, r;
  208. _mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
  209. return (r == 0) ? q : q+1;
  210. }
  211. /* Number of digits in the exponent. Not defined for MPD_SSIZE_MIN. */
  212. inline int
  213. mpd_exp_digits(mpd_ssize_t exp)
  214. {
  215. exp = (exp < 0) ? -exp : exp;
  216. return mpd_word_digits(exp);
  217. }
  218. /* Canonical */
  219. ALWAYS_INLINE int
  220. mpd_iscanonical(const mpd_t *dec UNUSED)
  221. {
  222. return 1;
  223. }
  224. /* Finite */
  225. ALWAYS_INLINE int
  226. mpd_isfinite(const mpd_t *dec)
  227. {
  228. return !(dec->flags & MPD_SPECIAL);
  229. }
  230. /* Infinite */
  231. ALWAYS_INLINE int
  232. mpd_isinfinite(const mpd_t *dec)
  233. {
  234. return dec->flags & MPD_INF;
  235. }
  236. /* NaN */
  237. ALWAYS_INLINE int
  238. mpd_isnan(const mpd_t *dec)
  239. {
  240. return dec->flags & (MPD_NAN|MPD_SNAN);
  241. }
  242. /* Negative */
  243. ALWAYS_INLINE int
  244. mpd_isnegative(const mpd_t *dec)
  245. {
  246. return dec->flags & MPD_NEG;
  247. }
  248. /* Positive */
  249. ALWAYS_INLINE int
  250. mpd_ispositive(const mpd_t *dec)
  251. {
  252. return !(dec->flags & MPD_NEG);
  253. }
  254. /* qNaN */
  255. ALWAYS_INLINE int
  256. mpd_isqnan(const mpd_t *dec)
  257. {
  258. return dec->flags & MPD_NAN;
  259. }
  260. /* Signed */
  261. ALWAYS_INLINE int
  262. mpd_issigned(const mpd_t *dec)
  263. {
  264. return dec->flags & MPD_NEG;
  265. }
  266. /* sNaN */
  267. ALWAYS_INLINE int
  268. mpd_issnan(const mpd_t *dec)
  269. {
  270. return dec->flags & MPD_SNAN;
  271. }
  272. /* Special */
  273. ALWAYS_INLINE int
  274. mpd_isspecial(const mpd_t *dec)
  275. {
  276. return dec->flags & MPD_SPECIAL;
  277. }
  278. /* Zero */
  279. ALWAYS_INLINE int
  280. mpd_iszero(const mpd_t *dec)
  281. {
  282. return !mpd_isspecial(dec) && mpd_msword(dec) == 0;
  283. }
  284. /* Test for zero when specials have been ruled out already */
  285. ALWAYS_INLINE int
  286. mpd_iszerocoeff(const mpd_t *dec)
  287. {
  288. return mpd_msword(dec) == 0;
  289. }
  290. /* Normal */
  291. inline int
  292. mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx)
  293. {
  294. if (mpd_isspecial(dec)) return 0;
  295. if (mpd_iszerocoeff(dec)) return 0;
  296. return mpd_adjexp(dec) >= ctx->emin;
  297. }
  298. /* Subnormal */
  299. inline int
  300. mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx)
  301. {
  302. if (mpd_isspecial(dec)) return 0;
  303. if (mpd_iszerocoeff(dec)) return 0;
  304. return mpd_adjexp(dec) < ctx->emin;
  305. }
  306. /* Odd word */
  307. ALWAYS_INLINE int
  308. mpd_isoddword(mpd_uint_t word)
  309. {
  310. return word & 1;
  311. }
  312. /* Odd coefficient */
  313. ALWAYS_INLINE int
  314. mpd_isoddcoeff(const mpd_t *dec)
  315. {
  316. return mpd_isoddword(dec->data[0]);
  317. }
  318. /* 0 if dec is positive, 1 if dec is negative */
  319. ALWAYS_INLINE uint8_t
  320. mpd_sign(const mpd_t *dec)
  321. {
  322. return dec->flags & MPD_NEG;
  323. }
  324. /* 1 if dec is positive, -1 if dec is negative */
  325. ALWAYS_INLINE int
  326. mpd_arith_sign(const mpd_t *dec)
  327. {
  328. return 1 - 2 * mpd_isnegative(dec);
  329. }
  330. /* Radix */
  331. ALWAYS_INLINE long
  332. mpd_radix(void)
  333. {
  334. return 10;
  335. }
  336. /* Dynamic decimal */
  337. ALWAYS_INLINE int
  338. mpd_isdynamic(const mpd_t *dec)
  339. {
  340. return !(dec->flags & MPD_STATIC);
  341. }
  342. /* Static decimal */
  343. ALWAYS_INLINE int
  344. mpd_isstatic(const mpd_t *dec)
  345. {
  346. return dec->flags & MPD_STATIC;
  347. }
  348. /* Data of decimal is dynamic */
  349. ALWAYS_INLINE int
  350. mpd_isdynamic_data(const mpd_t *dec)
  351. {
  352. return !(dec->flags & MPD_DATAFLAGS);
  353. }
  354. /* Data of decimal is static */
  355. ALWAYS_INLINE int
  356. mpd_isstatic_data(const mpd_t *dec)
  357. {
  358. return dec->flags & MPD_STATIC_DATA;
  359. }
  360. /* Data of decimal is shared */
  361. ALWAYS_INLINE int
  362. mpd_isshared_data(const mpd_t *dec)
  363. {
  364. return dec->flags & MPD_SHARED_DATA;
  365. }
  366. /* Data of decimal is const */
  367. ALWAYS_INLINE int
  368. mpd_isconst_data(const mpd_t *dec)
  369. {
  370. return dec->flags & MPD_CONST_DATA;
  371. }
  372. /******************************************************************************/
  373. /* Inline memory handling */
  374. /******************************************************************************/
  375. /* Fill destination with zeros */
  376. ALWAYS_INLINE void
  377. mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len)
  378. {
  379. mpd_size_t i;
  380. for (i = 0; i < len; i++) {
  381. dest[i] = 0;
  382. }
  383. }
  384. /* Free a decimal */
  385. ALWAYS_INLINE void
  386. mpd_del(mpd_t *dec)
  387. {
  388. if (mpd_isdynamic_data(dec)) {
  389. mpd_free(dec->data);
  390. }
  391. if (mpd_isdynamic(dec)) {
  392. mpd_free(dec);
  393. }
  394. }
  395. /*
  396. * Resize the coefficient. Existing data up to 'nwords' is left untouched.
  397. * Return 1 on success, 0 otherwise.
  398. *
  399. * Input invariant: MPD_MINALLOC <= result->alloc.
  400. *
  401. * Case nwords == result->alloc:
  402. * 'result' is unchanged. Return 1.
  403. *
  404. * Case nwords > result->alloc:
  405. * Case realloc success:
  406. * The value of 'result' does not change. Return 1.
  407. * Case realloc failure:
  408. * 'result' is NaN, status is updated with MPD_Malloc_error. Return 0.
  409. *
  410. * Case nwords < result->alloc:
  411. * Case is_static_data or realloc failure [1]:
  412. * 'result' is unchanged. Return 1.
  413. * Case realloc success:
  414. * The value of result is undefined (expected). Return 1.
  415. *
  416. *
  417. * [1] In that case the old (now oversized) area is still valid.
  418. */
  419. ALWAYS_INLINE int
  420. mpd_qresize(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
  421. {
  422. assert(!mpd_isconst_data(result)); /* illegal operation for a const */
  423. assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
  424. assert(MPD_MINALLOC <= result->alloc);
  425. nwords = (nwords <= MPD_MINALLOC) ? MPD_MINALLOC : nwords;
  426. if (nwords == result->alloc) {
  427. return 1;
  428. }
  429. if (mpd_isstatic_data(result)) {
  430. if (nwords > result->alloc) {
  431. return mpd_switch_to_dyn(result, nwords, status);
  432. }
  433. return 1;
  434. }
  435. return mpd_realloc_dyn(result, nwords, status);
  436. }
  437. /* Same as mpd_qresize, but the complete coefficient (including the old
  438. * memory area!) is initialized to zero. */
  439. ALWAYS_INLINE int
  440. mpd_qresize_zero(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
  441. {
  442. assert(!mpd_isconst_data(result)); /* illegal operation for a const */
  443. assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
  444. assert(MPD_MINALLOC <= result->alloc);
  445. nwords = (nwords <= MPD_MINALLOC) ? MPD_MINALLOC : nwords;
  446. if (nwords != result->alloc) {
  447. if (mpd_isstatic_data(result)) {
  448. if (nwords > result->alloc) {
  449. return mpd_switch_to_dyn_zero(result, nwords, status);
  450. }
  451. }
  452. else if (!mpd_realloc_dyn(result, nwords, status)) {
  453. return 0;
  454. }
  455. }
  456. mpd_uint_zero(result->data, nwords);
  457. return 1;
  458. }
  459. /*
  460. * Reduce memory size for the coefficient to MPD_MINALLOC. In theory,
  461. * realloc may fail even when reducing the memory size. But in that case
  462. * the old memory area is always big enough, so checking for MPD_Malloc_error
  463. * is not imperative.
  464. */
  465. ALWAYS_INLINE void
  466. mpd_minalloc(mpd_t *result)
  467. {
  468. assert(!mpd_isconst_data(result)); /* illegal operation for a const */
  469. assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
  470. if (!mpd_isstatic_data(result) && result->alloc > MPD_MINALLOC) {
  471. uint8_t err = 0;
  472. result->data = mpd_realloc(result->data, MPD_MINALLOC,
  473. sizeof *result->data, &err);
  474. if (!err) {
  475. result->alloc = MPD_MINALLOC;
  476. }
  477. }
  478. }
  479. int
  480. mpd_resize(mpd_t *result, mpd_ssize_t nwords, mpd_context_t *ctx)
  481. {
  482. uint32_t status = 0;
  483. if (!mpd_qresize(result, nwords, &status)) {
  484. mpd_addstatus_raise(ctx, status);
  485. return 0;
  486. }
  487. return 1;
  488. }
  489. int
  490. mpd_resize_zero(mpd_t *result, mpd_ssize_t nwords, mpd_context_t *ctx)
  491. {
  492. uint32_t status = 0;
  493. if (!mpd_qresize_zero(result, nwords, &status)) {
  494. mpd_addstatus_raise(ctx, status);
  495. return 0;
  496. }
  497. return 1;
  498. }
  499. /******************************************************************************/
  500. /* Set attributes of a decimal */
  501. /******************************************************************************/
  502. /* Set digits. Assumption: result->len is initialized and > 0. */
  503. inline void
  504. mpd_setdigits(mpd_t *result)
  505. {
  506. mpd_ssize_t wdigits = mpd_word_digits(mpd_msword(result));
  507. result->digits = wdigits + (result->len-1) * MPD_RDIGITS;
  508. }
  509. /* Set sign */
  510. ALWAYS_INLINE void
  511. mpd_set_sign(mpd_t *result, uint8_t sign)
  512. {
  513. result->flags &= ~MPD_NEG;
  514. result->flags |= sign;
  515. }
  516. /* Copy sign from another decimal */
  517. ALWAYS_INLINE void
  518. mpd_signcpy(mpd_t *result, const mpd_t *a)
  519. {
  520. uint8_t sign = a->flags&MPD_NEG;
  521. result->flags &= ~MPD_NEG;
  522. result->flags |= sign;
  523. }
  524. /* Set infinity */
  525. ALWAYS_INLINE void
  526. mpd_set_infinity(mpd_t *result)
  527. {
  528. result->flags &= ~MPD_SPECIAL;
  529. result->flags |= MPD_INF;
  530. }
  531. /* Set qNaN */
  532. ALWAYS_INLINE void
  533. mpd_set_qnan(mpd_t *result)
  534. {
  535. result->flags &= ~MPD_SPECIAL;
  536. result->flags |= MPD_NAN;
  537. }
  538. /* Set sNaN */
  539. ALWAYS_INLINE void
  540. mpd_set_snan(mpd_t *result)
  541. {
  542. result->flags &= ~MPD_SPECIAL;
  543. result->flags |= MPD_SNAN;
  544. }
  545. /* Set to negative */
  546. ALWAYS_INLINE void
  547. mpd_set_negative(mpd_t *result)
  548. {
  549. result->flags |= MPD_NEG;
  550. }
  551. /* Set to positive */
  552. ALWAYS_INLINE void
  553. mpd_set_positive(mpd_t *result)
  554. {
  555. result->flags &= ~MPD_NEG;
  556. }
  557. /* Set to dynamic */
  558. ALWAYS_INLINE void
  559. mpd_set_dynamic(mpd_t *result)
  560. {
  561. result->flags &= ~MPD_STATIC;
  562. }
  563. /* Set to static */
  564. ALWAYS_INLINE void
  565. mpd_set_static(mpd_t *result)
  566. {
  567. result->flags |= MPD_STATIC;
  568. }
  569. /* Set data to dynamic */
  570. ALWAYS_INLINE void
  571. mpd_set_dynamic_data(mpd_t *result)
  572. {
  573. result->flags &= ~MPD_DATAFLAGS;
  574. }
  575. /* Set data to static */
  576. ALWAYS_INLINE void
  577. mpd_set_static_data(mpd_t *result)
  578. {
  579. result->flags &= ~MPD_DATAFLAGS;
  580. result->flags |= MPD_STATIC_DATA;
  581. }
  582. /* Set data to shared */
  583. ALWAYS_INLINE void
  584. mpd_set_shared_data(mpd_t *result)
  585. {
  586. result->flags &= ~MPD_DATAFLAGS;
  587. result->flags |= MPD_SHARED_DATA;
  588. }
  589. /* Set data to const */
  590. ALWAYS_INLINE void
  591. mpd_set_const_data(mpd_t *result)
  592. {
  593. result->flags &= ~MPD_DATAFLAGS;
  594. result->flags |= MPD_CONST_DATA;
  595. }
  596. /* Clear flags, preserving memory attributes. */
  597. ALWAYS_INLINE void
  598. mpd_clear_flags(mpd_t *result)
  599. {
  600. result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
  601. }
  602. /* Set flags, preserving memory attributes. */
  603. ALWAYS_INLINE void
  604. mpd_set_flags(mpd_t *result, uint8_t flags)
  605. {
  606. result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
  607. result->flags |= flags;
  608. }
  609. /* Copy flags, preserving memory attributes of result. */
  610. ALWAYS_INLINE void
  611. mpd_copy_flags(mpd_t *result, const mpd_t *a)
  612. {
  613. uint8_t aflags = a->flags;
  614. result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
  615. result->flags |= (aflags & ~(MPD_STATIC|MPD_DATAFLAGS));
  616. }
  617. /* Initialize a workcontext from ctx. Set traps, flags and newtrap to 0. */
  618. static inline void
  619. mpd_workcontext(mpd_context_t *workctx, const mpd_context_t *ctx)
  620. {
  621. workctx->prec = ctx->prec;
  622. workctx->emax = ctx->emax;
  623. workctx->emin = ctx->emin;
  624. workctx->round = ctx->round;
  625. workctx->traps = 0;
  626. workctx->status = 0;
  627. workctx->newtrap = 0;
  628. workctx->clamp = ctx->clamp;
  629. workctx->allcr = ctx->allcr;
  630. }
  631. /******************************************************************************/
  632. /* Getting and setting parts of decimals */
  633. /******************************************************************************/
  634. /* Flip the sign of a decimal */
  635. static inline void
  636. _mpd_negate(mpd_t *dec)
  637. {
  638. dec->flags ^= MPD_NEG;
  639. }
  640. /* Set coefficient to zero */
  641. void
  642. mpd_zerocoeff(mpd_t *result)
  643. {
  644. mpd_minalloc(result);
  645. result->digits = 1;
  646. result->len = 1;
  647. result->data[0] = 0;
  648. }
  649. /* Set the coefficient to all nines. */
  650. void
  651. mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
  652. {
  653. mpd_ssize_t len, r;
  654. _mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
  655. len = (r == 0) ? len : len+1;
  656. if (!mpd_qresize(result, len, status)) {
  657. return;
  658. }
  659. result->len = len;
  660. result->digits = ctx->prec;
  661. --len;
  662. if (r > 0) {
  663. result->data[len--] = mpd_pow10[r]-1;
  664. }
  665. for (; len >= 0; --len) {
  666. result->data[len] = MPD_RADIX-1;
  667. }
  668. }
  669. /*
  670. * Cut off the most significant digits so that the rest fits in ctx->prec.
  671. * Cannot fail.
  672. */
  673. static void
  674. _mpd_cap(mpd_t *result, const mpd_context_t *ctx)
  675. {
  676. uint32_t dummy;
  677. mpd_ssize_t len, r;
  678. if (result->len > 0 && result->digits > ctx->prec) {
  679. _mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
  680. len = (r == 0) ? len : len+1;
  681. if (r != 0) {
  682. result->data[len-1] %= mpd_pow10[r];
  683. }
  684. len = _mpd_real_size(result->data, len);
  685. /* resize to fewer words cannot fail */
  686. mpd_qresize(result, len, &dummy);
  687. result->len = len;
  688. mpd_setdigits(result);
  689. }
  690. if (mpd_iszero(result)) {
  691. _settriple(result, mpd_sign(result), 0, result->exp);
  692. }
  693. }
  694. /*
  695. * Cut off the most significant digits of a NaN payload so that the rest
  696. * fits in ctx->prec - ctx->clamp. Cannot fail.
  697. */
  698. static void
  699. _mpd_fix_nan(mpd_t *result, const mpd_context_t *ctx)
  700. {
  701. uint32_t dummy;
  702. mpd_ssize_t prec;
  703. mpd_ssize_t len, r;
  704. prec = ctx->prec - ctx->clamp;
  705. if (result->len > 0 && result->digits > prec) {
  706. if (prec == 0) {
  707. mpd_minalloc(result);
  708. result->len = result->digits = 0;
  709. }
  710. else {
  711. _mpd_idiv_word(&len, &r, prec, MPD_RDIGITS);
  712. len = (r == 0) ? len : len+1;
  713. if (r != 0) {
  714. result->data[len-1] %= mpd_pow10[r];
  715. }
  716. len = _mpd_real_size(result->data, len);
  717. /* resize to fewer words cannot fail */
  718. mpd_qresize(result, len, &dummy);
  719. result->len = len;
  720. mpd_setdigits(result);
  721. if (mpd_iszerocoeff(result)) {
  722. /* NaN0 is not a valid representation */
  723. result->len = result->digits = 0;
  724. }
  725. }
  726. }
  727. }
  728. /*
  729. * Get n most significant digits from a decimal, where 0 < n <= MPD_UINT_DIGITS.
  730. * Assumes MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for 32 and 64 bit
  731. * machines.
  732. *
  733. * The result of the operation will be in lo. If the operation is impossible,
  734. * hi will be nonzero. This is used to indicate an error.
  735. */
  736. static inline void
  737. _mpd_get_msdigits(mpd_uint_t *hi, mpd_uint_t *lo, const mpd_t *dec,
  738. unsigned int n)
  739. {
  740. mpd_uint_t r, tmp;
  741. assert(0 < n && n <= MPD_RDIGITS+1);
  742. _mpd_div_word(&tmp, &r, dec->digits, MPD_RDIGITS);
  743. r = (r == 0) ? MPD_RDIGITS : r; /* digits in the most significant word */
  744. *hi = 0;
  745. *lo = dec->data[dec->len-1];
  746. if (n <= r) {
  747. *lo /= mpd_pow10[r-n];
  748. }
  749. else if (dec->len > 1) {
  750. /* at this point 1 <= r < n <= MPD_RDIGITS+1 */
  751. _mpd_mul_words(hi, lo, *lo, mpd_pow10[n-r]);
  752. tmp = dec->data[dec->len-2] / mpd_pow10[MPD_RDIGITS-(n-r)];
  753. *lo = *lo + tmp;
  754. if (*lo < tmp) (*hi)++;
  755. }
  756. }
  757. /******************************************************************************/
  758. /* Gathering information about a decimal */
  759. /******************************************************************************/
  760. /* The real size of the coefficient without leading zero words. */
  761. static inline mpd_ssize_t
  762. _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size)
  763. {
  764. while (size > 1 && data[size-1] == 0) {
  765. size--;
  766. }
  767. return size;
  768. }
  769. /* Return number of trailing zeros. No errors are possible. */
  770. mpd_ssize_t
  771. mpd_trail_zeros(const mpd_t *dec)
  772. {
  773. mpd_uint_t word;
  774. mpd_ssize_t i, tz = 0;
  775. for (i=0; i < dec->len; ++i) {
  776. if (dec->data[i] != 0) {
  777. word = dec->data[i];
  778. tz = i * MPD_RDIGITS;
  779. while (word % 10 == 0) {
  780. word /= 10;
  781. tz++;
  782. }
  783. break;
  784. }
  785. }
  786. return tz;
  787. }
  788. /* Integer: Undefined for specials */
  789. static int
  790. _mpd_isint(const mpd_t *dec)
  791. {
  792. mpd_ssize_t tz;
  793. if (mpd_iszerocoeff(dec)) {
  794. return 1;
  795. }
  796. tz = mpd_trail_zeros(dec);
  797. return (dec->exp + tz >= 0);
  798. }
  799. /* Integer */
  800. int
  801. mpd_isinteger(const mpd_t *dec)
  802. {
  803. if (mpd_isspecial(dec)) {
  804. return 0;
  805. }
  806. return _mpd_isint(dec);
  807. }
  808. /* Word is a power of 10 */
  809. static int
  810. mpd_word_ispow10(mpd_uint_t word)
  811. {
  812. int n;
  813. n = mpd_word_digits(word);
  814. if (word == mpd_pow10[n-1]) {
  815. return 1;
  816. }
  817. return 0;
  818. }
  819. /* Coefficient is a power of 10 */
  820. static int
  821. mpd_coeff_ispow10(const mpd_t *dec)
  822. {
  823. if (mpd_word_ispow10(mpd_msword(dec))) {
  824. if (_mpd_isallzero(dec->data, dec->len-1)) {
  825. return 1;
  826. }
  827. }
  828. return 0;
  829. }
  830. /* All digits of a word are nines */
  831. static int
  832. mpd_word_isallnine(mpd_uint_t word)
  833. {
  834. int n;
  835. n = mpd_word_digits(word);
  836. if (word == mpd_pow10[n]-1) {
  837. return 1;
  838. }
  839. return 0;
  840. }
  841. /* All digits of the coefficient are nines */
  842. static int
  843. mpd_coeff_isallnine(const mpd_t *dec)
  844. {
  845. if (mpd_word_isallnine(mpd_msword(dec))) {
  846. if (_mpd_isallnine(dec->data, dec->len-1)) {
  847. return 1;
  848. }
  849. }
  850. return 0;
  851. }
  852. /* Odd decimal: Undefined for non-integers! */
  853. int
  854. mpd_isodd(const mpd_t *dec)
  855. {
  856. mpd_uint_t q, r;
  857. assert(mpd_isinteger(dec));
  858. if (mpd_iszerocoeff(dec)) return 0;
  859. if (dec->exp < 0) {
  860. _mpd_div_word(&q, &r, -dec->exp, MPD_RDIGITS);
  861. q = dec->data[q] / mpd_pow10[r];
  862. return mpd_isoddword(q);
  863. }
  864. return dec->exp == 0 && mpd_isoddword(dec->data[0]);
  865. }
  866. /* Even: Undefined for non-integers! */
  867. int
  868. mpd_iseven(const mpd_t *dec)
  869. {
  870. return !mpd_isodd(dec);
  871. }
  872. /******************************************************************************/
  873. /* Getting and setting decimals */
  874. /******************************************************************************/
  875. /* Internal function: Set a static decimal from a triple, no error checking. */
  876. static void
  877. _ssettriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
  878. {
  879. mpd_set_flags(result, sign);
  880. result->exp = exp;
  881. _mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
  882. result->len = (result->data[1] == 0) ? 1 : 2;
  883. mpd_setdigits(result);
  884. }
  885. /* Internal function: Set a decimal from a triple, no error checking. */
  886. static void
  887. _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
  888. {
  889. mpd_minalloc(result);
  890. mpd_set_flags(result, sign);
  891. result->exp = exp;
  892. _mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
  893. result->len = (result->data[1] == 0) ? 1 : 2;
  894. mpd_setdigits(result);
  895. }
  896. /* Set a special number from a triple */
  897. void
  898. mpd_setspecial(mpd_t *result, uint8_t sign, uint8_t type)
  899. {
  900. mpd_minalloc(result);
  901. result->flags &= ~(MPD_NEG|MPD_SPECIAL);
  902. result->flags |= (sign|type);
  903. result->exp = result->digits = result->len = 0;
  904. }
  905. /* Set result of NaN with an error status */
  906. void
  907. mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status)
  908. {
  909. mpd_minalloc(result);
  910. mpd_set_qnan(result);
  911. mpd_set_positive(result);
  912. result->exp = result->digits = result->len = 0;
  913. *status |= flags;
  914. }
  915. /* quietly set a static decimal from an mpd_ssize_t */
  916. void
  917. mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx,
  918. uint32_t *status)
  919. {
  920. mpd_uint_t u;
  921. uint8_t sign = MPD_POS;
  922. if (a < 0) {
  923. if (a == MPD_SSIZE_MIN) {
  924. u = (mpd_uint_t)MPD_SSIZE_MAX +
  925. (-(MPD_SSIZE_MIN+MPD_SSIZE_MAX));
  926. }
  927. else {
  928. u = -a;
  929. }
  930. sign = MPD_NEG;
  931. }
  932. else {
  933. u = a;
  934. }
  935. _ssettriple(result, sign, u, 0);
  936. mpd_qfinalize(result, ctx, status);
  937. }
  938. /* quietly set a static decimal from an mpd_uint_t */
  939. void
  940. mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx,
  941. uint32_t *status)
  942. {
  943. _ssettriple(result, MPD_POS, a, 0);
  944. mpd_qfinalize(result, ctx, status);
  945. }
  946. /* quietly set a static decimal from an int32_t */
  947. void
  948. mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx,
  949. uint32_t *status)
  950. {
  951. mpd_qsset_ssize(result, a, ctx, status);
  952. }
  953. /* quietly set a static decimal from a uint32_t */
  954. void
  955. mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx,
  956. uint32_t *status)
  957. {
  958. mpd_qsset_uint(result, a, ctx, status);
  959. }
  960. #ifdef CONFIG_64
  961. /* quietly set a static decimal from an int64_t */
  962. void
  963. mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
  964. uint32_t *status)
  965. {
  966. mpd_qsset_ssize(result, a, ctx, status);
  967. }
  968. /* quietly set a static decimal from a uint64_t */
  969. void
  970. mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
  971. uint32_t *status)
  972. {
  973. mpd_qsset_uint(result, a, ctx, status);
  974. }
  975. #endif
  976. /* quietly set a decimal from an mpd_ssize_t */
  977. void
  978. mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx,
  979. uint32_t *status)
  980. {
  981. mpd_minalloc(result);
  982. mpd_qsset_ssize(result, a, ctx, status);
  983. }
  984. /* quietly set a decimal from an mpd_uint_t */
  985. void
  986. mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx,
  987. uint32_t *status)
  988. {
  989. _settriple(result, MPD_POS, a, 0);
  990. mpd_qfinalize(result, ctx, status);
  991. }
  992. /* quietly set a decimal from an int32_t */
  993. void
  994. mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx,
  995. uint32_t *status)
  996. {
  997. mpd_qset_ssize(result, a, ctx, status);
  998. }
  999. /* quietly set a decimal from a uint32_t */
  1000. void
  1001. mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx,
  1002. uint32_t *status)
  1003. {
  1004. mpd_qset_uint(result, a, ctx, status);
  1005. }
  1006. #if defined(CONFIG_32) && !defined(LEGACY_COMPILER)
  1007. /* set a decimal from a uint64_t */
  1008. static void
  1009. _c32setu64(mpd_t *result, uint64_t u, uint8_t sign, uint32_t *status)
  1010. {
  1011. mpd_uint_t w[3];
  1012. uint64_t q;
  1013. int i, len;
  1014. len = 0;
  1015. do {
  1016. q = u / MPD_RADIX;
  1017. w[len] = (mpd_uint_t)(u - q * MPD_RADIX);
  1018. u = q; len++;
  1019. } while (u != 0);
  1020. if (!mpd_qresize(result, len, status)) {
  1021. return;
  1022. }
  1023. for (i = 0; i < len; i++) {
  1024. result->data[i] = w[i];
  1025. }
  1026. mpd_set_sign(result, sign);
  1027. result->exp = 0;
  1028. result->len = len;
  1029. mpd_setdigits(result);
  1030. }
  1031. static void
  1032. _c32_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
  1033. uint32_t *status)
  1034. {
  1035. _c32setu64(result, a, MPD_POS, status);
  1036. mpd_qfinalize(result, ctx, status);
  1037. }
  1038. /* set a decimal from an int64_t */
  1039. static void
  1040. _c32_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
  1041. uint32_t *status)
  1042. {
  1043. uint64_t u;
  1044. uint8_t sign = MPD_POS;
  1045. if (a < 0) {
  1046. if (a == INT64_MIN) {
  1047. u = (uint64_t)INT64_MAX + (-(INT64_MIN+INT64_MAX));
  1048. }
  1049. else {
  1050. u = -a;
  1051. }
  1052. sign = MPD_NEG;
  1053. }
  1054. else {
  1055. u = a;
  1056. }
  1057. _c32setu64(result, u, sign, status);
  1058. mpd_qfinalize(result, ctx, status);
  1059. }
  1060. #endif /* CONFIG_32 && !LEGACY_COMPILER */
  1061. #ifndef LEGACY_COMPILER
  1062. /* quietly set a decimal from an int64_t */
  1063. void
  1064. mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
  1065. uint32_t *status)
  1066. {
  1067. #ifdef CONFIG_64
  1068. mpd_qset_ssize(result, a, ctx, status);
  1069. #else
  1070. _c32_qset_i64(result, a, ctx, status);
  1071. #endif
  1072. }
  1073. /* quietly set a decimal from a uint64_t */
  1074. void
  1075. mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
  1076. uint32_t *status)
  1077. {
  1078. #ifdef CONFIG_64
  1079. mpd_qset_uint(result, a, ctx, status);
  1080. #else
  1081. _c32_qset_u64(result, a, ctx, status);
  1082. #endif
  1083. }
  1084. #endif /* !LEGACY_COMPILER */
  1085. /*
  1086. * Quietly get an mpd_uint_t from a decimal. Assumes
  1087. * MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for
  1088. * 32 and 64 bit machines.
  1089. *
  1090. * If the operation is impossible, MPD_Invalid_operation is set.
  1091. */
  1092. static mpd_uint_t
  1093. _mpd_qget_uint(int use_sign, const mpd_t *a, uint32_t *status)
  1094. {
  1095. mpd_t tmp;
  1096. mpd_uint_t tmp_data[2];
  1097. mpd_uint_t lo, hi;
  1098. if (mpd_isspecial(a)) {
  1099. *status |= MPD_Invalid_operation;
  1100. return MPD_UINT_MAX;
  1101. }
  1102. if (mpd_iszero(a)) {
  1103. return 0;
  1104. }
  1105. if (use_sign && mpd_isnegative(a)) {
  1106. *status |= MPD_Invalid_operation;
  1107. return MPD_UINT_MAX;
  1108. }
  1109. if (a->digits+a->exp > MPD_RDIGITS+1) {
  1110. *status |= MPD_Invalid_operation;
  1111. return MPD_UINT_MAX;
  1112. }
  1113. if (a->exp < 0) {
  1114. if (!_mpd_isint(a)) {
  1115. *status |= MPD_Invalid_operation;
  1116. return MPD_UINT_MAX;
  1117. }
  1118. /* At this point a->digits+a->exp <= MPD_RDIGITS+1,
  1119. * so the shift fits. */
  1120. tmp.data = tmp_data;
  1121. tmp.flags = MPD_STATIC|MPD_STATIC_DATA;
  1122. tmp.alloc = 2;
  1123. mpd_qsshiftr(&tmp, a, -a->exp);
  1124. tmp.exp = 0;
  1125. a = &tmp;
  1126. }
  1127. _mpd_get_msdigits(&hi, &lo, a, MPD_RDIGITS+1);
  1128. if (hi) {
  1129. *status |= MPD_Invalid_operation;
  1130. return MPD_UINT_MAX;
  1131. }
  1132. if (a->exp > 0) {
  1133. _mpd_mul_words(&hi, &lo, lo, mpd_pow10[a->exp]);
  1134. if (hi) {
  1135. *status |= MPD_Invalid_operation;
  1136. return MPD_UINT_MAX;
  1137. }
  1138. }
  1139. return lo;
  1140. }
  1141. /*
  1142. * Sets Invalid_operation for:
  1143. * - specials
  1144. * - negative numbers (except negative zero)
  1145. * - non-integers
  1146. * - overflow
  1147. */
  1148. mpd_uint_t
  1149. mpd_qget_uint(const mpd_t *a, uint32_t *status)
  1150. {
  1151. return _mpd_qget_uint(1, a, status);
  1152. }
  1153. /* Same as above, but gets the absolute value, i.e. the sign is ignored. */
  1154. mpd_uint_t
  1155. mpd_qabs_uint(const mpd_t *a, uint32_t *status)
  1156. {
  1157. return _mpd_qget_uint(0, a, status);
  1158. }
  1159. /* quietly get an mpd_ssize_t from a decimal */
  1160. mpd_ssize_t
  1161. mpd_qget_ssize(const mpd_t *a, uint32_t *status)
  1162. {
  1163. mpd_uint_t u;
  1164. int isneg;
  1165. u = mpd_qabs_uint(a, status);
  1166. if (*status&MPD_Invalid_operation) {
  1167. return MPD_SSIZE_MAX;
  1168. }
  1169. isneg = mpd_isnegative(a);
  1170. if (u <= MPD_SSIZE_MAX) {
  1171. return isneg ? -((mpd_ssize_t)u) : (mpd_ssize_t)u;
  1172. }
  1173. else if (isneg && u+(MPD_SSIZE_MIN+MPD_SSIZE_MAX) == MPD_SSIZE_MAX) {
  1174. return MPD_SSIZE_MIN;
  1175. }
  1176. *status |= MPD_Invalid_operation;
  1177. return MPD_SSIZE_MAX;
  1178. }
  1179. #if defined(CONFIG_32) && !defined(LEGACY_COMPILER)
  1180. /*
  1181. * Quietly get a uint64_t from a decimal. If the operation is impossible,
  1182. * MPD_Invalid_operation is set.
  1183. */
  1184. static uint64_t
  1185. _c32_qget_u64(int use_sign, const mpd_t *a, uint32_t *status)
  1186. {
  1187. MPD_NEW_STATIC(tmp,0,0,20,3);
  1188. mpd_context_t maxcontext;
  1189. uint64_t ret;
  1190. tmp_data[0] = 709551615;
  1191. tmp_data[1] = 446744073;
  1192. tmp_data[2] = 18;
  1193. if (mpd_isspecial(a)) {
  1194. *status |= MPD_Invalid_operation;
  1195. return UINT64_MAX;
  1196. }
  1197. if (mpd_iszero(a)) {
  1198. return 0;
  1199. }
  1200. if (use_sign && mpd_isnegative(a)) {
  1201. *status |= MPD_Invalid_operation;
  1202. return UINT64_MAX;
  1203. }
  1204. if (!_mpd_isint(a)) {
  1205. *status |= MPD_Invalid_operation;
  1206. return UINT64_MAX;
  1207. }
  1208. if (_mpd_cmp_abs(a, &tmp) > 0) {
  1209. *status |= MPD_Invalid_operation;
  1210. return UINT64_MAX;
  1211. }
  1212. mpd_maxcontext(&maxcontext);
  1213. mpd_qrescale(&tmp, a, 0, &maxcontext, &maxcontext.status);
  1214. maxcontext.status &= ~MPD_Rounded;
  1215. if (maxcontext.status != 0) {
  1216. *status |= (maxcontext.status|MPD_Invalid_operation); /* GCOV_NOT_REACHED */
  1217. return UINT64_MAX; /* GCOV_NOT_REACHED */
  1218. }
  1219. ret = 0;
  1220. switch (tmp.len) {
  1221. case 3:
  1222. ret += (uint64_t)tmp_data[2] * 1000000000000000000ULL;
  1223. case 2:
  1224. ret += (uint64_t)tmp_data[1] * 1000000000ULL;
  1225. case 1:
  1226. ret += tmp_data[0];
  1227. break;
  1228. default:
  1229. abort(); /* GCOV_NOT_REACHED */
  1230. }
  1231. return ret;
  1232. }
  1233. static int64_t
  1234. _c32_qget_i64(const mpd_t *a, uint32_t *status)
  1235. {
  1236. uint64_t u;
  1237. int isneg;
  1238. u = _c32_qget_u64(0, a, status);
  1239. if (*status&MPD_Invalid_operation) {
  1240. return INT64_MAX;
  1241. }
  1242. isneg = mpd_isnegative(a);
  1243. if (u <= INT64_MAX) {
  1244. return isneg ? -((int64_t)u) : (int64_t)u;
  1245. }
  1246. else if (isneg && u+(INT64_MIN+INT64_MAX) == INT64_MAX) {
  1247. return INT64_MIN;
  1248. }
  1249. *status |= MPD_Invalid_operation;
  1250. return INT64_MAX;
  1251. }
  1252. #endif /* CONFIG_32 && !LEGACY_COMPILER */
  1253. #ifdef CONFIG_64
  1254. /* quietly get a uint64_t from a decimal */
  1255. uint64_t
  1256. mpd_qget_u64(const mpd_t *a, uint32_t *status)
  1257. {
  1258. return mpd_qget_uint(a, status);
  1259. }
  1260. /* quietly get an int64_t from a decimal */
  1261. int64_t
  1262. mpd_qget_i64(const mpd_t *a, uint32_t *status)
  1263. {
  1264. return mpd_qget_ssize(a, status);
  1265. }
  1266. /* quietly get a uint32_t from a decimal */
  1267. uint32_t
  1268. mpd_qget_u32(const mpd_t *a, uint32_t *status)
  1269. {
  1270. uint64_t x = mpd_qget_uint(a, status);
  1271. if (*status&MPD_Invalid_operation) {
  1272. return UINT32_MAX;
  1273. }
  1274. if (x > UINT32_MAX) {
  1275. *status |= MPD_Invalid_operation;
  1276. return UINT32_MAX;
  1277. }
  1278. return (uint32_t)x;
  1279. }
  1280. /* quietly get an int32_t from a decimal */
  1281. int32_t
  1282. mpd_qget_i32(const mpd_t *a, uint32_t *status)
  1283. {
  1284. int64_t x = mpd_qget_ssize(a, status);
  1285. if (*status&MPD_Invalid_operation) {
  1286. return INT32_MAX;
  1287. }
  1288. if (x < INT32_MIN || x > INT32_MAX) {
  1289. *status |= MPD_Invalid_operation;
  1290. return INT32_MAX;
  1291. }
  1292. return (int32_t)x;
  1293. }
  1294. #else
  1295. #ifndef LEGACY_COMPILER
  1296. /* quietly get a uint64_t from a decimal */
  1297. uint64_t
  1298. mpd_qget_u64(const mpd_t *a, uint32_t *status)
  1299. {
  1300. return _c32_qget_u64(1, a, status);
  1301. }
  1302. /* quietly get an int64_t from a decimal */
  1303. int64_t
  1304. mpd_qget_i64(const mpd_t *a, uint32_t *status)
  1305. {
  1306. return _c32_qget_i64(a, status);
  1307. }
  1308. #endif
  1309. /* quietly get a uint32_t from a decimal */
  1310. uint32_t
  1311. mpd_qget_u32(const mpd_t *a, uint32_t *status)
  1312. {
  1313. return mpd_qget_uint(a, status);
  1314. }
  1315. /* quietly get an int32_t from a decimal */
  1316. int32_t
  1317. mpd_qget_i32(const mpd_t *a, uint32_t *status)
  1318. {
  1319. return mpd_qget_ssize(a, status);
  1320. }
  1321. #endif
  1322. /******************************************************************************/
  1323. /* Filtering input of functions, finalizing output of functions */
  1324. /******************************************************************************/
  1325. /*
  1326. * Check if the operand is NaN, copy to result and return 1 if this is
  1327. * the case. Copying can fail since NaNs are allowed to have a payload that
  1328. * does not fit in MPD_MINALLOC.
  1329. */
  1330. int
  1331. mpd_qcheck_nan(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  1332. uint32_t *status)
  1333. {
  1334. if (mpd_isnan(a)) {
  1335. *status |= mpd_issnan(a) ? MPD_Invalid_operation : 0;
  1336. mpd_qcopy(result, a, status);
  1337. mpd_set_qnan(result);
  1338. _mpd_fix_nan(result, ctx);
  1339. return 1;
  1340. }
  1341. return 0;
  1342. }
  1343. /*
  1344. * Check if either operand is NaN, copy to result and return 1 if this
  1345. * is the case. Copying can fail since NaNs are allowed to have a payload
  1346. * that does not fit in MPD_MINALLOC.
  1347. */
  1348. int
  1349. mpd_qcheck_nans(mpd_t *result, const mpd_t *a, const mpd_t *b,
  1350. const mpd_context_t *ctx, uint32_t *status)
  1351. {
  1352. if ((a->flags|b->flags)&(MPD_NAN|MPD_SNAN)) {
  1353. const mpd_t *choice = b;
  1354. if (mpd_issnan(a)) {
  1355. choice = a;
  1356. *status |= MPD_Invalid_operation;
  1357. }
  1358. else if (mpd_issnan(b)) {
  1359. *status |= MPD_Invalid_operation;
  1360. }
  1361. else if (mpd_isqnan(a)) {
  1362. choice = a;
  1363. }
  1364. mpd_qcopy(result, choice, status);
  1365. mpd_set_qnan(result);
  1366. _mpd_fix_nan(result, ctx);
  1367. return 1;
  1368. }
  1369. return 0;
  1370. }
  1371. /*
  1372. * Check if one of the operands is NaN, copy to result and return 1 if this
  1373. * is the case. Copying can fail since NaNs are allowed to have a payload
  1374. * that does not fit in MPD_MINALLOC.
  1375. */
  1376. static int
  1377. mpd_qcheck_3nans(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c,
  1378. const mpd_context_t *ctx, uint32_t *status)
  1379. {
  1380. if ((a->flags|b->flags|c->flags)&(MPD_NAN|MPD_SNAN)) {
  1381. const mpd_t *choice = c;
  1382. if (mpd_issnan(a)) {
  1383. choice = a;
  1384. *status |= MPD_Invalid_operation;
  1385. }
  1386. else if (mpd_issnan(b)) {
  1387. choice = b;
  1388. *status |= MPD_Invalid_operation;
  1389. }
  1390. else if (mpd_issnan(c)) {
  1391. *status |= MPD_Invalid_operation;
  1392. }
  1393. else if (mpd_isqnan(a)) {
  1394. choice = a;
  1395. }
  1396. else if (mpd_isqnan(b)) {
  1397. choice = b;
  1398. }
  1399. mpd_qcopy(result, choice, status);
  1400. mpd_set_qnan(result);
  1401. _mpd_fix_nan(result, ctx);
  1402. return 1;
  1403. }
  1404. return 0;
  1405. }
  1406. /* Check if rounding digit 'rnd' leads to an increment. */
  1407. static inline int
  1408. _mpd_rnd_incr(const mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx)
  1409. {
  1410. int ld;
  1411. switch (ctx->round) {
  1412. case MPD_ROUND_DOWN: case MPD_ROUND_TRUNC:
  1413. return 0;
  1414. case MPD_ROUND_HALF_UP:
  1415. return (rnd >= 5);
  1416. case MPD_ROUND_HALF_EVEN:
  1417. return (rnd > 5) || ((rnd == 5) && mpd_isoddcoeff(dec));
  1418. case MPD_ROUND_CEILING:
  1419. return !(rnd == 0 || mpd_isnegative(dec));
  1420. case MPD_ROUND_FLOOR:
  1421. return !(rnd == 0 || mpd_ispositive(dec));
  1422. case MPD_ROUND_HALF_DOWN:
  1423. return (rnd > 5);
  1424. case MPD_ROUND_UP:
  1425. return !(rnd == 0);
  1426. case MPD_ROUND_05UP:
  1427. ld = (int)mpd_lsd(dec->data[0]);
  1428. return (!(rnd == 0) && (ld == 0 || ld == 5));
  1429. default:
  1430. /* Without a valid context, further results will be undefined. */
  1431. return 0; /* GCOV_NOT_REACHED */
  1432. }
  1433. }
  1434. /*
  1435. * Apply rounding to a decimal that has been right-shifted into a full
  1436. * precision decimal. If an increment leads to an overflow of the precision,
  1437. * adjust the coefficient and the exponent and check the new exponent for
  1438. * overflow.
  1439. */
  1440. static inline void
  1441. _mpd_apply_round(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
  1442. uint32_t *status)
  1443. {
  1444. if (_mpd_rnd_incr(dec, rnd, ctx)) {
  1445. /* We have a number with exactly ctx->prec digits. The increment
  1446. * can only lead to an overflow if the decimal is all nines. In
  1447. * that case, the result is a power of ten with prec+1 digits.
  1448. *
  1449. * If the precision is a multiple of MPD_RDIGITS, this situation is
  1450. * detected by _mpd_baseincr returning a carry.
  1451. * If the precision is not a multiple of MPD_RDIGITS, we have to
  1452. * check if the result has one digit too many.
  1453. */
  1454. mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
  1455. if (carry) {
  1456. dec->data[dec->len-1] = mpd_pow10[MPD_RDIGITS-1];
  1457. dec->exp += 1;
  1458. _mpd_check_exp(dec, ctx, status);
  1459. return;
  1460. }
  1461. mpd_setdigits(dec);
  1462. if (dec->digits > ctx->prec) {
  1463. mpd_qshiftr_inplace(dec, 1);
  1464. dec->exp += 1;
  1465. dec->digits = ctx->prec;
  1466. _mpd_check_exp(dec, ctx, status);
  1467. }
  1468. }
  1469. }
  1470. /*
  1471. * Apply rounding to a decimal. Allow overflow of the precision.
  1472. */
  1473. static inline void
  1474. _mpd_apply_round_excess(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
  1475. uint32_t *status)
  1476. {
  1477. if (_mpd_rnd_incr(dec, rnd, ctx)) {
  1478. mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
  1479. if (carry) {
  1480. if (!mpd_qresize(dec, dec->len+1, status)) {
  1481. return;
  1482. }
  1483. dec->data[dec->len] = 1;
  1484. dec->len += 1;
  1485. }
  1486. mpd_setdigits(dec);
  1487. }
  1488. }
  1489. /*
  1490. * Apply rounding to a decimal that has been right-shifted into a decimal
  1491. * with full precision or less. Return failure if an increment would
  1492. * overflow the precision.
  1493. */
  1494. static inline int
  1495. _mpd_apply_round_fit(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
  1496. uint32_t *status)
  1497. {
  1498. if (_mpd_rnd_incr(dec, rnd, ctx)) {
  1499. mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
  1500. if (carry) {
  1501. if (!mpd_qresize(dec, dec->len+1, status)) {
  1502. return 0;
  1503. }
  1504. dec->data[dec->len] = 1;
  1505. dec->len += 1;
  1506. }
  1507. mpd_setdigits(dec);
  1508. if (dec->digits > ctx->prec) {
  1509. mpd_seterror(dec, MPD_Invalid_operation, status);
  1510. return 0;
  1511. }
  1512. }
  1513. return 1;
  1514. }
  1515. /* Check a normal number for overflow, underflow, clamping. If the operand
  1516. is modified, it will be zero, special or (sub)normal with a coefficient
  1517. that fits into the current context precision. */
  1518. static inline void
  1519. _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
  1520. {
  1521. mpd_ssize_t adjexp, etiny, shift;
  1522. int rnd;
  1523. adjexp = mpd_adjexp(dec);
  1524. if (adjexp > ctx->emax) {
  1525. if (mpd_iszerocoeff(dec)) {
  1526. dec->exp = ctx->emax;
  1527. if (ctx->clamp) {
  1528. dec->exp -= (ctx->prec-1);
  1529. }
  1530. mpd_zerocoeff(dec);
  1531. *status |= MPD_Clamped;
  1532. return;
  1533. }
  1534. switch (ctx->round) {
  1535. case MPD_ROUND_HALF_UP: case MPD_ROUND_HALF_EVEN:
  1536. case MPD_ROUND_HALF_DOWN: case MPD_ROUND_UP:
  1537. case MPD_ROUND_TRUNC:
  1538. mpd_setspecial(dec, mpd_sign(dec), MPD_INF);
  1539. break;
  1540. case MPD_ROUND_DOWN: case MPD_ROUND_05UP:
  1541. mpd_qmaxcoeff(dec, ctx, status);
  1542. dec->exp = ctx->emax - ctx->prec + 1;
  1543. break;
  1544. case MPD_ROUND_CEILING:
  1545. if (mpd_isnegative(dec)) {
  1546. mpd_qmaxcoeff(dec, ctx, status);
  1547. dec->exp = ctx->emax - ctx->prec + 1;
  1548. }
  1549. else {
  1550. mpd_setspecial(dec, MPD_POS, MPD_INF);
  1551. }
  1552. break;
  1553. case MPD_ROUND_FLOOR:
  1554. if (mpd_ispositive(dec)) {
  1555. mpd_qmaxcoeff(dec, ctx, status);
  1556. dec->exp = ctx->emax - ctx->prec + 1;
  1557. }
  1558. else {
  1559. mpd_setspecial(dec, MPD_NEG, MPD_INF);
  1560. }
  1561. break;
  1562. default: /* debug */
  1563. abort(); /* GCOV_NOT_REACHED */
  1564. }
  1565. *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
  1566. } /* fold down */
  1567. else if (ctx->clamp && dec->exp > mpd_etop(ctx)) {
  1568. /* At this point adjexp=exp+digits-1 <= emax and exp > etop=emax-prec+1:
  1569. * (1) shift = exp -emax+prec-1 > 0
  1570. * (2) digits+shift = exp+digits-1 - emax + prec <= prec */
  1571. shift = dec->exp - mpd_etop(ctx);
  1572. if (!mpd_qshiftl(dec, dec, shift, status)) {
  1573. return;
  1574. }
  1575. dec->exp -= shift;
  1576. *status |= MPD_Clamped;
  1577. if (!mpd_iszerocoeff(dec) && adjexp < ctx->emin) {
  1578. /* Underflow is impossible, since exp < etiny=emin-prec+1
  1579. * and exp > etop=emax-prec+1 would imply emax < emin. */
  1580. *status |= MPD_Subnormal;
  1581. }
  1582. }
  1583. else if (adjexp < ctx->emin) {
  1584. etiny = mpd_etiny(ctx);
  1585. if (mpd_iszerocoeff(dec)) {
  1586. if (dec->exp < etiny) {
  1587. dec->exp = etiny;
  1588. mpd_zerocoeff(dec);
  1589. *status |= MPD_Clamped;
  1590. }
  1591. return;
  1592. }
  1593. *status |= MPD_Subnormal;
  1594. if (dec->exp < etiny) {
  1595. /* At this point adjexp=exp+digits-1 < emin and exp < etiny=emin-prec+1:
  1596. * (1) shift = emin-prec+1 - exp > 0
  1597. * (2) digits-shift = exp+digits-1 - emin + prec < prec */
  1598. shift = etiny - dec->exp;
  1599. rnd = (int)mpd_qshiftr_inplace(dec, shift);
  1600. dec->exp = etiny;
  1601. /* We always have a spare digit in case of an increment. */
  1602. _mpd_apply_round_excess(dec, rnd, ctx, status);
  1603. *status |= MPD_Rounded;
  1604. if (rnd) {
  1605. *status |= (MPD_Inexact|MPD_Underflow);
  1606. if (mpd_iszerocoeff(dec)) {
  1607. mpd_zerocoeff(dec);
  1608. *status |= MPD_Clamped;
  1609. }
  1610. }
  1611. }
  1612. /* Case exp >= etiny=emin-prec+1:
  1613. * (1) adjexp=exp+digits-1 < emin
  1614. * (2) digits < emin-exp+1 <= prec */
  1615. }
  1616. }
  1617. /* Transcendental functions do not always set Underflow reliably,
  1618. * since they only use as much precision as is necessary for correct
  1619. * rounding. If a result like 1.0000000000e-101 is finalized, there
  1620. * is no rounding digit that would trigger Underflow. But we can
  1621. * assume Inexact, so a short check suffices. */
  1622. static inline void
  1623. mpd_check_underflow(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
  1624. {
  1625. if (mpd_adjexp(dec) < ctx->emin && !mpd_iszero(dec) &&
  1626. dec->exp < mpd_etiny(ctx)) {
  1627. *status |= MPD_Underflow;
  1628. }
  1629. }
  1630. /* Check if a normal number must be rounded after the exponent has been checked. */
  1631. static inline void
  1632. _mpd_check_round(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
  1633. {
  1634. mpd_uint_t rnd;
  1635. mpd_ssize_t shift;
  1636. /* must handle specials: _mpd_check_exp() can produce infinities or NaNs */
  1637. if (mpd_isspecial(dec)) {
  1638. return;
  1639. }
  1640. if (dec->digits > ctx->prec) {
  1641. shift = dec->digits - ctx->prec;
  1642. rnd = mpd_qshiftr_inplace(dec, shift);
  1643. dec->exp += shift;
  1644. _mpd_apply_round(dec, rnd, ctx, status);
  1645. *status |= MPD_Rounded;
  1646. if (rnd) {
  1647. *status |= MPD_Inexact;
  1648. }
  1649. }
  1650. }
  1651. /* Finalize all operations. */
  1652. void
  1653. mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
  1654. {
  1655. if (mpd_isspecial(result)) {
  1656. if (mpd_isnan(result)) {
  1657. _mpd_fix_nan(result, ctx);
  1658. }
  1659. return;
  1660. }
  1661. _mpd_check_exp(result, ctx, status);
  1662. _mpd_check_round(result, ctx, status);
  1663. }
  1664. /******************************************************************************/
  1665. /* Copying */
  1666. /******************************************************************************/
  1667. /* Internal function: Copy a decimal, share data with src: USE WITH CARE! */
  1668. static inline void
  1669. _mpd_copy_shared(mpd_t *dest, const mpd_t *src)
  1670. {
  1671. dest->flags = src->flags;
  1672. dest->exp = src->exp;
  1673. dest->digits = src->digits;
  1674. dest->len = src->len;
  1675. dest->alloc = src->alloc;
  1676. dest->data = src->data;
  1677. mpd_set_shared_data(dest);
  1678. }
  1679. /*
  1680. * Copy a decimal. In case of an error, status is set to MPD_Malloc_error.
  1681. */
  1682. int
  1683. mpd_qcopy(mpd_t *result, const mpd_t *a, uint32_t *status)
  1684. {
  1685. if (result == a) return 1;
  1686. if (!mpd_qresize(result, a->len, status)) {
  1687. return 0;
  1688. }
  1689. mpd_copy_flags(result, a);
  1690. result->exp = a->exp;
  1691. result->digits = a->digits;
  1692. result->len = a->len;
  1693. memcpy(result->data, a->data, a->len * (sizeof *result->data));
  1694. return 1;
  1695. }
  1696. /*
  1697. * Copy to a decimal with a static buffer. The caller has to make sure that
  1698. * the buffer is big enough. Cannot fail.
  1699. */
  1700. static void
  1701. mpd_qcopy_static(mpd_t *result, const mpd_t *a)
  1702. {
  1703. if (result == a) return;
  1704. memcpy(result->data, a->data, a->len * (sizeof *result->data));
  1705. mpd_copy_flags(result, a);
  1706. result->exp = a->exp;
  1707. result->digits = a->digits;
  1708. result->len = a->len;
  1709. }
  1710. /*
  1711. * Return a newly allocated copy of the operand. In case of an error,
  1712. * status is set to MPD_Malloc_error and the return value is NULL.
  1713. */
  1714. mpd_t *
  1715. mpd_qncopy(const mpd_t *a)
  1716. {
  1717. mpd_t *result;
  1718. if ((result = mpd_qnew_size(a->len)) == NULL) {
  1719. return NULL;
  1720. }
  1721. memcpy(result->data, a->data, a->len * (sizeof *result->data));
  1722. mpd_copy_flags(result, a);
  1723. result->exp = a->exp;
  1724. result->digits = a->digits;
  1725. result->len = a->len;
  1726. return result;
  1727. }
  1728. /*
  1729. * Copy a decimal and set the sign to positive. In case of an error, the
  1730. * status is set to MPD_Malloc_error.
  1731. */
  1732. int
  1733. mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status)
  1734. {
  1735. if (!mpd_qcopy(result, a, status)) {
  1736. return 0;
  1737. }
  1738. mpd_set_positive(result);
  1739. return 1;
  1740. }
  1741. /*
  1742. * Copy a decimal and negate the sign. In case of an error, the
  1743. * status is set to MPD_Malloc_error.
  1744. */
  1745. int
  1746. mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status)
  1747. {
  1748. if (!mpd_qcopy(result, a, status)) {
  1749. return 0;
  1750. }
  1751. _mpd_negate(result);
  1752. return 1;
  1753. }
  1754. /*
  1755. * Copy a decimal, setting the sign of the first operand to the sign of the
  1756. * second operand. In case of an error, the status is set to MPD_Malloc_error.
  1757. */
  1758. int
  1759. mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status)
  1760. {
  1761. uint8_t sign_b = mpd_sign(b); /* result may equal b! */
  1762. if (!mpd_qcopy(result, a, status)) {
  1763. return 0;
  1764. }
  1765. mpd_set_sign(result, sign_b);
  1766. return 1;
  1767. }
  1768. /******************************************************************************/
  1769. /* Comparisons */
  1770. /******************************************************************************/
  1771. /*
  1772. * For all functions that compare two operands and return an int the usual
  1773. * convention applies to the return value:
  1774. *
  1775. * -1 if op1 < op2
  1776. * 0 if op1 == op2
  1777. * 1 if op1 > op2
  1778. *
  1779. * INT_MAX for error
  1780. */
  1781. /* Convenience macro. If a and b are not equal, return from the calling
  1782. * function with the correct comparison value. */
  1783. #define CMP_EQUAL_OR_RETURN(a, b) \
  1784. if (a != b) { \
  1785. if (a < b) { \
  1786. return -1; \
  1787. } \
  1788. return 1; \
  1789. }
  1790. /*
  1791. * Compare the data of big and small. This function does the equivalent
  1792. * of first shifting small to the left and then comparing the data of
  1793. * big and small, except that no allocation for the left shift is needed.
  1794. */
  1795. static int
  1796. _mpd_basecmp(mpd_uint_t *big, mpd_uint_t *small, mpd_size_t n, mpd_size_t m,
  1797. mpd_size_t shift)
  1798. {
  1799. #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
  1800. /* spurious uninitialized warnings */
  1801. mpd_uint_t l=l, lprev=lprev, h=h;
  1802. #else
  1803. mpd_uint_t l, lprev, h;
  1804. #endif
  1805. mpd_uint_t q, r;
  1806. mpd_uint_t ph, x;
  1807. assert(m > 0 && n >= m && shift > 0);
  1808. _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS);
  1809. if (r != 0) {
  1810. ph = mpd_pow10[r];
  1811. --m; --n;
  1812. _mpd_divmod_pow10(&h, &lprev, small[m--], MPD_RDIGITS-r);
  1813. if (h != 0) {
  1814. CMP_EQUAL_OR_RETURN(big[n], h)
  1815. --n;
  1816. }
  1817. for (; m != MPD_SIZE_MAX; m--,n--) {
  1818. _mpd_divmod_pow10(&h, &l, small[m], MPD_RDIGITS-r);
  1819. x = ph * lprev + h;
  1820. CMP_EQUAL_OR_RETURN(big[n], x)
  1821. lprev = l;
  1822. }
  1823. x = ph * lprev;
  1824. CMP_EQUAL_OR_RETURN(big[q], x)
  1825. }
  1826. else {
  1827. while (--m != MPD_SIZE_MAX) {
  1828. CMP_EQUAL_OR_RETURN(big[m+q], small[m])
  1829. }
  1830. }
  1831. return !_mpd_isallzero(big, q);
  1832. }
  1833. /* Compare two decimals with the same adjusted exponent. */
  1834. static int
  1835. _mpd_cmp_same_adjexp(const mpd_t *a, const mpd_t *b)
  1836. {
  1837. mpd_ssize_t shift, i;
  1838. if (a->exp != b->exp) {
  1839. /* Cannot wrap: a->exp + a->digits = b->exp + b->digits, so
  1840. * a->exp - b->exp = b->digits - a->digits. */
  1841. shift = a->exp - b->exp;
  1842. if (shift > 0) {
  1843. return -1 * _mpd_basecmp(b->data, a->data, b->len, a->len, shift);
  1844. }
  1845. else {
  1846. return _mpd_basecmp(a->data, b->data, a->len, b->len, -shift);
  1847. }
  1848. }
  1849. /*
  1850. * At this point adjexp(a) == adjexp(b) and a->exp == b->exp,
  1851. * so a->digits == b->digits, therefore a->len == b->len.
  1852. */
  1853. for (i = a->len-1; i >= 0; --i) {
  1854. CMP_EQUAL_OR_RETURN(a->data[i], b->data[i])
  1855. }
  1856. return 0;
  1857. }
  1858. /* Compare two numerical values. */
  1859. static int
  1860. _mpd_cmp(const mpd_t *a, const mpd_t *b)
  1861. {
  1862. mpd_ssize_t adjexp_a, adjexp_b;
  1863. /* equal pointers */
  1864. if (a == b) {
  1865. return 0;
  1866. }
  1867. /* infinities */
  1868. if (mpd_isinfinite(a)) {
  1869. if (mpd_isinfinite(b)) {
  1870. return mpd_isnegative(b) - mpd_isnegative(a);
  1871. }
  1872. return mpd_arith_sign(a);
  1873. }
  1874. if (mpd_isinfinite(b)) {
  1875. return -mpd_arith_sign(b);
  1876. }
  1877. /* zeros */
  1878. if (mpd_iszerocoeff(a)) {
  1879. if (mpd_iszerocoeff(b)) {
  1880. return 0;
  1881. }
  1882. return -mpd_arith_sign(b);
  1883. }
  1884. if (mpd_iszerocoeff(b)) {
  1885. return mpd_arith_sign(a);
  1886. }
  1887. /* different signs */
  1888. if (mpd_sign(a) != mpd_sign(b)) {
  1889. return mpd_sign(b) - mpd_sign(a);
  1890. }
  1891. /* different adjusted exponents */
  1892. adjexp_a = mpd_adjexp(a);
  1893. adjexp_b = mpd_adjexp(b);
  1894. if (adjexp_a != adjexp_b) {
  1895. if (adjexp_a < adjexp_b) {
  1896. return -1 * mpd_arith_sign(a);
  1897. }
  1898. return mpd_arith_sign(a);
  1899. }
  1900. /* same adjusted exponents */
  1901. return _mpd_cmp_same_adjexp(a, b) * mpd_arith_sign(a);
  1902. }
  1903. /* Compare the absolutes of two numerical values. */
  1904. static int
  1905. _mpd_cmp_abs(const mpd_t *a, const mpd_t *b)
  1906. {
  1907. mpd_ssize_t adjexp_a, adjexp_b;
  1908. /* equal pointers */
  1909. if (a == b) {
  1910. return 0;
  1911. }
  1912. /* infinities */
  1913. if (mpd_isinfinite(a)) {
  1914. if (mpd_isinfinite(b)) {
  1915. return 0;
  1916. }
  1917. return 1;
  1918. }
  1919. if (mpd_isinfinite(b)) {
  1920. return -1;
  1921. }
  1922. /* zeros */
  1923. if (mpd_iszerocoeff(a)) {
  1924. if (mpd_iszerocoeff(b)) {
  1925. return 0;
  1926. }
  1927. return -1;
  1928. }
  1929. if (mpd_iszerocoeff(b)) {
  1930. return 1;
  1931. }
  1932. /* different adjusted exponents */
  1933. adjexp_a = mpd_adjexp(a);
  1934. adjexp_b = mpd_adjexp(b);
  1935. if (adjexp_a != adjexp_b) {
  1936. if (adjexp_a < adjexp_b) {
  1937. return -1;
  1938. }
  1939. return 1;
  1940. }
  1941. /* same adjusted exponents */
  1942. return _mpd_cmp_same_adjexp(a, b);
  1943. }
  1944. /* Compare two values and return an integer result. */
  1945. int
  1946. mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status)
  1947. {
  1948. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  1949. if (mpd_isnan(a) || mpd_isnan(b)) {
  1950. *status |= MPD_Invalid_operation;
  1951. return INT_MAX;
  1952. }
  1953. }
  1954. return _mpd_cmp(a, b);
  1955. }
  1956. /*
  1957. * Compare a and b, convert the usual integer result to a decimal and
  1958. * store it in 'result'. For convenience, the integer result of the comparison
  1959. * is returned. Comparisons involving NaNs return NaN/INT_MAX.
  1960. */
  1961. int
  1962. mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b,
  1963. const mpd_context_t *ctx, uint32_t *status)
  1964. {
  1965. int c;
  1966. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  1967. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  1968. return INT_MAX;
  1969. }
  1970. }
  1971. c = _mpd_cmp(a, b);
  1972. _settriple(result, (c < 0), (c != 0), 0);
  1973. return c;
  1974. }
  1975. /* Same as mpd_compare(), but signal for all NaNs, i.e. also for quiet NaNs. */
  1976. int
  1977. mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b,
  1978. const mpd_context_t *ctx, uint32_t *status)
  1979. {
  1980. int c;
  1981. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  1982. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  1983. *status |= MPD_Invalid_operation;
  1984. return INT_MAX;
  1985. }
  1986. }
  1987. c = _mpd_cmp(a, b);
  1988. _settriple(result, (c < 0), (c != 0), 0);
  1989. return c;
  1990. }
  1991. /* Compare the operands using a total order. */
  1992. int
  1993. mpd_cmp_total(const mpd_t *a, const mpd_t *b)
  1994. {
  1995. mpd_t aa, bb;
  1996. int nan_a, nan_b;
  1997. int c;
  1998. if (mpd_sign(a) != mpd_sign(b)) {
  1999. return mpd_sign(b) - mpd_sign(a);
  2000. }
  2001. if (mpd_isnan(a)) {
  2002. c = 1;
  2003. if (mpd_isnan(b)) {
  2004. nan_a = (mpd_isqnan(a)) ? 1 : 0;
  2005. nan_b = (mpd_isqnan(b)) ? 1 : 0;
  2006. if (nan_b == nan_a) {
  2007. if (a->len > 0 && b->len > 0) {
  2008. _mpd_copy_shared(&aa, a);
  2009. _mpd_copy_shared(&bb, b);
  2010. aa.exp = bb.exp = 0;
  2011. /* compare payload */
  2012. c = _mpd_cmp_abs(&aa, &bb);
  2013. }
  2014. else {
  2015. c = (a->len > 0) - (b->len > 0);
  2016. }
  2017. }
  2018. else {
  2019. c = nan_a - nan_b;
  2020. }
  2021. }
  2022. }
  2023. else if (mpd_isnan(b)) {
  2024. c = -1;
  2025. }
  2026. else {
  2027. c = _mpd_cmp_abs(a, b);
  2028. if (c == 0 && a->exp != b->exp) {
  2029. c = (a->exp < b->exp) ? -1 : 1;
  2030. }
  2031. }
  2032. return c * mpd_arith_sign(a);
  2033. }
  2034. /*
  2035. * Compare a and b according to a total order, convert the usual integer result
  2036. * to a decimal and store it in 'result'. For convenience, the integer result
  2037. * of the comparison is returned.
  2038. */
  2039. int
  2040. mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b)
  2041. {
  2042. int c;
  2043. c = mpd_cmp_total(a, b);
  2044. _settriple(result, (c < 0), (c != 0), 0);
  2045. return c;
  2046. }
  2047. /* Compare the magnitude of the operands using a total order. */
  2048. int
  2049. mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b)
  2050. {
  2051. mpd_t aa, bb;
  2052. _mpd_copy_shared(&aa, a);
  2053. _mpd_copy_shared(&bb, b);
  2054. mpd_set_positive(&aa);
  2055. mpd_set_positive(&bb);
  2056. return mpd_cmp_total(&aa, &bb);
  2057. }
  2058. /*
  2059. * Compare the magnitude of a and b according to a total order, convert the
  2060. * the usual integer result to a decimal and store it in 'result'.
  2061. * For convenience, the integer result of the comparison is returned.
  2062. */
  2063. int
  2064. mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b)
  2065. {
  2066. int c;
  2067. c = mpd_cmp_total_mag(a, b);
  2068. _settriple(result, (c < 0), (c != 0), 0);
  2069. return c;
  2070. }
  2071. /* Determine an ordering for operands that are numerically equal. */
  2072. static inline int
  2073. _mpd_cmp_numequal(const mpd_t *a, const mpd_t *b)
  2074. {
  2075. int sign_a, sign_b;
  2076. int c;
  2077. sign_a = mpd_sign(a);
  2078. sign_b = mpd_sign(b);
  2079. if (sign_a != sign_b) {
  2080. c = sign_b - sign_a;
  2081. }
  2082. else {
  2083. c = (a->exp < b->exp) ? -1 : 1;
  2084. c *= mpd_arith_sign(a);
  2085. }
  2086. return c;
  2087. }
  2088. /******************************************************************************/
  2089. /* Shifting the coefficient */
  2090. /******************************************************************************/
  2091. /*
  2092. * Shift the coefficient of the operand to the left, no check for specials.
  2093. * Both operands may be the same pointer. If the result length has to be
  2094. * increased, mpd_qresize() might fail with MPD_Malloc_error.
  2095. */
  2096. int
  2097. mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status)
  2098. {
  2099. mpd_ssize_t size;
  2100. assert(!mpd_isspecial(a));
  2101. assert(n >= 0);
  2102. if (mpd_iszerocoeff(a) || n == 0) {
  2103. return mpd_qcopy(result, a, status);
  2104. }
  2105. size = mpd_digits_to_size(a->digits+n);
  2106. if (!mpd_qresize(result, size, status)) {
  2107. return 0; /* result is NaN */
  2108. }
  2109. _mpd_baseshiftl(result->data, a->data, size, a->len, n);
  2110. mpd_copy_flags(result, a);
  2111. result->exp = a->exp;
  2112. result->digits = a->digits+n;
  2113. result->len = size;
  2114. return 1;
  2115. }
  2116. /* Determine the rounding indicator if all digits of the coefficient are shifted
  2117. * out of the picture. */
  2118. static mpd_uint_t
  2119. _mpd_get_rnd(const mpd_uint_t *data, mpd_ssize_t len, int use_msd)
  2120. {
  2121. mpd_uint_t rnd = 0, rest = 0, word;
  2122. word = data[len-1];
  2123. /* special treatment for the most significant digit if shift == digits */
  2124. if (use_msd) {
  2125. _mpd_divmod_pow10(&rnd, &rest, word, mpd_word_digits(word)-1);
  2126. if (len > 1 && rest == 0) {
  2127. rest = !_mpd_isallzero(data, len-1);
  2128. }
  2129. }
  2130. else {
  2131. rest = !_mpd_isallzero(data, len);
  2132. }
  2133. return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd;
  2134. }
  2135. /*
  2136. * Same as mpd_qshiftr(), but 'result' is an mpd_t with a static coefficient.
  2137. * It is the caller's responsibility to ensure that the coefficient is big
  2138. * enough. The function cannot fail.
  2139. */
  2140. static mpd_uint_t
  2141. mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n)
  2142. {
  2143. mpd_uint_t rnd;
  2144. mpd_ssize_t size;
  2145. assert(!mpd_isspecial(a));
  2146. assert(n >= 0);
  2147. if (mpd_iszerocoeff(a) || n == 0) {
  2148. mpd_qcopy_static(result, a);
  2149. return 0;
  2150. }
  2151. if (n >= a->digits) {
  2152. rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits));
  2153. mpd_zerocoeff(result);
  2154. }
  2155. else {
  2156. result->digits = a->digits-n;
  2157. size = mpd_digits_to_size(result->digits);
  2158. rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
  2159. result->len = size;
  2160. }
  2161. mpd_copy_flags(result, a);
  2162. result->exp = a->exp;
  2163. return rnd;
  2164. }
  2165. /*
  2166. * Inplace shift of the coefficient to the right, no check for specials.
  2167. * Returns the rounding indicator for mpd_rnd_incr().
  2168. * The function cannot fail.
  2169. */
  2170. mpd_uint_t
  2171. mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n)
  2172. {
  2173. uint32_t dummy;
  2174. mpd_uint_t rnd;
  2175. mpd_ssize_t size;
  2176. assert(!mpd_isspecial(result));
  2177. assert(n >= 0);
  2178. if (mpd_iszerocoeff(result) || n == 0) {
  2179. return 0;
  2180. }
  2181. if (n >= result->digits) {
  2182. rnd = _mpd_get_rnd(result->data, result->len, (n==result->digits));
  2183. mpd_zerocoeff(result);
  2184. }
  2185. else {
  2186. rnd = _mpd_baseshiftr(result->data, result->data, result->len, n);
  2187. result->digits -= n;
  2188. size = mpd_digits_to_size(result->digits);
  2189. /* reducing the size cannot fail */
  2190. mpd_qresize(result, size, &dummy);
  2191. result->len = size;
  2192. }
  2193. return rnd;
  2194. }
  2195. /*
  2196. * Shift the coefficient of the operand to the right, no check for specials.
  2197. * Both operands may be the same pointer. Returns the rounding indicator to
  2198. * be used by mpd_rnd_incr(). If the result length has to be increased,
  2199. * mpd_qcopy() or mpd_qresize() might fail with MPD_Malloc_error. In those
  2200. * cases, MPD_UINT_MAX is returned.
  2201. */
  2202. mpd_uint_t
  2203. mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status)
  2204. {
  2205. mpd_uint_t rnd;
  2206. mpd_ssize_t size;
  2207. assert(!mpd_isspecial(a));
  2208. assert(n >= 0);
  2209. if (mpd_iszerocoeff(a) || n == 0) {
  2210. if (!mpd_qcopy(result, a, status)) {
  2211. return MPD_UINT_MAX;
  2212. }
  2213. return 0;
  2214. }
  2215. if (n >= a->digits) {
  2216. rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits));
  2217. mpd_zerocoeff(result);
  2218. }
  2219. else {
  2220. result->digits = a->digits-n;
  2221. size = mpd_digits_to_size(result->digits);
  2222. if (result == a) {
  2223. rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
  2224. /* reducing the size cannot fail */
  2225. mpd_qresize(result, size, status);
  2226. }
  2227. else {
  2228. if (!mpd_qresize(result, size, status)) {
  2229. return MPD_UINT_MAX;
  2230. }
  2231. rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
  2232. }
  2233. result->len = size;
  2234. }
  2235. mpd_copy_flags(result, a);
  2236. result->exp = a->exp;
  2237. return rnd;
  2238. }
  2239. /******************************************************************************/
  2240. /* Miscellaneous operations */
  2241. /******************************************************************************/
  2242. /* Logical And */
  2243. void
  2244. mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2245. const mpd_context_t *ctx, uint32_t *status)
  2246. {
  2247. const mpd_t *big = a, *small = b;
  2248. mpd_uint_t x, y, z, xbit, ybit;
  2249. int k, mswdigits;
  2250. mpd_ssize_t i;
  2251. if (mpd_isspecial(a) || mpd_isspecial(b) ||
  2252. mpd_isnegative(a) || mpd_isnegative(b) ||
  2253. a->exp != 0 || b->exp != 0) {
  2254. mpd_seterror(result, MPD_Invalid_operation, status);
  2255. return;
  2256. }
  2257. if (b->digits > a->digits) {
  2258. big = b;
  2259. small = a;
  2260. }
  2261. if (!mpd_qresize(result, big->len, status)) {
  2262. return;
  2263. }
  2264. /* full words */
  2265. for (i = 0; i < small->len-1; i++) {
  2266. x = small->data[i];
  2267. y = big->data[i];
  2268. z = 0;
  2269. for (k = 0; k < MPD_RDIGITS; k++) {
  2270. xbit = x % 10;
  2271. x /= 10;
  2272. ybit = y % 10;
  2273. y /= 10;
  2274. if (xbit > 1 || ybit > 1) {
  2275. goto invalid_operation;
  2276. }
  2277. z += (xbit&ybit) ? mpd_pow10[k] : 0;
  2278. }
  2279. result->data[i] = z;
  2280. }
  2281. /* most significant word of small */
  2282. x = small->data[i];
  2283. y = big->data[i];
  2284. z = 0;
  2285. mswdigits = mpd_word_digits(x);
  2286. for (k = 0; k < mswdigits; k++) {
  2287. xbit = x % 10;
  2288. x /= 10;
  2289. ybit = y % 10;
  2290. y /= 10;
  2291. if (xbit > 1 || ybit > 1) {
  2292. goto invalid_operation;
  2293. }
  2294. z += (xbit&ybit) ? mpd_pow10[k] : 0;
  2295. }
  2296. result->data[i++] = z;
  2297. /* scan the rest of y for digits > 1 */
  2298. for (; k < MPD_RDIGITS; k++) {
  2299. ybit = y % 10;
  2300. y /= 10;
  2301. if (ybit > 1) {
  2302. goto invalid_operation;
  2303. }
  2304. }
  2305. /* scan the rest of big for digits > 1 */
  2306. for (; i < big->len; i++) {
  2307. y = big->data[i];
  2308. for (k = 0; k < MPD_RDIGITS; k++) {
  2309. ybit = y % 10;
  2310. y /= 10;
  2311. if (ybit > 1) {
  2312. goto invalid_operation;
  2313. }
  2314. }
  2315. }
  2316. mpd_clear_flags(result);
  2317. result->exp = 0;
  2318. result->len = _mpd_real_size(result->data, small->len);
  2319. mpd_qresize(result, result->len, status);
  2320. mpd_setdigits(result);
  2321. _mpd_cap(result, ctx);
  2322. return;
  2323. invalid_operation:
  2324. mpd_seterror(result, MPD_Invalid_operation, status);
  2325. }
  2326. /* Class of an operand. Returns a pointer to the constant name. */
  2327. const char *
  2328. mpd_class(const mpd_t *a, const mpd_context_t *ctx)
  2329. {
  2330. if (mpd_isnan(a)) {
  2331. if (mpd_isqnan(a))
  2332. return "NaN";
  2333. else
  2334. return "sNaN";
  2335. }
  2336. else if (mpd_ispositive(a)) {
  2337. if (mpd_isinfinite(a))
  2338. return "+Infinity";
  2339. else if (mpd_iszero(a))
  2340. return "+Zero";
  2341. else if (mpd_isnormal(a, ctx))
  2342. return "+Normal";
  2343. else
  2344. return "+Subnormal";
  2345. }
  2346. else {
  2347. if (mpd_isinfinite(a))
  2348. return "-Infinity";
  2349. else if (mpd_iszero(a))
  2350. return "-Zero";
  2351. else if (mpd_isnormal(a, ctx))
  2352. return "-Normal";
  2353. else
  2354. return "-Subnormal";
  2355. }
  2356. }
  2357. /* Logical Xor */
  2358. void
  2359. mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  2360. uint32_t *status)
  2361. {
  2362. mpd_uint_t x, z, xbit;
  2363. mpd_ssize_t i, digits, len;
  2364. mpd_ssize_t q, r;
  2365. int k;
  2366. if (mpd_isspecial(a) || mpd_isnegative(a) || a->exp != 0) {
  2367. mpd_seterror(result, MPD_Invalid_operation, status);
  2368. return;
  2369. }
  2370. digits = (a->digits < ctx->prec) ? ctx->prec : a->digits;
  2371. _mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
  2372. len = (r == 0) ? q : q+1;
  2373. if (!mpd_qresize(result, len, status)) {
  2374. return;
  2375. }
  2376. for (i = 0; i < len; i++) {
  2377. x = (i < a->len) ? a->data[i] : 0;
  2378. z = 0;
  2379. for (k = 0; k < MPD_RDIGITS; k++) {
  2380. xbit = x % 10;
  2381. x /= 10;
  2382. if (xbit > 1) {
  2383. goto invalid_operation;
  2384. }
  2385. z += !xbit ? mpd_pow10[k] : 0;
  2386. }
  2387. result->data[i] = z;
  2388. }
  2389. mpd_clear_flags(result);
  2390. result->exp = 0;
  2391. result->len = _mpd_real_size(result->data, len);
  2392. mpd_qresize(result, result->len, status);
  2393. mpd_setdigits(result);
  2394. _mpd_cap(result, ctx);
  2395. return;
  2396. invalid_operation:
  2397. mpd_seterror(result, MPD_Invalid_operation, status);
  2398. }
  2399. /* Exponent of the magnitude of the most significant digit of the operand. */
  2400. void
  2401. mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  2402. uint32_t *status)
  2403. {
  2404. if (mpd_isspecial(a)) {
  2405. if (mpd_qcheck_nan(result, a, ctx, status)) {
  2406. return;
  2407. }
  2408. mpd_setspecial(result, MPD_POS, MPD_INF);
  2409. }
  2410. else if (mpd_iszerocoeff(a)) {
  2411. mpd_setspecial(result, MPD_NEG, MPD_INF);
  2412. *status |= MPD_Division_by_zero;
  2413. }
  2414. else {
  2415. mpd_qset_ssize(result, mpd_adjexp(a), ctx, status);
  2416. }
  2417. }
  2418. /* Logical Or */
  2419. void
  2420. mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2421. const mpd_context_t *ctx, uint32_t *status)
  2422. {
  2423. const mpd_t *big = a, *small = b;
  2424. mpd_uint_t x, y, z, xbit, ybit;
  2425. int k, mswdigits;
  2426. mpd_ssize_t i;
  2427. if (mpd_isspecial(a) || mpd_isspecial(b) ||
  2428. mpd_isnegative(a) || mpd_isnegative(b) ||
  2429. a->exp != 0 || b->exp != 0) {
  2430. mpd_seterror(result, MPD_Invalid_operation, status);
  2431. return;
  2432. }
  2433. if (b->digits > a->digits) {
  2434. big = b;
  2435. small = a;
  2436. }
  2437. if (!mpd_qresize(result, big->len, status)) {
  2438. return;
  2439. }
  2440. /* full words */
  2441. for (i = 0; i < small->len-1; i++) {
  2442. x = small->data[i];
  2443. y = big->data[i];
  2444. z = 0;
  2445. for (k = 0; k < MPD_RDIGITS; k++) {
  2446. xbit = x % 10;
  2447. x /= 10;
  2448. ybit = y % 10;
  2449. y /= 10;
  2450. if (xbit > 1 || ybit > 1) {
  2451. goto invalid_operation;
  2452. }
  2453. z += (xbit|ybit) ? mpd_pow10[k] : 0;
  2454. }
  2455. result->data[i] = z;
  2456. }
  2457. /* most significant word of small */
  2458. x = small->data[i];
  2459. y = big->data[i];
  2460. z = 0;
  2461. mswdigits = mpd_word_digits(x);
  2462. for (k = 0; k < mswdigits; k++) {
  2463. xbit = x % 10;
  2464. x /= 10;
  2465. ybit = y % 10;
  2466. y /= 10;
  2467. if (xbit > 1 || ybit > 1) {
  2468. goto invalid_operation;
  2469. }
  2470. z += (xbit|ybit) ? mpd_pow10[k] : 0;
  2471. }
  2472. /* scan for digits > 1 and copy the rest of y */
  2473. for (; k < MPD_RDIGITS; k++) {
  2474. ybit = y % 10;
  2475. y /= 10;
  2476. if (ybit > 1) {
  2477. goto invalid_operation;
  2478. }
  2479. z += ybit*mpd_pow10[k];
  2480. }
  2481. result->data[i++] = z;
  2482. /* scan for digits > 1 and copy the rest of big */
  2483. for (; i < big->len; i++) {
  2484. y = big->data[i];
  2485. for (k = 0; k < MPD_RDIGITS; k++) {
  2486. ybit = y % 10;
  2487. y /= 10;
  2488. if (ybit > 1) {
  2489. goto invalid_operation;
  2490. }
  2491. }
  2492. result->data[i] = big->data[i];
  2493. }
  2494. mpd_clear_flags(result);
  2495. result->exp = 0;
  2496. result->len = _mpd_real_size(result->data, big->len);
  2497. mpd_qresize(result, result->len, status);
  2498. mpd_setdigits(result);
  2499. _mpd_cap(result, ctx);
  2500. return;
  2501. invalid_operation:
  2502. mpd_seterror(result, MPD_Invalid_operation, status);
  2503. }
  2504. /*
  2505. * Rotate the coefficient of 'a' by 'b' digits. 'b' must be an integer with
  2506. * exponent 0.
  2507. */
  2508. void
  2509. mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2510. const mpd_context_t *ctx, uint32_t *status)
  2511. {
  2512. uint32_t workstatus = 0;
  2513. MPD_NEW_STATIC(tmp,0,0,0,0);
  2514. MPD_NEW_STATIC(big,0,0,0,0);
  2515. MPD_NEW_STATIC(small,0,0,0,0);
  2516. mpd_ssize_t n, lshift, rshift;
  2517. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  2518. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  2519. return;
  2520. }
  2521. }
  2522. if (b->exp != 0 || mpd_isinfinite(b)) {
  2523. mpd_seterror(result, MPD_Invalid_operation, status);
  2524. return;
  2525. }
  2526. n = mpd_qget_ssize(b, &workstatus);
  2527. if (workstatus&MPD_Invalid_operation) {
  2528. mpd_seterror(result, MPD_Invalid_operation, status);
  2529. return;
  2530. }
  2531. if (n > ctx->prec || n < -ctx->prec) {
  2532. mpd_seterror(result, MPD_Invalid_operation, status);
  2533. return;
  2534. }
  2535. if (mpd_isinfinite(a)) {
  2536. mpd_qcopy(result, a, status);
  2537. return;
  2538. }
  2539. if (n >= 0) {
  2540. lshift = n;
  2541. rshift = ctx->prec-n;
  2542. }
  2543. else {
  2544. lshift = ctx->prec+n;
  2545. rshift = -n;
  2546. }
  2547. if (a->digits > ctx->prec) {
  2548. if (!mpd_qcopy(&tmp, a, status)) {
  2549. mpd_seterror(result, MPD_Malloc_error, status);
  2550. goto finish;
  2551. }
  2552. _mpd_cap(&tmp, ctx);
  2553. a = &tmp;
  2554. }
  2555. if (!mpd_qshiftl(&big, a, lshift, status)) {
  2556. mpd_seterror(result, MPD_Malloc_error, status);
  2557. goto finish;
  2558. }
  2559. _mpd_cap(&big, ctx);
  2560. if (mpd_qshiftr(&small, a, rshift, status) == MPD_UINT_MAX) {
  2561. mpd_seterror(result, MPD_Malloc_error, status);
  2562. goto finish;
  2563. }
  2564. _mpd_qadd(result, &big, &small, ctx, status);
  2565. finish:
  2566. mpd_del(&tmp);
  2567. mpd_del(&big);
  2568. mpd_del(&small);
  2569. }
  2570. /*
  2571. * b must be an integer with exponent 0 and in the range +-2*(emax + prec).
  2572. * XXX: In my opinion +-(2*emax + prec) would be more sensible.
  2573. * The result is a with the value of b added to its exponent.
  2574. */
  2575. void
  2576. mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2577. const mpd_context_t *ctx, uint32_t *status)
  2578. {
  2579. uint32_t workstatus = 0;
  2580. mpd_uint_t n, maxjump;
  2581. #ifndef LEGACY_COMPILER
  2582. int64_t exp;
  2583. #else
  2584. mpd_uint_t x;
  2585. int x_sign, n_sign;
  2586. mpd_ssize_t exp;
  2587. #endif
  2588. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  2589. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  2590. return;
  2591. }
  2592. }
  2593. if (b->exp != 0 || mpd_isinfinite(b)) {
  2594. mpd_seterror(result, MPD_Invalid_operation, status);
  2595. return;
  2596. }
  2597. n = mpd_qabs_uint(b, &workstatus);
  2598. /* the spec demands this */
  2599. maxjump = 2 * (mpd_uint_t)(ctx->emax + ctx->prec);
  2600. if (n > maxjump || workstatus&MPD_Invalid_operation) {
  2601. mpd_seterror(result, MPD_Invalid_operation, status);
  2602. return;
  2603. }
  2604. if (mpd_isinfinite(a)) {
  2605. mpd_qcopy(result, a, status);
  2606. return;
  2607. }
  2608. #ifndef LEGACY_COMPILER
  2609. exp = a->exp + (int64_t)n * mpd_arith_sign(b);
  2610. exp = (exp > MPD_EXP_INF) ? MPD_EXP_INF : exp;
  2611. exp = (exp < MPD_EXP_CLAMP) ? MPD_EXP_CLAMP : exp;
  2612. #else
  2613. x = (a->exp < 0) ? -a->exp : a->exp;
  2614. x_sign = (a->exp < 0) ? 1 : 0;
  2615. n_sign = mpd_isnegative(b) ? 1 : 0;
  2616. if (x_sign == n_sign) {
  2617. x = x + n;
  2618. if (x < n) x = MPD_UINT_MAX;
  2619. }
  2620. else {
  2621. x_sign = (x >= n) ? x_sign : n_sign;
  2622. x = (x >= n) ? x - n : n - x;
  2623. }
  2624. if (!x_sign && x > MPD_EXP_INF) x = MPD_EXP_INF;
  2625. if (x_sign && x > -MPD_EXP_CLAMP) x = -MPD_EXP_CLAMP;
  2626. exp = x_sign ? -((mpd_ssize_t)x) : (mpd_ssize_t)x;
  2627. #endif
  2628. mpd_qcopy(result, a, status);
  2629. result->exp = (mpd_ssize_t)exp;
  2630. mpd_qfinalize(result, ctx, status);
  2631. }
  2632. /*
  2633. * Shift the coefficient by n digits, positive n is a left shift. In the case
  2634. * of a left shift, the result is decapitated to fit the context precision. If
  2635. * you don't want that, use mpd_shiftl().
  2636. */
  2637. void
  2638. mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx,
  2639. uint32_t *status)
  2640. {
  2641. if (mpd_isspecial(a)) {
  2642. if (mpd_qcheck_nan(result, a, ctx, status)) {
  2643. return;
  2644. }
  2645. mpd_qcopy(result, a, status);
  2646. return;
  2647. }
  2648. if (n >= 0 && n <= ctx->prec) {
  2649. mpd_qshiftl(result, a, n, status);
  2650. _mpd_cap(result, ctx);
  2651. }
  2652. else if (n < 0 && n >= -ctx->prec) {
  2653. if (!mpd_qcopy(result, a, status)) {
  2654. return;
  2655. }
  2656. _mpd_cap(result, ctx);
  2657. mpd_qshiftr_inplace(result, -n);
  2658. }
  2659. else {
  2660. mpd_seterror(result, MPD_Invalid_operation, status);
  2661. }
  2662. }
  2663. /*
  2664. * Same as mpd_shiftn(), but the shift is specified by the decimal b, which
  2665. * must be an integer with a zero exponent. Infinities remain infinities.
  2666. */
  2667. void
  2668. mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx,
  2669. uint32_t *status)
  2670. {
  2671. uint32_t workstatus = 0;
  2672. mpd_ssize_t n;
  2673. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  2674. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  2675. return;
  2676. }
  2677. }
  2678. if (b->exp != 0 || mpd_isinfinite(b)) {
  2679. mpd_seterror(result, MPD_Invalid_operation, status);
  2680. return;
  2681. }
  2682. n = mpd_qget_ssize(b, &workstatus);
  2683. if (workstatus&MPD_Invalid_operation) {
  2684. mpd_seterror(result, MPD_Invalid_operation, status);
  2685. return;
  2686. }
  2687. if (n > ctx->prec || n < -ctx->prec) {
  2688. mpd_seterror(result, MPD_Invalid_operation, status);
  2689. return;
  2690. }
  2691. if (mpd_isinfinite(a)) {
  2692. mpd_qcopy(result, a, status);
  2693. return;
  2694. }
  2695. if (n >= 0) {
  2696. mpd_qshiftl(result, a, n, status);
  2697. _mpd_cap(result, ctx);
  2698. }
  2699. else {
  2700. if (!mpd_qcopy(result, a, status)) {
  2701. return;
  2702. }
  2703. _mpd_cap(result, ctx);
  2704. mpd_qshiftr_inplace(result, -n);
  2705. }
  2706. }
  2707. /* Logical Xor */
  2708. void
  2709. mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2710. const mpd_context_t *ctx, uint32_t *status)
  2711. {
  2712. const mpd_t *big = a, *small = b;
  2713. mpd_uint_t x, y, z, xbit, ybit;
  2714. int k, mswdigits;
  2715. mpd_ssize_t i;
  2716. if (mpd_isspecial(a) || mpd_isspecial(b) ||
  2717. mpd_isnegative(a) || mpd_isnegative(b) ||
  2718. a->exp != 0 || b->exp != 0) {
  2719. mpd_seterror(result, MPD_Invalid_operation, status);
  2720. return;
  2721. }
  2722. if (b->digits > a->digits) {
  2723. big = b;
  2724. small = a;
  2725. }
  2726. if (!mpd_qresize(result, big->len, status)) {
  2727. return;
  2728. }
  2729. /* full words */
  2730. for (i = 0; i < small->len-1; i++) {
  2731. x = small->data[i];
  2732. y = big->data[i];
  2733. z = 0;
  2734. for (k = 0; k < MPD_RDIGITS; k++) {
  2735. xbit = x % 10;
  2736. x /= 10;
  2737. ybit = y % 10;
  2738. y /= 10;
  2739. if (xbit > 1 || ybit > 1) {
  2740. goto invalid_operation;
  2741. }
  2742. z += (xbit^ybit) ? mpd_pow10[k] : 0;
  2743. }
  2744. result->data[i] = z;
  2745. }
  2746. /* most significant word of small */
  2747. x = small->data[i];
  2748. y = big->data[i];
  2749. z = 0;
  2750. mswdigits = mpd_word_digits(x);
  2751. for (k = 0; k < mswdigits; k++) {
  2752. xbit = x % 10;
  2753. x /= 10;
  2754. ybit = y % 10;
  2755. y /= 10;
  2756. if (xbit > 1 || ybit > 1) {
  2757. goto invalid_operation;
  2758. }
  2759. z += (xbit^ybit) ? mpd_pow10[k] : 0;
  2760. }
  2761. /* scan for digits > 1 and copy the rest of y */
  2762. for (; k < MPD_RDIGITS; k++) {
  2763. ybit = y % 10;
  2764. y /= 10;
  2765. if (ybit > 1) {
  2766. goto invalid_operation;
  2767. }
  2768. z += ybit*mpd_pow10[k];
  2769. }
  2770. result->data[i++] = z;
  2771. /* scan for digits > 1 and copy the rest of big */
  2772. for (; i < big->len; i++) {
  2773. y = big->data[i];
  2774. for (k = 0; k < MPD_RDIGITS; k++) {
  2775. ybit = y % 10;
  2776. y /= 10;
  2777. if (ybit > 1) {
  2778. goto invalid_operation;
  2779. }
  2780. }
  2781. result->data[i] = big->data[i];
  2782. }
  2783. mpd_clear_flags(result);
  2784. result->exp = 0;
  2785. result->len = _mpd_real_size(result->data, big->len);
  2786. mpd_qresize(result, result->len, status);
  2787. mpd_setdigits(result);
  2788. _mpd_cap(result, ctx);
  2789. return;
  2790. invalid_operation:
  2791. mpd_seterror(result, MPD_Invalid_operation, status);
  2792. }
  2793. /******************************************************************************/
  2794. /* Arithmetic operations */
  2795. /******************************************************************************/
  2796. /*
  2797. * The absolute value of a. If a is negative, the result is the same
  2798. * as the result of the minus operation. Otherwise, the result is the
  2799. * result of the plus operation.
  2800. */
  2801. void
  2802. mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  2803. uint32_t *status)
  2804. {
  2805. if (mpd_isspecial(a)) {
  2806. if (mpd_qcheck_nan(result, a, ctx, status)) {
  2807. return;
  2808. }
  2809. }
  2810. if (mpd_isnegative(a)) {
  2811. mpd_qminus(result, a, ctx, status);
  2812. }
  2813. else {
  2814. mpd_qplus(result, a, ctx, status);
  2815. }
  2816. }
  2817. static inline void
  2818. _mpd_ptrswap(const mpd_t **a, const mpd_t **b)
  2819. {
  2820. const mpd_t *t = *a;
  2821. *a = *b;
  2822. *b = t;
  2823. }
  2824. /* Add or subtract infinities. */
  2825. static void
  2826. _mpd_qaddsub_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b,
  2827. uint32_t *status)
  2828. {
  2829. if (mpd_isinfinite(a)) {
  2830. if (mpd_sign(a) != sign_b && mpd_isinfinite(b)) {
  2831. mpd_seterror(result, MPD_Invalid_operation, status);
  2832. }
  2833. else {
  2834. mpd_setspecial(result, mpd_sign(a), MPD_INF);
  2835. }
  2836. return;
  2837. }
  2838. assert(mpd_isinfinite(b));
  2839. mpd_setspecial(result, sign_b, MPD_INF);
  2840. }
  2841. /* Add or subtract non-special numbers. */
  2842. static void
  2843. _mpd_qaddsub(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b,
  2844. const mpd_context_t *ctx, uint32_t *status)
  2845. {
  2846. const mpd_t *big, *small;
  2847. MPD_NEW_STATIC(big_aligned,0,0,0,0);
  2848. MPD_NEW_CONST(tiny,0,0,1,1,1,1);
  2849. mpd_uint_t carry;
  2850. mpd_ssize_t newsize, shift;
  2851. mpd_ssize_t exp, i;
  2852. int swap = 0;
  2853. /* compare exponents */
  2854. big = a; small = b;
  2855. if (big->exp != small->exp) {
  2856. if (small->exp > big->exp) {
  2857. _mpd_ptrswap(&big, &small);
  2858. swap++;
  2859. }
  2860. /* align the coefficients */
  2861. if (!mpd_iszerocoeff(big)) {
  2862. exp = big->exp - 1;
  2863. exp += (big->digits > ctx->prec) ? 0 : big->digits-ctx->prec-1;
  2864. if (mpd_adjexp(small) < exp) {
  2865. /*
  2866. * Avoid huge shifts by substituting a value for small that is
  2867. * guaranteed to produce the same results.
  2868. *
  2869. * adjexp(small) < exp if and only if:
  2870. *
  2871. * bdigits <= prec AND
  2872. * bdigits+shift >= prec+2+sdigits AND
  2873. * exp = bexp+bdigits-prec-2
  2874. *
  2875. * 1234567000000000 -> bdigits + shift
  2876. * ----------XX1234 -> sdigits
  2877. * ----------X1 -> tiny-digits
  2878. * |- prec -|
  2879. *
  2880. * OR
  2881. *
  2882. * bdigits > prec AND
  2883. * shift > sdigits AND
  2884. * exp = bexp-1
  2885. *
  2886. * 1234567892100000 -> bdigits + shift
  2887. * ----------XX1234 -> sdigits
  2888. * ----------X1 -> tiny-digits
  2889. * |- prec -|
  2890. *
  2891. * If tiny is zero, adding or subtracting is a no-op.
  2892. * Otherwise, adding tiny generates a non-zero digit either
  2893. * below the rounding digit or the least significant digit
  2894. * of big. When subtracting, tiny is in the same position as
  2895. * the carry that would be generated by subtracting sdigits.
  2896. */
  2897. mpd_copy_flags(&tiny, small);
  2898. tiny.exp = exp;
  2899. tiny.digits = 1;
  2900. tiny.len = 1;
  2901. tiny.data[0] = mpd_iszerocoeff(small) ? 0 : 1;
  2902. small = &tiny;
  2903. }
  2904. /* This cannot wrap: the difference is positive and <= maxprec */
  2905. shift = big->exp - small->exp;
  2906. if (!mpd_qshiftl(&big_aligned, big, shift, status)) {
  2907. mpd_seterror(result, MPD_Malloc_error, status);
  2908. goto finish;
  2909. }
  2910. big = &big_aligned;
  2911. }
  2912. }
  2913. result->exp = small->exp;
  2914. /* compare length of coefficients */
  2915. if (big->len < small->len) {
  2916. _mpd_ptrswap(&big, &small);
  2917. swap++;
  2918. }
  2919. newsize = big->len;
  2920. if (!mpd_qresize(result, newsize, status)) {
  2921. goto finish;
  2922. }
  2923. if (mpd_sign(a) == sign_b) {
  2924. carry = _mpd_baseadd(result->data, big->data, small->data,
  2925. big->len, small->len);
  2926. if (carry) {
  2927. newsize = big->len + 1;
  2928. if (!mpd_qresize(result, newsize, status)) {
  2929. goto finish;
  2930. }
  2931. result->data[newsize-1] = carry;
  2932. }
  2933. result->len = newsize;
  2934. mpd_set_flags(result, sign_b);
  2935. }
  2936. else {
  2937. if (big->len == small->len) {
  2938. for (i=big->len-1; i >= 0; --i) {
  2939. if (big->data[i] != small->data[i]) {
  2940. if (big->data[i] < small->data[i]) {
  2941. _mpd_ptrswap(&big, &small);
  2942. swap++;
  2943. }
  2944. break;
  2945. }
  2946. }
  2947. }
  2948. _mpd_basesub(result->data, big->data, small->data,
  2949. big->len, small->len);
  2950. newsize = _mpd_real_size(result->data, big->len);
  2951. /* resize to smaller cannot fail */
  2952. (void)mpd_qresize(result, newsize, status);
  2953. result->len = newsize;
  2954. sign_b = (swap & 1) ? sign_b : mpd_sign(a);
  2955. mpd_set_flags(result, sign_b);
  2956. if (mpd_iszerocoeff(result)) {
  2957. mpd_set_positive(result);
  2958. if (ctx->round == MPD_ROUND_FLOOR) {
  2959. mpd_set_negative(result);
  2960. }
  2961. }
  2962. }
  2963. mpd_setdigits(result);
  2964. finish:
  2965. mpd_del(&big_aligned);
  2966. }
  2967. /* Add a and b. No specials, no finalizing. */
  2968. static void
  2969. _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2970. const mpd_context_t *ctx, uint32_t *status)
  2971. {
  2972. _mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status);
  2973. }
  2974. /* Subtract b from a. No specials, no finalizing. */
  2975. static void
  2976. _mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2977. const mpd_context_t *ctx, uint32_t *status)
  2978. {
  2979. _mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status);
  2980. }
  2981. /* Add a and b. */
  2982. void
  2983. mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2984. const mpd_context_t *ctx, uint32_t *status)
  2985. {
  2986. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  2987. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  2988. return;
  2989. }
  2990. _mpd_qaddsub_inf(result, a, b, mpd_sign(b), status);
  2991. return;
  2992. }
  2993. _mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status);
  2994. mpd_qfinalize(result, ctx, status);
  2995. }
  2996. /* Add a and b. Set NaN/Invalid_operation if the result is inexact. */
  2997. static void
  2998. _mpd_qadd_exact(mpd_t *result, const mpd_t *a, const mpd_t *b,
  2999. const mpd_context_t *ctx, uint32_t *status)
  3000. {
  3001. uint32_t workstatus = 0;
  3002. mpd_qadd(result, a, b, ctx, &workstatus);
  3003. *status |= workstatus;
  3004. if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) {
  3005. mpd_seterror(result, MPD_Invalid_operation, status);
  3006. }
  3007. }
  3008. /* Subtract b from a. */
  3009. void
  3010. mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b,
  3011. const mpd_context_t *ctx, uint32_t *status)
  3012. {
  3013. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  3014. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  3015. return;
  3016. }
  3017. _mpd_qaddsub_inf(result, a, b, !mpd_sign(b), status);
  3018. return;
  3019. }
  3020. _mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status);
  3021. mpd_qfinalize(result, ctx, status);
  3022. }
  3023. /* Subtract b from a. Set NaN/Invalid_operation if the result is inexact. */
  3024. static void
  3025. _mpd_qsub_exact(mpd_t *result, const mpd_t *a, const mpd_t *b,
  3026. const mpd_context_t *ctx, uint32_t *status)
  3027. {
  3028. uint32_t workstatus = 0;
  3029. mpd_qsub(result, a, b, ctx, &workstatus);
  3030. *status |= workstatus;
  3031. if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) {
  3032. mpd_seterror(result, MPD_Invalid_operation, status);
  3033. }
  3034. }
  3035. /* Add decimal and mpd_ssize_t. */
  3036. void
  3037. mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
  3038. const mpd_context_t *ctx, uint32_t *status)
  3039. {
  3040. mpd_context_t maxcontext;
  3041. MPD_NEW_STATIC(bb,0,0,0,0);
  3042. mpd_maxcontext(&maxcontext);
  3043. mpd_qsset_ssize(&bb, b, &maxcontext, status);
  3044. mpd_qadd(result, a, &bb, ctx, status);
  3045. mpd_del(&bb);
  3046. }
  3047. /* Add decimal and mpd_uint_t. */
  3048. void
  3049. mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
  3050. const mpd_context_t *ctx, uint32_t *status)
  3051. {
  3052. mpd_context_t maxcontext;
  3053. MPD_NEW_STATIC(bb,0,0,0,0);
  3054. mpd_maxcontext(&maxcontext);
  3055. mpd_qsset_uint(&bb, b, &maxcontext, status);
  3056. mpd_qadd(result, a, &bb, ctx, status);
  3057. mpd_del(&bb);
  3058. }
  3059. /* Subtract mpd_ssize_t from decimal. */
  3060. void
  3061. mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
  3062. const mpd_context_t *ctx, uint32_t *status)
  3063. {
  3064. mpd_context_t maxcontext;
  3065. MPD_NEW_STATIC(bb,0,0,0,0);
  3066. mpd_maxcontext(&maxcontext);
  3067. mpd_qsset_ssize(&bb, b, &maxcontext, status);
  3068. mpd_qsub(result, a, &bb, ctx, status);
  3069. mpd_del(&bb);
  3070. }
  3071. /* Subtract mpd_uint_t from decimal. */
  3072. void
  3073. mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
  3074. const mpd_context_t *ctx, uint32_t *status)
  3075. {
  3076. mpd_context_t maxcontext;
  3077. MPD_NEW_STATIC(bb,0,0,0,0);
  3078. mpd_maxcontext(&maxcontext);
  3079. mpd_qsset_uint(&bb, b, &maxcontext, status);
  3080. mpd_qsub(result, a, &bb, ctx, status);
  3081. mpd_del(&bb);
  3082. }
  3083. /* Add decimal and int32_t. */
  3084. void
  3085. mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b,
  3086. const mpd_context_t *ctx, uint32_t *status)
  3087. {
  3088. mpd_qadd_ssize(result, a, b, ctx, status);
  3089. }
  3090. /* Add decimal and uint32_t. */
  3091. void
  3092. mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b,
  3093. const mpd_context_t *ctx, uint32_t *status)
  3094. {
  3095. mpd_qadd_uint(result, a, b, ctx, status);
  3096. }
  3097. #ifdef CONFIG_64
  3098. /* Add decimal and int64_t. */
  3099. void
  3100. mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b,
  3101. const mpd_context_t *ctx, uint32_t *status)
  3102. {
  3103. mpd_qadd_ssize(result, a, b, ctx, status);
  3104. }
  3105. /* Add decimal and uint64_t. */
  3106. void
  3107. mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  3108. const mpd_context_t *ctx, uint32_t *status)
  3109. {
  3110. mpd_qadd_uint(result, a, b, ctx, status);
  3111. }
  3112. #elif !defined(LEGACY_COMPILER)
  3113. /* Add decimal and int64_t. */
  3114. void
  3115. mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b,
  3116. const mpd_context_t *ctx, uint32_t *status)
  3117. {
  3118. mpd_context_t maxcontext;
  3119. MPD_NEW_STATIC(bb,0,0,0,0);
  3120. mpd_maxcontext(&maxcontext);
  3121. mpd_qset_i64(&bb, b, &maxcontext, status);
  3122. mpd_qadd(result, a, &bb, ctx, status);
  3123. mpd_del(&bb);
  3124. }
  3125. /* Add decimal and uint64_t. */
  3126. void
  3127. mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  3128. const mpd_context_t *ctx, uint32_t *status)
  3129. {
  3130. mpd_context_t maxcontext;
  3131. MPD_NEW_STATIC(bb,0,0,0,0);
  3132. mpd_maxcontext(&maxcontext);
  3133. mpd_qset_u64(&bb, b, &maxcontext, status);
  3134. mpd_qadd(result, a, &bb, ctx, status);
  3135. mpd_del(&bb);
  3136. }
  3137. #endif
  3138. /* Subtract int32_t from decimal. */
  3139. void
  3140. mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b,
  3141. const mpd_context_t *ctx, uint32_t *status)
  3142. {
  3143. mpd_qsub_ssize(result, a, b, ctx, status);
  3144. }
  3145. /* Subtract uint32_t from decimal. */
  3146. void
  3147. mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b,
  3148. const mpd_context_t *ctx, uint32_t *status)
  3149. {
  3150. mpd_qsub_uint(result, a, b, ctx, status);
  3151. }
  3152. #ifdef CONFIG_64
  3153. /* Subtract int64_t from decimal. */
  3154. void
  3155. mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b,
  3156. const mpd_context_t *ctx, uint32_t *status)
  3157. {
  3158. mpd_qsub_ssize(result, a, b, ctx, status);
  3159. }
  3160. /* Subtract uint64_t from decimal. */
  3161. void
  3162. mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  3163. const mpd_context_t *ctx, uint32_t *status)
  3164. {
  3165. mpd_qsub_uint(result, a, b, ctx, status);
  3166. }
  3167. #elif !defined(LEGACY_COMPILER)
  3168. /* Subtract int64_t from decimal. */
  3169. void
  3170. mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b,
  3171. const mpd_context_t *ctx, uint32_t *status)
  3172. {
  3173. mpd_context_t maxcontext;
  3174. MPD_NEW_STATIC(bb,0,0,0,0);
  3175. mpd_maxcontext(&maxcontext);
  3176. mpd_qset_i64(&bb, b, &maxcontext, status);
  3177. mpd_qsub(result, a, &bb, ctx, status);
  3178. mpd_del(&bb);
  3179. }
  3180. /* Subtract uint64_t from decimal. */
  3181. void
  3182. mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  3183. const mpd_context_t *ctx, uint32_t *status)
  3184. {
  3185. mpd_context_t maxcontext;
  3186. MPD_NEW_STATIC(bb,0,0,0,0);
  3187. mpd_maxcontext(&maxcontext);
  3188. mpd_qset_u64(&bb, b, &maxcontext, status);
  3189. mpd_qsub(result, a, &bb, ctx, status);
  3190. mpd_del(&bb);
  3191. }
  3192. #endif
  3193. /* Divide infinities. */
  3194. static void
  3195. _mpd_qdiv_inf(mpd_t *result, const mpd_t *a, const mpd_t *b,
  3196. const mpd_context_t *ctx, uint32_t *status)
  3197. {
  3198. if (mpd_isinfinite(a)) {
  3199. if (mpd_isinfinite(b)) {
  3200. mpd_seterror(result, MPD_Invalid_operation, status);
  3201. return;
  3202. }
  3203. mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
  3204. return;
  3205. }
  3206. assert(mpd_isinfinite(b));
  3207. _settriple(result, mpd_sign(a)^mpd_sign(b), 0, mpd_etiny(ctx));
  3208. *status |= MPD_Clamped;
  3209. }
  3210. enum {NO_IDEAL_EXP, SET_IDEAL_EXP};
  3211. /* Divide a by b. */
  3212. static void
  3213. _mpd_qdiv(int action, mpd_t *q, const mpd_t *a, const mpd_t *b,
  3214. const mpd_context_t *ctx, uint32_t *status)
  3215. {
  3216. MPD_NEW_STATIC(aligned,0,0,0,0);
  3217. mpd_uint_t ld;
  3218. mpd_ssize_t shift, exp, tz;
  3219. mpd_ssize_t newsize;
  3220. mpd_ssize_t ideal_exp;
  3221. mpd_uint_t rem;
  3222. uint8_t sign_a = mpd_sign(a);
  3223. uint8_t sign_b = mpd_sign(b);
  3224. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  3225. if (mpd_qcheck_nans(q, a, b, ctx, status)) {
  3226. return;
  3227. }
  3228. _mpd_qdiv_inf(q, a, b, ctx, status);
  3229. return;
  3230. }
  3231. if (mpd_iszerocoeff(b)) {
  3232. if (mpd_iszerocoeff(a)) {
  3233. mpd_seterror(q, MPD_Division_undefined, status);
  3234. }
  3235. else {
  3236. mpd_setspecial(q, sign_a^sign_b, MPD_INF);
  3237. *status |= MPD_Division_by_zero;
  3238. }
  3239. return;
  3240. }
  3241. if (mpd_iszerocoeff(a)) {
  3242. exp = a->exp - b->exp;
  3243. _settriple(q, sign_a^sign_b, 0, exp);
  3244. mpd_qfinalize(q, ctx, status);
  3245. return;
  3246. }
  3247. shift = (b->digits - a->digits) + ctx->prec + 1;
  3248. ideal_exp = a->exp - b->exp;
  3249. exp = ideal_exp - shift;
  3250. if (shift > 0) {
  3251. if (!mpd_qshiftl(&aligned, a, shift, status)) {
  3252. mpd_seterror(q, MPD_Malloc_error, status);
  3253. goto finish;
  3254. }
  3255. a = &aligned;
  3256. }
  3257. else if (shift < 0) {
  3258. shift = -shift;
  3259. if (!mpd_qshiftl(&aligned, b, shift, status)) {
  3260. mpd_seterror(q, MPD_Malloc_error, status);
  3261. goto finish;
  3262. }
  3263. b = &aligned;
  3264. }
  3265. newsize = a->len - b->len + 1;
  3266. if ((q != b && q != a) || (q == b && newsize > b->len)) {
  3267. if (!mpd_qresize(q, newsize, status)) {
  3268. mpd_seterror(q, MPD_Malloc_error, status);
  3269. goto finish;
  3270. }
  3271. }
  3272. if (b->len == 1) {
  3273. rem = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
  3274. }
  3275. else if (b->len <= MPD_NEWTONDIV_CUTOFF) {
  3276. int ret = _mpd_basedivmod(q->data, NULL, a->data, b->data,
  3277. a->len, b->len);
  3278. if (ret < 0) {
  3279. mpd_seterror(q, MPD_Malloc_error, status);
  3280. goto finish;
  3281. }
  3282. rem = ret;
  3283. }
  3284. else {
  3285. MPD_NEW_STATIC(r,0,0,0,0);
  3286. _mpd_base_ndivmod(q, &r, a, b, status);
  3287. if (mpd_isspecial(q) || mpd_isspecial(&r)) {
  3288. mpd_setspecial(q, MPD_POS, MPD_NAN);
  3289. mpd_del(&r);
  3290. goto finish;
  3291. }
  3292. rem = !mpd_iszerocoeff(&r);
  3293. mpd_del(&r);
  3294. newsize = q->len;
  3295. }
  3296. newsize = _mpd_real_size(q->data, newsize);
  3297. /* resize to smaller cannot fail */
  3298. mpd_qresize(q, newsize, status);
  3299. mpd_set_flags(q, sign_a^sign_b);
  3300. q->len = newsize;
  3301. mpd_setdigits(q);
  3302. shift = ideal_exp - exp;
  3303. if (rem) {
  3304. ld = mpd_lsd(q->data[0]);
  3305. if (ld == 0 || ld == 5) {
  3306. q->data[0] += 1;
  3307. }
  3308. }
  3309. else if (action == SET_IDEAL_EXP && shift > 0) {
  3310. tz = mpd_trail_zeros(q);
  3311. shift = (tz > shift) ? shift : tz;
  3312. mpd_qshiftr_inplace(q, shift);
  3313. exp += shift;
  3314. }
  3315. q->exp = exp;
  3316. finish:
  3317. mpd_del(&aligned);
  3318. mpd_qfinalize(q, ctx, status);
  3319. }
  3320. /* Divide a by b. */
  3321. void
  3322. mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b,
  3323. const mpd_context_t *ctx, uint32_t *status)
  3324. {
  3325. _mpd_qdiv(SET_IDEAL_EXP, q, a, b, ctx, status);
  3326. }
  3327. /* Internal function. */
  3328. static void
  3329. _mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
  3330. const mpd_context_t *ctx, uint32_t *status)
  3331. {
  3332. MPD_NEW_STATIC(aligned,0,0,0,0);
  3333. mpd_ssize_t qsize, rsize;
  3334. mpd_ssize_t ideal_exp, expdiff, shift;
  3335. uint8_t sign_a = mpd_sign(a);
  3336. uint8_t sign_ab = mpd_sign(a)^mpd_sign(b);
  3337. ideal_exp = (a->exp > b->exp) ? b->exp : a->exp;
  3338. if (mpd_iszerocoeff(a)) {
  3339. if (!mpd_qcopy(r, a, status)) {
  3340. goto nanresult; /* GCOV_NOT_REACHED */
  3341. }
  3342. r->exp = ideal_exp;
  3343. _settriple(q, sign_ab, 0, 0);
  3344. return;
  3345. }
  3346. expdiff = mpd_adjexp(a) - mpd_adjexp(b);
  3347. if (expdiff < 0) {
  3348. if (a->exp > b->exp) {
  3349. /* positive and less than b->digits - a->digits */
  3350. shift = a->exp - b->exp;
  3351. if (!mpd_qshiftl(r, a, shift, status)) {
  3352. goto nanresult;
  3353. }
  3354. r->exp = ideal_exp;
  3355. }
  3356. else {
  3357. if (!mpd_qcopy(r, a, status)) {
  3358. goto nanresult;
  3359. }
  3360. }
  3361. _settriple(q, sign_ab, 0, 0);
  3362. return;
  3363. }
  3364. if (expdiff > ctx->prec) {
  3365. *status |= MPD_Division_impossible;
  3366. goto nanresult;
  3367. }
  3368. /*
  3369. * At this point we have:
  3370. * (1) 0 <= a->exp + a->digits - b->exp - b->digits <= prec
  3371. * (2) a->exp - b->exp >= b->digits - a->digits
  3372. * (3) a->exp - b->exp <= prec + b->digits - a->digits
  3373. */
  3374. if (a->exp != b->exp) {
  3375. shift = a->exp - b->exp;
  3376. if (shift > 0) {
  3377. /* by (3), after the shift a->digits <= prec + b->digits */
  3378. if (!mpd_qshiftl(&aligned, a, shift, status)) {
  3379. goto nanresult;
  3380. }
  3381. a = &aligned;
  3382. }
  3383. else {
  3384. shift = -shift;
  3385. /* by (2), after the shift b->digits <= a->digits */
  3386. if (!mpd_qshiftl(&aligned, b, shift, status)) {
  3387. goto nanresult;
  3388. }
  3389. b = &aligned;
  3390. }
  3391. }
  3392. qsize = a->len - b->len + 1;
  3393. if (!(q == a && qsize < a->len) && !(q == b && qsize < b->len)) {
  3394. if (!mpd_qresize(q, qsize, status)) {
  3395. goto nanresult;
  3396. }
  3397. }
  3398. rsize = b->len;
  3399. if (!(r == a && rsize < a->len)) {
  3400. if (!mpd_qresize(r, rsize, status)) {
  3401. goto nanresult;
  3402. }
  3403. }
  3404. if (b->len == 1) {
  3405. if (a->len == 1) {
  3406. _mpd_div_word(&q->data[0], &r->data[0], a->data[0], b->data[0]);
  3407. }
  3408. else {
  3409. r->data[0] = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
  3410. }
  3411. }
  3412. else if (b->len <= MPD_NEWTONDIV_CUTOFF) {
  3413. int ret;
  3414. ret = _mpd_basedivmod(q->data, r->data, a->data, b->data,
  3415. a->len, b->len);
  3416. if (ret == -1) {
  3417. *status |= MPD_Malloc_error;
  3418. goto nanresult;
  3419. }
  3420. }
  3421. else {
  3422. _mpd_base_ndivmod(q, r, a, b, status);
  3423. if (mpd_isspecial(q) || mpd_isspecial(r)) {
  3424. goto nanresult;
  3425. }
  3426. qsize = q->len;
  3427. rsize = r->len;
  3428. }
  3429. qsize = _mpd_real_size(q->data, qsize);
  3430. /* resize to smaller cannot fail */
  3431. mpd_qresize(q, qsize, status);
  3432. q->len = qsize;
  3433. mpd_setdigits(q);
  3434. mpd_set_flags(q, sign_ab);
  3435. q->exp = 0;
  3436. if (q->digits > ctx->prec) {
  3437. *status |= MPD_Division_impossible;
  3438. goto nanresult;
  3439. }
  3440. rsize = _mpd_real_size(r->data, rsize);
  3441. /* resize to smaller cannot fail */
  3442. mpd_qresize(r, rsize, status);
  3443. r->len = rsize;
  3444. mpd_setdigits(r);
  3445. mpd_set_flags(r, sign_a);
  3446. r->exp = ideal_exp;
  3447. out:
  3448. mpd_del(&aligned);
  3449. return;
  3450. nanresult:
  3451. mpd_setspecial(q, MPD_POS, MPD_NAN);
  3452. mpd_setspecial(r, MPD_POS, MPD_NAN);
  3453. goto out;
  3454. }
  3455. /* Integer division with remainder. */
  3456. void
  3457. mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
  3458. const mpd_context_t *ctx, uint32_t *status)
  3459. {
  3460. uint8_t sign = mpd_sign(a)^mpd_sign(b);
  3461. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  3462. if (mpd_qcheck_nans(q, a, b, ctx, status)) {
  3463. mpd_qcopy(r, q, status);
  3464. return;
  3465. }
  3466. if (mpd_isinfinite(a)) {
  3467. if (mpd_isinfinite(b)) {
  3468. mpd_setspecial(q, MPD_POS, MPD_NAN);
  3469. }
  3470. else {
  3471. mpd_setspecial(q, sign, MPD_INF);
  3472. }
  3473. mpd_setspecial(r, MPD_POS, MPD_NAN);
  3474. *status |= MPD_Invalid_operation;
  3475. return;
  3476. }
  3477. if (mpd_isinfinite(b)) {
  3478. if (!mpd_qcopy(r, a, status)) {
  3479. mpd_seterror(q, MPD_Malloc_error, status);
  3480. return;
  3481. }
  3482. mpd_qfinalize(r, ctx, status);
  3483. _settriple(q, sign, 0, 0);
  3484. return;
  3485. }
  3486. /* debug */
  3487. abort(); /* GCOV_NOT_REACHED */
  3488. }
  3489. if (mpd_iszerocoeff(b)) {
  3490. if (mpd_iszerocoeff(a)) {
  3491. mpd_setspecial(q, MPD_POS, MPD_NAN);
  3492. mpd_setspecial(r, MPD_POS, MPD_NAN);
  3493. *status |= MPD_Division_undefined;
  3494. }
  3495. else {
  3496. mpd_setspecial(q, sign, MPD_INF);
  3497. mpd_setspecial(r, MPD_POS, MPD_NAN);
  3498. *status |= (MPD_Division_by_zero|MPD_Invalid_operation);
  3499. }
  3500. return;
  3501. }
  3502. _mpd_qdivmod(q, r, a, b, ctx, status);
  3503. mpd_qfinalize(q, ctx, status);
  3504. mpd_qfinalize(r, ctx, status);
  3505. }
  3506. void
  3507. mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b,
  3508. const mpd_context_t *ctx, uint32_t *status)
  3509. {
  3510. MPD_NEW_STATIC(r,0,0,0,0);
  3511. uint8_t sign = mpd_sign(a)^mpd_sign(b);
  3512. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  3513. if (mpd_qcheck_nans(q, a, b, ctx, status)) {
  3514. return;
  3515. }
  3516. if (mpd_isinfinite(a) && mpd_isinfinite(b)) {
  3517. mpd_seterror(q, MPD_Invalid_operation, status);
  3518. return;
  3519. }
  3520. if (mpd_isinfinite(a)) {
  3521. mpd_setspecial(q, sign, MPD_INF);
  3522. return;
  3523. }
  3524. if (mpd_isinfinite(b)) {
  3525. _settriple(q, sign, 0, 0);
  3526. return;
  3527. }
  3528. /* debug */
  3529. abort(); /* GCOV_NOT_REACHED */
  3530. }
  3531. if (mpd_iszerocoeff(b)) {
  3532. if (mpd_iszerocoeff(a)) {
  3533. mpd_seterror(q, MPD_Division_undefined, status);
  3534. }
  3535. else {
  3536. mpd_setspecial(q, sign, MPD_INF);
  3537. *status |= MPD_Division_by_zero;
  3538. }
  3539. return;
  3540. }
  3541. _mpd_qdivmod(q, &r, a, b, ctx, status);
  3542. mpd_del(&r);
  3543. mpd_qfinalize(q, ctx, status);
  3544. }
  3545. /* Divide decimal by mpd_ssize_t. */
  3546. void
  3547. mpd_qdiv_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
  3548. const mpd_context_t *ctx, uint32_t *status)
  3549. {
  3550. mpd_context_t maxcontext;
  3551. MPD_NEW_STATIC(bb,0,0,0,0);
  3552. mpd_maxcontext(&maxcontext);
  3553. mpd_qsset_ssize(&bb, b, &maxcontext, status);
  3554. mpd_qdiv(result, a, &bb, ctx, status);
  3555. mpd_del(&bb);
  3556. }
  3557. /* Divide decimal by mpd_uint_t. */
  3558. void
  3559. mpd_qdiv_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
  3560. const mpd_context_t *ctx, uint32_t *status)
  3561. {
  3562. mpd_context_t maxcontext;
  3563. MPD_NEW_STATIC(bb,0,0,0,0);
  3564. mpd_maxcontext(&maxcontext);
  3565. mpd_qsset_uint(&bb, b, &maxcontext, status);
  3566. mpd_qdiv(result, a, &bb, ctx, status);
  3567. mpd_del(&bb);
  3568. }
  3569. /* Divide decimal by int32_t. */
  3570. void
  3571. mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b,
  3572. const mpd_context_t *ctx, uint32_t *status)
  3573. {
  3574. mpd_qdiv_ssize(result, a, b, ctx, status);
  3575. }
  3576. /* Divide decimal by uint32_t. */
  3577. void
  3578. mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b,
  3579. const mpd_context_t *ctx, uint32_t *status)
  3580. {
  3581. mpd_qdiv_uint(result, a, b, ctx, status);
  3582. }
  3583. #ifdef CONFIG_64
  3584. /* Divide decimal by int64_t. */
  3585. void
  3586. mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b,
  3587. const mpd_context_t *ctx, uint32_t *status)
  3588. {
  3589. mpd_qdiv_ssize(result, a, b, ctx, status);
  3590. }
  3591. /* Divide decimal by uint64_t. */
  3592. void
  3593. mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  3594. const mpd_context_t *ctx, uint32_t *status)
  3595. {
  3596. mpd_qdiv_uint(result, a, b, ctx, status);
  3597. }
  3598. #elif !defined(LEGACY_COMPILER)
  3599. /* Divide decimal by int64_t. */
  3600. void
  3601. mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b,
  3602. const mpd_context_t *ctx, uint32_t *status)
  3603. {
  3604. mpd_context_t maxcontext;
  3605. MPD_NEW_STATIC(bb,0,0,0,0);
  3606. mpd_maxcontext(&maxcontext);
  3607. mpd_qset_i64(&bb, b, &maxcontext, status);
  3608. mpd_qdiv(result, a, &bb, ctx, status);
  3609. mpd_del(&bb);
  3610. }
  3611. /* Divide decimal by uint64_t. */
  3612. void
  3613. mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  3614. const mpd_context_t *ctx, uint32_t *status)
  3615. {
  3616. mpd_context_t maxcontext;
  3617. MPD_NEW_STATIC(bb,0,0,0,0);
  3618. mpd_maxcontext(&maxcontext);
  3619. mpd_qset_u64(&bb, b, &maxcontext, status);
  3620. mpd_qdiv(result, a, &bb, ctx, status);
  3621. mpd_del(&bb);
  3622. }
  3623. #endif
  3624. /* Pad the result with trailing zeros if it has fewer digits than prec. */
  3625. static void
  3626. _mpd_zeropad(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
  3627. {
  3628. if (!mpd_isspecial(result) && !mpd_iszero(result) &&
  3629. result->digits < ctx->prec) {
  3630. mpd_ssize_t shift = ctx->prec - result->digits;
  3631. mpd_qshiftl(result, result, shift, status);
  3632. result->exp -= shift;
  3633. }
  3634. }
  3635. /* Check if the result is guaranteed to be one. */
  3636. static int
  3637. _mpd_qexp_check_one(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  3638. uint32_t *status)
  3639. {
  3640. MPD_NEW_CONST(lim,0,-(ctx->prec+1),1,1,1,9);
  3641. MPD_NEW_SHARED(aa, a);
  3642. mpd_set_positive(&aa);
  3643. /* abs(a) <= 9 * 10**(-prec-1) */
  3644. if (_mpd_cmp(&aa, &lim) <= 0) {
  3645. _settriple(result, 0, 1, 0);
  3646. *status |= MPD_Rounded|MPD_Inexact;
  3647. return 1;
  3648. }
  3649. return 0;
  3650. }
  3651. /*
  3652. * Get the number of iterations for the Horner scheme in _mpd_qexp().
  3653. */
  3654. static inline mpd_ssize_t
  3655. _mpd_get_exp_iterations(const mpd_t *r, mpd_ssize_t p)
  3656. {
  3657. mpd_ssize_t log10pbyr; /* lower bound for log10(p / abs(r)) */
  3658. mpd_ssize_t n;
  3659. assert(p >= 10);
  3660. assert(!mpd_iszero(r));
  3661. assert(-p < mpd_adjexp(r) && mpd_adjexp(r) <= -1);
  3662. #ifdef CONFIG_64
  3663. if (p > (mpd_ssize_t)(1ULL<<52)) {
  3664. return MPD_SSIZE_MAX;
  3665. }
  3666. #endif
  3667. /*
  3668. * Lower bound for log10(p / abs(r)): adjexp(p) - (adjexp(r) + 1)
  3669. * At this point (for CONFIG_64, CONFIG_32 is not problematic):
  3670. * 1) 10 <= p <= 2**52
  3671. * 2) -p < adjexp(r) <= -1
  3672. * 3) 1 <= log10pbyr <= 2**52 + 14
  3673. */
  3674. log10pbyr = (mpd_word_digits(p)-1) - (mpd_adjexp(r)+1);
  3675. /*
  3676. * The numerator in the paper is 1.435 * p - 1.182, calculated
  3677. * exactly. We compensate for rounding errors by using 1.43503.
  3678. * ACL2 proofs:
  3679. * 1) exp-iter-approx-lower-bound: The term below evaluated
  3680. * in 53-bit floating point arithmetic is greater than or
  3681. * equal to the exact term used in the paper.
  3682. * 2) exp-iter-approx-upper-bound: The term below is less than
  3683. * or equal to 3/2 * p <= 3/2 * 2**52.
  3684. */
  3685. n = (mpd_ssize_t)ceil((1.43503*(double)p - 1.182) / (double)log10pbyr);
  3686. return n >= 3 ? n : 3;
  3687. }
  3688. /*
  3689. * Internal function, specials have been dealt with. Apart from Overflow
  3690. * and Underflow, two cases must be considered for the error of the result:
  3691. *
  3692. * 1) abs(a) <= 9 * 10**(-prec-1) ==> result == 1
  3693. *
  3694. * Absolute error: abs(1 - e**x) < 10**(-prec)
  3695. * -------------------------------------------
  3696. *
  3697. * 2) abs(a) > 9 * 10**(-prec-1)
  3698. *
  3699. * Relative error: abs(result - e**x) < 0.5 * 10**(-prec) * e**x
  3700. * -------------------------------------------------------------
  3701. *
  3702. * The algorithm is from Hull&Abrham, Variable Precision Exponential Function,
  3703. * ACM Transactions on Mathematical Software, Vol. 12, No. 2, June 1986.
  3704. *
  3705. * Main differences:
  3706. *
  3707. * - The number of iterations for the Horner scheme is calculated using
  3708. * 53-bit floating point arithmetic.
  3709. *
  3710. * - In the error analysis for ER (relative error accumulated in the
  3711. * evaluation of the truncated series) the reduced operand r may
  3712. * have any number of digits.
  3713. * ACL2 proof: exponent-relative-error
  3714. *
  3715. * - The analysis for early abortion has been adapted for the mpd_t
  3716. * ranges.
  3717. */
  3718. static void
  3719. _mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  3720. uint32_t *status)
  3721. {
  3722. mpd_context_t workctx;
  3723. MPD_NEW_STATIC(tmp,0,0,0,0);
  3724. MPD_NEW_STATIC(sum,0,0,0,0);
  3725. MPD_NEW_CONST(word,0,0,1,1,1,1);
  3726. mpd_ssize_t j, n, t;
  3727. assert(!mpd_isspecial(a));
  3728. if (mpd_iszerocoeff(a)) {
  3729. _settriple(result, MPD_POS, 1, 0);
  3730. return;
  3731. }
  3732. /*
  3733. * We are calculating e^x = e^(r*10^t) = (e^r)^(10^t), where abs(r) < 1 and t >= 0.
  3734. *
  3735. * If t > 0, we have:
  3736. *
  3737. * (1) 0.1 <= r < 1, so e^0.1 <= e^r. If t > MAX_T, overflow occurs:
  3738. *
  3739. * MAX-EMAX+1 < log10(e^(0.1*10*t)) <= log10(e^(r*10^t)) < adjexp(e^(r*10^t))+1
  3740. *
  3741. * (2) -1 < r <= -0.1, so e^r <= e^-0.1. If t > MAX_T, underflow occurs:
  3742. *
  3743. * adjexp(e^(r*10^t)) <= log10(e^(r*10^t)) <= log10(e^(-0.1*10^t)) < MIN-ETINY
  3744. */
  3745. #if defined(CONFIG_64)
  3746. #define MPD_EXP_MAX_T 19
  3747. #elif defined(CONFIG_32)
  3748. #define MPD_EXP_MAX_T 10
  3749. #endif
  3750. t = a->digits + a->exp;
  3751. t = (t > 0) ? t : 0;
  3752. if (t > MPD_EXP_MAX_T) {
  3753. if (mpd_ispositive(a)) {
  3754. mpd_setspecial(result, MPD_POS, MPD_INF);
  3755. *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
  3756. }
  3757. else {
  3758. _settriple(result, MPD_POS, 0, mpd_etiny(ctx));
  3759. *status |= (MPD_Inexact|MPD_Rounded|MPD_Subnormal|
  3760. MPD_Underflow|MPD_Clamped);
  3761. }
  3762. return;
  3763. }
  3764. /* abs(a) <= 9 * 10**(-prec-1) */
  3765. if (_mpd_qexp_check_one(result, a, ctx, status)) {
  3766. return;
  3767. }
  3768. mpd_maxcontext(&workctx);
  3769. workctx.prec = ctx->prec + t + 2;
  3770. workctx.prec = (workctx.prec < 10) ? 10 : workctx.prec;
  3771. workctx.round = MPD_ROUND_HALF_EVEN;
  3772. if (!mpd_qcopy(result, a, status)) {
  3773. return;
  3774. }
  3775. result->exp -= t;
  3776. /*
  3777. * At this point:
  3778. * 1) 9 * 10**(-prec-1) < abs(a)
  3779. * 2) 9 * 10**(-prec-t-1) < abs(r)
  3780. * 3) log10(9) - prec - t - 1 < log10(abs(r)) < adjexp(abs(r)) + 1
  3781. * 4) - prec - t - 2 < adjexp(abs(r)) <= -1
  3782. */
  3783. n = _mpd_get_exp_iterations(result, workctx.prec);
  3784. if (n == MPD_SSIZE_MAX) {
  3785. mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_UNLIKELY */
  3786. return; /* GCOV_UNLIKELY */
  3787. }
  3788. _settriple(&sum, MPD_POS, 1, 0);
  3789. for (j = n-1; j >= 1; j--) {
  3790. word.data[0] = j;
  3791. mpd_setdigits(&word);
  3792. mpd_qdiv(&tmp, result, &word, &workctx, &workctx.status);
  3793. mpd_qfma(&sum, &sum, &tmp, &one, &workctx, &workctx.status);
  3794. }
  3795. #ifdef CONFIG_64
  3796. _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status);
  3797. #else
  3798. if (t <= MPD_MAX_POW10) {
  3799. _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status);
  3800. }
  3801. else {
  3802. t -= MPD_MAX_POW10;
  3803. _mpd_qpow_uint(&tmp, &sum, mpd_pow10[MPD_MAX_POW10], MPD_POS,
  3804. &workctx, status);
  3805. _mpd_qpow_uint(result, &tmp, mpd_pow10[t], MPD_POS, &workctx, status);
  3806. }
  3807. #endif
  3808. mpd_del(&tmp);
  3809. mpd_del(&sum);
  3810. *status |= (workctx.status&MPD_Errors);
  3811. *status |= (MPD_Inexact|MPD_Rounded);
  3812. }
  3813. /* exp(a) */
  3814. void
  3815. mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  3816. uint32_t *status)
  3817. {
  3818. mpd_context_t workctx;
  3819. if (mpd_isspecial(a)) {
  3820. if (mpd_qcheck_nan(result, a, ctx, status)) {
  3821. return;
  3822. }
  3823. if (mpd_isnegative(a)) {
  3824. _settriple(result, MPD_POS, 0, 0);
  3825. }
  3826. else {
  3827. mpd_setspecial(result, MPD_POS, MPD_INF);
  3828. }
  3829. return;
  3830. }
  3831. if (mpd_iszerocoeff(a)) {
  3832. _settriple(result, MPD_POS, 1, 0);
  3833. return;
  3834. }
  3835. workctx = *ctx;
  3836. workctx.round = MPD_ROUND_HALF_EVEN;
  3837. if (ctx->allcr) {
  3838. MPD_NEW_STATIC(t1, 0,0,0,0);
  3839. MPD_NEW_STATIC(t2, 0,0,0,0);
  3840. MPD_NEW_STATIC(ulp, 0,0,0,0);
  3841. MPD_NEW_STATIC(aa, 0,0,0,0);
  3842. mpd_ssize_t prec;
  3843. mpd_ssize_t ulpexp;
  3844. uint32_t workstatus;
  3845. if (result == a) {
  3846. if (!mpd_qcopy(&aa, a, status)) {
  3847. mpd_seterror(result, MPD_Malloc_error, status);
  3848. return;
  3849. }
  3850. a = &aa;
  3851. }
  3852. workctx.clamp = 0;
  3853. prec = ctx->prec + 3;
  3854. while (1) {
  3855. workctx.prec = prec;
  3856. workstatus = 0;
  3857. _mpd_qexp(result, a, &workctx, &workstatus);
  3858. *status |= workstatus;
  3859. ulpexp = result->exp + result->digits - workctx.prec;
  3860. if (workstatus & MPD_Underflow) {
  3861. /* The effective work precision is result->digits. */
  3862. ulpexp = result->exp;
  3863. }
  3864. _ssettriple(&ulp, MPD_POS, 1, ulpexp);
  3865. /*
  3866. * At this point [1]:
  3867. * 1) abs(result - e**x) < 0.5 * 10**(-prec) * e**x
  3868. * 2) result - ulp < e**x < result + ulp
  3869. * 3) result - ulp < result < result + ulp
  3870. *
  3871. * If round(result-ulp)==round(result+ulp), then
  3872. * round(result)==round(e**x). Therefore the result
  3873. * is correctly rounded.
  3874. *
  3875. * [1] If abs(a) <= 9 * 10**(-prec-1), use the absolute
  3876. * error for a similar argument.
  3877. */
  3878. workctx.prec = ctx->prec;
  3879. mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status);
  3880. mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status);
  3881. if (mpd_isspecial(result) || mpd_iszerocoeff(result) ||
  3882. mpd_qcmp(&t1, &t2, status) == 0) {
  3883. workctx.clamp = ctx->clamp;
  3884. _mpd_zeropad(result, &workctx, status);
  3885. mpd_check_underflow(result, &workctx, status);
  3886. mpd_qfinalize(result, &workctx, status);
  3887. break;
  3888. }
  3889. prec += MPD_RDIGITS;
  3890. }
  3891. mpd_del(&t1);
  3892. mpd_del(&t2);
  3893. mpd_del(&ulp);
  3894. mpd_del(&aa);
  3895. }
  3896. else {
  3897. _mpd_qexp(result, a, &workctx, status);
  3898. _mpd_zeropad(result, &workctx, status);
  3899. mpd_check_underflow(result, &workctx, status);
  3900. mpd_qfinalize(result, &workctx, status);
  3901. }
  3902. }
  3903. /* Fused multiply-add: (a * b) + c, with a single final rounding. */
  3904. void
  3905. mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c,
  3906. const mpd_context_t *ctx, uint32_t *status)
  3907. {
  3908. uint32_t workstatus = 0;
  3909. mpd_t *cc = NULL;
  3910. if (result == c) {
  3911. if ((cc = mpd_qncopy(c)) == NULL) {
  3912. mpd_seterror(result, MPD_Malloc_error, status);
  3913. return;
  3914. }
  3915. c = cc;
  3916. }
  3917. _mpd_qmul(result, a, b, ctx, &workstatus);
  3918. if (!(workstatus&MPD_Invalid_operation)) {
  3919. mpd_qadd(result, result, c, ctx, &workstatus);
  3920. }
  3921. if (cc) mpd_del(cc);
  3922. *status |= workstatus;
  3923. }
  3924. /*
  3925. * Schedule the optimal precision increase for the Newton iteration.
  3926. * v := input operand
  3927. * z_0 := initial approximation
  3928. * initprec := natural number such that abs(log(v) - z_0) < 10**-initprec
  3929. * maxprec := target precision
  3930. *
  3931. * For convenience the output klist contains the elements in reverse order:
  3932. * klist := [k_n-1, ..., k_0], where
  3933. * 1) k_0 <= initprec and
  3934. * 2) abs(log(v) - result) < 10**(-2*k_n-1 + 1) <= 10**-maxprec.
  3935. */
  3936. static inline int
  3937. ln_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], mpd_ssize_t maxprec,
  3938. mpd_ssize_t initprec)
  3939. {
  3940. mpd_ssize_t k;
  3941. int i;
  3942. assert(maxprec >= 2 && initprec >= 2);
  3943. if (maxprec <= initprec) return -1;
  3944. i = 0; k = maxprec;
  3945. do {
  3946. k = (k+2) / 2;
  3947. klist[i++] = k;
  3948. } while (k > initprec);
  3949. return i-1;
  3950. }
  3951. /* The constants have been verified with both decimal.py and mpfr. */
  3952. #ifdef CONFIG_64
  3953. #if MPD_RDIGITS != 19
  3954. #error "mpdecimal.c: MPD_RDIGITS must be 19."
  3955. #endif
  3956. static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = {
  3957. 6983716328982174407ULL, 9089704281976336583ULL, 1515961135648465461ULL,
  3958. 4416816335727555703ULL, 2900988039194170265ULL, 2307925037472986509ULL,
  3959. 107598438319191292ULL, 3466624107184669231ULL, 4450099781311469159ULL,
  3960. 9807828059751193854ULL, 7713456862091670584ULL, 1492198849978748873ULL,
  3961. 6528728696511086257ULL, 2385392051446341972ULL, 8692180205189339507ULL,
  3962. 6518769751037497088ULL, 2375253577097505395ULL, 9095610299291824318ULL,
  3963. 982748238504564801ULL, 5438635917781170543ULL, 7547331541421808427ULL,
  3964. 752371033310119785ULL, 3171643095059950878ULL, 9785265383207606726ULL,
  3965. 2932258279850258550ULL, 5497347726624257094ULL, 2976979522110718264ULL,
  3966. 9221477656763693866ULL, 1979650047149510504ULL, 6674183485704422507ULL,
  3967. 9702766860595249671ULL, 9278096762712757753ULL, 9314848524948644871ULL,
  3968. 6826928280848118428ULL, 754403708474699401ULL, 230105703089634572ULL,
  3969. 1929203337658714166ULL, 7589402567763113569ULL, 4208241314695689016ULL,
  3970. 2922455440575892572ULL, 9356734206705811364ULL, 2684916746550586856ULL,
  3971. 644507064800027750ULL, 9476834636167921018ULL, 5659121373450747856ULL,
  3972. 2835522011480466371ULL, 6470806855677432162ULL, 7141748003688084012ULL,
  3973. 9619404400222105101ULL, 5504893431493939147ULL, 6674744042432743651ULL,
  3974. 2287698219886746543ULL, 7773262884616336622ULL, 1985283935053089653ULL,
  3975. 4680843799894826233ULL, 8168948290720832555ULL, 8067566662873690987ULL,
  3976. 6248633409525465082ULL, 9829834196778404228ULL, 3524802359972050895ULL,
  3977. 3327900967572609677ULL, 110148862877297603ULL, 179914546843642076ULL,
  3978. 2302585092994045684ULL
  3979. };
  3980. #else
  3981. #if MPD_RDIGITS != 9
  3982. #error "mpdecimal.c: MPD_RDIGITS must be 9."
  3983. #endif
  3984. static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = {
  3985. 401682692UL, 708474699UL, 720754403UL, 30896345UL, 602301057UL, 765871416UL,
  3986. 192920333UL, 763113569UL, 589402567UL, 956890167UL, 82413146UL, 589257242UL,
  3987. 245544057UL, 811364292UL, 734206705UL, 868569356UL, 167465505UL, 775026849UL,
  3988. 706480002UL, 18064450UL, 636167921UL, 569476834UL, 734507478UL, 156591213UL,
  3989. 148046637UL, 283552201UL, 677432162UL, 470806855UL, 880840126UL, 417480036UL,
  3990. 210510171UL, 940440022UL, 939147961UL, 893431493UL, 436515504UL, 440424327UL,
  3991. 654366747UL, 821988674UL, 622228769UL, 884616336UL, 537773262UL, 350530896UL,
  3992. 319852839UL, 989482623UL, 468084379UL, 720832555UL, 168948290UL, 736909878UL,
  3993. 675666628UL, 546508280UL, 863340952UL, 404228624UL, 834196778UL, 508959829UL,
  3994. 23599720UL, 967735248UL, 96757260UL, 603332790UL, 862877297UL, 760110148UL,
  3995. 468436420UL, 401799145UL, 299404568UL, 230258509UL
  3996. };
  3997. #endif
  3998. /* _mpd_ln10 is used directly for precisions smaller than MINALLOC_MAX*RDIGITS.
  3999. Otherwise, it serves as the initial approximation for calculating ln(10). */
  4000. static const mpd_t _mpd_ln10 = {
  4001. MPD_STATIC|MPD_CONST_DATA, -(MPD_MINALLOC_MAX*MPD_RDIGITS-1),
  4002. MPD_MINALLOC_MAX*MPD_RDIGITS, MPD_MINALLOC_MAX, MPD_MINALLOC_MAX,
  4003. (mpd_uint_t *)mpd_ln10_data
  4004. };
  4005. /*
  4006. * Set 'result' to log(10).
  4007. * Ulp error: abs(result - log(10)) < ulp(log(10))
  4008. * Relative error: abs(result - log(10)) < 5 * 10**-prec * log(10)
  4009. *
  4010. * NOTE: The relative error is not derived from the ulp error, but
  4011. * calculated separately using the fact that 23/10 < log(10) < 24/10.
  4012. */
  4013. void
  4014. mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status)
  4015. {
  4016. mpd_context_t varcontext, maxcontext;
  4017. MPD_NEW_STATIC(tmp, 0,0,0,0);
  4018. MPD_NEW_CONST(static10, 0,0,2,1,1,10);
  4019. mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
  4020. mpd_uint_t rnd;
  4021. mpd_ssize_t shift;
  4022. int i;
  4023. assert(prec >= 1);
  4024. shift = MPD_MINALLOC_MAX*MPD_RDIGITS-prec;
  4025. shift = shift < 0 ? 0 : shift;
  4026. rnd = mpd_qshiftr(result, &_mpd_ln10, shift, status);
  4027. if (rnd == MPD_UINT_MAX) {
  4028. mpd_seterror(result, MPD_Malloc_error, status);
  4029. return;
  4030. }
  4031. result->exp = -(result->digits-1);
  4032. mpd_maxcontext(&maxcontext);
  4033. if (prec < MPD_MINALLOC_MAX*MPD_RDIGITS) {
  4034. maxcontext.prec = prec;
  4035. _mpd_apply_round_excess(result, rnd, &maxcontext, status);
  4036. *status |= (MPD_Inexact|MPD_Rounded);
  4037. return;
  4038. }
  4039. mpd_maxcontext(&varcontext);
  4040. varcontext.round = MPD_ROUND_TRUNC;
  4041. i = ln_schedule_prec(klist, prec+2, -result->exp);
  4042. for (; i >= 0; i--) {
  4043. varcontext.prec = 2*klist[i]+3;
  4044. result->flags ^= MPD_NEG;
  4045. _mpd_qexp(&tmp, result, &varcontext, status);
  4046. result->flags ^= MPD_NEG;
  4047. mpd_qmul(&tmp, &static10, &tmp, &varcontext, status);
  4048. mpd_qsub(&tmp, &tmp, &one, &maxcontext, status);
  4049. mpd_qadd(result, result, &tmp, &maxcontext, status);
  4050. if (mpd_isspecial(result)) {
  4051. break;
  4052. }
  4053. }
  4054. mpd_del(&tmp);
  4055. maxcontext.prec = prec;
  4056. mpd_qfinalize(result, &maxcontext, status);
  4057. }
  4058. /*
  4059. * Initial approximations for the ln() iteration. The values have the
  4060. * following properties (established with both decimal.py and mpfr):
  4061. *
  4062. * Index 0 - 400, logarithms of x in [1.00, 5.00]:
  4063. * abs(lnapprox[i] * 10**-3 - log((i+100)/100)) < 10**-2
  4064. * abs(lnapprox[i] * 10**-3 - log((i+1+100)/100)) < 10**-2
  4065. *
  4066. * Index 401 - 899, logarithms of x in (0.500, 0.999]:
  4067. * abs(-lnapprox[i] * 10**-3 - log((i+100)/1000)) < 10**-2
  4068. * abs(-lnapprox[i] * 10**-3 - log((i+1+100)/1000)) < 10**-2
  4069. */
  4070. static const uint16_t lnapprox[900] = {
  4071. /* index 0 - 400: log((i+100)/100) * 1000 */
  4072. 0, 10, 20, 30, 39, 49, 58, 68, 77, 86, 95, 104, 113, 122, 131, 140, 148, 157,
  4073. 166, 174, 182, 191, 199, 207, 215, 223, 231, 239, 247, 255, 262, 270, 278,
  4074. 285, 293, 300, 308, 315, 322, 329, 336, 344, 351, 358, 365, 372, 378, 385,
  4075. 392, 399, 406, 412, 419, 425, 432, 438, 445, 451, 457, 464, 470, 476, 482,
  4076. 489, 495, 501, 507, 513, 519, 525, 531, 536, 542, 548, 554, 560, 565, 571,
  4077. 577, 582, 588, 593, 599, 604, 610, 615, 621, 626, 631, 637, 642, 647, 652,
  4078. 658, 663, 668, 673, 678, 683, 688, 693, 698, 703, 708, 713, 718, 723, 728,
  4079. 732, 737, 742, 747, 751, 756, 761, 766, 770, 775, 779, 784, 788, 793, 798,
  4080. 802, 806, 811, 815, 820, 824, 829, 833, 837, 842, 846, 850, 854, 859, 863,
  4081. 867, 871, 876, 880, 884, 888, 892, 896, 900, 904, 908, 912, 916, 920, 924,
  4082. 928, 932, 936, 940, 944, 948, 952, 956, 959, 963, 967, 971, 975, 978, 982,
  4083. 986, 990, 993, 997, 1001, 1004, 1008, 1012, 1015, 1019, 1022, 1026, 1030,
  4084. 1033, 1037, 1040, 1044, 1047, 1051, 1054, 1058, 1061, 1065, 1068, 1072, 1075,
  4085. 1078, 1082, 1085, 1089, 1092, 1095, 1099, 1102, 1105, 1109, 1112, 1115, 1118,
  4086. 1122, 1125, 1128, 1131, 1135, 1138, 1141, 1144, 1147, 1151, 1154, 1157, 1160,
  4087. 1163, 1166, 1169, 1172, 1176, 1179, 1182, 1185, 1188, 1191, 1194, 1197, 1200,
  4088. 1203, 1206, 1209, 1212, 1215, 1218, 1221, 1224, 1227, 1230, 1233, 1235, 1238,
  4089. 1241, 1244, 1247, 1250, 1253, 1256, 1258, 1261, 1264, 1267, 1270, 1273, 1275,
  4090. 1278, 1281, 1284, 1286, 1289, 1292, 1295, 1297, 1300, 1303, 1306, 1308, 1311,
  4091. 1314, 1316, 1319, 1322, 1324, 1327, 1330, 1332, 1335, 1338, 1340, 1343, 1345,
  4092. 1348, 1351, 1353, 1356, 1358, 1361, 1364, 1366, 1369, 1371, 1374, 1376, 1379,
  4093. 1381, 1384, 1386, 1389, 1391, 1394, 1396, 1399, 1401, 1404, 1406, 1409, 1411,
  4094. 1413, 1416, 1418, 1421, 1423, 1426, 1428, 1430, 1433, 1435, 1437, 1440, 1442,
  4095. 1445, 1447, 1449, 1452, 1454, 1456, 1459, 1461, 1463, 1466, 1468, 1470, 1472,
  4096. 1475, 1477, 1479, 1482, 1484, 1486, 1488, 1491, 1493, 1495, 1497, 1500, 1502,
  4097. 1504, 1506, 1509, 1511, 1513, 1515, 1517, 1520, 1522, 1524, 1526, 1528, 1530,
  4098. 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1548, 1550, 1552, 1554, 1556, 1558,
  4099. 1560, 1562, 1564, 1567, 1569, 1571, 1573, 1575, 1577, 1579, 1581, 1583, 1585,
  4100. 1587, 1589, 1591, 1593, 1595, 1597, 1599, 1601, 1603, 1605, 1607, 1609,
  4101. /* index 401 - 899: -log((i+100)/1000) * 1000 */
  4102. 691, 689, 687, 685, 683, 681, 679, 677, 675, 673, 671, 669, 668, 666, 664,
  4103. 662, 660, 658, 656, 654, 652, 650, 648, 646, 644, 642, 641, 639, 637, 635,
  4104. 633, 631, 629, 627, 626, 624, 622, 620, 618, 616, 614, 612, 611, 609, 607,
  4105. 605, 603, 602, 600, 598, 596, 594, 592, 591, 589, 587, 585, 583, 582, 580,
  4106. 578, 576, 574, 573, 571, 569, 567, 566, 564, 562, 560, 559, 557, 555, 553,
  4107. 552, 550, 548, 546, 545, 543, 541, 540, 538, 536, 534, 533, 531, 529, 528,
  4108. 526, 524, 523, 521, 519, 518, 516, 514, 512, 511, 509, 508, 506, 504, 502,
  4109. 501, 499, 498, 496, 494, 493, 491, 489, 488, 486, 484, 483, 481, 480, 478,
  4110. 476, 475, 473, 472, 470, 468, 467, 465, 464, 462, 460, 459, 457, 456, 454,
  4111. 453, 451, 449, 448, 446, 445, 443, 442, 440, 438, 437, 435, 434, 432, 431,
  4112. 429, 428, 426, 425, 423, 422, 420, 419, 417, 416, 414, 412, 411, 410, 408,
  4113. 406, 405, 404, 402, 400, 399, 398, 396, 394, 393, 392, 390, 389, 387, 386,
  4114. 384, 383, 381, 380, 378, 377, 375, 374, 372, 371, 370, 368, 367, 365, 364,
  4115. 362, 361, 360, 358, 357, 355, 354, 352, 351, 350, 348, 347, 345, 344, 342,
  4116. 341, 340, 338, 337, 336, 334, 333, 331, 330, 328, 327, 326, 324, 323, 322,
  4117. 320, 319, 318, 316, 315, 313, 312, 311, 309, 308, 306, 305, 304, 302, 301,
  4118. 300, 298, 297, 296, 294, 293, 292, 290, 289, 288, 286, 285, 284, 282, 281,
  4119. 280, 278, 277, 276, 274, 273, 272, 270, 269, 268, 267, 265, 264, 263, 261,
  4120. 260, 259, 258, 256, 255, 254, 252, 251, 250, 248, 247, 246, 245, 243, 242,
  4121. 241, 240, 238, 237, 236, 234, 233, 232, 231, 229, 228, 227, 226, 224, 223,
  4122. 222, 221, 219, 218, 217, 216, 214, 213, 212, 211, 210, 208, 207, 206, 205,
  4123. 203, 202, 201, 200, 198, 197, 196, 195, 194, 192, 191, 190, 189, 188, 186,
  4124. 185, 184, 183, 182, 180, 179, 178, 177, 176, 174, 173, 172, 171, 170, 168,
  4125. 167, 166, 165, 164, 162, 161, 160, 159, 158, 157, 156, 154, 153, 152, 151,
  4126. 150, 148, 147, 146, 145, 144, 143, 142, 140, 139, 138, 137, 136, 135, 134,
  4127. 132, 131, 130, 129, 128, 127, 126, 124, 123, 122, 121, 120, 119, 118, 116,
  4128. 115, 114, 113, 112, 111, 110, 109, 108, 106, 105, 104, 103, 102, 101, 100,
  4129. 99, 98, 97, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79,
  4130. 78, 77, 76, 75, 74, 73, 72, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59,
  4131. 58, 57, 56, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39,
  4132. 38, 37, 36, 35, 34, 33, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19,
  4133. 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  4134. };
  4135. /*
  4136. * Internal ln() function that does not check for specials, zero or one.
  4137. * Relative error: abs(result - log(a)) < 0.1 * 10**-prec * abs(log(a))
  4138. */
  4139. static void
  4140. _mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  4141. uint32_t *status)
  4142. {
  4143. mpd_context_t varcontext, maxcontext;
  4144. mpd_t *z = (mpd_t *) result;
  4145. MPD_NEW_STATIC(v,0,0,0,0);
  4146. MPD_NEW_STATIC(vtmp,0,0,0,0);
  4147. MPD_NEW_STATIC(tmp,0,0,0,0);
  4148. mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
  4149. mpd_ssize_t maxprec, shift, t;
  4150. mpd_ssize_t a_digits, a_exp;
  4151. mpd_uint_t dummy, x;
  4152. int i;
  4153. assert(!mpd_isspecial(a) && !mpd_iszerocoeff(a));
  4154. /*
  4155. * We are calculating ln(a) = ln(v * 10^t) = ln(v) + t*ln(10),
  4156. * where 0.5 < v <= 5.
  4157. */
  4158. if (!mpd_qcopy(&v, a, status)) {
  4159. mpd_seterror(result, MPD_Malloc_error, status);
  4160. goto finish;
  4161. }
  4162. /* Initial approximation: we have at least one non-zero digit */
  4163. _mpd_get_msdigits(&dummy, &x, &v, 3);
  4164. if (x < 10) x *= 10;
  4165. if (x < 100) x *= 10;
  4166. x -= 100;
  4167. /* a may equal z */
  4168. a_digits = a->digits;
  4169. a_exp = a->exp;
  4170. mpd_minalloc(z);
  4171. mpd_clear_flags(z);
  4172. z->data[0] = lnapprox[x];
  4173. z->len = 1;
  4174. z->exp = -3;
  4175. mpd_setdigits(z);
  4176. if (x <= 400) {
  4177. /* Reduce the input operand to 1.00 <= v <= 5.00. Let y = x + 100,
  4178. * so 100 <= y <= 500. Since y contains the most significant digits
  4179. * of v, y/100 <= v < (y+1)/100 and abs(z - log(v)) < 10**-2. */
  4180. v.exp = -(a_digits - 1);
  4181. t = a_exp + a_digits - 1;
  4182. }
  4183. else {
  4184. /* Reduce the input operand to 0.500 < v <= 0.999. Let y = x + 100,
  4185. * so 500 < y <= 999. Since y contains the most significant digits
  4186. * of v, y/1000 <= v < (y+1)/1000 and abs(z - log(v)) < 10**-2. */
  4187. v.exp = -a_digits;
  4188. t = a_exp + a_digits;
  4189. mpd_set_negative(z);
  4190. }
  4191. mpd_maxcontext(&maxcontext);
  4192. mpd_maxcontext(&varcontext);
  4193. varcontext.round = MPD_ROUND_TRUNC;
  4194. maxprec = ctx->prec + 2;
  4195. if (t == 0 && (x <= 15 || x >= 800)) {
  4196. /* 0.900 <= v <= 1.15: Estimate the magnitude of the logarithm.
  4197. * If ln(v) will underflow, skip the loop. Otherwise, adjust the
  4198. * precision upwards in order to obtain a sufficient number of
  4199. * significant digits.
  4200. *
  4201. * Case v > 1:
  4202. * abs((v-1)/10) < abs((v-1)/v) < abs(ln(v)) < abs(v-1)
  4203. * Case v < 1:
  4204. * abs(v-1) < abs(ln(v)) < abs((v-1)/v) < abs((v-1)*10)
  4205. */
  4206. int cmp = _mpd_cmp(&v, &one);
  4207. /* Upper bound (assume v > 1): abs(v-1), unrounded */
  4208. _mpd_qsub(&tmp, &v, &one, &maxcontext, &maxcontext.status);
  4209. if (maxcontext.status & MPD_Errors) {
  4210. mpd_seterror(result, MPD_Malloc_error, status);
  4211. goto finish;
  4212. }
  4213. if (cmp < 0) {
  4214. /* v < 1: abs((v-1)*10) */
  4215. tmp.exp += 1;
  4216. }
  4217. if (mpd_adjexp(&tmp) < mpd_etiny(ctx)) {
  4218. /* The upper bound is less than etiny: Underflow to zero */
  4219. _settriple(result, (cmp<0), 1, mpd_etiny(ctx)-1);
  4220. goto finish;
  4221. }
  4222. /* Lower bound: abs((v-1)/10) or abs(v-1) */
  4223. tmp.exp -= 1;
  4224. if (mpd_adjexp(&tmp) < 0) {
  4225. /* Absolute error of the loop: abs(z - log(v)) < 10**-p. If
  4226. * p = ctx->prec+2-adjexp(lower), then the relative error of
  4227. * the result is (using 10**adjexp(x) <= abs(x)):
  4228. *
  4229. * abs(z - log(v)) / abs(log(v)) < 10**-p / abs(log(v))
  4230. * <= 10**(-ctx->prec-2)
  4231. */
  4232. maxprec = maxprec - mpd_adjexp(&tmp);
  4233. }
  4234. }
  4235. i = ln_schedule_prec(klist, maxprec, 2);
  4236. for (; i >= 0; i--) {
  4237. varcontext.prec = 2*klist[i]+3;
  4238. z->flags ^= MPD_NEG;
  4239. _mpd_qexp(&tmp, z, &varcontext, status);
  4240. z->flags ^= MPD_NEG;
  4241. if (v.digits > varcontext.prec) {
  4242. shift = v.digits - varcontext.prec;
  4243. mpd_qshiftr(&vtmp, &v, shift, status);
  4244. vtmp.exp += shift;
  4245. mpd_qmul(&tmp, &vtmp, &tmp, &varcontext, status);
  4246. }
  4247. else {
  4248. mpd_qmul(&tmp, &v, &tmp, &varcontext, status);
  4249. }
  4250. mpd_qsub(&tmp, &tmp, &one, &maxcontext, status);
  4251. mpd_qadd(z, z, &tmp, &maxcontext, status);
  4252. if (mpd_isspecial(z)) {
  4253. break;
  4254. }
  4255. }
  4256. /*
  4257. * Case t == 0:
  4258. * t * log(10) == 0, the result does not change and the analysis
  4259. * above applies. If v < 0.900 or v > 1.15, the relative error is
  4260. * less than 10**(-ctx.prec-1).
  4261. * Case t != 0:
  4262. * z := approx(log(v))
  4263. * y := approx(log(10))
  4264. * p := maxprec = ctx->prec + 2
  4265. * Absolute errors:
  4266. * 1) abs(z - log(v)) < 10**-p
  4267. * 2) abs(y - log(10)) < 10**-p
  4268. * The multiplication is exact, so:
  4269. * 3) abs(t*y - t*log(10)) < t*10**-p
  4270. * The sum is exact, so:
  4271. * 4) abs((z + t*y) - (log(v) + t*log(10))) < (abs(t) + 1) * 10**-p
  4272. * Bounds for log(v) and log(10):
  4273. * 5) -7/10 < log(v) < 17/10
  4274. * 6) 23/10 < log(10) < 24/10
  4275. * Using 4), 5), 6) and t != 0, the relative error is:
  4276. *
  4277. * 7) relerr < ((abs(t) + 1)*10**-p) / abs(log(v) + t*log(10))
  4278. * < 0.5 * 10**(-p + 1) = 0.5 * 10**(-ctx->prec-1)
  4279. */
  4280. mpd_qln10(&v, maxprec+1, status);
  4281. mpd_qmul_ssize(&tmp, &v, t, &maxcontext, status);
  4282. mpd_qadd(result, &tmp, z, &maxcontext, status);
  4283. finish:
  4284. *status |= (MPD_Inexact|MPD_Rounded);
  4285. mpd_del(&v);
  4286. mpd_del(&vtmp);
  4287. mpd_del(&tmp);
  4288. }
  4289. /* ln(a) */
  4290. void
  4291. mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  4292. uint32_t *status)
  4293. {
  4294. mpd_context_t workctx;
  4295. mpd_ssize_t adjexp, t;
  4296. if (mpd_isspecial(a)) {
  4297. if (mpd_qcheck_nan(result, a, ctx, status)) {
  4298. return;
  4299. }
  4300. if (mpd_isnegative(a)) {
  4301. mpd_seterror(result, MPD_Invalid_operation, status);
  4302. return;
  4303. }
  4304. mpd_setspecial(result, MPD_POS, MPD_INF);
  4305. return;
  4306. }
  4307. if (mpd_iszerocoeff(a)) {
  4308. mpd_setspecial(result, MPD_NEG, MPD_INF);
  4309. return;
  4310. }
  4311. if (mpd_isnegative(a)) {
  4312. mpd_seterror(result, MPD_Invalid_operation, status);
  4313. return;
  4314. }
  4315. if (_mpd_cmp(a, &one) == 0) {
  4316. _settriple(result, MPD_POS, 0, 0);
  4317. return;
  4318. }
  4319. /*
  4320. * Check if the result will overflow (0 < x, x != 1):
  4321. * 1) log10(x) < 0 iff adjexp(x) < 0
  4322. * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y)
  4323. * 3) 0 < x /\ x != 1 ==> 2 * abs(log10(x)) < abs(log(x))
  4324. * 4) adjexp(x) <= log10(x) < adjexp(x) + 1
  4325. *
  4326. * Case adjexp(x) >= 0:
  4327. * 5) 2 * adjexp(x) < abs(log(x))
  4328. * Case adjexp(x) > 0:
  4329. * 6) adjexp(2 * adjexp(x)) <= adjexp(abs(log(x)))
  4330. * Case adjexp(x) == 0:
  4331. * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered)
  4332. *
  4333. * Case adjexp(x) < 0:
  4334. * 7) 2 * (-adjexp(x) - 1) < abs(log(x))
  4335. * Case adjexp(x) < -1:
  4336. * 8) adjexp(2 * (-adjexp(x) - 1)) <= adjexp(abs(log(x)))
  4337. * Case adjexp(x) == -1:
  4338. * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered)
  4339. */
  4340. adjexp = mpd_adjexp(a);
  4341. t = (adjexp < 0) ? -adjexp-1 : adjexp;
  4342. t *= 2;
  4343. if (mpd_exp_digits(t)-1 > ctx->emax) {
  4344. *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
  4345. mpd_setspecial(result, (adjexp<0), MPD_INF);
  4346. return;
  4347. }
  4348. workctx = *ctx;
  4349. workctx.round = MPD_ROUND_HALF_EVEN;
  4350. if (ctx->allcr) {
  4351. MPD_NEW_STATIC(t1, 0,0,0,0);
  4352. MPD_NEW_STATIC(t2, 0,0,0,0);
  4353. MPD_NEW_STATIC(ulp, 0,0,0,0);
  4354. MPD_NEW_STATIC(aa, 0,0,0,0);
  4355. mpd_ssize_t prec;
  4356. if (result == a) {
  4357. if (!mpd_qcopy(&aa, a, status)) {
  4358. mpd_seterror(result, MPD_Malloc_error, status);
  4359. return;
  4360. }
  4361. a = &aa;
  4362. }
  4363. workctx.clamp = 0;
  4364. prec = ctx->prec + 3;
  4365. while (1) {
  4366. workctx.prec = prec;
  4367. _mpd_qln(result, a, &workctx, status);
  4368. _ssettriple(&ulp, MPD_POS, 1,
  4369. result->exp + result->digits-workctx.prec);
  4370. workctx.prec = ctx->prec;
  4371. mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status);
  4372. mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status);
  4373. if (mpd_isspecial(result) || mpd_iszerocoeff(result) ||
  4374. mpd_qcmp(&t1, &t2, status) == 0) {
  4375. workctx.clamp = ctx->clamp;
  4376. mpd_check_underflow(result, &workctx, status);
  4377. mpd_qfinalize(result, &workctx, status);
  4378. break;
  4379. }
  4380. prec += MPD_RDIGITS;
  4381. }
  4382. mpd_del(&t1);
  4383. mpd_del(&t2);
  4384. mpd_del(&ulp);
  4385. mpd_del(&aa);
  4386. }
  4387. else {
  4388. _mpd_qln(result, a, &workctx, status);
  4389. mpd_check_underflow(result, &workctx, status);
  4390. mpd_qfinalize(result, &workctx, status);
  4391. }
  4392. }
  4393. /*
  4394. * Internal log10() function that does not check for specials, zero or one.
  4395. * Case SKIP_FINALIZE:
  4396. * Relative error: abs(result - log10(a)) < 0.1 * 10**-prec * abs(log10(a))
  4397. * Case DO_FINALIZE:
  4398. * Ulp error: abs(result - log10(a)) < ulp(log10(a))
  4399. */
  4400. enum {SKIP_FINALIZE, DO_FINALIZE};
  4401. static void
  4402. _mpd_qlog10(int action, mpd_t *result, const mpd_t *a,
  4403. const mpd_context_t *ctx, uint32_t *status)
  4404. {
  4405. mpd_context_t workctx;
  4406. MPD_NEW_STATIC(ln10,0,0,0,0);
  4407. mpd_maxcontext(&workctx);
  4408. workctx.prec = ctx->prec + 3;
  4409. /* relative error: 0.1 * 10**(-p-3). The specific underflow shortcut
  4410. * in _mpd_qln() does not change the final result. */
  4411. _mpd_qln(result, a, &workctx, status);
  4412. /* relative error: 5 * 10**(-p-3) */
  4413. mpd_qln10(&ln10, workctx.prec, status);
  4414. if (action == DO_FINALIZE) {
  4415. workctx = *ctx;
  4416. workctx.round = MPD_ROUND_HALF_EVEN;
  4417. }
  4418. /* SKIP_FINALIZE: relative error: 5 * 10**(-p-3) */
  4419. _mpd_qdiv(NO_IDEAL_EXP, result, result, &ln10, &workctx, status);
  4420. mpd_del(&ln10);
  4421. }
  4422. /* log10(a) */
  4423. void
  4424. mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  4425. uint32_t *status)
  4426. {
  4427. mpd_context_t workctx;
  4428. mpd_ssize_t adjexp, t;
  4429. workctx = *ctx;
  4430. workctx.round = MPD_ROUND_HALF_EVEN;
  4431. if (mpd_isspecial(a)) {
  4432. if (mpd_qcheck_nan(result, a, ctx, status)) {
  4433. return;
  4434. }
  4435. if (mpd_isnegative(a)) {
  4436. mpd_seterror(result, MPD_Invalid_operation, status);
  4437. return;
  4438. }
  4439. mpd_setspecial(result, MPD_POS, MPD_INF);
  4440. return;
  4441. }
  4442. if (mpd_iszerocoeff(a)) {
  4443. mpd_setspecial(result, MPD_NEG, MPD_INF);
  4444. return;
  4445. }
  4446. if (mpd_isnegative(a)) {
  4447. mpd_seterror(result, MPD_Invalid_operation, status);
  4448. return;
  4449. }
  4450. if (mpd_coeff_ispow10(a)) {
  4451. uint8_t sign = 0;
  4452. adjexp = mpd_adjexp(a);
  4453. if (adjexp < 0) {
  4454. sign = 1;
  4455. adjexp = -adjexp;
  4456. }
  4457. _settriple(result, sign, adjexp, 0);
  4458. mpd_qfinalize(result, &workctx, status);
  4459. return;
  4460. }
  4461. /*
  4462. * Check if the result will overflow (0 < x, x != 1):
  4463. * 1) log10(x) < 0 iff adjexp(x) < 0
  4464. * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y)
  4465. * 3) adjexp(x) <= log10(x) < adjexp(x) + 1
  4466. *
  4467. * Case adjexp(x) >= 0:
  4468. * 4) adjexp(x) <= abs(log10(x))
  4469. * Case adjexp(x) > 0:
  4470. * 5) adjexp(adjexp(x)) <= adjexp(abs(log10(x)))
  4471. * Case adjexp(x) == 0:
  4472. * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered)
  4473. *
  4474. * Case adjexp(x) < 0:
  4475. * 6) -adjexp(x) - 1 < abs(log10(x))
  4476. * Case adjexp(x) < -1:
  4477. * 7) adjexp(-adjexp(x) - 1) <= adjexp(abs(log(x)))
  4478. * Case adjexp(x) == -1:
  4479. * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered)
  4480. */
  4481. adjexp = mpd_adjexp(a);
  4482. t = (adjexp < 0) ? -adjexp-1 : adjexp;
  4483. if (mpd_exp_digits(t)-1 > ctx->emax) {
  4484. *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
  4485. mpd_setspecial(result, (adjexp<0), MPD_INF);
  4486. return;
  4487. }
  4488. if (ctx->allcr) {
  4489. MPD_NEW_STATIC(t1, 0,0,0,0);
  4490. MPD_NEW_STATIC(t2, 0,0,0,0);
  4491. MPD_NEW_STATIC(ulp, 0,0,0,0);
  4492. MPD_NEW_STATIC(aa, 0,0,0,0);
  4493. mpd_ssize_t prec;
  4494. if (result == a) {
  4495. if (!mpd_qcopy(&aa, a, status)) {
  4496. mpd_seterror(result, MPD_Malloc_error, status);
  4497. return;
  4498. }
  4499. a = &aa;
  4500. }
  4501. workctx.clamp = 0;
  4502. prec = ctx->prec + 3;
  4503. while (1) {
  4504. workctx.prec = prec;
  4505. _mpd_qlog10(SKIP_FINALIZE, result, a, &workctx, status);
  4506. _ssettriple(&ulp, MPD_POS, 1,
  4507. result->exp + result->digits-workctx.prec);
  4508. workctx.prec = ctx->prec;
  4509. mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status);
  4510. mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status);
  4511. if (mpd_isspecial(result) || mpd_iszerocoeff(result) ||
  4512. mpd_qcmp(&t1, &t2, status) == 0) {
  4513. workctx.clamp = ctx->clamp;
  4514. mpd_check_underflow(result, &workctx, status);
  4515. mpd_qfinalize(result, &workctx, status);
  4516. break;
  4517. }
  4518. prec += MPD_RDIGITS;
  4519. }
  4520. mpd_del(&t1);
  4521. mpd_del(&t2);
  4522. mpd_del(&ulp);
  4523. mpd_del(&aa);
  4524. }
  4525. else {
  4526. _mpd_qlog10(DO_FINALIZE, result, a, &workctx, status);
  4527. mpd_check_underflow(result, &workctx, status);
  4528. }
  4529. }
  4530. /*
  4531. * Maximum of the two operands. Attention: If one operand is a quiet NaN and the
  4532. * other is numeric, the numeric operand is returned. This may not be what one
  4533. * expects.
  4534. */
  4535. void
  4536. mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b,
  4537. const mpd_context_t *ctx, uint32_t *status)
  4538. {
  4539. int c;
  4540. if (mpd_isqnan(a) && !mpd_isnan(b)) {
  4541. mpd_qcopy(result, b, status);
  4542. }
  4543. else if (mpd_isqnan(b) && !mpd_isnan(a)) {
  4544. mpd_qcopy(result, a, status);
  4545. }
  4546. else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  4547. return;
  4548. }
  4549. else {
  4550. c = _mpd_cmp(a, b);
  4551. if (c == 0) {
  4552. c = _mpd_cmp_numequal(a, b);
  4553. }
  4554. if (c < 0) {
  4555. mpd_qcopy(result, b, status);
  4556. }
  4557. else {
  4558. mpd_qcopy(result, a, status);
  4559. }
  4560. }
  4561. mpd_qfinalize(result, ctx, status);
  4562. }
  4563. /*
  4564. * Maximum magnitude: Same as mpd_max(), but compares the operands with their
  4565. * sign ignored.
  4566. */
  4567. void
  4568. mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b,
  4569. const mpd_context_t *ctx, uint32_t *status)
  4570. {
  4571. int c;
  4572. if (mpd_isqnan(a) && !mpd_isnan(b)) {
  4573. mpd_qcopy(result, b, status);
  4574. }
  4575. else if (mpd_isqnan(b) && !mpd_isnan(a)) {
  4576. mpd_qcopy(result, a, status);
  4577. }
  4578. else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  4579. return;
  4580. }
  4581. else {
  4582. c = _mpd_cmp_abs(a, b);
  4583. if (c == 0) {
  4584. c = _mpd_cmp_numequal(a, b);
  4585. }
  4586. if (c < 0) {
  4587. mpd_qcopy(result, b, status);
  4588. }
  4589. else {
  4590. mpd_qcopy(result, a, status);
  4591. }
  4592. }
  4593. mpd_qfinalize(result, ctx, status);
  4594. }
  4595. /*
  4596. * Minimum of the two operands. Attention: If one operand is a quiet NaN and the
  4597. * other is numeric, the numeric operand is returned. This may not be what one
  4598. * expects.
  4599. */
  4600. void
  4601. mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b,
  4602. const mpd_context_t *ctx, uint32_t *status)
  4603. {
  4604. int c;
  4605. if (mpd_isqnan(a) && !mpd_isnan(b)) {
  4606. mpd_qcopy(result, b, status);
  4607. }
  4608. else if (mpd_isqnan(b) && !mpd_isnan(a)) {
  4609. mpd_qcopy(result, a, status);
  4610. }
  4611. else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  4612. return;
  4613. }
  4614. else {
  4615. c = _mpd_cmp(a, b);
  4616. if (c == 0) {
  4617. c = _mpd_cmp_numequal(a, b);
  4618. }
  4619. if (c < 0) {
  4620. mpd_qcopy(result, a, status);
  4621. }
  4622. else {
  4623. mpd_qcopy(result, b, status);
  4624. }
  4625. }
  4626. mpd_qfinalize(result, ctx, status);
  4627. }
  4628. /*
  4629. * Minimum magnitude: Same as mpd_min(), but compares the operands with their
  4630. * sign ignored.
  4631. */
  4632. void
  4633. mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b,
  4634. const mpd_context_t *ctx, uint32_t *status)
  4635. {
  4636. int c;
  4637. if (mpd_isqnan(a) && !mpd_isnan(b)) {
  4638. mpd_qcopy(result, b, status);
  4639. }
  4640. else if (mpd_isqnan(b) && !mpd_isnan(a)) {
  4641. mpd_qcopy(result, a, status);
  4642. }
  4643. else if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  4644. return;
  4645. }
  4646. else {
  4647. c = _mpd_cmp_abs(a, b);
  4648. if (c == 0) {
  4649. c = _mpd_cmp_numequal(a, b);
  4650. }
  4651. if (c < 0) {
  4652. mpd_qcopy(result, a, status);
  4653. }
  4654. else {
  4655. mpd_qcopy(result, b, status);
  4656. }
  4657. }
  4658. mpd_qfinalize(result, ctx, status);
  4659. }
  4660. /* Minimum space needed for the result array in _karatsuba_rec(). */
  4661. static inline mpd_size_t
  4662. _kmul_resultsize(mpd_size_t la, mpd_size_t lb)
  4663. {
  4664. mpd_size_t n, m;
  4665. n = add_size_t(la, lb);
  4666. n = add_size_t(n, 1);
  4667. m = (la+1)/2 + 1;
  4668. m = mul_size_t(m, 3);
  4669. return (m > n) ? m : n;
  4670. }
  4671. /* Work space needed in _karatsuba_rec(). lim >= 4 */
  4672. static inline mpd_size_t
  4673. _kmul_worksize(mpd_size_t n, mpd_size_t lim)
  4674. {
  4675. mpd_size_t m;
  4676. if (n <= lim) {
  4677. return 0;
  4678. }
  4679. m = (n+1)/2 + 1;
  4680. return add_size_t(mul_size_t(m, 2), _kmul_worksize(m, lim));
  4681. }
  4682. #define MPD_KARATSUBA_BASECASE 16 /* must be >= 4 */
  4683. /*
  4684. * Add the product of a and b to c.
  4685. * c must be _kmul_resultsize(la, lb) in size.
  4686. * w is used as a work array and must be _kmul_worksize(a, lim) in size.
  4687. * Roman E. Maeder, Storage Allocation for the Karatsuba Integer Multiplication
  4688. * Algorithm. In "Design and implementation of symbolic computation systems",
  4689. * Springer, 1993, ISBN 354057235X, 9783540572350.
  4690. */
  4691. static void
  4692. _karatsuba_rec(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b,
  4693. mpd_uint_t *w, mpd_size_t la, mpd_size_t lb)
  4694. {
  4695. mpd_size_t m, lt;
  4696. assert(la >= lb && lb > 0);
  4697. assert(la <= MPD_KARATSUBA_BASECASE || w != NULL);
  4698. if (la <= MPD_KARATSUBA_BASECASE) {
  4699. _mpd_basemul(c, a, b, la, lb);
  4700. return;
  4701. }
  4702. m = (la+1)/2; /* ceil(la/2) */
  4703. /* lb <= m < la */
  4704. if (lb <= m) {
  4705. /* lb can now be larger than la-m */
  4706. if (lb > la-m) {
  4707. lt = lb + lb + 1; /* space needed for result array */
  4708. mpd_uint_zero(w, lt); /* clear result array */
  4709. _karatsuba_rec(w, b, a+m, w+lt, lb, la-m); /* b*ah */
  4710. }
  4711. else {
  4712. lt = (la-m) + (la-m) + 1; /* space needed for result array */
  4713. mpd_uint_zero(w, lt); /* clear result array */
  4714. _karatsuba_rec(w, a+m, b, w+lt, la-m, lb); /* ah*b */
  4715. }
  4716. _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */
  4717. lt = m + m + 1; /* space needed for the result array */
  4718. mpd_uint_zero(w, lt); /* clear result array */
  4719. _karatsuba_rec(w, a, b, w+lt, m, lb); /* al*b */
  4720. _mpd_baseaddto(c, w, m+lb); /* add al*b */
  4721. return;
  4722. }
  4723. /* la >= lb > m */
  4724. memcpy(w, a, m * sizeof *w);
  4725. w[m] = 0;
  4726. _mpd_baseaddto(w, a+m, la-m);
  4727. memcpy(w+(m+1), b, m * sizeof *w);
  4728. w[m+1+m] = 0;
  4729. _mpd_baseaddto(w+(m+1), b+m, lb-m);
  4730. _karatsuba_rec(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1);
  4731. lt = (la-m) + (la-m) + 1;
  4732. mpd_uint_zero(w, lt);
  4733. _karatsuba_rec(w, a+m, b+m, w+lt, la-m, lb-m);
  4734. _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m));
  4735. _mpd_basesubfrom(c+m, w, (la-m) + (lb-m));
  4736. lt = m + m + 1;
  4737. mpd_uint_zero(w, lt);
  4738. _karatsuba_rec(w, a, b, w+lt, m, m);
  4739. _mpd_baseaddto(c, w, m+m);
  4740. _mpd_basesubfrom(c+m, w, m+m);
  4741. return;
  4742. }
  4743. /*
  4744. * Multiply u and v, using Karatsuba multiplication. Returns a pointer
  4745. * to the result or NULL in case of failure (malloc error).
  4746. * Conditions: ulen >= vlen, ulen >= 4
  4747. */
  4748. static mpd_uint_t *
  4749. _mpd_kmul(const mpd_uint_t *u, const mpd_uint_t *v,
  4750. mpd_size_t ulen, mpd_size_t vlen,
  4751. mpd_size_t *rsize)
  4752. {
  4753. mpd_uint_t *result = NULL, *w = NULL;
  4754. mpd_size_t m;
  4755. assert(ulen >= 4);
  4756. assert(ulen >= vlen);
  4757. *rsize = _kmul_resultsize(ulen, vlen);
  4758. if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) {
  4759. return NULL;
  4760. }
  4761. m = _kmul_worksize(ulen, MPD_KARATSUBA_BASECASE);
  4762. if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) {
  4763. mpd_free(result);
  4764. return NULL;
  4765. }
  4766. _karatsuba_rec(result, u, v, w, ulen, vlen);
  4767. if (w) mpd_free(w);
  4768. return result;
  4769. }
  4770. /*
  4771. * Determine the minimum length for the number theoretic transform. Valid
  4772. * transform lengths are 2**n or 3*2**n, where 2**n <= MPD_MAXTRANSFORM_2N.
  4773. * The function finds the shortest length m such that rsize <= m.
  4774. */
  4775. static inline mpd_size_t
  4776. _mpd_get_transform_len(mpd_size_t rsize)
  4777. {
  4778. mpd_size_t log2rsize;
  4779. mpd_size_t x, step;
  4780. assert(rsize >= 4);
  4781. log2rsize = mpd_bsr(rsize);
  4782. if (rsize <= 1024) {
  4783. /* 2**n is faster in this range. */
  4784. x = ((mpd_size_t)1)<<log2rsize;
  4785. return (rsize == x) ? x : x<<1;
  4786. }
  4787. else if (rsize <= MPD_MAXTRANSFORM_2N) {
  4788. x = ((mpd_size_t)1)<<log2rsize;
  4789. if (rsize == x) return x;
  4790. step = x>>1;
  4791. x += step;
  4792. return (rsize <= x) ? x : x + step;
  4793. }
  4794. else if (rsize <= MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2) {
  4795. return MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2;
  4796. }
  4797. else if (rsize <= 3*MPD_MAXTRANSFORM_2N) {
  4798. return 3*MPD_MAXTRANSFORM_2N;
  4799. }
  4800. else {
  4801. return MPD_SIZE_MAX;
  4802. }
  4803. }
  4804. #ifdef PPRO
  4805. #ifndef _MSC_VER
  4806. static inline unsigned short
  4807. _mpd_get_control87(void)
  4808. {
  4809. unsigned short cw;
  4810. __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
  4811. return cw;
  4812. }
  4813. static inline void
  4814. _mpd_set_control87(unsigned short cw)
  4815. {
  4816. __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
  4817. }
  4818. #endif
  4819. static unsigned int
  4820. mpd_set_fenv(void)
  4821. {
  4822. unsigned int cw;
  4823. #ifdef _MSC_VER
  4824. unsigned int flags =
  4825. _EM_INVALID|_EM_DENORMAL|_EM_ZERODIVIDE|_EM_OVERFLOW|
  4826. _EM_UNDERFLOW|_EM_INEXACT|_RC_CHOP|_PC_64;
  4827. unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC;
  4828. unsigned int dummy;
  4829. __control87_2(0, 0, &cw, NULL);
  4830. __control87_2(flags, mask, &dummy, NULL);
  4831. #else
  4832. cw = _mpd_get_control87();
  4833. _mpd_set_control87(cw|0xF3F);
  4834. #endif
  4835. return cw;
  4836. }
  4837. static void
  4838. mpd_restore_fenv(unsigned int cw)
  4839. {
  4840. #ifdef _MSC_VER
  4841. unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC;
  4842. unsigned int dummy;
  4843. __control87_2(cw, mask, &dummy, NULL);
  4844. #else
  4845. _mpd_set_control87((unsigned short)cw);
  4846. #endif
  4847. }
  4848. #endif /* PPRO */
  4849. /*
  4850. * Multiply u and v, using the fast number theoretic transform. Returns
  4851. * a pointer to the result or NULL in case of failure (malloc error).
  4852. */
  4853. static mpd_uint_t *
  4854. _mpd_fntmul(const mpd_uint_t *u, const mpd_uint_t *v,
  4855. mpd_size_t ulen, mpd_size_t vlen,
  4856. mpd_size_t *rsize)
  4857. {
  4858. mpd_uint_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *vtmp = NULL;
  4859. mpd_size_t n;
  4860. #ifdef PPRO
  4861. unsigned int cw;
  4862. cw = mpd_set_fenv();
  4863. #endif
  4864. *rsize = add_size_t(ulen, vlen);
  4865. if ((n = _mpd_get_transform_len(*rsize)) == MPD_SIZE_MAX) {
  4866. goto malloc_error;
  4867. }
  4868. if ((c1 = mpd_calloc(n, sizeof *c1)) == NULL) {
  4869. goto malloc_error;
  4870. }
  4871. if ((c2 = mpd_calloc(n, sizeof *c2)) == NULL) {
  4872. goto malloc_error;
  4873. }
  4874. if ((c3 = mpd_calloc(n, sizeof *c3)) == NULL) {
  4875. goto malloc_error;
  4876. }
  4877. memcpy(c1, u, ulen * (sizeof *c1));
  4878. memcpy(c2, u, ulen * (sizeof *c2));
  4879. memcpy(c3, u, ulen * (sizeof *c3));
  4880. if (u == v) {
  4881. if (!fnt_autoconvolute(c1, n, P1) ||
  4882. !fnt_autoconvolute(c2, n, P2) ||
  4883. !fnt_autoconvolute(c3, n, P3)) {
  4884. goto malloc_error;
  4885. }
  4886. }
  4887. else {
  4888. if ((vtmp = mpd_calloc(n, sizeof *vtmp)) == NULL) {
  4889. goto malloc_error;
  4890. }
  4891. memcpy(vtmp, v, vlen * (sizeof *vtmp));
  4892. if (!fnt_convolute(c1, vtmp, n, P1)) {
  4893. mpd_free(vtmp);
  4894. goto malloc_error;
  4895. }
  4896. memcpy(vtmp, v, vlen * (sizeof *vtmp));
  4897. mpd_uint_zero(vtmp+vlen, n-vlen);
  4898. if (!fnt_convolute(c2, vtmp, n, P2)) {
  4899. mpd_free(vtmp);
  4900. goto malloc_error;
  4901. }
  4902. memcpy(vtmp, v, vlen * (sizeof *vtmp));
  4903. mpd_uint_zero(vtmp+vlen, n-vlen);
  4904. if (!fnt_convolute(c3, vtmp, n, P3)) {
  4905. mpd_free(vtmp);
  4906. goto malloc_error;
  4907. }
  4908. mpd_free(vtmp);
  4909. }
  4910. crt3(c1, c2, c3, *rsize);
  4911. out:
  4912. #ifdef PPRO
  4913. mpd_restore_fenv(cw);
  4914. #endif
  4915. if (c2) mpd_free(c2);
  4916. if (c3) mpd_free(c3);
  4917. return c1;
  4918. malloc_error:
  4919. if (c1) mpd_free(c1);
  4920. c1 = NULL;
  4921. goto out;
  4922. }
  4923. /*
  4924. * Karatsuba multiplication with FNT/basemul as the base case.
  4925. */
  4926. static int
  4927. _karatsuba_rec_fnt(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b,
  4928. mpd_uint_t *w, mpd_size_t la, mpd_size_t lb)
  4929. {
  4930. mpd_size_t m, lt;
  4931. assert(la >= lb && lb > 0);
  4932. assert(la <= 3*(MPD_MAXTRANSFORM_2N/2) || w != NULL);
  4933. if (la <= 3*(MPD_MAXTRANSFORM_2N/2)) {
  4934. if (lb <= 192) {
  4935. _mpd_basemul(c, b, a, lb, la);
  4936. }
  4937. else {
  4938. mpd_uint_t *result;
  4939. mpd_size_t dummy;
  4940. if ((result = _mpd_fntmul(a, b, la, lb, &dummy)) == NULL) {
  4941. return 0;
  4942. }
  4943. memcpy(c, result, (la+lb) * (sizeof *result));
  4944. mpd_free(result);
  4945. }
  4946. return 1;
  4947. }
  4948. m = (la+1)/2; /* ceil(la/2) */
  4949. /* lb <= m < la */
  4950. if (lb <= m) {
  4951. /* lb can now be larger than la-m */
  4952. if (lb > la-m) {
  4953. lt = lb + lb + 1; /* space needed for result array */
  4954. mpd_uint_zero(w, lt); /* clear result array */
  4955. if (!_karatsuba_rec_fnt(w, b, a+m, w+lt, lb, la-m)) { /* b*ah */
  4956. return 0; /* GCOV_UNLIKELY */
  4957. }
  4958. }
  4959. else {
  4960. lt = (la-m) + (la-m) + 1; /* space needed for result array */
  4961. mpd_uint_zero(w, lt); /* clear result array */
  4962. if (!_karatsuba_rec_fnt(w, a+m, b, w+lt, la-m, lb)) { /* ah*b */
  4963. return 0; /* GCOV_UNLIKELY */
  4964. }
  4965. }
  4966. _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */
  4967. lt = m + m + 1; /* space needed for the result array */
  4968. mpd_uint_zero(w, lt); /* clear result array */
  4969. if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, lb)) { /* al*b */
  4970. return 0; /* GCOV_UNLIKELY */
  4971. }
  4972. _mpd_baseaddto(c, w, m+lb); /* add al*b */
  4973. return 1;
  4974. }
  4975. /* la >= lb > m */
  4976. memcpy(w, a, m * sizeof *w);
  4977. w[m] = 0;
  4978. _mpd_baseaddto(w, a+m, la-m);
  4979. memcpy(w+(m+1), b, m * sizeof *w);
  4980. w[m+1+m] = 0;
  4981. _mpd_baseaddto(w+(m+1), b+m, lb-m);
  4982. if (!_karatsuba_rec_fnt(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1)) {
  4983. return 0; /* GCOV_UNLIKELY */
  4984. }
  4985. lt = (la-m) + (la-m) + 1;
  4986. mpd_uint_zero(w, lt);
  4987. if (!_karatsuba_rec_fnt(w, a+m, b+m, w+lt, la-m, lb-m)) {
  4988. return 0; /* GCOV_UNLIKELY */
  4989. }
  4990. _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m));
  4991. _mpd_basesubfrom(c+m, w, (la-m) + (lb-m));
  4992. lt = m + m + 1;
  4993. mpd_uint_zero(w, lt);
  4994. if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, m)) {
  4995. return 0; /* GCOV_UNLIKELY */
  4996. }
  4997. _mpd_baseaddto(c, w, m+m);
  4998. _mpd_basesubfrom(c+m, w, m+m);
  4999. return 1;
  5000. }
  5001. /*
  5002. * Multiply u and v, using Karatsuba multiplication with the FNT as the
  5003. * base case. Returns a pointer to the result or NULL in case of failure
  5004. * (malloc error). Conditions: ulen >= vlen, ulen >= 4.
  5005. */
  5006. static mpd_uint_t *
  5007. _mpd_kmul_fnt(const mpd_uint_t *u, const mpd_uint_t *v,
  5008. mpd_size_t ulen, mpd_size_t vlen,
  5009. mpd_size_t *rsize)
  5010. {
  5011. mpd_uint_t *result = NULL, *w = NULL;
  5012. mpd_size_t m;
  5013. assert(ulen >= 4);
  5014. assert(ulen >= vlen);
  5015. *rsize = _kmul_resultsize(ulen, vlen);
  5016. if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) {
  5017. return NULL;
  5018. }
  5019. m = _kmul_worksize(ulen, 3*(MPD_MAXTRANSFORM_2N/2));
  5020. if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) {
  5021. mpd_free(result); /* GCOV_UNLIKELY */
  5022. return NULL; /* GCOV_UNLIKELY */
  5023. }
  5024. if (!_karatsuba_rec_fnt(result, u, v, w, ulen, vlen)) {
  5025. mpd_free(result);
  5026. result = NULL;
  5027. }
  5028. if (w) mpd_free(w);
  5029. return result;
  5030. }
  5031. /* Deal with the special cases of multiplying infinities. */
  5032. static void
  5033. _mpd_qmul_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status)
  5034. {
  5035. if (mpd_isinfinite(a)) {
  5036. if (mpd_iszero(b)) {
  5037. mpd_seterror(result, MPD_Invalid_operation, status);
  5038. }
  5039. else {
  5040. mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
  5041. }
  5042. return;
  5043. }
  5044. assert(mpd_isinfinite(b));
  5045. if (mpd_iszero(a)) {
  5046. mpd_seterror(result, MPD_Invalid_operation, status);
  5047. }
  5048. else {
  5049. mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
  5050. }
  5051. }
  5052. /*
  5053. * Internal function: Multiply a and b. _mpd_qmul deals with specials but
  5054. * does NOT finalize the result. This is for use in mpd_fma().
  5055. */
  5056. static inline void
  5057. _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
  5058. const mpd_context_t *ctx, uint32_t *status)
  5059. {
  5060. const mpd_t *big = a, *small = b;
  5061. mpd_uint_t *rdata = NULL;
  5062. mpd_uint_t rbuf[MPD_MINALLOC_MAX];
  5063. mpd_size_t rsize, i;
  5064. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  5065. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  5066. return;
  5067. }
  5068. _mpd_qmul_inf(result, a, b, status);
  5069. return;
  5070. }
  5071. if (small->len > big->len) {
  5072. _mpd_ptrswap(&big, &small);
  5073. }
  5074. rsize = big->len + small->len;
  5075. if (big->len == 1) {
  5076. _mpd_singlemul(result->data, big->data[0], small->data[0]);
  5077. goto finish;
  5078. }
  5079. if (rsize <= (mpd_size_t)MPD_MINALLOC_MAX) {
  5080. if (big->len == 2) {
  5081. _mpd_mul_2_le2(rbuf, big->data, small->data, small->len);
  5082. }
  5083. else {
  5084. mpd_uint_zero(rbuf, rsize);
  5085. if (small->len == 1) {
  5086. _mpd_shortmul(rbuf, big->data, big->len, small->data[0]);
  5087. }
  5088. else {
  5089. _mpd_basemul(rbuf, small->data, big->data, small->len, big->len);
  5090. }
  5091. }
  5092. if (!mpd_qresize(result, rsize, status)) {
  5093. return;
  5094. }
  5095. for(i = 0; i < rsize; i++) {
  5096. result->data[i] = rbuf[i];
  5097. }
  5098. goto finish;
  5099. }
  5100. if (small->len <= 256) {
  5101. rdata = mpd_calloc(rsize, sizeof *rdata);
  5102. if (rdata != NULL) {
  5103. if (small->len == 1) {
  5104. _mpd_shortmul(rdata, big->data, big->len, small->data[0]);
  5105. }
  5106. else {
  5107. _mpd_basemul(rdata, small->data, big->data, small->len, big->len);
  5108. }
  5109. }
  5110. }
  5111. else if (rsize <= 1024) {
  5112. rdata = _mpd_kmul(big->data, small->data, big->len, small->len, &rsize);
  5113. }
  5114. else if (rsize <= 3*MPD_MAXTRANSFORM_2N) {
  5115. rdata = _mpd_fntmul(big->data, small->data, big->len, small->len, &rsize);
  5116. }
  5117. else {
  5118. rdata = _mpd_kmul_fnt(big->data, small->data, big->len, small->len, &rsize);
  5119. }
  5120. if (rdata == NULL) {
  5121. mpd_seterror(result, MPD_Malloc_error, status);
  5122. return;
  5123. }
  5124. if (mpd_isdynamic_data(result)) {
  5125. mpd_free(result->data);
  5126. }
  5127. result->data = rdata;
  5128. result->alloc = rsize;
  5129. mpd_set_dynamic_data(result);
  5130. finish:
  5131. mpd_set_flags(result, mpd_sign(a)^mpd_sign(b));
  5132. result->exp = big->exp + small->exp;
  5133. result->len = _mpd_real_size(result->data, rsize);
  5134. /* resize to smaller cannot fail */
  5135. mpd_qresize(result, result->len, status);
  5136. mpd_setdigits(result);
  5137. }
  5138. /* Multiply a and b. */
  5139. void
  5140. mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
  5141. const mpd_context_t *ctx, uint32_t *status)
  5142. {
  5143. _mpd_qmul(result, a, b, ctx, status);
  5144. mpd_qfinalize(result, ctx, status);
  5145. }
  5146. /* Multiply a and b. Set NaN/Invalid_operation if the result is inexact. */
  5147. static void
  5148. _mpd_qmul_exact(mpd_t *result, const mpd_t *a, const mpd_t *b,
  5149. const mpd_context_t *ctx, uint32_t *status)
  5150. {
  5151. uint32_t workstatus = 0;
  5152. mpd_qmul(result, a, b, ctx, &workstatus);
  5153. *status |= workstatus;
  5154. if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) {
  5155. mpd_seterror(result, MPD_Invalid_operation, status);
  5156. }
  5157. }
  5158. /* Multiply decimal and mpd_ssize_t. */
  5159. void
  5160. mpd_qmul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
  5161. const mpd_context_t *ctx, uint32_t *status)
  5162. {
  5163. mpd_context_t maxcontext;
  5164. MPD_NEW_STATIC(bb,0,0,0,0);
  5165. mpd_maxcontext(&maxcontext);
  5166. mpd_qsset_ssize(&bb, b, &maxcontext, status);
  5167. mpd_qmul(result, a, &bb, ctx, status);
  5168. mpd_del(&bb);
  5169. }
  5170. /* Multiply decimal and mpd_uint_t. */
  5171. void
  5172. mpd_qmul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
  5173. const mpd_context_t *ctx, uint32_t *status)
  5174. {
  5175. mpd_context_t maxcontext;
  5176. MPD_NEW_STATIC(bb,0,0,0,0);
  5177. mpd_maxcontext(&maxcontext);
  5178. mpd_qsset_uint(&bb, b, &maxcontext, status);
  5179. mpd_qmul(result, a, &bb, ctx, status);
  5180. mpd_del(&bb);
  5181. }
  5182. void
  5183. mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b,
  5184. const mpd_context_t *ctx, uint32_t *status)
  5185. {
  5186. mpd_qmul_ssize(result, a, b, ctx, status);
  5187. }
  5188. void
  5189. mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b,
  5190. const mpd_context_t *ctx, uint32_t *status)
  5191. {
  5192. mpd_qmul_uint(result, a, b, ctx, status);
  5193. }
  5194. #ifdef CONFIG_64
  5195. void
  5196. mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b,
  5197. const mpd_context_t *ctx, uint32_t *status)
  5198. {
  5199. mpd_qmul_ssize(result, a, b, ctx, status);
  5200. }
  5201. void
  5202. mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  5203. const mpd_context_t *ctx, uint32_t *status)
  5204. {
  5205. mpd_qmul_uint(result, a, b, ctx, status);
  5206. }
  5207. #elif !defined(LEGACY_COMPILER)
  5208. /* Multiply decimal and int64_t. */
  5209. void
  5210. mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b,
  5211. const mpd_context_t *ctx, uint32_t *status)
  5212. {
  5213. mpd_context_t maxcontext;
  5214. MPD_NEW_STATIC(bb,0,0,0,0);
  5215. mpd_maxcontext(&maxcontext);
  5216. mpd_qset_i64(&bb, b, &maxcontext, status);
  5217. mpd_qmul(result, a, &bb, ctx, status);
  5218. mpd_del(&bb);
  5219. }
  5220. /* Multiply decimal and uint64_t. */
  5221. void
  5222. mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b,
  5223. const mpd_context_t *ctx, uint32_t *status)
  5224. {
  5225. mpd_context_t maxcontext;
  5226. MPD_NEW_STATIC(bb,0,0,0,0);
  5227. mpd_maxcontext(&maxcontext);
  5228. mpd_qset_u64(&bb, b, &maxcontext, status);
  5229. mpd_qmul(result, a, &bb, ctx, status);
  5230. mpd_del(&bb);
  5231. }
  5232. #endif
  5233. /* Like the minus operator. */
  5234. void
  5235. mpd_qminus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  5236. uint32_t *status)
  5237. {
  5238. if (mpd_isspecial(a)) {
  5239. if (mpd_qcheck_nan(result, a, ctx, status)) {
  5240. return;
  5241. }
  5242. }
  5243. if (mpd_iszero(a) && ctx->round != MPD_ROUND_FLOOR) {
  5244. mpd_qcopy_abs(result, a, status);
  5245. }
  5246. else {
  5247. mpd_qcopy_negate(result, a, status);
  5248. }
  5249. mpd_qfinalize(result, ctx, status);
  5250. }
  5251. /* Like the plus operator. */
  5252. void
  5253. mpd_qplus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  5254. uint32_t *status)
  5255. {
  5256. if (mpd_isspecial(a)) {
  5257. if (mpd_qcheck_nan(result, a, ctx, status)) {
  5258. return;
  5259. }
  5260. }
  5261. if (mpd_iszero(a) && ctx->round != MPD_ROUND_FLOOR) {
  5262. mpd_qcopy_abs(result, a, status);
  5263. }
  5264. else {
  5265. mpd_qcopy(result, a, status);
  5266. }
  5267. mpd_qfinalize(result, ctx, status);
  5268. }
  5269. /* The largest representable number that is smaller than the operand. */
  5270. void
  5271. mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  5272. uint32_t *status)
  5273. {
  5274. mpd_context_t workctx;
  5275. MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1);
  5276. if (mpd_isspecial(a)) {
  5277. if (mpd_qcheck_nan(result, a, ctx, status)) {
  5278. return;
  5279. }
  5280. assert(mpd_isinfinite(a));
  5281. if (mpd_isnegative(a)) {
  5282. mpd_qcopy(result, a, status);
  5283. return;
  5284. }
  5285. else {
  5286. mpd_clear_flags(result);
  5287. mpd_qmaxcoeff(result, ctx, status);
  5288. if (mpd_isnan(result)) {
  5289. return;
  5290. }
  5291. result->exp = mpd_etop(ctx);
  5292. return;
  5293. }
  5294. }
  5295. mpd_workcontext(&workctx, ctx);
  5296. workctx.round = MPD_ROUND_FLOOR;
  5297. if (!mpd_qcopy(result, a, status)) {
  5298. return;
  5299. }
  5300. mpd_qfinalize(result, &workctx, &workctx.status);
  5301. if (workctx.status&(MPD_Inexact|MPD_Errors)) {
  5302. *status |= (workctx.status&MPD_Errors);
  5303. return;
  5304. }
  5305. workctx.status = 0;
  5306. mpd_qsub(result, a, &tiny, &workctx, &workctx.status);
  5307. *status |= (workctx.status&MPD_Errors);
  5308. }
  5309. /* The smallest representable number that is larger than the operand. */
  5310. void
  5311. mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  5312. uint32_t *status)
  5313. {
  5314. mpd_context_t workctx;
  5315. MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1);
  5316. if (mpd_isspecial(a)) {
  5317. if (mpd_qcheck_nan(result, a, ctx, status)) {
  5318. return;
  5319. }
  5320. assert(mpd_isinfinite(a));
  5321. if (mpd_ispositive(a)) {
  5322. mpd_qcopy(result, a, status);
  5323. }
  5324. else {
  5325. mpd_clear_flags(result);
  5326. mpd_qmaxcoeff(result, ctx, status);
  5327. if (mpd_isnan(result)) {
  5328. return;
  5329. }
  5330. mpd_set_flags(result, MPD_NEG);
  5331. result->exp = mpd_etop(ctx);
  5332. }
  5333. return;
  5334. }
  5335. mpd_workcontext(&workctx, ctx);
  5336. workctx.round = MPD_ROUND_CEILING;
  5337. if (!mpd_qcopy(result, a, status)) {
  5338. return;
  5339. }
  5340. mpd_qfinalize(result, &workctx, &workctx.status);
  5341. if (workctx.status & (MPD_Inexact|MPD_Errors)) {
  5342. *status |= (workctx.status&MPD_Errors);
  5343. return;
  5344. }
  5345. workctx.status = 0;
  5346. mpd_qadd(result, a, &tiny, &workctx, &workctx.status);
  5347. *status |= (workctx.status&MPD_Errors);
  5348. }
  5349. /*
  5350. * The number closest to the first operand that is in the direction towards
  5351. * the second operand.
  5352. */
  5353. void
  5354. mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b,
  5355. const mpd_context_t *ctx, uint32_t *status)
  5356. {
  5357. int c;
  5358. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  5359. return;
  5360. }
  5361. c = _mpd_cmp(a, b);
  5362. if (c == 0) {
  5363. mpd_qcopy_sign(result, a, b, status);
  5364. return;
  5365. }
  5366. if (c < 0) {
  5367. mpd_qnext_plus(result, a, ctx, status);
  5368. }
  5369. else {
  5370. mpd_qnext_minus(result, a, ctx, status);
  5371. }
  5372. if (mpd_isinfinite(result)) {
  5373. *status |= (MPD_Overflow|MPD_Rounded|MPD_Inexact);
  5374. }
  5375. else if (mpd_adjexp(result) < ctx->emin) {
  5376. *status |= (MPD_Underflow|MPD_Subnormal|MPD_Rounded|MPD_Inexact);
  5377. if (mpd_iszero(result)) {
  5378. *status |= MPD_Clamped;
  5379. }
  5380. }
  5381. }
  5382. /*
  5383. * Internal function: Integer power with mpd_uint_t exponent. The function
  5384. * can fail with MPD_Malloc_error.
  5385. *
  5386. * The error is equal to the error incurred in k-1 multiplications. Assuming
  5387. * the upper bound for the relative error in each operation:
  5388. *
  5389. * abs(err) = 5 * 10**-prec
  5390. * result = x**k * (1 + err)**(k-1)
  5391. */
  5392. static inline void
  5393. _mpd_qpow_uint(mpd_t *result, const mpd_t *base, mpd_uint_t exp,
  5394. uint8_t resultsign, const mpd_context_t *ctx, uint32_t *status)
  5395. {
  5396. uint32_t workstatus = 0;
  5397. mpd_uint_t n;
  5398. if (exp == 0) {
  5399. _settriple(result, resultsign, 1, 0); /* GCOV_NOT_REACHED */
  5400. return; /* GCOV_NOT_REACHED */
  5401. }
  5402. if (!mpd_qcopy(result, base, status)) {
  5403. return;
  5404. }
  5405. n = mpd_bits[mpd_bsr(exp)];
  5406. while (n >>= 1) {
  5407. mpd_qmul(result, result, result, ctx, &workstatus);
  5408. if (exp & n) {
  5409. mpd_qmul(result, result, base, ctx, &workstatus);
  5410. }
  5411. if (mpd_isspecial(result) ||
  5412. (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) {
  5413. break;
  5414. }
  5415. }
  5416. *status |= workstatus;
  5417. mpd_set_sign(result, resultsign);
  5418. }
  5419. /*
  5420. * Internal function: Integer power with mpd_t exponent, tbase and texp
  5421. * are modified!! Function can fail with MPD_Malloc_error.
  5422. *
  5423. * The error is equal to the error incurred in k multiplications. Assuming
  5424. * the upper bound for the relative error in each operation:
  5425. *
  5426. * abs(err) = 5 * 10**-prec
  5427. * result = x**k * (1 + err)**k
  5428. */
  5429. static inline void
  5430. _mpd_qpow_mpd(mpd_t *result, mpd_t *tbase, mpd_t *texp, uint8_t resultsign,
  5431. const mpd_context_t *ctx, uint32_t *status)
  5432. {
  5433. uint32_t workstatus = 0;
  5434. mpd_context_t maxctx;
  5435. MPD_NEW_CONST(two,0,0,1,1,1,2);
  5436. mpd_maxcontext(&maxctx);
  5437. /* resize to smaller cannot fail */
  5438. mpd_qcopy(result, &one, status);
  5439. while (!mpd_iszero(texp)) {
  5440. if (mpd_isodd(texp)) {
  5441. mpd_qmul(result, result, tbase, ctx, &workstatus);
  5442. *status |= workstatus;
  5443. if (mpd_isspecial(result) ||
  5444. (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) {
  5445. break;
  5446. }
  5447. }
  5448. mpd_qmul(tbase, tbase, tbase, ctx, &workstatus);
  5449. mpd_qdivint(texp, texp, &two, &maxctx, &workstatus);
  5450. if (mpd_isnan(tbase) || mpd_isnan(texp)) {
  5451. mpd_seterror(result, workstatus&MPD_Errors, status);
  5452. return;
  5453. }
  5454. }
  5455. mpd_set_sign(result, resultsign);
  5456. }
  5457. /*
  5458. * The power function for integer exponents. Relative error _before_ the
  5459. * final rounding to prec:
  5460. * abs(result - base**exp) < 0.1 * 10**-prec * abs(base**exp)
  5461. */
  5462. static void
  5463. _mpd_qpow_int(mpd_t *result, const mpd_t *base, const mpd_t *exp,
  5464. uint8_t resultsign,
  5465. const mpd_context_t *ctx, uint32_t *status)
  5466. {
  5467. mpd_context_t workctx;
  5468. MPD_NEW_STATIC(tbase,0,0,0,0);
  5469. MPD_NEW_STATIC(texp,0,0,0,0);
  5470. mpd_ssize_t n;
  5471. mpd_workcontext(&workctx, ctx);
  5472. workctx.prec += (exp->digits + exp->exp + 2);
  5473. workctx.round = MPD_ROUND_HALF_EVEN;
  5474. workctx.clamp = 0;
  5475. if (mpd_isnegative(exp)) {
  5476. workctx.prec += 1;
  5477. mpd_qdiv(&tbase, &one, base, &workctx, status);
  5478. if (*status&MPD_Errors) {
  5479. mpd_setspecial(result, MPD_POS, MPD_NAN);
  5480. goto finish;
  5481. }
  5482. }
  5483. else {
  5484. if (!mpd_qcopy(&tbase, base, status)) {
  5485. mpd_setspecial(result, MPD_POS, MPD_NAN);
  5486. goto finish;
  5487. }
  5488. }
  5489. n = mpd_qabs_uint(exp, &workctx.status);
  5490. if (workctx.status&MPD_Invalid_operation) {
  5491. if (!mpd_qcopy(&texp, exp, status)) {
  5492. mpd_setspecial(result, MPD_POS, MPD_NAN); /* GCOV_UNLIKELY */
  5493. goto finish; /* GCOV_UNLIKELY */
  5494. }
  5495. _mpd_qpow_mpd(result, &tbase, &texp, resultsign, &workctx, status);
  5496. }
  5497. else {
  5498. _mpd_qpow_uint(result, &tbase, n, resultsign, &workctx, status);
  5499. }
  5500. if (mpd_isinfinite(result)) {
  5501. /* for ROUND_DOWN, ROUND_FLOOR, etc. */
  5502. _settriple(result, resultsign, 1, MPD_EXP_INF);
  5503. }
  5504. finish:
  5505. mpd_del(&tbase);
  5506. mpd_del(&texp);
  5507. mpd_qfinalize(result, ctx, status);
  5508. }
  5509. /*
  5510. * If the exponent is infinite and base equals one, the result is one
  5511. * with a coefficient of length prec. Otherwise, result is undefined.
  5512. * Return the value of the comparison against one.
  5513. */
  5514. static int
  5515. _qcheck_pow_one_inf(mpd_t *result, const mpd_t *base, uint8_t resultsign,
  5516. const mpd_context_t *ctx, uint32_t *status)
  5517. {
  5518. mpd_ssize_t shift;
  5519. int cmp;
  5520. if ((cmp = _mpd_cmp(base, &one)) == 0) {
  5521. shift = ctx->prec-1;
  5522. mpd_qshiftl(result, &one, shift, status);
  5523. result->exp = -shift;
  5524. mpd_set_flags(result, resultsign);
  5525. *status |= (MPD_Inexact|MPD_Rounded);
  5526. }
  5527. return cmp;
  5528. }
  5529. /*
  5530. * If abs(base) equals one, calculate the correct power of one result.
  5531. * Otherwise, result is undefined. Return the value of the comparison
  5532. * against 1.
  5533. *
  5534. * This is an internal function that does not check for specials.
  5535. */
  5536. static int
  5537. _qcheck_pow_one(mpd_t *result, const mpd_t *base, const mpd_t *exp,
  5538. uint8_t resultsign,
  5539. const mpd_context_t *ctx, uint32_t *status)
  5540. {
  5541. uint32_t workstatus = 0;
  5542. mpd_ssize_t shift;
  5543. int cmp;
  5544. if ((cmp = _mpd_cmp_abs(base, &one)) == 0) {
  5545. if (_mpd_isint(exp)) {
  5546. if (mpd_isnegative(exp)) {
  5547. _settriple(result, resultsign, 1, 0);
  5548. return 0;
  5549. }
  5550. /* 1.000**3 = 1.000000000 */
  5551. mpd_qmul_ssize(result, exp, -base->exp, ctx, &workstatus);
  5552. if (workstatus&MPD_Errors) {
  5553. *status |= (workstatus&MPD_Errors);
  5554. return 0;
  5555. }
  5556. /* digits-1 after exponentiation */
  5557. shift = mpd_qget_ssize(result, &workstatus);
  5558. /* shift is MPD_SSIZE_MAX if result is too large */
  5559. if (shift > ctx->prec-1) {
  5560. shift = ctx->prec-1;
  5561. *status |= MPD_Rounded;
  5562. }
  5563. }
  5564. else if (mpd_ispositive(base)) {
  5565. shift = ctx->prec-1;
  5566. *status |= (MPD_Inexact|MPD_Rounded);
  5567. }
  5568. else {
  5569. return -2; /* GCOV_NOT_REACHED */
  5570. }
  5571. if (!mpd_qshiftl(result, &one, shift, status)) {
  5572. return 0;
  5573. }
  5574. result->exp = -shift;
  5575. mpd_set_flags(result, resultsign);
  5576. }
  5577. return cmp;
  5578. }
  5579. /*
  5580. * Detect certain over/underflow of x**y.
  5581. * ACL2 proof: pow-bounds.lisp.
  5582. *
  5583. * Symbols:
  5584. *
  5585. * e: EXP_INF or EXP_CLAMP
  5586. * x: base
  5587. * y: exponent
  5588. *
  5589. * omega(e) = log10(abs(e))
  5590. * zeta(x) = log10(abs(log10(x)))
  5591. * theta(y) = log10(abs(y))
  5592. *
  5593. * Upper and lower bounds:
  5594. *
  5595. * ub_omega(e) = ceil(log10(abs(e)))
  5596. * lb_theta(y) = floor(log10(abs(y)))
  5597. *
  5598. * | floor(log10(floor(abs(log10(x))))) if x < 1/10 or x >= 10
  5599. * lb_zeta(x) = | floor(log10(abs(x-1)/10)) if 1/10 <= x < 1
  5600. * | floor(log10(abs((x-1)/100))) if 1 < x < 10
  5601. *
  5602. * ub_omega(e) and lb_theta(y) are obviously upper and lower bounds
  5603. * for omega(e) and theta(y).
  5604. *
  5605. * lb_zeta is a lower bound for zeta(x):
  5606. *
  5607. * x < 1/10 or x >= 10:
  5608. *
  5609. * abs(log10(x)) >= 1, so the outer log10 is well defined. Since log10
  5610. * is strictly increasing, the end result is a lower bound.
  5611. *
  5612. * 1/10 <= x < 1:
  5613. *
  5614. * We use: log10(x) <= (x-1)/log(10)
  5615. * abs(log10(x)) >= abs(x-1)/log(10)
  5616. * abs(log10(x)) >= abs(x-1)/10
  5617. *
  5618. * 1 < x < 10:
  5619. *
  5620. * We use: (x-1)/(x*log(10)) < log10(x)
  5621. * abs((x-1)/100) < abs(log10(x))
  5622. *
  5623. * XXX: abs((x-1)/10) would work, need ACL2 proof.
  5624. *
  5625. *
  5626. * Let (0 < x < 1 and y < 0) or (x > 1 and y > 0). (H1)
  5627. * Let ub_omega(exp_inf) < lb_zeta(x) + lb_theta(y) (H2)
  5628. *
  5629. * Then:
  5630. * log10(abs(exp_inf)) < log10(abs(log10(x))) + log10(abs(y)). (1)
  5631. * exp_inf < log10(x) * y (2)
  5632. * 10**exp_inf < x**y (3)
  5633. *
  5634. * Let (0 < x < 1 and y > 0) or (x > 1 and y < 0). (H3)
  5635. * Let ub_omega(exp_clamp) < lb_zeta(x) + lb_theta(y) (H4)
  5636. *
  5637. * Then:
  5638. * log10(abs(exp_clamp)) < log10(abs(log10(x))) + log10(abs(y)). (4)
  5639. * log10(x) * y < exp_clamp (5)
  5640. * x**y < 10**exp_clamp (6)
  5641. *
  5642. */
  5643. static mpd_ssize_t
  5644. _lower_bound_zeta(const mpd_t *x, uint32_t *status)
  5645. {
  5646. mpd_context_t maxctx;
  5647. MPD_NEW_STATIC(scratch,0,0,0,0);
  5648. mpd_ssize_t t, u;
  5649. t = mpd_adjexp(x);
  5650. if (t > 0) {
  5651. /* x >= 10 -> floor(log10(floor(abs(log10(x))))) */
  5652. return mpd_exp_digits(t) - 1;
  5653. }
  5654. else if (t < -1) {
  5655. /* x < 1/10 -> floor(log10(floor(abs(log10(x))))) */
  5656. return mpd_exp_digits(t+1) - 1;
  5657. }
  5658. else {
  5659. mpd_maxcontext(&maxctx);
  5660. mpd_qsub(&scratch, x, &one, &maxctx, status);
  5661. if (mpd_isspecial(&scratch)) {
  5662. mpd_del(&scratch);
  5663. return MPD_SSIZE_MAX;
  5664. }
  5665. u = mpd_adjexp(&scratch);
  5666. mpd_del(&scratch);
  5667. /* t == -1, 1/10 <= x < 1 -> floor(log10(abs(x-1)/10))
  5668. * t == 0, 1 < x < 10 -> floor(log10(abs(x-1)/100)) */
  5669. return (t == 0) ? u-2 : u-1;
  5670. }
  5671. }
  5672. /*
  5673. * Detect cases of certain overflow/underflow in the power function.
  5674. * Assumptions: x != 1, y != 0. The proof above is for positive x.
  5675. * If x is negative and y is an odd integer, x**y == -(abs(x)**y),
  5676. * so the analysis does not change.
  5677. */
  5678. static int
  5679. _qcheck_pow_bounds(mpd_t *result, const mpd_t *x, const mpd_t *y,
  5680. uint8_t resultsign,
  5681. const mpd_context_t *ctx, uint32_t *status)
  5682. {
  5683. MPD_NEW_SHARED(abs_x, x);
  5684. mpd_ssize_t ub_omega, lb_zeta, lb_theta;
  5685. uint8_t sign;
  5686. mpd_set_positive(&abs_x);
  5687. lb_theta = mpd_adjexp(y);
  5688. lb_zeta = _lower_bound_zeta(&abs_x, status);
  5689. if (lb_zeta == MPD_SSIZE_MAX) {
  5690. mpd_seterror(result, MPD_Malloc_error, status);
  5691. return 1;
  5692. }
  5693. sign = (mpd_adjexp(&abs_x) < 0) ^ mpd_sign(y);
  5694. if (sign == 0) {
  5695. /* (0 < |x| < 1 and y < 0) or (|x| > 1 and y > 0) */
  5696. ub_omega = mpd_exp_digits(ctx->emax);
  5697. if (ub_omega < lb_zeta + lb_theta) {
  5698. _settriple(result, resultsign, 1, MPD_EXP_INF);
  5699. mpd_qfinalize(result, ctx, status);
  5700. return 1;
  5701. }
  5702. }
  5703. else {
  5704. /* (0 < |x| < 1 and y > 0) or (|x| > 1 and y < 0). */
  5705. ub_omega = mpd_exp_digits(mpd_etiny(ctx));
  5706. if (ub_omega < lb_zeta + lb_theta) {
  5707. _settriple(result, resultsign, 1, mpd_etiny(ctx)-1);
  5708. mpd_qfinalize(result, ctx, status);
  5709. return 1;
  5710. }
  5711. }
  5712. return 0;
  5713. }
  5714. /*
  5715. * TODO: Implement algorithm for computing exact powers from decimal.py.
  5716. * In order to prevent infinite loops, this has to be called before
  5717. * using Ziv's strategy for correct rounding.
  5718. */
  5719. /*
  5720. static int
  5721. _mpd_qpow_exact(mpd_t *result, const mpd_t *base, const mpd_t *exp,
  5722. const mpd_context_t *ctx, uint32_t *status)
  5723. {
  5724. return 0;
  5725. }
  5726. */
  5727. /*
  5728. * The power function for real exponents.
  5729. * Relative error: abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1)
  5730. */
  5731. static void
  5732. _mpd_qpow_real(mpd_t *result, const mpd_t *base, const mpd_t *exp,
  5733. const mpd_context_t *ctx, uint32_t *status)
  5734. {
  5735. mpd_context_t workctx;
  5736. MPD_NEW_STATIC(texp,0,0,0,0);
  5737. if (!mpd_qcopy(&texp, exp, status)) {
  5738. mpd_seterror(result, MPD_Malloc_error, status);
  5739. return;
  5740. }
  5741. mpd_maxcontext(&workctx);
  5742. workctx.prec = (base->digits > ctx->prec) ? base->digits : ctx->prec;
  5743. workctx.prec += (4 + MPD_EXPDIGITS);
  5744. workctx.round = MPD_ROUND_HALF_EVEN;
  5745. workctx.allcr = ctx->allcr;
  5746. /*
  5747. * extra := MPD_EXPDIGITS = MPD_EXP_MAX_T
  5748. * wp := prec + 4 + extra
  5749. * abs(err) < 5 * 10**-wp
  5750. * y := log(base) * exp
  5751. * Calculate:
  5752. * 1) e**(y * (1 + err)**2) * (1 + err)
  5753. * = e**y * e**(y * (2*err + err**2)) * (1 + err)
  5754. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5755. * Relative error of the underlined term:
  5756. * 2) abs(e**(y * (2*err + err**2)) - 1)
  5757. * Case abs(y) >= 10**extra:
  5758. * 3) adjexp(y)+1 > log10(abs(y)) >= extra
  5759. * This triggers the Overflow/Underflow shortcut in _mpd_qexp(),
  5760. * so no further analysis is necessary.
  5761. * Case abs(y) < 10**extra:
  5762. * 4) abs(y * (2*err + err**2)) < 1/5 * 10**(-prec - 2)
  5763. * Use (see _mpd_qexp):
  5764. * 5) abs(x) <= 9/10 * 10**-p ==> abs(e**x - 1) < 10**-p
  5765. * With 2), 4) and 5):
  5766. * 6) abs(e**(y * (2*err + err**2)) - 1) < 10**(-prec - 2)
  5767. * The complete relative error of 1) is:
  5768. * 7) abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1)
  5769. */
  5770. mpd_qln(result, base, &workctx, &workctx.status);
  5771. mpd_qmul(result, result, &texp, &workctx, &workctx.status);
  5772. mpd_qexp(result, result, &workctx, status);
  5773. mpd_del(&texp);
  5774. *status |= (workctx.status&MPD_Errors);
  5775. *status |= (MPD_Inexact|MPD_Rounded);
  5776. }
  5777. /* The power function: base**exp */
  5778. void
  5779. mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp,
  5780. const mpd_context_t *ctx, uint32_t *status)
  5781. {
  5782. uint8_t resultsign = 0;
  5783. int intexp = 0;
  5784. int cmp;
  5785. if (mpd_isspecial(base) || mpd_isspecial(exp)) {
  5786. if (mpd_qcheck_nans(result, base, exp, ctx, status)) {
  5787. return;
  5788. }
  5789. }
  5790. if (mpd_isinteger(exp)) {
  5791. intexp = 1;
  5792. resultsign = mpd_isnegative(base) && mpd_isodd(exp);
  5793. }
  5794. if (mpd_iszero(base)) {
  5795. if (mpd_iszero(exp)) {
  5796. mpd_seterror(result, MPD_Invalid_operation, status);
  5797. }
  5798. else if (mpd_isnegative(exp)) {
  5799. mpd_setspecial(result, resultsign, MPD_INF);
  5800. }
  5801. else {
  5802. _settriple(result, resultsign, 0, 0);
  5803. }
  5804. return;
  5805. }
  5806. if (mpd_isnegative(base)) {
  5807. if (!intexp || mpd_isinfinite(exp)) {
  5808. mpd_seterror(result, MPD_Invalid_operation, status);
  5809. return;
  5810. }
  5811. }
  5812. if (mpd_isinfinite(exp)) {
  5813. /* power of one */
  5814. cmp = _qcheck_pow_one_inf(result, base, resultsign, ctx, status);
  5815. if (cmp == 0) {
  5816. return;
  5817. }
  5818. else {
  5819. cmp *= mpd_arith_sign(exp);
  5820. if (cmp < 0) {
  5821. _settriple(result, resultsign, 0, 0);
  5822. }
  5823. else {
  5824. mpd_setspecial(result, resultsign, MPD_INF);
  5825. }
  5826. }
  5827. return;
  5828. }
  5829. if (mpd_isinfinite(base)) {
  5830. if (mpd_iszero(exp)) {
  5831. _settriple(result, resultsign, 1, 0);
  5832. }
  5833. else if (mpd_isnegative(exp)) {
  5834. _settriple(result, resultsign, 0, 0);
  5835. }
  5836. else {
  5837. mpd_setspecial(result, resultsign, MPD_INF);
  5838. }
  5839. return;
  5840. }
  5841. if (mpd_iszero(exp)) {
  5842. _settriple(result, resultsign, 1, 0);
  5843. return;
  5844. }
  5845. if (_qcheck_pow_one(result, base, exp, resultsign, ctx, status) == 0) {
  5846. return;
  5847. }
  5848. if (_qcheck_pow_bounds(result, base, exp, resultsign, ctx, status)) {
  5849. return;
  5850. }
  5851. if (intexp) {
  5852. _mpd_qpow_int(result, base, exp, resultsign, ctx, status);
  5853. }
  5854. else {
  5855. _mpd_qpow_real(result, base, exp, ctx, status);
  5856. if (!mpd_isspecial(result) && _mpd_cmp(result, &one) == 0) {
  5857. mpd_ssize_t shift = ctx->prec-1;
  5858. mpd_qshiftl(result, &one, shift, status);
  5859. result->exp = -shift;
  5860. }
  5861. if (mpd_isinfinite(result)) {
  5862. /* for ROUND_DOWN, ROUND_FLOOR, etc. */
  5863. _settriple(result, MPD_POS, 1, MPD_EXP_INF);
  5864. }
  5865. mpd_qfinalize(result, ctx, status);
  5866. }
  5867. }
  5868. /*
  5869. * Internal function: Integer powmod with mpd_uint_t exponent, base is modified!
  5870. * Function can fail with MPD_Malloc_error.
  5871. */
  5872. static inline void
  5873. _mpd_qpowmod_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp,
  5874. const mpd_t *mod, uint32_t *status)
  5875. {
  5876. mpd_context_t maxcontext;
  5877. mpd_maxcontext(&maxcontext);
  5878. /* resize to smaller cannot fail */
  5879. mpd_qcopy(result, &one, status);
  5880. while (exp > 0) {
  5881. if (exp & 1) {
  5882. _mpd_qmul_exact(result, result, base, &maxcontext, status);
  5883. mpd_qrem(result, result, mod, &maxcontext, status);
  5884. }
  5885. _mpd_qmul_exact(base, base, base, &maxcontext, status);
  5886. mpd_qrem(base, base, mod, &maxcontext, status);
  5887. exp >>= 1;
  5888. }
  5889. }
  5890. /* The powmod function: (base**exp) % mod */
  5891. void
  5892. mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp,
  5893. const mpd_t *mod,
  5894. const mpd_context_t *ctx, uint32_t *status)
  5895. {
  5896. mpd_context_t maxcontext;
  5897. MPD_NEW_STATIC(tbase,0,0,0,0);
  5898. MPD_NEW_STATIC(texp,0,0,0,0);
  5899. MPD_NEW_STATIC(tmod,0,0,0,0);
  5900. MPD_NEW_STATIC(tmp,0,0,0,0);
  5901. MPD_NEW_CONST(two,0,0,1,1,1,2);
  5902. mpd_ssize_t tbase_exp, texp_exp;
  5903. mpd_ssize_t i;
  5904. mpd_t t;
  5905. mpd_uint_t r;
  5906. uint8_t sign;
  5907. if (mpd_isspecial(base) || mpd_isspecial(exp) || mpd_isspecial(mod)) {
  5908. if (mpd_qcheck_3nans(result, base, exp, mod, ctx, status)) {
  5909. return;
  5910. }
  5911. mpd_seterror(result, MPD_Invalid_operation, status);
  5912. return;
  5913. }
  5914. if (!_mpd_isint(base) || !_mpd_isint(exp) || !_mpd_isint(mod)) {
  5915. mpd_seterror(result, MPD_Invalid_operation, status);
  5916. return;
  5917. }
  5918. if (mpd_iszerocoeff(mod)) {
  5919. mpd_seterror(result, MPD_Invalid_operation, status);
  5920. return;
  5921. }
  5922. if (mod->digits+mod->exp > ctx->prec) {
  5923. mpd_seterror(result, MPD_Invalid_operation, status);
  5924. return;
  5925. }
  5926. sign = (mpd_isnegative(base)) && (mpd_isodd(exp));
  5927. if (mpd_iszerocoeff(exp)) {
  5928. if (mpd_iszerocoeff(base)) {
  5929. mpd_seterror(result, MPD_Invalid_operation, status);
  5930. return;
  5931. }
  5932. r = (_mpd_cmp_abs(mod, &one)==0) ? 0 : 1;
  5933. _settriple(result, sign, r, 0);
  5934. return;
  5935. }
  5936. if (mpd_isnegative(exp)) {
  5937. mpd_seterror(result, MPD_Invalid_operation, status);
  5938. return;
  5939. }
  5940. if (mpd_iszerocoeff(base)) {
  5941. _settriple(result, sign, 0, 0);
  5942. return;
  5943. }
  5944. mpd_maxcontext(&maxcontext);
  5945. mpd_qrescale(&tmod, mod, 0, &maxcontext, &maxcontext.status);
  5946. if (maxcontext.status&MPD_Errors) {
  5947. mpd_seterror(result, maxcontext.status&MPD_Errors, status);
  5948. goto out;
  5949. }
  5950. maxcontext.status = 0;
  5951. mpd_set_positive(&tmod);
  5952. mpd_qround_to_int(&tbase, base, &maxcontext, status);
  5953. mpd_set_positive(&tbase);
  5954. tbase_exp = tbase.exp;
  5955. tbase.exp = 0;
  5956. mpd_qround_to_int(&texp, exp, &maxcontext, status);
  5957. texp_exp = texp.exp;
  5958. texp.exp = 0;
  5959. /* base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo */
  5960. mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status);
  5961. mpd_qshiftl(result, &one, tbase_exp, status);
  5962. mpd_qrem(result, result, &tmod, &maxcontext, status);
  5963. _mpd_qmul_exact(&tbase, &tbase, result, &maxcontext, status);
  5964. mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status);
  5965. if (mpd_isspecial(&tbase) ||
  5966. mpd_isspecial(&texp) ||
  5967. mpd_isspecial(&tmod)) {
  5968. goto mpd_errors;
  5969. }
  5970. for (i = 0; i < texp_exp; i++) {
  5971. _mpd_qpowmod_uint(&tmp, &tbase, 10, &tmod, status);
  5972. t = tmp;
  5973. tmp = tbase;
  5974. tbase = t;
  5975. }
  5976. if (mpd_isspecial(&tbase)) {
  5977. goto mpd_errors; /* GCOV_UNLIKELY */
  5978. }
  5979. /* resize to smaller cannot fail */
  5980. mpd_qcopy(result, &one, status);
  5981. while (mpd_isfinite(&texp) && !mpd_iszero(&texp)) {
  5982. if (mpd_isodd(&texp)) {
  5983. _mpd_qmul_exact(result, result, &tbase, &maxcontext, status);
  5984. mpd_qrem(result, result, &tmod, &maxcontext, status);
  5985. }
  5986. _mpd_qmul_exact(&tbase, &tbase, &tbase, &maxcontext, status);
  5987. mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status);
  5988. mpd_qdivint(&texp, &texp, &two, &maxcontext, status);
  5989. }
  5990. if (mpd_isspecial(&texp) || mpd_isspecial(&tbase) ||
  5991. mpd_isspecial(&tmod) || mpd_isspecial(result)) {
  5992. /* MPD_Malloc_error */
  5993. goto mpd_errors;
  5994. }
  5995. else {
  5996. mpd_set_sign(result, sign);
  5997. }
  5998. out:
  5999. mpd_del(&tbase);
  6000. mpd_del(&texp);
  6001. mpd_del(&tmod);
  6002. mpd_del(&tmp);
  6003. return;
  6004. mpd_errors:
  6005. mpd_setspecial(result, MPD_POS, MPD_NAN);
  6006. goto out;
  6007. }
  6008. void
  6009. mpd_qquantize(mpd_t *result, const mpd_t *a, const mpd_t *b,
  6010. const mpd_context_t *ctx, uint32_t *status)
  6011. {
  6012. uint32_t workstatus = 0;
  6013. mpd_ssize_t b_exp = b->exp;
  6014. mpd_ssize_t expdiff, shift;
  6015. mpd_uint_t rnd;
  6016. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  6017. if (mpd_qcheck_nans(result, a, b, ctx, status)) {
  6018. return;
  6019. }
  6020. if (mpd_isinfinite(a) && mpd_isinfinite(b)) {
  6021. mpd_qcopy(result, a, status);
  6022. return;
  6023. }
  6024. mpd_seterror(result, MPD_Invalid_operation, status);
  6025. return;
  6026. }
  6027. if (b->exp > ctx->emax || b->exp < mpd_etiny(ctx)) {
  6028. mpd_seterror(result, MPD_Invalid_operation, status);
  6029. return;
  6030. }
  6031. if (mpd_iszero(a)) {
  6032. _settriple(result, mpd_sign(a), 0, b->exp);
  6033. mpd_qfinalize(result, ctx, status);
  6034. return;
  6035. }
  6036. expdiff = a->exp - b->exp;
  6037. if (a->digits + expdiff > ctx->prec) {
  6038. mpd_seterror(result, MPD_Invalid_operation, status);
  6039. return;
  6040. }
  6041. if (expdiff >= 0) {
  6042. shift = expdiff;
  6043. if (!mpd_qshiftl(result, a, shift, status)) {
  6044. return;
  6045. }
  6046. result->exp = b_exp;
  6047. }
  6048. else {
  6049. /* At this point expdiff < 0 and a->digits+expdiff <= prec,
  6050. * so the shift before an increment will fit in prec. */
  6051. shift = -expdiff;
  6052. rnd = mpd_qshiftr(result, a, shift, status);
  6053. if (rnd == MPD_UINT_MAX) {
  6054. return;
  6055. }
  6056. result->exp = b_exp;
  6057. if (!_mpd_apply_round_fit(result, rnd, ctx, status)) {
  6058. return;
  6059. }
  6060. workstatus |= MPD_Rounded;
  6061. if (rnd) {
  6062. workstatus |= MPD_Inexact;
  6063. }
  6064. }
  6065. if (mpd_adjexp(result) > ctx->emax ||
  6066. mpd_adjexp(result) < mpd_etiny(ctx)) {
  6067. mpd_seterror(result, MPD_Invalid_operation, status);
  6068. return;
  6069. }
  6070. *status |= workstatus;
  6071. mpd_qfinalize(result, ctx, status);
  6072. }
  6073. void
  6074. mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6075. uint32_t *status)
  6076. {
  6077. mpd_ssize_t shift, maxexp, maxshift;
  6078. uint8_t sign_a = mpd_sign(a);
  6079. if (mpd_isspecial(a)) {
  6080. if (mpd_qcheck_nan(result, a, ctx, status)) {
  6081. return;
  6082. }
  6083. mpd_qcopy(result, a, status);
  6084. return;
  6085. }
  6086. if (!mpd_qcopy(result, a, status)) {
  6087. return;
  6088. }
  6089. mpd_qfinalize(result, ctx, status);
  6090. if (mpd_isspecial(result)) {
  6091. return;
  6092. }
  6093. if (mpd_iszero(result)) {
  6094. _settriple(result, sign_a, 0, 0);
  6095. return;
  6096. }
  6097. shift = mpd_trail_zeros(result);
  6098. maxexp = (ctx->clamp) ? mpd_etop(ctx) : ctx->emax;
  6099. /* After the finalizing above result->exp <= maxexp. */
  6100. maxshift = maxexp - result->exp;
  6101. shift = (shift > maxshift) ? maxshift : shift;
  6102. mpd_qshiftr_inplace(result, shift);
  6103. result->exp += shift;
  6104. }
  6105. void
  6106. mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx,
  6107. uint32_t *status)
  6108. {
  6109. MPD_NEW_STATIC(q,0,0,0,0);
  6110. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  6111. if (mpd_qcheck_nans(r, a, b, ctx, status)) {
  6112. return;
  6113. }
  6114. if (mpd_isinfinite(a)) {
  6115. mpd_seterror(r, MPD_Invalid_operation, status);
  6116. return;
  6117. }
  6118. if (mpd_isinfinite(b)) {
  6119. mpd_qcopy(r, a, status);
  6120. mpd_qfinalize(r, ctx, status);
  6121. return;
  6122. }
  6123. /* debug */
  6124. abort(); /* GCOV_NOT_REACHED */
  6125. }
  6126. if (mpd_iszerocoeff(b)) {
  6127. if (mpd_iszerocoeff(a)) {
  6128. mpd_seterror(r, MPD_Division_undefined, status);
  6129. }
  6130. else {
  6131. mpd_seterror(r, MPD_Invalid_operation, status);
  6132. }
  6133. return;
  6134. }
  6135. _mpd_qdivmod(&q, r, a, b, ctx, status);
  6136. mpd_del(&q);
  6137. mpd_qfinalize(r, ctx, status);
  6138. }
  6139. void
  6140. mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b,
  6141. const mpd_context_t *ctx, uint32_t *status)
  6142. {
  6143. mpd_context_t workctx;
  6144. MPD_NEW_STATIC(btmp,0,0,0,0);
  6145. MPD_NEW_STATIC(q,0,0,0,0);
  6146. mpd_ssize_t expdiff, qdigits;
  6147. int cmp, isodd, allnine;
  6148. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  6149. if (mpd_qcheck_nans(r, a, b, ctx, status)) {
  6150. return;
  6151. }
  6152. if (mpd_isinfinite(a)) {
  6153. mpd_seterror(r, MPD_Invalid_operation, status);
  6154. return;
  6155. }
  6156. if (mpd_isinfinite(b)) {
  6157. mpd_qcopy(r, a, status);
  6158. mpd_qfinalize(r, ctx, status);
  6159. return;
  6160. }
  6161. /* debug */
  6162. abort(); /* GCOV_NOT_REACHED */
  6163. }
  6164. if (mpd_iszerocoeff(b)) {
  6165. if (mpd_iszerocoeff(a)) {
  6166. mpd_seterror(r, MPD_Division_undefined, status);
  6167. }
  6168. else {
  6169. mpd_seterror(r, MPD_Invalid_operation, status);
  6170. }
  6171. return;
  6172. }
  6173. if (r == b) {
  6174. if (!mpd_qcopy(&btmp, b, status)) {
  6175. mpd_seterror(r, MPD_Malloc_error, status);
  6176. return;
  6177. }
  6178. b = &btmp;
  6179. }
  6180. _mpd_qdivmod(&q, r, a, b, ctx, status);
  6181. if (mpd_isnan(&q) || mpd_isnan(r)) {
  6182. goto finish;
  6183. }
  6184. if (mpd_iszerocoeff(r)) {
  6185. goto finish;
  6186. }
  6187. expdiff = mpd_adjexp(b) - mpd_adjexp(r);
  6188. if (-1 <= expdiff && expdiff <= 1) {
  6189. allnine = mpd_coeff_isallnine(&q);
  6190. qdigits = q.digits;
  6191. isodd = mpd_isodd(&q);
  6192. mpd_maxcontext(&workctx);
  6193. if (mpd_sign(a) == mpd_sign(b)) {
  6194. /* sign(r) == sign(b) */
  6195. _mpd_qsub(&q, r, b, &workctx, &workctx.status);
  6196. }
  6197. else {
  6198. /* sign(r) != sign(b) */
  6199. _mpd_qadd(&q, r, b, &workctx, &workctx.status);
  6200. }
  6201. if (workctx.status&MPD_Errors) {
  6202. mpd_seterror(r, workctx.status&MPD_Errors, status);
  6203. goto finish;
  6204. }
  6205. cmp = _mpd_cmp_abs(&q, r);
  6206. if (cmp < 0 || (cmp == 0 && isodd)) {
  6207. /* abs(r) > abs(b)/2 or abs(r) == abs(b)/2 and isodd(quotient) */
  6208. if (allnine && qdigits == ctx->prec) {
  6209. /* abs(quotient) + 1 == 10**prec */
  6210. mpd_seterror(r, MPD_Division_impossible, status);
  6211. goto finish;
  6212. }
  6213. mpd_qcopy(r, &q, status);
  6214. }
  6215. }
  6216. finish:
  6217. mpd_del(&btmp);
  6218. mpd_del(&q);
  6219. mpd_qfinalize(r, ctx, status);
  6220. }
  6221. static void
  6222. _mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp,
  6223. const mpd_context_t *ctx, uint32_t *status)
  6224. {
  6225. mpd_ssize_t expdiff, shift;
  6226. mpd_uint_t rnd;
  6227. if (mpd_isspecial(a)) {
  6228. mpd_qcopy(result, a, status);
  6229. return;
  6230. }
  6231. if (mpd_iszero(a)) {
  6232. _settriple(result, mpd_sign(a), 0, exp);
  6233. return;
  6234. }
  6235. expdiff = a->exp - exp;
  6236. if (expdiff >= 0) {
  6237. shift = expdiff;
  6238. if (a->digits + shift > MPD_MAX_PREC+1) {
  6239. mpd_seterror(result, MPD_Invalid_operation, status);
  6240. return;
  6241. }
  6242. if (!mpd_qshiftl(result, a, shift, status)) {
  6243. return;
  6244. }
  6245. result->exp = exp;
  6246. }
  6247. else {
  6248. shift = -expdiff;
  6249. rnd = mpd_qshiftr(result, a, shift, status);
  6250. if (rnd == MPD_UINT_MAX) {
  6251. return;
  6252. }
  6253. result->exp = exp;
  6254. _mpd_apply_round_excess(result, rnd, ctx, status);
  6255. *status |= MPD_Rounded;
  6256. if (rnd) {
  6257. *status |= MPD_Inexact;
  6258. }
  6259. }
  6260. if (mpd_issubnormal(result, ctx)) {
  6261. *status |= MPD_Subnormal;
  6262. }
  6263. }
  6264. /*
  6265. * Rescale a number so that it has exponent 'exp'. Does not regard context
  6266. * precision, emax, emin, but uses the rounding mode. Special numbers are
  6267. * quietly copied. Restrictions:
  6268. *
  6269. * MPD_MIN_ETINY <= exp <= MPD_MAX_EMAX+1
  6270. * result->digits <= MPD_MAX_PREC+1
  6271. */
  6272. void
  6273. mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp,
  6274. const mpd_context_t *ctx, uint32_t *status)
  6275. {
  6276. if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY) {
  6277. mpd_seterror(result, MPD_Invalid_operation, status);
  6278. return;
  6279. }
  6280. _mpd_qrescale(result, a, exp, ctx, status);
  6281. }
  6282. /*
  6283. * Same as mpd_qrescale, but with relaxed restrictions. The result of this
  6284. * function should only be used for formatting a number and never as input
  6285. * for other operations.
  6286. *
  6287. * MPD_MIN_ETINY-MPD_MAX_PREC <= exp <= MPD_MAX_EMAX+1
  6288. * result->digits <= MPD_MAX_PREC+1
  6289. */
  6290. void
  6291. mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp,
  6292. const mpd_context_t *ctx, uint32_t *status)
  6293. {
  6294. if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY-MPD_MAX_PREC) {
  6295. mpd_seterror(result, MPD_Invalid_operation, status);
  6296. return;
  6297. }
  6298. _mpd_qrescale(result, a, exp, ctx, status);
  6299. }
  6300. /* Round to an integer according to 'action' and ctx->round. */
  6301. enum {TO_INT_EXACT, TO_INT_SILENT, TO_INT_TRUNC};
  6302. static void
  6303. _mpd_qround_to_integral(int action, mpd_t *result, const mpd_t *a,
  6304. const mpd_context_t *ctx, uint32_t *status)
  6305. {
  6306. mpd_uint_t rnd;
  6307. if (mpd_isspecial(a)) {
  6308. if (mpd_qcheck_nan(result, a, ctx, status)) {
  6309. return;
  6310. }
  6311. mpd_qcopy(result, a, status);
  6312. return;
  6313. }
  6314. if (a->exp >= 0) {
  6315. mpd_qcopy(result, a, status);
  6316. return;
  6317. }
  6318. if (mpd_iszerocoeff(a)) {
  6319. _settriple(result, mpd_sign(a), 0, 0);
  6320. return;
  6321. }
  6322. rnd = mpd_qshiftr(result, a, -a->exp, status);
  6323. if (rnd == MPD_UINT_MAX) {
  6324. return;
  6325. }
  6326. result->exp = 0;
  6327. if (action == TO_INT_EXACT || action == TO_INT_SILENT) {
  6328. _mpd_apply_round_excess(result, rnd, ctx, status);
  6329. if (action == TO_INT_EXACT) {
  6330. *status |= MPD_Rounded;
  6331. if (rnd) {
  6332. *status |= MPD_Inexact;
  6333. }
  6334. }
  6335. }
  6336. }
  6337. void
  6338. mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6339. uint32_t *status)
  6340. {
  6341. (void)_mpd_qround_to_integral(TO_INT_EXACT, result, a, ctx, status);
  6342. }
  6343. void
  6344. mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6345. uint32_t *status)
  6346. {
  6347. (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, ctx, status);
  6348. }
  6349. void
  6350. mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6351. uint32_t *status)
  6352. {
  6353. (void)_mpd_qround_to_integral(TO_INT_TRUNC, result, a, ctx, status);
  6354. }
  6355. void
  6356. mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6357. uint32_t *status)
  6358. {
  6359. mpd_context_t workctx = *ctx;
  6360. workctx.round = MPD_ROUND_FLOOR;
  6361. (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a,
  6362. &workctx, status);
  6363. }
  6364. void
  6365. mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6366. uint32_t *status)
  6367. {
  6368. mpd_context_t workctx = *ctx;
  6369. workctx.round = MPD_ROUND_CEILING;
  6370. (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a,
  6371. &workctx, status);
  6372. }
  6373. int
  6374. mpd_same_quantum(const mpd_t *a, const mpd_t *b)
  6375. {
  6376. if (mpd_isspecial(a) || mpd_isspecial(b)) {
  6377. return ((mpd_isnan(a) && mpd_isnan(b)) ||
  6378. (mpd_isinfinite(a) && mpd_isinfinite(b)));
  6379. }
  6380. return a->exp == b->exp;
  6381. }
  6382. /* Schedule the increase in precision for the Newton iteration. */
  6383. static inline int
  6384. recpr_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2],
  6385. mpd_ssize_t maxprec, mpd_ssize_t initprec)
  6386. {
  6387. mpd_ssize_t k;
  6388. int i;
  6389. assert(maxprec > 0 && initprec > 0);
  6390. if (maxprec <= initprec) return -1;
  6391. i = 0; k = maxprec;
  6392. do {
  6393. k = (k+1) / 2;
  6394. klist[i++] = k;
  6395. } while (k > initprec);
  6396. return i-1;
  6397. }
  6398. /*
  6399. * Initial approximation for the reciprocal:
  6400. * k_0 := MPD_RDIGITS-2
  6401. * z_0 := 10**(-k_0) * floor(10**(2*k_0 + 2) / floor(v * 10**(k_0 + 2)))
  6402. * Absolute error:
  6403. * |1/v - z_0| < 10**(-k_0)
  6404. * ACL2 proof: maxerror-inverse-approx
  6405. */
  6406. static void
  6407. _mpd_qreciprocal_approx(mpd_t *z, const mpd_t *v, uint32_t *status)
  6408. {
  6409. mpd_uint_t p10data[2] = {0, mpd_pow10[MPD_RDIGITS-2]};
  6410. mpd_uint_t dummy, word;
  6411. int n;
  6412. assert(v->exp == -v->digits);
  6413. _mpd_get_msdigits(&dummy, &word, v, MPD_RDIGITS);
  6414. n = mpd_word_digits(word);
  6415. word *= mpd_pow10[MPD_RDIGITS-n];
  6416. mpd_qresize(z, 2, status);
  6417. (void)_mpd_shortdiv(z->data, p10data, 2, word);
  6418. mpd_clear_flags(z);
  6419. z->exp = -(MPD_RDIGITS-2);
  6420. z->len = (z->data[1] == 0) ? 1 : 2;
  6421. mpd_setdigits(z);
  6422. }
  6423. /*
  6424. * Reciprocal, calculated with Newton's Method. Assumption: result != a.
  6425. * NOTE: The comments in the function show that certain operations are
  6426. * exact. The proof for the maximum error is too long to fit in here.
  6427. * ACL2 proof: maxerror-inverse-complete
  6428. */
  6429. static void
  6430. _mpd_qreciprocal(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6431. uint32_t *status)
  6432. {
  6433. mpd_context_t varcontext, maxcontext;
  6434. mpd_t *z = result; /* current approximation */
  6435. mpd_t *v; /* a, normalized to a number between 0.1 and 1 */
  6436. MPD_NEW_SHARED(vtmp, a); /* v shares data with a */
  6437. MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */
  6438. MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */
  6439. MPD_NEW_CONST(two,0,0,1,1,1,2); /* const 2 */
  6440. mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
  6441. mpd_ssize_t adj, maxprec, initprec;
  6442. uint8_t sign = mpd_sign(a);
  6443. int i;
  6444. assert(result != a);
  6445. v = &vtmp;
  6446. mpd_clear_flags(v);
  6447. adj = v->digits + v->exp;
  6448. v->exp = -v->digits;
  6449. /* Initial approximation */
  6450. _mpd_qreciprocal_approx(z, v, status);
  6451. mpd_maxcontext(&varcontext);
  6452. mpd_maxcontext(&maxcontext);
  6453. varcontext.round = maxcontext.round = MPD_ROUND_TRUNC;
  6454. varcontext.emax = maxcontext.emax = MPD_MAX_EMAX + 100;
  6455. varcontext.emin = maxcontext.emin = MPD_MIN_EMIN - 100;
  6456. maxcontext.prec = MPD_MAX_PREC + 100;
  6457. maxprec = ctx->prec;
  6458. maxprec += 2;
  6459. initprec = MPD_RDIGITS-3;
  6460. i = recpr_schedule_prec(klist, maxprec, initprec);
  6461. for (; i >= 0; i--) {
  6462. /* Loop invariant: z->digits <= klist[i]+7 */
  6463. /* Let s := z**2, exact result */
  6464. _mpd_qmul_exact(&s, z, z, &maxcontext, status);
  6465. varcontext.prec = 2*klist[i] + 5;
  6466. if (v->digits > varcontext.prec) {
  6467. /* Let t := v, truncated to n >= 2*k+5 fraction digits */
  6468. mpd_qshiftr(&t, v, v->digits-varcontext.prec, status);
  6469. t.exp = -varcontext.prec;
  6470. /* Let t := trunc(v)*s, truncated to n >= 2*k+1 fraction digits */
  6471. mpd_qmul(&t, &t, &s, &varcontext, status);
  6472. }
  6473. else { /* v->digits <= 2*k+5 */
  6474. /* Let t := v*s, truncated to n >= 2*k+1 fraction digits */
  6475. mpd_qmul(&t, v, &s, &varcontext, status);
  6476. }
  6477. /* Let s := 2*z, exact result */
  6478. _mpd_qmul_exact(&s, z, &two, &maxcontext, status);
  6479. /* s.digits < t.digits <= 2*k+5, |adjexp(s)-adjexp(t)| <= 1,
  6480. * so the subtraction generates at most 2*k+6 <= klist[i+1]+7
  6481. * digits. The loop invariant is preserved. */
  6482. _mpd_qsub_exact(z, &s, &t, &maxcontext, status);
  6483. }
  6484. if (!mpd_isspecial(z)) {
  6485. z->exp -= adj;
  6486. mpd_set_flags(z, sign);
  6487. }
  6488. mpd_del(&s);
  6489. mpd_del(&t);
  6490. mpd_qfinalize(z, ctx, status);
  6491. }
  6492. /*
  6493. * Internal function for large numbers:
  6494. *
  6495. * q, r = divmod(coeff(a), coeff(b))
  6496. *
  6497. * Strategy: Multiply the dividend by the reciprocal of the divisor. The
  6498. * inexact result is fixed by a small loop, using at most one iteration.
  6499. *
  6500. * ACL2 proofs:
  6501. * ------------
  6502. * 1) q is a natural number. (ndivmod-quotient-natp)
  6503. * 2) r is a natural number. (ndivmod-remainder-natp)
  6504. * 3) a = q * b + r (ndivmod-q*b+r==a)
  6505. * 4) r < b (ndivmod-remainder-<-b)
  6506. */
  6507. static void
  6508. _mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
  6509. uint32_t *status)
  6510. {
  6511. mpd_context_t workctx;
  6512. mpd_t *qq = q, *rr = r;
  6513. mpd_t aa, bb;
  6514. int k;
  6515. _mpd_copy_shared(&aa, a);
  6516. _mpd_copy_shared(&bb, b);
  6517. mpd_set_positive(&aa);
  6518. mpd_set_positive(&bb);
  6519. aa.exp = 0;
  6520. bb.exp = 0;
  6521. if (q == a || q == b) {
  6522. if ((qq = mpd_qnew()) == NULL) {
  6523. *status |= MPD_Malloc_error;
  6524. goto nanresult;
  6525. }
  6526. }
  6527. if (r == a || r == b) {
  6528. if ((rr = mpd_qnew()) == NULL) {
  6529. *status |= MPD_Malloc_error;
  6530. goto nanresult;
  6531. }
  6532. }
  6533. mpd_maxcontext(&workctx);
  6534. /* Let prec := adigits - bdigits + 4 */
  6535. workctx.prec = a->digits - b->digits + 1 + 3;
  6536. if (a->digits > MPD_MAX_PREC || workctx.prec > MPD_MAX_PREC) {
  6537. *status |= MPD_Division_impossible;
  6538. goto nanresult;
  6539. }
  6540. /* Let x := _mpd_qreciprocal(b, prec)
  6541. * Then x is bounded by:
  6542. * 1) 1/b - 10**(-prec - bdigits) < x < 1/b + 10**(-prec - bdigits)
  6543. * 2) 1/b - 10**(-adigits - 4) < x < 1/b + 10**(-adigits - 4)
  6544. */
  6545. _mpd_qreciprocal(rr, &bb, &workctx, &workctx.status);
  6546. /* Get an estimate for the quotient. Let q := a * x
  6547. * Then q is bounded by:
  6548. * 3) a/b - 10**-4 < q < a/b + 10**-4
  6549. */
  6550. _mpd_qmul(qq, &aa, rr, &workctx, &workctx.status);
  6551. /* Truncate q to an integer:
  6552. * 4) a/b - 2 < trunc(q) < a/b + 1
  6553. */
  6554. mpd_qtrunc(qq, qq, &workctx, &workctx.status);
  6555. workctx.prec = aa.digits + 3;
  6556. workctx.emax = MPD_MAX_EMAX + 3;
  6557. workctx.emin = MPD_MIN_EMIN - 3;
  6558. /* Multiply the estimate for q by b:
  6559. * 5) a - 2 * b < trunc(q) * b < a + b
  6560. */
  6561. _mpd_qmul(rr, &bb, qq, &workctx, &workctx.status);
  6562. /* Get the estimate for r such that a = q * b + r. */
  6563. _mpd_qsub_exact(rr, &aa, rr, &workctx, &workctx.status);
  6564. /* Fix the result. At this point -b < r < 2*b, so the correction loop
  6565. takes at most one iteration. */
  6566. for (k = 0;; k++) {
  6567. if (mpd_isspecial(qq) || mpd_isspecial(rr)) {
  6568. *status |= (workctx.status&MPD_Errors);
  6569. goto nanresult;
  6570. }
  6571. if (k > 2) { /* Allow two iterations despite the proof. */
  6572. mpd_err_warn("libmpdec: internal error in " /* GCOV_NOT_REACHED */
  6573. "_mpd_base_ndivmod: please report"); /* GCOV_NOT_REACHED */
  6574. *status |= MPD_Invalid_operation; /* GCOV_NOT_REACHED */
  6575. goto nanresult; /* GCOV_NOT_REACHED */
  6576. }
  6577. /* r < 0 */
  6578. else if (_mpd_cmp(&zero, rr) == 1) {
  6579. _mpd_qadd_exact(rr, rr, &bb, &workctx, &workctx.status);
  6580. _mpd_qadd_exact(qq, qq, &minus_one, &workctx, &workctx.status);
  6581. }
  6582. /* 0 <= r < b */
  6583. else if (_mpd_cmp(rr, &bb) == -1) {
  6584. break;
  6585. }
  6586. /* r >= b */
  6587. else {
  6588. _mpd_qsub_exact(rr, rr, &bb, &workctx, &workctx.status);
  6589. _mpd_qadd_exact(qq, qq, &one, &workctx, &workctx.status);
  6590. }
  6591. }
  6592. if (qq != q) {
  6593. if (!mpd_qcopy(q, qq, status)) {
  6594. goto nanresult; /* GCOV_UNLIKELY */
  6595. }
  6596. mpd_del(qq);
  6597. }
  6598. if (rr != r) {
  6599. if (!mpd_qcopy(r, rr, status)) {
  6600. goto nanresult; /* GCOV_UNLIKELY */
  6601. }
  6602. mpd_del(rr);
  6603. }
  6604. *status |= (workctx.status&MPD_Errors);
  6605. return;
  6606. nanresult:
  6607. if (qq && qq != q) mpd_del(qq);
  6608. if (rr && rr != r) mpd_del(rr);
  6609. mpd_setspecial(q, MPD_POS, MPD_NAN);
  6610. mpd_setspecial(r, MPD_POS, MPD_NAN);
  6611. }
  6612. /* LIBMPDEC_ONLY */
  6613. /*
  6614. * Schedule the optimal precision increase for the Newton iteration.
  6615. * v := input operand
  6616. * z_0 := initial approximation
  6617. * initprec := natural number such that abs(sqrt(v) - z_0) < 10**-initprec
  6618. * maxprec := target precision
  6619. *
  6620. * For convenience the output klist contains the elements in reverse order:
  6621. * klist := [k_n-1, ..., k_0], where
  6622. * 1) k_0 <= initprec and
  6623. * 2) abs(sqrt(v) - result) < 10**(-2*k_n-1 + 2) <= 10**-maxprec.
  6624. */
  6625. static inline int
  6626. invroot_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2],
  6627. mpd_ssize_t maxprec, mpd_ssize_t initprec)
  6628. {
  6629. mpd_ssize_t k;
  6630. int i;
  6631. assert(maxprec >= 3 && initprec >= 3);
  6632. if (maxprec <= initprec) return -1;
  6633. i = 0; k = maxprec;
  6634. do {
  6635. k = (k+3) / 2;
  6636. klist[i++] = k;
  6637. } while (k > initprec);
  6638. return i-1;
  6639. }
  6640. /*
  6641. * Initial approximation for the inverse square root function.
  6642. * Input:
  6643. * v := rational number, with 1 <= v < 100
  6644. * vhat := floor(v * 10**6)
  6645. * Output:
  6646. * z := approximation to 1/sqrt(v), such that abs(z - 1/sqrt(v)) < 10**-3.
  6647. */
  6648. static inline void
  6649. _invroot_init_approx(mpd_t *z, mpd_uint_t vhat)
  6650. {
  6651. mpd_uint_t lo = 1000;
  6652. mpd_uint_t hi = 10000;
  6653. mpd_uint_t a, sq;
  6654. assert(lo*lo <= vhat && vhat < (hi+1)*(hi+1));
  6655. for(;;) {
  6656. a = (lo + hi) / 2;
  6657. sq = a * a;
  6658. if (vhat >= sq) {
  6659. if (vhat < sq + 2*a + 1) {
  6660. break;
  6661. }
  6662. lo = a + 1;
  6663. }
  6664. else {
  6665. hi = a - 1;
  6666. }
  6667. }
  6668. /*
  6669. * After the binary search we have:
  6670. * 1) a**2 <= floor(v * 10**6) < (a + 1)**2
  6671. * This implies:
  6672. * 2) a**2 <= v * 10**6 < (a + 1)**2
  6673. * 3) a <= sqrt(v) * 10**3 < a + 1
  6674. * Since 10**3 <= a:
  6675. * 4) 0 <= 10**prec/a - 1/sqrt(v) < 10**-prec
  6676. * We have:
  6677. * 5) 10**3/a - 10**-3 < floor(10**9/a) * 10**-6 <= 10**3/a
  6678. * Merging 4) and 5):
  6679. * 6) abs(floor(10**9/a) * 10**-6 - 1/sqrt(v)) < 10**-3
  6680. */
  6681. mpd_minalloc(z);
  6682. mpd_clear_flags(z);
  6683. z->data[0] = 1000000000UL / a;
  6684. z->len = 1;
  6685. z->exp = -6;
  6686. mpd_setdigits(z);
  6687. }
  6688. /*
  6689. * Set 'result' to 1/sqrt(a).
  6690. * Relative error: abs(result - 1/sqrt(a)) < 10**-prec * 1/sqrt(a)
  6691. */
  6692. static void
  6693. _mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6694. uint32_t *status)
  6695. {
  6696. uint32_t workstatus = 0;
  6697. mpd_context_t varcontext, maxcontext;
  6698. mpd_t *z = result; /* current approximation */
  6699. mpd_t *v; /* a, normalized to a number between 1 and 100 */
  6700. MPD_NEW_SHARED(vtmp, a); /* by default v will share data with a */
  6701. MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */
  6702. MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */
  6703. MPD_NEW_CONST(one_half,0,-1,1,1,1,5);
  6704. MPD_NEW_CONST(three,0,0,1,1,1,3);
  6705. mpd_ssize_t klist[MPD_MAX_PREC_LOG2];
  6706. mpd_ssize_t ideal_exp, shift;
  6707. mpd_ssize_t adj, tz;
  6708. mpd_ssize_t maxprec, fracdigits;
  6709. mpd_uint_t vhat, dummy;
  6710. int i, n;
  6711. ideal_exp = -(a->exp - (a->exp & 1)) / 2;
  6712. v = &vtmp;
  6713. if (result == a) {
  6714. if ((v = mpd_qncopy(a)) == NULL) {
  6715. mpd_seterror(result, MPD_Malloc_error, status);
  6716. return;
  6717. }
  6718. }
  6719. /* normalize a to 1 <= v < 100 */
  6720. if ((v->digits+v->exp) & 1) {
  6721. fracdigits = v->digits - 1;
  6722. v->exp = -fracdigits;
  6723. n = (v->digits > 7) ? 7 : (int)v->digits;
  6724. /* Let vhat := floor(v * 10**(2*initprec)) */
  6725. _mpd_get_msdigits(&dummy, &vhat, v, n);
  6726. if (n < 7) {
  6727. vhat *= mpd_pow10[7-n];
  6728. }
  6729. }
  6730. else {
  6731. fracdigits = v->digits - 2;
  6732. v->exp = -fracdigits;
  6733. n = (v->digits > 8) ? 8 : (int)v->digits;
  6734. /* Let vhat := floor(v * 10**(2*initprec)) */
  6735. _mpd_get_msdigits(&dummy, &vhat, v, n);
  6736. if (n < 8) {
  6737. vhat *= mpd_pow10[8-n];
  6738. }
  6739. }
  6740. adj = (a->exp-v->exp) / 2;
  6741. /* initial approximation */
  6742. _invroot_init_approx(z, vhat);
  6743. mpd_maxcontext(&maxcontext);
  6744. mpd_maxcontext(&varcontext);
  6745. varcontext.round = MPD_ROUND_TRUNC;
  6746. maxprec = ctx->prec + 1;
  6747. /* initprec == 3 */
  6748. i = invroot_schedule_prec(klist, maxprec, 3);
  6749. for (; i >= 0; i--) {
  6750. varcontext.prec = 2*klist[i]+2;
  6751. mpd_qmul(&s, z, z, &maxcontext, &workstatus);
  6752. if (v->digits > varcontext.prec) {
  6753. shift = v->digits - varcontext.prec;
  6754. mpd_qshiftr(&t, v, shift, &workstatus);
  6755. t.exp += shift;
  6756. mpd_qmul(&t, &t, &s, &varcontext, &workstatus);
  6757. }
  6758. else {
  6759. mpd_qmul(&t, v, &s, &varcontext, &workstatus);
  6760. }
  6761. mpd_qsub(&t, &three, &t, &maxcontext, &workstatus);
  6762. mpd_qmul(z, z, &t, &varcontext, &workstatus);
  6763. mpd_qmul(z, z, &one_half, &maxcontext, &workstatus);
  6764. }
  6765. z->exp -= adj;
  6766. tz = mpd_trail_zeros(result);
  6767. shift = ideal_exp - result->exp;
  6768. shift = (tz > shift) ? shift : tz;
  6769. if (shift > 0) {
  6770. mpd_qshiftr_inplace(result, shift);
  6771. result->exp += shift;
  6772. }
  6773. mpd_del(&s);
  6774. mpd_del(&t);
  6775. if (v != &vtmp) mpd_del(v);
  6776. *status |= (workstatus&MPD_Errors);
  6777. *status |= (MPD_Rounded|MPD_Inexact);
  6778. }
  6779. void
  6780. mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6781. uint32_t *status)
  6782. {
  6783. mpd_context_t workctx;
  6784. if (mpd_isspecial(a)) {
  6785. if (mpd_qcheck_nan(result, a, ctx, status)) {
  6786. return;
  6787. }
  6788. if (mpd_isnegative(a)) {
  6789. mpd_seterror(result, MPD_Invalid_operation, status);
  6790. return;
  6791. }
  6792. /* positive infinity */
  6793. _settriple(result, MPD_POS, 0, mpd_etiny(ctx));
  6794. *status |= MPD_Clamped;
  6795. return;
  6796. }
  6797. if (mpd_iszero(a)) {
  6798. mpd_setspecial(result, mpd_sign(a), MPD_INF);
  6799. *status |= MPD_Division_by_zero;
  6800. return;
  6801. }
  6802. if (mpd_isnegative(a)) {
  6803. mpd_seterror(result, MPD_Invalid_operation, status);
  6804. return;
  6805. }
  6806. workctx = *ctx;
  6807. workctx.prec += 2;
  6808. workctx.round = MPD_ROUND_HALF_EVEN;
  6809. _mpd_qinvroot(result, a, &workctx, status);
  6810. mpd_qfinalize(result, ctx, status);
  6811. }
  6812. /* END LIBMPDEC_ONLY */
  6813. /* Algorithm from decimal.py */
  6814. void
  6815. mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
  6816. uint32_t *status)
  6817. {
  6818. mpd_context_t maxcontext;
  6819. MPD_NEW_STATIC(c,0,0,0,0);
  6820. MPD_NEW_STATIC(q,0,0,0,0);
  6821. MPD_NEW_STATIC(r,0,0,0,0);
  6822. MPD_NEW_CONST(two,0,0,1,1,1,2);
  6823. mpd_ssize_t prec, ideal_exp;
  6824. mpd_ssize_t l, shift;
  6825. int exact = 0;
  6826. ideal_exp = (a->exp - (a->exp & 1)) / 2;
  6827. if (mpd_isspecial(a)) {
  6828. if (mpd_qcheck_nan(result, a, ctx, status)) {
  6829. return;
  6830. }
  6831. if (mpd_isnegative(a)) {
  6832. mpd_seterror(result, MPD_Invalid_operation, status);
  6833. return;
  6834. }
  6835. mpd_setspecial(result, MPD_POS, MPD_INF);
  6836. return;
  6837. }
  6838. if (mpd_iszero(a)) {
  6839. _settriple(result, mpd_sign(a), 0, ideal_exp);
  6840. mpd_qfinalize(result, ctx, status);
  6841. return;
  6842. }
  6843. if (mpd_isnegative(a)) {
  6844. mpd_seterror(result, MPD_Invalid_operation, status);
  6845. return;
  6846. }
  6847. mpd_maxcontext(&maxcontext);
  6848. prec = ctx->prec + 1;
  6849. if (!mpd_qcopy(&c, a, status)) {
  6850. goto malloc_error;
  6851. }
  6852. c.exp = 0;
  6853. if (a->exp & 1) {
  6854. if (!mpd_qshiftl(&c, &c, 1, status)) {
  6855. goto malloc_error;
  6856. }
  6857. l = (a->digits >> 1) + 1;
  6858. }
  6859. else {
  6860. l = (a->digits + 1) >> 1;
  6861. }
  6862. shift = prec - l;
  6863. if (shift >= 0) {
  6864. if (!mpd_qshiftl(&c, &c, 2*shift, status)) {
  6865. goto malloc_error;
  6866. }
  6867. exact = 1;
  6868. }
  6869. else {
  6870. exact = !mpd_qshiftr_inplace(&c, -2*shift);
  6871. }
  6872. ideal_exp -= shift;
  6873. /* find result = floor(sqrt(c)) using Newton's method */
  6874. if (!mpd_qshiftl(result, &one, prec, status)) {
  6875. goto malloc_error;
  6876. }
  6877. while (1) {
  6878. _mpd_qdivmod(&q, &r, &c, result, &maxcontext, &maxcontext.status);
  6879. if (mpd_isspecial(result) || mpd_isspecial(&q)) {
  6880. mpd_seterror(result, maxcontext.status&MPD_Errors, status);
  6881. goto out;
  6882. }
  6883. if (_mpd_cmp(result, &q) <= 0) {
  6884. break;
  6885. }
  6886. _mpd_qadd_exact(result, result, &q, &maxcontext, &maxcontext.status);
  6887. if (mpd_isspecial(result)) {
  6888. mpd_seterror(result, maxcontext.status&MPD_Errors, status);
  6889. goto out;
  6890. }
  6891. _mpd_qdivmod(result, &r, result, &two, &maxcontext, &maxcontext.status);
  6892. }
  6893. if (exact) {
  6894. _mpd_qmul_exact(&r, result, result, &maxcontext, &maxcontext.status);
  6895. if (mpd_isspecial(&r)) {
  6896. mpd_seterror(result, maxcontext.status&MPD_Errors, status);
  6897. goto out;
  6898. }
  6899. exact = (_mpd_cmp(&r, &c) == 0);
  6900. }
  6901. if (exact) {
  6902. if (shift >= 0) {
  6903. mpd_qshiftr_inplace(result, shift);
  6904. }
  6905. else {
  6906. if (!mpd_qshiftl(result, result, -shift, status)) {
  6907. goto malloc_error;
  6908. }
  6909. }
  6910. ideal_exp += shift;
  6911. }
  6912. else {
  6913. int lsd = (int)mpd_lsd(result->data[0]);
  6914. if (lsd == 0 || lsd == 5) {
  6915. result->data[0] += 1;
  6916. }
  6917. }
  6918. result->exp = ideal_exp;
  6919. out:
  6920. mpd_del(&c);
  6921. mpd_del(&q);
  6922. mpd_del(&r);
  6923. maxcontext = *ctx;
  6924. maxcontext.round = MPD_ROUND_HALF_EVEN;
  6925. mpd_qfinalize(result, &maxcontext, status);
  6926. return;
  6927. malloc_error:
  6928. mpd_seterror(result, MPD_Malloc_error, status);
  6929. goto out;
  6930. }
  6931. /******************************************************************************/
  6932. /* Base conversions */
  6933. /******************************************************************************/
  6934. /* Space needed to represent an integer mpd_t in base 'base'. */
  6935. size_t
  6936. mpd_sizeinbase(const mpd_t *a, uint32_t base)
  6937. {
  6938. double x;
  6939. size_t digits;
  6940. assert(mpd_isinteger(a));
  6941. assert(base >= 2);
  6942. if (mpd_iszero(a)) {
  6943. return 1;
  6944. }
  6945. digits = a->digits+a->exp;
  6946. assert(digits > 0);
  6947. #ifdef CONFIG_64
  6948. /* ceil(2711437152599294 / log10(2)) + 4 == 2**53 */
  6949. if (digits > 2711437152599294ULL) {
  6950. return SIZE_MAX;
  6951. }
  6952. #endif
  6953. x = (double)digits / log10(base);
  6954. return (x > SIZE_MAX-1) ? SIZE_MAX : (size_t)x + 1;
  6955. }
  6956. /* Space needed to import a base 'base' integer of length 'srclen'. */
  6957. static mpd_ssize_t
  6958. _mpd_importsize(size_t srclen, uint32_t base)
  6959. {
  6960. double x;
  6961. assert(srclen > 0);
  6962. assert(base >= 2);
  6963. #if SIZE_MAX == UINT64_MAX
  6964. if (srclen > (1ULL<<53)) {
  6965. return MPD_SSIZE_MAX;
  6966. }
  6967. #endif
  6968. x = (double)srclen * (log10(base)/MPD_RDIGITS);
  6969. return (x >= MPD_MAXIMPORT) ? MPD_SSIZE_MAX : (mpd_ssize_t)x + 1;
  6970. }
  6971. static uint8_t
  6972. mpd_resize_u16(uint16_t **w, size_t nmemb)
  6973. {
  6974. uint8_t err = 0;
  6975. *w = mpd_realloc(*w, nmemb, sizeof **w, &err);
  6976. return !err;
  6977. }
  6978. static uint8_t
  6979. mpd_resize_u32(uint32_t **w, size_t nmemb)
  6980. {
  6981. uint8_t err = 0;
  6982. *w = mpd_realloc(*w, nmemb, sizeof **w, &err);
  6983. return !err;
  6984. }
  6985. static size_t
  6986. _baseconv_to_u16(uint16_t **w, size_t wlen, mpd_uint_t wbase,
  6987. mpd_uint_t *u, mpd_ssize_t ulen)
  6988. {
  6989. size_t n = 0;
  6990. assert(wlen > 0 && ulen > 0);
  6991. assert(wbase <= (1U<<16));
  6992. do {
  6993. if (n >= wlen) {
  6994. if (!mpd_resize_u16(w, n+1)) {
  6995. return SIZE_MAX;
  6996. }
  6997. wlen = n+1;
  6998. }
  6999. (*w)[n++] = (uint16_t)_mpd_shortdiv(u, u, ulen, wbase);
  7000. /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */
  7001. ulen = _mpd_real_size(u, ulen);
  7002. } while (u[ulen-1] != 0);
  7003. return n;
  7004. }
  7005. static size_t
  7006. _coeff_from_u16(mpd_t *w, mpd_ssize_t wlen,
  7007. const mpd_uint_t *u, size_t ulen, uint32_t ubase,
  7008. uint32_t *status)
  7009. {
  7010. mpd_ssize_t n = 0;
  7011. mpd_uint_t carry;
  7012. assert(wlen > 0 && ulen > 0);
  7013. assert(ubase <= (1U<<16));
  7014. w->data[n++] = u[--ulen];
  7015. while (--ulen != SIZE_MAX) {
  7016. carry = _mpd_shortmul_c(w->data, w->data, n, ubase);
  7017. if (carry) {
  7018. if (n >= wlen) {
  7019. if (!mpd_qresize(w, n+1, status)) {
  7020. return SIZE_MAX;
  7021. }
  7022. wlen = n+1;
  7023. }
  7024. w->data[n++] = carry;
  7025. }
  7026. carry = _mpd_shortadd(w->data, n, u[ulen]);
  7027. if (carry) {
  7028. if (n >= wlen) {
  7029. if (!mpd_qresize(w, n+1, status)) {
  7030. return SIZE_MAX;
  7031. }
  7032. wlen = n+1;
  7033. }
  7034. w->data[n++] = carry;
  7035. }
  7036. }
  7037. return n;
  7038. }
  7039. /* target base wbase < source base ubase */
  7040. static size_t
  7041. _baseconv_to_smaller(uint32_t **w, size_t wlen, uint32_t wbase,
  7042. mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase)
  7043. {
  7044. size_t n = 0;
  7045. assert(wlen > 0 && ulen > 0);
  7046. assert(wbase < ubase);
  7047. do {
  7048. if (n >= wlen) {
  7049. if (!mpd_resize_u32(w, n+1)) {
  7050. return SIZE_MAX;
  7051. }
  7052. wlen = n+1;
  7053. }
  7054. (*w)[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase);
  7055. /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */
  7056. ulen = _mpd_real_size(u, ulen);
  7057. } while (u[ulen-1] != 0);
  7058. return n;
  7059. }
  7060. #ifdef CONFIG_32
  7061. /* target base 'wbase' == source base 'ubase' */
  7062. static size_t
  7063. _copy_equal_base(uint32_t **w, size_t wlen,
  7064. const uint32_t *u, size_t ulen)
  7065. {
  7066. if (wlen < ulen) {
  7067. if (!mpd_resize_u32(w, ulen)) {
  7068. return SIZE_MAX;
  7069. }
  7070. }
  7071. memcpy(*w, u, ulen * (sizeof **w));
  7072. return ulen;
  7073. }
  7074. /* target base 'wbase' > source base 'ubase' */
  7075. static size_t
  7076. _baseconv_to_larger(uint32_t **w, size_t wlen, mpd_uint_t wbase,
  7077. const mpd_uint_t *u, size_t ulen, mpd_uint_t ubase)
  7078. {
  7079. size_t n = 0;
  7080. mpd_uint_t carry;
  7081. assert(wlen > 0 && ulen > 0);
  7082. assert(ubase < wbase);
  7083. (*w)[n++] = u[--ulen];
  7084. while (--ulen != SIZE_MAX) {
  7085. carry = _mpd_shortmul_b(*w, *w, n, ubase, wbase);
  7086. if (carry) {
  7087. if (n >= wlen) {
  7088. if (!mpd_resize_u32(w, n+1)) {
  7089. return SIZE_MAX;
  7090. }
  7091. wlen = n+1;
  7092. }
  7093. (*w)[n++] = carry;
  7094. }
  7095. carry = _mpd_shortadd_b(*w, n, u[ulen], wbase);
  7096. if (carry) {
  7097. if (n >= wlen) {
  7098. if (!mpd_resize_u32(w, n+1)) {
  7099. return SIZE_MAX;
  7100. }
  7101. wlen = n+1;
  7102. }
  7103. (*w)[n++] = carry;
  7104. }
  7105. }
  7106. return n;
  7107. }
  7108. /* target base wbase < source base ubase */
  7109. static size_t
  7110. _coeff_from_larger_base(mpd_t *w, size_t wlen, mpd_uint_t wbase,
  7111. mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase,
  7112. uint32_t *status)
  7113. {
  7114. size_t n = 0;
  7115. assert(wlen > 0 && ulen > 0);
  7116. assert(wbase < ubase);
  7117. do {
  7118. if (n >= wlen) {
  7119. if (!mpd_qresize(w, n+1, status)) {
  7120. return SIZE_MAX;
  7121. }
  7122. wlen = n+1;
  7123. }
  7124. w->data[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase);
  7125. /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */
  7126. ulen = _mpd_real_size(u, ulen);
  7127. } while (u[ulen-1] != 0);
  7128. return n;
  7129. }
  7130. #endif
  7131. /* target base 'wbase' > source base 'ubase' */
  7132. static size_t
  7133. _coeff_from_smaller_base(mpd_t *w, mpd_ssize_t wlen, mpd_uint_t wbase,
  7134. const uint32_t *u, size_t ulen, mpd_uint_t ubase,
  7135. uint32_t *status)
  7136. {
  7137. mpd_ssize_t n = 0;
  7138. mpd_uint_t carry;
  7139. assert(wlen > 0 && ulen > 0);
  7140. assert(wbase > ubase);
  7141. w->data[n++] = u[--ulen];
  7142. while (--ulen != SIZE_MAX) {
  7143. carry = _mpd_shortmul_b(w->data, w->data, n, ubase, wbase);
  7144. if (carry) {
  7145. if (n >= wlen) {
  7146. if (!mpd_qresize(w, n+1, status)) {
  7147. return SIZE_MAX;
  7148. }
  7149. wlen = n+1;
  7150. }
  7151. w->data[n++] = carry;
  7152. }
  7153. carry = _mpd_shortadd_b(w->data, n, u[ulen], wbase);
  7154. if (carry) {
  7155. if (n >= wlen) {
  7156. if (!mpd_qresize(w, n+1, status)) {
  7157. return SIZE_MAX;
  7158. }
  7159. wlen = n+1;
  7160. }
  7161. w->data[n++] = carry;
  7162. }
  7163. }
  7164. return n;
  7165. }
  7166. /*
  7167. * Convert an integer mpd_t to a multiprecision integer with base <= 2**16.
  7168. * The least significant word of the result is (*rdata)[0].
  7169. *
  7170. * If rdata is NULL, space is allocated by the function and rlen is irrelevant.
  7171. * In case of an error any allocated storage is freed and rdata is set back to
  7172. * NULL.
  7173. *
  7174. * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation
  7175. * functions and rlen MUST be correct. If necessary, the function will resize
  7176. * rdata. In case of an error the caller must free rdata.
  7177. *
  7178. * Return value: In case of success, the exact length of rdata, SIZE_MAX
  7179. * otherwise.
  7180. */
  7181. size_t
  7182. mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t rbase,
  7183. const mpd_t *src, uint32_t *status)
  7184. {
  7185. MPD_NEW_STATIC(tsrc,0,0,0,0);
  7186. int alloc = 0; /* rdata == NULL */
  7187. size_t n;
  7188. assert(rbase <= (1U<<16));
  7189. if (mpd_isspecial(src) || !_mpd_isint(src)) {
  7190. *status |= MPD_Invalid_operation;
  7191. return SIZE_MAX;
  7192. }
  7193. if (*rdata == NULL) {
  7194. rlen = mpd_sizeinbase(src, rbase);
  7195. if (rlen == SIZE_MAX) {
  7196. *status |= MPD_Invalid_operation;
  7197. return SIZE_MAX;
  7198. }
  7199. *rdata = mpd_alloc(rlen, sizeof **rdata);
  7200. if (*rdata == NULL) {
  7201. goto malloc_error;
  7202. }
  7203. alloc = 1;
  7204. }
  7205. if (mpd_iszero(src)) {
  7206. **rdata = 0;
  7207. return 1;
  7208. }
  7209. if (src->exp >= 0) {
  7210. if (!mpd_qshiftl(&tsrc, src, src->exp, status)) {
  7211. goto malloc_error;
  7212. }
  7213. }
  7214. else {
  7215. if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) {
  7216. goto malloc_error;
  7217. }
  7218. }
  7219. n = _baseconv_to_u16(rdata, rlen, rbase, tsrc.data, tsrc.len);
  7220. if (n == SIZE_MAX) {
  7221. goto malloc_error;
  7222. }
  7223. out:
  7224. mpd_del(&tsrc);
  7225. return n;
  7226. malloc_error:
  7227. if (alloc) {
  7228. mpd_free(*rdata);
  7229. *rdata = NULL;
  7230. }
  7231. n = SIZE_MAX;
  7232. *status |= MPD_Malloc_error;
  7233. goto out;
  7234. }
  7235. /*
  7236. * Convert an integer mpd_t to a multiprecision integer with base<=UINT32_MAX.
  7237. * The least significant word of the result is (*rdata)[0].
  7238. *
  7239. * If rdata is NULL, space is allocated by the function and rlen is irrelevant.
  7240. * In case of an error any allocated storage is freed and rdata is set back to
  7241. * NULL.
  7242. *
  7243. * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation
  7244. * functions and rlen MUST be correct. If necessary, the function will resize
  7245. * rdata. In case of an error the caller must free rdata.
  7246. *
  7247. * Return value: In case of success, the exact length of rdata, SIZE_MAX
  7248. * otherwise.
  7249. */
  7250. size_t
  7251. mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t rbase,
  7252. const mpd_t *src, uint32_t *status)
  7253. {
  7254. MPD_NEW_STATIC(tsrc,0,0,0,0);
  7255. int alloc = 0; /* rdata == NULL */
  7256. size_t n;
  7257. if (mpd_isspecial(src) || !_mpd_isint(src)) {
  7258. *status |= MPD_Invalid_operation;
  7259. return SIZE_MAX;
  7260. }
  7261. if (*rdata == NULL) {
  7262. rlen = mpd_sizeinbase(src, rbase);
  7263. if (rlen == SIZE_MAX) {
  7264. *status |= MPD_Invalid_operation;
  7265. return SIZE_MAX;
  7266. }
  7267. *rdata = mpd_alloc(rlen, sizeof **rdata);
  7268. if (*rdata == NULL) {
  7269. goto malloc_error;
  7270. }
  7271. alloc = 1;
  7272. }
  7273. if (mpd_iszero(src)) {
  7274. **rdata = 0;
  7275. return 1;
  7276. }
  7277. if (src->exp >= 0) {
  7278. if (!mpd_qshiftl(&tsrc, src, src->exp, status)) {
  7279. goto malloc_error;
  7280. }
  7281. }
  7282. else {
  7283. if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) {
  7284. goto malloc_error;
  7285. }
  7286. }
  7287. #ifdef CONFIG_64
  7288. n = _baseconv_to_smaller(rdata, rlen, rbase,
  7289. tsrc.data, tsrc.len, MPD_RADIX);
  7290. #else
  7291. if (rbase == MPD_RADIX) {
  7292. n = _copy_equal_base(rdata, rlen, tsrc.data, tsrc.len);
  7293. }
  7294. else if (rbase < MPD_RADIX) {
  7295. n = _baseconv_to_smaller(rdata, rlen, rbase,
  7296. tsrc.data, tsrc.len, MPD_RADIX);
  7297. }
  7298. else {
  7299. n = _baseconv_to_larger(rdata, rlen, rbase,
  7300. tsrc.data, tsrc.len, MPD_RADIX);
  7301. }
  7302. #endif
  7303. if (n == SIZE_MAX) {
  7304. goto malloc_error;
  7305. }
  7306. out:
  7307. mpd_del(&tsrc);
  7308. return n;
  7309. malloc_error:
  7310. if (alloc) {
  7311. mpd_free(*rdata);
  7312. *rdata = NULL;
  7313. }
  7314. n = SIZE_MAX;
  7315. *status |= MPD_Malloc_error;
  7316. goto out;
  7317. }
  7318. /*
  7319. * Converts a multiprecision integer with base <= UINT16_MAX+1 to an mpd_t.
  7320. * The least significant word of the source is srcdata[0].
  7321. */
  7322. void
  7323. mpd_qimport_u16(mpd_t *result,
  7324. const uint16_t *srcdata, size_t srclen,
  7325. uint8_t srcsign, uint32_t srcbase,
  7326. const mpd_context_t *ctx, uint32_t *status)
  7327. {
  7328. mpd_uint_t *usrc; /* uint16_t src copied to an mpd_uint_t array */
  7329. mpd_ssize_t rlen; /* length of the result */
  7330. size_t n;
  7331. assert(srclen > 0);
  7332. assert(srcbase <= (1U<<16));
  7333. rlen = _mpd_importsize(srclen, srcbase);
  7334. if (rlen == MPD_SSIZE_MAX) {
  7335. mpd_seterror(result, MPD_Invalid_operation, status);
  7336. return;
  7337. }
  7338. usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc);
  7339. if (usrc == NULL) {
  7340. mpd_seterror(result, MPD_Malloc_error, status);
  7341. return;
  7342. }
  7343. for (n = 0; n < srclen; n++) {
  7344. usrc[n] = srcdata[n];
  7345. }
  7346. if (!mpd_qresize(result, rlen, status)) {
  7347. goto finish;
  7348. }
  7349. n = _coeff_from_u16(result, rlen, usrc, srclen, srcbase, status);
  7350. if (n == SIZE_MAX) {
  7351. goto finish;
  7352. }
  7353. mpd_set_flags(result, srcsign);
  7354. result->exp = 0;
  7355. result->len = n;
  7356. mpd_setdigits(result);
  7357. mpd_qresize(result, result->len, status);
  7358. mpd_qfinalize(result, ctx, status);
  7359. finish:
  7360. mpd_free(usrc);
  7361. }
  7362. /*
  7363. * Converts a multiprecision integer with base <= UINT32_MAX to an mpd_t.
  7364. * The least significant word of the source is srcdata[0].
  7365. */
  7366. void
  7367. mpd_qimport_u32(mpd_t *result,
  7368. const uint32_t *srcdata, size_t srclen,
  7369. uint8_t srcsign, uint32_t srcbase,
  7370. const mpd_context_t *ctx, uint32_t *status)
  7371. {
  7372. mpd_ssize_t rlen; /* length of the result */
  7373. size_t n;
  7374. assert(srclen > 0);
  7375. rlen = _mpd_importsize(srclen, srcbase);
  7376. if (rlen == MPD_SSIZE_MAX) {
  7377. mpd_seterror(result, MPD_Invalid_operation, status);
  7378. return;
  7379. }
  7380. if (!mpd_qresize(result, rlen, status)) {
  7381. return;
  7382. }
  7383. #ifdef CONFIG_64
  7384. n = _coeff_from_smaller_base(result, rlen, MPD_RADIX,
  7385. srcdata, srclen, srcbase,
  7386. status);
  7387. #else
  7388. if (srcbase == MPD_RADIX) {
  7389. if (!mpd_qresize(result, srclen, status)) {
  7390. return;
  7391. }
  7392. memcpy(result->data, srcdata, srclen * (sizeof *srcdata));
  7393. n = srclen;
  7394. }
  7395. else if (srcbase < MPD_RADIX) {
  7396. n = _coeff_from_smaller_base(result, rlen, MPD_RADIX,
  7397. srcdata, srclen, srcbase,
  7398. status);
  7399. }
  7400. else {
  7401. mpd_uint_t *usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc);
  7402. if (usrc == NULL) {
  7403. mpd_seterror(result, MPD_Malloc_error, status);
  7404. return;
  7405. }
  7406. for (n = 0; n < srclen; n++) {
  7407. usrc[n] = srcdata[n];
  7408. }
  7409. n = _coeff_from_larger_base(result, rlen, MPD_RADIX,
  7410. usrc, (mpd_ssize_t)srclen, srcbase,
  7411. status);
  7412. mpd_free(usrc);
  7413. }
  7414. #endif
  7415. if (n == SIZE_MAX) {
  7416. return;
  7417. }
  7418. mpd_set_flags(result, srcsign);
  7419. result->exp = 0;
  7420. result->len = n;
  7421. mpd_setdigits(result);
  7422. mpd_qresize(result, result->len, status);
  7423. mpd_qfinalize(result, ctx, status);
  7424. }