2
0

softfloat.c 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233
  1. /*
  2. * QEMU float support
  3. *
  4. * The code in this source file is derived from release 2a of the SoftFloat
  5. * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
  6. * some later contributions) are provided under that license, as detailed below.
  7. * It has subsequently been modified by contributors to the QEMU Project,
  8. * so some portions are provided under:
  9. * the SoftFloat-2a license
  10. * the BSD license
  11. * GPL-v2-or-later
  12. *
  13. * Any future contributions to this file after December 1st 2014 will be
  14. * taken to be licensed under the Softfloat-2a license unless specifically
  15. * indicated otherwise.
  16. */
  17. /*
  18. ===============================================================================
  19. This C source file is part of the SoftFloat IEC/IEEE Floating-point
  20. Arithmetic Package, Release 2a.
  21. Written by John R. Hauser. This work was made possible in part by the
  22. International Computer Science Institute, located at Suite 600, 1947 Center
  23. Street, Berkeley, California 94704. Funding was partially provided by the
  24. National Science Foundation under grant MIP-9311980. The original version
  25. of this code was written as part of a project to build a fixed-point vector
  26. processor in collaboration with the University of California at Berkeley,
  27. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  28. is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
  29. arithmetic/SoftFloat.html'.
  30. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  31. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  32. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  33. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  34. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  35. Derivative works are acceptable, even for commercial purposes, so long as
  36. (1) they include prominent notice that the work is derivative, and (2) they
  37. include prominent notice akin to these four paragraphs for those parts of
  38. this code that are retained.
  39. ===============================================================================
  40. */
  41. /* BSD licensing:
  42. * Copyright (c) 2006, Fabrice Bellard
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions are met:
  47. *
  48. * 1. Redistributions of source code must retain the above copyright notice,
  49. * this list of conditions and the following disclaimer.
  50. *
  51. * 2. Redistributions in binary form must reproduce the above copyright notice,
  52. * this list of conditions and the following disclaimer in the documentation
  53. * and/or other materials provided with the distribution.
  54. *
  55. * 3. Neither the name of the copyright holder nor the names of its contributors
  56. * may be used to endorse or promote products derived from this software without
  57. * specific prior written permission.
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  60. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  63. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  64. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  65. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  66. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  67. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  68. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  69. * THE POSSIBILITY OF SUCH DAMAGE.
  70. */
  71. /* Portions of this work are licensed under the terms of the GNU GPL,
  72. * version 2 or later. See the COPYING file in the top-level directory.
  73. */
  74. /* softfloat (and in particular the code in softfloat-specialize.h) is
  75. * target-dependent and needs the TARGET_* macros.
  76. */
  77. #include "qemu/osdep.h"
  78. #include <math.h>
  79. #include "qemu/bitops.h"
  80. #include "fpu/softfloat.h"
  81. /* We only need stdlib for abort() */
  82. /*----------------------------------------------------------------------------
  83. | Primitive arithmetic functions, including multi-word arithmetic, and
  84. | division and square root approximations. (Can be specialized to target if
  85. | desired.)
  86. *----------------------------------------------------------------------------*/
  87. #include "fpu/softfloat-macros.h"
  88. /*
  89. * Hardfloat
  90. *
  91. * Fast emulation of guest FP instructions is challenging for two reasons.
  92. * First, FP instruction semantics are similar but not identical, particularly
  93. * when handling NaNs. Second, emulating at reasonable speed the guest FP
  94. * exception flags is not trivial: reading the host's flags register with a
  95. * feclearexcept & fetestexcept pair is slow [slightly slower than soft-fp],
  96. * and trapping on every FP exception is not fast nor pleasant to work with.
  97. *
  98. * We address these challenges by leveraging the host FPU for a subset of the
  99. * operations. To do this we expand on the idea presented in this paper:
  100. *
  101. * Guo, Yu-Chuan, et al. "Translating the ARM Neon and VFP instructions in a
  102. * binary translator." Software: Practice and Experience 46.12 (2016):1591-1615.
  103. *
  104. * The idea is thus to leverage the host FPU to (1) compute FP operations
  105. * and (2) identify whether FP exceptions occurred while avoiding
  106. * expensive exception flag register accesses.
  107. *
  108. * An important optimization shown in the paper is that given that exception
  109. * flags are rarely cleared by the guest, we can avoid recomputing some flags.
  110. * This is particularly useful for the inexact flag, which is very frequently
  111. * raised in floating-point workloads.
  112. *
  113. * We optimize the code further by deferring to soft-fp whenever FP exception
  114. * detection might get hairy. Two examples: (1) when at least one operand is
  115. * denormal/inf/NaN; (2) when operands are not guaranteed to lead to a 0 result
  116. * and the result is < the minimum normal.
  117. */
  118. #define GEN_INPUT_FLUSH__NOCHECK(name, soft_t) \
  119. static inline void name(soft_t *a, float_status *s) \
  120. { \
  121. if (unlikely(soft_t ## _is_denormal(*a))) { \
  122. *a = soft_t ## _set_sign(soft_t ## _zero, \
  123. soft_t ## _is_neg(*a)); \
  124. s->float_exception_flags |= float_flag_input_denormal; \
  125. } \
  126. }
  127. GEN_INPUT_FLUSH__NOCHECK(float32_input_flush__nocheck, float32)
  128. GEN_INPUT_FLUSH__NOCHECK(float64_input_flush__nocheck, float64)
  129. #undef GEN_INPUT_FLUSH__NOCHECK
  130. #define GEN_INPUT_FLUSH1(name, soft_t) \
  131. static inline void name(soft_t *a, float_status *s) \
  132. { \
  133. if (likely(!s->flush_inputs_to_zero)) { \
  134. return; \
  135. } \
  136. soft_t ## _input_flush__nocheck(a, s); \
  137. }
  138. GEN_INPUT_FLUSH1(float32_input_flush1, float32)
  139. GEN_INPUT_FLUSH1(float64_input_flush1, float64)
  140. #undef GEN_INPUT_FLUSH1
  141. #define GEN_INPUT_FLUSH2(name, soft_t) \
  142. static inline void name(soft_t *a, soft_t *b, float_status *s) \
  143. { \
  144. if (likely(!s->flush_inputs_to_zero)) { \
  145. return; \
  146. } \
  147. soft_t ## _input_flush__nocheck(a, s); \
  148. soft_t ## _input_flush__nocheck(b, s); \
  149. }
  150. GEN_INPUT_FLUSH2(float32_input_flush2, float32)
  151. GEN_INPUT_FLUSH2(float64_input_flush2, float64)
  152. #undef GEN_INPUT_FLUSH2
  153. #define GEN_INPUT_FLUSH3(name, soft_t) \
  154. static inline void name(soft_t *a, soft_t *b, soft_t *c, float_status *s) \
  155. { \
  156. if (likely(!s->flush_inputs_to_zero)) { \
  157. return; \
  158. } \
  159. soft_t ## _input_flush__nocheck(a, s); \
  160. soft_t ## _input_flush__nocheck(b, s); \
  161. soft_t ## _input_flush__nocheck(c, s); \
  162. }
  163. GEN_INPUT_FLUSH3(float32_input_flush3, float32)
  164. GEN_INPUT_FLUSH3(float64_input_flush3, float64)
  165. #undef GEN_INPUT_FLUSH3
  166. /*
  167. * Choose whether to use fpclassify or float32/64_* primitives in the generated
  168. * hardfloat functions. Each combination of number of inputs and float size
  169. * gets its own value.
  170. */
  171. #if defined(__x86_64__)
  172. # define QEMU_HARDFLOAT_1F32_USE_FP 0
  173. # define QEMU_HARDFLOAT_1F64_USE_FP 1
  174. # define QEMU_HARDFLOAT_2F32_USE_FP 0
  175. # define QEMU_HARDFLOAT_2F64_USE_FP 1
  176. # define QEMU_HARDFLOAT_3F32_USE_FP 0
  177. # define QEMU_HARDFLOAT_3F64_USE_FP 1
  178. #else
  179. # define QEMU_HARDFLOAT_1F32_USE_FP 0
  180. # define QEMU_HARDFLOAT_1F64_USE_FP 0
  181. # define QEMU_HARDFLOAT_2F32_USE_FP 0
  182. # define QEMU_HARDFLOAT_2F64_USE_FP 0
  183. # define QEMU_HARDFLOAT_3F32_USE_FP 0
  184. # define QEMU_HARDFLOAT_3F64_USE_FP 0
  185. #endif
  186. /*
  187. * QEMU_HARDFLOAT_USE_ISINF chooses whether to use isinf() over
  188. * float{32,64}_is_infinity when !USE_FP.
  189. * On x86_64/aarch64, using the former over the latter can yield a ~6% speedup.
  190. * On power64 however, using isinf() reduces fp-bench performance by up to 50%.
  191. */
  192. #if defined(__x86_64__) || defined(__aarch64__)
  193. # define QEMU_HARDFLOAT_USE_ISINF 1
  194. #else
  195. # define QEMU_HARDFLOAT_USE_ISINF 0
  196. #endif
  197. /*
  198. * Some targets clear the FP flags before most FP operations. This prevents
  199. * the use of hardfloat, since hardfloat relies on the inexact flag being
  200. * already set.
  201. */
  202. #if defined(TARGET_PPC) || defined(__FAST_MATH__)
  203. # if defined(__FAST_MATH__)
  204. # warning disabling hardfloat due to -ffast-math: hardfloat requires an exact \
  205. IEEE implementation
  206. # endif
  207. # define QEMU_NO_HARDFLOAT 1
  208. # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN
  209. #else
  210. # define QEMU_NO_HARDFLOAT 0
  211. # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN __attribute__((noinline))
  212. #endif
  213. static inline bool can_use_fpu(const float_status *s)
  214. {
  215. if (QEMU_NO_HARDFLOAT) {
  216. return false;
  217. }
  218. return likely(s->float_exception_flags & float_flag_inexact &&
  219. s->float_rounding_mode == float_round_nearest_even);
  220. }
  221. /*
  222. * Hardfloat generation functions. Each operation can have two flavors:
  223. * either using softfloat primitives (e.g. float32_is_zero_or_normal) for
  224. * most condition checks, or native ones (e.g. fpclassify).
  225. *
  226. * The flavor is chosen by the callers. Instead of using macros, we rely on the
  227. * compiler to propagate constants and inline everything into the callers.
  228. *
  229. * We only generate functions for operations with two inputs, since only
  230. * these are common enough to justify consolidating them into common code.
  231. */
  232. typedef union {
  233. float32 s;
  234. float h;
  235. } union_float32;
  236. typedef union {
  237. float64 s;
  238. double h;
  239. } union_float64;
  240. typedef bool (*f32_check_fn)(union_float32 a, union_float32 b);
  241. typedef bool (*f64_check_fn)(union_float64 a, union_float64 b);
  242. typedef float32 (*soft_f32_op2_fn)(float32 a, float32 b, float_status *s);
  243. typedef float64 (*soft_f64_op2_fn)(float64 a, float64 b, float_status *s);
  244. typedef float (*hard_f32_op2_fn)(float a, float b);
  245. typedef double (*hard_f64_op2_fn)(double a, double b);
  246. /* 2-input is-zero-or-normal */
  247. static inline bool f32_is_zon2(union_float32 a, union_float32 b)
  248. {
  249. if (QEMU_HARDFLOAT_2F32_USE_FP) {
  250. /*
  251. * Not using a temp variable for consecutive fpclassify calls ends up
  252. * generating faster code.
  253. */
  254. return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
  255. (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO);
  256. }
  257. return float32_is_zero_or_normal(a.s) &&
  258. float32_is_zero_or_normal(b.s);
  259. }
  260. static inline bool f64_is_zon2(union_float64 a, union_float64 b)
  261. {
  262. if (QEMU_HARDFLOAT_2F64_USE_FP) {
  263. return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
  264. (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO);
  265. }
  266. return float64_is_zero_or_normal(a.s) &&
  267. float64_is_zero_or_normal(b.s);
  268. }
  269. /* 3-input is-zero-or-normal */
  270. static inline
  271. bool f32_is_zon3(union_float32 a, union_float32 b, union_float32 c)
  272. {
  273. if (QEMU_HARDFLOAT_3F32_USE_FP) {
  274. return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
  275. (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO) &&
  276. (fpclassify(c.h) == FP_NORMAL || fpclassify(c.h) == FP_ZERO);
  277. }
  278. return float32_is_zero_or_normal(a.s) &&
  279. float32_is_zero_or_normal(b.s) &&
  280. float32_is_zero_or_normal(c.s);
  281. }
  282. static inline
  283. bool f64_is_zon3(union_float64 a, union_float64 b, union_float64 c)
  284. {
  285. if (QEMU_HARDFLOAT_3F64_USE_FP) {
  286. return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
  287. (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO) &&
  288. (fpclassify(c.h) == FP_NORMAL || fpclassify(c.h) == FP_ZERO);
  289. }
  290. return float64_is_zero_or_normal(a.s) &&
  291. float64_is_zero_or_normal(b.s) &&
  292. float64_is_zero_or_normal(c.s);
  293. }
  294. static inline bool f32_is_inf(union_float32 a)
  295. {
  296. if (QEMU_HARDFLOAT_USE_ISINF) {
  297. return isinf(a.h);
  298. }
  299. return float32_is_infinity(a.s);
  300. }
  301. static inline bool f64_is_inf(union_float64 a)
  302. {
  303. if (QEMU_HARDFLOAT_USE_ISINF) {
  304. return isinf(a.h);
  305. }
  306. return float64_is_infinity(a.s);
  307. }
  308. static inline float32
  309. float32_gen2(float32 xa, float32 xb, float_status *s,
  310. hard_f32_op2_fn hard, soft_f32_op2_fn soft,
  311. f32_check_fn pre, f32_check_fn post)
  312. {
  313. union_float32 ua, ub, ur;
  314. ua.s = xa;
  315. ub.s = xb;
  316. if (unlikely(!can_use_fpu(s))) {
  317. goto soft;
  318. }
  319. float32_input_flush2(&ua.s, &ub.s, s);
  320. if (unlikely(!pre(ua, ub))) {
  321. goto soft;
  322. }
  323. ur.h = hard(ua.h, ub.h);
  324. if (unlikely(f32_is_inf(ur))) {
  325. s->float_exception_flags |= float_flag_overflow;
  326. } else if (unlikely(fabsf(ur.h) <= FLT_MIN) && post(ua, ub)) {
  327. goto soft;
  328. }
  329. return ur.s;
  330. soft:
  331. return soft(ua.s, ub.s, s);
  332. }
  333. static inline float64
  334. float64_gen2(float64 xa, float64 xb, float_status *s,
  335. hard_f64_op2_fn hard, soft_f64_op2_fn soft,
  336. f64_check_fn pre, f64_check_fn post)
  337. {
  338. union_float64 ua, ub, ur;
  339. ua.s = xa;
  340. ub.s = xb;
  341. if (unlikely(!can_use_fpu(s))) {
  342. goto soft;
  343. }
  344. float64_input_flush2(&ua.s, &ub.s, s);
  345. if (unlikely(!pre(ua, ub))) {
  346. goto soft;
  347. }
  348. ur.h = hard(ua.h, ub.h);
  349. if (unlikely(f64_is_inf(ur))) {
  350. s->float_exception_flags |= float_flag_overflow;
  351. } else if (unlikely(fabs(ur.h) <= DBL_MIN) && post(ua, ub)) {
  352. goto soft;
  353. }
  354. return ur.s;
  355. soft:
  356. return soft(ua.s, ub.s, s);
  357. }
  358. /*----------------------------------------------------------------------------
  359. | Returns the fraction bits of the single-precision floating-point value `a'.
  360. *----------------------------------------------------------------------------*/
  361. static inline uint32_t extractFloat32Frac(float32 a)
  362. {
  363. return float32_val(a) & 0x007FFFFF;
  364. }
  365. /*----------------------------------------------------------------------------
  366. | Returns the exponent bits of the single-precision floating-point value `a'.
  367. *----------------------------------------------------------------------------*/
  368. static inline int extractFloat32Exp(float32 a)
  369. {
  370. return (float32_val(a) >> 23) & 0xFF;
  371. }
  372. /*----------------------------------------------------------------------------
  373. | Returns the sign bit of the single-precision floating-point value `a'.
  374. *----------------------------------------------------------------------------*/
  375. static inline bool extractFloat32Sign(float32 a)
  376. {
  377. return float32_val(a) >> 31;
  378. }
  379. /*----------------------------------------------------------------------------
  380. | Returns the fraction bits of the double-precision floating-point value `a'.
  381. *----------------------------------------------------------------------------*/
  382. static inline uint64_t extractFloat64Frac(float64 a)
  383. {
  384. return float64_val(a) & UINT64_C(0x000FFFFFFFFFFFFF);
  385. }
  386. /*----------------------------------------------------------------------------
  387. | Returns the exponent bits of the double-precision floating-point value `a'.
  388. *----------------------------------------------------------------------------*/
  389. static inline int extractFloat64Exp(float64 a)
  390. {
  391. return (float64_val(a) >> 52) & 0x7FF;
  392. }
  393. /*----------------------------------------------------------------------------
  394. | Returns the sign bit of the double-precision floating-point value `a'.
  395. *----------------------------------------------------------------------------*/
  396. static inline bool extractFloat64Sign(float64 a)
  397. {
  398. return float64_val(a) >> 63;
  399. }
  400. /*
  401. * Classify a floating point number. Everything above float_class_qnan
  402. * is a NaN so cls >= float_class_qnan is any NaN.
  403. */
  404. typedef enum __attribute__ ((__packed__)) {
  405. float_class_unclassified,
  406. float_class_zero,
  407. float_class_normal,
  408. float_class_inf,
  409. float_class_qnan, /* all NaNs from here */
  410. float_class_snan,
  411. } FloatClass;
  412. /* Simple helpers for checking if, or what kind of, NaN we have */
  413. static inline __attribute__((unused)) bool is_nan(FloatClass c)
  414. {
  415. return unlikely(c >= float_class_qnan);
  416. }
  417. static inline __attribute__((unused)) bool is_snan(FloatClass c)
  418. {
  419. return c == float_class_snan;
  420. }
  421. static inline __attribute__((unused)) bool is_qnan(FloatClass c)
  422. {
  423. return c == float_class_qnan;
  424. }
  425. /*
  426. * Structure holding all of the decomposed parts of a float. The
  427. * exponent is unbiased and the fraction is normalized. All
  428. * calculations are done with a 64 bit fraction and then rounded as
  429. * appropriate for the final format.
  430. *
  431. * Thanks to the packed FloatClass a decent compiler should be able to
  432. * fit the whole structure into registers and avoid using the stack
  433. * for parameter passing.
  434. */
  435. typedef struct {
  436. uint64_t frac;
  437. int32_t exp;
  438. FloatClass cls;
  439. bool sign;
  440. } FloatParts;
  441. #define DECOMPOSED_BINARY_POINT (64 - 2)
  442. #define DECOMPOSED_IMPLICIT_BIT (1ull << DECOMPOSED_BINARY_POINT)
  443. #define DECOMPOSED_OVERFLOW_BIT (DECOMPOSED_IMPLICIT_BIT << 1)
  444. /* Structure holding all of the relevant parameters for a format.
  445. * exp_size: the size of the exponent field
  446. * exp_bias: the offset applied to the exponent field
  447. * exp_max: the maximum normalised exponent
  448. * frac_size: the size of the fraction field
  449. * frac_shift: shift to normalise the fraction with DECOMPOSED_BINARY_POINT
  450. * The following are computed based the size of fraction
  451. * frac_lsb: least significant bit of fraction
  452. * frac_lsbm1: the bit below the least significant bit (for rounding)
  453. * round_mask/roundeven_mask: masks used for rounding
  454. * The following optional modifiers are available:
  455. * arm_althp: handle ARM Alternative Half Precision
  456. */
  457. typedef struct {
  458. int exp_size;
  459. int exp_bias;
  460. int exp_max;
  461. int frac_size;
  462. int frac_shift;
  463. uint64_t frac_lsb;
  464. uint64_t frac_lsbm1;
  465. uint64_t round_mask;
  466. uint64_t roundeven_mask;
  467. bool arm_althp;
  468. } FloatFmt;
  469. /* Expand fields based on the size of exponent and fraction */
  470. #define FLOAT_PARAMS(E, F) \
  471. .exp_size = E, \
  472. .exp_bias = ((1 << E) - 1) >> 1, \
  473. .exp_max = (1 << E) - 1, \
  474. .frac_size = F, \
  475. .frac_shift = DECOMPOSED_BINARY_POINT - F, \
  476. .frac_lsb = 1ull << (DECOMPOSED_BINARY_POINT - F), \
  477. .frac_lsbm1 = 1ull << ((DECOMPOSED_BINARY_POINT - F) - 1), \
  478. .round_mask = (1ull << (DECOMPOSED_BINARY_POINT - F)) - 1, \
  479. .roundeven_mask = (2ull << (DECOMPOSED_BINARY_POINT - F)) - 1
  480. static const FloatFmt float16_params = {
  481. FLOAT_PARAMS(5, 10)
  482. };
  483. static const FloatFmt float16_params_ahp = {
  484. FLOAT_PARAMS(5, 10),
  485. .arm_althp = true
  486. };
  487. static const FloatFmt float32_params = {
  488. FLOAT_PARAMS(8, 23)
  489. };
  490. static const FloatFmt float64_params = {
  491. FLOAT_PARAMS(11, 52)
  492. };
  493. /* Unpack a float to parts, but do not canonicalize. */
  494. static inline FloatParts unpack_raw(FloatFmt fmt, uint64_t raw)
  495. {
  496. const int sign_pos = fmt.frac_size + fmt.exp_size;
  497. return (FloatParts) {
  498. .cls = float_class_unclassified,
  499. .sign = extract64(raw, sign_pos, 1),
  500. .exp = extract64(raw, fmt.frac_size, fmt.exp_size),
  501. .frac = extract64(raw, 0, fmt.frac_size),
  502. };
  503. }
  504. static inline FloatParts float16_unpack_raw(float16 f)
  505. {
  506. return unpack_raw(float16_params, f);
  507. }
  508. static inline FloatParts float32_unpack_raw(float32 f)
  509. {
  510. return unpack_raw(float32_params, f);
  511. }
  512. static inline FloatParts float64_unpack_raw(float64 f)
  513. {
  514. return unpack_raw(float64_params, f);
  515. }
  516. /* Pack a float from parts, but do not canonicalize. */
  517. static inline uint64_t pack_raw(FloatFmt fmt, FloatParts p)
  518. {
  519. const int sign_pos = fmt.frac_size + fmt.exp_size;
  520. uint64_t ret = deposit64(p.frac, fmt.frac_size, fmt.exp_size, p.exp);
  521. return deposit64(ret, sign_pos, 1, p.sign);
  522. }
  523. static inline float16 float16_pack_raw(FloatParts p)
  524. {
  525. return make_float16(pack_raw(float16_params, p));
  526. }
  527. static inline float32 float32_pack_raw(FloatParts p)
  528. {
  529. return make_float32(pack_raw(float32_params, p));
  530. }
  531. static inline float64 float64_pack_raw(FloatParts p)
  532. {
  533. return make_float64(pack_raw(float64_params, p));
  534. }
  535. /*----------------------------------------------------------------------------
  536. | Functions and definitions to determine: (1) whether tininess for underflow
  537. | is detected before or after rounding by default, (2) what (if anything)
  538. | happens when exceptions are raised, (3) how signaling NaNs are distinguished
  539. | from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
  540. | are propagated from function inputs to output. These details are target-
  541. | specific.
  542. *----------------------------------------------------------------------------*/
  543. #include "softfloat-specialize.c.inc"
  544. /* Canonicalize EXP and FRAC, setting CLS. */
  545. static FloatParts sf_canonicalize(FloatParts part, const FloatFmt *parm,
  546. float_status *status)
  547. {
  548. if (part.exp == parm->exp_max && !parm->arm_althp) {
  549. if (part.frac == 0) {
  550. part.cls = float_class_inf;
  551. } else {
  552. part.frac <<= parm->frac_shift;
  553. part.cls = (parts_is_snan_frac(part.frac, status)
  554. ? float_class_snan : float_class_qnan);
  555. }
  556. } else if (part.exp == 0) {
  557. if (likely(part.frac == 0)) {
  558. part.cls = float_class_zero;
  559. } else if (status->flush_inputs_to_zero) {
  560. float_raise(float_flag_input_denormal, status);
  561. part.cls = float_class_zero;
  562. part.frac = 0;
  563. } else {
  564. int shift = clz64(part.frac) - 1;
  565. part.cls = float_class_normal;
  566. part.exp = parm->frac_shift - parm->exp_bias - shift + 1;
  567. part.frac <<= shift;
  568. }
  569. } else {
  570. part.cls = float_class_normal;
  571. part.exp -= parm->exp_bias;
  572. part.frac = DECOMPOSED_IMPLICIT_BIT + (part.frac << parm->frac_shift);
  573. }
  574. return part;
  575. }
  576. /* Round and uncanonicalize a floating-point number by parts. There
  577. * are FRAC_SHIFT bits that may require rounding at the bottom of the
  578. * fraction; these bits will be removed. The exponent will be biased
  579. * by EXP_BIAS and must be bounded by [EXP_MAX-1, 0].
  580. */
  581. static FloatParts round_canonical(FloatParts p, float_status *s,
  582. const FloatFmt *parm)
  583. {
  584. const uint64_t frac_lsb = parm->frac_lsb;
  585. const uint64_t frac_lsbm1 = parm->frac_lsbm1;
  586. const uint64_t round_mask = parm->round_mask;
  587. const uint64_t roundeven_mask = parm->roundeven_mask;
  588. const int exp_max = parm->exp_max;
  589. const int frac_shift = parm->frac_shift;
  590. uint64_t frac, inc;
  591. int exp, flags = 0;
  592. bool overflow_norm;
  593. frac = p.frac;
  594. exp = p.exp;
  595. switch (p.cls) {
  596. case float_class_normal:
  597. switch (s->float_rounding_mode) {
  598. case float_round_nearest_even:
  599. overflow_norm = false;
  600. inc = ((frac & roundeven_mask) != frac_lsbm1 ? frac_lsbm1 : 0);
  601. break;
  602. case float_round_ties_away:
  603. overflow_norm = false;
  604. inc = frac_lsbm1;
  605. break;
  606. case float_round_to_zero:
  607. overflow_norm = true;
  608. inc = 0;
  609. break;
  610. case float_round_up:
  611. inc = p.sign ? 0 : round_mask;
  612. overflow_norm = p.sign;
  613. break;
  614. case float_round_down:
  615. inc = p.sign ? round_mask : 0;
  616. overflow_norm = !p.sign;
  617. break;
  618. case float_round_to_odd:
  619. overflow_norm = true;
  620. inc = frac & frac_lsb ? 0 : round_mask;
  621. break;
  622. default:
  623. g_assert_not_reached();
  624. }
  625. exp += parm->exp_bias;
  626. if (likely(exp > 0)) {
  627. if (frac & round_mask) {
  628. flags |= float_flag_inexact;
  629. frac += inc;
  630. if (frac & DECOMPOSED_OVERFLOW_BIT) {
  631. frac >>= 1;
  632. exp++;
  633. }
  634. }
  635. frac >>= frac_shift;
  636. if (parm->arm_althp) {
  637. /* ARM Alt HP eschews Inf and NaN for a wider exponent. */
  638. if (unlikely(exp > exp_max)) {
  639. /* Overflow. Return the maximum normal. */
  640. flags = float_flag_invalid;
  641. exp = exp_max;
  642. frac = -1;
  643. }
  644. } else if (unlikely(exp >= exp_max)) {
  645. flags |= float_flag_overflow | float_flag_inexact;
  646. if (overflow_norm) {
  647. exp = exp_max - 1;
  648. frac = -1;
  649. } else {
  650. p.cls = float_class_inf;
  651. goto do_inf;
  652. }
  653. }
  654. } else if (s->flush_to_zero) {
  655. flags |= float_flag_output_denormal;
  656. p.cls = float_class_zero;
  657. goto do_zero;
  658. } else {
  659. bool is_tiny = s->tininess_before_rounding
  660. || (exp < 0)
  661. || !((frac + inc) & DECOMPOSED_OVERFLOW_BIT);
  662. shift64RightJamming(frac, 1 - exp, &frac);
  663. if (frac & round_mask) {
  664. /* Need to recompute round-to-even. */
  665. switch (s->float_rounding_mode) {
  666. case float_round_nearest_even:
  667. inc = ((frac & roundeven_mask) != frac_lsbm1
  668. ? frac_lsbm1 : 0);
  669. break;
  670. case float_round_to_odd:
  671. inc = frac & frac_lsb ? 0 : round_mask;
  672. break;
  673. default:
  674. break;
  675. }
  676. flags |= float_flag_inexact;
  677. frac += inc;
  678. }
  679. exp = (frac & DECOMPOSED_IMPLICIT_BIT ? 1 : 0);
  680. frac >>= frac_shift;
  681. if (is_tiny && (flags & float_flag_inexact)) {
  682. flags |= float_flag_underflow;
  683. }
  684. if (exp == 0 && frac == 0) {
  685. p.cls = float_class_zero;
  686. }
  687. }
  688. break;
  689. case float_class_zero:
  690. do_zero:
  691. exp = 0;
  692. frac = 0;
  693. break;
  694. case float_class_inf:
  695. do_inf:
  696. assert(!parm->arm_althp);
  697. exp = exp_max;
  698. frac = 0;
  699. break;
  700. case float_class_qnan:
  701. case float_class_snan:
  702. assert(!parm->arm_althp);
  703. exp = exp_max;
  704. frac >>= parm->frac_shift;
  705. break;
  706. default:
  707. g_assert_not_reached();
  708. }
  709. float_raise(flags, s);
  710. p.exp = exp;
  711. p.frac = frac;
  712. return p;
  713. }
  714. /* Explicit FloatFmt version */
  715. static FloatParts float16a_unpack_canonical(float16 f, float_status *s,
  716. const FloatFmt *params)
  717. {
  718. return sf_canonicalize(float16_unpack_raw(f), params, s);
  719. }
  720. static FloatParts float16_unpack_canonical(float16 f, float_status *s)
  721. {
  722. return float16a_unpack_canonical(f, s, &float16_params);
  723. }
  724. static float16 float16a_round_pack_canonical(FloatParts p, float_status *s,
  725. const FloatFmt *params)
  726. {
  727. return float16_pack_raw(round_canonical(p, s, params));
  728. }
  729. static float16 float16_round_pack_canonical(FloatParts p, float_status *s)
  730. {
  731. return float16a_round_pack_canonical(p, s, &float16_params);
  732. }
  733. static FloatParts float32_unpack_canonical(float32 f, float_status *s)
  734. {
  735. return sf_canonicalize(float32_unpack_raw(f), &float32_params, s);
  736. }
  737. static float32 float32_round_pack_canonical(FloatParts p, float_status *s)
  738. {
  739. return float32_pack_raw(round_canonical(p, s, &float32_params));
  740. }
  741. static FloatParts float64_unpack_canonical(float64 f, float_status *s)
  742. {
  743. return sf_canonicalize(float64_unpack_raw(f), &float64_params, s);
  744. }
  745. static float64 float64_round_pack_canonical(FloatParts p, float_status *s)
  746. {
  747. return float64_pack_raw(round_canonical(p, s, &float64_params));
  748. }
  749. static FloatParts return_nan(FloatParts a, float_status *s)
  750. {
  751. switch (a.cls) {
  752. case float_class_snan:
  753. s->float_exception_flags |= float_flag_invalid;
  754. a = parts_silence_nan(a, s);
  755. /* fall through */
  756. case float_class_qnan:
  757. if (s->default_nan_mode) {
  758. return parts_default_nan(s);
  759. }
  760. break;
  761. default:
  762. g_assert_not_reached();
  763. }
  764. return a;
  765. }
  766. static FloatParts pick_nan(FloatParts a, FloatParts b, float_status *s)
  767. {
  768. if (is_snan(a.cls) || is_snan(b.cls)) {
  769. s->float_exception_flags |= float_flag_invalid;
  770. }
  771. if (s->default_nan_mode) {
  772. return parts_default_nan(s);
  773. } else {
  774. if (pickNaN(a.cls, b.cls,
  775. a.frac > b.frac ||
  776. (a.frac == b.frac && a.sign < b.sign))) {
  777. a = b;
  778. }
  779. if (is_snan(a.cls)) {
  780. return parts_silence_nan(a, s);
  781. }
  782. }
  783. return a;
  784. }
  785. static FloatParts pick_nan_muladd(FloatParts a, FloatParts b, FloatParts c,
  786. bool inf_zero, float_status *s)
  787. {
  788. int which;
  789. if (is_snan(a.cls) || is_snan(b.cls) || is_snan(c.cls)) {
  790. s->float_exception_flags |= float_flag_invalid;
  791. }
  792. which = pickNaNMulAdd(a.cls, b.cls, c.cls, inf_zero, s);
  793. if (s->default_nan_mode) {
  794. /* Note that this check is after pickNaNMulAdd so that function
  795. * has an opportunity to set the Invalid flag.
  796. */
  797. which = 3;
  798. }
  799. switch (which) {
  800. case 0:
  801. break;
  802. case 1:
  803. a = b;
  804. break;
  805. case 2:
  806. a = c;
  807. break;
  808. case 3:
  809. return parts_default_nan(s);
  810. default:
  811. g_assert_not_reached();
  812. }
  813. if (is_snan(a.cls)) {
  814. return parts_silence_nan(a, s);
  815. }
  816. return a;
  817. }
  818. /*
  819. * Returns the result of adding or subtracting the values of the
  820. * floating-point values `a' and `b'. The operation is performed
  821. * according to the IEC/IEEE Standard for Binary Floating-Point
  822. * Arithmetic.
  823. */
  824. static FloatParts addsub_floats(FloatParts a, FloatParts b, bool subtract,
  825. float_status *s)
  826. {
  827. bool a_sign = a.sign;
  828. bool b_sign = b.sign ^ subtract;
  829. if (a_sign != b_sign) {
  830. /* Subtraction */
  831. if (a.cls == float_class_normal && b.cls == float_class_normal) {
  832. if (a.exp > b.exp || (a.exp == b.exp && a.frac >= b.frac)) {
  833. shift64RightJamming(b.frac, a.exp - b.exp, &b.frac);
  834. a.frac = a.frac - b.frac;
  835. } else {
  836. shift64RightJamming(a.frac, b.exp - a.exp, &a.frac);
  837. a.frac = b.frac - a.frac;
  838. a.exp = b.exp;
  839. a_sign ^= 1;
  840. }
  841. if (a.frac == 0) {
  842. a.cls = float_class_zero;
  843. a.sign = s->float_rounding_mode == float_round_down;
  844. } else {
  845. int shift = clz64(a.frac) - 1;
  846. a.frac = a.frac << shift;
  847. a.exp = a.exp - shift;
  848. a.sign = a_sign;
  849. }
  850. return a;
  851. }
  852. if (is_nan(a.cls) || is_nan(b.cls)) {
  853. return pick_nan(a, b, s);
  854. }
  855. if (a.cls == float_class_inf) {
  856. if (b.cls == float_class_inf) {
  857. float_raise(float_flag_invalid, s);
  858. return parts_default_nan(s);
  859. }
  860. return a;
  861. }
  862. if (a.cls == float_class_zero && b.cls == float_class_zero) {
  863. a.sign = s->float_rounding_mode == float_round_down;
  864. return a;
  865. }
  866. if (a.cls == float_class_zero || b.cls == float_class_inf) {
  867. b.sign = a_sign ^ 1;
  868. return b;
  869. }
  870. if (b.cls == float_class_zero) {
  871. return a;
  872. }
  873. } else {
  874. /* Addition */
  875. if (a.cls == float_class_normal && b.cls == float_class_normal) {
  876. if (a.exp > b.exp) {
  877. shift64RightJamming(b.frac, a.exp - b.exp, &b.frac);
  878. } else if (a.exp < b.exp) {
  879. shift64RightJamming(a.frac, b.exp - a.exp, &a.frac);
  880. a.exp = b.exp;
  881. }
  882. a.frac += b.frac;
  883. if (a.frac & DECOMPOSED_OVERFLOW_BIT) {
  884. shift64RightJamming(a.frac, 1, &a.frac);
  885. a.exp += 1;
  886. }
  887. return a;
  888. }
  889. if (is_nan(a.cls) || is_nan(b.cls)) {
  890. return pick_nan(a, b, s);
  891. }
  892. if (a.cls == float_class_inf || b.cls == float_class_zero) {
  893. return a;
  894. }
  895. if (b.cls == float_class_inf || a.cls == float_class_zero) {
  896. b.sign = b_sign;
  897. return b;
  898. }
  899. }
  900. g_assert_not_reached();
  901. }
  902. /*
  903. * Returns the result of adding or subtracting the floating-point
  904. * values `a' and `b'. The operation is performed according to the
  905. * IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  906. */
  907. float16 QEMU_FLATTEN float16_add(float16 a, float16 b, float_status *status)
  908. {
  909. FloatParts pa = float16_unpack_canonical(a, status);
  910. FloatParts pb = float16_unpack_canonical(b, status);
  911. FloatParts pr = addsub_floats(pa, pb, false, status);
  912. return float16_round_pack_canonical(pr, status);
  913. }
  914. float16 QEMU_FLATTEN float16_sub(float16 a, float16 b, float_status *status)
  915. {
  916. FloatParts pa = float16_unpack_canonical(a, status);
  917. FloatParts pb = float16_unpack_canonical(b, status);
  918. FloatParts pr = addsub_floats(pa, pb, true, status);
  919. return float16_round_pack_canonical(pr, status);
  920. }
  921. static float32 QEMU_SOFTFLOAT_ATTR
  922. soft_f32_addsub(float32 a, float32 b, bool subtract, float_status *status)
  923. {
  924. FloatParts pa = float32_unpack_canonical(a, status);
  925. FloatParts pb = float32_unpack_canonical(b, status);
  926. FloatParts pr = addsub_floats(pa, pb, subtract, status);
  927. return float32_round_pack_canonical(pr, status);
  928. }
  929. static inline float32 soft_f32_add(float32 a, float32 b, float_status *status)
  930. {
  931. return soft_f32_addsub(a, b, false, status);
  932. }
  933. static inline float32 soft_f32_sub(float32 a, float32 b, float_status *status)
  934. {
  935. return soft_f32_addsub(a, b, true, status);
  936. }
  937. static float64 QEMU_SOFTFLOAT_ATTR
  938. soft_f64_addsub(float64 a, float64 b, bool subtract, float_status *status)
  939. {
  940. FloatParts pa = float64_unpack_canonical(a, status);
  941. FloatParts pb = float64_unpack_canonical(b, status);
  942. FloatParts pr = addsub_floats(pa, pb, subtract, status);
  943. return float64_round_pack_canonical(pr, status);
  944. }
  945. static inline float64 soft_f64_add(float64 a, float64 b, float_status *status)
  946. {
  947. return soft_f64_addsub(a, b, false, status);
  948. }
  949. static inline float64 soft_f64_sub(float64 a, float64 b, float_status *status)
  950. {
  951. return soft_f64_addsub(a, b, true, status);
  952. }
  953. static float hard_f32_add(float a, float b)
  954. {
  955. return a + b;
  956. }
  957. static float hard_f32_sub(float a, float b)
  958. {
  959. return a - b;
  960. }
  961. static double hard_f64_add(double a, double b)
  962. {
  963. return a + b;
  964. }
  965. static double hard_f64_sub(double a, double b)
  966. {
  967. return a - b;
  968. }
  969. static bool f32_addsubmul_post(union_float32 a, union_float32 b)
  970. {
  971. if (QEMU_HARDFLOAT_2F32_USE_FP) {
  972. return !(fpclassify(a.h) == FP_ZERO && fpclassify(b.h) == FP_ZERO);
  973. }
  974. return !(float32_is_zero(a.s) && float32_is_zero(b.s));
  975. }
  976. static bool f64_addsubmul_post(union_float64 a, union_float64 b)
  977. {
  978. if (QEMU_HARDFLOAT_2F64_USE_FP) {
  979. return !(fpclassify(a.h) == FP_ZERO && fpclassify(b.h) == FP_ZERO);
  980. } else {
  981. return !(float64_is_zero(a.s) && float64_is_zero(b.s));
  982. }
  983. }
  984. static float32 float32_addsub(float32 a, float32 b, float_status *s,
  985. hard_f32_op2_fn hard, soft_f32_op2_fn soft)
  986. {
  987. return float32_gen2(a, b, s, hard, soft,
  988. f32_is_zon2, f32_addsubmul_post);
  989. }
  990. static float64 float64_addsub(float64 a, float64 b, float_status *s,
  991. hard_f64_op2_fn hard, soft_f64_op2_fn soft)
  992. {
  993. return float64_gen2(a, b, s, hard, soft,
  994. f64_is_zon2, f64_addsubmul_post);
  995. }
  996. float32 QEMU_FLATTEN
  997. float32_add(float32 a, float32 b, float_status *s)
  998. {
  999. return float32_addsub(a, b, s, hard_f32_add, soft_f32_add);
  1000. }
  1001. float32 QEMU_FLATTEN
  1002. float32_sub(float32 a, float32 b, float_status *s)
  1003. {
  1004. return float32_addsub(a, b, s, hard_f32_sub, soft_f32_sub);
  1005. }
  1006. float64 QEMU_FLATTEN
  1007. float64_add(float64 a, float64 b, float_status *s)
  1008. {
  1009. return float64_addsub(a, b, s, hard_f64_add, soft_f64_add);
  1010. }
  1011. float64 QEMU_FLATTEN
  1012. float64_sub(float64 a, float64 b, float_status *s)
  1013. {
  1014. return float64_addsub(a, b, s, hard_f64_sub, soft_f64_sub);
  1015. }
  1016. /*
  1017. * Returns the result of multiplying the floating-point values `a' and
  1018. * `b'. The operation is performed according to the IEC/IEEE Standard
  1019. * for Binary Floating-Point Arithmetic.
  1020. */
  1021. static FloatParts mul_floats(FloatParts a, FloatParts b, float_status *s)
  1022. {
  1023. bool sign = a.sign ^ b.sign;
  1024. if (a.cls == float_class_normal && b.cls == float_class_normal) {
  1025. uint64_t hi, lo;
  1026. int exp = a.exp + b.exp;
  1027. mul64To128(a.frac, b.frac, &hi, &lo);
  1028. shift128RightJamming(hi, lo, DECOMPOSED_BINARY_POINT, &hi, &lo);
  1029. if (lo & DECOMPOSED_OVERFLOW_BIT) {
  1030. shift64RightJamming(lo, 1, &lo);
  1031. exp += 1;
  1032. }
  1033. /* Re-use a */
  1034. a.exp = exp;
  1035. a.sign = sign;
  1036. a.frac = lo;
  1037. return a;
  1038. }
  1039. /* handle all the NaN cases */
  1040. if (is_nan(a.cls) || is_nan(b.cls)) {
  1041. return pick_nan(a, b, s);
  1042. }
  1043. /* Inf * Zero == NaN */
  1044. if ((a.cls == float_class_inf && b.cls == float_class_zero) ||
  1045. (a.cls == float_class_zero && b.cls == float_class_inf)) {
  1046. s->float_exception_flags |= float_flag_invalid;
  1047. return parts_default_nan(s);
  1048. }
  1049. /* Multiply by 0 or Inf */
  1050. if (a.cls == float_class_inf || a.cls == float_class_zero) {
  1051. a.sign = sign;
  1052. return a;
  1053. }
  1054. if (b.cls == float_class_inf || b.cls == float_class_zero) {
  1055. b.sign = sign;
  1056. return b;
  1057. }
  1058. g_assert_not_reached();
  1059. }
  1060. float16 QEMU_FLATTEN float16_mul(float16 a, float16 b, float_status *status)
  1061. {
  1062. FloatParts pa = float16_unpack_canonical(a, status);
  1063. FloatParts pb = float16_unpack_canonical(b, status);
  1064. FloatParts pr = mul_floats(pa, pb, status);
  1065. return float16_round_pack_canonical(pr, status);
  1066. }
  1067. static float32 QEMU_SOFTFLOAT_ATTR
  1068. soft_f32_mul(float32 a, float32 b, float_status *status)
  1069. {
  1070. FloatParts pa = float32_unpack_canonical(a, status);
  1071. FloatParts pb = float32_unpack_canonical(b, status);
  1072. FloatParts pr = mul_floats(pa, pb, status);
  1073. return float32_round_pack_canonical(pr, status);
  1074. }
  1075. static float64 QEMU_SOFTFLOAT_ATTR
  1076. soft_f64_mul(float64 a, float64 b, float_status *status)
  1077. {
  1078. FloatParts pa = float64_unpack_canonical(a, status);
  1079. FloatParts pb = float64_unpack_canonical(b, status);
  1080. FloatParts pr = mul_floats(pa, pb, status);
  1081. return float64_round_pack_canonical(pr, status);
  1082. }
  1083. static float hard_f32_mul(float a, float b)
  1084. {
  1085. return a * b;
  1086. }
  1087. static double hard_f64_mul(double a, double b)
  1088. {
  1089. return a * b;
  1090. }
  1091. float32 QEMU_FLATTEN
  1092. float32_mul(float32 a, float32 b, float_status *s)
  1093. {
  1094. return float32_gen2(a, b, s, hard_f32_mul, soft_f32_mul,
  1095. f32_is_zon2, f32_addsubmul_post);
  1096. }
  1097. float64 QEMU_FLATTEN
  1098. float64_mul(float64 a, float64 b, float_status *s)
  1099. {
  1100. return float64_gen2(a, b, s, hard_f64_mul, soft_f64_mul,
  1101. f64_is_zon2, f64_addsubmul_post);
  1102. }
  1103. /*
  1104. * Returns the result of multiplying the floating-point values `a' and
  1105. * `b' then adding 'c', with no intermediate rounding step after the
  1106. * multiplication. The operation is performed according to the
  1107. * IEC/IEEE Standard for Binary Floating-Point Arithmetic 754-2008.
  1108. * The flags argument allows the caller to select negation of the
  1109. * addend, the intermediate product, or the final result. (The
  1110. * difference between this and having the caller do a separate
  1111. * negation is that negating externally will flip the sign bit on
  1112. * NaNs.)
  1113. */
  1114. static FloatParts muladd_floats(FloatParts a, FloatParts b, FloatParts c,
  1115. int flags, float_status *s)
  1116. {
  1117. bool inf_zero = ((1 << a.cls) | (1 << b.cls)) ==
  1118. ((1 << float_class_inf) | (1 << float_class_zero));
  1119. bool p_sign;
  1120. bool sign_flip = flags & float_muladd_negate_result;
  1121. FloatClass p_class;
  1122. uint64_t hi, lo;
  1123. int p_exp;
  1124. /* It is implementation-defined whether the cases of (0,inf,qnan)
  1125. * and (inf,0,qnan) raise InvalidOperation or not (and what QNaN
  1126. * they return if they do), so we have to hand this information
  1127. * off to the target-specific pick-a-NaN routine.
  1128. */
  1129. if (is_nan(a.cls) || is_nan(b.cls) || is_nan(c.cls)) {
  1130. return pick_nan_muladd(a, b, c, inf_zero, s);
  1131. }
  1132. if (inf_zero) {
  1133. s->float_exception_flags |= float_flag_invalid;
  1134. return parts_default_nan(s);
  1135. }
  1136. if (flags & float_muladd_negate_c) {
  1137. c.sign ^= 1;
  1138. }
  1139. p_sign = a.sign ^ b.sign;
  1140. if (flags & float_muladd_negate_product) {
  1141. p_sign ^= 1;
  1142. }
  1143. if (a.cls == float_class_inf || b.cls == float_class_inf) {
  1144. p_class = float_class_inf;
  1145. } else if (a.cls == float_class_zero || b.cls == float_class_zero) {
  1146. p_class = float_class_zero;
  1147. } else {
  1148. p_class = float_class_normal;
  1149. }
  1150. if (c.cls == float_class_inf) {
  1151. if (p_class == float_class_inf && p_sign != c.sign) {
  1152. s->float_exception_flags |= float_flag_invalid;
  1153. return parts_default_nan(s);
  1154. } else {
  1155. a.cls = float_class_inf;
  1156. a.sign = c.sign ^ sign_flip;
  1157. return a;
  1158. }
  1159. }
  1160. if (p_class == float_class_inf) {
  1161. a.cls = float_class_inf;
  1162. a.sign = p_sign ^ sign_flip;
  1163. return a;
  1164. }
  1165. if (p_class == float_class_zero) {
  1166. if (c.cls == float_class_zero) {
  1167. if (p_sign != c.sign) {
  1168. p_sign = s->float_rounding_mode == float_round_down;
  1169. }
  1170. c.sign = p_sign;
  1171. } else if (flags & float_muladd_halve_result) {
  1172. c.exp -= 1;
  1173. }
  1174. c.sign ^= sign_flip;
  1175. return c;
  1176. }
  1177. /* a & b should be normals now... */
  1178. assert(a.cls == float_class_normal &&
  1179. b.cls == float_class_normal);
  1180. p_exp = a.exp + b.exp;
  1181. /* Multiply of 2 62-bit numbers produces a (2*62) == 124-bit
  1182. * result.
  1183. */
  1184. mul64To128(a.frac, b.frac, &hi, &lo);
  1185. /* binary point now at bit 124 */
  1186. /* check for overflow */
  1187. if (hi & (1ULL << (DECOMPOSED_BINARY_POINT * 2 + 1 - 64))) {
  1188. shift128RightJamming(hi, lo, 1, &hi, &lo);
  1189. p_exp += 1;
  1190. }
  1191. /* + add/sub */
  1192. if (c.cls == float_class_zero) {
  1193. /* move binary point back to 62 */
  1194. shift128RightJamming(hi, lo, DECOMPOSED_BINARY_POINT, &hi, &lo);
  1195. } else {
  1196. int exp_diff = p_exp - c.exp;
  1197. if (p_sign == c.sign) {
  1198. /* Addition */
  1199. if (exp_diff <= 0) {
  1200. shift128RightJamming(hi, lo,
  1201. DECOMPOSED_BINARY_POINT - exp_diff,
  1202. &hi, &lo);
  1203. lo += c.frac;
  1204. p_exp = c.exp;
  1205. } else {
  1206. uint64_t c_hi, c_lo;
  1207. /* shift c to the same binary point as the product (124) */
  1208. c_hi = c.frac >> 2;
  1209. c_lo = 0;
  1210. shift128RightJamming(c_hi, c_lo,
  1211. exp_diff,
  1212. &c_hi, &c_lo);
  1213. add128(hi, lo, c_hi, c_lo, &hi, &lo);
  1214. /* move binary point back to 62 */
  1215. shift128RightJamming(hi, lo, DECOMPOSED_BINARY_POINT, &hi, &lo);
  1216. }
  1217. if (lo & DECOMPOSED_OVERFLOW_BIT) {
  1218. shift64RightJamming(lo, 1, &lo);
  1219. p_exp += 1;
  1220. }
  1221. } else {
  1222. /* Subtraction */
  1223. uint64_t c_hi, c_lo;
  1224. /* make C binary point match product at bit 124 */
  1225. c_hi = c.frac >> 2;
  1226. c_lo = 0;
  1227. if (exp_diff <= 0) {
  1228. shift128RightJamming(hi, lo, -exp_diff, &hi, &lo);
  1229. if (exp_diff == 0
  1230. &&
  1231. (hi > c_hi || (hi == c_hi && lo >= c_lo))) {
  1232. sub128(hi, lo, c_hi, c_lo, &hi, &lo);
  1233. } else {
  1234. sub128(c_hi, c_lo, hi, lo, &hi, &lo);
  1235. p_sign ^= 1;
  1236. p_exp = c.exp;
  1237. }
  1238. } else {
  1239. shift128RightJamming(c_hi, c_lo,
  1240. exp_diff,
  1241. &c_hi, &c_lo);
  1242. sub128(hi, lo, c_hi, c_lo, &hi, &lo);
  1243. }
  1244. if (hi == 0 && lo == 0) {
  1245. a.cls = float_class_zero;
  1246. a.sign = s->float_rounding_mode == float_round_down;
  1247. a.sign ^= sign_flip;
  1248. return a;
  1249. } else {
  1250. int shift;
  1251. if (hi != 0) {
  1252. shift = clz64(hi);
  1253. } else {
  1254. shift = clz64(lo) + 64;
  1255. }
  1256. /* Normalizing to a binary point of 124 is the
  1257. correct adjust for the exponent. However since we're
  1258. shifting, we might as well put the binary point back
  1259. at 62 where we really want it. Therefore shift as
  1260. if we're leaving 1 bit at the top of the word, but
  1261. adjust the exponent as if we're leaving 3 bits. */
  1262. shift -= 1;
  1263. if (shift >= 64) {
  1264. lo = lo << (shift - 64);
  1265. } else {
  1266. hi = (hi << shift) | (lo >> (64 - shift));
  1267. lo = hi | ((lo << shift) != 0);
  1268. }
  1269. p_exp -= shift - 2;
  1270. }
  1271. }
  1272. }
  1273. if (flags & float_muladd_halve_result) {
  1274. p_exp -= 1;
  1275. }
  1276. /* finally prepare our result */
  1277. a.cls = float_class_normal;
  1278. a.sign = p_sign ^ sign_flip;
  1279. a.exp = p_exp;
  1280. a.frac = lo;
  1281. return a;
  1282. }
  1283. float16 QEMU_FLATTEN float16_muladd(float16 a, float16 b, float16 c,
  1284. int flags, float_status *status)
  1285. {
  1286. FloatParts pa = float16_unpack_canonical(a, status);
  1287. FloatParts pb = float16_unpack_canonical(b, status);
  1288. FloatParts pc = float16_unpack_canonical(c, status);
  1289. FloatParts pr = muladd_floats(pa, pb, pc, flags, status);
  1290. return float16_round_pack_canonical(pr, status);
  1291. }
  1292. static float32 QEMU_SOFTFLOAT_ATTR
  1293. soft_f32_muladd(float32 a, float32 b, float32 c, int flags,
  1294. float_status *status)
  1295. {
  1296. FloatParts pa = float32_unpack_canonical(a, status);
  1297. FloatParts pb = float32_unpack_canonical(b, status);
  1298. FloatParts pc = float32_unpack_canonical(c, status);
  1299. FloatParts pr = muladd_floats(pa, pb, pc, flags, status);
  1300. return float32_round_pack_canonical(pr, status);
  1301. }
  1302. static float64 QEMU_SOFTFLOAT_ATTR
  1303. soft_f64_muladd(float64 a, float64 b, float64 c, int flags,
  1304. float_status *status)
  1305. {
  1306. FloatParts pa = float64_unpack_canonical(a, status);
  1307. FloatParts pb = float64_unpack_canonical(b, status);
  1308. FloatParts pc = float64_unpack_canonical(c, status);
  1309. FloatParts pr = muladd_floats(pa, pb, pc, flags, status);
  1310. return float64_round_pack_canonical(pr, status);
  1311. }
  1312. static bool force_soft_fma;
  1313. float32 QEMU_FLATTEN
  1314. float32_muladd(float32 xa, float32 xb, float32 xc, int flags, float_status *s)
  1315. {
  1316. union_float32 ua, ub, uc, ur;
  1317. ua.s = xa;
  1318. ub.s = xb;
  1319. uc.s = xc;
  1320. if (unlikely(!can_use_fpu(s))) {
  1321. goto soft;
  1322. }
  1323. if (unlikely(flags & float_muladd_halve_result)) {
  1324. goto soft;
  1325. }
  1326. float32_input_flush3(&ua.s, &ub.s, &uc.s, s);
  1327. if (unlikely(!f32_is_zon3(ua, ub, uc))) {
  1328. goto soft;
  1329. }
  1330. if (unlikely(force_soft_fma)) {
  1331. goto soft;
  1332. }
  1333. /*
  1334. * When (a || b) == 0, there's no need to check for under/over flow,
  1335. * since we know the addend is (normal || 0) and the product is 0.
  1336. */
  1337. if (float32_is_zero(ua.s) || float32_is_zero(ub.s)) {
  1338. union_float32 up;
  1339. bool prod_sign;
  1340. prod_sign = float32_is_neg(ua.s) ^ float32_is_neg(ub.s);
  1341. prod_sign ^= !!(flags & float_muladd_negate_product);
  1342. up.s = float32_set_sign(float32_zero, prod_sign);
  1343. if (flags & float_muladd_negate_c) {
  1344. uc.h = -uc.h;
  1345. }
  1346. ur.h = up.h + uc.h;
  1347. } else {
  1348. union_float32 ua_orig = ua;
  1349. union_float32 uc_orig = uc;
  1350. if (flags & float_muladd_negate_product) {
  1351. ua.h = -ua.h;
  1352. }
  1353. if (flags & float_muladd_negate_c) {
  1354. uc.h = -uc.h;
  1355. }
  1356. ur.h = fmaf(ua.h, ub.h, uc.h);
  1357. if (unlikely(f32_is_inf(ur))) {
  1358. s->float_exception_flags |= float_flag_overflow;
  1359. } else if (unlikely(fabsf(ur.h) <= FLT_MIN)) {
  1360. ua = ua_orig;
  1361. uc = uc_orig;
  1362. goto soft;
  1363. }
  1364. }
  1365. if (flags & float_muladd_negate_result) {
  1366. return float32_chs(ur.s);
  1367. }
  1368. return ur.s;
  1369. soft:
  1370. return soft_f32_muladd(ua.s, ub.s, uc.s, flags, s);
  1371. }
  1372. float64 QEMU_FLATTEN
  1373. float64_muladd(float64 xa, float64 xb, float64 xc, int flags, float_status *s)
  1374. {
  1375. union_float64 ua, ub, uc, ur;
  1376. ua.s = xa;
  1377. ub.s = xb;
  1378. uc.s = xc;
  1379. if (unlikely(!can_use_fpu(s))) {
  1380. goto soft;
  1381. }
  1382. if (unlikely(flags & float_muladd_halve_result)) {
  1383. goto soft;
  1384. }
  1385. float64_input_flush3(&ua.s, &ub.s, &uc.s, s);
  1386. if (unlikely(!f64_is_zon3(ua, ub, uc))) {
  1387. goto soft;
  1388. }
  1389. if (unlikely(force_soft_fma)) {
  1390. goto soft;
  1391. }
  1392. /*
  1393. * When (a || b) == 0, there's no need to check for under/over flow,
  1394. * since we know the addend is (normal || 0) and the product is 0.
  1395. */
  1396. if (float64_is_zero(ua.s) || float64_is_zero(ub.s)) {
  1397. union_float64 up;
  1398. bool prod_sign;
  1399. prod_sign = float64_is_neg(ua.s) ^ float64_is_neg(ub.s);
  1400. prod_sign ^= !!(flags & float_muladd_negate_product);
  1401. up.s = float64_set_sign(float64_zero, prod_sign);
  1402. if (flags & float_muladd_negate_c) {
  1403. uc.h = -uc.h;
  1404. }
  1405. ur.h = up.h + uc.h;
  1406. } else {
  1407. union_float64 ua_orig = ua;
  1408. union_float64 uc_orig = uc;
  1409. if (flags & float_muladd_negate_product) {
  1410. ua.h = -ua.h;
  1411. }
  1412. if (flags & float_muladd_negate_c) {
  1413. uc.h = -uc.h;
  1414. }
  1415. ur.h = fma(ua.h, ub.h, uc.h);
  1416. if (unlikely(f64_is_inf(ur))) {
  1417. s->float_exception_flags |= float_flag_overflow;
  1418. } else if (unlikely(fabs(ur.h) <= FLT_MIN)) {
  1419. ua = ua_orig;
  1420. uc = uc_orig;
  1421. goto soft;
  1422. }
  1423. }
  1424. if (flags & float_muladd_negate_result) {
  1425. return float64_chs(ur.s);
  1426. }
  1427. return ur.s;
  1428. soft:
  1429. return soft_f64_muladd(ua.s, ub.s, uc.s, flags, s);
  1430. }
  1431. /*
  1432. * Returns the result of dividing the floating-point value `a' by the
  1433. * corresponding value `b'. The operation is performed according to
  1434. * the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  1435. */
  1436. static FloatParts div_floats(FloatParts a, FloatParts b, float_status *s)
  1437. {
  1438. bool sign = a.sign ^ b.sign;
  1439. if (a.cls == float_class_normal && b.cls == float_class_normal) {
  1440. uint64_t n0, n1, q, r;
  1441. int exp = a.exp - b.exp;
  1442. /*
  1443. * We want a 2*N / N-bit division to produce exactly an N-bit
  1444. * result, so that we do not lose any precision and so that we
  1445. * do not have to renormalize afterward. If A.frac < B.frac,
  1446. * then division would produce an (N-1)-bit result; shift A left
  1447. * by one to produce the an N-bit result, and decrement the
  1448. * exponent to match.
  1449. *
  1450. * The udiv_qrnnd algorithm that we're using requires normalization,
  1451. * i.e. the msb of the denominator must be set. Since we know that
  1452. * DECOMPOSED_BINARY_POINT is msb-1, the inputs must be shifted left
  1453. * by one (more), and the remainder must be shifted right by one.
  1454. */
  1455. if (a.frac < b.frac) {
  1456. exp -= 1;
  1457. shift128Left(0, a.frac, DECOMPOSED_BINARY_POINT + 2, &n1, &n0);
  1458. } else {
  1459. shift128Left(0, a.frac, DECOMPOSED_BINARY_POINT + 1, &n1, &n0);
  1460. }
  1461. q = udiv_qrnnd(&r, n1, n0, b.frac << 1);
  1462. /*
  1463. * Set lsb if there is a remainder, to set inexact.
  1464. * As mentioned above, to find the actual value of the remainder we
  1465. * would need to shift right, but (1) we are only concerned about
  1466. * non-zero-ness, and (2) the remainder will always be even because
  1467. * both inputs to the division primitive are even.
  1468. */
  1469. a.frac = q | (r != 0);
  1470. a.sign = sign;
  1471. a.exp = exp;
  1472. return a;
  1473. }
  1474. /* handle all the NaN cases */
  1475. if (is_nan(a.cls) || is_nan(b.cls)) {
  1476. return pick_nan(a, b, s);
  1477. }
  1478. /* 0/0 or Inf/Inf */
  1479. if (a.cls == b.cls
  1480. &&
  1481. (a.cls == float_class_inf || a.cls == float_class_zero)) {
  1482. s->float_exception_flags |= float_flag_invalid;
  1483. return parts_default_nan(s);
  1484. }
  1485. /* Inf / x or 0 / x */
  1486. if (a.cls == float_class_inf || a.cls == float_class_zero) {
  1487. a.sign = sign;
  1488. return a;
  1489. }
  1490. /* Div 0 => Inf */
  1491. if (b.cls == float_class_zero) {
  1492. s->float_exception_flags |= float_flag_divbyzero;
  1493. a.cls = float_class_inf;
  1494. a.sign = sign;
  1495. return a;
  1496. }
  1497. /* Div by Inf */
  1498. if (b.cls == float_class_inf) {
  1499. a.cls = float_class_zero;
  1500. a.sign = sign;
  1501. return a;
  1502. }
  1503. g_assert_not_reached();
  1504. }
  1505. float16 float16_div(float16 a, float16 b, float_status *status)
  1506. {
  1507. FloatParts pa = float16_unpack_canonical(a, status);
  1508. FloatParts pb = float16_unpack_canonical(b, status);
  1509. FloatParts pr = div_floats(pa, pb, status);
  1510. return float16_round_pack_canonical(pr, status);
  1511. }
  1512. static float32 QEMU_SOFTFLOAT_ATTR
  1513. soft_f32_div(float32 a, float32 b, float_status *status)
  1514. {
  1515. FloatParts pa = float32_unpack_canonical(a, status);
  1516. FloatParts pb = float32_unpack_canonical(b, status);
  1517. FloatParts pr = div_floats(pa, pb, status);
  1518. return float32_round_pack_canonical(pr, status);
  1519. }
  1520. static float64 QEMU_SOFTFLOAT_ATTR
  1521. soft_f64_div(float64 a, float64 b, float_status *status)
  1522. {
  1523. FloatParts pa = float64_unpack_canonical(a, status);
  1524. FloatParts pb = float64_unpack_canonical(b, status);
  1525. FloatParts pr = div_floats(pa, pb, status);
  1526. return float64_round_pack_canonical(pr, status);
  1527. }
  1528. static float hard_f32_div(float a, float b)
  1529. {
  1530. return a / b;
  1531. }
  1532. static double hard_f64_div(double a, double b)
  1533. {
  1534. return a / b;
  1535. }
  1536. static bool f32_div_pre(union_float32 a, union_float32 b)
  1537. {
  1538. if (QEMU_HARDFLOAT_2F32_USE_FP) {
  1539. return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
  1540. fpclassify(b.h) == FP_NORMAL;
  1541. }
  1542. return float32_is_zero_or_normal(a.s) && float32_is_normal(b.s);
  1543. }
  1544. static bool f64_div_pre(union_float64 a, union_float64 b)
  1545. {
  1546. if (QEMU_HARDFLOAT_2F64_USE_FP) {
  1547. return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
  1548. fpclassify(b.h) == FP_NORMAL;
  1549. }
  1550. return float64_is_zero_or_normal(a.s) && float64_is_normal(b.s);
  1551. }
  1552. static bool f32_div_post(union_float32 a, union_float32 b)
  1553. {
  1554. if (QEMU_HARDFLOAT_2F32_USE_FP) {
  1555. return fpclassify(a.h) != FP_ZERO;
  1556. }
  1557. return !float32_is_zero(a.s);
  1558. }
  1559. static bool f64_div_post(union_float64 a, union_float64 b)
  1560. {
  1561. if (QEMU_HARDFLOAT_2F64_USE_FP) {
  1562. return fpclassify(a.h) != FP_ZERO;
  1563. }
  1564. return !float64_is_zero(a.s);
  1565. }
  1566. float32 QEMU_FLATTEN
  1567. float32_div(float32 a, float32 b, float_status *s)
  1568. {
  1569. return float32_gen2(a, b, s, hard_f32_div, soft_f32_div,
  1570. f32_div_pre, f32_div_post);
  1571. }
  1572. float64 QEMU_FLATTEN
  1573. float64_div(float64 a, float64 b, float_status *s)
  1574. {
  1575. return float64_gen2(a, b, s, hard_f64_div, soft_f64_div,
  1576. f64_div_pre, f64_div_post);
  1577. }
  1578. /*
  1579. * Float to Float conversions
  1580. *
  1581. * Returns the result of converting one float format to another. The
  1582. * conversion is performed according to the IEC/IEEE Standard for
  1583. * Binary Floating-Point Arithmetic.
  1584. *
  1585. * The float_to_float helper only needs to take care of raising
  1586. * invalid exceptions and handling the conversion on NaNs.
  1587. */
  1588. static FloatParts float_to_float(FloatParts a, const FloatFmt *dstf,
  1589. float_status *s)
  1590. {
  1591. if (dstf->arm_althp) {
  1592. switch (a.cls) {
  1593. case float_class_qnan:
  1594. case float_class_snan:
  1595. /* There is no NaN in the destination format. Raise Invalid
  1596. * and return a zero with the sign of the input NaN.
  1597. */
  1598. s->float_exception_flags |= float_flag_invalid;
  1599. a.cls = float_class_zero;
  1600. a.frac = 0;
  1601. a.exp = 0;
  1602. break;
  1603. case float_class_inf:
  1604. /* There is no Inf in the destination format. Raise Invalid
  1605. * and return the maximum normal with the correct sign.
  1606. */
  1607. s->float_exception_flags |= float_flag_invalid;
  1608. a.cls = float_class_normal;
  1609. a.exp = dstf->exp_max;
  1610. a.frac = ((1ull << dstf->frac_size) - 1) << dstf->frac_shift;
  1611. break;
  1612. default:
  1613. break;
  1614. }
  1615. } else if (is_nan(a.cls)) {
  1616. if (is_snan(a.cls)) {
  1617. s->float_exception_flags |= float_flag_invalid;
  1618. a = parts_silence_nan(a, s);
  1619. }
  1620. if (s->default_nan_mode) {
  1621. return parts_default_nan(s);
  1622. }
  1623. }
  1624. return a;
  1625. }
  1626. float32 float16_to_float32(float16 a, bool ieee, float_status *s)
  1627. {
  1628. const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
  1629. FloatParts p = float16a_unpack_canonical(a, s, fmt16);
  1630. FloatParts pr = float_to_float(p, &float32_params, s);
  1631. return float32_round_pack_canonical(pr, s);
  1632. }
  1633. float64 float16_to_float64(float16 a, bool ieee, float_status *s)
  1634. {
  1635. const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
  1636. FloatParts p = float16a_unpack_canonical(a, s, fmt16);
  1637. FloatParts pr = float_to_float(p, &float64_params, s);
  1638. return float64_round_pack_canonical(pr, s);
  1639. }
  1640. float16 float32_to_float16(float32 a, bool ieee, float_status *s)
  1641. {
  1642. const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
  1643. FloatParts p = float32_unpack_canonical(a, s);
  1644. FloatParts pr = float_to_float(p, fmt16, s);
  1645. return float16a_round_pack_canonical(pr, s, fmt16);
  1646. }
  1647. static float64 QEMU_SOFTFLOAT_ATTR
  1648. soft_float32_to_float64(float32 a, float_status *s)
  1649. {
  1650. FloatParts p = float32_unpack_canonical(a, s);
  1651. FloatParts pr = float_to_float(p, &float64_params, s);
  1652. return float64_round_pack_canonical(pr, s);
  1653. }
  1654. float64 float32_to_float64(float32 a, float_status *s)
  1655. {
  1656. if (likely(float32_is_normal(a))) {
  1657. /* Widening conversion can never produce inexact results. */
  1658. union_float32 uf;
  1659. union_float64 ud;
  1660. uf.s = a;
  1661. ud.h = uf.h;
  1662. return ud.s;
  1663. } else if (float32_is_zero(a)) {
  1664. return float64_set_sign(float64_zero, float32_is_neg(a));
  1665. } else {
  1666. return soft_float32_to_float64(a, s);
  1667. }
  1668. }
  1669. float16 float64_to_float16(float64 a, bool ieee, float_status *s)
  1670. {
  1671. const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
  1672. FloatParts p = float64_unpack_canonical(a, s);
  1673. FloatParts pr = float_to_float(p, fmt16, s);
  1674. return float16a_round_pack_canonical(pr, s, fmt16);
  1675. }
  1676. float32 float64_to_float32(float64 a, float_status *s)
  1677. {
  1678. FloatParts p = float64_unpack_canonical(a, s);
  1679. FloatParts pr = float_to_float(p, &float32_params, s);
  1680. return float32_round_pack_canonical(pr, s);
  1681. }
  1682. /*
  1683. * Rounds the floating-point value `a' to an integer, and returns the
  1684. * result as a floating-point value. The operation is performed
  1685. * according to the IEC/IEEE Standard for Binary Floating-Point
  1686. * Arithmetic.
  1687. */
  1688. static FloatParts round_to_int(FloatParts a, FloatRoundMode rmode,
  1689. int scale, float_status *s)
  1690. {
  1691. switch (a.cls) {
  1692. case float_class_qnan:
  1693. case float_class_snan:
  1694. return return_nan(a, s);
  1695. case float_class_zero:
  1696. case float_class_inf:
  1697. /* already "integral" */
  1698. break;
  1699. case float_class_normal:
  1700. scale = MIN(MAX(scale, -0x10000), 0x10000);
  1701. a.exp += scale;
  1702. if (a.exp >= DECOMPOSED_BINARY_POINT) {
  1703. /* already integral */
  1704. break;
  1705. }
  1706. if (a.exp < 0) {
  1707. bool one;
  1708. /* all fractional */
  1709. s->float_exception_flags |= float_flag_inexact;
  1710. switch (rmode) {
  1711. case float_round_nearest_even:
  1712. one = a.exp == -1 && a.frac > DECOMPOSED_IMPLICIT_BIT;
  1713. break;
  1714. case float_round_ties_away:
  1715. one = a.exp == -1 && a.frac >= DECOMPOSED_IMPLICIT_BIT;
  1716. break;
  1717. case float_round_to_zero:
  1718. one = false;
  1719. break;
  1720. case float_round_up:
  1721. one = !a.sign;
  1722. break;
  1723. case float_round_down:
  1724. one = a.sign;
  1725. break;
  1726. case float_round_to_odd:
  1727. one = true;
  1728. break;
  1729. default:
  1730. g_assert_not_reached();
  1731. }
  1732. if (one) {
  1733. a.frac = DECOMPOSED_IMPLICIT_BIT;
  1734. a.exp = 0;
  1735. } else {
  1736. a.cls = float_class_zero;
  1737. }
  1738. } else {
  1739. uint64_t frac_lsb = DECOMPOSED_IMPLICIT_BIT >> a.exp;
  1740. uint64_t frac_lsbm1 = frac_lsb >> 1;
  1741. uint64_t rnd_even_mask = (frac_lsb - 1) | frac_lsb;
  1742. uint64_t rnd_mask = rnd_even_mask >> 1;
  1743. uint64_t inc;
  1744. switch (rmode) {
  1745. case float_round_nearest_even:
  1746. inc = ((a.frac & rnd_even_mask) != frac_lsbm1 ? frac_lsbm1 : 0);
  1747. break;
  1748. case float_round_ties_away:
  1749. inc = frac_lsbm1;
  1750. break;
  1751. case float_round_to_zero:
  1752. inc = 0;
  1753. break;
  1754. case float_round_up:
  1755. inc = a.sign ? 0 : rnd_mask;
  1756. break;
  1757. case float_round_down:
  1758. inc = a.sign ? rnd_mask : 0;
  1759. break;
  1760. case float_round_to_odd:
  1761. inc = a.frac & frac_lsb ? 0 : rnd_mask;
  1762. break;
  1763. default:
  1764. g_assert_not_reached();
  1765. }
  1766. if (a.frac & rnd_mask) {
  1767. s->float_exception_flags |= float_flag_inexact;
  1768. a.frac += inc;
  1769. a.frac &= ~rnd_mask;
  1770. if (a.frac & DECOMPOSED_OVERFLOW_BIT) {
  1771. a.frac >>= 1;
  1772. a.exp++;
  1773. }
  1774. }
  1775. }
  1776. break;
  1777. default:
  1778. g_assert_not_reached();
  1779. }
  1780. return a;
  1781. }
  1782. float16 float16_round_to_int(float16 a, float_status *s)
  1783. {
  1784. FloatParts pa = float16_unpack_canonical(a, s);
  1785. FloatParts pr = round_to_int(pa, s->float_rounding_mode, 0, s);
  1786. return float16_round_pack_canonical(pr, s);
  1787. }
  1788. float32 float32_round_to_int(float32 a, float_status *s)
  1789. {
  1790. FloatParts pa = float32_unpack_canonical(a, s);
  1791. FloatParts pr = round_to_int(pa, s->float_rounding_mode, 0, s);
  1792. return float32_round_pack_canonical(pr, s);
  1793. }
  1794. float64 float64_round_to_int(float64 a, float_status *s)
  1795. {
  1796. FloatParts pa = float64_unpack_canonical(a, s);
  1797. FloatParts pr = round_to_int(pa, s->float_rounding_mode, 0, s);
  1798. return float64_round_pack_canonical(pr, s);
  1799. }
  1800. /*
  1801. * Returns the result of converting the floating-point value `a' to
  1802. * the two's complement integer format. The conversion is performed
  1803. * according to the IEC/IEEE Standard for Binary Floating-Point
  1804. * Arithmetic---which means in particular that the conversion is
  1805. * rounded according to the current rounding mode. If `a' is a NaN,
  1806. * the largest positive integer is returned. Otherwise, if the
  1807. * conversion overflows, the largest integer with the same sign as `a'
  1808. * is returned.
  1809. */
  1810. static int64_t round_to_int_and_pack(FloatParts in, FloatRoundMode rmode,
  1811. int scale, int64_t min, int64_t max,
  1812. float_status *s)
  1813. {
  1814. uint64_t r;
  1815. int orig_flags = get_float_exception_flags(s);
  1816. FloatParts p = round_to_int(in, rmode, scale, s);
  1817. switch (p.cls) {
  1818. case float_class_snan:
  1819. case float_class_qnan:
  1820. s->float_exception_flags = orig_flags | float_flag_invalid;
  1821. return max;
  1822. case float_class_inf:
  1823. s->float_exception_flags = orig_flags | float_flag_invalid;
  1824. return p.sign ? min : max;
  1825. case float_class_zero:
  1826. return 0;
  1827. case float_class_normal:
  1828. if (p.exp < DECOMPOSED_BINARY_POINT) {
  1829. r = p.frac >> (DECOMPOSED_BINARY_POINT - p.exp);
  1830. } else if (p.exp - DECOMPOSED_BINARY_POINT < 2) {
  1831. r = p.frac << (p.exp - DECOMPOSED_BINARY_POINT);
  1832. } else {
  1833. r = UINT64_MAX;
  1834. }
  1835. if (p.sign) {
  1836. if (r <= -(uint64_t) min) {
  1837. return -r;
  1838. } else {
  1839. s->float_exception_flags = orig_flags | float_flag_invalid;
  1840. return min;
  1841. }
  1842. } else {
  1843. if (r <= max) {
  1844. return r;
  1845. } else {
  1846. s->float_exception_flags = orig_flags | float_flag_invalid;
  1847. return max;
  1848. }
  1849. }
  1850. default:
  1851. g_assert_not_reached();
  1852. }
  1853. }
  1854. int16_t float16_to_int16_scalbn(float16 a, FloatRoundMode rmode, int scale,
  1855. float_status *s)
  1856. {
  1857. return round_to_int_and_pack(float16_unpack_canonical(a, s),
  1858. rmode, scale, INT16_MIN, INT16_MAX, s);
  1859. }
  1860. int32_t float16_to_int32_scalbn(float16 a, FloatRoundMode rmode, int scale,
  1861. float_status *s)
  1862. {
  1863. return round_to_int_and_pack(float16_unpack_canonical(a, s),
  1864. rmode, scale, INT32_MIN, INT32_MAX, s);
  1865. }
  1866. int64_t float16_to_int64_scalbn(float16 a, FloatRoundMode rmode, int scale,
  1867. float_status *s)
  1868. {
  1869. return round_to_int_and_pack(float16_unpack_canonical(a, s),
  1870. rmode, scale, INT64_MIN, INT64_MAX, s);
  1871. }
  1872. int16_t float32_to_int16_scalbn(float32 a, FloatRoundMode rmode, int scale,
  1873. float_status *s)
  1874. {
  1875. return round_to_int_and_pack(float32_unpack_canonical(a, s),
  1876. rmode, scale, INT16_MIN, INT16_MAX, s);
  1877. }
  1878. int32_t float32_to_int32_scalbn(float32 a, FloatRoundMode rmode, int scale,
  1879. float_status *s)
  1880. {
  1881. return round_to_int_and_pack(float32_unpack_canonical(a, s),
  1882. rmode, scale, INT32_MIN, INT32_MAX, s);
  1883. }
  1884. int64_t float32_to_int64_scalbn(float32 a, FloatRoundMode rmode, int scale,
  1885. float_status *s)
  1886. {
  1887. return round_to_int_and_pack(float32_unpack_canonical(a, s),
  1888. rmode, scale, INT64_MIN, INT64_MAX, s);
  1889. }
  1890. int16_t float64_to_int16_scalbn(float64 a, FloatRoundMode rmode, int scale,
  1891. float_status *s)
  1892. {
  1893. return round_to_int_and_pack(float64_unpack_canonical(a, s),
  1894. rmode, scale, INT16_MIN, INT16_MAX, s);
  1895. }
  1896. int32_t float64_to_int32_scalbn(float64 a, FloatRoundMode rmode, int scale,
  1897. float_status *s)
  1898. {
  1899. return round_to_int_and_pack(float64_unpack_canonical(a, s),
  1900. rmode, scale, INT32_MIN, INT32_MAX, s);
  1901. }
  1902. int64_t float64_to_int64_scalbn(float64 a, FloatRoundMode rmode, int scale,
  1903. float_status *s)
  1904. {
  1905. return round_to_int_and_pack(float64_unpack_canonical(a, s),
  1906. rmode, scale, INT64_MIN, INT64_MAX, s);
  1907. }
  1908. int16_t float16_to_int16(float16 a, float_status *s)
  1909. {
  1910. return float16_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
  1911. }
  1912. int32_t float16_to_int32(float16 a, float_status *s)
  1913. {
  1914. return float16_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
  1915. }
  1916. int64_t float16_to_int64(float16 a, float_status *s)
  1917. {
  1918. return float16_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
  1919. }
  1920. int16_t float32_to_int16(float32 a, float_status *s)
  1921. {
  1922. return float32_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
  1923. }
  1924. int32_t float32_to_int32(float32 a, float_status *s)
  1925. {
  1926. return float32_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
  1927. }
  1928. int64_t float32_to_int64(float32 a, float_status *s)
  1929. {
  1930. return float32_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
  1931. }
  1932. int16_t float64_to_int16(float64 a, float_status *s)
  1933. {
  1934. return float64_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
  1935. }
  1936. int32_t float64_to_int32(float64 a, float_status *s)
  1937. {
  1938. return float64_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
  1939. }
  1940. int64_t float64_to_int64(float64 a, float_status *s)
  1941. {
  1942. return float64_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
  1943. }
  1944. int16_t float16_to_int16_round_to_zero(float16 a, float_status *s)
  1945. {
  1946. return float16_to_int16_scalbn(a, float_round_to_zero, 0, s);
  1947. }
  1948. int32_t float16_to_int32_round_to_zero(float16 a, float_status *s)
  1949. {
  1950. return float16_to_int32_scalbn(a, float_round_to_zero, 0, s);
  1951. }
  1952. int64_t float16_to_int64_round_to_zero(float16 a, float_status *s)
  1953. {
  1954. return float16_to_int64_scalbn(a, float_round_to_zero, 0, s);
  1955. }
  1956. int16_t float32_to_int16_round_to_zero(float32 a, float_status *s)
  1957. {
  1958. return float32_to_int16_scalbn(a, float_round_to_zero, 0, s);
  1959. }
  1960. int32_t float32_to_int32_round_to_zero(float32 a, float_status *s)
  1961. {
  1962. return float32_to_int32_scalbn(a, float_round_to_zero, 0, s);
  1963. }
  1964. int64_t float32_to_int64_round_to_zero(float32 a, float_status *s)
  1965. {
  1966. return float32_to_int64_scalbn(a, float_round_to_zero, 0, s);
  1967. }
  1968. int16_t float64_to_int16_round_to_zero(float64 a, float_status *s)
  1969. {
  1970. return float64_to_int16_scalbn(a, float_round_to_zero, 0, s);
  1971. }
  1972. int32_t float64_to_int32_round_to_zero(float64 a, float_status *s)
  1973. {
  1974. return float64_to_int32_scalbn(a, float_round_to_zero, 0, s);
  1975. }
  1976. int64_t float64_to_int64_round_to_zero(float64 a, float_status *s)
  1977. {
  1978. return float64_to_int64_scalbn(a, float_round_to_zero, 0, s);
  1979. }
  1980. /*
  1981. * Returns the result of converting the floating-point value `a' to
  1982. * the unsigned integer format. The conversion is performed according
  1983. * to the IEC/IEEE Standard for Binary Floating-Point
  1984. * Arithmetic---which means in particular that the conversion is
  1985. * rounded according to the current rounding mode. If `a' is a NaN,
  1986. * the largest unsigned integer is returned. Otherwise, if the
  1987. * conversion overflows, the largest unsigned integer is returned. If
  1988. * the 'a' is negative, the result is rounded and zero is returned;
  1989. * values that do not round to zero will raise the inexact exception
  1990. * flag.
  1991. */
  1992. static uint64_t round_to_uint_and_pack(FloatParts in, FloatRoundMode rmode,
  1993. int scale, uint64_t max,
  1994. float_status *s)
  1995. {
  1996. int orig_flags = get_float_exception_flags(s);
  1997. FloatParts p = round_to_int(in, rmode, scale, s);
  1998. uint64_t r;
  1999. switch (p.cls) {
  2000. case float_class_snan:
  2001. case float_class_qnan:
  2002. s->float_exception_flags = orig_flags | float_flag_invalid;
  2003. return max;
  2004. case float_class_inf:
  2005. s->float_exception_flags = orig_flags | float_flag_invalid;
  2006. return p.sign ? 0 : max;
  2007. case float_class_zero:
  2008. return 0;
  2009. case float_class_normal:
  2010. if (p.sign) {
  2011. s->float_exception_flags = orig_flags | float_flag_invalid;
  2012. return 0;
  2013. }
  2014. if (p.exp < DECOMPOSED_BINARY_POINT) {
  2015. r = p.frac >> (DECOMPOSED_BINARY_POINT - p.exp);
  2016. } else if (p.exp - DECOMPOSED_BINARY_POINT < 2) {
  2017. r = p.frac << (p.exp - DECOMPOSED_BINARY_POINT);
  2018. } else {
  2019. s->float_exception_flags = orig_flags | float_flag_invalid;
  2020. return max;
  2021. }
  2022. /* For uint64 this will never trip, but if p.exp is too large
  2023. * to shift a decomposed fraction we shall have exited via the
  2024. * 3rd leg above.
  2025. */
  2026. if (r > max) {
  2027. s->float_exception_flags = orig_flags | float_flag_invalid;
  2028. return max;
  2029. }
  2030. return r;
  2031. default:
  2032. g_assert_not_reached();
  2033. }
  2034. }
  2035. uint16_t float16_to_uint16_scalbn(float16 a, FloatRoundMode rmode, int scale,
  2036. float_status *s)
  2037. {
  2038. return round_to_uint_and_pack(float16_unpack_canonical(a, s),
  2039. rmode, scale, UINT16_MAX, s);
  2040. }
  2041. uint32_t float16_to_uint32_scalbn(float16 a, FloatRoundMode rmode, int scale,
  2042. float_status *s)
  2043. {
  2044. return round_to_uint_and_pack(float16_unpack_canonical(a, s),
  2045. rmode, scale, UINT32_MAX, s);
  2046. }
  2047. uint64_t float16_to_uint64_scalbn(float16 a, FloatRoundMode rmode, int scale,
  2048. float_status *s)
  2049. {
  2050. return round_to_uint_and_pack(float16_unpack_canonical(a, s),
  2051. rmode, scale, UINT64_MAX, s);
  2052. }
  2053. uint16_t float32_to_uint16_scalbn(float32 a, FloatRoundMode rmode, int scale,
  2054. float_status *s)
  2055. {
  2056. return round_to_uint_and_pack(float32_unpack_canonical(a, s),
  2057. rmode, scale, UINT16_MAX, s);
  2058. }
  2059. uint32_t float32_to_uint32_scalbn(float32 a, FloatRoundMode rmode, int scale,
  2060. float_status *s)
  2061. {
  2062. return round_to_uint_and_pack(float32_unpack_canonical(a, s),
  2063. rmode, scale, UINT32_MAX, s);
  2064. }
  2065. uint64_t float32_to_uint64_scalbn(float32 a, FloatRoundMode rmode, int scale,
  2066. float_status *s)
  2067. {
  2068. return round_to_uint_and_pack(float32_unpack_canonical(a, s),
  2069. rmode, scale, UINT64_MAX, s);
  2070. }
  2071. uint16_t float64_to_uint16_scalbn(float64 a, FloatRoundMode rmode, int scale,
  2072. float_status *s)
  2073. {
  2074. return round_to_uint_and_pack(float64_unpack_canonical(a, s),
  2075. rmode, scale, UINT16_MAX, s);
  2076. }
  2077. uint32_t float64_to_uint32_scalbn(float64 a, FloatRoundMode rmode, int scale,
  2078. float_status *s)
  2079. {
  2080. return round_to_uint_and_pack(float64_unpack_canonical(a, s),
  2081. rmode, scale, UINT32_MAX, s);
  2082. }
  2083. uint64_t float64_to_uint64_scalbn(float64 a, FloatRoundMode rmode, int scale,
  2084. float_status *s)
  2085. {
  2086. return round_to_uint_and_pack(float64_unpack_canonical(a, s),
  2087. rmode, scale, UINT64_MAX, s);
  2088. }
  2089. uint16_t float16_to_uint16(float16 a, float_status *s)
  2090. {
  2091. return float16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
  2092. }
  2093. uint32_t float16_to_uint32(float16 a, float_status *s)
  2094. {
  2095. return float16_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
  2096. }
  2097. uint64_t float16_to_uint64(float16 a, float_status *s)
  2098. {
  2099. return float16_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
  2100. }
  2101. uint16_t float32_to_uint16(float32 a, float_status *s)
  2102. {
  2103. return float32_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
  2104. }
  2105. uint32_t float32_to_uint32(float32 a, float_status *s)
  2106. {
  2107. return float32_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
  2108. }
  2109. uint64_t float32_to_uint64(float32 a, float_status *s)
  2110. {
  2111. return float32_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
  2112. }
  2113. uint16_t float64_to_uint16(float64 a, float_status *s)
  2114. {
  2115. return float64_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
  2116. }
  2117. uint32_t float64_to_uint32(float64 a, float_status *s)
  2118. {
  2119. return float64_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
  2120. }
  2121. uint64_t float64_to_uint64(float64 a, float_status *s)
  2122. {
  2123. return float64_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
  2124. }
  2125. uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *s)
  2126. {
  2127. return float16_to_uint16_scalbn(a, float_round_to_zero, 0, s);
  2128. }
  2129. uint32_t float16_to_uint32_round_to_zero(float16 a, float_status *s)
  2130. {
  2131. return float16_to_uint32_scalbn(a, float_round_to_zero, 0, s);
  2132. }
  2133. uint64_t float16_to_uint64_round_to_zero(float16 a, float_status *s)
  2134. {
  2135. return float16_to_uint64_scalbn(a, float_round_to_zero, 0, s);
  2136. }
  2137. uint16_t float32_to_uint16_round_to_zero(float32 a, float_status *s)
  2138. {
  2139. return float32_to_uint16_scalbn(a, float_round_to_zero, 0, s);
  2140. }
  2141. uint32_t float32_to_uint32_round_to_zero(float32 a, float_status *s)
  2142. {
  2143. return float32_to_uint32_scalbn(a, float_round_to_zero, 0, s);
  2144. }
  2145. uint64_t float32_to_uint64_round_to_zero(float32 a, float_status *s)
  2146. {
  2147. return float32_to_uint64_scalbn(a, float_round_to_zero, 0, s);
  2148. }
  2149. uint16_t float64_to_uint16_round_to_zero(float64 a, float_status *s)
  2150. {
  2151. return float64_to_uint16_scalbn(a, float_round_to_zero, 0, s);
  2152. }
  2153. uint32_t float64_to_uint32_round_to_zero(float64 a, float_status *s)
  2154. {
  2155. return float64_to_uint32_scalbn(a, float_round_to_zero, 0, s);
  2156. }
  2157. uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *s)
  2158. {
  2159. return float64_to_uint64_scalbn(a, float_round_to_zero, 0, s);
  2160. }
  2161. /*
  2162. * Integer to float conversions
  2163. *
  2164. * Returns the result of converting the two's complement integer `a'
  2165. * to the floating-point format. The conversion is performed according
  2166. * to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  2167. */
  2168. static FloatParts int_to_float(int64_t a, int scale, float_status *status)
  2169. {
  2170. FloatParts r = { .sign = false };
  2171. if (a == 0) {
  2172. r.cls = float_class_zero;
  2173. } else {
  2174. uint64_t f = a;
  2175. int shift;
  2176. r.cls = float_class_normal;
  2177. if (a < 0) {
  2178. f = -f;
  2179. r.sign = true;
  2180. }
  2181. shift = clz64(f) - 1;
  2182. scale = MIN(MAX(scale, -0x10000), 0x10000);
  2183. r.exp = DECOMPOSED_BINARY_POINT - shift + scale;
  2184. r.frac = (shift < 0 ? DECOMPOSED_IMPLICIT_BIT : f << shift);
  2185. }
  2186. return r;
  2187. }
  2188. float16 int64_to_float16_scalbn(int64_t a, int scale, float_status *status)
  2189. {
  2190. FloatParts pa = int_to_float(a, scale, status);
  2191. return float16_round_pack_canonical(pa, status);
  2192. }
  2193. float16 int32_to_float16_scalbn(int32_t a, int scale, float_status *status)
  2194. {
  2195. return int64_to_float16_scalbn(a, scale, status);
  2196. }
  2197. float16 int16_to_float16_scalbn(int16_t a, int scale, float_status *status)
  2198. {
  2199. return int64_to_float16_scalbn(a, scale, status);
  2200. }
  2201. float16 int64_to_float16(int64_t a, float_status *status)
  2202. {
  2203. return int64_to_float16_scalbn(a, 0, status);
  2204. }
  2205. float16 int32_to_float16(int32_t a, float_status *status)
  2206. {
  2207. return int64_to_float16_scalbn(a, 0, status);
  2208. }
  2209. float16 int16_to_float16(int16_t a, float_status *status)
  2210. {
  2211. return int64_to_float16_scalbn(a, 0, status);
  2212. }
  2213. float32 int64_to_float32_scalbn(int64_t a, int scale, float_status *status)
  2214. {
  2215. FloatParts pa = int_to_float(a, scale, status);
  2216. return float32_round_pack_canonical(pa, status);
  2217. }
  2218. float32 int32_to_float32_scalbn(int32_t a, int scale, float_status *status)
  2219. {
  2220. return int64_to_float32_scalbn(a, scale, status);
  2221. }
  2222. float32 int16_to_float32_scalbn(int16_t a, int scale, float_status *status)
  2223. {
  2224. return int64_to_float32_scalbn(a, scale, status);
  2225. }
  2226. float32 int64_to_float32(int64_t a, float_status *status)
  2227. {
  2228. return int64_to_float32_scalbn(a, 0, status);
  2229. }
  2230. float32 int32_to_float32(int32_t a, float_status *status)
  2231. {
  2232. return int64_to_float32_scalbn(a, 0, status);
  2233. }
  2234. float32 int16_to_float32(int16_t a, float_status *status)
  2235. {
  2236. return int64_to_float32_scalbn(a, 0, status);
  2237. }
  2238. float64 int64_to_float64_scalbn(int64_t a, int scale, float_status *status)
  2239. {
  2240. FloatParts pa = int_to_float(a, scale, status);
  2241. return float64_round_pack_canonical(pa, status);
  2242. }
  2243. float64 int32_to_float64_scalbn(int32_t a, int scale, float_status *status)
  2244. {
  2245. return int64_to_float64_scalbn(a, scale, status);
  2246. }
  2247. float64 int16_to_float64_scalbn(int16_t a, int scale, float_status *status)
  2248. {
  2249. return int64_to_float64_scalbn(a, scale, status);
  2250. }
  2251. float64 int64_to_float64(int64_t a, float_status *status)
  2252. {
  2253. return int64_to_float64_scalbn(a, 0, status);
  2254. }
  2255. float64 int32_to_float64(int32_t a, float_status *status)
  2256. {
  2257. return int64_to_float64_scalbn(a, 0, status);
  2258. }
  2259. float64 int16_to_float64(int16_t a, float_status *status)
  2260. {
  2261. return int64_to_float64_scalbn(a, 0, status);
  2262. }
  2263. /*
  2264. * Unsigned Integer to float conversions
  2265. *
  2266. * Returns the result of converting the unsigned integer `a' to the
  2267. * floating-point format. The conversion is performed according to the
  2268. * IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  2269. */
  2270. static FloatParts uint_to_float(uint64_t a, int scale, float_status *status)
  2271. {
  2272. FloatParts r = { .sign = false };
  2273. if (a == 0) {
  2274. r.cls = float_class_zero;
  2275. } else {
  2276. scale = MIN(MAX(scale, -0x10000), 0x10000);
  2277. r.cls = float_class_normal;
  2278. if ((int64_t)a < 0) {
  2279. r.exp = DECOMPOSED_BINARY_POINT + 1 + scale;
  2280. shift64RightJamming(a, 1, &a);
  2281. r.frac = a;
  2282. } else {
  2283. int shift = clz64(a) - 1;
  2284. r.exp = DECOMPOSED_BINARY_POINT - shift + scale;
  2285. r.frac = a << shift;
  2286. }
  2287. }
  2288. return r;
  2289. }
  2290. float16 uint64_to_float16_scalbn(uint64_t a, int scale, float_status *status)
  2291. {
  2292. FloatParts pa = uint_to_float(a, scale, status);
  2293. return float16_round_pack_canonical(pa, status);
  2294. }
  2295. float16 uint32_to_float16_scalbn(uint32_t a, int scale, float_status *status)
  2296. {
  2297. return uint64_to_float16_scalbn(a, scale, status);
  2298. }
  2299. float16 uint16_to_float16_scalbn(uint16_t a, int scale, float_status *status)
  2300. {
  2301. return uint64_to_float16_scalbn(a, scale, status);
  2302. }
  2303. float16 uint64_to_float16(uint64_t a, float_status *status)
  2304. {
  2305. return uint64_to_float16_scalbn(a, 0, status);
  2306. }
  2307. float16 uint32_to_float16(uint32_t a, float_status *status)
  2308. {
  2309. return uint64_to_float16_scalbn(a, 0, status);
  2310. }
  2311. float16 uint16_to_float16(uint16_t a, float_status *status)
  2312. {
  2313. return uint64_to_float16_scalbn(a, 0, status);
  2314. }
  2315. float32 uint64_to_float32_scalbn(uint64_t a, int scale, float_status *status)
  2316. {
  2317. FloatParts pa = uint_to_float(a, scale, status);
  2318. return float32_round_pack_canonical(pa, status);
  2319. }
  2320. float32 uint32_to_float32_scalbn(uint32_t a, int scale, float_status *status)
  2321. {
  2322. return uint64_to_float32_scalbn(a, scale, status);
  2323. }
  2324. float32 uint16_to_float32_scalbn(uint16_t a, int scale, float_status *status)
  2325. {
  2326. return uint64_to_float32_scalbn(a, scale, status);
  2327. }
  2328. float32 uint64_to_float32(uint64_t a, float_status *status)
  2329. {
  2330. return uint64_to_float32_scalbn(a, 0, status);
  2331. }
  2332. float32 uint32_to_float32(uint32_t a, float_status *status)
  2333. {
  2334. return uint64_to_float32_scalbn(a, 0, status);
  2335. }
  2336. float32 uint16_to_float32(uint16_t a, float_status *status)
  2337. {
  2338. return uint64_to_float32_scalbn(a, 0, status);
  2339. }
  2340. float64 uint64_to_float64_scalbn(uint64_t a, int scale, float_status *status)
  2341. {
  2342. FloatParts pa = uint_to_float(a, scale, status);
  2343. return float64_round_pack_canonical(pa, status);
  2344. }
  2345. float64 uint32_to_float64_scalbn(uint32_t a, int scale, float_status *status)
  2346. {
  2347. return uint64_to_float64_scalbn(a, scale, status);
  2348. }
  2349. float64 uint16_to_float64_scalbn(uint16_t a, int scale, float_status *status)
  2350. {
  2351. return uint64_to_float64_scalbn(a, scale, status);
  2352. }
  2353. float64 uint64_to_float64(uint64_t a, float_status *status)
  2354. {
  2355. return uint64_to_float64_scalbn(a, 0, status);
  2356. }
  2357. float64 uint32_to_float64(uint32_t a, float_status *status)
  2358. {
  2359. return uint64_to_float64_scalbn(a, 0, status);
  2360. }
  2361. float64 uint16_to_float64(uint16_t a, float_status *status)
  2362. {
  2363. return uint64_to_float64_scalbn(a, 0, status);
  2364. }
  2365. /* Float Min/Max */
  2366. /* min() and max() functions. These can't be implemented as
  2367. * 'compare and pick one input' because that would mishandle
  2368. * NaNs and +0 vs -0.
  2369. *
  2370. * minnum() and maxnum() functions. These are similar to the min()
  2371. * and max() functions but if one of the arguments is a QNaN and
  2372. * the other is numerical then the numerical argument is returned.
  2373. * SNaNs will get quietened before being returned.
  2374. * minnum() and maxnum correspond to the IEEE 754-2008 minNum()
  2375. * and maxNum() operations. min() and max() are the typical min/max
  2376. * semantics provided by many CPUs which predate that specification.
  2377. *
  2378. * minnummag() and maxnummag() functions correspond to minNumMag()
  2379. * and minNumMag() from the IEEE-754 2008.
  2380. */
  2381. static FloatParts minmax_floats(FloatParts a, FloatParts b, bool ismin,
  2382. bool ieee, bool ismag, float_status *s)
  2383. {
  2384. if (unlikely(is_nan(a.cls) || is_nan(b.cls))) {
  2385. if (ieee) {
  2386. /* Takes two floating-point values `a' and `b', one of
  2387. * which is a NaN, and returns the appropriate NaN
  2388. * result. If either `a' or `b' is a signaling NaN,
  2389. * the invalid exception is raised.
  2390. */
  2391. if (is_snan(a.cls) || is_snan(b.cls)) {
  2392. return pick_nan(a, b, s);
  2393. } else if (is_nan(a.cls) && !is_nan(b.cls)) {
  2394. return b;
  2395. } else if (is_nan(b.cls) && !is_nan(a.cls)) {
  2396. return a;
  2397. }
  2398. }
  2399. return pick_nan(a, b, s);
  2400. } else {
  2401. int a_exp, b_exp;
  2402. switch (a.cls) {
  2403. case float_class_normal:
  2404. a_exp = a.exp;
  2405. break;
  2406. case float_class_inf:
  2407. a_exp = INT_MAX;
  2408. break;
  2409. case float_class_zero:
  2410. a_exp = INT_MIN;
  2411. break;
  2412. default:
  2413. g_assert_not_reached();
  2414. break;
  2415. }
  2416. switch (b.cls) {
  2417. case float_class_normal:
  2418. b_exp = b.exp;
  2419. break;
  2420. case float_class_inf:
  2421. b_exp = INT_MAX;
  2422. break;
  2423. case float_class_zero:
  2424. b_exp = INT_MIN;
  2425. break;
  2426. default:
  2427. g_assert_not_reached();
  2428. break;
  2429. }
  2430. if (ismag && (a_exp != b_exp || a.frac != b.frac)) {
  2431. bool a_less = a_exp < b_exp;
  2432. if (a_exp == b_exp) {
  2433. a_less = a.frac < b.frac;
  2434. }
  2435. return a_less ^ ismin ? b : a;
  2436. }
  2437. if (a.sign == b.sign) {
  2438. bool a_less = a_exp < b_exp;
  2439. if (a_exp == b_exp) {
  2440. a_less = a.frac < b.frac;
  2441. }
  2442. return a.sign ^ a_less ^ ismin ? b : a;
  2443. } else {
  2444. return a.sign ^ ismin ? b : a;
  2445. }
  2446. }
  2447. }
  2448. #define MINMAX(sz, name, ismin, isiee, ismag) \
  2449. float ## sz float ## sz ## _ ## name(float ## sz a, float ## sz b, \
  2450. float_status *s) \
  2451. { \
  2452. FloatParts pa = float ## sz ## _unpack_canonical(a, s); \
  2453. FloatParts pb = float ## sz ## _unpack_canonical(b, s); \
  2454. FloatParts pr = minmax_floats(pa, pb, ismin, isiee, ismag, s); \
  2455. \
  2456. return float ## sz ## _round_pack_canonical(pr, s); \
  2457. }
  2458. MINMAX(16, min, true, false, false)
  2459. MINMAX(16, minnum, true, true, false)
  2460. MINMAX(16, minnummag, true, true, true)
  2461. MINMAX(16, max, false, false, false)
  2462. MINMAX(16, maxnum, false, true, false)
  2463. MINMAX(16, maxnummag, false, true, true)
  2464. MINMAX(32, min, true, false, false)
  2465. MINMAX(32, minnum, true, true, false)
  2466. MINMAX(32, minnummag, true, true, true)
  2467. MINMAX(32, max, false, false, false)
  2468. MINMAX(32, maxnum, false, true, false)
  2469. MINMAX(32, maxnummag, false, true, true)
  2470. MINMAX(64, min, true, false, false)
  2471. MINMAX(64, minnum, true, true, false)
  2472. MINMAX(64, minnummag, true, true, true)
  2473. MINMAX(64, max, false, false, false)
  2474. MINMAX(64, maxnum, false, true, false)
  2475. MINMAX(64, maxnummag, false, true, true)
  2476. #undef MINMAX
  2477. /* Floating point compare */
  2478. static FloatRelation compare_floats(FloatParts a, FloatParts b, bool is_quiet,
  2479. float_status *s)
  2480. {
  2481. if (is_nan(a.cls) || is_nan(b.cls)) {
  2482. if (!is_quiet ||
  2483. a.cls == float_class_snan ||
  2484. b.cls == float_class_snan) {
  2485. s->float_exception_flags |= float_flag_invalid;
  2486. }
  2487. return float_relation_unordered;
  2488. }
  2489. if (a.cls == float_class_zero) {
  2490. if (b.cls == float_class_zero) {
  2491. return float_relation_equal;
  2492. }
  2493. return b.sign ? float_relation_greater : float_relation_less;
  2494. } else if (b.cls == float_class_zero) {
  2495. return a.sign ? float_relation_less : float_relation_greater;
  2496. }
  2497. /* The only really important thing about infinity is its sign. If
  2498. * both are infinities the sign marks the smallest of the two.
  2499. */
  2500. if (a.cls == float_class_inf) {
  2501. if ((b.cls == float_class_inf) && (a.sign == b.sign)) {
  2502. return float_relation_equal;
  2503. }
  2504. return a.sign ? float_relation_less : float_relation_greater;
  2505. } else if (b.cls == float_class_inf) {
  2506. return b.sign ? float_relation_greater : float_relation_less;
  2507. }
  2508. if (a.sign != b.sign) {
  2509. return a.sign ? float_relation_less : float_relation_greater;
  2510. }
  2511. if (a.exp == b.exp) {
  2512. if (a.frac == b.frac) {
  2513. return float_relation_equal;
  2514. }
  2515. if (a.sign) {
  2516. return a.frac > b.frac ?
  2517. float_relation_less : float_relation_greater;
  2518. } else {
  2519. return a.frac > b.frac ?
  2520. float_relation_greater : float_relation_less;
  2521. }
  2522. } else {
  2523. if (a.sign) {
  2524. return a.exp > b.exp ? float_relation_less : float_relation_greater;
  2525. } else {
  2526. return a.exp > b.exp ? float_relation_greater : float_relation_less;
  2527. }
  2528. }
  2529. }
  2530. #define COMPARE(name, attr, sz) \
  2531. static int attr \
  2532. name(float ## sz a, float ## sz b, bool is_quiet, float_status *s) \
  2533. { \
  2534. FloatParts pa = float ## sz ## _unpack_canonical(a, s); \
  2535. FloatParts pb = float ## sz ## _unpack_canonical(b, s); \
  2536. return compare_floats(pa, pb, is_quiet, s); \
  2537. }
  2538. COMPARE(soft_f16_compare, QEMU_FLATTEN, 16)
  2539. COMPARE(soft_f32_compare, QEMU_SOFTFLOAT_ATTR, 32)
  2540. COMPARE(soft_f64_compare, QEMU_SOFTFLOAT_ATTR, 64)
  2541. #undef COMPARE
  2542. FloatRelation float16_compare(float16 a, float16 b, float_status *s)
  2543. {
  2544. return soft_f16_compare(a, b, false, s);
  2545. }
  2546. FloatRelation float16_compare_quiet(float16 a, float16 b, float_status *s)
  2547. {
  2548. return soft_f16_compare(a, b, true, s);
  2549. }
  2550. static FloatRelation QEMU_FLATTEN
  2551. f32_compare(float32 xa, float32 xb, bool is_quiet, float_status *s)
  2552. {
  2553. union_float32 ua, ub;
  2554. ua.s = xa;
  2555. ub.s = xb;
  2556. if (QEMU_NO_HARDFLOAT) {
  2557. goto soft;
  2558. }
  2559. float32_input_flush2(&ua.s, &ub.s, s);
  2560. if (isgreaterequal(ua.h, ub.h)) {
  2561. if (isgreater(ua.h, ub.h)) {
  2562. return float_relation_greater;
  2563. }
  2564. return float_relation_equal;
  2565. }
  2566. if (likely(isless(ua.h, ub.h))) {
  2567. return float_relation_less;
  2568. }
  2569. /* The only condition remaining is unordered.
  2570. * Fall through to set flags.
  2571. */
  2572. soft:
  2573. return soft_f32_compare(ua.s, ub.s, is_quiet, s);
  2574. }
  2575. FloatRelation float32_compare(float32 a, float32 b, float_status *s)
  2576. {
  2577. return f32_compare(a, b, false, s);
  2578. }
  2579. FloatRelation float32_compare_quiet(float32 a, float32 b, float_status *s)
  2580. {
  2581. return f32_compare(a, b, true, s);
  2582. }
  2583. static FloatRelation QEMU_FLATTEN
  2584. f64_compare(float64 xa, float64 xb, bool is_quiet, float_status *s)
  2585. {
  2586. union_float64 ua, ub;
  2587. ua.s = xa;
  2588. ub.s = xb;
  2589. if (QEMU_NO_HARDFLOAT) {
  2590. goto soft;
  2591. }
  2592. float64_input_flush2(&ua.s, &ub.s, s);
  2593. if (isgreaterequal(ua.h, ub.h)) {
  2594. if (isgreater(ua.h, ub.h)) {
  2595. return float_relation_greater;
  2596. }
  2597. return float_relation_equal;
  2598. }
  2599. if (likely(isless(ua.h, ub.h))) {
  2600. return float_relation_less;
  2601. }
  2602. /* The only condition remaining is unordered.
  2603. * Fall through to set flags.
  2604. */
  2605. soft:
  2606. return soft_f64_compare(ua.s, ub.s, is_quiet, s);
  2607. }
  2608. FloatRelation float64_compare(float64 a, float64 b, float_status *s)
  2609. {
  2610. return f64_compare(a, b, false, s);
  2611. }
  2612. FloatRelation float64_compare_quiet(float64 a, float64 b, float_status *s)
  2613. {
  2614. return f64_compare(a, b, true, s);
  2615. }
  2616. /* Multiply A by 2 raised to the power N. */
  2617. static FloatParts scalbn_decomposed(FloatParts a, int n, float_status *s)
  2618. {
  2619. if (unlikely(is_nan(a.cls))) {
  2620. return return_nan(a, s);
  2621. }
  2622. if (a.cls == float_class_normal) {
  2623. /* The largest float type (even though not supported by FloatParts)
  2624. * is float128, which has a 15 bit exponent. Bounding N to 16 bits
  2625. * still allows rounding to infinity, without allowing overflow
  2626. * within the int32_t that backs FloatParts.exp.
  2627. */
  2628. n = MIN(MAX(n, -0x10000), 0x10000);
  2629. a.exp += n;
  2630. }
  2631. return a;
  2632. }
  2633. float16 float16_scalbn(float16 a, int n, float_status *status)
  2634. {
  2635. FloatParts pa = float16_unpack_canonical(a, status);
  2636. FloatParts pr = scalbn_decomposed(pa, n, status);
  2637. return float16_round_pack_canonical(pr, status);
  2638. }
  2639. float32 float32_scalbn(float32 a, int n, float_status *status)
  2640. {
  2641. FloatParts pa = float32_unpack_canonical(a, status);
  2642. FloatParts pr = scalbn_decomposed(pa, n, status);
  2643. return float32_round_pack_canonical(pr, status);
  2644. }
  2645. float64 float64_scalbn(float64 a, int n, float_status *status)
  2646. {
  2647. FloatParts pa = float64_unpack_canonical(a, status);
  2648. FloatParts pr = scalbn_decomposed(pa, n, status);
  2649. return float64_round_pack_canonical(pr, status);
  2650. }
  2651. /*
  2652. * Square Root
  2653. *
  2654. * The old softfloat code did an approximation step before zeroing in
  2655. * on the final result. However for simpleness we just compute the
  2656. * square root by iterating down from the implicit bit to enough extra
  2657. * bits to ensure we get a correctly rounded result.
  2658. *
  2659. * This does mean however the calculation is slower than before,
  2660. * especially for 64 bit floats.
  2661. */
  2662. static FloatParts sqrt_float(FloatParts a, float_status *s, const FloatFmt *p)
  2663. {
  2664. uint64_t a_frac, r_frac, s_frac;
  2665. int bit, last_bit;
  2666. if (is_nan(a.cls)) {
  2667. return return_nan(a, s);
  2668. }
  2669. if (a.cls == float_class_zero) {
  2670. return a; /* sqrt(+-0) = +-0 */
  2671. }
  2672. if (a.sign) {
  2673. s->float_exception_flags |= float_flag_invalid;
  2674. return parts_default_nan(s);
  2675. }
  2676. if (a.cls == float_class_inf) {
  2677. return a; /* sqrt(+inf) = +inf */
  2678. }
  2679. assert(a.cls == float_class_normal);
  2680. /* We need two overflow bits at the top. Adding room for that is a
  2681. * right shift. If the exponent is odd, we can discard the low bit
  2682. * by multiplying the fraction by 2; that's a left shift. Combine
  2683. * those and we shift right if the exponent is even.
  2684. */
  2685. a_frac = a.frac;
  2686. if (!(a.exp & 1)) {
  2687. a_frac >>= 1;
  2688. }
  2689. a.exp >>= 1;
  2690. /* Bit-by-bit computation of sqrt. */
  2691. r_frac = 0;
  2692. s_frac = 0;
  2693. /* Iterate from implicit bit down to the 3 extra bits to compute a
  2694. * properly rounded result. Remember we've inserted one more bit
  2695. * at the top, so these positions are one less.
  2696. */
  2697. bit = DECOMPOSED_BINARY_POINT - 1;
  2698. last_bit = MAX(p->frac_shift - 4, 0);
  2699. do {
  2700. uint64_t q = 1ULL << bit;
  2701. uint64_t t_frac = s_frac + q;
  2702. if (t_frac <= a_frac) {
  2703. s_frac = t_frac + q;
  2704. a_frac -= t_frac;
  2705. r_frac += q;
  2706. }
  2707. a_frac <<= 1;
  2708. } while (--bit >= last_bit);
  2709. /* Undo the right shift done above. If there is any remaining
  2710. * fraction, the result is inexact. Set the sticky bit.
  2711. */
  2712. a.frac = (r_frac << 1) + (a_frac != 0);
  2713. return a;
  2714. }
  2715. float16 QEMU_FLATTEN float16_sqrt(float16 a, float_status *status)
  2716. {
  2717. FloatParts pa = float16_unpack_canonical(a, status);
  2718. FloatParts pr = sqrt_float(pa, status, &float16_params);
  2719. return float16_round_pack_canonical(pr, status);
  2720. }
  2721. static float32 QEMU_SOFTFLOAT_ATTR
  2722. soft_f32_sqrt(float32 a, float_status *status)
  2723. {
  2724. FloatParts pa = float32_unpack_canonical(a, status);
  2725. FloatParts pr = sqrt_float(pa, status, &float32_params);
  2726. return float32_round_pack_canonical(pr, status);
  2727. }
  2728. static float64 QEMU_SOFTFLOAT_ATTR
  2729. soft_f64_sqrt(float64 a, float_status *status)
  2730. {
  2731. FloatParts pa = float64_unpack_canonical(a, status);
  2732. FloatParts pr = sqrt_float(pa, status, &float64_params);
  2733. return float64_round_pack_canonical(pr, status);
  2734. }
  2735. float32 QEMU_FLATTEN float32_sqrt(float32 xa, float_status *s)
  2736. {
  2737. union_float32 ua, ur;
  2738. ua.s = xa;
  2739. if (unlikely(!can_use_fpu(s))) {
  2740. goto soft;
  2741. }
  2742. float32_input_flush1(&ua.s, s);
  2743. if (QEMU_HARDFLOAT_1F32_USE_FP) {
  2744. if (unlikely(!(fpclassify(ua.h) == FP_NORMAL ||
  2745. fpclassify(ua.h) == FP_ZERO) ||
  2746. signbit(ua.h))) {
  2747. goto soft;
  2748. }
  2749. } else if (unlikely(!float32_is_zero_or_normal(ua.s) ||
  2750. float32_is_neg(ua.s))) {
  2751. goto soft;
  2752. }
  2753. ur.h = sqrtf(ua.h);
  2754. return ur.s;
  2755. soft:
  2756. return soft_f32_sqrt(ua.s, s);
  2757. }
  2758. float64 QEMU_FLATTEN float64_sqrt(float64 xa, float_status *s)
  2759. {
  2760. union_float64 ua, ur;
  2761. ua.s = xa;
  2762. if (unlikely(!can_use_fpu(s))) {
  2763. goto soft;
  2764. }
  2765. float64_input_flush1(&ua.s, s);
  2766. if (QEMU_HARDFLOAT_1F64_USE_FP) {
  2767. if (unlikely(!(fpclassify(ua.h) == FP_NORMAL ||
  2768. fpclassify(ua.h) == FP_ZERO) ||
  2769. signbit(ua.h))) {
  2770. goto soft;
  2771. }
  2772. } else if (unlikely(!float64_is_zero_or_normal(ua.s) ||
  2773. float64_is_neg(ua.s))) {
  2774. goto soft;
  2775. }
  2776. ur.h = sqrt(ua.h);
  2777. return ur.s;
  2778. soft:
  2779. return soft_f64_sqrt(ua.s, s);
  2780. }
  2781. /*----------------------------------------------------------------------------
  2782. | The pattern for a default generated NaN.
  2783. *----------------------------------------------------------------------------*/
  2784. float16 float16_default_nan(float_status *status)
  2785. {
  2786. FloatParts p = parts_default_nan(status);
  2787. p.frac >>= float16_params.frac_shift;
  2788. return float16_pack_raw(p);
  2789. }
  2790. float32 float32_default_nan(float_status *status)
  2791. {
  2792. FloatParts p = parts_default_nan(status);
  2793. p.frac >>= float32_params.frac_shift;
  2794. return float32_pack_raw(p);
  2795. }
  2796. float64 float64_default_nan(float_status *status)
  2797. {
  2798. FloatParts p = parts_default_nan(status);
  2799. p.frac >>= float64_params.frac_shift;
  2800. return float64_pack_raw(p);
  2801. }
  2802. float128 float128_default_nan(float_status *status)
  2803. {
  2804. FloatParts p = parts_default_nan(status);
  2805. float128 r;
  2806. /* Extrapolate from the choices made by parts_default_nan to fill
  2807. * in the quad-floating format. If the low bit is set, assume we
  2808. * want to set all non-snan bits.
  2809. */
  2810. r.low = -(p.frac & 1);
  2811. r.high = p.frac >> (DECOMPOSED_BINARY_POINT - 48);
  2812. r.high |= UINT64_C(0x7FFF000000000000);
  2813. r.high |= (uint64_t)p.sign << 63;
  2814. return r;
  2815. }
  2816. /*----------------------------------------------------------------------------
  2817. | Returns a quiet NaN from a signalling NaN for the floating point value `a'.
  2818. *----------------------------------------------------------------------------*/
  2819. float16 float16_silence_nan(float16 a, float_status *status)
  2820. {
  2821. FloatParts p = float16_unpack_raw(a);
  2822. p.frac <<= float16_params.frac_shift;
  2823. p = parts_silence_nan(p, status);
  2824. p.frac >>= float16_params.frac_shift;
  2825. return float16_pack_raw(p);
  2826. }
  2827. float32 float32_silence_nan(float32 a, float_status *status)
  2828. {
  2829. FloatParts p = float32_unpack_raw(a);
  2830. p.frac <<= float32_params.frac_shift;
  2831. p = parts_silence_nan(p, status);
  2832. p.frac >>= float32_params.frac_shift;
  2833. return float32_pack_raw(p);
  2834. }
  2835. float64 float64_silence_nan(float64 a, float_status *status)
  2836. {
  2837. FloatParts p = float64_unpack_raw(a);
  2838. p.frac <<= float64_params.frac_shift;
  2839. p = parts_silence_nan(p, status);
  2840. p.frac >>= float64_params.frac_shift;
  2841. return float64_pack_raw(p);
  2842. }
  2843. /*----------------------------------------------------------------------------
  2844. | If `a' is denormal and we are in flush-to-zero mode then set the
  2845. | input-denormal exception and return zero. Otherwise just return the value.
  2846. *----------------------------------------------------------------------------*/
  2847. static bool parts_squash_denormal(FloatParts p, float_status *status)
  2848. {
  2849. if (p.exp == 0 && p.frac != 0) {
  2850. float_raise(float_flag_input_denormal, status);
  2851. return true;
  2852. }
  2853. return false;
  2854. }
  2855. float16 float16_squash_input_denormal(float16 a, float_status *status)
  2856. {
  2857. if (status->flush_inputs_to_zero) {
  2858. FloatParts p = float16_unpack_raw(a);
  2859. if (parts_squash_denormal(p, status)) {
  2860. return float16_set_sign(float16_zero, p.sign);
  2861. }
  2862. }
  2863. return a;
  2864. }
  2865. float32 float32_squash_input_denormal(float32 a, float_status *status)
  2866. {
  2867. if (status->flush_inputs_to_zero) {
  2868. FloatParts p = float32_unpack_raw(a);
  2869. if (parts_squash_denormal(p, status)) {
  2870. return float32_set_sign(float32_zero, p.sign);
  2871. }
  2872. }
  2873. return a;
  2874. }
  2875. float64 float64_squash_input_denormal(float64 a, float_status *status)
  2876. {
  2877. if (status->flush_inputs_to_zero) {
  2878. FloatParts p = float64_unpack_raw(a);
  2879. if (parts_squash_denormal(p, status)) {
  2880. return float64_set_sign(float64_zero, p.sign);
  2881. }
  2882. }
  2883. return a;
  2884. }
  2885. /*----------------------------------------------------------------------------
  2886. | Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
  2887. | and 7, and returns the properly rounded 32-bit integer corresponding to the
  2888. | input. If `zSign' is 1, the input is negated before being converted to an
  2889. | integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point input
  2890. | is simply rounded to an integer, with the inexact exception raised if the
  2891. | input cannot be represented exactly as an integer. However, if the fixed-
  2892. | point input is too large, the invalid exception is raised and the largest
  2893. | positive or negative integer is returned.
  2894. *----------------------------------------------------------------------------*/
  2895. static int32_t roundAndPackInt32(bool zSign, uint64_t absZ,
  2896. float_status *status)
  2897. {
  2898. int8_t roundingMode;
  2899. bool roundNearestEven;
  2900. int8_t roundIncrement, roundBits;
  2901. int32_t z;
  2902. roundingMode = status->float_rounding_mode;
  2903. roundNearestEven = ( roundingMode == float_round_nearest_even );
  2904. switch (roundingMode) {
  2905. case float_round_nearest_even:
  2906. case float_round_ties_away:
  2907. roundIncrement = 0x40;
  2908. break;
  2909. case float_round_to_zero:
  2910. roundIncrement = 0;
  2911. break;
  2912. case float_round_up:
  2913. roundIncrement = zSign ? 0 : 0x7f;
  2914. break;
  2915. case float_round_down:
  2916. roundIncrement = zSign ? 0x7f : 0;
  2917. break;
  2918. case float_round_to_odd:
  2919. roundIncrement = absZ & 0x80 ? 0 : 0x7f;
  2920. break;
  2921. default:
  2922. abort();
  2923. }
  2924. roundBits = absZ & 0x7F;
  2925. absZ = ( absZ + roundIncrement )>>7;
  2926. if (!(roundBits ^ 0x40) && roundNearestEven) {
  2927. absZ &= ~1;
  2928. }
  2929. z = absZ;
  2930. if ( zSign ) z = - z;
  2931. if ( ( absZ>>32 ) || ( z && ( ( z < 0 ) ^ zSign ) ) ) {
  2932. float_raise(float_flag_invalid, status);
  2933. return zSign ? INT32_MIN : INT32_MAX;
  2934. }
  2935. if (roundBits) {
  2936. status->float_exception_flags |= float_flag_inexact;
  2937. }
  2938. return z;
  2939. }
  2940. /*----------------------------------------------------------------------------
  2941. | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
  2942. | `absZ1', with binary point between bits 63 and 64 (between the input words),
  2943. | and returns the properly rounded 64-bit integer corresponding to the input.
  2944. | If `zSign' is 1, the input is negated before being converted to an integer.
  2945. | Ordinarily, the fixed-point input is simply rounded to an integer, with
  2946. | the inexact exception raised if the input cannot be represented exactly as
  2947. | an integer. However, if the fixed-point input is too large, the invalid
  2948. | exception is raised and the largest positive or negative integer is
  2949. | returned.
  2950. *----------------------------------------------------------------------------*/
  2951. static int64_t roundAndPackInt64(bool zSign, uint64_t absZ0, uint64_t absZ1,
  2952. float_status *status)
  2953. {
  2954. int8_t roundingMode;
  2955. bool roundNearestEven, increment;
  2956. int64_t z;
  2957. roundingMode = status->float_rounding_mode;
  2958. roundNearestEven = ( roundingMode == float_round_nearest_even );
  2959. switch (roundingMode) {
  2960. case float_round_nearest_even:
  2961. case float_round_ties_away:
  2962. increment = ((int64_t) absZ1 < 0);
  2963. break;
  2964. case float_round_to_zero:
  2965. increment = 0;
  2966. break;
  2967. case float_round_up:
  2968. increment = !zSign && absZ1;
  2969. break;
  2970. case float_round_down:
  2971. increment = zSign && absZ1;
  2972. break;
  2973. case float_round_to_odd:
  2974. increment = !(absZ0 & 1) && absZ1;
  2975. break;
  2976. default:
  2977. abort();
  2978. }
  2979. if ( increment ) {
  2980. ++absZ0;
  2981. if ( absZ0 == 0 ) goto overflow;
  2982. if (!(absZ1 << 1) && roundNearestEven) {
  2983. absZ0 &= ~1;
  2984. }
  2985. }
  2986. z = absZ0;
  2987. if ( zSign ) z = - z;
  2988. if ( z && ( ( z < 0 ) ^ zSign ) ) {
  2989. overflow:
  2990. float_raise(float_flag_invalid, status);
  2991. return zSign ? INT64_MIN : INT64_MAX;
  2992. }
  2993. if (absZ1) {
  2994. status->float_exception_flags |= float_flag_inexact;
  2995. }
  2996. return z;
  2997. }
  2998. /*----------------------------------------------------------------------------
  2999. | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
  3000. | `absZ1', with binary point between bits 63 and 64 (between the input words),
  3001. | and returns the properly rounded 64-bit unsigned integer corresponding to the
  3002. | input. Ordinarily, the fixed-point input is simply rounded to an integer,
  3003. | with the inexact exception raised if the input cannot be represented exactly
  3004. | as an integer. However, if the fixed-point input is too large, the invalid
  3005. | exception is raised and the largest unsigned integer is returned.
  3006. *----------------------------------------------------------------------------*/
  3007. static int64_t roundAndPackUint64(bool zSign, uint64_t absZ0,
  3008. uint64_t absZ1, float_status *status)
  3009. {
  3010. int8_t roundingMode;
  3011. bool roundNearestEven, increment;
  3012. roundingMode = status->float_rounding_mode;
  3013. roundNearestEven = (roundingMode == float_round_nearest_even);
  3014. switch (roundingMode) {
  3015. case float_round_nearest_even:
  3016. case float_round_ties_away:
  3017. increment = ((int64_t)absZ1 < 0);
  3018. break;
  3019. case float_round_to_zero:
  3020. increment = 0;
  3021. break;
  3022. case float_round_up:
  3023. increment = !zSign && absZ1;
  3024. break;
  3025. case float_round_down:
  3026. increment = zSign && absZ1;
  3027. break;
  3028. case float_round_to_odd:
  3029. increment = !(absZ0 & 1) && absZ1;
  3030. break;
  3031. default:
  3032. abort();
  3033. }
  3034. if (increment) {
  3035. ++absZ0;
  3036. if (absZ0 == 0) {
  3037. float_raise(float_flag_invalid, status);
  3038. return UINT64_MAX;
  3039. }
  3040. if (!(absZ1 << 1) && roundNearestEven) {
  3041. absZ0 &= ~1;
  3042. }
  3043. }
  3044. if (zSign && absZ0) {
  3045. float_raise(float_flag_invalid, status);
  3046. return 0;
  3047. }
  3048. if (absZ1) {
  3049. status->float_exception_flags |= float_flag_inexact;
  3050. }
  3051. return absZ0;
  3052. }
  3053. /*----------------------------------------------------------------------------
  3054. | Normalizes the subnormal single-precision floating-point value represented
  3055. | by the denormalized significand `aSig'. The normalized exponent and
  3056. | significand are stored at the locations pointed to by `zExpPtr' and
  3057. | `zSigPtr', respectively.
  3058. *----------------------------------------------------------------------------*/
  3059. static void
  3060. normalizeFloat32Subnormal(uint32_t aSig, int *zExpPtr, uint32_t *zSigPtr)
  3061. {
  3062. int8_t shiftCount;
  3063. shiftCount = clz32(aSig) - 8;
  3064. *zSigPtr = aSig<<shiftCount;
  3065. *zExpPtr = 1 - shiftCount;
  3066. }
  3067. /*----------------------------------------------------------------------------
  3068. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3069. | and significand `zSig', and returns the proper single-precision floating-
  3070. | point value corresponding to the abstract input. Ordinarily, the abstract
  3071. | value is simply rounded and packed into the single-precision format, with
  3072. | the inexact exception raised if the abstract input cannot be represented
  3073. | exactly. However, if the abstract value is too large, the overflow and
  3074. | inexact exceptions are raised and an infinity or maximal finite value is
  3075. | returned. If the abstract value is too small, the input value is rounded to
  3076. | a subnormal number, and the underflow and inexact exceptions are raised if
  3077. | the abstract input cannot be represented exactly as a subnormal single-
  3078. | precision floating-point number.
  3079. | The input significand `zSig' has its binary point between bits 30
  3080. | and 29, which is 7 bits to the left of the usual location. This shifted
  3081. | significand must be normalized or smaller. If `zSig' is not normalized,
  3082. | `zExp' must be 0; in that case, the result returned is a subnormal number,
  3083. | and it must not require rounding. In the usual case that `zSig' is
  3084. | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  3085. | The handling of underflow and overflow follows the IEC/IEEE Standard for
  3086. | Binary Floating-Point Arithmetic.
  3087. *----------------------------------------------------------------------------*/
  3088. static float32 roundAndPackFloat32(bool zSign, int zExp, uint32_t zSig,
  3089. float_status *status)
  3090. {
  3091. int8_t roundingMode;
  3092. bool roundNearestEven;
  3093. int8_t roundIncrement, roundBits;
  3094. bool isTiny;
  3095. roundingMode = status->float_rounding_mode;
  3096. roundNearestEven = ( roundingMode == float_round_nearest_even );
  3097. switch (roundingMode) {
  3098. case float_round_nearest_even:
  3099. case float_round_ties_away:
  3100. roundIncrement = 0x40;
  3101. break;
  3102. case float_round_to_zero:
  3103. roundIncrement = 0;
  3104. break;
  3105. case float_round_up:
  3106. roundIncrement = zSign ? 0 : 0x7f;
  3107. break;
  3108. case float_round_down:
  3109. roundIncrement = zSign ? 0x7f : 0;
  3110. break;
  3111. case float_round_to_odd:
  3112. roundIncrement = zSig & 0x80 ? 0 : 0x7f;
  3113. break;
  3114. default:
  3115. abort();
  3116. break;
  3117. }
  3118. roundBits = zSig & 0x7F;
  3119. if ( 0xFD <= (uint16_t) zExp ) {
  3120. if ( ( 0xFD < zExp )
  3121. || ( ( zExp == 0xFD )
  3122. && ( (int32_t) ( zSig + roundIncrement ) < 0 ) )
  3123. ) {
  3124. bool overflow_to_inf = roundingMode != float_round_to_odd &&
  3125. roundIncrement != 0;
  3126. float_raise(float_flag_overflow | float_flag_inexact, status);
  3127. return packFloat32(zSign, 0xFF, -!overflow_to_inf);
  3128. }
  3129. if ( zExp < 0 ) {
  3130. if (status->flush_to_zero) {
  3131. float_raise(float_flag_output_denormal, status);
  3132. return packFloat32(zSign, 0, 0);
  3133. }
  3134. isTiny = status->tininess_before_rounding
  3135. || (zExp < -1)
  3136. || (zSig + roundIncrement < 0x80000000);
  3137. shift32RightJamming( zSig, - zExp, &zSig );
  3138. zExp = 0;
  3139. roundBits = zSig & 0x7F;
  3140. if (isTiny && roundBits) {
  3141. float_raise(float_flag_underflow, status);
  3142. }
  3143. if (roundingMode == float_round_to_odd) {
  3144. /*
  3145. * For round-to-odd case, the roundIncrement depends on
  3146. * zSig which just changed.
  3147. */
  3148. roundIncrement = zSig & 0x80 ? 0 : 0x7f;
  3149. }
  3150. }
  3151. }
  3152. if (roundBits) {
  3153. status->float_exception_flags |= float_flag_inexact;
  3154. }
  3155. zSig = ( zSig + roundIncrement )>>7;
  3156. if (!(roundBits ^ 0x40) && roundNearestEven) {
  3157. zSig &= ~1;
  3158. }
  3159. if ( zSig == 0 ) zExp = 0;
  3160. return packFloat32( zSign, zExp, zSig );
  3161. }
  3162. /*----------------------------------------------------------------------------
  3163. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3164. | and significand `zSig', and returns the proper single-precision floating-
  3165. | point value corresponding to the abstract input. This routine is just like
  3166. | `roundAndPackFloat32' except that `zSig' does not have to be normalized.
  3167. | Bit 31 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
  3168. | floating-point exponent.
  3169. *----------------------------------------------------------------------------*/
  3170. static float32
  3171. normalizeRoundAndPackFloat32(bool zSign, int zExp, uint32_t zSig,
  3172. float_status *status)
  3173. {
  3174. int8_t shiftCount;
  3175. shiftCount = clz32(zSig) - 1;
  3176. return roundAndPackFloat32(zSign, zExp - shiftCount, zSig<<shiftCount,
  3177. status);
  3178. }
  3179. /*----------------------------------------------------------------------------
  3180. | Normalizes the subnormal double-precision floating-point value represented
  3181. | by the denormalized significand `aSig'. The normalized exponent and
  3182. | significand are stored at the locations pointed to by `zExpPtr' and
  3183. | `zSigPtr', respectively.
  3184. *----------------------------------------------------------------------------*/
  3185. static void
  3186. normalizeFloat64Subnormal(uint64_t aSig, int *zExpPtr, uint64_t *zSigPtr)
  3187. {
  3188. int8_t shiftCount;
  3189. shiftCount = clz64(aSig) - 11;
  3190. *zSigPtr = aSig<<shiftCount;
  3191. *zExpPtr = 1 - shiftCount;
  3192. }
  3193. /*----------------------------------------------------------------------------
  3194. | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  3195. | double-precision floating-point value, returning the result. After being
  3196. | shifted into the proper positions, the three fields are simply added
  3197. | together to form the result. This means that any integer portion of `zSig'
  3198. | will be added into the exponent. Since a properly normalized significand
  3199. | will have an integer portion equal to 1, the `zExp' input should be 1 less
  3200. | than the desired result exponent whenever `zSig' is a complete, normalized
  3201. | significand.
  3202. *----------------------------------------------------------------------------*/
  3203. static inline float64 packFloat64(bool zSign, int zExp, uint64_t zSig)
  3204. {
  3205. return make_float64(
  3206. ( ( (uint64_t) zSign )<<63 ) + ( ( (uint64_t) zExp )<<52 ) + zSig);
  3207. }
  3208. /*----------------------------------------------------------------------------
  3209. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3210. | and significand `zSig', and returns the proper double-precision floating-
  3211. | point value corresponding to the abstract input. Ordinarily, the abstract
  3212. | value is simply rounded and packed into the double-precision format, with
  3213. | the inexact exception raised if the abstract input cannot be represented
  3214. | exactly. However, if the abstract value is too large, the overflow and
  3215. | inexact exceptions are raised and an infinity or maximal finite value is
  3216. | returned. If the abstract value is too small, the input value is rounded to
  3217. | a subnormal number, and the underflow and inexact exceptions are raised if
  3218. | the abstract input cannot be represented exactly as a subnormal double-
  3219. | precision floating-point number.
  3220. | The input significand `zSig' has its binary point between bits 62
  3221. | and 61, which is 10 bits to the left of the usual location. This shifted
  3222. | significand must be normalized or smaller. If `zSig' is not normalized,
  3223. | `zExp' must be 0; in that case, the result returned is a subnormal number,
  3224. | and it must not require rounding. In the usual case that `zSig' is
  3225. | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  3226. | The handling of underflow and overflow follows the IEC/IEEE Standard for
  3227. | Binary Floating-Point Arithmetic.
  3228. *----------------------------------------------------------------------------*/
  3229. static float64 roundAndPackFloat64(bool zSign, int zExp, uint64_t zSig,
  3230. float_status *status)
  3231. {
  3232. int8_t roundingMode;
  3233. bool roundNearestEven;
  3234. int roundIncrement, roundBits;
  3235. bool isTiny;
  3236. roundingMode = status->float_rounding_mode;
  3237. roundNearestEven = ( roundingMode == float_round_nearest_even );
  3238. switch (roundingMode) {
  3239. case float_round_nearest_even:
  3240. case float_round_ties_away:
  3241. roundIncrement = 0x200;
  3242. break;
  3243. case float_round_to_zero:
  3244. roundIncrement = 0;
  3245. break;
  3246. case float_round_up:
  3247. roundIncrement = zSign ? 0 : 0x3ff;
  3248. break;
  3249. case float_round_down:
  3250. roundIncrement = zSign ? 0x3ff : 0;
  3251. break;
  3252. case float_round_to_odd:
  3253. roundIncrement = (zSig & 0x400) ? 0 : 0x3ff;
  3254. break;
  3255. default:
  3256. abort();
  3257. }
  3258. roundBits = zSig & 0x3FF;
  3259. if ( 0x7FD <= (uint16_t) zExp ) {
  3260. if ( ( 0x7FD < zExp )
  3261. || ( ( zExp == 0x7FD )
  3262. && ( (int64_t) ( zSig + roundIncrement ) < 0 ) )
  3263. ) {
  3264. bool overflow_to_inf = roundingMode != float_round_to_odd &&
  3265. roundIncrement != 0;
  3266. float_raise(float_flag_overflow | float_flag_inexact, status);
  3267. return packFloat64(zSign, 0x7FF, -(!overflow_to_inf));
  3268. }
  3269. if ( zExp < 0 ) {
  3270. if (status->flush_to_zero) {
  3271. float_raise(float_flag_output_denormal, status);
  3272. return packFloat64(zSign, 0, 0);
  3273. }
  3274. isTiny = status->tininess_before_rounding
  3275. || (zExp < -1)
  3276. || (zSig + roundIncrement < UINT64_C(0x8000000000000000));
  3277. shift64RightJamming( zSig, - zExp, &zSig );
  3278. zExp = 0;
  3279. roundBits = zSig & 0x3FF;
  3280. if (isTiny && roundBits) {
  3281. float_raise(float_flag_underflow, status);
  3282. }
  3283. if (roundingMode == float_round_to_odd) {
  3284. /*
  3285. * For round-to-odd case, the roundIncrement depends on
  3286. * zSig which just changed.
  3287. */
  3288. roundIncrement = (zSig & 0x400) ? 0 : 0x3ff;
  3289. }
  3290. }
  3291. }
  3292. if (roundBits) {
  3293. status->float_exception_flags |= float_flag_inexact;
  3294. }
  3295. zSig = ( zSig + roundIncrement )>>10;
  3296. if (!(roundBits ^ 0x200) && roundNearestEven) {
  3297. zSig &= ~1;
  3298. }
  3299. if ( zSig == 0 ) zExp = 0;
  3300. return packFloat64( zSign, zExp, zSig );
  3301. }
  3302. /*----------------------------------------------------------------------------
  3303. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3304. | and significand `zSig', and returns the proper double-precision floating-
  3305. | point value corresponding to the abstract input. This routine is just like
  3306. | `roundAndPackFloat64' except that `zSig' does not have to be normalized.
  3307. | Bit 63 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
  3308. | floating-point exponent.
  3309. *----------------------------------------------------------------------------*/
  3310. static float64
  3311. normalizeRoundAndPackFloat64(bool zSign, int zExp, uint64_t zSig,
  3312. float_status *status)
  3313. {
  3314. int8_t shiftCount;
  3315. shiftCount = clz64(zSig) - 1;
  3316. return roundAndPackFloat64(zSign, zExp - shiftCount, zSig<<shiftCount,
  3317. status);
  3318. }
  3319. /*----------------------------------------------------------------------------
  3320. | Normalizes the subnormal extended double-precision floating-point value
  3321. | represented by the denormalized significand `aSig'. The normalized exponent
  3322. | and significand are stored at the locations pointed to by `zExpPtr' and
  3323. | `zSigPtr', respectively.
  3324. *----------------------------------------------------------------------------*/
  3325. void normalizeFloatx80Subnormal(uint64_t aSig, int32_t *zExpPtr,
  3326. uint64_t *zSigPtr)
  3327. {
  3328. int8_t shiftCount;
  3329. shiftCount = clz64(aSig);
  3330. *zSigPtr = aSig<<shiftCount;
  3331. *zExpPtr = 1 - shiftCount;
  3332. }
  3333. /*----------------------------------------------------------------------------
  3334. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3335. | and extended significand formed by the concatenation of `zSig0' and `zSig1',
  3336. | and returns the proper extended double-precision floating-point value
  3337. | corresponding to the abstract input. Ordinarily, the abstract value is
  3338. | rounded and packed into the extended double-precision format, with the
  3339. | inexact exception raised if the abstract input cannot be represented
  3340. | exactly. However, if the abstract value is too large, the overflow and
  3341. | inexact exceptions are raised and an infinity or maximal finite value is
  3342. | returned. If the abstract value is too small, the input value is rounded to
  3343. | a subnormal number, and the underflow and inexact exceptions are raised if
  3344. | the abstract input cannot be represented exactly as a subnormal extended
  3345. | double-precision floating-point number.
  3346. | If `roundingPrecision' is 32 or 64, the result is rounded to the same
  3347. | number of bits as single or double precision, respectively. Otherwise, the
  3348. | result is rounded to the full precision of the extended double-precision
  3349. | format.
  3350. | The input significand must be normalized or smaller. If the input
  3351. | significand is not normalized, `zExp' must be 0; in that case, the result
  3352. | returned is a subnormal number, and it must not require rounding. The
  3353. | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
  3354. | Floating-Point Arithmetic.
  3355. *----------------------------------------------------------------------------*/
  3356. floatx80 roundAndPackFloatx80(int8_t roundingPrecision, bool zSign,
  3357. int32_t zExp, uint64_t zSig0, uint64_t zSig1,
  3358. float_status *status)
  3359. {
  3360. int8_t roundingMode;
  3361. bool roundNearestEven, increment, isTiny;
  3362. int64_t roundIncrement, roundMask, roundBits;
  3363. roundingMode = status->float_rounding_mode;
  3364. roundNearestEven = ( roundingMode == float_round_nearest_even );
  3365. if ( roundingPrecision == 80 ) goto precision80;
  3366. if ( roundingPrecision == 64 ) {
  3367. roundIncrement = UINT64_C(0x0000000000000400);
  3368. roundMask = UINT64_C(0x00000000000007FF);
  3369. }
  3370. else if ( roundingPrecision == 32 ) {
  3371. roundIncrement = UINT64_C(0x0000008000000000);
  3372. roundMask = UINT64_C(0x000000FFFFFFFFFF);
  3373. }
  3374. else {
  3375. goto precision80;
  3376. }
  3377. zSig0 |= ( zSig1 != 0 );
  3378. switch (roundingMode) {
  3379. case float_round_nearest_even:
  3380. case float_round_ties_away:
  3381. break;
  3382. case float_round_to_zero:
  3383. roundIncrement = 0;
  3384. break;
  3385. case float_round_up:
  3386. roundIncrement = zSign ? 0 : roundMask;
  3387. break;
  3388. case float_round_down:
  3389. roundIncrement = zSign ? roundMask : 0;
  3390. break;
  3391. default:
  3392. abort();
  3393. }
  3394. roundBits = zSig0 & roundMask;
  3395. if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
  3396. if ( ( 0x7FFE < zExp )
  3397. || ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
  3398. ) {
  3399. goto overflow;
  3400. }
  3401. if ( zExp <= 0 ) {
  3402. if (status->flush_to_zero) {
  3403. float_raise(float_flag_output_denormal, status);
  3404. return packFloatx80(zSign, 0, 0);
  3405. }
  3406. isTiny = status->tininess_before_rounding
  3407. || (zExp < 0 )
  3408. || (zSig0 <= zSig0 + roundIncrement);
  3409. shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
  3410. zExp = 0;
  3411. roundBits = zSig0 & roundMask;
  3412. if (isTiny && roundBits) {
  3413. float_raise(float_flag_underflow, status);
  3414. }
  3415. if (roundBits) {
  3416. status->float_exception_flags |= float_flag_inexact;
  3417. }
  3418. zSig0 += roundIncrement;
  3419. if ( (int64_t) zSig0 < 0 ) zExp = 1;
  3420. roundIncrement = roundMask + 1;
  3421. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  3422. roundMask |= roundIncrement;
  3423. }
  3424. zSig0 &= ~ roundMask;
  3425. return packFloatx80( zSign, zExp, zSig0 );
  3426. }
  3427. }
  3428. if (roundBits) {
  3429. status->float_exception_flags |= float_flag_inexact;
  3430. }
  3431. zSig0 += roundIncrement;
  3432. if ( zSig0 < roundIncrement ) {
  3433. ++zExp;
  3434. zSig0 = UINT64_C(0x8000000000000000);
  3435. }
  3436. roundIncrement = roundMask + 1;
  3437. if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
  3438. roundMask |= roundIncrement;
  3439. }
  3440. zSig0 &= ~ roundMask;
  3441. if ( zSig0 == 0 ) zExp = 0;
  3442. return packFloatx80( zSign, zExp, zSig0 );
  3443. precision80:
  3444. switch (roundingMode) {
  3445. case float_round_nearest_even:
  3446. case float_round_ties_away:
  3447. increment = ((int64_t)zSig1 < 0);
  3448. break;
  3449. case float_round_to_zero:
  3450. increment = 0;
  3451. break;
  3452. case float_round_up:
  3453. increment = !zSign && zSig1;
  3454. break;
  3455. case float_round_down:
  3456. increment = zSign && zSig1;
  3457. break;
  3458. default:
  3459. abort();
  3460. }
  3461. if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
  3462. if ( ( 0x7FFE < zExp )
  3463. || ( ( zExp == 0x7FFE )
  3464. && ( zSig0 == UINT64_C(0xFFFFFFFFFFFFFFFF) )
  3465. && increment
  3466. )
  3467. ) {
  3468. roundMask = 0;
  3469. overflow:
  3470. float_raise(float_flag_overflow | float_flag_inexact, status);
  3471. if ( ( roundingMode == float_round_to_zero )
  3472. || ( zSign && ( roundingMode == float_round_up ) )
  3473. || ( ! zSign && ( roundingMode == float_round_down ) )
  3474. ) {
  3475. return packFloatx80( zSign, 0x7FFE, ~ roundMask );
  3476. }
  3477. return packFloatx80(zSign,
  3478. floatx80_infinity_high,
  3479. floatx80_infinity_low);
  3480. }
  3481. if ( zExp <= 0 ) {
  3482. isTiny = status->tininess_before_rounding
  3483. || (zExp < 0)
  3484. || !increment
  3485. || (zSig0 < UINT64_C(0xFFFFFFFFFFFFFFFF));
  3486. shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
  3487. zExp = 0;
  3488. if (isTiny && zSig1) {
  3489. float_raise(float_flag_underflow, status);
  3490. }
  3491. if (zSig1) {
  3492. status->float_exception_flags |= float_flag_inexact;
  3493. }
  3494. switch (roundingMode) {
  3495. case float_round_nearest_even:
  3496. case float_round_ties_away:
  3497. increment = ((int64_t)zSig1 < 0);
  3498. break;
  3499. case float_round_to_zero:
  3500. increment = 0;
  3501. break;
  3502. case float_round_up:
  3503. increment = !zSign && zSig1;
  3504. break;
  3505. case float_round_down:
  3506. increment = zSign && zSig1;
  3507. break;
  3508. default:
  3509. abort();
  3510. }
  3511. if ( increment ) {
  3512. ++zSig0;
  3513. if (!(zSig1 << 1) && roundNearestEven) {
  3514. zSig0 &= ~1;
  3515. }
  3516. if ( (int64_t) zSig0 < 0 ) zExp = 1;
  3517. }
  3518. return packFloatx80( zSign, zExp, zSig0 );
  3519. }
  3520. }
  3521. if (zSig1) {
  3522. status->float_exception_flags |= float_flag_inexact;
  3523. }
  3524. if ( increment ) {
  3525. ++zSig0;
  3526. if ( zSig0 == 0 ) {
  3527. ++zExp;
  3528. zSig0 = UINT64_C(0x8000000000000000);
  3529. }
  3530. else {
  3531. if (!(zSig1 << 1) && roundNearestEven) {
  3532. zSig0 &= ~1;
  3533. }
  3534. }
  3535. }
  3536. else {
  3537. if ( zSig0 == 0 ) zExp = 0;
  3538. }
  3539. return packFloatx80( zSign, zExp, zSig0 );
  3540. }
  3541. /*----------------------------------------------------------------------------
  3542. | Takes an abstract floating-point value having sign `zSign', exponent
  3543. | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
  3544. | and returns the proper extended double-precision floating-point value
  3545. | corresponding to the abstract input. This routine is just like
  3546. | `roundAndPackFloatx80' except that the input significand does not have to be
  3547. | normalized.
  3548. *----------------------------------------------------------------------------*/
  3549. floatx80 normalizeRoundAndPackFloatx80(int8_t roundingPrecision,
  3550. bool zSign, int32_t zExp,
  3551. uint64_t zSig0, uint64_t zSig1,
  3552. float_status *status)
  3553. {
  3554. int8_t shiftCount;
  3555. if ( zSig0 == 0 ) {
  3556. zSig0 = zSig1;
  3557. zSig1 = 0;
  3558. zExp -= 64;
  3559. }
  3560. shiftCount = clz64(zSig0);
  3561. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  3562. zExp -= shiftCount;
  3563. return roundAndPackFloatx80(roundingPrecision, zSign, zExp,
  3564. zSig0, zSig1, status);
  3565. }
  3566. /*----------------------------------------------------------------------------
  3567. | Returns the least-significant 64 fraction bits of the quadruple-precision
  3568. | floating-point value `a'.
  3569. *----------------------------------------------------------------------------*/
  3570. static inline uint64_t extractFloat128Frac1( float128 a )
  3571. {
  3572. return a.low;
  3573. }
  3574. /*----------------------------------------------------------------------------
  3575. | Returns the most-significant 48 fraction bits of the quadruple-precision
  3576. | floating-point value `a'.
  3577. *----------------------------------------------------------------------------*/
  3578. static inline uint64_t extractFloat128Frac0( float128 a )
  3579. {
  3580. return a.high & UINT64_C(0x0000FFFFFFFFFFFF);
  3581. }
  3582. /*----------------------------------------------------------------------------
  3583. | Returns the exponent bits of the quadruple-precision floating-point value
  3584. | `a'.
  3585. *----------------------------------------------------------------------------*/
  3586. static inline int32_t extractFloat128Exp( float128 a )
  3587. {
  3588. return ( a.high>>48 ) & 0x7FFF;
  3589. }
  3590. /*----------------------------------------------------------------------------
  3591. | Returns the sign bit of the quadruple-precision floating-point value `a'.
  3592. *----------------------------------------------------------------------------*/
  3593. static inline bool extractFloat128Sign(float128 a)
  3594. {
  3595. return a.high >> 63;
  3596. }
  3597. /*----------------------------------------------------------------------------
  3598. | Normalizes the subnormal quadruple-precision floating-point value
  3599. | represented by the denormalized significand formed by the concatenation of
  3600. | `aSig0' and `aSig1'. The normalized exponent is stored at the location
  3601. | pointed to by `zExpPtr'. The most significant 49 bits of the normalized
  3602. | significand are stored at the location pointed to by `zSig0Ptr', and the
  3603. | least significant 64 bits of the normalized significand are stored at the
  3604. | location pointed to by `zSig1Ptr'.
  3605. *----------------------------------------------------------------------------*/
  3606. static void
  3607. normalizeFloat128Subnormal(
  3608. uint64_t aSig0,
  3609. uint64_t aSig1,
  3610. int32_t *zExpPtr,
  3611. uint64_t *zSig0Ptr,
  3612. uint64_t *zSig1Ptr
  3613. )
  3614. {
  3615. int8_t shiftCount;
  3616. if ( aSig0 == 0 ) {
  3617. shiftCount = clz64(aSig1) - 15;
  3618. if ( shiftCount < 0 ) {
  3619. *zSig0Ptr = aSig1>>( - shiftCount );
  3620. *zSig1Ptr = aSig1<<( shiftCount & 63 );
  3621. }
  3622. else {
  3623. *zSig0Ptr = aSig1<<shiftCount;
  3624. *zSig1Ptr = 0;
  3625. }
  3626. *zExpPtr = - shiftCount - 63;
  3627. }
  3628. else {
  3629. shiftCount = clz64(aSig0) - 15;
  3630. shortShift128Left( aSig0, aSig1, shiftCount, zSig0Ptr, zSig1Ptr );
  3631. *zExpPtr = 1 - shiftCount;
  3632. }
  3633. }
  3634. /*----------------------------------------------------------------------------
  3635. | Packs the sign `zSign', the exponent `zExp', and the significand formed
  3636. | by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
  3637. | floating-point value, returning the result. After being shifted into the
  3638. | proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
  3639. | added together to form the most significant 32 bits of the result. This
  3640. | means that any integer portion of `zSig0' will be added into the exponent.
  3641. | Since a properly normalized significand will have an integer portion equal
  3642. | to 1, the `zExp' input should be 1 less than the desired result exponent
  3643. | whenever `zSig0' and `zSig1' concatenated form a complete, normalized
  3644. | significand.
  3645. *----------------------------------------------------------------------------*/
  3646. static inline float128
  3647. packFloat128(bool zSign, int32_t zExp, uint64_t zSig0, uint64_t zSig1)
  3648. {
  3649. float128 z;
  3650. z.low = zSig1;
  3651. z.high = ((uint64_t)zSign << 63) + ((uint64_t)zExp << 48) + zSig0;
  3652. return z;
  3653. }
  3654. /*----------------------------------------------------------------------------
  3655. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3656. | and extended significand formed by the concatenation of `zSig0', `zSig1',
  3657. | and `zSig2', and returns the proper quadruple-precision floating-point value
  3658. | corresponding to the abstract input. Ordinarily, the abstract value is
  3659. | simply rounded and packed into the quadruple-precision format, with the
  3660. | inexact exception raised if the abstract input cannot be represented
  3661. | exactly. However, if the abstract value is too large, the overflow and
  3662. | inexact exceptions are raised and an infinity or maximal finite value is
  3663. | returned. If the abstract value is too small, the input value is rounded to
  3664. | a subnormal number, and the underflow and inexact exceptions are raised if
  3665. | the abstract input cannot be represented exactly as a subnormal quadruple-
  3666. | precision floating-point number.
  3667. | The input significand must be normalized or smaller. If the input
  3668. | significand is not normalized, `zExp' must be 0; in that case, the result
  3669. | returned is a subnormal number, and it must not require rounding. In the
  3670. | usual case that the input significand is normalized, `zExp' must be 1 less
  3671. | than the ``true'' floating-point exponent. The handling of underflow and
  3672. | overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3673. *----------------------------------------------------------------------------*/
  3674. static float128 roundAndPackFloat128(bool zSign, int32_t zExp,
  3675. uint64_t zSig0, uint64_t zSig1,
  3676. uint64_t zSig2, float_status *status)
  3677. {
  3678. int8_t roundingMode;
  3679. bool roundNearestEven, increment, isTiny;
  3680. roundingMode = status->float_rounding_mode;
  3681. roundNearestEven = ( roundingMode == float_round_nearest_even );
  3682. switch (roundingMode) {
  3683. case float_round_nearest_even:
  3684. case float_round_ties_away:
  3685. increment = ((int64_t)zSig2 < 0);
  3686. break;
  3687. case float_round_to_zero:
  3688. increment = 0;
  3689. break;
  3690. case float_round_up:
  3691. increment = !zSign && zSig2;
  3692. break;
  3693. case float_round_down:
  3694. increment = zSign && zSig2;
  3695. break;
  3696. case float_round_to_odd:
  3697. increment = !(zSig1 & 0x1) && zSig2;
  3698. break;
  3699. default:
  3700. abort();
  3701. }
  3702. if ( 0x7FFD <= (uint32_t) zExp ) {
  3703. if ( ( 0x7FFD < zExp )
  3704. || ( ( zExp == 0x7FFD )
  3705. && eq128(
  3706. UINT64_C(0x0001FFFFFFFFFFFF),
  3707. UINT64_C(0xFFFFFFFFFFFFFFFF),
  3708. zSig0,
  3709. zSig1
  3710. )
  3711. && increment
  3712. )
  3713. ) {
  3714. float_raise(float_flag_overflow | float_flag_inexact, status);
  3715. if ( ( roundingMode == float_round_to_zero )
  3716. || ( zSign && ( roundingMode == float_round_up ) )
  3717. || ( ! zSign && ( roundingMode == float_round_down ) )
  3718. || (roundingMode == float_round_to_odd)
  3719. ) {
  3720. return
  3721. packFloat128(
  3722. zSign,
  3723. 0x7FFE,
  3724. UINT64_C(0x0000FFFFFFFFFFFF),
  3725. UINT64_C(0xFFFFFFFFFFFFFFFF)
  3726. );
  3727. }
  3728. return packFloat128( zSign, 0x7FFF, 0, 0 );
  3729. }
  3730. if ( zExp < 0 ) {
  3731. if (status->flush_to_zero) {
  3732. float_raise(float_flag_output_denormal, status);
  3733. return packFloat128(zSign, 0, 0, 0);
  3734. }
  3735. isTiny = status->tininess_before_rounding
  3736. || (zExp < -1)
  3737. || !increment
  3738. || lt128(zSig0, zSig1,
  3739. UINT64_C(0x0001FFFFFFFFFFFF),
  3740. UINT64_C(0xFFFFFFFFFFFFFFFF));
  3741. shift128ExtraRightJamming(
  3742. zSig0, zSig1, zSig2, - zExp, &zSig0, &zSig1, &zSig2 );
  3743. zExp = 0;
  3744. if (isTiny && zSig2) {
  3745. float_raise(float_flag_underflow, status);
  3746. }
  3747. switch (roundingMode) {
  3748. case float_round_nearest_even:
  3749. case float_round_ties_away:
  3750. increment = ((int64_t)zSig2 < 0);
  3751. break;
  3752. case float_round_to_zero:
  3753. increment = 0;
  3754. break;
  3755. case float_round_up:
  3756. increment = !zSign && zSig2;
  3757. break;
  3758. case float_round_down:
  3759. increment = zSign && zSig2;
  3760. break;
  3761. case float_round_to_odd:
  3762. increment = !(zSig1 & 0x1) && zSig2;
  3763. break;
  3764. default:
  3765. abort();
  3766. }
  3767. }
  3768. }
  3769. if (zSig2) {
  3770. status->float_exception_flags |= float_flag_inexact;
  3771. }
  3772. if ( increment ) {
  3773. add128( zSig0, zSig1, 0, 1, &zSig0, &zSig1 );
  3774. if ((zSig2 + zSig2 == 0) && roundNearestEven) {
  3775. zSig1 &= ~1;
  3776. }
  3777. }
  3778. else {
  3779. if ( ( zSig0 | zSig1 ) == 0 ) zExp = 0;
  3780. }
  3781. return packFloat128( zSign, zExp, zSig0, zSig1 );
  3782. }
  3783. /*----------------------------------------------------------------------------
  3784. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  3785. | and significand formed by the concatenation of `zSig0' and `zSig1', and
  3786. | returns the proper quadruple-precision floating-point value corresponding
  3787. | to the abstract input. This routine is just like `roundAndPackFloat128'
  3788. | except that the input significand has fewer bits and does not have to be
  3789. | normalized. In all cases, `zExp' must be 1 less than the ``true'' floating-
  3790. | point exponent.
  3791. *----------------------------------------------------------------------------*/
  3792. static float128 normalizeRoundAndPackFloat128(bool zSign, int32_t zExp,
  3793. uint64_t zSig0, uint64_t zSig1,
  3794. float_status *status)
  3795. {
  3796. int8_t shiftCount;
  3797. uint64_t zSig2;
  3798. if ( zSig0 == 0 ) {
  3799. zSig0 = zSig1;
  3800. zSig1 = 0;
  3801. zExp -= 64;
  3802. }
  3803. shiftCount = clz64(zSig0) - 15;
  3804. if ( 0 <= shiftCount ) {
  3805. zSig2 = 0;
  3806. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  3807. }
  3808. else {
  3809. shift128ExtraRightJamming(
  3810. zSig0, zSig1, 0, - shiftCount, &zSig0, &zSig1, &zSig2 );
  3811. }
  3812. zExp -= shiftCount;
  3813. return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
  3814. }
  3815. /*----------------------------------------------------------------------------
  3816. | Returns the result of converting the 32-bit two's complement integer `a'
  3817. | to the extended double-precision floating-point format. The conversion
  3818. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  3819. | Arithmetic.
  3820. *----------------------------------------------------------------------------*/
  3821. floatx80 int32_to_floatx80(int32_t a, float_status *status)
  3822. {
  3823. bool zSign;
  3824. uint32_t absA;
  3825. int8_t shiftCount;
  3826. uint64_t zSig;
  3827. if ( a == 0 ) return packFloatx80( 0, 0, 0 );
  3828. zSign = ( a < 0 );
  3829. absA = zSign ? - a : a;
  3830. shiftCount = clz32(absA) + 32;
  3831. zSig = absA;
  3832. return packFloatx80( zSign, 0x403E - shiftCount, zSig<<shiftCount );
  3833. }
  3834. /*----------------------------------------------------------------------------
  3835. | Returns the result of converting the 32-bit two's complement integer `a' to
  3836. | the quadruple-precision floating-point format. The conversion is performed
  3837. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3838. *----------------------------------------------------------------------------*/
  3839. float128 int32_to_float128(int32_t a, float_status *status)
  3840. {
  3841. bool zSign;
  3842. uint32_t absA;
  3843. int8_t shiftCount;
  3844. uint64_t zSig0;
  3845. if ( a == 0 ) return packFloat128( 0, 0, 0, 0 );
  3846. zSign = ( a < 0 );
  3847. absA = zSign ? - a : a;
  3848. shiftCount = clz32(absA) + 17;
  3849. zSig0 = absA;
  3850. return packFloat128( zSign, 0x402E - shiftCount, zSig0<<shiftCount, 0 );
  3851. }
  3852. /*----------------------------------------------------------------------------
  3853. | Returns the result of converting the 64-bit two's complement integer `a'
  3854. | to the extended double-precision floating-point format. The conversion
  3855. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  3856. | Arithmetic.
  3857. *----------------------------------------------------------------------------*/
  3858. floatx80 int64_to_floatx80(int64_t a, float_status *status)
  3859. {
  3860. bool zSign;
  3861. uint64_t absA;
  3862. int8_t shiftCount;
  3863. if ( a == 0 ) return packFloatx80( 0, 0, 0 );
  3864. zSign = ( a < 0 );
  3865. absA = zSign ? - a : a;
  3866. shiftCount = clz64(absA);
  3867. return packFloatx80( zSign, 0x403E - shiftCount, absA<<shiftCount );
  3868. }
  3869. /*----------------------------------------------------------------------------
  3870. | Returns the result of converting the 64-bit two's complement integer `a' to
  3871. | the quadruple-precision floating-point format. The conversion is performed
  3872. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3873. *----------------------------------------------------------------------------*/
  3874. float128 int64_to_float128(int64_t a, float_status *status)
  3875. {
  3876. bool zSign;
  3877. uint64_t absA;
  3878. int8_t shiftCount;
  3879. int32_t zExp;
  3880. uint64_t zSig0, zSig1;
  3881. if ( a == 0 ) return packFloat128( 0, 0, 0, 0 );
  3882. zSign = ( a < 0 );
  3883. absA = zSign ? - a : a;
  3884. shiftCount = clz64(absA) + 49;
  3885. zExp = 0x406E - shiftCount;
  3886. if ( 64 <= shiftCount ) {
  3887. zSig1 = 0;
  3888. zSig0 = absA;
  3889. shiftCount -= 64;
  3890. }
  3891. else {
  3892. zSig1 = absA;
  3893. zSig0 = 0;
  3894. }
  3895. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  3896. return packFloat128( zSign, zExp, zSig0, zSig1 );
  3897. }
  3898. /*----------------------------------------------------------------------------
  3899. | Returns the result of converting the 64-bit unsigned integer `a'
  3900. | to the quadruple-precision floating-point format. The conversion is performed
  3901. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3902. *----------------------------------------------------------------------------*/
  3903. float128 uint64_to_float128(uint64_t a, float_status *status)
  3904. {
  3905. if (a == 0) {
  3906. return float128_zero;
  3907. }
  3908. return normalizeRoundAndPackFloat128(0, 0x406E, 0, a, status);
  3909. }
  3910. /*----------------------------------------------------------------------------
  3911. | Returns the result of converting the single-precision floating-point value
  3912. | `a' to the extended double-precision floating-point format. The conversion
  3913. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  3914. | Arithmetic.
  3915. *----------------------------------------------------------------------------*/
  3916. floatx80 float32_to_floatx80(float32 a, float_status *status)
  3917. {
  3918. bool aSign;
  3919. int aExp;
  3920. uint32_t aSig;
  3921. a = float32_squash_input_denormal(a, status);
  3922. aSig = extractFloat32Frac( a );
  3923. aExp = extractFloat32Exp( a );
  3924. aSign = extractFloat32Sign( a );
  3925. if ( aExp == 0xFF ) {
  3926. if (aSig) {
  3927. floatx80 res = commonNaNToFloatx80(float32ToCommonNaN(a, status),
  3928. status);
  3929. return floatx80_silence_nan(res, status);
  3930. }
  3931. return packFloatx80(aSign,
  3932. floatx80_infinity_high,
  3933. floatx80_infinity_low);
  3934. }
  3935. if ( aExp == 0 ) {
  3936. if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
  3937. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  3938. }
  3939. aSig |= 0x00800000;
  3940. return packFloatx80( aSign, aExp + 0x3F80, ( (uint64_t) aSig )<<40 );
  3941. }
  3942. /*----------------------------------------------------------------------------
  3943. | Returns the result of converting the single-precision floating-point value
  3944. | `a' to the double-precision floating-point format. The conversion is
  3945. | performed according to the IEC/IEEE Standard for Binary Floating-Point
  3946. | Arithmetic.
  3947. *----------------------------------------------------------------------------*/
  3948. float128 float32_to_float128(float32 a, float_status *status)
  3949. {
  3950. bool aSign;
  3951. int aExp;
  3952. uint32_t aSig;
  3953. a = float32_squash_input_denormal(a, status);
  3954. aSig = extractFloat32Frac( a );
  3955. aExp = extractFloat32Exp( a );
  3956. aSign = extractFloat32Sign( a );
  3957. if ( aExp == 0xFF ) {
  3958. if (aSig) {
  3959. return commonNaNToFloat128(float32ToCommonNaN(a, status), status);
  3960. }
  3961. return packFloat128( aSign, 0x7FFF, 0, 0 );
  3962. }
  3963. if ( aExp == 0 ) {
  3964. if ( aSig == 0 ) return packFloat128( aSign, 0, 0, 0 );
  3965. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  3966. --aExp;
  3967. }
  3968. return packFloat128( aSign, aExp + 0x3F80, ( (uint64_t) aSig )<<25, 0 );
  3969. }
  3970. /*----------------------------------------------------------------------------
  3971. | Returns the remainder of the single-precision floating-point value `a'
  3972. | with respect to the corresponding value `b'. The operation is performed
  3973. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3974. *----------------------------------------------------------------------------*/
  3975. float32 float32_rem(float32 a, float32 b, float_status *status)
  3976. {
  3977. bool aSign, zSign;
  3978. int aExp, bExp, expDiff;
  3979. uint32_t aSig, bSig;
  3980. uint32_t q;
  3981. uint64_t aSig64, bSig64, q64;
  3982. uint32_t alternateASig;
  3983. int32_t sigMean;
  3984. a = float32_squash_input_denormal(a, status);
  3985. b = float32_squash_input_denormal(b, status);
  3986. aSig = extractFloat32Frac( a );
  3987. aExp = extractFloat32Exp( a );
  3988. aSign = extractFloat32Sign( a );
  3989. bSig = extractFloat32Frac( b );
  3990. bExp = extractFloat32Exp( b );
  3991. if ( aExp == 0xFF ) {
  3992. if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) {
  3993. return propagateFloat32NaN(a, b, status);
  3994. }
  3995. float_raise(float_flag_invalid, status);
  3996. return float32_default_nan(status);
  3997. }
  3998. if ( bExp == 0xFF ) {
  3999. if (bSig) {
  4000. return propagateFloat32NaN(a, b, status);
  4001. }
  4002. return a;
  4003. }
  4004. if ( bExp == 0 ) {
  4005. if ( bSig == 0 ) {
  4006. float_raise(float_flag_invalid, status);
  4007. return float32_default_nan(status);
  4008. }
  4009. normalizeFloat32Subnormal( bSig, &bExp, &bSig );
  4010. }
  4011. if ( aExp == 0 ) {
  4012. if ( aSig == 0 ) return a;
  4013. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  4014. }
  4015. expDiff = aExp - bExp;
  4016. aSig |= 0x00800000;
  4017. bSig |= 0x00800000;
  4018. if ( expDiff < 32 ) {
  4019. aSig <<= 8;
  4020. bSig <<= 8;
  4021. if ( expDiff < 0 ) {
  4022. if ( expDiff < -1 ) return a;
  4023. aSig >>= 1;
  4024. }
  4025. q = ( bSig <= aSig );
  4026. if ( q ) aSig -= bSig;
  4027. if ( 0 < expDiff ) {
  4028. q = ( ( (uint64_t) aSig )<<32 ) / bSig;
  4029. q >>= 32 - expDiff;
  4030. bSig >>= 2;
  4031. aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
  4032. }
  4033. else {
  4034. aSig >>= 2;
  4035. bSig >>= 2;
  4036. }
  4037. }
  4038. else {
  4039. if ( bSig <= aSig ) aSig -= bSig;
  4040. aSig64 = ( (uint64_t) aSig )<<40;
  4041. bSig64 = ( (uint64_t) bSig )<<40;
  4042. expDiff -= 64;
  4043. while ( 0 < expDiff ) {
  4044. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  4045. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  4046. aSig64 = - ( ( bSig * q64 )<<38 );
  4047. expDiff -= 62;
  4048. }
  4049. expDiff += 64;
  4050. q64 = estimateDiv128To64( aSig64, 0, bSig64 );
  4051. q64 = ( 2 < q64 ) ? q64 - 2 : 0;
  4052. q = q64>>( 64 - expDiff );
  4053. bSig <<= 6;
  4054. aSig = ( ( aSig64>>33 )<<( expDiff - 1 ) ) - bSig * q;
  4055. }
  4056. do {
  4057. alternateASig = aSig;
  4058. ++q;
  4059. aSig -= bSig;
  4060. } while ( 0 <= (int32_t) aSig );
  4061. sigMean = aSig + alternateASig;
  4062. if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
  4063. aSig = alternateASig;
  4064. }
  4065. zSign = ( (int32_t) aSig < 0 );
  4066. if ( zSign ) aSig = - aSig;
  4067. return normalizeRoundAndPackFloat32(aSign ^ zSign, bExp, aSig, status);
  4068. }
  4069. /*----------------------------------------------------------------------------
  4070. | Returns the binary exponential of the single-precision floating-point value
  4071. | `a'. The operation is performed according to the IEC/IEEE Standard for
  4072. | Binary Floating-Point Arithmetic.
  4073. |
  4074. | Uses the following identities:
  4075. |
  4076. | 1. -------------------------------------------------------------------------
  4077. | x x*ln(2)
  4078. | 2 = e
  4079. |
  4080. | 2. -------------------------------------------------------------------------
  4081. | 2 3 4 5 n
  4082. | x x x x x x x
  4083. | e = 1 + --- + --- + --- + --- + --- + ... + --- + ...
  4084. | 1! 2! 3! 4! 5! n!
  4085. *----------------------------------------------------------------------------*/
  4086. static const float64 float32_exp2_coefficients[15] =
  4087. {
  4088. const_float64( 0x3ff0000000000000ll ), /* 1 */
  4089. const_float64( 0x3fe0000000000000ll ), /* 2 */
  4090. const_float64( 0x3fc5555555555555ll ), /* 3 */
  4091. const_float64( 0x3fa5555555555555ll ), /* 4 */
  4092. const_float64( 0x3f81111111111111ll ), /* 5 */
  4093. const_float64( 0x3f56c16c16c16c17ll ), /* 6 */
  4094. const_float64( 0x3f2a01a01a01a01all ), /* 7 */
  4095. const_float64( 0x3efa01a01a01a01all ), /* 8 */
  4096. const_float64( 0x3ec71de3a556c734ll ), /* 9 */
  4097. const_float64( 0x3e927e4fb7789f5cll ), /* 10 */
  4098. const_float64( 0x3e5ae64567f544e4ll ), /* 11 */
  4099. const_float64( 0x3e21eed8eff8d898ll ), /* 12 */
  4100. const_float64( 0x3de6124613a86d09ll ), /* 13 */
  4101. const_float64( 0x3da93974a8c07c9dll ), /* 14 */
  4102. const_float64( 0x3d6ae7f3e733b81fll ), /* 15 */
  4103. };
  4104. float32 float32_exp2(float32 a, float_status *status)
  4105. {
  4106. bool aSign;
  4107. int aExp;
  4108. uint32_t aSig;
  4109. float64 r, x, xn;
  4110. int i;
  4111. a = float32_squash_input_denormal(a, status);
  4112. aSig = extractFloat32Frac( a );
  4113. aExp = extractFloat32Exp( a );
  4114. aSign = extractFloat32Sign( a );
  4115. if ( aExp == 0xFF) {
  4116. if (aSig) {
  4117. return propagateFloat32NaN(a, float32_zero, status);
  4118. }
  4119. return (aSign) ? float32_zero : a;
  4120. }
  4121. if (aExp == 0) {
  4122. if (aSig == 0) return float32_one;
  4123. }
  4124. float_raise(float_flag_inexact, status);
  4125. /* ******************************* */
  4126. /* using float64 for approximation */
  4127. /* ******************************* */
  4128. x = float32_to_float64(a, status);
  4129. x = float64_mul(x, float64_ln2, status);
  4130. xn = x;
  4131. r = float64_one;
  4132. for (i = 0 ; i < 15 ; i++) {
  4133. float64 f;
  4134. f = float64_mul(xn, float32_exp2_coefficients[i], status);
  4135. r = float64_add(r, f, status);
  4136. xn = float64_mul(xn, x, status);
  4137. }
  4138. return float64_to_float32(r, status);
  4139. }
  4140. /*----------------------------------------------------------------------------
  4141. | Returns the binary log of the single-precision floating-point value `a'.
  4142. | The operation is performed according to the IEC/IEEE Standard for Binary
  4143. | Floating-Point Arithmetic.
  4144. *----------------------------------------------------------------------------*/
  4145. float32 float32_log2(float32 a, float_status *status)
  4146. {
  4147. bool aSign, zSign;
  4148. int aExp;
  4149. uint32_t aSig, zSig, i;
  4150. a = float32_squash_input_denormal(a, status);
  4151. aSig = extractFloat32Frac( a );
  4152. aExp = extractFloat32Exp( a );
  4153. aSign = extractFloat32Sign( a );
  4154. if ( aExp == 0 ) {
  4155. if ( aSig == 0 ) return packFloat32( 1, 0xFF, 0 );
  4156. normalizeFloat32Subnormal( aSig, &aExp, &aSig );
  4157. }
  4158. if ( aSign ) {
  4159. float_raise(float_flag_invalid, status);
  4160. return float32_default_nan(status);
  4161. }
  4162. if ( aExp == 0xFF ) {
  4163. if (aSig) {
  4164. return propagateFloat32NaN(a, float32_zero, status);
  4165. }
  4166. return a;
  4167. }
  4168. aExp -= 0x7F;
  4169. aSig |= 0x00800000;
  4170. zSign = aExp < 0;
  4171. zSig = aExp << 23;
  4172. for (i = 1 << 22; i > 0; i >>= 1) {
  4173. aSig = ( (uint64_t)aSig * aSig ) >> 23;
  4174. if ( aSig & 0x01000000 ) {
  4175. aSig >>= 1;
  4176. zSig |= i;
  4177. }
  4178. }
  4179. if ( zSign )
  4180. zSig = -zSig;
  4181. return normalizeRoundAndPackFloat32(zSign, 0x85, zSig, status);
  4182. }
  4183. /*----------------------------------------------------------------------------
  4184. | Returns the result of converting the double-precision floating-point value
  4185. | `a' to the extended double-precision floating-point format. The conversion
  4186. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  4187. | Arithmetic.
  4188. *----------------------------------------------------------------------------*/
  4189. floatx80 float64_to_floatx80(float64 a, float_status *status)
  4190. {
  4191. bool aSign;
  4192. int aExp;
  4193. uint64_t aSig;
  4194. a = float64_squash_input_denormal(a, status);
  4195. aSig = extractFloat64Frac( a );
  4196. aExp = extractFloat64Exp( a );
  4197. aSign = extractFloat64Sign( a );
  4198. if ( aExp == 0x7FF ) {
  4199. if (aSig) {
  4200. floatx80 res = commonNaNToFloatx80(float64ToCommonNaN(a, status),
  4201. status);
  4202. return floatx80_silence_nan(res, status);
  4203. }
  4204. return packFloatx80(aSign,
  4205. floatx80_infinity_high,
  4206. floatx80_infinity_low);
  4207. }
  4208. if ( aExp == 0 ) {
  4209. if ( aSig == 0 ) return packFloatx80( aSign, 0, 0 );
  4210. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  4211. }
  4212. return
  4213. packFloatx80(
  4214. aSign, aExp + 0x3C00, (aSig | UINT64_C(0x0010000000000000)) << 11);
  4215. }
  4216. /*----------------------------------------------------------------------------
  4217. | Returns the result of converting the double-precision floating-point value
  4218. | `a' to the quadruple-precision floating-point format. The conversion is
  4219. | performed according to the IEC/IEEE Standard for Binary Floating-Point
  4220. | Arithmetic.
  4221. *----------------------------------------------------------------------------*/
  4222. float128 float64_to_float128(float64 a, float_status *status)
  4223. {
  4224. bool aSign;
  4225. int aExp;
  4226. uint64_t aSig, zSig0, zSig1;
  4227. a = float64_squash_input_denormal(a, status);
  4228. aSig = extractFloat64Frac( a );
  4229. aExp = extractFloat64Exp( a );
  4230. aSign = extractFloat64Sign( a );
  4231. if ( aExp == 0x7FF ) {
  4232. if (aSig) {
  4233. return commonNaNToFloat128(float64ToCommonNaN(a, status), status);
  4234. }
  4235. return packFloat128( aSign, 0x7FFF, 0, 0 );
  4236. }
  4237. if ( aExp == 0 ) {
  4238. if ( aSig == 0 ) return packFloat128( aSign, 0, 0, 0 );
  4239. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  4240. --aExp;
  4241. }
  4242. shift128Right( aSig, 0, 4, &zSig0, &zSig1 );
  4243. return packFloat128( aSign, aExp + 0x3C00, zSig0, zSig1 );
  4244. }
  4245. /*----------------------------------------------------------------------------
  4246. | Returns the remainder of the double-precision floating-point value `a'
  4247. | with respect to the corresponding value `b'. The operation is performed
  4248. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4249. *----------------------------------------------------------------------------*/
  4250. float64 float64_rem(float64 a, float64 b, float_status *status)
  4251. {
  4252. bool aSign, zSign;
  4253. int aExp, bExp, expDiff;
  4254. uint64_t aSig, bSig;
  4255. uint64_t q, alternateASig;
  4256. int64_t sigMean;
  4257. a = float64_squash_input_denormal(a, status);
  4258. b = float64_squash_input_denormal(b, status);
  4259. aSig = extractFloat64Frac( a );
  4260. aExp = extractFloat64Exp( a );
  4261. aSign = extractFloat64Sign( a );
  4262. bSig = extractFloat64Frac( b );
  4263. bExp = extractFloat64Exp( b );
  4264. if ( aExp == 0x7FF ) {
  4265. if ( aSig || ( ( bExp == 0x7FF ) && bSig ) ) {
  4266. return propagateFloat64NaN(a, b, status);
  4267. }
  4268. float_raise(float_flag_invalid, status);
  4269. return float64_default_nan(status);
  4270. }
  4271. if ( bExp == 0x7FF ) {
  4272. if (bSig) {
  4273. return propagateFloat64NaN(a, b, status);
  4274. }
  4275. return a;
  4276. }
  4277. if ( bExp == 0 ) {
  4278. if ( bSig == 0 ) {
  4279. float_raise(float_flag_invalid, status);
  4280. return float64_default_nan(status);
  4281. }
  4282. normalizeFloat64Subnormal( bSig, &bExp, &bSig );
  4283. }
  4284. if ( aExp == 0 ) {
  4285. if ( aSig == 0 ) return a;
  4286. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  4287. }
  4288. expDiff = aExp - bExp;
  4289. aSig = (aSig | UINT64_C(0x0010000000000000)) << 11;
  4290. bSig = (bSig | UINT64_C(0x0010000000000000)) << 11;
  4291. if ( expDiff < 0 ) {
  4292. if ( expDiff < -1 ) return a;
  4293. aSig >>= 1;
  4294. }
  4295. q = ( bSig <= aSig );
  4296. if ( q ) aSig -= bSig;
  4297. expDiff -= 64;
  4298. while ( 0 < expDiff ) {
  4299. q = estimateDiv128To64( aSig, 0, bSig );
  4300. q = ( 2 < q ) ? q - 2 : 0;
  4301. aSig = - ( ( bSig>>2 ) * q );
  4302. expDiff -= 62;
  4303. }
  4304. expDiff += 64;
  4305. if ( 0 < expDiff ) {
  4306. q = estimateDiv128To64( aSig, 0, bSig );
  4307. q = ( 2 < q ) ? q - 2 : 0;
  4308. q >>= 64 - expDiff;
  4309. bSig >>= 2;
  4310. aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q;
  4311. }
  4312. else {
  4313. aSig >>= 2;
  4314. bSig >>= 2;
  4315. }
  4316. do {
  4317. alternateASig = aSig;
  4318. ++q;
  4319. aSig -= bSig;
  4320. } while ( 0 <= (int64_t) aSig );
  4321. sigMean = aSig + alternateASig;
  4322. if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) {
  4323. aSig = alternateASig;
  4324. }
  4325. zSign = ( (int64_t) aSig < 0 );
  4326. if ( zSign ) aSig = - aSig;
  4327. return normalizeRoundAndPackFloat64(aSign ^ zSign, bExp, aSig, status);
  4328. }
  4329. /*----------------------------------------------------------------------------
  4330. | Returns the binary log of the double-precision floating-point value `a'.
  4331. | The operation is performed according to the IEC/IEEE Standard for Binary
  4332. | Floating-Point Arithmetic.
  4333. *----------------------------------------------------------------------------*/
  4334. float64 float64_log2(float64 a, float_status *status)
  4335. {
  4336. bool aSign, zSign;
  4337. int aExp;
  4338. uint64_t aSig, aSig0, aSig1, zSig, i;
  4339. a = float64_squash_input_denormal(a, status);
  4340. aSig = extractFloat64Frac( a );
  4341. aExp = extractFloat64Exp( a );
  4342. aSign = extractFloat64Sign( a );
  4343. if ( aExp == 0 ) {
  4344. if ( aSig == 0 ) return packFloat64( 1, 0x7FF, 0 );
  4345. normalizeFloat64Subnormal( aSig, &aExp, &aSig );
  4346. }
  4347. if ( aSign ) {
  4348. float_raise(float_flag_invalid, status);
  4349. return float64_default_nan(status);
  4350. }
  4351. if ( aExp == 0x7FF ) {
  4352. if (aSig) {
  4353. return propagateFloat64NaN(a, float64_zero, status);
  4354. }
  4355. return a;
  4356. }
  4357. aExp -= 0x3FF;
  4358. aSig |= UINT64_C(0x0010000000000000);
  4359. zSign = aExp < 0;
  4360. zSig = (uint64_t)aExp << 52;
  4361. for (i = 1LL << 51; i > 0; i >>= 1) {
  4362. mul64To128( aSig, aSig, &aSig0, &aSig1 );
  4363. aSig = ( aSig0 << 12 ) | ( aSig1 >> 52 );
  4364. if ( aSig & UINT64_C(0x0020000000000000) ) {
  4365. aSig >>= 1;
  4366. zSig |= i;
  4367. }
  4368. }
  4369. if ( zSign )
  4370. zSig = -zSig;
  4371. return normalizeRoundAndPackFloat64(zSign, 0x408, zSig, status);
  4372. }
  4373. /*----------------------------------------------------------------------------
  4374. | Returns the result of converting the extended double-precision floating-
  4375. | point value `a' to the 32-bit two's complement integer format. The
  4376. | conversion is performed according to the IEC/IEEE Standard for Binary
  4377. | Floating-Point Arithmetic---which means in particular that the conversion
  4378. | is rounded according to the current rounding mode. If `a' is a NaN, the
  4379. | largest positive integer is returned. Otherwise, if the conversion
  4380. | overflows, the largest integer with the same sign as `a' is returned.
  4381. *----------------------------------------------------------------------------*/
  4382. int32_t floatx80_to_int32(floatx80 a, float_status *status)
  4383. {
  4384. bool aSign;
  4385. int32_t aExp, shiftCount;
  4386. uint64_t aSig;
  4387. if (floatx80_invalid_encoding(a)) {
  4388. float_raise(float_flag_invalid, status);
  4389. return 1 << 31;
  4390. }
  4391. aSig = extractFloatx80Frac( a );
  4392. aExp = extractFloatx80Exp( a );
  4393. aSign = extractFloatx80Sign( a );
  4394. if ( ( aExp == 0x7FFF ) && (uint64_t) ( aSig<<1 ) ) aSign = 0;
  4395. shiftCount = 0x4037 - aExp;
  4396. if ( shiftCount <= 0 ) shiftCount = 1;
  4397. shift64RightJamming( aSig, shiftCount, &aSig );
  4398. return roundAndPackInt32(aSign, aSig, status);
  4399. }
  4400. /*----------------------------------------------------------------------------
  4401. | Returns the result of converting the extended double-precision floating-
  4402. | point value `a' to the 32-bit two's complement integer format. The
  4403. | conversion is performed according to the IEC/IEEE Standard for Binary
  4404. | Floating-Point Arithmetic, except that the conversion is always rounded
  4405. | toward zero. If `a' is a NaN, the largest positive integer is returned.
  4406. | Otherwise, if the conversion overflows, the largest integer with the same
  4407. | sign as `a' is returned.
  4408. *----------------------------------------------------------------------------*/
  4409. int32_t floatx80_to_int32_round_to_zero(floatx80 a, float_status *status)
  4410. {
  4411. bool aSign;
  4412. int32_t aExp, shiftCount;
  4413. uint64_t aSig, savedASig;
  4414. int32_t z;
  4415. if (floatx80_invalid_encoding(a)) {
  4416. float_raise(float_flag_invalid, status);
  4417. return 1 << 31;
  4418. }
  4419. aSig = extractFloatx80Frac( a );
  4420. aExp = extractFloatx80Exp( a );
  4421. aSign = extractFloatx80Sign( a );
  4422. if ( 0x401E < aExp ) {
  4423. if ( ( aExp == 0x7FFF ) && (uint64_t) ( aSig<<1 ) ) aSign = 0;
  4424. goto invalid;
  4425. }
  4426. else if ( aExp < 0x3FFF ) {
  4427. if (aExp || aSig) {
  4428. status->float_exception_flags |= float_flag_inexact;
  4429. }
  4430. return 0;
  4431. }
  4432. shiftCount = 0x403E - aExp;
  4433. savedASig = aSig;
  4434. aSig >>= shiftCount;
  4435. z = aSig;
  4436. if ( aSign ) z = - z;
  4437. if ( ( z < 0 ) ^ aSign ) {
  4438. invalid:
  4439. float_raise(float_flag_invalid, status);
  4440. return aSign ? (int32_t) 0x80000000 : 0x7FFFFFFF;
  4441. }
  4442. if ( ( aSig<<shiftCount ) != savedASig ) {
  4443. status->float_exception_flags |= float_flag_inexact;
  4444. }
  4445. return z;
  4446. }
  4447. /*----------------------------------------------------------------------------
  4448. | Returns the result of converting the extended double-precision floating-
  4449. | point value `a' to the 64-bit two's complement integer format. The
  4450. | conversion is performed according to the IEC/IEEE Standard for Binary
  4451. | Floating-Point Arithmetic---which means in particular that the conversion
  4452. | is rounded according to the current rounding mode. If `a' is a NaN,
  4453. | the largest positive integer is returned. Otherwise, if the conversion
  4454. | overflows, the largest integer with the same sign as `a' is returned.
  4455. *----------------------------------------------------------------------------*/
  4456. int64_t floatx80_to_int64(floatx80 a, float_status *status)
  4457. {
  4458. bool aSign;
  4459. int32_t aExp, shiftCount;
  4460. uint64_t aSig, aSigExtra;
  4461. if (floatx80_invalid_encoding(a)) {
  4462. float_raise(float_flag_invalid, status);
  4463. return 1ULL << 63;
  4464. }
  4465. aSig = extractFloatx80Frac( a );
  4466. aExp = extractFloatx80Exp( a );
  4467. aSign = extractFloatx80Sign( a );
  4468. shiftCount = 0x403E - aExp;
  4469. if ( shiftCount <= 0 ) {
  4470. if ( shiftCount ) {
  4471. float_raise(float_flag_invalid, status);
  4472. if (!aSign || floatx80_is_any_nan(a)) {
  4473. return INT64_MAX;
  4474. }
  4475. return INT64_MIN;
  4476. }
  4477. aSigExtra = 0;
  4478. }
  4479. else {
  4480. shift64ExtraRightJamming( aSig, 0, shiftCount, &aSig, &aSigExtra );
  4481. }
  4482. return roundAndPackInt64(aSign, aSig, aSigExtra, status);
  4483. }
  4484. /*----------------------------------------------------------------------------
  4485. | Returns the result of converting the extended double-precision floating-
  4486. | point value `a' to the 64-bit two's complement integer format. The
  4487. | conversion is performed according to the IEC/IEEE Standard for Binary
  4488. | Floating-Point Arithmetic, except that the conversion is always rounded
  4489. | toward zero. If `a' is a NaN, the largest positive integer is returned.
  4490. | Otherwise, if the conversion overflows, the largest integer with the same
  4491. | sign as `a' is returned.
  4492. *----------------------------------------------------------------------------*/
  4493. int64_t floatx80_to_int64_round_to_zero(floatx80 a, float_status *status)
  4494. {
  4495. bool aSign;
  4496. int32_t aExp, shiftCount;
  4497. uint64_t aSig;
  4498. int64_t z;
  4499. if (floatx80_invalid_encoding(a)) {
  4500. float_raise(float_flag_invalid, status);
  4501. return 1ULL << 63;
  4502. }
  4503. aSig = extractFloatx80Frac( a );
  4504. aExp = extractFloatx80Exp( a );
  4505. aSign = extractFloatx80Sign( a );
  4506. shiftCount = aExp - 0x403E;
  4507. if ( 0 <= shiftCount ) {
  4508. aSig &= UINT64_C(0x7FFFFFFFFFFFFFFF);
  4509. if ( ( a.high != 0xC03E ) || aSig ) {
  4510. float_raise(float_flag_invalid, status);
  4511. if ( ! aSign || ( ( aExp == 0x7FFF ) && aSig ) ) {
  4512. return INT64_MAX;
  4513. }
  4514. }
  4515. return INT64_MIN;
  4516. }
  4517. else if ( aExp < 0x3FFF ) {
  4518. if (aExp | aSig) {
  4519. status->float_exception_flags |= float_flag_inexact;
  4520. }
  4521. return 0;
  4522. }
  4523. z = aSig>>( - shiftCount );
  4524. if ( (uint64_t) ( aSig<<( shiftCount & 63 ) ) ) {
  4525. status->float_exception_flags |= float_flag_inexact;
  4526. }
  4527. if ( aSign ) z = - z;
  4528. return z;
  4529. }
  4530. /*----------------------------------------------------------------------------
  4531. | Returns the result of converting the extended double-precision floating-
  4532. | point value `a' to the single-precision floating-point format. The
  4533. | conversion is performed according to the IEC/IEEE Standard for Binary
  4534. | Floating-Point Arithmetic.
  4535. *----------------------------------------------------------------------------*/
  4536. float32 floatx80_to_float32(floatx80 a, float_status *status)
  4537. {
  4538. bool aSign;
  4539. int32_t aExp;
  4540. uint64_t aSig;
  4541. if (floatx80_invalid_encoding(a)) {
  4542. float_raise(float_flag_invalid, status);
  4543. return float32_default_nan(status);
  4544. }
  4545. aSig = extractFloatx80Frac( a );
  4546. aExp = extractFloatx80Exp( a );
  4547. aSign = extractFloatx80Sign( a );
  4548. if ( aExp == 0x7FFF ) {
  4549. if ( (uint64_t) ( aSig<<1 ) ) {
  4550. float32 res = commonNaNToFloat32(floatx80ToCommonNaN(a, status),
  4551. status);
  4552. return float32_silence_nan(res, status);
  4553. }
  4554. return packFloat32( aSign, 0xFF, 0 );
  4555. }
  4556. shift64RightJamming( aSig, 33, &aSig );
  4557. if ( aExp || aSig ) aExp -= 0x3F81;
  4558. return roundAndPackFloat32(aSign, aExp, aSig, status);
  4559. }
  4560. /*----------------------------------------------------------------------------
  4561. | Returns the result of converting the extended double-precision floating-
  4562. | point value `a' to the double-precision floating-point format. The
  4563. | conversion is performed according to the IEC/IEEE Standard for Binary
  4564. | Floating-Point Arithmetic.
  4565. *----------------------------------------------------------------------------*/
  4566. float64 floatx80_to_float64(floatx80 a, float_status *status)
  4567. {
  4568. bool aSign;
  4569. int32_t aExp;
  4570. uint64_t aSig, zSig;
  4571. if (floatx80_invalid_encoding(a)) {
  4572. float_raise(float_flag_invalid, status);
  4573. return float64_default_nan(status);
  4574. }
  4575. aSig = extractFloatx80Frac( a );
  4576. aExp = extractFloatx80Exp( a );
  4577. aSign = extractFloatx80Sign( a );
  4578. if ( aExp == 0x7FFF ) {
  4579. if ( (uint64_t) ( aSig<<1 ) ) {
  4580. float64 res = commonNaNToFloat64(floatx80ToCommonNaN(a, status),
  4581. status);
  4582. return float64_silence_nan(res, status);
  4583. }
  4584. return packFloat64( aSign, 0x7FF, 0 );
  4585. }
  4586. shift64RightJamming( aSig, 1, &zSig );
  4587. if ( aExp || aSig ) aExp -= 0x3C01;
  4588. return roundAndPackFloat64(aSign, aExp, zSig, status);
  4589. }
  4590. /*----------------------------------------------------------------------------
  4591. | Returns the result of converting the extended double-precision floating-
  4592. | point value `a' to the quadruple-precision floating-point format. The
  4593. | conversion is performed according to the IEC/IEEE Standard for Binary
  4594. | Floating-Point Arithmetic.
  4595. *----------------------------------------------------------------------------*/
  4596. float128 floatx80_to_float128(floatx80 a, float_status *status)
  4597. {
  4598. bool aSign;
  4599. int aExp;
  4600. uint64_t aSig, zSig0, zSig1;
  4601. if (floatx80_invalid_encoding(a)) {
  4602. float_raise(float_flag_invalid, status);
  4603. return float128_default_nan(status);
  4604. }
  4605. aSig = extractFloatx80Frac( a );
  4606. aExp = extractFloatx80Exp( a );
  4607. aSign = extractFloatx80Sign( a );
  4608. if ( ( aExp == 0x7FFF ) && (uint64_t) ( aSig<<1 ) ) {
  4609. float128 res = commonNaNToFloat128(floatx80ToCommonNaN(a, status),
  4610. status);
  4611. return float128_silence_nan(res, status);
  4612. }
  4613. shift128Right( aSig<<1, 0, 16, &zSig0, &zSig1 );
  4614. return packFloat128( aSign, aExp, zSig0, zSig1 );
  4615. }
  4616. /*----------------------------------------------------------------------------
  4617. | Rounds the extended double-precision floating-point value `a'
  4618. | to the precision provided by floatx80_rounding_precision and returns the
  4619. | result as an extended double-precision floating-point value.
  4620. | The operation is performed according to the IEC/IEEE Standard for Binary
  4621. | Floating-Point Arithmetic.
  4622. *----------------------------------------------------------------------------*/
  4623. floatx80 floatx80_round(floatx80 a, float_status *status)
  4624. {
  4625. return roundAndPackFloatx80(status->floatx80_rounding_precision,
  4626. extractFloatx80Sign(a),
  4627. extractFloatx80Exp(a),
  4628. extractFloatx80Frac(a), 0, status);
  4629. }
  4630. /*----------------------------------------------------------------------------
  4631. | Rounds the extended double-precision floating-point value `a' to an integer,
  4632. | and returns the result as an extended quadruple-precision floating-point
  4633. | value. The operation is performed according to the IEC/IEEE Standard for
  4634. | Binary Floating-Point Arithmetic.
  4635. *----------------------------------------------------------------------------*/
  4636. floatx80 floatx80_round_to_int(floatx80 a, float_status *status)
  4637. {
  4638. bool aSign;
  4639. int32_t aExp;
  4640. uint64_t lastBitMask, roundBitsMask;
  4641. floatx80 z;
  4642. if (floatx80_invalid_encoding(a)) {
  4643. float_raise(float_flag_invalid, status);
  4644. return floatx80_default_nan(status);
  4645. }
  4646. aExp = extractFloatx80Exp( a );
  4647. if ( 0x403E <= aExp ) {
  4648. if ( ( aExp == 0x7FFF ) && (uint64_t) ( extractFloatx80Frac( a )<<1 ) ) {
  4649. return propagateFloatx80NaN(a, a, status);
  4650. }
  4651. return a;
  4652. }
  4653. if ( aExp < 0x3FFF ) {
  4654. if ( ( aExp == 0 )
  4655. && ( (uint64_t) ( extractFloatx80Frac( a ) ) == 0 ) ) {
  4656. return a;
  4657. }
  4658. status->float_exception_flags |= float_flag_inexact;
  4659. aSign = extractFloatx80Sign( a );
  4660. switch (status->float_rounding_mode) {
  4661. case float_round_nearest_even:
  4662. if ( ( aExp == 0x3FFE ) && (uint64_t) ( extractFloatx80Frac( a )<<1 )
  4663. ) {
  4664. return
  4665. packFloatx80( aSign, 0x3FFF, UINT64_C(0x8000000000000000));
  4666. }
  4667. break;
  4668. case float_round_ties_away:
  4669. if (aExp == 0x3FFE) {
  4670. return packFloatx80(aSign, 0x3FFF, UINT64_C(0x8000000000000000));
  4671. }
  4672. break;
  4673. case float_round_down:
  4674. return
  4675. aSign ?
  4676. packFloatx80( 1, 0x3FFF, UINT64_C(0x8000000000000000))
  4677. : packFloatx80( 0, 0, 0 );
  4678. case float_round_up:
  4679. return
  4680. aSign ? packFloatx80( 1, 0, 0 )
  4681. : packFloatx80( 0, 0x3FFF, UINT64_C(0x8000000000000000));
  4682. case float_round_to_zero:
  4683. break;
  4684. default:
  4685. g_assert_not_reached();
  4686. }
  4687. return packFloatx80( aSign, 0, 0 );
  4688. }
  4689. lastBitMask = 1;
  4690. lastBitMask <<= 0x403E - aExp;
  4691. roundBitsMask = lastBitMask - 1;
  4692. z = a;
  4693. switch (status->float_rounding_mode) {
  4694. case float_round_nearest_even:
  4695. z.low += lastBitMask>>1;
  4696. if ((z.low & roundBitsMask) == 0) {
  4697. z.low &= ~lastBitMask;
  4698. }
  4699. break;
  4700. case float_round_ties_away:
  4701. z.low += lastBitMask >> 1;
  4702. break;
  4703. case float_round_to_zero:
  4704. break;
  4705. case float_round_up:
  4706. if (!extractFloatx80Sign(z)) {
  4707. z.low += roundBitsMask;
  4708. }
  4709. break;
  4710. case float_round_down:
  4711. if (extractFloatx80Sign(z)) {
  4712. z.low += roundBitsMask;
  4713. }
  4714. break;
  4715. default:
  4716. abort();
  4717. }
  4718. z.low &= ~ roundBitsMask;
  4719. if ( z.low == 0 ) {
  4720. ++z.high;
  4721. z.low = UINT64_C(0x8000000000000000);
  4722. }
  4723. if (z.low != a.low) {
  4724. status->float_exception_flags |= float_flag_inexact;
  4725. }
  4726. return z;
  4727. }
  4728. /*----------------------------------------------------------------------------
  4729. | Returns the result of adding the absolute values of the extended double-
  4730. | precision floating-point values `a' and `b'. If `zSign' is 1, the sum is
  4731. | negated before being returned. `zSign' is ignored if the result is a NaN.
  4732. | The addition is performed according to the IEC/IEEE Standard for Binary
  4733. | Floating-Point Arithmetic.
  4734. *----------------------------------------------------------------------------*/
  4735. static floatx80 addFloatx80Sigs(floatx80 a, floatx80 b, bool zSign,
  4736. float_status *status)
  4737. {
  4738. int32_t aExp, bExp, zExp;
  4739. uint64_t aSig, bSig, zSig0, zSig1;
  4740. int32_t expDiff;
  4741. aSig = extractFloatx80Frac( a );
  4742. aExp = extractFloatx80Exp( a );
  4743. bSig = extractFloatx80Frac( b );
  4744. bExp = extractFloatx80Exp( b );
  4745. expDiff = aExp - bExp;
  4746. if ( 0 < expDiff ) {
  4747. if ( aExp == 0x7FFF ) {
  4748. if ((uint64_t)(aSig << 1)) {
  4749. return propagateFloatx80NaN(a, b, status);
  4750. }
  4751. return a;
  4752. }
  4753. if ( bExp == 0 ) --expDiff;
  4754. shift64ExtraRightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  4755. zExp = aExp;
  4756. }
  4757. else if ( expDiff < 0 ) {
  4758. if ( bExp == 0x7FFF ) {
  4759. if ((uint64_t)(bSig << 1)) {
  4760. return propagateFloatx80NaN(a, b, status);
  4761. }
  4762. return packFloatx80(zSign,
  4763. floatx80_infinity_high,
  4764. floatx80_infinity_low);
  4765. }
  4766. if ( aExp == 0 ) ++expDiff;
  4767. shift64ExtraRightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  4768. zExp = bExp;
  4769. }
  4770. else {
  4771. if ( aExp == 0x7FFF ) {
  4772. if ( (uint64_t) ( ( aSig | bSig )<<1 ) ) {
  4773. return propagateFloatx80NaN(a, b, status);
  4774. }
  4775. return a;
  4776. }
  4777. zSig1 = 0;
  4778. zSig0 = aSig + bSig;
  4779. if ( aExp == 0 ) {
  4780. if ((aSig | bSig) & UINT64_C(0x8000000000000000) && zSig0 < aSig) {
  4781. /* At least one of the values is a pseudo-denormal,
  4782. * and there is a carry out of the result. */
  4783. zExp = 1;
  4784. goto shiftRight1;
  4785. }
  4786. if (zSig0 == 0) {
  4787. return packFloatx80(zSign, 0, 0);
  4788. }
  4789. normalizeFloatx80Subnormal( zSig0, &zExp, &zSig0 );
  4790. goto roundAndPack;
  4791. }
  4792. zExp = aExp;
  4793. goto shiftRight1;
  4794. }
  4795. zSig0 = aSig + bSig;
  4796. if ( (int64_t) zSig0 < 0 ) goto roundAndPack;
  4797. shiftRight1:
  4798. shift64ExtraRightJamming( zSig0, zSig1, 1, &zSig0, &zSig1 );
  4799. zSig0 |= UINT64_C(0x8000000000000000);
  4800. ++zExp;
  4801. roundAndPack:
  4802. return roundAndPackFloatx80(status->floatx80_rounding_precision,
  4803. zSign, zExp, zSig0, zSig1, status);
  4804. }
  4805. /*----------------------------------------------------------------------------
  4806. | Returns the result of subtracting the absolute values of the extended
  4807. | double-precision floating-point values `a' and `b'. If `zSign' is 1, the
  4808. | difference is negated before being returned. `zSign' is ignored if the
  4809. | result is a NaN. The subtraction is performed according to the IEC/IEEE
  4810. | Standard for Binary Floating-Point Arithmetic.
  4811. *----------------------------------------------------------------------------*/
  4812. static floatx80 subFloatx80Sigs(floatx80 a, floatx80 b, bool zSign,
  4813. float_status *status)
  4814. {
  4815. int32_t aExp, bExp, zExp;
  4816. uint64_t aSig, bSig, zSig0, zSig1;
  4817. int32_t expDiff;
  4818. aSig = extractFloatx80Frac( a );
  4819. aExp = extractFloatx80Exp( a );
  4820. bSig = extractFloatx80Frac( b );
  4821. bExp = extractFloatx80Exp( b );
  4822. expDiff = aExp - bExp;
  4823. if ( 0 < expDiff ) goto aExpBigger;
  4824. if ( expDiff < 0 ) goto bExpBigger;
  4825. if ( aExp == 0x7FFF ) {
  4826. if ( (uint64_t) ( ( aSig | bSig )<<1 ) ) {
  4827. return propagateFloatx80NaN(a, b, status);
  4828. }
  4829. float_raise(float_flag_invalid, status);
  4830. return floatx80_default_nan(status);
  4831. }
  4832. if ( aExp == 0 ) {
  4833. aExp = 1;
  4834. bExp = 1;
  4835. }
  4836. zSig1 = 0;
  4837. if ( bSig < aSig ) goto aBigger;
  4838. if ( aSig < bSig ) goto bBigger;
  4839. return packFloatx80(status->float_rounding_mode == float_round_down, 0, 0);
  4840. bExpBigger:
  4841. if ( bExp == 0x7FFF ) {
  4842. if ((uint64_t)(bSig << 1)) {
  4843. return propagateFloatx80NaN(a, b, status);
  4844. }
  4845. return packFloatx80(zSign ^ 1, floatx80_infinity_high,
  4846. floatx80_infinity_low);
  4847. }
  4848. if ( aExp == 0 ) ++expDiff;
  4849. shift128RightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  4850. bBigger:
  4851. sub128( bSig, 0, aSig, zSig1, &zSig0, &zSig1 );
  4852. zExp = bExp;
  4853. zSign ^= 1;
  4854. goto normalizeRoundAndPack;
  4855. aExpBigger:
  4856. if ( aExp == 0x7FFF ) {
  4857. if ((uint64_t)(aSig << 1)) {
  4858. return propagateFloatx80NaN(a, b, status);
  4859. }
  4860. return a;
  4861. }
  4862. if ( bExp == 0 ) --expDiff;
  4863. shift128RightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  4864. aBigger:
  4865. sub128( aSig, 0, bSig, zSig1, &zSig0, &zSig1 );
  4866. zExp = aExp;
  4867. normalizeRoundAndPack:
  4868. return normalizeRoundAndPackFloatx80(status->floatx80_rounding_precision,
  4869. zSign, zExp, zSig0, zSig1, status);
  4870. }
  4871. /*----------------------------------------------------------------------------
  4872. | Returns the result of adding the extended double-precision floating-point
  4873. | values `a' and `b'. The operation is performed according to the IEC/IEEE
  4874. | Standard for Binary Floating-Point Arithmetic.
  4875. *----------------------------------------------------------------------------*/
  4876. floatx80 floatx80_add(floatx80 a, floatx80 b, float_status *status)
  4877. {
  4878. bool aSign, bSign;
  4879. if (floatx80_invalid_encoding(a) || floatx80_invalid_encoding(b)) {
  4880. float_raise(float_flag_invalid, status);
  4881. return floatx80_default_nan(status);
  4882. }
  4883. aSign = extractFloatx80Sign( a );
  4884. bSign = extractFloatx80Sign( b );
  4885. if ( aSign == bSign ) {
  4886. return addFloatx80Sigs(a, b, aSign, status);
  4887. }
  4888. else {
  4889. return subFloatx80Sigs(a, b, aSign, status);
  4890. }
  4891. }
  4892. /*----------------------------------------------------------------------------
  4893. | Returns the result of subtracting the extended double-precision floating-
  4894. | point values `a' and `b'. The operation is performed according to the
  4895. | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4896. *----------------------------------------------------------------------------*/
  4897. floatx80 floatx80_sub(floatx80 a, floatx80 b, float_status *status)
  4898. {
  4899. bool aSign, bSign;
  4900. if (floatx80_invalid_encoding(a) || floatx80_invalid_encoding(b)) {
  4901. float_raise(float_flag_invalid, status);
  4902. return floatx80_default_nan(status);
  4903. }
  4904. aSign = extractFloatx80Sign( a );
  4905. bSign = extractFloatx80Sign( b );
  4906. if ( aSign == bSign ) {
  4907. return subFloatx80Sigs(a, b, aSign, status);
  4908. }
  4909. else {
  4910. return addFloatx80Sigs(a, b, aSign, status);
  4911. }
  4912. }
  4913. /*----------------------------------------------------------------------------
  4914. | Returns the result of multiplying the extended double-precision floating-
  4915. | point values `a' and `b'. The operation is performed according to the
  4916. | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4917. *----------------------------------------------------------------------------*/
  4918. floatx80 floatx80_mul(floatx80 a, floatx80 b, float_status *status)
  4919. {
  4920. bool aSign, bSign, zSign;
  4921. int32_t aExp, bExp, zExp;
  4922. uint64_t aSig, bSig, zSig0, zSig1;
  4923. if (floatx80_invalid_encoding(a) || floatx80_invalid_encoding(b)) {
  4924. float_raise(float_flag_invalid, status);
  4925. return floatx80_default_nan(status);
  4926. }
  4927. aSig = extractFloatx80Frac( a );
  4928. aExp = extractFloatx80Exp( a );
  4929. aSign = extractFloatx80Sign( a );
  4930. bSig = extractFloatx80Frac( b );
  4931. bExp = extractFloatx80Exp( b );
  4932. bSign = extractFloatx80Sign( b );
  4933. zSign = aSign ^ bSign;
  4934. if ( aExp == 0x7FFF ) {
  4935. if ( (uint64_t) ( aSig<<1 )
  4936. || ( ( bExp == 0x7FFF ) && (uint64_t) ( bSig<<1 ) ) ) {
  4937. return propagateFloatx80NaN(a, b, status);
  4938. }
  4939. if ( ( bExp | bSig ) == 0 ) goto invalid;
  4940. return packFloatx80(zSign, floatx80_infinity_high,
  4941. floatx80_infinity_low);
  4942. }
  4943. if ( bExp == 0x7FFF ) {
  4944. if ((uint64_t)(bSig << 1)) {
  4945. return propagateFloatx80NaN(a, b, status);
  4946. }
  4947. if ( ( aExp | aSig ) == 0 ) {
  4948. invalid:
  4949. float_raise(float_flag_invalid, status);
  4950. return floatx80_default_nan(status);
  4951. }
  4952. return packFloatx80(zSign, floatx80_infinity_high,
  4953. floatx80_infinity_low);
  4954. }
  4955. if ( aExp == 0 ) {
  4956. if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
  4957. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  4958. }
  4959. if ( bExp == 0 ) {
  4960. if ( bSig == 0 ) return packFloatx80( zSign, 0, 0 );
  4961. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  4962. }
  4963. zExp = aExp + bExp - 0x3FFE;
  4964. mul64To128( aSig, bSig, &zSig0, &zSig1 );
  4965. if ( 0 < (int64_t) zSig0 ) {
  4966. shortShift128Left( zSig0, zSig1, 1, &zSig0, &zSig1 );
  4967. --zExp;
  4968. }
  4969. return roundAndPackFloatx80(status->floatx80_rounding_precision,
  4970. zSign, zExp, zSig0, zSig1, status);
  4971. }
  4972. /*----------------------------------------------------------------------------
  4973. | Returns the result of dividing the extended double-precision floating-point
  4974. | value `a' by the corresponding value `b'. The operation is performed
  4975. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4976. *----------------------------------------------------------------------------*/
  4977. floatx80 floatx80_div(floatx80 a, floatx80 b, float_status *status)
  4978. {
  4979. bool aSign, bSign, zSign;
  4980. int32_t aExp, bExp, zExp;
  4981. uint64_t aSig, bSig, zSig0, zSig1;
  4982. uint64_t rem0, rem1, rem2, term0, term1, term2;
  4983. if (floatx80_invalid_encoding(a) || floatx80_invalid_encoding(b)) {
  4984. float_raise(float_flag_invalid, status);
  4985. return floatx80_default_nan(status);
  4986. }
  4987. aSig = extractFloatx80Frac( a );
  4988. aExp = extractFloatx80Exp( a );
  4989. aSign = extractFloatx80Sign( a );
  4990. bSig = extractFloatx80Frac( b );
  4991. bExp = extractFloatx80Exp( b );
  4992. bSign = extractFloatx80Sign( b );
  4993. zSign = aSign ^ bSign;
  4994. if ( aExp == 0x7FFF ) {
  4995. if ((uint64_t)(aSig << 1)) {
  4996. return propagateFloatx80NaN(a, b, status);
  4997. }
  4998. if ( bExp == 0x7FFF ) {
  4999. if ((uint64_t)(bSig << 1)) {
  5000. return propagateFloatx80NaN(a, b, status);
  5001. }
  5002. goto invalid;
  5003. }
  5004. return packFloatx80(zSign, floatx80_infinity_high,
  5005. floatx80_infinity_low);
  5006. }
  5007. if ( bExp == 0x7FFF ) {
  5008. if ((uint64_t)(bSig << 1)) {
  5009. return propagateFloatx80NaN(a, b, status);
  5010. }
  5011. return packFloatx80( zSign, 0, 0 );
  5012. }
  5013. if ( bExp == 0 ) {
  5014. if ( bSig == 0 ) {
  5015. if ( ( aExp | aSig ) == 0 ) {
  5016. invalid:
  5017. float_raise(float_flag_invalid, status);
  5018. return floatx80_default_nan(status);
  5019. }
  5020. float_raise(float_flag_divbyzero, status);
  5021. return packFloatx80(zSign, floatx80_infinity_high,
  5022. floatx80_infinity_low);
  5023. }
  5024. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  5025. }
  5026. if ( aExp == 0 ) {
  5027. if ( aSig == 0 ) return packFloatx80( zSign, 0, 0 );
  5028. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  5029. }
  5030. zExp = aExp - bExp + 0x3FFE;
  5031. rem1 = 0;
  5032. if ( bSig <= aSig ) {
  5033. shift128Right( aSig, 0, 1, &aSig, &rem1 );
  5034. ++zExp;
  5035. }
  5036. zSig0 = estimateDiv128To64( aSig, rem1, bSig );
  5037. mul64To128( bSig, zSig0, &term0, &term1 );
  5038. sub128( aSig, rem1, term0, term1, &rem0, &rem1 );
  5039. while ( (int64_t) rem0 < 0 ) {
  5040. --zSig0;
  5041. add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
  5042. }
  5043. zSig1 = estimateDiv128To64( rem1, 0, bSig );
  5044. if ( (uint64_t) ( zSig1<<1 ) <= 8 ) {
  5045. mul64To128( bSig, zSig1, &term1, &term2 );
  5046. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  5047. while ( (int64_t) rem1 < 0 ) {
  5048. --zSig1;
  5049. add128( rem1, rem2, 0, bSig, &rem1, &rem2 );
  5050. }
  5051. zSig1 |= ( ( rem1 | rem2 ) != 0 );
  5052. }
  5053. return roundAndPackFloatx80(status->floatx80_rounding_precision,
  5054. zSign, zExp, zSig0, zSig1, status);
  5055. }
  5056. /*----------------------------------------------------------------------------
  5057. | Returns the remainder of the extended double-precision floating-point value
  5058. | `a' with respect to the corresponding value `b'. The operation is performed
  5059. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic,
  5060. | if 'mod' is false; if 'mod' is true, return the remainder based on truncating
  5061. | the quotient toward zero instead. '*quotient' is set to the low 64 bits of
  5062. | the absolute value of the integer quotient.
  5063. *----------------------------------------------------------------------------*/
  5064. floatx80 floatx80_modrem(floatx80 a, floatx80 b, bool mod, uint64_t *quotient,
  5065. float_status *status)
  5066. {
  5067. bool aSign, zSign;
  5068. int32_t aExp, bExp, expDiff, aExpOrig;
  5069. uint64_t aSig0, aSig1, bSig;
  5070. uint64_t q, term0, term1, alternateASig0, alternateASig1;
  5071. *quotient = 0;
  5072. if (floatx80_invalid_encoding(a) || floatx80_invalid_encoding(b)) {
  5073. float_raise(float_flag_invalid, status);
  5074. return floatx80_default_nan(status);
  5075. }
  5076. aSig0 = extractFloatx80Frac( a );
  5077. aExpOrig = aExp = extractFloatx80Exp( a );
  5078. aSign = extractFloatx80Sign( a );
  5079. bSig = extractFloatx80Frac( b );
  5080. bExp = extractFloatx80Exp( b );
  5081. if ( aExp == 0x7FFF ) {
  5082. if ( (uint64_t) ( aSig0<<1 )
  5083. || ( ( bExp == 0x7FFF ) && (uint64_t) ( bSig<<1 ) ) ) {
  5084. return propagateFloatx80NaN(a, b, status);
  5085. }
  5086. goto invalid;
  5087. }
  5088. if ( bExp == 0x7FFF ) {
  5089. if ((uint64_t)(bSig << 1)) {
  5090. return propagateFloatx80NaN(a, b, status);
  5091. }
  5092. if (aExp == 0 && aSig0 >> 63) {
  5093. /*
  5094. * Pseudo-denormal argument must be returned in normalized
  5095. * form.
  5096. */
  5097. return packFloatx80(aSign, 1, aSig0);
  5098. }
  5099. return a;
  5100. }
  5101. if ( bExp == 0 ) {
  5102. if ( bSig == 0 ) {
  5103. invalid:
  5104. float_raise(float_flag_invalid, status);
  5105. return floatx80_default_nan(status);
  5106. }
  5107. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  5108. }
  5109. if ( aExp == 0 ) {
  5110. if ( aSig0 == 0 ) return a;
  5111. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  5112. }
  5113. zSign = aSign;
  5114. expDiff = aExp - bExp;
  5115. aSig1 = 0;
  5116. if ( expDiff < 0 ) {
  5117. if ( mod || expDiff < -1 ) {
  5118. if (aExp == 1 && aExpOrig == 0) {
  5119. /*
  5120. * Pseudo-denormal argument must be returned in
  5121. * normalized form.
  5122. */
  5123. return packFloatx80(aSign, aExp, aSig0);
  5124. }
  5125. return a;
  5126. }
  5127. shift128Right( aSig0, 0, 1, &aSig0, &aSig1 );
  5128. expDiff = 0;
  5129. }
  5130. *quotient = q = ( bSig <= aSig0 );
  5131. if ( q ) aSig0 -= bSig;
  5132. expDiff -= 64;
  5133. while ( 0 < expDiff ) {
  5134. q = estimateDiv128To64( aSig0, aSig1, bSig );
  5135. q = ( 2 < q ) ? q - 2 : 0;
  5136. mul64To128( bSig, q, &term0, &term1 );
  5137. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  5138. shortShift128Left( aSig0, aSig1, 62, &aSig0, &aSig1 );
  5139. expDiff -= 62;
  5140. *quotient <<= 62;
  5141. *quotient += q;
  5142. }
  5143. expDiff += 64;
  5144. if ( 0 < expDiff ) {
  5145. q = estimateDiv128To64( aSig0, aSig1, bSig );
  5146. q = ( 2 < q ) ? q - 2 : 0;
  5147. q >>= 64 - expDiff;
  5148. mul64To128( bSig, q<<( 64 - expDiff ), &term0, &term1 );
  5149. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  5150. shortShift128Left( 0, bSig, 64 - expDiff, &term0, &term1 );
  5151. while ( le128( term0, term1, aSig0, aSig1 ) ) {
  5152. ++q;
  5153. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  5154. }
  5155. if (expDiff < 64) {
  5156. *quotient <<= expDiff;
  5157. } else {
  5158. *quotient = 0;
  5159. }
  5160. *quotient += q;
  5161. }
  5162. else {
  5163. term1 = 0;
  5164. term0 = bSig;
  5165. }
  5166. if (!mod) {
  5167. sub128( term0, term1, aSig0, aSig1, &alternateASig0, &alternateASig1 );
  5168. if ( lt128( alternateASig0, alternateASig1, aSig0, aSig1 )
  5169. || ( eq128( alternateASig0, alternateASig1, aSig0, aSig1 )
  5170. && ( q & 1 ) )
  5171. ) {
  5172. aSig0 = alternateASig0;
  5173. aSig1 = alternateASig1;
  5174. zSign = ! zSign;
  5175. ++*quotient;
  5176. }
  5177. }
  5178. return
  5179. normalizeRoundAndPackFloatx80(
  5180. 80, zSign, bExp + expDiff, aSig0, aSig1, status);
  5181. }
  5182. /*----------------------------------------------------------------------------
  5183. | Returns the remainder of the extended double-precision floating-point value
  5184. | `a' with respect to the corresponding value `b'. The operation is performed
  5185. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  5186. *----------------------------------------------------------------------------*/
  5187. floatx80 floatx80_rem(floatx80 a, floatx80 b, float_status *status)
  5188. {
  5189. uint64_t quotient;
  5190. return floatx80_modrem(a, b, false, &quotient, status);
  5191. }
  5192. /*----------------------------------------------------------------------------
  5193. | Returns the remainder of the extended double-precision floating-point value
  5194. | `a' with respect to the corresponding value `b', with the quotient truncated
  5195. | toward zero.
  5196. *----------------------------------------------------------------------------*/
  5197. floatx80 floatx80_mod(floatx80 a, floatx80 b, float_status *status)
  5198. {
  5199. uint64_t quotient;
  5200. return floatx80_modrem(a, b, true, &quotient, status);
  5201. }
  5202. /*----------------------------------------------------------------------------
  5203. | Returns the square root of the extended double-precision floating-point
  5204. | value `a'. The operation is performed according to the IEC/IEEE Standard
  5205. | for Binary Floating-Point Arithmetic.
  5206. *----------------------------------------------------------------------------*/
  5207. floatx80 floatx80_sqrt(floatx80 a, float_status *status)
  5208. {
  5209. bool aSign;
  5210. int32_t aExp, zExp;
  5211. uint64_t aSig0, aSig1, zSig0, zSig1, doubleZSig0;
  5212. uint64_t rem0, rem1, rem2, rem3, term0, term1, term2, term3;
  5213. if (floatx80_invalid_encoding(a)) {
  5214. float_raise(float_flag_invalid, status);
  5215. return floatx80_default_nan(status);
  5216. }
  5217. aSig0 = extractFloatx80Frac( a );
  5218. aExp = extractFloatx80Exp( a );
  5219. aSign = extractFloatx80Sign( a );
  5220. if ( aExp == 0x7FFF ) {
  5221. if ((uint64_t)(aSig0 << 1)) {
  5222. return propagateFloatx80NaN(a, a, status);
  5223. }
  5224. if ( ! aSign ) return a;
  5225. goto invalid;
  5226. }
  5227. if ( aSign ) {
  5228. if ( ( aExp | aSig0 ) == 0 ) return a;
  5229. invalid:
  5230. float_raise(float_flag_invalid, status);
  5231. return floatx80_default_nan(status);
  5232. }
  5233. if ( aExp == 0 ) {
  5234. if ( aSig0 == 0 ) return packFloatx80( 0, 0, 0 );
  5235. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  5236. }
  5237. zExp = ( ( aExp - 0x3FFF )>>1 ) + 0x3FFF;
  5238. zSig0 = estimateSqrt32( aExp, aSig0>>32 );
  5239. shift128Right( aSig0, 0, 2 + ( aExp & 1 ), &aSig0, &aSig1 );
  5240. zSig0 = estimateDiv128To64( aSig0, aSig1, zSig0<<32 ) + ( zSig0<<30 );
  5241. doubleZSig0 = zSig0<<1;
  5242. mul64To128( zSig0, zSig0, &term0, &term1 );
  5243. sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
  5244. while ( (int64_t) rem0 < 0 ) {
  5245. --zSig0;
  5246. doubleZSig0 -= 2;
  5247. add128( rem0, rem1, zSig0>>63, doubleZSig0 | 1, &rem0, &rem1 );
  5248. }
  5249. zSig1 = estimateDiv128To64( rem1, 0, doubleZSig0 );
  5250. if ( ( zSig1 & UINT64_C(0x3FFFFFFFFFFFFFFF) ) <= 5 ) {
  5251. if ( zSig1 == 0 ) zSig1 = 1;
  5252. mul64To128( doubleZSig0, zSig1, &term1, &term2 );
  5253. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  5254. mul64To128( zSig1, zSig1, &term2, &term3 );
  5255. sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
  5256. while ( (int64_t) rem1 < 0 ) {
  5257. --zSig1;
  5258. shortShift128Left( 0, zSig1, 1, &term2, &term3 );
  5259. term3 |= 1;
  5260. term2 |= doubleZSig0;
  5261. add192( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 );
  5262. }
  5263. zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
  5264. }
  5265. shortShift128Left( 0, zSig1, 1, &zSig0, &zSig1 );
  5266. zSig0 |= doubleZSig0;
  5267. return roundAndPackFloatx80(status->floatx80_rounding_precision,
  5268. 0, zExp, zSig0, zSig1, status);
  5269. }
  5270. /*----------------------------------------------------------------------------
  5271. | Returns the result of converting the quadruple-precision floating-point
  5272. | value `a' to the 32-bit two's complement integer format. The conversion
  5273. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5274. | Arithmetic---which means in particular that the conversion is rounded
  5275. | according to the current rounding mode. If `a' is a NaN, the largest
  5276. | positive integer is returned. Otherwise, if the conversion overflows, the
  5277. | largest integer with the same sign as `a' is returned.
  5278. *----------------------------------------------------------------------------*/
  5279. int32_t float128_to_int32(float128 a, float_status *status)
  5280. {
  5281. bool aSign;
  5282. int32_t aExp, shiftCount;
  5283. uint64_t aSig0, aSig1;
  5284. aSig1 = extractFloat128Frac1( a );
  5285. aSig0 = extractFloat128Frac0( a );
  5286. aExp = extractFloat128Exp( a );
  5287. aSign = extractFloat128Sign( a );
  5288. if ( ( aExp == 0x7FFF ) && ( aSig0 | aSig1 ) ) aSign = 0;
  5289. if ( aExp ) aSig0 |= UINT64_C(0x0001000000000000);
  5290. aSig0 |= ( aSig1 != 0 );
  5291. shiftCount = 0x4028 - aExp;
  5292. if ( 0 < shiftCount ) shift64RightJamming( aSig0, shiftCount, &aSig0 );
  5293. return roundAndPackInt32(aSign, aSig0, status);
  5294. }
  5295. /*----------------------------------------------------------------------------
  5296. | Returns the result of converting the quadruple-precision floating-point
  5297. | value `a' to the 32-bit two's complement integer format. The conversion
  5298. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5299. | Arithmetic, except that the conversion is always rounded toward zero. If
  5300. | `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  5301. | conversion overflows, the largest integer with the same sign as `a' is
  5302. | returned.
  5303. *----------------------------------------------------------------------------*/
  5304. int32_t float128_to_int32_round_to_zero(float128 a, float_status *status)
  5305. {
  5306. bool aSign;
  5307. int32_t aExp, shiftCount;
  5308. uint64_t aSig0, aSig1, savedASig;
  5309. int32_t z;
  5310. aSig1 = extractFloat128Frac1( a );
  5311. aSig0 = extractFloat128Frac0( a );
  5312. aExp = extractFloat128Exp( a );
  5313. aSign = extractFloat128Sign( a );
  5314. aSig0 |= ( aSig1 != 0 );
  5315. if ( 0x401E < aExp ) {
  5316. if ( ( aExp == 0x7FFF ) && aSig0 ) aSign = 0;
  5317. goto invalid;
  5318. }
  5319. else if ( aExp < 0x3FFF ) {
  5320. if (aExp || aSig0) {
  5321. status->float_exception_flags |= float_flag_inexact;
  5322. }
  5323. return 0;
  5324. }
  5325. aSig0 |= UINT64_C(0x0001000000000000);
  5326. shiftCount = 0x402F - aExp;
  5327. savedASig = aSig0;
  5328. aSig0 >>= shiftCount;
  5329. z = aSig0;
  5330. if ( aSign ) z = - z;
  5331. if ( ( z < 0 ) ^ aSign ) {
  5332. invalid:
  5333. float_raise(float_flag_invalid, status);
  5334. return aSign ? INT32_MIN : INT32_MAX;
  5335. }
  5336. if ( ( aSig0<<shiftCount ) != savedASig ) {
  5337. status->float_exception_flags |= float_flag_inexact;
  5338. }
  5339. return z;
  5340. }
  5341. /*----------------------------------------------------------------------------
  5342. | Returns the result of converting the quadruple-precision floating-point
  5343. | value `a' to the 64-bit two's complement integer format. The conversion
  5344. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5345. | Arithmetic---which means in particular that the conversion is rounded
  5346. | according to the current rounding mode. If `a' is a NaN, the largest
  5347. | positive integer is returned. Otherwise, if the conversion overflows, the
  5348. | largest integer with the same sign as `a' is returned.
  5349. *----------------------------------------------------------------------------*/
  5350. int64_t float128_to_int64(float128 a, float_status *status)
  5351. {
  5352. bool aSign;
  5353. int32_t aExp, shiftCount;
  5354. uint64_t aSig0, aSig1;
  5355. aSig1 = extractFloat128Frac1( a );
  5356. aSig0 = extractFloat128Frac0( a );
  5357. aExp = extractFloat128Exp( a );
  5358. aSign = extractFloat128Sign( a );
  5359. if ( aExp ) aSig0 |= UINT64_C(0x0001000000000000);
  5360. shiftCount = 0x402F - aExp;
  5361. if ( shiftCount <= 0 ) {
  5362. if ( 0x403E < aExp ) {
  5363. float_raise(float_flag_invalid, status);
  5364. if ( ! aSign
  5365. || ( ( aExp == 0x7FFF )
  5366. && ( aSig1 || ( aSig0 != UINT64_C(0x0001000000000000) ) )
  5367. )
  5368. ) {
  5369. return INT64_MAX;
  5370. }
  5371. return INT64_MIN;
  5372. }
  5373. shortShift128Left( aSig0, aSig1, - shiftCount, &aSig0, &aSig1 );
  5374. }
  5375. else {
  5376. shift64ExtraRightJamming( aSig0, aSig1, shiftCount, &aSig0, &aSig1 );
  5377. }
  5378. return roundAndPackInt64(aSign, aSig0, aSig1, status);
  5379. }
  5380. /*----------------------------------------------------------------------------
  5381. | Returns the result of converting the quadruple-precision floating-point
  5382. | value `a' to the 64-bit two's complement integer format. The conversion
  5383. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5384. | Arithmetic, except that the conversion is always rounded toward zero.
  5385. | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
  5386. | the conversion overflows, the largest integer with the same sign as `a' is
  5387. | returned.
  5388. *----------------------------------------------------------------------------*/
  5389. int64_t float128_to_int64_round_to_zero(float128 a, float_status *status)
  5390. {
  5391. bool aSign;
  5392. int32_t aExp, shiftCount;
  5393. uint64_t aSig0, aSig1;
  5394. int64_t z;
  5395. aSig1 = extractFloat128Frac1( a );
  5396. aSig0 = extractFloat128Frac0( a );
  5397. aExp = extractFloat128Exp( a );
  5398. aSign = extractFloat128Sign( a );
  5399. if ( aExp ) aSig0 |= UINT64_C(0x0001000000000000);
  5400. shiftCount = aExp - 0x402F;
  5401. if ( 0 < shiftCount ) {
  5402. if ( 0x403E <= aExp ) {
  5403. aSig0 &= UINT64_C(0x0000FFFFFFFFFFFF);
  5404. if ( ( a.high == UINT64_C(0xC03E000000000000) )
  5405. && ( aSig1 < UINT64_C(0x0002000000000000) ) ) {
  5406. if (aSig1) {
  5407. status->float_exception_flags |= float_flag_inexact;
  5408. }
  5409. }
  5410. else {
  5411. float_raise(float_flag_invalid, status);
  5412. if ( ! aSign || ( ( aExp == 0x7FFF ) && ( aSig0 | aSig1 ) ) ) {
  5413. return INT64_MAX;
  5414. }
  5415. }
  5416. return INT64_MIN;
  5417. }
  5418. z = ( aSig0<<shiftCount ) | ( aSig1>>( ( - shiftCount ) & 63 ) );
  5419. if ( (uint64_t) ( aSig1<<shiftCount ) ) {
  5420. status->float_exception_flags |= float_flag_inexact;
  5421. }
  5422. }
  5423. else {
  5424. if ( aExp < 0x3FFF ) {
  5425. if ( aExp | aSig0 | aSig1 ) {
  5426. status->float_exception_flags |= float_flag_inexact;
  5427. }
  5428. return 0;
  5429. }
  5430. z = aSig0>>( - shiftCount );
  5431. if ( aSig1
  5432. || ( shiftCount && (uint64_t) ( aSig0<<( shiftCount & 63 ) ) ) ) {
  5433. status->float_exception_flags |= float_flag_inexact;
  5434. }
  5435. }
  5436. if ( aSign ) z = - z;
  5437. return z;
  5438. }
  5439. /*----------------------------------------------------------------------------
  5440. | Returns the result of converting the quadruple-precision floating-point value
  5441. | `a' to the 64-bit unsigned integer format. The conversion is
  5442. | performed according to the IEC/IEEE Standard for Binary Floating-Point
  5443. | Arithmetic---which means in particular that the conversion is rounded
  5444. | according to the current rounding mode. If `a' is a NaN, the largest
  5445. | positive integer is returned. If the conversion overflows, the
  5446. | largest unsigned integer is returned. If 'a' is negative, the value is
  5447. | rounded and zero is returned; negative values that do not round to zero
  5448. | will raise the inexact exception.
  5449. *----------------------------------------------------------------------------*/
  5450. uint64_t float128_to_uint64(float128 a, float_status *status)
  5451. {
  5452. bool aSign;
  5453. int aExp;
  5454. int shiftCount;
  5455. uint64_t aSig0, aSig1;
  5456. aSig0 = extractFloat128Frac0(a);
  5457. aSig1 = extractFloat128Frac1(a);
  5458. aExp = extractFloat128Exp(a);
  5459. aSign = extractFloat128Sign(a);
  5460. if (aSign && (aExp > 0x3FFE)) {
  5461. float_raise(float_flag_invalid, status);
  5462. if (float128_is_any_nan(a)) {
  5463. return UINT64_MAX;
  5464. } else {
  5465. return 0;
  5466. }
  5467. }
  5468. if (aExp) {
  5469. aSig0 |= UINT64_C(0x0001000000000000);
  5470. }
  5471. shiftCount = 0x402F - aExp;
  5472. if (shiftCount <= 0) {
  5473. if (0x403E < aExp) {
  5474. float_raise(float_flag_invalid, status);
  5475. return UINT64_MAX;
  5476. }
  5477. shortShift128Left(aSig0, aSig1, -shiftCount, &aSig0, &aSig1);
  5478. } else {
  5479. shift64ExtraRightJamming(aSig0, aSig1, shiftCount, &aSig0, &aSig1);
  5480. }
  5481. return roundAndPackUint64(aSign, aSig0, aSig1, status);
  5482. }
  5483. uint64_t float128_to_uint64_round_to_zero(float128 a, float_status *status)
  5484. {
  5485. uint64_t v;
  5486. signed char current_rounding_mode = status->float_rounding_mode;
  5487. set_float_rounding_mode(float_round_to_zero, status);
  5488. v = float128_to_uint64(a, status);
  5489. set_float_rounding_mode(current_rounding_mode, status);
  5490. return v;
  5491. }
  5492. /*----------------------------------------------------------------------------
  5493. | Returns the result of converting the quadruple-precision floating-point
  5494. | value `a' to the 32-bit unsigned integer format. The conversion
  5495. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5496. | Arithmetic except that the conversion is always rounded toward zero.
  5497. | If `a' is a NaN, the largest positive integer is returned. Otherwise,
  5498. | if the conversion overflows, the largest unsigned integer is returned.
  5499. | If 'a' is negative, the value is rounded and zero is returned; negative
  5500. | values that do not round to zero will raise the inexact exception.
  5501. *----------------------------------------------------------------------------*/
  5502. uint32_t float128_to_uint32_round_to_zero(float128 a, float_status *status)
  5503. {
  5504. uint64_t v;
  5505. uint32_t res;
  5506. int old_exc_flags = get_float_exception_flags(status);
  5507. v = float128_to_uint64_round_to_zero(a, status);
  5508. if (v > 0xffffffff) {
  5509. res = 0xffffffff;
  5510. } else {
  5511. return v;
  5512. }
  5513. set_float_exception_flags(old_exc_flags, status);
  5514. float_raise(float_flag_invalid, status);
  5515. return res;
  5516. }
  5517. /*----------------------------------------------------------------------------
  5518. | Returns the result of converting the quadruple-precision floating-point value
  5519. | `a' to the 32-bit unsigned integer format. The conversion is
  5520. | performed according to the IEC/IEEE Standard for Binary Floating-Point
  5521. | Arithmetic---which means in particular that the conversion is rounded
  5522. | according to the current rounding mode. If `a' is a NaN, the largest
  5523. | positive integer is returned. If the conversion overflows, the
  5524. | largest unsigned integer is returned. If 'a' is negative, the value is
  5525. | rounded and zero is returned; negative values that do not round to zero
  5526. | will raise the inexact exception.
  5527. *----------------------------------------------------------------------------*/
  5528. uint32_t float128_to_uint32(float128 a, float_status *status)
  5529. {
  5530. uint64_t v;
  5531. uint32_t res;
  5532. int old_exc_flags = get_float_exception_flags(status);
  5533. v = float128_to_uint64(a, status);
  5534. if (v > 0xffffffff) {
  5535. res = 0xffffffff;
  5536. } else {
  5537. return v;
  5538. }
  5539. set_float_exception_flags(old_exc_flags, status);
  5540. float_raise(float_flag_invalid, status);
  5541. return res;
  5542. }
  5543. /*----------------------------------------------------------------------------
  5544. | Returns the result of converting the quadruple-precision floating-point
  5545. | value `a' to the single-precision floating-point format. The conversion
  5546. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5547. | Arithmetic.
  5548. *----------------------------------------------------------------------------*/
  5549. float32 float128_to_float32(float128 a, float_status *status)
  5550. {
  5551. bool aSign;
  5552. int32_t aExp;
  5553. uint64_t aSig0, aSig1;
  5554. uint32_t zSig;
  5555. aSig1 = extractFloat128Frac1( a );
  5556. aSig0 = extractFloat128Frac0( a );
  5557. aExp = extractFloat128Exp( a );
  5558. aSign = extractFloat128Sign( a );
  5559. if ( aExp == 0x7FFF ) {
  5560. if ( aSig0 | aSig1 ) {
  5561. return commonNaNToFloat32(float128ToCommonNaN(a, status), status);
  5562. }
  5563. return packFloat32( aSign, 0xFF, 0 );
  5564. }
  5565. aSig0 |= ( aSig1 != 0 );
  5566. shift64RightJamming( aSig0, 18, &aSig0 );
  5567. zSig = aSig0;
  5568. if ( aExp || zSig ) {
  5569. zSig |= 0x40000000;
  5570. aExp -= 0x3F81;
  5571. }
  5572. return roundAndPackFloat32(aSign, aExp, zSig, status);
  5573. }
  5574. /*----------------------------------------------------------------------------
  5575. | Returns the result of converting the quadruple-precision floating-point
  5576. | value `a' to the double-precision floating-point format. The conversion
  5577. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  5578. | Arithmetic.
  5579. *----------------------------------------------------------------------------*/
  5580. float64 float128_to_float64(float128 a, float_status *status)
  5581. {
  5582. bool aSign;
  5583. int32_t aExp;
  5584. uint64_t aSig0, aSig1;
  5585. aSig1 = extractFloat128Frac1( a );
  5586. aSig0 = extractFloat128Frac0( a );
  5587. aExp = extractFloat128Exp( a );
  5588. aSign = extractFloat128Sign( a );
  5589. if ( aExp == 0x7FFF ) {
  5590. if ( aSig0 | aSig1 ) {
  5591. return commonNaNToFloat64(float128ToCommonNaN(a, status), status);
  5592. }
  5593. return packFloat64( aSign, 0x7FF, 0 );
  5594. }
  5595. shortShift128Left( aSig0, aSig1, 14, &aSig0, &aSig1 );
  5596. aSig0 |= ( aSig1 != 0 );
  5597. if ( aExp || aSig0 ) {
  5598. aSig0 |= UINT64_C(0x4000000000000000);
  5599. aExp -= 0x3C01;
  5600. }
  5601. return roundAndPackFloat64(aSign, aExp, aSig0, status);
  5602. }
  5603. /*----------------------------------------------------------------------------
  5604. | Returns the result of converting the quadruple-precision floating-point
  5605. | value `a' to the extended double-precision floating-point format. The
  5606. | conversion is performed according to the IEC/IEEE Standard for Binary
  5607. | Floating-Point Arithmetic.
  5608. *----------------------------------------------------------------------------*/
  5609. floatx80 float128_to_floatx80(float128 a, float_status *status)
  5610. {
  5611. bool aSign;
  5612. int32_t aExp;
  5613. uint64_t aSig0, aSig1;
  5614. aSig1 = extractFloat128Frac1( a );
  5615. aSig0 = extractFloat128Frac0( a );
  5616. aExp = extractFloat128Exp( a );
  5617. aSign = extractFloat128Sign( a );
  5618. if ( aExp == 0x7FFF ) {
  5619. if ( aSig0 | aSig1 ) {
  5620. floatx80 res = commonNaNToFloatx80(float128ToCommonNaN(a, status),
  5621. status);
  5622. return floatx80_silence_nan(res, status);
  5623. }
  5624. return packFloatx80(aSign, floatx80_infinity_high,
  5625. floatx80_infinity_low);
  5626. }
  5627. if ( aExp == 0 ) {
  5628. if ( ( aSig0 | aSig1 ) == 0 ) return packFloatx80( aSign, 0, 0 );
  5629. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  5630. }
  5631. else {
  5632. aSig0 |= UINT64_C(0x0001000000000000);
  5633. }
  5634. shortShift128Left( aSig0, aSig1, 15, &aSig0, &aSig1 );
  5635. return roundAndPackFloatx80(80, aSign, aExp, aSig0, aSig1, status);
  5636. }
  5637. /*----------------------------------------------------------------------------
  5638. | Rounds the quadruple-precision floating-point value `a' to an integer, and
  5639. | returns the result as a quadruple-precision floating-point value. The
  5640. | operation is performed according to the IEC/IEEE Standard for Binary
  5641. | Floating-Point Arithmetic.
  5642. *----------------------------------------------------------------------------*/
  5643. float128 float128_round_to_int(float128 a, float_status *status)
  5644. {
  5645. bool aSign;
  5646. int32_t aExp;
  5647. uint64_t lastBitMask, roundBitsMask;
  5648. float128 z;
  5649. aExp = extractFloat128Exp( a );
  5650. if ( 0x402F <= aExp ) {
  5651. if ( 0x406F <= aExp ) {
  5652. if ( ( aExp == 0x7FFF )
  5653. && ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) )
  5654. ) {
  5655. return propagateFloat128NaN(a, a, status);
  5656. }
  5657. return a;
  5658. }
  5659. lastBitMask = 1;
  5660. lastBitMask = ( lastBitMask<<( 0x406E - aExp ) )<<1;
  5661. roundBitsMask = lastBitMask - 1;
  5662. z = a;
  5663. switch (status->float_rounding_mode) {
  5664. case float_round_nearest_even:
  5665. if ( lastBitMask ) {
  5666. add128( z.high, z.low, 0, lastBitMask>>1, &z.high, &z.low );
  5667. if ( ( z.low & roundBitsMask ) == 0 ) z.low &= ~ lastBitMask;
  5668. }
  5669. else {
  5670. if ( (int64_t) z.low < 0 ) {
  5671. ++z.high;
  5672. if ( (uint64_t) ( z.low<<1 ) == 0 ) z.high &= ~1;
  5673. }
  5674. }
  5675. break;
  5676. case float_round_ties_away:
  5677. if (lastBitMask) {
  5678. add128(z.high, z.low, 0, lastBitMask >> 1, &z.high, &z.low);
  5679. } else {
  5680. if ((int64_t) z.low < 0) {
  5681. ++z.high;
  5682. }
  5683. }
  5684. break;
  5685. case float_round_to_zero:
  5686. break;
  5687. case float_round_up:
  5688. if (!extractFloat128Sign(z)) {
  5689. add128(z.high, z.low, 0, roundBitsMask, &z.high, &z.low);
  5690. }
  5691. break;
  5692. case float_round_down:
  5693. if (extractFloat128Sign(z)) {
  5694. add128(z.high, z.low, 0, roundBitsMask, &z.high, &z.low);
  5695. }
  5696. break;
  5697. case float_round_to_odd:
  5698. /*
  5699. * Note that if lastBitMask == 0, the last bit is the lsb
  5700. * of high, and roundBitsMask == -1.
  5701. */
  5702. if ((lastBitMask ? z.low & lastBitMask : z.high & 1) == 0) {
  5703. add128(z.high, z.low, 0, roundBitsMask, &z.high, &z.low);
  5704. }
  5705. break;
  5706. default:
  5707. abort();
  5708. }
  5709. z.low &= ~ roundBitsMask;
  5710. }
  5711. else {
  5712. if ( aExp < 0x3FFF ) {
  5713. if ( ( ( (uint64_t) ( a.high<<1 ) ) | a.low ) == 0 ) return a;
  5714. status->float_exception_flags |= float_flag_inexact;
  5715. aSign = extractFloat128Sign( a );
  5716. switch (status->float_rounding_mode) {
  5717. case float_round_nearest_even:
  5718. if ( ( aExp == 0x3FFE )
  5719. && ( extractFloat128Frac0( a )
  5720. | extractFloat128Frac1( a ) )
  5721. ) {
  5722. return packFloat128( aSign, 0x3FFF, 0, 0 );
  5723. }
  5724. break;
  5725. case float_round_ties_away:
  5726. if (aExp == 0x3FFE) {
  5727. return packFloat128(aSign, 0x3FFF, 0, 0);
  5728. }
  5729. break;
  5730. case float_round_down:
  5731. return
  5732. aSign ? packFloat128( 1, 0x3FFF, 0, 0 )
  5733. : packFloat128( 0, 0, 0, 0 );
  5734. case float_round_up:
  5735. return
  5736. aSign ? packFloat128( 1, 0, 0, 0 )
  5737. : packFloat128( 0, 0x3FFF, 0, 0 );
  5738. case float_round_to_odd:
  5739. return packFloat128(aSign, 0x3FFF, 0, 0);
  5740. case float_round_to_zero:
  5741. break;
  5742. }
  5743. return packFloat128( aSign, 0, 0, 0 );
  5744. }
  5745. lastBitMask = 1;
  5746. lastBitMask <<= 0x402F - aExp;
  5747. roundBitsMask = lastBitMask - 1;
  5748. z.low = 0;
  5749. z.high = a.high;
  5750. switch (status->float_rounding_mode) {
  5751. case float_round_nearest_even:
  5752. z.high += lastBitMask>>1;
  5753. if ( ( ( z.high & roundBitsMask ) | a.low ) == 0 ) {
  5754. z.high &= ~ lastBitMask;
  5755. }
  5756. break;
  5757. case float_round_ties_away:
  5758. z.high += lastBitMask>>1;
  5759. break;
  5760. case float_round_to_zero:
  5761. break;
  5762. case float_round_up:
  5763. if (!extractFloat128Sign(z)) {
  5764. z.high |= ( a.low != 0 );
  5765. z.high += roundBitsMask;
  5766. }
  5767. break;
  5768. case float_round_down:
  5769. if (extractFloat128Sign(z)) {
  5770. z.high |= (a.low != 0);
  5771. z.high += roundBitsMask;
  5772. }
  5773. break;
  5774. case float_round_to_odd:
  5775. if ((z.high & lastBitMask) == 0) {
  5776. z.high |= (a.low != 0);
  5777. z.high += roundBitsMask;
  5778. }
  5779. break;
  5780. default:
  5781. abort();
  5782. }
  5783. z.high &= ~ roundBitsMask;
  5784. }
  5785. if ( ( z.low != a.low ) || ( z.high != a.high ) ) {
  5786. status->float_exception_flags |= float_flag_inexact;
  5787. }
  5788. return z;
  5789. }
  5790. /*----------------------------------------------------------------------------
  5791. | Returns the result of adding the absolute values of the quadruple-precision
  5792. | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
  5793. | before being returned. `zSign' is ignored if the result is a NaN.
  5794. | The addition is performed according to the IEC/IEEE Standard for Binary
  5795. | Floating-Point Arithmetic.
  5796. *----------------------------------------------------------------------------*/
  5797. static float128 addFloat128Sigs(float128 a, float128 b, bool zSign,
  5798. float_status *status)
  5799. {
  5800. int32_t aExp, bExp, zExp;
  5801. uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2;
  5802. int32_t expDiff;
  5803. aSig1 = extractFloat128Frac1( a );
  5804. aSig0 = extractFloat128Frac0( a );
  5805. aExp = extractFloat128Exp( a );
  5806. bSig1 = extractFloat128Frac1( b );
  5807. bSig0 = extractFloat128Frac0( b );
  5808. bExp = extractFloat128Exp( b );
  5809. expDiff = aExp - bExp;
  5810. if ( 0 < expDiff ) {
  5811. if ( aExp == 0x7FFF ) {
  5812. if (aSig0 | aSig1) {
  5813. return propagateFloat128NaN(a, b, status);
  5814. }
  5815. return a;
  5816. }
  5817. if ( bExp == 0 ) {
  5818. --expDiff;
  5819. }
  5820. else {
  5821. bSig0 |= UINT64_C(0x0001000000000000);
  5822. }
  5823. shift128ExtraRightJamming(
  5824. bSig0, bSig1, 0, expDiff, &bSig0, &bSig1, &zSig2 );
  5825. zExp = aExp;
  5826. }
  5827. else if ( expDiff < 0 ) {
  5828. if ( bExp == 0x7FFF ) {
  5829. if (bSig0 | bSig1) {
  5830. return propagateFloat128NaN(a, b, status);
  5831. }
  5832. return packFloat128( zSign, 0x7FFF, 0, 0 );
  5833. }
  5834. if ( aExp == 0 ) {
  5835. ++expDiff;
  5836. }
  5837. else {
  5838. aSig0 |= UINT64_C(0x0001000000000000);
  5839. }
  5840. shift128ExtraRightJamming(
  5841. aSig0, aSig1, 0, - expDiff, &aSig0, &aSig1, &zSig2 );
  5842. zExp = bExp;
  5843. }
  5844. else {
  5845. if ( aExp == 0x7FFF ) {
  5846. if ( aSig0 | aSig1 | bSig0 | bSig1 ) {
  5847. return propagateFloat128NaN(a, b, status);
  5848. }
  5849. return a;
  5850. }
  5851. add128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
  5852. if ( aExp == 0 ) {
  5853. if (status->flush_to_zero) {
  5854. if (zSig0 | zSig1) {
  5855. float_raise(float_flag_output_denormal, status);
  5856. }
  5857. return packFloat128(zSign, 0, 0, 0);
  5858. }
  5859. return packFloat128( zSign, 0, zSig0, zSig1 );
  5860. }
  5861. zSig2 = 0;
  5862. zSig0 |= UINT64_C(0x0002000000000000);
  5863. zExp = aExp;
  5864. goto shiftRight1;
  5865. }
  5866. aSig0 |= UINT64_C(0x0001000000000000);
  5867. add128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
  5868. --zExp;
  5869. if ( zSig0 < UINT64_C(0x0002000000000000) ) goto roundAndPack;
  5870. ++zExp;
  5871. shiftRight1:
  5872. shift128ExtraRightJamming(
  5873. zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 );
  5874. roundAndPack:
  5875. return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
  5876. }
  5877. /*----------------------------------------------------------------------------
  5878. | Returns the result of subtracting the absolute values of the quadruple-
  5879. | precision floating-point values `a' and `b'. If `zSign' is 1, the
  5880. | difference is negated before being returned. `zSign' is ignored if the
  5881. | result is a NaN. The subtraction is performed according to the IEC/IEEE
  5882. | Standard for Binary Floating-Point Arithmetic.
  5883. *----------------------------------------------------------------------------*/
  5884. static float128 subFloat128Sigs(float128 a, float128 b, bool zSign,
  5885. float_status *status)
  5886. {
  5887. int32_t aExp, bExp, zExp;
  5888. uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1;
  5889. int32_t expDiff;
  5890. aSig1 = extractFloat128Frac1( a );
  5891. aSig0 = extractFloat128Frac0( a );
  5892. aExp = extractFloat128Exp( a );
  5893. bSig1 = extractFloat128Frac1( b );
  5894. bSig0 = extractFloat128Frac0( b );
  5895. bExp = extractFloat128Exp( b );
  5896. expDiff = aExp - bExp;
  5897. shortShift128Left( aSig0, aSig1, 14, &aSig0, &aSig1 );
  5898. shortShift128Left( bSig0, bSig1, 14, &bSig0, &bSig1 );
  5899. if ( 0 < expDiff ) goto aExpBigger;
  5900. if ( expDiff < 0 ) goto bExpBigger;
  5901. if ( aExp == 0x7FFF ) {
  5902. if ( aSig0 | aSig1 | bSig0 | bSig1 ) {
  5903. return propagateFloat128NaN(a, b, status);
  5904. }
  5905. float_raise(float_flag_invalid, status);
  5906. return float128_default_nan(status);
  5907. }
  5908. if ( aExp == 0 ) {
  5909. aExp = 1;
  5910. bExp = 1;
  5911. }
  5912. if ( bSig0 < aSig0 ) goto aBigger;
  5913. if ( aSig0 < bSig0 ) goto bBigger;
  5914. if ( bSig1 < aSig1 ) goto aBigger;
  5915. if ( aSig1 < bSig1 ) goto bBigger;
  5916. return packFloat128(status->float_rounding_mode == float_round_down,
  5917. 0, 0, 0);
  5918. bExpBigger:
  5919. if ( bExp == 0x7FFF ) {
  5920. if (bSig0 | bSig1) {
  5921. return propagateFloat128NaN(a, b, status);
  5922. }
  5923. return packFloat128( zSign ^ 1, 0x7FFF, 0, 0 );
  5924. }
  5925. if ( aExp == 0 ) {
  5926. ++expDiff;
  5927. }
  5928. else {
  5929. aSig0 |= UINT64_C(0x4000000000000000);
  5930. }
  5931. shift128RightJamming( aSig0, aSig1, - expDiff, &aSig0, &aSig1 );
  5932. bSig0 |= UINT64_C(0x4000000000000000);
  5933. bBigger:
  5934. sub128( bSig0, bSig1, aSig0, aSig1, &zSig0, &zSig1 );
  5935. zExp = bExp;
  5936. zSign ^= 1;
  5937. goto normalizeRoundAndPack;
  5938. aExpBigger:
  5939. if ( aExp == 0x7FFF ) {
  5940. if (aSig0 | aSig1) {
  5941. return propagateFloat128NaN(a, b, status);
  5942. }
  5943. return a;
  5944. }
  5945. if ( bExp == 0 ) {
  5946. --expDiff;
  5947. }
  5948. else {
  5949. bSig0 |= UINT64_C(0x4000000000000000);
  5950. }
  5951. shift128RightJamming( bSig0, bSig1, expDiff, &bSig0, &bSig1 );
  5952. aSig0 |= UINT64_C(0x4000000000000000);
  5953. aBigger:
  5954. sub128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
  5955. zExp = aExp;
  5956. normalizeRoundAndPack:
  5957. --zExp;
  5958. return normalizeRoundAndPackFloat128(zSign, zExp - 14, zSig0, zSig1,
  5959. status);
  5960. }
  5961. /*----------------------------------------------------------------------------
  5962. | Returns the result of adding the quadruple-precision floating-point values
  5963. | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  5964. | for Binary Floating-Point Arithmetic.
  5965. *----------------------------------------------------------------------------*/
  5966. float128 float128_add(float128 a, float128 b, float_status *status)
  5967. {
  5968. bool aSign, bSign;
  5969. aSign = extractFloat128Sign( a );
  5970. bSign = extractFloat128Sign( b );
  5971. if ( aSign == bSign ) {
  5972. return addFloat128Sigs(a, b, aSign, status);
  5973. }
  5974. else {
  5975. return subFloat128Sigs(a, b, aSign, status);
  5976. }
  5977. }
  5978. /*----------------------------------------------------------------------------
  5979. | Returns the result of subtracting the quadruple-precision floating-point
  5980. | values `a' and `b'. The operation is performed according to the IEC/IEEE
  5981. | Standard for Binary Floating-Point Arithmetic.
  5982. *----------------------------------------------------------------------------*/
  5983. float128 float128_sub(float128 a, float128 b, float_status *status)
  5984. {
  5985. bool aSign, bSign;
  5986. aSign = extractFloat128Sign( a );
  5987. bSign = extractFloat128Sign( b );
  5988. if ( aSign == bSign ) {
  5989. return subFloat128Sigs(a, b, aSign, status);
  5990. }
  5991. else {
  5992. return addFloat128Sigs(a, b, aSign, status);
  5993. }
  5994. }
  5995. /*----------------------------------------------------------------------------
  5996. | Returns the result of multiplying the quadruple-precision floating-point
  5997. | values `a' and `b'. The operation is performed according to the IEC/IEEE
  5998. | Standard for Binary Floating-Point Arithmetic.
  5999. *----------------------------------------------------------------------------*/
  6000. float128 float128_mul(float128 a, float128 b, float_status *status)
  6001. {
  6002. bool aSign, bSign, zSign;
  6003. int32_t aExp, bExp, zExp;
  6004. uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2, zSig3;
  6005. aSig1 = extractFloat128Frac1( a );
  6006. aSig0 = extractFloat128Frac0( a );
  6007. aExp = extractFloat128Exp( a );
  6008. aSign = extractFloat128Sign( a );
  6009. bSig1 = extractFloat128Frac1( b );
  6010. bSig0 = extractFloat128Frac0( b );
  6011. bExp = extractFloat128Exp( b );
  6012. bSign = extractFloat128Sign( b );
  6013. zSign = aSign ^ bSign;
  6014. if ( aExp == 0x7FFF ) {
  6015. if ( ( aSig0 | aSig1 )
  6016. || ( ( bExp == 0x7FFF ) && ( bSig0 | bSig1 ) ) ) {
  6017. return propagateFloat128NaN(a, b, status);
  6018. }
  6019. if ( ( bExp | bSig0 | bSig1 ) == 0 ) goto invalid;
  6020. return packFloat128( zSign, 0x7FFF, 0, 0 );
  6021. }
  6022. if ( bExp == 0x7FFF ) {
  6023. if (bSig0 | bSig1) {
  6024. return propagateFloat128NaN(a, b, status);
  6025. }
  6026. if ( ( aExp | aSig0 | aSig1 ) == 0 ) {
  6027. invalid:
  6028. float_raise(float_flag_invalid, status);
  6029. return float128_default_nan(status);
  6030. }
  6031. return packFloat128( zSign, 0x7FFF, 0, 0 );
  6032. }
  6033. if ( aExp == 0 ) {
  6034. if ( ( aSig0 | aSig1 ) == 0 ) return packFloat128( zSign, 0, 0, 0 );
  6035. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  6036. }
  6037. if ( bExp == 0 ) {
  6038. if ( ( bSig0 | bSig1 ) == 0 ) return packFloat128( zSign, 0, 0, 0 );
  6039. normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
  6040. }
  6041. zExp = aExp + bExp - 0x4000;
  6042. aSig0 |= UINT64_C(0x0001000000000000);
  6043. shortShift128Left( bSig0, bSig1, 16, &bSig0, &bSig1 );
  6044. mul128To256( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1, &zSig2, &zSig3 );
  6045. add128( zSig0, zSig1, aSig0, aSig1, &zSig0, &zSig1 );
  6046. zSig2 |= ( zSig3 != 0 );
  6047. if (UINT64_C( 0x0002000000000000) <= zSig0 ) {
  6048. shift128ExtraRightJamming(
  6049. zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 );
  6050. ++zExp;
  6051. }
  6052. return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
  6053. }
  6054. /*----------------------------------------------------------------------------
  6055. | Returns the result of dividing the quadruple-precision floating-point value
  6056. | `a' by the corresponding value `b'. The operation is performed according to
  6057. | the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  6058. *----------------------------------------------------------------------------*/
  6059. float128 float128_div(float128 a, float128 b, float_status *status)
  6060. {
  6061. bool aSign, bSign, zSign;
  6062. int32_t aExp, bExp, zExp;
  6063. uint64_t aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2;
  6064. uint64_t rem0, rem1, rem2, rem3, term0, term1, term2, term3;
  6065. aSig1 = extractFloat128Frac1( a );
  6066. aSig0 = extractFloat128Frac0( a );
  6067. aExp = extractFloat128Exp( a );
  6068. aSign = extractFloat128Sign( a );
  6069. bSig1 = extractFloat128Frac1( b );
  6070. bSig0 = extractFloat128Frac0( b );
  6071. bExp = extractFloat128Exp( b );
  6072. bSign = extractFloat128Sign( b );
  6073. zSign = aSign ^ bSign;
  6074. if ( aExp == 0x7FFF ) {
  6075. if (aSig0 | aSig1) {
  6076. return propagateFloat128NaN(a, b, status);
  6077. }
  6078. if ( bExp == 0x7FFF ) {
  6079. if (bSig0 | bSig1) {
  6080. return propagateFloat128NaN(a, b, status);
  6081. }
  6082. goto invalid;
  6083. }
  6084. return packFloat128( zSign, 0x7FFF, 0, 0 );
  6085. }
  6086. if ( bExp == 0x7FFF ) {
  6087. if (bSig0 | bSig1) {
  6088. return propagateFloat128NaN(a, b, status);
  6089. }
  6090. return packFloat128( zSign, 0, 0, 0 );
  6091. }
  6092. if ( bExp == 0 ) {
  6093. if ( ( bSig0 | bSig1 ) == 0 ) {
  6094. if ( ( aExp | aSig0 | aSig1 ) == 0 ) {
  6095. invalid:
  6096. float_raise(float_flag_invalid, status);
  6097. return float128_default_nan(status);
  6098. }
  6099. float_raise(float_flag_divbyzero, status);
  6100. return packFloat128( zSign, 0x7FFF, 0, 0 );
  6101. }
  6102. normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
  6103. }
  6104. if ( aExp == 0 ) {
  6105. if ( ( aSig0 | aSig1 ) == 0 ) return packFloat128( zSign, 0, 0, 0 );
  6106. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  6107. }
  6108. zExp = aExp - bExp + 0x3FFD;
  6109. shortShift128Left(
  6110. aSig0 | UINT64_C(0x0001000000000000), aSig1, 15, &aSig0, &aSig1 );
  6111. shortShift128Left(
  6112. bSig0 | UINT64_C(0x0001000000000000), bSig1, 15, &bSig0, &bSig1 );
  6113. if ( le128( bSig0, bSig1, aSig0, aSig1 ) ) {
  6114. shift128Right( aSig0, aSig1, 1, &aSig0, &aSig1 );
  6115. ++zExp;
  6116. }
  6117. zSig0 = estimateDiv128To64( aSig0, aSig1, bSig0 );
  6118. mul128By64To192( bSig0, bSig1, zSig0, &term0, &term1, &term2 );
  6119. sub192( aSig0, aSig1, 0, term0, term1, term2, &rem0, &rem1, &rem2 );
  6120. while ( (int64_t) rem0 < 0 ) {
  6121. --zSig0;
  6122. add192( rem0, rem1, rem2, 0, bSig0, bSig1, &rem0, &rem1, &rem2 );
  6123. }
  6124. zSig1 = estimateDiv128To64( rem1, rem2, bSig0 );
  6125. if ( ( zSig1 & 0x3FFF ) <= 4 ) {
  6126. mul128By64To192( bSig0, bSig1, zSig1, &term1, &term2, &term3 );
  6127. sub192( rem1, rem2, 0, term1, term2, term3, &rem1, &rem2, &rem3 );
  6128. while ( (int64_t) rem1 < 0 ) {
  6129. --zSig1;
  6130. add192( rem1, rem2, rem3, 0, bSig0, bSig1, &rem1, &rem2, &rem3 );
  6131. }
  6132. zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
  6133. }
  6134. shift128ExtraRightJamming( zSig0, zSig1, 0, 15, &zSig0, &zSig1, &zSig2 );
  6135. return roundAndPackFloat128(zSign, zExp, zSig0, zSig1, zSig2, status);
  6136. }
  6137. /*----------------------------------------------------------------------------
  6138. | Returns the remainder of the quadruple-precision floating-point value `a'
  6139. | with respect to the corresponding value `b'. The operation is performed
  6140. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  6141. *----------------------------------------------------------------------------*/
  6142. float128 float128_rem(float128 a, float128 b, float_status *status)
  6143. {
  6144. bool aSign, zSign;
  6145. int32_t aExp, bExp, expDiff;
  6146. uint64_t aSig0, aSig1, bSig0, bSig1, q, term0, term1, term2;
  6147. uint64_t allZero, alternateASig0, alternateASig1, sigMean1;
  6148. int64_t sigMean0;
  6149. aSig1 = extractFloat128Frac1( a );
  6150. aSig0 = extractFloat128Frac0( a );
  6151. aExp = extractFloat128Exp( a );
  6152. aSign = extractFloat128Sign( a );
  6153. bSig1 = extractFloat128Frac1( b );
  6154. bSig0 = extractFloat128Frac0( b );
  6155. bExp = extractFloat128Exp( b );
  6156. if ( aExp == 0x7FFF ) {
  6157. if ( ( aSig0 | aSig1 )
  6158. || ( ( bExp == 0x7FFF ) && ( bSig0 | bSig1 ) ) ) {
  6159. return propagateFloat128NaN(a, b, status);
  6160. }
  6161. goto invalid;
  6162. }
  6163. if ( bExp == 0x7FFF ) {
  6164. if (bSig0 | bSig1) {
  6165. return propagateFloat128NaN(a, b, status);
  6166. }
  6167. return a;
  6168. }
  6169. if ( bExp == 0 ) {
  6170. if ( ( bSig0 | bSig1 ) == 0 ) {
  6171. invalid:
  6172. float_raise(float_flag_invalid, status);
  6173. return float128_default_nan(status);
  6174. }
  6175. normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
  6176. }
  6177. if ( aExp == 0 ) {
  6178. if ( ( aSig0 | aSig1 ) == 0 ) return a;
  6179. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  6180. }
  6181. expDiff = aExp - bExp;
  6182. if ( expDiff < -1 ) return a;
  6183. shortShift128Left(
  6184. aSig0 | UINT64_C(0x0001000000000000),
  6185. aSig1,
  6186. 15 - ( expDiff < 0 ),
  6187. &aSig0,
  6188. &aSig1
  6189. );
  6190. shortShift128Left(
  6191. bSig0 | UINT64_C(0x0001000000000000), bSig1, 15, &bSig0, &bSig1 );
  6192. q = le128( bSig0, bSig1, aSig0, aSig1 );
  6193. if ( q ) sub128( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 );
  6194. expDiff -= 64;
  6195. while ( 0 < expDiff ) {
  6196. q = estimateDiv128To64( aSig0, aSig1, bSig0 );
  6197. q = ( 4 < q ) ? q - 4 : 0;
  6198. mul128By64To192( bSig0, bSig1, q, &term0, &term1, &term2 );
  6199. shortShift192Left( term0, term1, term2, 61, &term1, &term2, &allZero );
  6200. shortShift128Left( aSig0, aSig1, 61, &aSig0, &allZero );
  6201. sub128( aSig0, 0, term1, term2, &aSig0, &aSig1 );
  6202. expDiff -= 61;
  6203. }
  6204. if ( -64 < expDiff ) {
  6205. q = estimateDiv128To64( aSig0, aSig1, bSig0 );
  6206. q = ( 4 < q ) ? q - 4 : 0;
  6207. q >>= - expDiff;
  6208. shift128Right( bSig0, bSig1, 12, &bSig0, &bSig1 );
  6209. expDiff += 52;
  6210. if ( expDiff < 0 ) {
  6211. shift128Right( aSig0, aSig1, - expDiff, &aSig0, &aSig1 );
  6212. }
  6213. else {
  6214. shortShift128Left( aSig0, aSig1, expDiff, &aSig0, &aSig1 );
  6215. }
  6216. mul128By64To192( bSig0, bSig1, q, &term0, &term1, &term2 );
  6217. sub128( aSig0, aSig1, term1, term2, &aSig0, &aSig1 );
  6218. }
  6219. else {
  6220. shift128Right( aSig0, aSig1, 12, &aSig0, &aSig1 );
  6221. shift128Right( bSig0, bSig1, 12, &bSig0, &bSig1 );
  6222. }
  6223. do {
  6224. alternateASig0 = aSig0;
  6225. alternateASig1 = aSig1;
  6226. ++q;
  6227. sub128( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 );
  6228. } while ( 0 <= (int64_t) aSig0 );
  6229. add128(
  6230. aSig0, aSig1, alternateASig0, alternateASig1, (uint64_t *)&sigMean0, &sigMean1 );
  6231. if ( ( sigMean0 < 0 )
  6232. || ( ( ( sigMean0 | sigMean1 ) == 0 ) && ( q & 1 ) ) ) {
  6233. aSig0 = alternateASig0;
  6234. aSig1 = alternateASig1;
  6235. }
  6236. zSign = ( (int64_t) aSig0 < 0 );
  6237. if ( zSign ) sub128( 0, 0, aSig0, aSig1, &aSig0, &aSig1 );
  6238. return normalizeRoundAndPackFloat128(aSign ^ zSign, bExp - 4, aSig0, aSig1,
  6239. status);
  6240. }
  6241. /*----------------------------------------------------------------------------
  6242. | Returns the square root of the quadruple-precision floating-point value `a'.
  6243. | The operation is performed according to the IEC/IEEE Standard for Binary
  6244. | Floating-Point Arithmetic.
  6245. *----------------------------------------------------------------------------*/
  6246. float128 float128_sqrt(float128 a, float_status *status)
  6247. {
  6248. bool aSign;
  6249. int32_t aExp, zExp;
  6250. uint64_t aSig0, aSig1, zSig0, zSig1, zSig2, doubleZSig0;
  6251. uint64_t rem0, rem1, rem2, rem3, term0, term1, term2, term3;
  6252. aSig1 = extractFloat128Frac1( a );
  6253. aSig0 = extractFloat128Frac0( a );
  6254. aExp = extractFloat128Exp( a );
  6255. aSign = extractFloat128Sign( a );
  6256. if ( aExp == 0x7FFF ) {
  6257. if (aSig0 | aSig1) {
  6258. return propagateFloat128NaN(a, a, status);
  6259. }
  6260. if ( ! aSign ) return a;
  6261. goto invalid;
  6262. }
  6263. if ( aSign ) {
  6264. if ( ( aExp | aSig0 | aSig1 ) == 0 ) return a;
  6265. invalid:
  6266. float_raise(float_flag_invalid, status);
  6267. return float128_default_nan(status);
  6268. }
  6269. if ( aExp == 0 ) {
  6270. if ( ( aSig0 | aSig1 ) == 0 ) return packFloat128( 0, 0, 0, 0 );
  6271. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  6272. }
  6273. zExp = ( ( aExp - 0x3FFF )>>1 ) + 0x3FFE;
  6274. aSig0 |= UINT64_C(0x0001000000000000);
  6275. zSig0 = estimateSqrt32( aExp, aSig0>>17 );
  6276. shortShift128Left( aSig0, aSig1, 13 - ( aExp & 1 ), &aSig0, &aSig1 );
  6277. zSig0 = estimateDiv128To64( aSig0, aSig1, zSig0<<32 ) + ( zSig0<<30 );
  6278. doubleZSig0 = zSig0<<1;
  6279. mul64To128( zSig0, zSig0, &term0, &term1 );
  6280. sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
  6281. while ( (int64_t) rem0 < 0 ) {
  6282. --zSig0;
  6283. doubleZSig0 -= 2;
  6284. add128( rem0, rem1, zSig0>>63, doubleZSig0 | 1, &rem0, &rem1 );
  6285. }
  6286. zSig1 = estimateDiv128To64( rem1, 0, doubleZSig0 );
  6287. if ( ( zSig1 & 0x1FFF ) <= 5 ) {
  6288. if ( zSig1 == 0 ) zSig1 = 1;
  6289. mul64To128( doubleZSig0, zSig1, &term1, &term2 );
  6290. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  6291. mul64To128( zSig1, zSig1, &term2, &term3 );
  6292. sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
  6293. while ( (int64_t) rem1 < 0 ) {
  6294. --zSig1;
  6295. shortShift128Left( 0, zSig1, 1, &term2, &term3 );
  6296. term3 |= 1;
  6297. term2 |= doubleZSig0;
  6298. add192( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 );
  6299. }
  6300. zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 );
  6301. }
  6302. shift128ExtraRightJamming( zSig0, zSig1, 0, 14, &zSig0, &zSig1, &zSig2 );
  6303. return roundAndPackFloat128(0, zExp, zSig0, zSig1, zSig2, status);
  6304. }
  6305. static inline FloatRelation
  6306. floatx80_compare_internal(floatx80 a, floatx80 b, bool is_quiet,
  6307. float_status *status)
  6308. {
  6309. bool aSign, bSign;
  6310. if (floatx80_invalid_encoding(a) || floatx80_invalid_encoding(b)) {
  6311. float_raise(float_flag_invalid, status);
  6312. return float_relation_unordered;
  6313. }
  6314. if (( ( extractFloatx80Exp( a ) == 0x7fff ) &&
  6315. ( extractFloatx80Frac( a )<<1 ) ) ||
  6316. ( ( extractFloatx80Exp( b ) == 0x7fff ) &&
  6317. ( extractFloatx80Frac( b )<<1 ) )) {
  6318. if (!is_quiet ||
  6319. floatx80_is_signaling_nan(a, status) ||
  6320. floatx80_is_signaling_nan(b, status)) {
  6321. float_raise(float_flag_invalid, status);
  6322. }
  6323. return float_relation_unordered;
  6324. }
  6325. aSign = extractFloatx80Sign( a );
  6326. bSign = extractFloatx80Sign( b );
  6327. if ( aSign != bSign ) {
  6328. if ( ( ( (uint16_t) ( ( a.high | b.high ) << 1 ) ) == 0) &&
  6329. ( ( a.low | b.low ) == 0 ) ) {
  6330. /* zero case */
  6331. return float_relation_equal;
  6332. } else {
  6333. return 1 - (2 * aSign);
  6334. }
  6335. } else {
  6336. /* Normalize pseudo-denormals before comparison. */
  6337. if ((a.high & 0x7fff) == 0 && a.low & UINT64_C(0x8000000000000000)) {
  6338. ++a.high;
  6339. }
  6340. if ((b.high & 0x7fff) == 0 && b.low & UINT64_C(0x8000000000000000)) {
  6341. ++b.high;
  6342. }
  6343. if (a.low == b.low && a.high == b.high) {
  6344. return float_relation_equal;
  6345. } else {
  6346. return 1 - 2 * (aSign ^ ( lt128( a.high, a.low, b.high, b.low ) ));
  6347. }
  6348. }
  6349. }
  6350. FloatRelation floatx80_compare(floatx80 a, floatx80 b, float_status *status)
  6351. {
  6352. return floatx80_compare_internal(a, b, 0, status);
  6353. }
  6354. FloatRelation floatx80_compare_quiet(floatx80 a, floatx80 b,
  6355. float_status *status)
  6356. {
  6357. return floatx80_compare_internal(a, b, 1, status);
  6358. }
  6359. static inline FloatRelation
  6360. float128_compare_internal(float128 a, float128 b, bool is_quiet,
  6361. float_status *status)
  6362. {
  6363. bool aSign, bSign;
  6364. if (( ( extractFloat128Exp( a ) == 0x7fff ) &&
  6365. ( extractFloat128Frac0( a ) | extractFloat128Frac1( a ) ) ) ||
  6366. ( ( extractFloat128Exp( b ) == 0x7fff ) &&
  6367. ( extractFloat128Frac0( b ) | extractFloat128Frac1( b ) ) )) {
  6368. if (!is_quiet ||
  6369. float128_is_signaling_nan(a, status) ||
  6370. float128_is_signaling_nan(b, status)) {
  6371. float_raise(float_flag_invalid, status);
  6372. }
  6373. return float_relation_unordered;
  6374. }
  6375. aSign = extractFloat128Sign( a );
  6376. bSign = extractFloat128Sign( b );
  6377. if ( aSign != bSign ) {
  6378. if ( ( ( ( a.high | b.high )<<1 ) | a.low | b.low ) == 0 ) {
  6379. /* zero case */
  6380. return float_relation_equal;
  6381. } else {
  6382. return 1 - (2 * aSign);
  6383. }
  6384. } else {
  6385. if (a.low == b.low && a.high == b.high) {
  6386. return float_relation_equal;
  6387. } else {
  6388. return 1 - 2 * (aSign ^ ( lt128( a.high, a.low, b.high, b.low ) ));
  6389. }
  6390. }
  6391. }
  6392. FloatRelation float128_compare(float128 a, float128 b, float_status *status)
  6393. {
  6394. return float128_compare_internal(a, b, 0, status);
  6395. }
  6396. FloatRelation float128_compare_quiet(float128 a, float128 b,
  6397. float_status *status)
  6398. {
  6399. return float128_compare_internal(a, b, 1, status);
  6400. }
  6401. floatx80 floatx80_scalbn(floatx80 a, int n, float_status *status)
  6402. {
  6403. bool aSign;
  6404. int32_t aExp;
  6405. uint64_t aSig;
  6406. if (floatx80_invalid_encoding(a)) {
  6407. float_raise(float_flag_invalid, status);
  6408. return floatx80_default_nan(status);
  6409. }
  6410. aSig = extractFloatx80Frac( a );
  6411. aExp = extractFloatx80Exp( a );
  6412. aSign = extractFloatx80Sign( a );
  6413. if ( aExp == 0x7FFF ) {
  6414. if ( aSig<<1 ) {
  6415. return propagateFloatx80NaN(a, a, status);
  6416. }
  6417. return a;
  6418. }
  6419. if (aExp == 0) {
  6420. if (aSig == 0) {
  6421. return a;
  6422. }
  6423. aExp++;
  6424. }
  6425. if (n > 0x10000) {
  6426. n = 0x10000;
  6427. } else if (n < -0x10000) {
  6428. n = -0x10000;
  6429. }
  6430. aExp += n;
  6431. return normalizeRoundAndPackFloatx80(status->floatx80_rounding_precision,
  6432. aSign, aExp, aSig, 0, status);
  6433. }
  6434. float128 float128_scalbn(float128 a, int n, float_status *status)
  6435. {
  6436. bool aSign;
  6437. int32_t aExp;
  6438. uint64_t aSig0, aSig1;
  6439. aSig1 = extractFloat128Frac1( a );
  6440. aSig0 = extractFloat128Frac0( a );
  6441. aExp = extractFloat128Exp( a );
  6442. aSign = extractFloat128Sign( a );
  6443. if ( aExp == 0x7FFF ) {
  6444. if ( aSig0 | aSig1 ) {
  6445. return propagateFloat128NaN(a, a, status);
  6446. }
  6447. return a;
  6448. }
  6449. if (aExp != 0) {
  6450. aSig0 |= UINT64_C(0x0001000000000000);
  6451. } else if (aSig0 == 0 && aSig1 == 0) {
  6452. return a;
  6453. } else {
  6454. aExp++;
  6455. }
  6456. if (n > 0x10000) {
  6457. n = 0x10000;
  6458. } else if (n < -0x10000) {
  6459. n = -0x10000;
  6460. }
  6461. aExp += n - 1;
  6462. return normalizeRoundAndPackFloat128( aSign, aExp, aSig0, aSig1
  6463. , status);
  6464. }
  6465. static void __attribute__((constructor)) softfloat_init(void)
  6466. {
  6467. union_float64 ua, ub, uc, ur;
  6468. if (QEMU_NO_HARDFLOAT) {
  6469. return;
  6470. }
  6471. /*
  6472. * Test that the host's FMA is not obviously broken. For example,
  6473. * glibc < 2.23 can perform an incorrect FMA on certain hosts; see
  6474. * https://sourceware.org/bugzilla/show_bug.cgi?id=13304
  6475. */
  6476. ua.s = 0x0020000000000001ULL;
  6477. ub.s = 0x3ca0000000000000ULL;
  6478. uc.s = 0x0020000000000000ULL;
  6479. ur.h = fma(ua.h, ub.h, uc.h);
  6480. if (ur.s != 0x0020000000000001ULL) {
  6481. force_soft_fma = true;
  6482. }
  6483. }