softfpu.pp 278 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984
  1. {*
  2. ===============================================================================
  3. The original notice of the softfloat package is shown below. The conversion
  4. to pascal was done by Carl Eric Codere in 2002 ([email protected]).
  5. ===============================================================================
  6. This C source file is part of the SoftFloat IEC/IEEE Floating-Point
  7. Arithmetic Package, Release 2a.
  8. Written by John R. Hauser. This work was made possible in part by the
  9. International Computer Science Institute, located at Suite 600, 1947 Center
  10. Street, Berkeley, California 94704. Funding was partially provided by the
  11. National Science Foundation under grant MIP-9311980. The original version
  12. of this code was written as part of a project to build a fixed-point vector
  13. processor in collaboration with the University of California at Berkeley,
  14. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  15. is available through the Web page
  16. `http://HTTP.CS.Berkeley.EDU/~jhauser/arithmetic/SoftFloat.html'.
  17. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  18. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  19. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  20. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  21. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  22. Derivative works are acceptable, even for commercial purposes, so long as
  23. (1) they include prominent notice that the work is derivative, and (2) they
  24. include prominent notice akin to these four paragraphs for those parts of
  25. this code that are retained.
  26. ===============================================================================
  27. The float80 and float128 part is translated from the softfloat package
  28. by Florian Klaempfl and contained the following copyright notice
  29. The code might contain some duplicate stuff because the floatx80/float128 port was
  30. done based on the 64 bit enabled softfloat code.
  31. ===============================================================================
  32. This C source file is part of the SoftFloat IEC/IEEE Floating-point Arithmetic
  33. Package, Release 2b.
  34. Written by John R. Hauser. This work was made possible in part by the
  35. International Computer Science Institute, located at Suite 600, 1947 Center
  36. Street, Berkeley, California 94704. Funding was partially provided by the
  37. National Science Foundation under grant MIP-9311980. The original version
  38. of this code was written as part of a project to build a fixed-point vector
  39. processor in collaboration with the University of California at Berkeley,
  40. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  41. is available through the Web page `http://www.cs.berkeley.edu/~jhauser/
  42. arithmetic/SoftFloat.html'.
  43. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has
  44. been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES
  45. RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS
  46. AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES,
  47. COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE
  48. EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE
  49. INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR
  50. OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE.
  51. Derivative works are acceptable, even for commercial purposes, so long as
  52. (1) the source code for the derivative work includes prominent notice that
  53. the work is derivative, and (2) the source code includes prominent notice with
  54. these four paragraphs for those parts of this code that are retained.
  55. ===============================================================================
  56. *}
  57. { $define FPC_SOFTFLOAT_FLOATX80}
  58. { $define FPC_SOFTFLOAT_FLOAT128}
  59. { the softfpu unit can be also embedded directly into the system unit }
  60. {$if not(defined(fpc_softfpu_interface)) and not(defined(fpc_softfpu_implementation))}
  61. {$mode objfpc}
  62. unit softfpu;
  63. { Overflow checking must be disabled,
  64. since some operations expect overflow!
  65. }
  66. {$Q-}
  67. {$goto on}
  68. interface
  69. {$endif not(defined(fpc_softfpu_interface)) and not(defined(fpc_softfpu_implementation))}
  70. {$if not(defined(fpc_softfpu_implementation))}
  71. {
  72. -------------------------------------------------------------------------------
  73. Software IEC/IEEE floating-point types.
  74. -------------------------------------------------------------------------------
  75. }
  76. TYPE
  77. float32 = longword;
  78. { we use here a record in the function header because
  79. the record allows bitwise conversion to single }
  80. float32rec = record
  81. float32 : float32;
  82. end;
  83. flag = byte;
  84. uint8 = byte;
  85. int8 = shortint;
  86. uint16 = word;
  87. int16 = smallint;
  88. uint32 = longword;
  89. int32 = longint;
  90. bits8 = byte;
  91. sbits8 = shortint;
  92. bits16 = word;
  93. sbits16 = smallint;
  94. sbits32 = longint;
  95. bits32 = longword;
  96. {$ifndef fpc}
  97. qword = int64;
  98. {$endif}
  99. uint64 = qword;
  100. bits64 = qword;
  101. sbits64 = int64;
  102. {$ifdef ENDIAN_LITTLE}
  103. float64 = packed record
  104. low: bits32;
  105. high: bits32;
  106. end;
  107. int64rec = packed record
  108. low: bits32;
  109. high: bits32;
  110. end;
  111. floatx80 = packed record
  112. low : qword;
  113. high : word;
  114. end;
  115. float128 = packed record
  116. low : qword;
  117. high : qword;
  118. end;
  119. {$else}
  120. float64 = packed record
  121. high,low : bits32;
  122. end;
  123. int64rec = packed record
  124. high,low : bits32;
  125. end;
  126. floatx80 = packed record
  127. high : word;
  128. low : qword;
  129. end;
  130. float128 = packed record
  131. high : qword;
  132. low : qword;
  133. end;
  134. {$endif}
  135. {*
  136. -------------------------------------------------------------------------------
  137. Returns 1 if the double-precision floating-point value `a' is less than
  138. the corresponding value `b', and 0 otherwise. The comparison is performed
  139. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  140. -------------------------------------------------------------------------------
  141. *}
  142. Function float64_lt(a: float64;b: float64): flag; compilerproc;
  143. {*
  144. -------------------------------------------------------------------------------
  145. Returns 1 if the double-precision floating-point value `a' is less than
  146. or equal to the corresponding value `b', and 0 otherwise. The comparison
  147. is performed according to the IEC/IEEE Standard for Binary Floating-Point
  148. Arithmetic.
  149. -------------------------------------------------------------------------------
  150. *}
  151. Function float64_le(a: float64;b: float64): flag; compilerproc;
  152. {*
  153. -------------------------------------------------------------------------------
  154. Returns 1 if the double-precision floating-point value `a' is equal to
  155. the corresponding value `b', and 0 otherwise. The comparison is performed
  156. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  157. -------------------------------------------------------------------------------
  158. *}
  159. Function float64_eq(a: float64;b: float64): flag; compilerproc;
  160. {*
  161. -------------------------------------------------------------------------------
  162. Returns the square root of the double-precision floating-point value `a'.
  163. The operation is performed according to the IEC/IEEE Standard for Binary
  164. Floating-Point Arithmetic.
  165. -------------------------------------------------------------------------------
  166. *}
  167. Procedure float64_sqrt( a: float64; var out: float64 ); compilerproc;
  168. {*
  169. -------------------------------------------------------------------------------
  170. Returns the remainder of the double-precision floating-point value `a'
  171. with respect to the corresponding value `b'. The operation is performed
  172. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  173. -------------------------------------------------------------------------------
  174. *}
  175. Function float64_rem(a: float64; b : float64) : float64; compilerproc;
  176. {*
  177. -------------------------------------------------------------------------------
  178. Returns the result of dividing the double-precision floating-point value `a'
  179. by the corresponding value `b'. The operation is performed according to the
  180. IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  181. -------------------------------------------------------------------------------
  182. *}
  183. Function float64_div(a: float64; b : float64) : float64; compilerproc;
  184. {*
  185. -------------------------------------------------------------------------------
  186. Returns the result of multiplying the double-precision floating-point values
  187. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  188. for Binary Floating-Point Arithmetic.
  189. -------------------------------------------------------------------------------
  190. *}
  191. Function float64_mul( a: float64; b:float64) : float64; compilerproc;
  192. {*
  193. -------------------------------------------------------------------------------
  194. Returns the result of subtracting the double-precision floating-point values
  195. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  196. for Binary Floating-Point Arithmetic.
  197. -------------------------------------------------------------------------------
  198. *}
  199. Function float64_sub(a: float64; b : float64) : float64; compilerproc;
  200. {*
  201. -------------------------------------------------------------------------------
  202. Returns the result of adding the double-precision floating-point values `a'
  203. and `b'. The operation is performed according to the IEC/IEEE Standard for
  204. Binary Floating-Point Arithmetic.
  205. -------------------------------------------------------------------------------
  206. *}
  207. Function float64_add( a: float64; b : float64) : float64; compilerproc;
  208. {*
  209. -------------------------------------------------------------------------------
  210. Rounds the double-precision floating-point value `a' to an integer,
  211. and returns the result as a double-precision floating-point value. The
  212. operation is performed according to the IEC/IEEE Standard for Binary
  213. Floating-Point Arithmetic.
  214. -------------------------------------------------------------------------------
  215. *}
  216. Function float64_round_to_int(a: float64) : float64; compilerproc;
  217. {*
  218. -------------------------------------------------------------------------------
  219. Returns the result of converting the double-precision floating-point value
  220. `a' to the single-precision floating-point format. The conversion is
  221. performed according to the IEC/IEEE Standard for Binary Floating-Point
  222. Arithmetic.
  223. -------------------------------------------------------------------------------
  224. *}
  225. Function float64_to_float32(a: float64) : float32rec; compilerproc;
  226. {*
  227. -------------------------------------------------------------------------------
  228. Returns the result of converting the double-precision floating-point value
  229. `a' to the 32-bit two's complement integer format. The conversion is
  230. performed according to the IEC/IEEE Standard for Binary Floating-Point
  231. Arithmetic, except that the conversion is always rounded toward zero.
  232. If `a' is a NaN, the largest positive integer is returned. Otherwise, if
  233. the conversion overflows, the largest integer with the same sign as `a' is
  234. returned.
  235. -------------------------------------------------------------------------------
  236. *}
  237. Function float64_to_int32_round_to_zero(a: float64 ): int32; compilerproc;
  238. {*
  239. -------------------------------------------------------------------------------
  240. Returns the result of converting the double-precision floating-point value
  241. `a' to the 32-bit two's complement integer format. The conversion is
  242. performed according to the IEC/IEEE Standard for Binary Floating-Point
  243. Arithmetic---which means in particular that the conversion is rounded
  244. according to the current rounding mode. If `a' is a NaN, the largest
  245. positive integer is returned. Otherwise, if the conversion overflows, the
  246. largest integer with the same sign as `a' is returned.
  247. -------------------------------------------------------------------------------
  248. *}
  249. Function float64_to_int32(a: float64): int32; compilerproc;
  250. {*
  251. -------------------------------------------------------------------------------
  252. Returns 1 if the single-precision floating-point value `a' is less than
  253. the corresponding value `b', and 0 otherwise. The comparison is performed
  254. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  255. -------------------------------------------------------------------------------
  256. *}
  257. Function float32_lt( a:float32rec ; b : float32rec): flag; compilerproc;
  258. {*
  259. -------------------------------------------------------------------------------
  260. Returns 1 if the single-precision floating-point value `a' is less than
  261. or equal to the corresponding value `b', and 0 otherwise. The comparison
  262. is performed according to the IEC/IEEE Standard for Binary Floating-Point
  263. Arithmetic.
  264. -------------------------------------------------------------------------------
  265. *}
  266. Function float32_le( a: float32rec; b : float32rec ):flag; compilerproc;
  267. {*
  268. -------------------------------------------------------------------------------
  269. Returns 1 if the single-precision floating-point value `a' is equal to
  270. the corresponding value `b', and 0 otherwise. The comparison is performed
  271. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  272. -------------------------------------------------------------------------------
  273. *}
  274. Function float32_eq( a:float32rec; b:float32rec): flag; compilerproc;
  275. {*
  276. -------------------------------------------------------------------------------
  277. Returns the square root of the single-precision floating-point value `a'.
  278. The operation is performed according to the IEC/IEEE Standard for Binary
  279. Floating-Point Arithmetic.
  280. -------------------------------------------------------------------------------
  281. *}
  282. Function float32_sqrt(a: float32rec ): float32rec; compilerproc;
  283. {*
  284. -------------------------------------------------------------------------------
  285. Returns the remainder of the single-precision floating-point value `a'
  286. with respect to the corresponding value `b'. The operation is performed
  287. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  288. -------------------------------------------------------------------------------
  289. *}
  290. Function float32_rem(a: float32rec; b: float32rec ):float32rec; compilerproc;
  291. {*
  292. -------------------------------------------------------------------------------
  293. Returns the result of dividing the single-precision floating-point value `a'
  294. by the corresponding value `b'. The operation is performed according to the
  295. IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  296. -------------------------------------------------------------------------------
  297. *}
  298. Function float32_div(a: float32rec;b: float32rec ): float32rec; compilerproc;
  299. {*
  300. -------------------------------------------------------------------------------
  301. Returns the result of multiplying the single-precision floating-point values
  302. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  303. for Binary Floating-Point Arithmetic.
  304. -------------------------------------------------------------------------------
  305. *}
  306. Function float32_mul(a: float32rec; b: float32rec ) : float32rec; compilerproc;
  307. {*
  308. -------------------------------------------------------------------------------
  309. Returns the result of subtracting the single-precision floating-point values
  310. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  311. for Binary Floating-Point Arithmetic.
  312. -------------------------------------------------------------------------------
  313. *}
  314. Function float32_sub( a: float32rec ; b:float32rec ): float32rec; compilerproc;
  315. {*
  316. -------------------------------------------------------------------------------
  317. Returns the result of adding the single-precision floating-point values `a'
  318. and `b'. The operation is performed according to the IEC/IEEE Standard for
  319. Binary Floating-Point Arithmetic.
  320. -------------------------------------------------------------------------------
  321. *}
  322. Function float32_add( a: float32rec; b:float32rec ): float32rec; compilerproc;
  323. {*
  324. -------------------------------------------------------------------------------
  325. Rounds the single-precision floating-point value `a' to an integer,
  326. and returns the result as a single-precision floating-point value. The
  327. operation is performed according to the IEC/IEEE Standard for Binary
  328. Floating-Point Arithmetic.
  329. -------------------------------------------------------------------------------
  330. *}
  331. Function float32_round_to_int( a: float32rec): float32rec; compilerproc;
  332. {*
  333. -------------------------------------------------------------------------------
  334. Returns the result of converting the single-precision floating-point value
  335. `a' to the double-precision floating-point format. The conversion is
  336. performed according to the IEC/IEEE Standard for Binary Floating-Point
  337. Arithmetic.
  338. -------------------------------------------------------------------------------
  339. *}
  340. Function float32_to_float64( a : float32rec) : Float64; compilerproc;
  341. {*
  342. -------------------------------------------------------------------------------
  343. Returns the result of converting the single-precision floating-point value
  344. `a' to the 32-bit two's complement integer format. The conversion is
  345. performed according to the IEC/IEEE Standard for Binary Floating-Point
  346. Arithmetic, except that the conversion is always rounded toward zero.
  347. If `a' is a NaN, the largest positive integer is returned. Otherwise, if
  348. the conversion overflows, the largest integer with the same sign as `a' is
  349. returned.
  350. -------------------------------------------------------------------------------
  351. *}
  352. Function float32_to_int32_round_to_zero( a: Float32rec ): int32; compilerproc;
  353. {*
  354. -------------------------------------------------------------------------------
  355. Returns the result of converting the single-precision floating-point value
  356. `a' to the 32-bit two's complement integer format. The conversion is
  357. performed according to the IEC/IEEE Standard for Binary Floating-Point
  358. Arithmetic---which means in particular that the conversion is rounded
  359. according to the current rounding mode. If `a' is a NaN, the largest
  360. positive integer is returned. Otherwise, if the conversion overflows, the
  361. largest integer with the same sign as `a' is returned.
  362. -------------------------------------------------------------------------------
  363. *}
  364. Function float32_to_int32( a : float32rec) : int32; compilerproc;
  365. {*
  366. -------------------------------------------------------------------------------
  367. Returns the result of converting the 32-bit two's complement integer `a' to
  368. the double-precision floating-point format. The conversion is performed
  369. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  370. -------------------------------------------------------------------------------
  371. *}
  372. Function int32_to_float64( a: int32) : float64; compilerproc;
  373. {*
  374. -------------------------------------------------------------------------------
  375. Returns the result of converting the 32-bit two's complement integer `a' to
  376. the single-precision floating-point format. The conversion is performed
  377. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  378. -------------------------------------------------------------------------------
  379. *}
  380. Function int32_to_float32( a: int32): float32rec; compilerproc;
  381. {*----------------------------------------------------------------------------
  382. | Returns the result of converting the 64-bit two's complement integer `a'
  383. | to the double-precision floating-point format. The conversion is performed
  384. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  385. *----------------------------------------------------------------------------*}
  386. Function int64_to_float64( a: int64 ): float64; compilerproc;
  387. {*----------------------------------------------------------------------------
  388. | Returns the result of converting the 64-bit two's complement integer `a'
  389. | to the single-precision floating-point format. The conversion is performed
  390. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  391. *----------------------------------------------------------------------------*}
  392. Function int64_to_float32( a: int64 ): float32rec; compilerproc;
  393. CONST
  394. {-------------------------------------------------------------------------------
  395. Software IEC/IEEE floating-point underflow tininess-detection mode.
  396. -------------------------------------------------------------------------------
  397. *}
  398. float_tininess_after_rounding = 0;
  399. float_tininess_before_rounding = 1;
  400. {*
  401. -------------------------------------------------------------------------------
  402. Software IEC/IEEE floating-point rounding mode.
  403. -------------------------------------------------------------------------------
  404. *}
  405. {
  406. Round to nearest.
  407. This is the default mode. It should be used unless there is a specific
  408. need for one of the others. In this mode results are rounded to the
  409. nearest representable value. If the result is midway between two
  410. representable values, the even representable is chosen. Even here
  411. means the lowest-order bit is zero. This rounding mode prevents
  412. statistical bias and guarantees numeric stability: round-off errors
  413. in a lengthy calculation will remain smaller than half of FLT_EPSILON.
  414. Round toward plus Infinity.
  415. All results are rounded to the smallest representable value which is
  416. greater than the result.
  417. Round toward minus Infinity.
  418. All results are rounded to the largest representable value which is
  419. less than the result.
  420. Round toward zero.
  421. All results are rounded to the largest representable value whose
  422. magnitude is less than that of the result. In other words, if the
  423. result is negative it is rounded up; if it is positive, it is
  424. rounded down.
  425. }
  426. float_round_nearest_even = 0;
  427. float_round_down = 1;
  428. float_round_up = 2;
  429. float_round_to_zero = 3;
  430. {*
  431. -------------------------------------------------------------------------------
  432. Floating-point rounding mode and exception flags.
  433. -------------------------------------------------------------------------------
  434. *}
  435. const
  436. float_rounding_mode : Byte = float_round_nearest_even;
  437. {*
  438. -------------------------------------------------------------------------------
  439. Underflow tininess-detection mode, statically initialized to default value.
  440. (The declaration in `softfloat.h' must match the `int8' type here.)
  441. -------------------------------------------------------------------------------
  442. *}
  443. const float_detect_tininess: int8 = float_tininess_after_rounding;
  444. {$endif not(defined(fpc_softfpu_implementation))}
  445. {$if not(defined(fpc_softfpu_interface)) and not(defined(fpc_softfpu_implementation))}
  446. implementation
  447. {$endif not(defined(fpc_softfpu_interface)) and not(defined(fpc_softfpu_implementation))}
  448. {$if not(defined(fpc_softfpu_interface))}
  449. (*****************************************************************************)
  450. (*----------------------------------------------------------------------------*)
  451. (* Primitive arithmetic functions, including multi-word arithmetic, and *)
  452. (* division and square root approximations. (Can be specialized to target if *)
  453. (* desired.) *)
  454. (* ---------------------------------------------------------------------------*)
  455. (*****************************************************************************)
  456. {*----------------------------------------------------------------------------
  457. | Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
  458. | and 7, and returns the properly rounded 32-bit integer corresponding to the
  459. | input. If `zSign' is 1, the input is negated before being converted to an
  460. | integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point input
  461. | is simply rounded to an integer, with the inexact exception raised if the
  462. | input cannot be represented exactly as an integer. However, if the fixed-
  463. | point input is too large, the invalid exception is raised and the largest
  464. | positive or negative integer is returned.
  465. *----------------------------------------------------------------------------*}
  466. function roundAndPackInt32( zSign: flag; absZ : bits64): int32;
  467. var
  468. roundingMode: int8;
  469. roundNearestEven: flag;
  470. roundIncrement, roundBits: int8;
  471. z: int32;
  472. begin
  473. roundingMode := float_rounding_mode;
  474. roundNearestEven := ord( roundingMode = float_round_nearest_even );
  475. roundIncrement := $40;
  476. if ( roundNearestEven=0 ) then
  477. begin
  478. if ( roundingMode = float_round_to_zero ) then
  479. begin
  480. roundIncrement := 0;
  481. end
  482. else begin
  483. roundIncrement := $7F;
  484. if ( zSign<>0 ) then
  485. begin
  486. if ( roundingMode = float_round_up ) then
  487. roundIncrement := 0;
  488. end
  489. else begin
  490. if ( roundingMode = float_round_down ) then
  491. roundIncrement := 0;
  492. end;
  493. end;
  494. end;
  495. roundBits := absZ and $7F;
  496. absZ := ( absZ + roundIncrement ) shr 7;
  497. absZ := absZ and not( ord( ( roundBits xor $40 ) = 0 ) and roundNearestEven );
  498. z := absZ;
  499. if ( zSign<>0 ) then
  500. z := - z;
  501. if ( ( absZ shr 32 ) or ( z and ( ord( z < 0 ) xor zSign ) ) )<>0 then
  502. begin
  503. float_raise( float_flag_invalid );
  504. if zSign<>0 then
  505. result:=sbits32($80000000)
  506. else
  507. result:=$7FFFFFFF;
  508. exit;
  509. end;
  510. if ( roundBits<>0 ) then
  511. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  512. result:=z;
  513. end;
  514. {*----------------------------------------------------------------------------
  515. | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
  516. | `absZ1', with binary point between bits 63 and 64 (between the input words),
  517. | and returns the properly rounded 64-bit integer corresponding to the input.
  518. | If `zSign' is 1, the input is negated before being converted to an integer.
  519. | Ordinarily, the fixed-point input is simply rounded to an integer, with
  520. | the inexact exception raised if the input cannot be represented exactly as
  521. | an integer. However, if the fixed-point input is too large, the invalid
  522. | exception is raised and the largest positive or negative integer is
  523. | returned.
  524. *----------------------------------------------------------------------------*}
  525. function roundAndPackInt64( zSign: flag; absZ0: bits64; absZ1 : bits64): int64;
  526. var
  527. roundingMode: int8;
  528. roundNearestEven, increment: flag;
  529. z: int64;
  530. label
  531. overflow;
  532. begin
  533. roundingMode := float_rounding_mode;
  534. roundNearestEven := ord( roundingMode = float_round_nearest_even );
  535. increment := ord( sbits64(absZ1) < 0 );
  536. if ( roundNearestEven=0 ) then
  537. begin
  538. if ( roundingMode = float_round_to_zero ) then
  539. begin
  540. increment := 0;
  541. end
  542. else begin
  543. if ( zSign<>0 ) then
  544. begin
  545. increment := ord(( roundingMode = float_round_down ) and (absZ1<>0));
  546. end
  547. else begin
  548. increment := ord(( roundingMode = float_round_up ) and (absZ1<>0));
  549. end;
  550. end;
  551. end;
  552. if ( increment<>0 ) then
  553. begin
  554. inc(absZ0);
  555. if ( absZ0 = 0 ) then
  556. goto overflow;
  557. absZ0 := absZ0 and not( ord( bits64( absZ1 shl 1 ) = 0 ) and roundNearestEven );
  558. end;
  559. z := absZ0;
  560. if ( zSign<>0 ) then
  561. z := - z;
  562. if ( (z<>0) and (( ord( z < 0 ) xor zSign )<>0) ) then
  563. begin
  564. overflow:
  565. float_raise( float_flag_invalid );
  566. if zSign<>0 then
  567. result:=int64($8000000000000000)
  568. else
  569. result:=int64($7FFFFFFFFFFFFFFF);
  570. end;
  571. if ( absZ1<>0 ) then
  572. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  573. result:=z;
  574. end;
  575. {*
  576. -------------------------------------------------------------------------------
  577. Shifts `a' right by the number of bits given in `count'. If any nonzero
  578. bits are shifted off, they are ``jammed'' into the least significant bit of
  579. the result by setting the least significant bit to 1. The value of `count'
  580. can be arbitrarily large; in particular, if `count' is greater than 32, the
  581. result will be either 0 or 1, depending on whether `a' is zero or nonzero.
  582. The result is stored in the location pointed to by `zPtr'.
  583. -------------------------------------------------------------------------------
  584. *}
  585. Procedure shift32RightJamming( a: bits32 ; count: int16 ; VAR zPtr :bits32);
  586. var
  587. z: Bits32;
  588. Begin
  589. if ( count = 0 ) then
  590. z := a
  591. else
  592. if ( count < 32 ) then
  593. Begin
  594. z := ( a shr count ) or bits32( (( a shl ( ( - count ) AND 31 )) ) <> 0);
  595. End
  596. else
  597. Begin
  598. z := bits32( a <> 0 );
  599. End;
  600. zPtr := z;
  601. End;
  602. {*----------------------------------------------------------------------------
  603. | Shifts the 128-bit value formed by concatenating `a0' and `a1' right by the
  604. | number of bits given in `count'. Any bits shifted off are lost. The value
  605. | of `count' can be arbitrarily large; in particular, if `count' is greater
  606. | than 128, the result will be 0. The result is broken into two 64-bit pieces
  607. | which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  608. *----------------------------------------------------------------------------*}
  609. procedure shift128Right(a0: bits64; a1: bits64; count: int16; var z0Ptr: bits64; z1Ptr : bits64);
  610. var
  611. z0, z1: bits64;
  612. negCount: int8;
  613. begin
  614. negCount := ( - count ) and 63;
  615. if ( count = 0 ) then
  616. begin
  617. z1 := a1;
  618. z0 := a0;
  619. end
  620. else if ( count < 64 ) then
  621. begin
  622. z1 := ( a0 shl negCount ) or ( a1 shr count );
  623. z0 := a0 shr count;
  624. end
  625. else
  626. begin
  627. if ( count shl 64 )<>0 then
  628. z1 := a0 shr ( count and 63 )
  629. else
  630. z1 := 0;
  631. z0 := 0;
  632. end;
  633. z1Ptr := z1;
  634. z0Ptr := z0;
  635. end;
  636. {*
  637. -------------------------------------------------------------------------------
  638. Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the
  639. number of bits given in `count'. Any bits shifted off are lost. The value
  640. of `count' can be arbitrarily large; in particular, if `count' is greater
  641. than 64, the result will be 0. The result is broken into two 32-bit pieces
  642. which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  643. -------------------------------------------------------------------------------
  644. *}
  645. Procedure
  646. shift64Right(
  647. a0 :bits32; a1: bits32; count:int16; VAR z0Ptr:bits32; VAR z1Ptr:bits32);
  648. Var
  649. z0, z1: bits32;
  650. negCount : int8;
  651. Begin
  652. negCount := ( - count ) AND 31;
  653. if ( count = 0 ) then
  654. Begin
  655. z1 := a1;
  656. z0 := a0;
  657. End
  658. else if ( count < 32 ) then
  659. Begin
  660. z1 := ( a0 shl negCount ) OR ( a1 shr count );
  661. z0 := a0 shr count;
  662. End
  663. else
  664. Begin
  665. if (count < 64) then
  666. z1 := ( a0 shr ( count AND 31 ) )
  667. else
  668. z1 := 0;
  669. z0 := 0;
  670. End;
  671. z1Ptr := z1;
  672. z0Ptr := z0;
  673. End;
  674. {*
  675. -------------------------------------------------------------------------------
  676. Shifts the 64-bit value formed by concatenating `a0' and `a1' right by the
  677. number of bits given in `count'. If any nonzero bits are shifted off, they
  678. are ``jammed'' into the least significant bit of the result by setting the
  679. least significant bit to 1. The value of `count' can be arbitrarily large;
  680. in particular, if `count' is greater than 64, the result will be either 0
  681. or 1, depending on whether the concatenation of `a0' and `a1' is zero or
  682. nonzero. The result is broken into two 32-bit pieces which are stored at
  683. the locations pointed to by `z0Ptr' and `z1Ptr'.
  684. -------------------------------------------------------------------------------
  685. *}
  686. Procedure
  687. shift64RightJamming(
  688. a0:bits32; a1: bits32; count:int16; VAR Z0Ptr :bits32;VAR z1Ptr: bits32 );
  689. VAR
  690. z0, z1 : bits32;
  691. negCount : int8;
  692. Begin
  693. negCount := ( - count ) AND 31;
  694. if ( count = 0 ) then
  695. Begin
  696. z1 := a1;
  697. z0 := a0;
  698. End
  699. else
  700. if ( count < 32 ) then
  701. Begin
  702. z1 := ( a0 shl negCount ) OR ( a1 shr count ) OR bits32( ( a1 shl negCount ) <> 0 );
  703. z0 := a0 shr count;
  704. End
  705. else
  706. Begin
  707. if ( count = 32 ) then
  708. Begin
  709. z1 := a0 OR bits32( a1 <> 0 );
  710. End
  711. else
  712. if ( count < 64 ) Then
  713. Begin
  714. z1 := ( a0 shr ( count AND 31 ) ) OR bits32( ( ( a0 shl negCount ) OR a1 ) <> 0 );
  715. End
  716. else
  717. Begin
  718. z1 := bits32( ( a0 OR a1 ) <> 0 );
  719. End;
  720. z0 := 0;
  721. End;
  722. z1Ptr := z1;
  723. z0Ptr := z0;
  724. End;
  725. {*----------------------------------------------------------------------------
  726. | Shifts `a' right by the number of bits given in `count'. If any nonzero
  727. | bits are shifted off, they are ``jammed'' into the least significant bit of
  728. | the result by setting the least significant bit to 1. The value of `count'
  729. | can be arbitrarily large; in particular, if `count' is greater than 64, the
  730. | result will be either 0 or 1, depending on whether `a' is zero or nonzero.
  731. | The result is stored in the location pointed to by `zPtr'.
  732. *----------------------------------------------------------------------------*}
  733. procedure shift64RightJamming(a: bits64; count: int16; var zPtr : bits64);
  734. var
  735. z: bits64;
  736. begin
  737. if ( count = 0 ) then
  738. begin
  739. z := a;
  740. end
  741. else if ( count < 64 ) then
  742. begin
  743. z := ( a shr count ) or ord( ( a shl ( ( - count ) and 63 ) ) <> 0 );
  744. end
  745. else
  746. begin
  747. z := ord( a <> 0 );
  748. end;
  749. zPtr := z;
  750. end;
  751. {*
  752. -------------------------------------------------------------------------------
  753. Shifts the 96-bit value formed by concatenating `a0', `a1', and `a2' right
  754. by 32 _plus_ the number of bits given in `count'. The shifted result is
  755. at most 64 nonzero bits; these are broken into two 32-bit pieces which are
  756. stored at the locations pointed to by `z0Ptr' and `z1Ptr'. The bits shifted
  757. off form a third 32-bit result as follows: The _last_ bit shifted off is
  758. the most-significant bit of the extra result, and the other 31 bits of the
  759. extra result are all zero if and only if _all_but_the_last_ bits shifted off
  760. were all zero. This extra result is stored in the location pointed to by
  761. `z2Ptr'. The value of `count' can be arbitrarily large.
  762. (This routine makes more sense if `a0', `a1', and `a2' are considered
  763. to form a fixed-point value with binary point between `a1' and `a2'. This
  764. fixed-point value is shifted right by the number of bits given in `count',
  765. and the integer part of the result is returned at the locations pointed to
  766. by `z0Ptr' and `z1Ptr'. The fractional part of the result may be slightly
  767. corrupted as described above, and is returned at the location pointed to by
  768. `z2Ptr'.)
  769. -------------------------------------------------------------------------------
  770. }
  771. Procedure
  772. shift64ExtraRightJamming(
  773. a0: bits32;
  774. a1: bits32;
  775. a2: bits32;
  776. count: int16;
  777. VAR z0Ptr: bits32;
  778. VAR z1Ptr: bits32;
  779. VAR z2Ptr: bits32
  780. );
  781. Var
  782. z0, z1, z2: bits32;
  783. negCount : int8;
  784. Begin
  785. negCount := ( - count ) AND 31;
  786. if ( count = 0 ) then
  787. Begin
  788. z2 := a2;
  789. z1 := a1;
  790. z0 := a0;
  791. End
  792. else
  793. Begin
  794. if ( count < 32 ) Then
  795. Begin
  796. z2 := a1 shl negCount;
  797. z1 := ( a0 shl negCount ) OR ( a1 shr count );
  798. z0 := a0 shr count;
  799. End
  800. else
  801. Begin
  802. if ( count = 32 ) then
  803. Begin
  804. z2 := a1;
  805. z1 := a0;
  806. End
  807. else
  808. Begin
  809. a2 := a2 or a1;
  810. if ( count < 64 ) then
  811. Begin
  812. z2 := a0 shl negCount;
  813. z1 := a0 shr ( count AND 31 );
  814. End
  815. else
  816. Begin
  817. if count = 64 then
  818. z2 := a0
  819. else
  820. z2 := bits32(a0 <> 0);
  821. z1 := 0;
  822. End;
  823. End;
  824. z0 := 0;
  825. End;
  826. z2 := z2 or bits32( a2 <> 0 );
  827. End;
  828. z2Ptr := z2;
  829. z1Ptr := z1;
  830. z0Ptr := z0;
  831. End;
  832. {*
  833. -------------------------------------------------------------------------------
  834. Shifts the 64-bit value formed by concatenating `a0' and `a1' left by the
  835. number of bits given in `count'. Any bits shifted off are lost. The value
  836. of `count' must be less than 32. The result is broken into two 32-bit
  837. pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  838. -------------------------------------------------------------------------------
  839. *}
  840. Procedure
  841. shortShift64Left(
  842. a0:bits32; a1:bits32; count:int16; VAR z0Ptr:bits32; VAR z1Ptr:bits32 );
  843. Begin
  844. z1Ptr := a1 shl count;
  845. if count = 0 then
  846. z0Ptr := a0
  847. else
  848. z0Ptr := ( a0 shl count ) OR ( a1 shr ( ( - count ) AND 31 ) );
  849. End;
  850. {*
  851. -------------------------------------------------------------------------------
  852. Shifts the 96-bit value formed by concatenating `a0', `a1', and `a2' left
  853. by the number of bits given in `count'. Any bits shifted off are lost.
  854. The value of `count' must be less than 32. The result is broken into three
  855. 32-bit pieces which are stored at the locations pointed to by `z0Ptr',
  856. `z1Ptr', and `z2Ptr'.
  857. -------------------------------------------------------------------------------
  858. *}
  859. Procedure
  860. shortShift96Left(
  861. a0: bits32;
  862. a1: bits32;
  863. a2: bits32;
  864. count: int16;
  865. VAR z0Ptr: bits32;
  866. VAR z1Ptr: bits32;
  867. VAR z2Ptr: bits32
  868. );
  869. Var
  870. z0, z1, z2: bits32;
  871. negCount: int8;
  872. Begin
  873. z2 := a2 shl count;
  874. z1 := a1 shl count;
  875. z0 := a0 shl count;
  876. if ( 0 < count ) then
  877. Begin
  878. negCount := ( ( - count ) AND 31 );
  879. z1 := z1 or (a2 shr negCount);
  880. z0 := z0 or (a1 shr negCount);
  881. End;
  882. z2Ptr := z2;
  883. z1Ptr := z1;
  884. z0Ptr := z0;
  885. End;
  886. {*----------------------------------------------------------------------------
  887. | Shifts the 128-bit value formed by concatenating `a0' and `a1' left by the
  888. | number of bits given in `count'. Any bits shifted off are lost. The value
  889. | of `count' must be less than 64. The result is broken into two 64-bit
  890. | pieces which are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  891. *----------------------------------------------------------------------------*}
  892. procedure shortShift128Left(a0: bits64; a1: bits64; count: int16; var z0Ptr: bits64; z1Ptr : bits64);inline;
  893. begin
  894. z1Ptr := a1 shl count;
  895. if count=0 then
  896. z0Ptr:=a0
  897. else
  898. z0Ptr:=( a0 shl count ) or ( a1 shr ( ( - count ) and 63 ) );
  899. end;
  900. {*
  901. -------------------------------------------------------------------------------
  902. Adds the 64-bit value formed by concatenating `a0' and `a1' to the 64-bit
  903. value formed by concatenating `b0' and `b1'. Addition is modulo 2^64, so
  904. any carry out is lost. The result is broken into two 32-bit pieces which
  905. are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  906. -------------------------------------------------------------------------------
  907. *}
  908. Procedure
  909. add64(
  910. a0:bits32; a1:bits32; b0:bits32; b1:bits32; VAR z0Ptr:bits32; VAR z1Ptr:bits32 );
  911. Var
  912. z1: bits32;
  913. Begin
  914. z1 := a1 + b1;
  915. z1Ptr := z1;
  916. z0Ptr := a0 + b0 + bits32( z1 < a1 );
  917. End;
  918. {*
  919. -------------------------------------------------------------------------------
  920. Adds the 96-bit value formed by concatenating `a0', `a1', and `a2' to the
  921. 96-bit value formed by concatenating `b0', `b1', and `b2'. Addition is
  922. modulo 2^96, so any carry out is lost. The result is broken into three
  923. 32-bit pieces which are stored at the locations pointed to by `z0Ptr',
  924. `z1Ptr', and `z2Ptr'.
  925. -------------------------------------------------------------------------------
  926. *}
  927. Procedure
  928. add96(
  929. a0: bits32;
  930. a1: bits32;
  931. a2: bits32;
  932. b0: bits32;
  933. b1: bits32;
  934. b2: bits32;
  935. VAR z0Ptr: bits32;
  936. VAR z1Ptr: bits32;
  937. VAR z2Ptr: bits32
  938. );
  939. var
  940. z0, z1, z2: bits32;
  941. carry0, carry1: int8;
  942. Begin
  943. z2 := a2 + b2;
  944. carry1 := int8( z2 < a2 );
  945. z1 := a1 + b1;
  946. carry0 := int8( z1 < a1 );
  947. z0 := a0 + b0;
  948. z1 := z1 + carry1;
  949. z0 := z0 + bits32( z1 < carry1 );
  950. z0 := z0 + carry0;
  951. z2Ptr := z2;
  952. z1Ptr := z1;
  953. z0Ptr := z0;
  954. End;
  955. {*
  956. -------------------------------------------------------------------------------
  957. Subtracts the 64-bit value formed by concatenating `b0' and `b1' from the
  958. 64-bit value formed by concatenating `a0' and `a1'. Subtraction is modulo
  959. 2^64, so any borrow out (carry out) is lost. The result is broken into two
  960. 32-bit pieces which are stored at the locations pointed to by `z0Ptr' and
  961. `z1Ptr'.
  962. -------------------------------------------------------------------------------
  963. *}
  964. Procedure
  965. sub64(
  966. a0: bits32; a1 : bits32; b0 :bits32; b1: bits32; VAR z0Ptr:bits32; VAR z1Ptr: bits32 );
  967. Begin
  968. z1Ptr := a1 - b1;
  969. z0Ptr := a0 - b0 - bits32( a1 < b1 );
  970. End;
  971. {*
  972. -------------------------------------------------------------------------------
  973. Subtracts the 96-bit value formed by concatenating `b0', `b1', and `b2' from
  974. the 96-bit value formed by concatenating `a0', `a1', and `a2'. Subtraction
  975. is modulo 2^96, so any borrow out (carry out) is lost. The result is broken
  976. into three 32-bit pieces which are stored at the locations pointed to by
  977. `z0Ptr', `z1Ptr', and `z2Ptr'.
  978. -------------------------------------------------------------------------------
  979. *}
  980. Procedure
  981. sub96(
  982. a0:bits32;
  983. a1:bits32;
  984. a2:bits32;
  985. b0:bits32;
  986. b1:bits32;
  987. b2:bits32;
  988. VAR z0Ptr:bits32;
  989. VAR z1Ptr:bits32;
  990. VAR z2Ptr:bits32
  991. );
  992. Var
  993. z0, z1, z2: bits32;
  994. borrow0, borrow1: int8;
  995. Begin
  996. z2 := a2 - b2;
  997. borrow1 := int8( a2 < b2 );
  998. z1 := a1 - b1;
  999. borrow0 := int8( a1 < b1 );
  1000. z0 := a0 - b0;
  1001. z0 := z0 - bits32( z1 < borrow1 );
  1002. z1 := z1 - borrow1;
  1003. z0 := z0 -borrow0;
  1004. z2Ptr := z2;
  1005. z1Ptr := z1;
  1006. z0Ptr := z0;
  1007. End;
  1008. {*
  1009. -------------------------------------------------------------------------------
  1010. Multiplies `a' by `b' to obtain a 64-bit product. The product is broken
  1011. into two 32-bit pieces which are stored at the locations pointed to by
  1012. `z0Ptr' and `z1Ptr'.
  1013. -------------------------------------------------------------------------------
  1014. *}
  1015. Procedure mul32To64( a:bits32; b:bits32; VAR z0Ptr: bits32; VAR z1Ptr
  1016. :bits32 );
  1017. Var
  1018. aHigh, aLow, bHigh, bLow: bits16;
  1019. z0, zMiddleA, zMiddleB, z1: bits32;
  1020. Begin
  1021. aLow := a and $ffff;
  1022. aHigh := a shr 16;
  1023. bLow := b and $ffff;
  1024. bHigh := b shr 16;
  1025. z1 := ( bits32( aLow) ) * bLow;
  1026. zMiddleA := ( bits32 (aLow) ) * bHigh;
  1027. zMiddleB := ( bits32 (aHigh) ) * bLow;
  1028. z0 := ( bits32 (aHigh) ) * bHigh;
  1029. zMiddleA := zMiddleA + zMiddleB;
  1030. z0 := z0 + ( ( bits32 ( zMiddleA < zMiddleB ) ) shl 16 ) + ( zMiddleA shr 16 );
  1031. zMiddleA := zmiddleA shl 16;
  1032. z1 := z1 + zMiddleA;
  1033. z0 := z0 + bits32( z1 < zMiddleA );
  1034. z1Ptr := z1;
  1035. z0Ptr := z0;
  1036. End;
  1037. {*
  1038. -------------------------------------------------------------------------------
  1039. Multiplies the 64-bit value formed by concatenating `a0' and `a1' by `b'
  1040. to obtain a 96-bit product. The product is broken into three 32-bit pieces
  1041. which are stored at the locations pointed to by `z0Ptr', `z1Ptr', and
  1042. `z2Ptr'.
  1043. -------------------------------------------------------------------------------
  1044. *}
  1045. Procedure
  1046. mul64By32To96(
  1047. a0:bits32;
  1048. a1:bits32;
  1049. b:bits32;
  1050. VAR z0Ptr:bits32;
  1051. VAR z1Ptr:bits32;
  1052. VAR z2Ptr:bits32
  1053. );
  1054. Var
  1055. z0, z1, z2, more1: bits32;
  1056. Begin
  1057. mul32To64( a1, b, z1, z2 );
  1058. mul32To64( a0, b, z0, more1 );
  1059. add64( z0, more1, 0, z1, z0, z1 );
  1060. z2Ptr := z2;
  1061. z1Ptr := z1;
  1062. z0Ptr := z0;
  1063. End;
  1064. {*
  1065. -------------------------------------------------------------------------------
  1066. Multiplies the 64-bit value formed by concatenating `a0' and `a1' to the
  1067. 64-bit value formed by concatenating `b0' and `b1' to obtain a 128-bit
  1068. product. The product is broken into four 32-bit pieces which are stored at
  1069. the locations pointed to by `z0Ptr', `z1Ptr', `z2Ptr', and `z3Ptr'.
  1070. -------------------------------------------------------------------------------
  1071. *}
  1072. Procedure
  1073. mul64To128(
  1074. a0:bits32;
  1075. a1:bits32;
  1076. b0:bits32;
  1077. b1:bits32;
  1078. VAR z0Ptr:bits32;
  1079. VAR z1Ptr:bits32;
  1080. VAR z2Ptr:bits32;
  1081. VAR z3Ptr:bits32
  1082. );
  1083. Var
  1084. z0, z1, z2, z3: bits32;
  1085. more1, more2: bits32;
  1086. Begin
  1087. mul32To64( a1, b1, z2, z3 );
  1088. mul32To64( a1, b0, z1, more2 );
  1089. add64( z1, more2, 0, z2, z1, z2 );
  1090. mul32To64( a0, b0, z0, more1 );
  1091. add64( z0, more1, 0, z1, z0, z1 );
  1092. mul32To64( a0, b1, more1, more2 );
  1093. add64( more1, more2, 0, z2, more1, z2 );
  1094. add64( z0, z1, 0, more1, z0, z1 );
  1095. z3Ptr := z3;
  1096. z2Ptr := z2;
  1097. z1Ptr := z1;
  1098. z0Ptr := z0;
  1099. End;
  1100. {*
  1101. -------------------------------------------------------------------------------
  1102. Returns an approximation to the 32-bit integer quotient obtained by dividing
  1103. `b' into the 64-bit value formed by concatenating `a0' and `a1'. The
  1104. divisor `b' must be at least 2^31. If q is the exact quotient truncated
  1105. toward zero, the approximation returned lies between q and q + 2 inclusive.
  1106. If the exact quotient q is larger than 32 bits, the maximum positive 32-bit
  1107. unsigned integer is returned.
  1108. -------------------------------------------------------------------------------
  1109. *}
  1110. Function estimateDiv64To32( a0:bits32; a1: bits32; b:bits32): bits32;
  1111. Var
  1112. b0, b1: bits32;
  1113. rem0, rem1, term0, term1: bits32;
  1114. z: bits32;
  1115. Begin
  1116. if ( b <= a0 ) then
  1117. Begin
  1118. estimateDiv64To32 := $FFFFFFFF;
  1119. exit;
  1120. End;
  1121. b0 := b shr 16;
  1122. if ( b0 shl 16 <= a0 ) then
  1123. z:= $FFFF0000
  1124. else
  1125. z:= ( a0 div b0 ) shl 16;
  1126. mul32To64( b, z, term0, term1 );
  1127. sub64( a0, a1, term0, term1, rem0, rem1 );
  1128. while ( ( sbits32 (rem0) ) < 0 ) do
  1129. Begin
  1130. z := z - $10000;
  1131. b1 := b shl 16;
  1132. add64( rem0, rem1, b0, b1, rem0, rem1 );
  1133. End;
  1134. rem0 := ( rem0 shl 16 ) OR ( rem1 shr 16 );
  1135. if ( b0 shl 16 <= rem0 ) then
  1136. z := z or $FFFF
  1137. else
  1138. z := z or (rem0 div b0);
  1139. estimateDiv64To32 := z;
  1140. End;
  1141. {*
  1142. -------------------------------------------------------------------------------
  1143. Returns an approximation to the square root of the 32-bit significand given
  1144. by `a'. Considered as an integer, `a' must be at least 2^31. If bit 0 of
  1145. `aExp' (the least significant bit) is 1, the integer returned approximates
  1146. 2^31*sqrt(`a'/2^31), where `a' is considered an integer. If bit 0 of `aExp'
  1147. is 0, the integer returned approximates 2^31*sqrt(`a'/2^30). In either
  1148. case, the approximation returned lies strictly within +/-2 of the exact
  1149. value.
  1150. -------------------------------------------------------------------------------
  1151. *}
  1152. Function estimateSqrt32( aExp: int16; a: bits32 ): bits32;
  1153. const sqrtOddAdjustments: array[0..15] of bits16 = (
  1154. $0004, $0022, $005D, $00B1, $011D, $019F, $0236, $02E0,
  1155. $039C, $0468, $0545, $0631, $072B, $0832, $0946, $0A67
  1156. );
  1157. const sqrtEvenAdjustments: array[0..15] of bits16 = (
  1158. $0A2D, $08AF, $075A, $0629, $051A, $0429, $0356, $029E,
  1159. $0200, $0179, $0109, $00AF, $0068, $0034, $0012, $0002
  1160. );
  1161. Var
  1162. index: int8;
  1163. z: bits32;
  1164. Begin
  1165. index := ( a shr 27 ) AND 15;
  1166. if ( aExp AND 1 ) <> 0 then
  1167. Begin
  1168. z := $4000 + ( a shr 17 ) - sqrtOddAdjustments[ index ];
  1169. z := ( ( a div z ) shl 14 ) + ( z shl 15 );
  1170. a := a shr 1;
  1171. End
  1172. else
  1173. Begin
  1174. z := $8000 + ( a shr 17 ) - sqrtEvenAdjustments[ index ];
  1175. z := a div z + z;
  1176. if ( $20000 <= z ) then
  1177. z := $FFFF8000
  1178. else
  1179. z := ( z shl 15 );
  1180. if ( z <= a ) then
  1181. Begin
  1182. estimateSqrt32 := bits32 ( ( sbits32 (a )) shr 1 );
  1183. exit;
  1184. End;
  1185. End;
  1186. estimateSqrt32 := ( ( estimateDiv64To32( a, 0, z ) ) shr 1 ) + ( z shr 1 );
  1187. End;
  1188. {*
  1189. -------------------------------------------------------------------------------
  1190. Returns the number of leading 0 bits before the most-significant 1 bit of
  1191. `a'. If `a' is zero, 32 is returned.
  1192. -------------------------------------------------------------------------------
  1193. *}
  1194. Function countLeadingZeros32( a:bits32 ): int8;
  1195. const countLeadingZerosHigh:array[0..255] of int8 = (
  1196. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  1197. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  1198. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1199. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1200. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1201. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1202. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1203. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1204. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1205. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1206. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1207. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1208. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1209. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1210. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1211. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  1212. );
  1213. Var
  1214. shiftCount: int8;
  1215. Begin
  1216. shiftCount := 0;
  1217. if ( a < $10000 ) then
  1218. Begin
  1219. shiftCount := shiftcount + 16;
  1220. a := a shl 16;
  1221. End;
  1222. if ( a < $1000000 ) then
  1223. Begin
  1224. shiftCount := shiftcount + 8;
  1225. a := a shl 8;
  1226. end;
  1227. shiftCount := shiftcount + countLeadingZerosHigh[ a shr 24 ];
  1228. countLeadingZeros32:= shiftCount;
  1229. End;
  1230. {*----------------------------------------------------------------------------
  1231. | Returns the number of leading 0 bits before the most-significant 1 bit of
  1232. | `a'. If `a' is zero, 64 is returned.
  1233. *----------------------------------------------------------------------------*}
  1234. function countLeadingZeros64( a : bits64): int8;
  1235. var
  1236. shiftcount : int8;
  1237. Begin
  1238. shiftCount := 0;
  1239. if ( a < (bits64(1) shl 32 )) then
  1240. shiftCount := shiftcount + 32
  1241. else
  1242. a := a shr 32;
  1243. shiftCount := shiftCount + countLeadingZeros32( a );
  1244. countLeadingZeros64:= shiftCount;
  1245. End;
  1246. {*
  1247. -------------------------------------------------------------------------------
  1248. Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is
  1249. equal to the 64-bit value formed by concatenating `b0' and `b1'. Otherwise,
  1250. returns 0.
  1251. -------------------------------------------------------------------------------
  1252. *}
  1253. Function eq64( a0: bits32; a1:bits32 ;b0:bits32; b1:bits32 ): flag;
  1254. Begin
  1255. eq64 := flag( a0 = b0 ) and flag( a1 = b1 );
  1256. End;
  1257. {*
  1258. -------------------------------------------------------------------------------
  1259. Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is less
  1260. than or equal to the 64-bit value formed by concatenating `b0' and `b1'.
  1261. Otherwise, returns 0.
  1262. -------------------------------------------------------------------------------
  1263. *}
  1264. Function le64( a0: bits32; a1:bits32 ;b0:bits32; b1:bits32 ): flag;
  1265. Begin
  1266. le64:= flag( a0 < b0 ) or flag( ( a0 = b0 ) and ( a1 <= b1 ) );
  1267. End;
  1268. {*
  1269. -------------------------------------------------------------------------------
  1270. Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is less
  1271. than the 64-bit value formed by concatenating `b0' and `b1'. Otherwise,
  1272. returns 0.
  1273. -------------------------------------------------------------------------------
  1274. *}
  1275. Function lt64( a0: bits32; a1:bits32 ;b0:bits32; b1:bits32 ): flag;
  1276. Begin
  1277. lt64 := flag( a0 < b0 ) or flag( ( a0 = b0 ) and ( a1 < b1 ) );
  1278. End;
  1279. {*
  1280. -------------------------------------------------------------------------------
  1281. Returns 1 if the 64-bit value formed by concatenating `a0' and `a1' is not
  1282. equal to the 64-bit value formed by concatenating `b0' and `b1'. Otherwise,
  1283. returns 0.
  1284. -------------------------------------------------------------------------------
  1285. *}
  1286. Function ne64( a0: bits32; a1:bits32 ;b0:bits32; b1:bits32 ): flag;
  1287. Begin
  1288. ne64:= flag( a0 <> b0 ) or flag( a1 <> b1 );
  1289. End;
  1290. (*****************************************************************************)
  1291. (* End Low-Level arithmetic *)
  1292. (*****************************************************************************)
  1293. {*
  1294. -------------------------------------------------------------------------------
  1295. Functions and definitions to determine: (1) whether tininess for underflow
  1296. is detected before or after rounding by default, (2) what (if anything)
  1297. happens when exceptions are raised, (3) how signaling NaNs are distinguished
  1298. from quiet NaNs, (4) the default generated quiet NaNs, and (4) how NaNs
  1299. are propagated from function inputs to output. These details are ENDIAN
  1300. specific
  1301. -------------------------------------------------------------------------------
  1302. *}
  1303. {$IFDEF ENDIAN_LITTLE}
  1304. {*
  1305. -------------------------------------------------------------------------------
  1306. Internal canonical NaN format.
  1307. -------------------------------------------------------------------------------
  1308. *}
  1309. TYPE
  1310. commonNaNT = packed record
  1311. sign: flag;
  1312. high, low : bits32;
  1313. end;
  1314. {*
  1315. -------------------------------------------------------------------------------
  1316. The pattern for a default generated single-precision NaN.
  1317. -------------------------------------------------------------------------------
  1318. *}
  1319. const float32_default_nan = $FFC00000;
  1320. {*
  1321. -------------------------------------------------------------------------------
  1322. Returns 1 if the single-precision floating-point value `a' is a NaN;
  1323. otherwise returns 0.
  1324. -------------------------------------------------------------------------------
  1325. *}
  1326. Function float32_is_nan( a : float32 ): flag;
  1327. Begin
  1328. float32_is_nan:= flag( $FF000000 < bits32 ( a shl 1 ) );
  1329. End;
  1330. {*
  1331. -------------------------------------------------------------------------------
  1332. Returns 1 if the single-precision floating-point value `a' is a signaling
  1333. NaN; otherwise returns 0.
  1334. -------------------------------------------------------------------------------
  1335. *}
  1336. Function float32_is_signaling_nan( a : float32 ): flag;
  1337. Begin
  1338. float32_is_signaling_nan := flag
  1339. ( ( ( a shr 22 ) and $1FF ) = $1FE ) and( a and $003FFFFF );
  1340. End;
  1341. {*
  1342. -------------------------------------------------------------------------------
  1343. Returns the result of converting the single-precision floating-point NaN
  1344. `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  1345. exception is raised.
  1346. -------------------------------------------------------------------------------
  1347. *}
  1348. Procedure float32ToCommonNaN( a: float32; VAR c:commonNaNT );
  1349. var
  1350. z : commonNaNT ;
  1351. Begin
  1352. if ( float32_is_signaling_nan( a ) <> 0) then
  1353. float_raise( float_flag_invalid );
  1354. z.sign := a shr 31;
  1355. z.low := 0;
  1356. z.high := a shl 9;
  1357. c := z;
  1358. End;
  1359. {*
  1360. -------------------------------------------------------------------------------
  1361. Returns the result of converting the canonical NaN `a' to the single-
  1362. precision floating-point format.
  1363. -------------------------------------------------------------------------------
  1364. *}
  1365. Function commonNaNToFloat32( a : commonNaNT ): float32;
  1366. Begin
  1367. commonNaNToFloat32 := ( ( bits32 (a.sign) ) shl 31 ) or $7FC00000 or ( a.high shr 9 );
  1368. End;
  1369. {*
  1370. -------------------------------------------------------------------------------
  1371. Takes two single-precision floating-point values `a' and `b', one of which
  1372. is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
  1373. signaling NaN, the invalid exception is raised.
  1374. -------------------------------------------------------------------------------
  1375. *}
  1376. Function propagateFloat32NaN( a : float32 ; b: float32 ): float32;
  1377. Var
  1378. aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN: flag;
  1379. label returnLargerSignificand;
  1380. Begin
  1381. aIsNaN := float32_is_nan( a );
  1382. aIsSignalingNaN := float32_is_signaling_nan( a );
  1383. bIsNaN := float32_is_nan( b );
  1384. bIsSignalingNaN := float32_is_signaling_nan( b );
  1385. a := a or $00400000;
  1386. b := b or $00400000;
  1387. if ( aIsSignalingNaN or bIsSignalingNaN ) <> 0 then
  1388. float_raise( float_flag_invalid );
  1389. if ( aIsSignalingNaN )<> 0 then
  1390. Begin
  1391. if ( bIsSignalingNaN ) <> 0 then
  1392. goto returnLargerSignificand;
  1393. if bIsNan <> 0 then
  1394. propagateFloat32NaN := b
  1395. else
  1396. propagateFloat32NaN := a;
  1397. exit;
  1398. End
  1399. else if ( aIsNaN <> 0) then
  1400. Begin
  1401. if ( bIsSignalingNaN or not bIsNaN )<> 0 then
  1402. Begin
  1403. propagateFloat32NaN := a;
  1404. exit;
  1405. End;
  1406. returnLargerSignificand:
  1407. if ( bits32 ( a shl 1 ) < bits32 ( b shl 1 ) ) then
  1408. Begin
  1409. propagateFloat32NaN := b;
  1410. exit;
  1411. End;
  1412. if ( bits32 ( b shl 1 ) < bits32 ( a shl 1 ) ) then
  1413. Begin
  1414. propagateFloat32NaN := a;
  1415. End;
  1416. if a < b then
  1417. propagateFloat32NaN := a
  1418. else
  1419. propagateFloat32NaN := b;
  1420. exit;
  1421. End
  1422. else
  1423. Begin
  1424. propagateFloat32NaN := b;
  1425. exit;
  1426. End;
  1427. End;
  1428. {*
  1429. -------------------------------------------------------------------------------
  1430. The pattern for a default generated double-precision NaN. The `high' and
  1431. `low' values hold the most- and least-significant bits, respectively.
  1432. -------------------------------------------------------------------------------
  1433. *}
  1434. const
  1435. float64_default_nan_high = $FFF80000;
  1436. float64_default_nan_low = $00000000;
  1437. {*
  1438. -------------------------------------------------------------------------------
  1439. Returns 1 if the double-precision floating-point value `a' is a NaN;
  1440. otherwise returns 0.
  1441. -------------------------------------------------------------------------------
  1442. *}
  1443. Function float64_is_nan( a : float64 ) : flag;
  1444. Begin
  1445. float64_is_nan :=
  1446. flag( $FFE00000 <= bits32 ( a.high shl 1 ) )
  1447. and ( a.low or ( a.high and $000FFFFF ) );
  1448. End;
  1449. {*
  1450. -------------------------------------------------------------------------------
  1451. Returns 1 if the double-precision floating-point value `a' is a signaling
  1452. NaN; otherwise returns 0.
  1453. -------------------------------------------------------------------------------
  1454. *}
  1455. Function float64_is_signaling_nan( a : float64 ): flag;
  1456. Begin
  1457. float64_is_signaling_nan :=
  1458. flag( ( ( a.high shr 19 ) and $FFF ) = $FFE )
  1459. and ( a.low or ( a.high and $0007FFFF ) );
  1460. End;
  1461. {*
  1462. -------------------------------------------------------------------------------
  1463. Returns the result of converting the double-precision floating-point NaN
  1464. `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  1465. exception is raised.
  1466. -------------------------------------------------------------------------------
  1467. *}
  1468. Procedure float64ToCommonNaN( a : float64; VAR c:commonNaNT );
  1469. Var
  1470. z : commonNaNT;
  1471. Begin
  1472. if ( float64_is_signaling_nan( a )<>0 ) then
  1473. float_raise( float_flag_invalid );
  1474. z.sign := a.high shr 31;
  1475. shortShift64Left( a.high, a.low, 12, z.high, z.low );
  1476. c := z;
  1477. End;
  1478. {*
  1479. -------------------------------------------------------------------------------
  1480. Returns the result of converting the canonical NaN `a' to the double-
  1481. precision floating-point format.
  1482. -------------------------------------------------------------------------------
  1483. *}
  1484. Procedure commonNaNToFloat64( a : commonNaNT; VAR c: float64 );
  1485. Var
  1486. z: float64;
  1487. Begin
  1488. shift64Right( a.high, a.low, 12, z.high, z.low );
  1489. z.high := z.high or ( ( bits32 (a.sign) ) shl 31 ) or $7FF80000;
  1490. c := z;
  1491. End;
  1492. {*
  1493. -------------------------------------------------------------------------------
  1494. Takes two double-precision floating-point values `a' and `b', one of which
  1495. is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
  1496. signaling NaN, the invalid exception is raised.
  1497. -------------------------------------------------------------------------------
  1498. *}
  1499. Procedure propagateFloat64NaN( a: float64; b: float64 ; VAR c: float64 );
  1500. Var
  1501. aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN: flag;
  1502. label returnLargerSignificand;
  1503. Begin
  1504. aIsNaN := float64_is_nan( a );
  1505. aIsSignalingNaN := float64_is_signaling_nan( a );
  1506. bIsNaN := float64_is_nan( b );
  1507. bIsSignalingNaN := float64_is_signaling_nan( b );
  1508. a.high := a.high or $00080000;
  1509. b.high := b.high or $00080000;
  1510. if ( aIsSignalingNaN or bIsSignalingNaN )<> 0 then
  1511. float_raise( float_flag_invalid );
  1512. if ( aIsSignalingNaN )<>0 then
  1513. Begin
  1514. if ( bIsSignalingNaN )<>0 then
  1515. goto returnLargerSignificand;
  1516. if bIsNan <> 0 then
  1517. c := b
  1518. else
  1519. c := a;
  1520. exit;
  1521. End
  1522. else if ( aIsNaN )<> 0 then
  1523. Begin
  1524. if ( bIsSignalingNaN or not bIsNaN ) <> 0 then
  1525. Begin
  1526. c := a;
  1527. exit;
  1528. End;
  1529. returnLargerSignificand:
  1530. if ( lt64( a.high shl 1, a.low, b.high shl 1, b.low ) ) <> 0 then
  1531. Begin
  1532. c := b;
  1533. exit;
  1534. End;
  1535. if ( lt64( b.high shl 1, b.low, a.high shl 1, a.low ) ) <> 0 then
  1536. Begin
  1537. c := a;
  1538. exit;
  1539. End;
  1540. if a.high < b.high then
  1541. c := a
  1542. else
  1543. c := b;
  1544. exit;
  1545. End
  1546. else
  1547. Begin
  1548. c := b;
  1549. exit;
  1550. End;
  1551. End;
  1552. {*----------------------------------------------------------------------------
  1553. | Returns 1 if the 128-bit value formed by concatenating `a0' and `a1' is less
  1554. | than the 128-bit value formed by concatenating `b0' and `b1'. Otherwise,
  1555. | returns 0.
  1556. *----------------------------------------------------------------------------*}
  1557. function lt128(a0: bits64; a1: bits64; b0: bits64; b1 : bits64): flag;inline;
  1558. begin
  1559. result := ord(( a0 < b0 ) or ( ( a0 = b0 ) and ( a1 < b1 ) ));
  1560. end;
  1561. {*----------------------------------------------------------------------------
  1562. | Returns 1 if the quadruple-precision floating-point value `a' is a NaN;
  1563. | otherwise returns 0.
  1564. *----------------------------------------------------------------------------*}
  1565. function float128_is_nan( a : float128): flag;
  1566. begin
  1567. result:= ord(( int64( $FFFE000000000000 ) <= bits64( a.high shl 1 ) )
  1568. and ( (a.low<>0) or (( a.high and int64( $0000FFFFFFFFFFFF ) )<>0 ) ));
  1569. end;
  1570. {*----------------------------------------------------------------------------
  1571. | Returns 1 if the quadruple-precision floating-point value `a' is a
  1572. | signaling NaN; otherwise returns 0.
  1573. *----------------------------------------------------------------------------*}
  1574. function float128_is_signaling_nan( a : float128): flag;
  1575. begin
  1576. result:=ord(( ( ( a.high shr 47 ) and $FFFF ) = $FFFE ) and
  1577. ( (a.low<>0) or (( a.high and int64( $00007FFFFFFFFFFF ) )<>0) ));
  1578. end;
  1579. {*----------------------------------------------------------------------------
  1580. | Returns the result of converting the quadruple-precision floating-point NaN
  1581. | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  1582. | exception is raised.
  1583. *----------------------------------------------------------------------------*}
  1584. function float128ToCommonNaN( a : float128): commonNaNT;
  1585. var
  1586. z: commonNaNT;
  1587. qhigh,qlow : qword;
  1588. begin
  1589. if ( float128_is_signaling_nan( a )<>0) then
  1590. float_raise( float_flag_invalid );
  1591. z.sign := a.high shr 63;
  1592. shortShift128Left( a.high, a.low, 16, qhigh, qlow );
  1593. z.high:=qhigh shr 32;
  1594. z.low:=qhigh and $ffffffff;
  1595. result:=z;
  1596. end;
  1597. {*----------------------------------------------------------------------------
  1598. | Returns the result of converting the canonical NaN `a' to the quadruple-
  1599. | precision floating-point format.
  1600. *----------------------------------------------------------------------------*}
  1601. function commonNaNToFloat128( a : commonNaNT): float128;
  1602. var
  1603. z: float128;
  1604. begin
  1605. shift128Right( a.high, a.low, 16, z.high, z.low );
  1606. z.high := z.high or ( ( bits64(a.sign) ) shl 63 ) or int64( $7FFF800000000000 );
  1607. result:=z;
  1608. end;
  1609. {*----------------------------------------------------------------------------
  1610. | Takes two quadruple-precision floating-point values `a' and `b', one of
  1611. | which is a NaN, and returns the appropriate NaN result. If either `a' or
  1612. | `b' is a signaling NaN, the invalid exception is raised.
  1613. *----------------------------------------------------------------------------*}
  1614. function propagateFloat128NaN( a: float128; b : float128): float128;
  1615. var
  1616. aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN: flag;
  1617. label
  1618. returnLargerSignificand;
  1619. begin
  1620. aIsNaN := float128_is_nan( a );
  1621. aIsSignalingNaN := float128_is_signaling_nan( a );
  1622. bIsNaN := float128_is_nan( b );
  1623. bIsSignalingNaN := float128_is_signaling_nan( b );
  1624. a.high := a.high or int64( $0000800000000000 );
  1625. b.high := b.high or int64( $0000800000000000 );
  1626. if ( aIsSignalingNaN or bIsSignalingNaN )<>0 then
  1627. float_raise( float_flag_invalid );
  1628. if ( aIsSignalingNaN )<>0 then
  1629. begin
  1630. if ( bIsSignalingNaN )<>0 then
  1631. goto returnLargerSignificand;
  1632. if bIsNaN<>0 then
  1633. result := b
  1634. else
  1635. result := a;
  1636. exit;
  1637. end
  1638. else if ( aIsNaN )<>0 then
  1639. begin
  1640. if ( bIsSignalingNaN or not( bIsNaN) )<>0 then
  1641. begin
  1642. result := a;
  1643. exit;
  1644. end;
  1645. returnLargerSignificand:
  1646. if ( lt128( a.high shl 1, a.low, b.high shl 1, b.low ) )<>0 then
  1647. begin
  1648. result := b;
  1649. exit;
  1650. end;
  1651. if ( lt128( b.high shl 1, b.low, a.high shl 1, a.low ) )<>0 then
  1652. begin
  1653. result := a;
  1654. exit
  1655. end;
  1656. if ( a.high < b.high ) then
  1657. result := a
  1658. else
  1659. result := b;
  1660. exit;
  1661. end
  1662. else
  1663. result:=b;
  1664. end;
  1665. {$ELSE}
  1666. { Big endian code }
  1667. (*----------------------------------------------------------------------------
  1668. | Internal canonical NaN format.
  1669. *----------------------------------------------------------------------------*)
  1670. type
  1671. commonNANT = packed record
  1672. sign : flag;
  1673. high, low : bits32;
  1674. end;
  1675. (*----------------------------------------------------------------------------
  1676. | The pattern for a default generated single-precision NaN.
  1677. *----------------------------------------------------------------------------*)
  1678. const float32_default_nan = $7FFFFFFF;
  1679. (*----------------------------------------------------------------------------
  1680. | Returns 1 if the single-precision floating-point value `a' is a NaN;
  1681. | otherwise returns 0.
  1682. *----------------------------------------------------------------------------*)
  1683. function float32_is_nan(a: float32): flag;
  1684. begin
  1685. float32_is_nan := flag( $FF000000 < bits32( a shl 1 ) );
  1686. end;
  1687. (*----------------------------------------------------------------------------
  1688. | Returns 1 if the single-precision floating-point value `a' is a signaling
  1689. | NaN; otherwise returns 0.
  1690. *----------------------------------------------------------------------------*)
  1691. function float32_is_signaling_nan(a: float32):flag;
  1692. begin
  1693. float32_is_signaling_nan := flag( ( ( a shr 22 ) and $1FF ) = $1FE ) and flag( boolean((a and $003FFFFF)<>0) );
  1694. end;
  1695. (*----------------------------------------------------------------------------
  1696. | Returns the result of converting the single-precision floating-point NaN
  1697. | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  1698. | exception is raised.
  1699. *----------------------------------------------------------------------------*)
  1700. Procedure float32ToCommonNaN( a: float32; VAR c:commonNaNT );
  1701. var
  1702. z: commonNANT;
  1703. begin
  1704. if float32_is_signaling_nan(a)<>0 then
  1705. float_raise(float_flag_invalid);
  1706. z.sign := a shr 31;
  1707. z.low := 0;
  1708. z.high := a shl 9;
  1709. c:=z;
  1710. end;
  1711. (*----------------------------------------------------------------------------
  1712. | Returns the result of converting the canonical NaN `a' to the single-
  1713. | precision floating-point format.
  1714. *----------------------------------------------------------------------------*)
  1715. function CommonNanToFloat32(a : CommonNaNT): float32;
  1716. begin
  1717. CommonNanToFloat32:= ( ( bits32( a.sign )) shl 31 ) OR $7FC00000 OR ( a.high shr 9 );
  1718. end;
  1719. (*----------------------------------------------------------------------------
  1720. | Takes two single-precision floating-point values `a' and `b', one of which
  1721. | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
  1722. | signaling NaN, the invalid exception is raised.
  1723. *----------------------------------------------------------------------------*)
  1724. function propagateFloat32NaN( a: float32 ; b: float32): float32;
  1725. var
  1726. aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN: flag;
  1727. begin
  1728. aIsNaN := float32_is_nan( a );
  1729. aIsSignalingNaN := float32_is_signaling_nan( a );
  1730. bIsNaN := float32_is_nan( b );
  1731. bIsSignalingNaN := float32_is_signaling_nan( b );
  1732. a := a or $00400000;
  1733. b := b or $00400000;
  1734. if ( aIsSignalingNaN or bIsSignalingNaN )<>0 then
  1735. float_raise( float_flag_invalid );
  1736. if bIsSignalingNaN<>0 then
  1737. propagateFloat32Nan := b
  1738. else if aIsSignalingNan<>0 then
  1739. propagateFloat32Nan := a
  1740. else if bIsNan<>0 then
  1741. propagateFloat32Nan := b
  1742. else
  1743. propagateFloat32Nan := a;
  1744. end;
  1745. (*----------------------------------------------------------------------------
  1746. | The pattern for a default generated double-precision NaN. The `high' and
  1747. | `low' values hold the most- and least-significant bits, respectively.
  1748. *----------------------------------------------------------------------------*)
  1749. const
  1750. float64_default_nan_high = $7FFFFFFF;
  1751. float64_default_nan_low = $FFFFFFFF;
  1752. (*----------------------------------------------------------------------------
  1753. | Returns 1 if the double-precision floating-point value `a' is a NaN;
  1754. | otherwise returns 0.
  1755. *----------------------------------------------------------------------------*)
  1756. function float64_is_nan(a: float64): flag;
  1757. begin
  1758. float64_is_nan := flag (
  1759. ( $FFE00000 <= bits32 ( a.high shl 1 ) )
  1760. and ( (a.low<>0) or (( a.high and $000FFFFF )<>0) ));
  1761. end;
  1762. (*----------------------------------------------------------------------------
  1763. | Returns 1 if the double-precision floating-point value `a' is a signaling
  1764. | NaN; otherwise returns 0.
  1765. *----------------------------------------------------------------------------*)
  1766. function float64_is_signaling_nan( a:float64): flag;
  1767. begin
  1768. float64_is_signaling_nan := flag(
  1769. ( ( ( a.high shr 19 ) and $FFF ) = $FFE )
  1770. and ( (a.low<>0) or ( ( a.high and $0007FFFF )<>0) ));
  1771. end;
  1772. (*----------------------------------------------------------------------------
  1773. | Returns the result of converting the double-precision floating-point NaN
  1774. | `a' to the canonical NaN format. If `a' is a signaling NaN, the invalid
  1775. | exception is raised.
  1776. *----------------------------------------------------------------------------*)
  1777. Procedure float64ToCommonNaN( a : float64; VAR c:commonNaNT );
  1778. var
  1779. z : commonNaNT;
  1780. begin
  1781. if ( float64_is_signaling_nan( a )<>0 ) then
  1782. float_raise( float_flag_invalid );
  1783. z.sign := a.high shr 31;
  1784. shortShift64Left( a.high, a.low, 12, z.high, z.low );
  1785. c:=z;
  1786. end;
  1787. (*----------------------------------------------------------------------------
  1788. | Returns the result of converting the canonical NaN `a' to the double-
  1789. | precision floating-point format.
  1790. *----------------------------------------------------------------------------*)
  1791. Procedure commonNaNToFloat64( a : commonNaNT; VAR c: float64 );
  1792. var
  1793. z: float64;
  1794. begin
  1795. shift64Right( a.high, a.low, 12, z.high, z.low );
  1796. z.high := z.high or ( ( bits32 (a.sign) ) shl 31 ) or $7FF80000;
  1797. c:=z;
  1798. end;
  1799. (*----------------------------------------------------------------------------
  1800. | Takes two double-precision floating-point values `a' and `b', one of which
  1801. | is a NaN, and returns the appropriate NaN result. If either `a' or `b' is a
  1802. | signaling NaN, the invalid exception is raised.
  1803. *----------------------------------------------------------------------------*)
  1804. Procedure propagateFloat64NaN( a: float64; b: float64 ; VAR c: float64 );
  1805. var
  1806. aIsNaN, aIsSignalingNaN, bIsNaN, bIsSignalingNaN : flag;
  1807. begin
  1808. aIsNaN := float64_is_nan( a );
  1809. aIsSignalingNaN := float64_is_signaling_nan( a );
  1810. bIsNaN := float64_is_nan( b );
  1811. bIsSignalingNaN := float64_is_signaling_nan( b );
  1812. a.high := a.high or $00080000;
  1813. b.high := b.high or $00080000;
  1814. if ( (aIsSignalingNaN<>0) or (bIsSignalingNaN<>0) ) then
  1815. float_raise( float_flag_invalid );
  1816. if bIsSignalingNaN<>0 then
  1817. c := b
  1818. else if aIsSignalingNan<>0 then
  1819. c := a
  1820. else if bIsNan<>0 then
  1821. c := b
  1822. else
  1823. c := a;
  1824. end;
  1825. {$ENDIF}
  1826. (****************************************************************************)
  1827. (* END ENDIAN SPECIFIC CODE *)
  1828. (****************************************************************************)
  1829. {*
  1830. -------------------------------------------------------------------------------
  1831. Returns the fraction bits of the single-precision floating-point value `a'.
  1832. -------------------------------------------------------------------------------
  1833. *}
  1834. Function ExtractFloat32Frac(a : Float32) : Bits32;
  1835. Begin
  1836. ExtractFloat32Frac := A AND $007FFFFF;
  1837. End;
  1838. {*
  1839. -------------------------------------------------------------------------------
  1840. Returns the exponent bits of the single-precision floating-point value `a'.
  1841. -------------------------------------------------------------------------------
  1842. *}
  1843. Function extractFloat32Exp( a: float32 ): Int16;
  1844. Begin
  1845. extractFloat32Exp := (a shr 23) AND $FF;
  1846. End;
  1847. {*
  1848. -------------------------------------------------------------------------------
  1849. Returns the sign bit of the single-precision floating-point value `a'.
  1850. -------------------------------------------------------------------------------
  1851. *}
  1852. Function extractFloat32Sign( a: float32 ): Flag;
  1853. Begin
  1854. extractFloat32Sign := a shr 31;
  1855. End;
  1856. {*
  1857. -------------------------------------------------------------------------------
  1858. Normalizes the subnormal single-precision floating-point value represented
  1859. by the denormalized significand `aSig'. The normalized exponent and
  1860. significand are stored at the locations pointed to by `zExpPtr' and
  1861. `zSigPtr', respectively.
  1862. -------------------------------------------------------------------------------
  1863. *}
  1864. Procedure normalizeFloat32Subnormal( aSig : bits32; VAR zExpPtr: Int16; VAR zSigPtr :bits32);
  1865. Var
  1866. ShiftCount : BYTE;
  1867. Begin
  1868. shiftCount := countLeadingZeros32( aSig ) - 8;
  1869. zSigPtr := aSig shl shiftCount;
  1870. zExpPtr := 1 - shiftCount;
  1871. End;
  1872. {*
  1873. -------------------------------------------------------------------------------
  1874. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  1875. single-precision floating-point value, returning the result. After being
  1876. shifted into the proper positions, the three fields are simply added
  1877. together to form the result. This means that any integer portion of `zSig'
  1878. will be added into the exponent. Since a properly normalized significand
  1879. will have an integer portion equal to 1, the `zExp' input should be 1 less
  1880. than the desired result exponent whenever `zSig' is a complete, normalized
  1881. significand.
  1882. -------------------------------------------------------------------------------
  1883. *}
  1884. Function packFloat32( zSign: Flag; zExp : Int16; zSig: Bits32 ): Float32;
  1885. Begin
  1886. packFloat32 := ( ( bits32( zSign) ) shl 31 ) + ( ( bits32 (zExp) ) shl 23 )
  1887. + zSig;
  1888. End;
  1889. {*
  1890. -------------------------------------------------------------------------------
  1891. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  1892. and significand `zSig', and returns the proper single-precision floating-
  1893. point value corresponding to the abstract input. Ordinarily, the abstract
  1894. value is simply rounded and packed into the single-precision format, with
  1895. the inexact exception raised if the abstract input cannot be represented
  1896. exactly. However, if the abstract value is too large, the overflow and
  1897. inexact exceptions are raised and an infinity or maximal finite value is
  1898. returned. If the abstract value is too small, the input value is rounded to
  1899. a subnormal number, and the underflow and inexact exceptions are raised if
  1900. the abstract input cannot be represented exactly as a subnormal single-
  1901. precision floating-point number.
  1902. The input significand `zSig' has its binary point between bits 30
  1903. and 29, which is 7 bits to the left of the usual location. This shifted
  1904. significand must be normalized or smaller. If `zSig' is not normalized,
  1905. `zExp' must be 0; in that case, the result returned is a subnormal number,
  1906. and it must not require rounding. In the usual case that `zSig' is
  1907. normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  1908. The handling of underflow and overflow follows the IEC/IEEE Standard for
  1909. Binary Floating-Point Arithmetic.
  1910. -------------------------------------------------------------------------------
  1911. *}
  1912. Function roundAndPackFloat32( zSign : Flag; zExp : Int16; zSig : Bits32 ) : float32;
  1913. Var
  1914. roundingMode : BYTE;
  1915. roundNearestEven : Flag;
  1916. roundIncrement, roundBits : BYTE;
  1917. IsTiny : Flag;
  1918. Begin
  1919. roundingMode := float_rounding_mode;
  1920. if (roundingMode = float_round_nearest_even) then
  1921. Begin
  1922. roundNearestEven := Flag(TRUE);
  1923. end
  1924. else
  1925. roundNearestEven := Flag(FALSE);
  1926. roundIncrement := $40;
  1927. if ( Boolean(roundNearestEven) = FALSE) then
  1928. Begin
  1929. if ( roundingMode = float_round_to_zero ) Then
  1930. Begin
  1931. roundIncrement := 0;
  1932. End
  1933. else
  1934. Begin
  1935. roundIncrement := $7F;
  1936. if ( zSign <> 0 ) then
  1937. Begin
  1938. if roundingMode = float_round_up then roundIncrement := 0;
  1939. End
  1940. else
  1941. Begin
  1942. if roundingMode = float_round_down then roundIncrement := 0;
  1943. End;
  1944. End
  1945. End;
  1946. roundBits := zSig AND $7F;
  1947. if ($FD <= bits16 (zExp) ) then
  1948. Begin
  1949. if (( $FD < zExp ) OR ( zExp = $FD ) AND ( sbits32 ( zSig + roundIncrement ) < 0 ) ) then
  1950. Begin
  1951. float_raise( float_flag_overflow OR float_flag_inexact );
  1952. roundAndPackFloat32:=packFloat32( zSign, $FF, 0 ) - Flag( roundIncrement = 0 );
  1953. exit;
  1954. End;
  1955. if ( zExp < 0 ) then
  1956. Begin
  1957. isTiny :=
  1958. flag(( float_detect_tininess = float_tininess_before_rounding )
  1959. OR ( zExp < -1 )
  1960. OR ( (zSig + roundIncrement) < $80000000 ));
  1961. shift32RightJamming( zSig, - zExp, zSig );
  1962. zExp := 0;
  1963. roundBits := zSig AND $7F;
  1964. if ( (isTiny = flag(TRUE)) and (roundBits<>0) ) then
  1965. float_raise( float_flag_underflow );
  1966. End;
  1967. End;
  1968. if ( roundBits )<> 0 then
  1969. softfloat_exception_flags := float_flag_inexact OR softfloat_exception_flags;
  1970. zSig := ( zSig + roundIncrement ) shr 7;
  1971. zSig := zSig AND not bits32( bits32( ( roundBits XOR $40 ) = 0 ) and roundNearestEven );
  1972. if ( zSig = 0 ) then zExp := 0;
  1973. roundAndPackFloat32 := packFloat32( zSign, zExp, zSig );
  1974. exit;
  1975. End;
  1976. {*
  1977. -------------------------------------------------------------------------------
  1978. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  1979. and significand `zSig', and returns the proper single-precision floating-
  1980. point value corresponding to the abstract input. This routine is just like
  1981. `roundAndPackFloat32' except that `zSig' does not have to be normalized.
  1982. Bit 31 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
  1983. floating-point exponent.
  1984. -------------------------------------------------------------------------------
  1985. *}
  1986. Function normalizeRoundAndPackFloat32( zSign: flag; zExp: int16; zSig:bits32 ): float32;
  1987. Var
  1988. ShiftCount : int8;
  1989. Begin
  1990. shiftCount := countLeadingZeros32( zSig ) - 1;
  1991. normalizeRoundAndPackFloat32 := roundAndPackFloat32( zSign, zExp - shiftCount, zSig shl shiftCount );
  1992. End;
  1993. {*
  1994. -------------------------------------------------------------------------------
  1995. Returns the most-significant 20 fraction bits of the double-precision
  1996. floating-point value `a'.
  1997. -------------------------------------------------------------------------------
  1998. *}
  1999. Function extractFloat64Frac0(a: float64): bits32;
  2000. Begin
  2001. extractFloat64Frac0 := a.high and $000FFFFF;
  2002. End;
  2003. {*
  2004. -------------------------------------------------------------------------------
  2005. Returns the least-significant 32 fraction bits of the double-precision
  2006. floating-point value `a'.
  2007. -------------------------------------------------------------------------------
  2008. *}
  2009. Function extractFloat64Frac1(a: float64): bits32;
  2010. Begin
  2011. extractFloat64Frac1 := a.low;
  2012. End;
  2013. {*
  2014. -------------------------------------------------------------------------------
  2015. Returns the exponent bits of the double-precision floating-point value `a'.
  2016. -------------------------------------------------------------------------------
  2017. *}
  2018. Function extractFloat64Exp(a: float64): int16;
  2019. Begin
  2020. extractFloat64Exp:= ( a.high shr 20 ) AND $7FF;
  2021. End;
  2022. {*
  2023. -------------------------------------------------------------------------------
  2024. Returns the sign bit of the double-precision floating-point value `a'.
  2025. -------------------------------------------------------------------------------
  2026. *}
  2027. Function extractFloat64Sign(a: float64) : flag;
  2028. Begin
  2029. extractFloat64Sign := a.high shr 31;
  2030. End;
  2031. {*
  2032. -------------------------------------------------------------------------------
  2033. Normalizes the subnormal double-precision floating-point value represented
  2034. by the denormalized significand formed by the concatenation of `aSig0' and
  2035. `aSig1'. The normalized exponent is stored at the location pointed to by
  2036. `zExpPtr'. The most significant 21 bits of the normalized significand are
  2037. stored at the location pointed to by `zSig0Ptr', and the least significant
  2038. 32 bits of the normalized significand are stored at the location pointed to
  2039. by `zSig1Ptr'.
  2040. -------------------------------------------------------------------------------
  2041. *}
  2042. Procedure normalizeFloat64Subnormal(
  2043. aSig0: bits32;
  2044. aSig1: bits32;
  2045. VAR zExpPtr : Int16;
  2046. VAR zSig0Ptr : Bits32;
  2047. VAR zSig1Ptr : Bits32
  2048. );
  2049. Var
  2050. ShiftCount : Int8;
  2051. Begin
  2052. if ( aSig0 = 0 ) then
  2053. Begin
  2054. shiftCount := countLeadingZeros32( aSig1 ) - 11;
  2055. if ( shiftCount < 0 ) then
  2056. Begin
  2057. zSig0Ptr := aSig1 shr ( - shiftCount );
  2058. zSig1Ptr := aSig1 shl ( shiftCount AND 31 );
  2059. End
  2060. else
  2061. Begin
  2062. zSig0Ptr := aSig1 shl shiftCount;
  2063. zSig1Ptr := 0;
  2064. End;
  2065. zExpPtr := - shiftCount - 31;
  2066. End
  2067. else
  2068. Begin
  2069. shiftCount := countLeadingZeros32( aSig0 ) - 11;
  2070. shortShift64Left( aSig0, aSig1, shiftCount, zSig0Ptr, zSig1Ptr );
  2071. zExpPtr := 1 - shiftCount;
  2072. End;
  2073. End;
  2074. {*
  2075. -------------------------------------------------------------------------------
  2076. Packs the sign `zSign', the exponent `zExp', and the significand formed by
  2077. the concatenation of `zSig0' and `zSig1' into a double-precision floating-
  2078. point value, returning the result. After being shifted into the proper
  2079. positions, the three fields `zSign', `zExp', and `zSig0' are simply added
  2080. together to form the most significant 32 bits of the result. This means
  2081. that any integer portion of `zSig0' will be added into the exponent. Since
  2082. a properly normalized significand will have an integer portion equal to 1,
  2083. the `zExp' input should be 1 less than the desired result exponent whenever
  2084. `zSig0' and `zSig1' concatenated form a complete, normalized significand.
  2085. -------------------------------------------------------------------------------
  2086. *}
  2087. Procedure
  2088. packFloat64( zSign: Flag; zExp: Int16; zSig0: Bits32; zSig1 : Bits32; VAR c : float64);
  2089. var
  2090. z: Float64;
  2091. Begin
  2092. z.low := zSig1;
  2093. z.high := ( ( bits32 (zSign) ) shl 31 ) + ( ( bits32 (zExp) ) shl 20 ) + zSig0;
  2094. c := z;
  2095. End;
  2096. {*----------------------------------------------------------------------------
  2097. | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  2098. | double-precision floating-point value, returning the result. After being
  2099. | shifted into the proper positions, the three fields are simply added
  2100. | together to form the result. This means that any integer portion of `zSig'
  2101. | will be added into the exponent. Since a properly normalized significand
  2102. | will have an integer portion equal to 1, the `zExp' input should be 1 less
  2103. | than the desired result exponent whenever `zSig' is a complete, normalized
  2104. | significand.
  2105. *----------------------------------------------------------------------------*}
  2106. function packFloat64( zSign: flag; zExp: int16; zSig : bits64): float64;inline;
  2107. begin
  2108. result := float64(( ( bits64(zSign) ) shl 63 ) + ( ( bits64(zExp) ) shl 52 ) + zSig);
  2109. end;
  2110. {*
  2111. -------------------------------------------------------------------------------
  2112. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  2113. and extended significand formed by the concatenation of `zSig0', `zSig1',
  2114. and `zSig2', and returns the proper double-precision floating-point value
  2115. corresponding to the abstract input. Ordinarily, the abstract value is
  2116. simply rounded and packed into the double-precision format, with the inexact
  2117. exception raised if the abstract input cannot be represented exactly.
  2118. However, if the abstract value is too large, the overflow and inexact
  2119. exceptions are raised and an infinity or maximal finite value is returned.
  2120. If the abstract value is too small, the input value is rounded to a
  2121. subnormal number, and the underflow and inexact exceptions are raised if the
  2122. abstract input cannot be represented exactly as a subnormal double-precision
  2123. floating-point number.
  2124. The input significand must be normalized or smaller. If the input
  2125. significand is not normalized, `zExp' must be 0; in that case, the result
  2126. returned is a subnormal number, and it must not require rounding. In the
  2127. usual case that the input significand is normalized, `zExp' must be 1 less
  2128. than the ``true'' floating-point exponent. The handling of underflow and
  2129. overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  2130. -------------------------------------------------------------------------------
  2131. *}
  2132. Procedure
  2133. roundAndPackFloat64(
  2134. zSign: Flag; zExp: Int16; zSig0: Bits32; zSig1: Bits32; zSig2: Bits32; Var c: Float64 );
  2135. Var
  2136. roundingMode : Int8;
  2137. roundNearestEven, increment, isTiny : Flag;
  2138. Begin
  2139. roundingMode := float_rounding_mode;
  2140. roundNearestEven := flag( roundingMode = float_round_nearest_even );
  2141. increment := flag( sbits32 (zSig2) < 0 );
  2142. if ( roundNearestEven = flag(FALSE) ) then
  2143. Begin
  2144. if ( roundingMode = float_round_to_zero ) then
  2145. increment := 0
  2146. else
  2147. Begin
  2148. if ( zSign )<> 0 then
  2149. Begin
  2150. increment := flag( roundingMode = float_round_down ) and zSig2;
  2151. End
  2152. else
  2153. Begin
  2154. increment := flag( roundingMode = float_round_up ) and zSig2;
  2155. End
  2156. End
  2157. End;
  2158. if ( $7FD <= bits16 (zExp) ) then
  2159. Begin
  2160. if (( $7FD < zExp )
  2161. or (( zExp = $7FD )
  2162. and (eq64( $001FFFFF, $FFFFFFFF, zSig0, zSig1 )<>0)
  2163. and (increment<>0)
  2164. )
  2165. ) then
  2166. Begin
  2167. float_raise( float_flag_overflow OR float_flag_inexact );
  2168. if (( roundingMode = float_round_to_zero )
  2169. or ( (zSign<>0) and ( roundingMode = float_round_up ) )
  2170. or ( (zSign = 0) and ( roundingMode = float_round_down ) )
  2171. ) then
  2172. Begin
  2173. packFloat64( zSign, $7FE, $000FFFFF, $FFFFFFFF, c );
  2174. exit;
  2175. End;
  2176. packFloat64( zSign, $7FF, 0, 0, c );
  2177. exit;
  2178. End;
  2179. if ( zExp < 0 ) then
  2180. Begin
  2181. isTiny :=
  2182. flag( float_detect_tininess = float_tininess_before_rounding )
  2183. or flag( zExp < -1 )
  2184. or flag(increment = 0)
  2185. or flag(lt64( zSig0, zSig1, $001FFFFF, $FFFFFFFF)<>0);
  2186. shift64ExtraRightJamming(
  2187. zSig0, zSig1, zSig2, - zExp, zSig0, zSig1, zSig2 );
  2188. zExp := 0;
  2189. if ( isTiny<>0) and (zSig2<>0 ) then float_raise( float_flag_underflow );
  2190. if ( roundNearestEven )<>0 then
  2191. Begin
  2192. increment := flag( sbits32 (zSig2) < 0 );
  2193. End
  2194. else
  2195. Begin
  2196. if ( zSign )<>0 then
  2197. Begin
  2198. increment := flag( roundingMode = float_round_down ) and zSig2;
  2199. End
  2200. else
  2201. Begin
  2202. increment := flag( roundingMode = float_round_up ) and zSig2;
  2203. End
  2204. End;
  2205. End;
  2206. End;
  2207. if ( zSig2 )<>0 then
  2208. softfloat_exception_flags := softfloat_exception_flags OR float_flag_inexact;
  2209. if ( increment )<>0 then
  2210. Begin
  2211. add64( zSig0, zSig1, 0, 1, zSig0, zSig1 );
  2212. zSig1 := zSig1 and not ( bits32(flag( zSig2 + zSig2 = 0 )) and roundNearestEven );
  2213. End
  2214. else
  2215. Begin
  2216. if ( ( zSig0 or zSig1 ) = 0 ) then zExp := 0;
  2217. End;
  2218. packFloat64( zSign, zExp, zSig0, zSig1, c );
  2219. End;
  2220. {*----------------------------------------------------------------------------
  2221. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  2222. | and significand `zSig', and returns the proper double-precision floating-
  2223. | point value corresponding to the abstract input. Ordinarily, the abstract
  2224. | value is simply rounded and packed into the double-precision format, with
  2225. | the inexact exception raised if the abstract input cannot be represented
  2226. | exactly. However, if the abstract value is too large, the overflow and
  2227. | inexact exceptions are raised and an infinity or maximal finite value is
  2228. | returned. If the abstract value is too small, the input value is rounded
  2229. | to a subnormal number, and the underflow and inexact exceptions are raised
  2230. | if the abstract input cannot be represented exactly as a subnormal double-
  2231. | precision floating-point number.
  2232. | The input significand `zSig' has its binary point between bits 62
  2233. | and 61, which is 10 bits to the left of the usual location. This shifted
  2234. | significand must be normalized or smaller. If `zSig' is not normalized,
  2235. | `zExp' must be 0; in that case, the result returned is a subnormal number,
  2236. | and it must not require rounding. In the usual case that `zSig' is
  2237. | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
  2238. | The handling of underflow and overflow follows the IEC/IEEE Standard for
  2239. | Binary Floating-Point Arithmetic.
  2240. *----------------------------------------------------------------------------*}
  2241. function roundAndPackFloat64( zSign: flag; zExp: int16; zSig : bits64): float64;
  2242. var
  2243. roundingMode: int8;
  2244. roundNearestEven: flag;
  2245. roundIncrement, roundBits: int16;
  2246. isTiny: flag;
  2247. begin
  2248. roundingMode := float_rounding_mode;
  2249. roundNearestEven := ord( roundingMode = float_round_nearest_even );
  2250. roundIncrement := $200;
  2251. if ( roundNearestEven=0 ) then
  2252. begin
  2253. if ( roundingMode = float_round_to_zero ) then
  2254. begin
  2255. roundIncrement := 0;
  2256. end
  2257. else begin
  2258. roundIncrement := $3FF;
  2259. if ( zSign<>0 ) then
  2260. begin
  2261. if ( roundingMode = float_round_up ) then
  2262. roundIncrement := 0;
  2263. end
  2264. else begin
  2265. if ( roundingMode = float_round_down ) then
  2266. roundIncrement := 0;
  2267. end
  2268. end
  2269. end;
  2270. roundBits := zSig and $3FF;
  2271. if ( $7FD <= bits16(zExp) ) then
  2272. begin
  2273. if ( ( $7FD < zExp )
  2274. or ( ( zExp = $7FD )
  2275. and ( sbits64( zSig + roundIncrement ) < 0 ) )
  2276. ) then
  2277. begin
  2278. float_raise( float_flag_overflow or float_flag_inexact );
  2279. result := float64(qword(packFloat64( zSign, $7FF, 0 )) - ord( roundIncrement = 0 ));
  2280. exit;
  2281. end;
  2282. if ( zExp < 0 ) then
  2283. begin
  2284. isTiny := ord(
  2285. ( float_detect_tininess = float_tininess_before_rounding )
  2286. or ( zExp < -1 )
  2287. or ( (zSig + roundIncrement) < int64( $8000000000000000 ) ) );
  2288. shift64RightJamming( zSig, - zExp, zSig );
  2289. zExp := 0;
  2290. roundBits := zSig and $3FF;
  2291. if ( isTiny and roundBits )<>0 then
  2292. float_raise( float_flag_underflow );
  2293. end
  2294. end;
  2295. if ( roundBits<>0 ) then
  2296. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  2297. zSig := ( zSig + roundIncrement ) shr 10;
  2298. zSig := zSig and not( ord( ( roundBits xor $200 ) = 0 ) and roundNearestEven );
  2299. if ( zSig = 0 ) then
  2300. zExp := 0;
  2301. result:=packFloat64( zSign, zExp, zSig );
  2302. end;
  2303. {*
  2304. -------------------------------------------------------------------------------
  2305. Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  2306. and significand formed by the concatenation of `zSig0' and `zSig1', and
  2307. returns the proper double-precision floating-point value corresponding
  2308. to the abstract input. This routine is just like `roundAndPackFloat64'
  2309. except that the input significand has fewer bits and does not have to be
  2310. normalized. In all cases, `zExp' must be 1 less than the ``true'' floating-
  2311. point exponent.
  2312. -------------------------------------------------------------------------------
  2313. *}
  2314. Procedure
  2315. normalizeRoundAndPackFloat64(
  2316. zSign:flag; zExp:int16; zSig0:bits32; zSig1:bits32; VAR c: float64 );
  2317. Var
  2318. shiftCount : int8;
  2319. zSig2 : bits32;
  2320. Begin
  2321. if ( zSig0 = 0 ) then
  2322. Begin
  2323. zSig0 := zSig1;
  2324. zSig1 := 0;
  2325. zExp := zExp -32;
  2326. End;
  2327. shiftCount := countLeadingZeros32( zSig0 ) - 11;
  2328. if ( 0 <= shiftCount ) then
  2329. Begin
  2330. zSig2 := 0;
  2331. shortShift64Left( zSig0, zSig1, shiftCount, zSig0, zSig1 );
  2332. End
  2333. else
  2334. Begin
  2335. shift64ExtraRightJamming
  2336. (zSig0, zSig1, 0, - shiftCount, zSig0, zSig1, zSig2 );
  2337. End;
  2338. zExp := zExp - shiftCount;
  2339. roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2, c );
  2340. End;
  2341. {*
  2342. -------------------------------------------------------------------------------
  2343. Returns the result of converting the 32-bit two's complement integer `a' to
  2344. the single-precision floating-point format. The conversion is performed
  2345. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  2346. -------------------------------------------------------------------------------
  2347. *}
  2348. Function int32_to_float32( a: int32): float32rec; compilerproc;
  2349. Var
  2350. zSign : Flag;
  2351. Begin
  2352. if ( a = 0 ) then
  2353. Begin
  2354. int32_to_float32.float32 := 0;
  2355. exit;
  2356. End;
  2357. if ( a = sbits32 ($80000000) ) then
  2358. Begin
  2359. int32_to_float32.float32 := packFloat32( 1, $9E, 0 );
  2360. exit;
  2361. end;
  2362. zSign := flag( a < 0 );
  2363. If zSign<>0 then
  2364. a := -a;
  2365. int32_to_float32.float32:=
  2366. normalizeRoundAndPackFloat32( zSign, $9C, a );
  2367. End;
  2368. {*
  2369. -------------------------------------------------------------------------------
  2370. Returns the result of converting the 32-bit two's complement integer `a' to
  2371. the double-precision floating-point format. The conversion is performed
  2372. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  2373. -------------------------------------------------------------------------------
  2374. *}
  2375. Function int32_to_float64( a: int32) : float64;{$ifdef fpc} [public,Alias:'INT32_TO_FLOAT64'];compilerproc;{$endif}
  2376. var
  2377. zSign : flag;
  2378. absA : bits32;
  2379. shiftCount : int8;
  2380. zSig0, zSig1 : bits32;
  2381. Begin
  2382. if ( a = 0 ) then
  2383. Begin
  2384. packFloat64( 0, 0, 0, 0, result );
  2385. exit;
  2386. end;
  2387. zSign := flag( a < 0 );
  2388. if ZSign<>0 then
  2389. AbsA := -a
  2390. else
  2391. AbsA := a;
  2392. shiftCount := countLeadingZeros32( absA ) - 11;
  2393. if ( 0 <= shiftCount ) then
  2394. Begin
  2395. zSig0 := absA shl shiftCount;
  2396. zSig1 := 0;
  2397. End
  2398. else
  2399. Begin
  2400. shift64Right( absA, 0, - shiftCount, zSig0, zSig1 );
  2401. End;
  2402. packFloat64( zSign, $412 - shiftCount, zSig0, zSig1, result );
  2403. End;
  2404. {*
  2405. -------------------------------------------------------------------------------
  2406. Returns the result of converting the single-precision floating-point value
  2407. `a' to the 32-bit two's complement integer format. The conversion is
  2408. performed according to the IEC/IEEE Standard for Binary Floating-Point
  2409. Arithmetic---which means in particular that the conversion is rounded
  2410. according to the current rounding mode. If `a' is a NaN, the largest
  2411. positive integer is returned. Otherwise, if the conversion overflows, the
  2412. largest integer with the same sign as `a' is returned.
  2413. -------------------------------------------------------------------------------
  2414. *}
  2415. Function float32_to_int32( a : float32rec) : int32;compilerproc;
  2416. Var
  2417. aSign: flag;
  2418. aExp, shiftCount: int16;
  2419. aSig, aSigExtra: bits32;
  2420. z: int32;
  2421. roundingMode: int8;
  2422. Begin
  2423. aSig := extractFloat32Frac( a.float32 );
  2424. aExp := extractFloat32Exp( a.float32 );
  2425. aSign := extractFloat32Sign( a.float32 );
  2426. shiftCount := aExp - $96;
  2427. if ( 0 <= shiftCount ) then
  2428. Begin
  2429. if ( $9E <= aExp ) then
  2430. Begin
  2431. if ( a.float32 <> $CF000000 ) then
  2432. Begin
  2433. float_raise( float_flag_invalid );
  2434. if ( (aSign=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  2435. Begin
  2436. float32_to_int32 := $7FFFFFFF;
  2437. exit;
  2438. End;
  2439. End;
  2440. float32_to_int32 := sbits32 ($80000000);
  2441. exit;
  2442. End;
  2443. z := ( aSig or $00800000 ) shl shiftCount;
  2444. if ( aSign<>0 ) then z := - z;
  2445. End
  2446. else
  2447. Begin
  2448. if ( aExp < $7E ) then
  2449. Begin
  2450. aSigExtra := aExp OR aSig;
  2451. z := 0;
  2452. End
  2453. else
  2454. Begin
  2455. aSig := aSig OR $00800000;
  2456. aSigExtra := aSig shl ( shiftCount and 31 );
  2457. z := aSig shr ( - shiftCount );
  2458. End;
  2459. if ( aSigExtra<>0 ) then
  2460. softfloat_exception_flags := softfloat_exception_flags
  2461. or float_flag_inexact;
  2462. roundingMode := float_rounding_mode;
  2463. if ( roundingMode = float_round_nearest_even ) then
  2464. Begin
  2465. if ( sbits32 (aSigExtra) < 0 ) then
  2466. Begin
  2467. Inc(z);
  2468. if ( bits32 ( aSigExtra shl 1 ) = 0 ) then
  2469. z := z and not 1;
  2470. End;
  2471. if ( aSign<>0 ) then
  2472. z := - z;
  2473. End
  2474. else
  2475. Begin
  2476. aSigExtra := flag( aSigExtra <> 0 );
  2477. if ( aSign<>0 ) then
  2478. Begin
  2479. z := z + (flag( roundingMode = float_round_down ) and aSigExtra);
  2480. z := - z;
  2481. End
  2482. else
  2483. Begin
  2484. z := z + (flag( roundingMode = float_round_up ) and aSigExtra);
  2485. End
  2486. End;
  2487. End;
  2488. float32_to_int32 := z;
  2489. End;
  2490. {*
  2491. -------------------------------------------------------------------------------
  2492. Returns the result of converting the single-precision floating-point value
  2493. `a' to the 32-bit two's complement integer format. The conversion is
  2494. performed according to the IEC/IEEE Standard for Binary Floating-Point
  2495. Arithmetic, except that the conversion is always rounded toward zero.
  2496. If `a' is a NaN, the largest positive integer is returned. Otherwise, if
  2497. the conversion overflows, the largest integer with the same sign as `a' is
  2498. returned.
  2499. -------------------------------------------------------------------------------
  2500. *}
  2501. Function float32_to_int32_round_to_zero( a: Float32rec ): int32;compilerproc;
  2502. Var
  2503. aSign : flag;
  2504. aExp, shiftCount : int16;
  2505. aSig : bits32;
  2506. z : int32;
  2507. Begin
  2508. aSig := extractFloat32Frac( a.float32 );
  2509. aExp := extractFloat32Exp( a.float32 );
  2510. aSign := extractFloat32Sign( a.float32 );
  2511. shiftCount := aExp - $9E;
  2512. if ( 0 <= shiftCount ) then
  2513. Begin
  2514. if ( a.float32 <> $CF000000 ) then
  2515. Begin
  2516. float_raise( float_flag_invalid );
  2517. if ( (aSign=0) or ( ( aExp = $FF ) and (aSig<>0) ) ) then
  2518. Begin
  2519. float32_to_int32_round_to_zero := $7FFFFFFF;
  2520. exit;
  2521. end;
  2522. End;
  2523. float32_to_int32_round_to_zero:= sbits32 ($80000000);
  2524. exit;
  2525. End
  2526. else
  2527. if ( aExp <= $7E ) then
  2528. Begin
  2529. if ( aExp or aSig )<>0 then
  2530. softfloat_exception_flags :=
  2531. softfloat_exception_flags or float_flag_inexact;
  2532. float32_to_int32_round_to_zero := 0;
  2533. exit;
  2534. End;
  2535. aSig := ( aSig or $00800000 ) shl 8;
  2536. z := aSig shr ( - shiftCount );
  2537. if ( bits32 ( aSig shl ( shiftCount and 31 ) )<> 0 ) then
  2538. Begin
  2539. softfloat_exception_flags :=
  2540. softfloat_exception_flags or float_flag_inexact;
  2541. End;
  2542. if ( aSign<>0 ) then z := - z;
  2543. float32_to_int32_round_to_zero := z;
  2544. End;
  2545. {*
  2546. -------------------------------------------------------------------------------
  2547. Returns the result of converting the single-precision floating-point value
  2548. `a' to the double-precision floating-point format. The conversion is
  2549. performed according to the IEC/IEEE Standard for Binary Floating-Point
  2550. Arithmetic.
  2551. -------------------------------------------------------------------------------
  2552. *}
  2553. Function float32_to_float64( a : float32rec) : Float64;compilerproc;
  2554. Var
  2555. aSign : flag;
  2556. aExp : int16;
  2557. aSig, zSig0, zSig1: bits32;
  2558. tmp : CommonNanT;
  2559. Begin
  2560. aSig := extractFloat32Frac( a.float32 );
  2561. aExp := extractFloat32Exp( a.float32 );
  2562. aSign := extractFloat32Sign( a.float32 );
  2563. if ( aExp = $FF ) then
  2564. Begin
  2565. if ( aSig<>0 ) then
  2566. Begin
  2567. float32ToCommonNaN(a.float32, tmp);
  2568. commonNaNToFloat64(tmp , result);
  2569. exit;
  2570. End;
  2571. packFloat64( aSign, $7FF, 0, 0, result);
  2572. exit;
  2573. End;
  2574. if ( aExp = 0 ) then
  2575. Begin
  2576. if ( aSig = 0 ) then
  2577. Begin
  2578. packFloat64( aSign, 0, 0, 0, result );
  2579. exit;
  2580. end;
  2581. normalizeFloat32Subnormal( aSig, aExp, aSig );
  2582. Dec(aExp);
  2583. End;
  2584. shift64Right( aSig, 0, 3, zSig0, zSig1 );
  2585. packFloat64( aSign, aExp + $380, zSig0, zSig1, result );
  2586. End;
  2587. {*
  2588. -------------------------------------------------------------------------------
  2589. Rounds the single-precision floating-point value `a' to an integer,
  2590. and returns the result as a single-precision floating-point value. The
  2591. operation is performed according to the IEC/IEEE Standard for Binary
  2592. Floating-Point Arithmetic.
  2593. -------------------------------------------------------------------------------
  2594. *}
  2595. Function float32_round_to_int( a: float32rec): float32rec;compilerproc;
  2596. Var
  2597. aSign: flag;
  2598. aExp: int16;
  2599. lastBitMask, roundBitsMask: bits32;
  2600. roundingMode: int8;
  2601. z: float32;
  2602. Begin
  2603. aExp := extractFloat32Exp( a.float32 );
  2604. if ( $96 <= aExp ) then
  2605. Begin
  2606. if ( ( aExp = $FF ) and (extractFloat32Frac( a.float32 )<>0) ) then
  2607. Begin
  2608. float32_round_to_int.float32 := propagateFloat32NaN( a.float32, a.float32 );
  2609. exit;
  2610. End;
  2611. float32_round_to_int:=a;
  2612. exit;
  2613. End;
  2614. if ( aExp <= $7E ) then
  2615. Begin
  2616. if ( bits32 ( a.float32 shl 1 ) = 0 ) then
  2617. Begin
  2618. float32_round_to_int:=a;
  2619. exit;
  2620. end;
  2621. softfloat_exception_flags
  2622. := softfloat_exception_flags OR float_flag_inexact;
  2623. aSign := extractFloat32Sign( a.float32 );
  2624. case ( float_rounding_mode ) of
  2625. float_round_nearest_even:
  2626. Begin
  2627. if ( ( aExp = $7E ) and (extractFloat32Frac( a.float32 )<>0) ) then
  2628. Begin
  2629. float32_round_to_int.float32 := packFloat32( aSign, $7F, 0 );
  2630. exit;
  2631. End;
  2632. End;
  2633. float_round_down:
  2634. Begin
  2635. if aSign <> 0 then
  2636. float32_round_to_int.float32 := $BF800000
  2637. else
  2638. float32_round_to_int.float32 := 0;
  2639. exit;
  2640. End;
  2641. float_round_up:
  2642. Begin
  2643. if aSign <> 0 then
  2644. float32_round_to_int.float32 := $80000000
  2645. else
  2646. float32_round_to_int.float32 := $3F800000;
  2647. exit;
  2648. End;
  2649. end;
  2650. float32_round_to_int.float32 := packFloat32( aSign, 0, 0 );
  2651. End;
  2652. lastBitMask := 1;
  2653. {_____________________________!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}
  2654. lastBitMask := lastBitMask shl ($96 - aExp);
  2655. roundBitsMask := lastBitMask - 1;
  2656. z := a.float32;
  2657. roundingMode := float_rounding_mode;
  2658. if ( roundingMode = float_round_nearest_even ) then
  2659. Begin
  2660. z := z + (lastBitMask shr 1);
  2661. if ( ( z and roundBitsMask ) = 0 ) then
  2662. z := z and not lastBitMask;
  2663. End
  2664. else if ( roundingMode <> float_round_to_zero ) then
  2665. Begin
  2666. if ( (extractFloat32Sign( z ) xor flag(roundingMode = float_round_up ))<>0 ) then
  2667. Begin
  2668. z := z + roundBitsMask;
  2669. End;
  2670. End;
  2671. z := z and not roundBitsMask;
  2672. if ( z <> a.float32 ) then
  2673. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  2674. float32_round_to_int.float32 := z;
  2675. End;
  2676. {*
  2677. -------------------------------------------------------------------------------
  2678. Returns the result of adding the absolute values of the single-precision
  2679. floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
  2680. before being returned. `zSign' is ignored if the result is a NaN.
  2681. The addition is performed according to the IEC/IEEE Standard for Binary
  2682. Floating-Point Arithmetic.
  2683. -------------------------------------------------------------------------------
  2684. *}
  2685. Function addFloat32Sigs( a:float32; b: float32; zSign:flag ): float32;
  2686. Var
  2687. aExp, bExp, zExp: int16;
  2688. aSig, bSig, zSig: bits32;
  2689. expDiff: int16;
  2690. label roundAndPack;
  2691. Begin
  2692. aSig:=extractFloat32Frac( a );
  2693. aExp:=extractFloat32Exp( a );
  2694. bSig:=extractFloat32Frac( b );
  2695. bExp := extractFloat32Exp( b );
  2696. expDiff := aExp - bExp;
  2697. aSig := aSig shl 6;
  2698. bSig := bSig shl 6;
  2699. if ( 0 < expDiff ) then
  2700. Begin
  2701. if ( aExp = $FF ) then
  2702. Begin
  2703. if ( aSig <> 0) then
  2704. Begin
  2705. addFloat32Sigs := propagateFloat32NaN( a, b );
  2706. exit;
  2707. End;
  2708. addFloat32Sigs := a;
  2709. exit;
  2710. End;
  2711. if ( bExp = 0 ) then
  2712. Begin
  2713. Dec(expDiff);
  2714. End
  2715. else
  2716. Begin
  2717. bSig := bSig or $20000000;
  2718. End;
  2719. shift32RightJamming( bSig, expDiff, bSig );
  2720. zExp := aExp;
  2721. End
  2722. else
  2723. If ( expDiff < 0 ) then
  2724. Begin
  2725. if ( bExp = $FF ) then
  2726. Begin
  2727. if ( bSig<>0 ) then
  2728. Begin
  2729. addFloat32Sigs := propagateFloat32NaN( a, b );
  2730. exit;
  2731. end;
  2732. addFloat32Sigs := packFloat32( zSign, $FF, 0 );
  2733. exit;
  2734. End;
  2735. if ( aExp = 0 ) then
  2736. Begin
  2737. Inc(expDiff);
  2738. End
  2739. else
  2740. Begin
  2741. aSig := aSig OR $20000000;
  2742. End;
  2743. shift32RightJamming( aSig, - expDiff, aSig );
  2744. zExp := bExp;
  2745. End
  2746. else
  2747. Begin
  2748. if ( aExp = $FF ) then
  2749. Begin
  2750. if ( aSig OR bSig )<> 0 then
  2751. Begin
  2752. addFloat32Sigs := propagateFloat32NaN( a, b );
  2753. exit;
  2754. end;
  2755. addFloat32Sigs := a;
  2756. exit;
  2757. End;
  2758. if ( aExp = 0 ) then
  2759. Begin
  2760. addFloat32Sigs := packFloat32( zSign, 0, ( aSig + bSig ) shr 6 );
  2761. exit;
  2762. end;
  2763. zSig := $40000000 + aSig + bSig;
  2764. zExp := aExp;
  2765. goto roundAndPack;
  2766. End;
  2767. aSig := aSig OR $20000000;
  2768. zSig := ( aSig + bSig ) shl 1;
  2769. Dec(zExp);
  2770. if ( sbits32 (zSig) < 0 ) then
  2771. Begin
  2772. zSig := aSig + bSig;
  2773. Inc(zExp);
  2774. End;
  2775. roundAndPack:
  2776. addFloat32Sigs := roundAndPackFloat32( zSign, zExp, zSig );
  2777. End;
  2778. {*
  2779. -------------------------------------------------------------------------------
  2780. Returns the result of subtracting the absolute values of the single-
  2781. precision floating-point values `a' and `b'. If `zSign' is 1, the
  2782. difference is negated before being returned. `zSign' is ignored if the
  2783. result is a NaN. The subtraction is performed according to the IEC/IEEE
  2784. Standard for Binary Floating-Point Arithmetic.
  2785. -------------------------------------------------------------------------------
  2786. *}
  2787. Function subFloat32Sigs( a:float32; b:float32; zSign:flag ): float32;
  2788. Var
  2789. aExp, bExp, zExp: int16;
  2790. aSig, bSig, zSig: bits32;
  2791. expDiff : int16;
  2792. label aExpBigger;
  2793. label bExpBigger;
  2794. label aBigger;
  2795. label bBigger;
  2796. label normalizeRoundAndPack;
  2797. Begin
  2798. aSig := extractFloat32Frac( a );
  2799. aExp := extractFloat32Exp( a );
  2800. bSig := extractFloat32Frac( b );
  2801. bExp := extractFloat32Exp( b );
  2802. expDiff := aExp - bExp;
  2803. aSig := aSig shl 7;
  2804. bSig := bSig shl 7;
  2805. if ( 0 < expDiff ) then goto aExpBigger;
  2806. if ( expDiff < 0 ) then goto bExpBigger;
  2807. if ( aExp = $FF ) then
  2808. Begin
  2809. if ( aSig OR bSig )<> 0 then
  2810. Begin
  2811. subFloat32Sigs := propagateFloat32NaN( a, b );
  2812. exit;
  2813. End;
  2814. float_raise( float_flag_invalid );
  2815. subFloat32Sigs := float32_default_nan;
  2816. exit;
  2817. End;
  2818. if ( aExp = 0 ) then
  2819. Begin
  2820. aExp := 1;
  2821. bExp := 1;
  2822. End;
  2823. if ( bSig < aSig ) Then goto aBigger;
  2824. if ( aSig < bSig ) Then goto bBigger;
  2825. subFloat32Sigs := packFloat32( flag(float_rounding_mode = float_round_down), 0, 0 );
  2826. exit;
  2827. bExpBigger:
  2828. if ( bExp = $FF ) then
  2829. Begin
  2830. if ( bSig<>0 ) then
  2831. Begin
  2832. subFloat32Sigs := propagateFloat32NaN( a, b );
  2833. exit;
  2834. End;
  2835. subFloat32Sigs := packFloat32( zSign XOR 1, $FF, 0 );
  2836. exit;
  2837. End;
  2838. if ( aExp = 0 ) then
  2839. Begin
  2840. Inc(expDiff);
  2841. End
  2842. else
  2843. Begin
  2844. aSig := aSig OR $40000000;
  2845. End;
  2846. shift32RightJamming( aSig, - expDiff, aSig );
  2847. bSig := bSig OR $40000000;
  2848. bBigger:
  2849. zSig := bSig - aSig;
  2850. zExp := bExp;
  2851. zSign := zSign xor 1;
  2852. goto normalizeRoundAndPack;
  2853. aExpBigger:
  2854. if ( aExp = $FF ) then
  2855. Begin
  2856. if ( aSig <> 0) then
  2857. Begin
  2858. subFloat32Sigs := propagateFloat32NaN( a, b );
  2859. exit;
  2860. End;
  2861. subFloat32Sigs := a;
  2862. exit;
  2863. End;
  2864. if ( bExp = 0 ) then
  2865. Begin
  2866. Dec(expDiff);
  2867. End
  2868. else
  2869. Begin
  2870. bSig := bSig OR $40000000;
  2871. End;
  2872. shift32RightJamming( bSig, expDiff, bSig );
  2873. aSig := aSig OR $40000000;
  2874. aBigger:
  2875. zSig := aSig - bSig;
  2876. zExp := aExp;
  2877. normalizeRoundAndPack:
  2878. Dec(zExp);
  2879. subFloat32Sigs := normalizeRoundAndPackFloat32( zSign, zExp, zSig );
  2880. End;
  2881. {*
  2882. -------------------------------------------------------------------------------
  2883. Returns the result of adding the single-precision floating-point values `a'
  2884. and `b'. The operation is performed according to the IEC/IEEE Standard for
  2885. Binary Floating-Point Arithmetic.
  2886. -------------------------------------------------------------------------------
  2887. *}
  2888. Function float32_add( a: float32rec; b:float32rec ): float32rec; compilerproc;
  2889. Var
  2890. aSign, bSign: Flag;
  2891. Begin
  2892. aSign := extractFloat32Sign( a.float32 );
  2893. bSign := extractFloat32Sign( b.float32 );
  2894. if ( aSign = bSign ) then
  2895. Begin
  2896. float32_add.float32 := addFloat32Sigs( a.float32, b.float32, aSign );
  2897. End
  2898. else
  2899. Begin
  2900. float32_add.float32 := subFloat32Sigs( a.float32, b.float32, aSign );
  2901. End;
  2902. End;
  2903. {*
  2904. -------------------------------------------------------------------------------
  2905. Returns the result of subtracting the single-precision floating-point values
  2906. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  2907. for Binary Floating-Point Arithmetic.
  2908. -------------------------------------------------------------------------------
  2909. *}
  2910. Function float32_sub( a: float32rec ; b:float32rec ): float32rec;compilerproc;
  2911. Var
  2912. aSign, bSign: flag;
  2913. Begin
  2914. aSign := extractFloat32Sign( a.float32 );
  2915. bSign := extractFloat32Sign( b.float32 );
  2916. if ( aSign = bSign ) then
  2917. Begin
  2918. float32_sub.float32 := subFloat32Sigs( a.float32, b.float32, aSign );
  2919. End
  2920. else
  2921. Begin
  2922. float32_sub.float32 := addFloat32Sigs( a.float32, b.float32, aSign );
  2923. End;
  2924. End;
  2925. {*
  2926. -------------------------------------------------------------------------------
  2927. Returns the result of multiplying the single-precision floating-point values
  2928. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  2929. for Binary Floating-Point Arithmetic.
  2930. -------------------------------------------------------------------------------
  2931. *}
  2932. Function float32_mul(a: float32rec; b: float32rec ) : float32rec; compilerproc;
  2933. Var
  2934. aSign, bSign, zSign: flag;
  2935. aExp, bExp, zExp : int16;
  2936. aSig, bSig, zSig0, zSig1: bits32;
  2937. Begin
  2938. aSig := extractFloat32Frac( a.float32 );
  2939. aExp := extractFloat32Exp( a.float32 );
  2940. aSign := extractFloat32Sign( a.float32 );
  2941. bSig := extractFloat32Frac( b.float32 );
  2942. bExp := extractFloat32Exp( b.float32 );
  2943. bSign := extractFloat32Sign( b.float32 );
  2944. zSign := aSign xor bSign;
  2945. if ( aExp = $FF ) then
  2946. Begin
  2947. if ( (aSig<>0) OR ( ( bExp = $FF ) AND (bSig<>0) ) ) then
  2948. Begin
  2949. float32_mul.float32 := propagateFloat32NaN( a.float32, b.float32 );
  2950. End;
  2951. if ( ( bExp OR bSig ) = 0 ) then
  2952. Begin
  2953. float_raise( float_flag_invalid );
  2954. float32_mul.float32 := float32_default_nan;
  2955. exit;
  2956. End;
  2957. float32_mul.float32 := packFloat32( zSign, $FF, 0 );
  2958. exit;
  2959. End;
  2960. if ( bExp = $FF ) then
  2961. Begin
  2962. if ( bSig <> 0 ) then
  2963. Begin
  2964. float32_mul.float32 := propagateFloat32NaN( a.float32, b.float32 );
  2965. exit;
  2966. End;
  2967. if ( ( aExp OR aSig ) = 0 ) then
  2968. Begin
  2969. float_raise( float_flag_invalid );
  2970. float32_mul.float32 := float32_default_nan;
  2971. exit;
  2972. End;
  2973. float32_mul.float32 := packFloat32( zSign, $FF, 0 );
  2974. exit;
  2975. End;
  2976. if ( aExp = 0 ) then
  2977. Begin
  2978. if ( aSig = 0 ) then
  2979. Begin
  2980. float32_mul.float32 := packFloat32( zSign, 0, 0 );
  2981. exit;
  2982. End;
  2983. normalizeFloat32Subnormal( aSig, aExp, aSig );
  2984. End;
  2985. if ( bExp = 0 ) then
  2986. Begin
  2987. if ( bSig = 0 ) then
  2988. Begin
  2989. float32_mul.float32 := packFloat32( zSign, 0, 0 );
  2990. exit;
  2991. End;
  2992. normalizeFloat32Subnormal( bSig, bExp, bSig );
  2993. End;
  2994. zExp := aExp + bExp - $7F;
  2995. aSig := ( aSig OR $00800000 ) shl 7;
  2996. bSig := ( bSig OR $00800000 ) shl 8;
  2997. mul32To64( aSig, bSig, zSig0, zSig1 );
  2998. zSig0 := zSig0 OR bits32( zSig1 <> 0 );
  2999. if ( 0 <= sbits32 ( zSig0 shl 1 ) ) then
  3000. Begin
  3001. zSig0 := zSig0 shl 1;
  3002. Dec(zExp);
  3003. End;
  3004. float32_mul.float32 := roundAndPackFloat32( zSign, zExp, zSig0 );
  3005. End;
  3006. {*
  3007. -------------------------------------------------------------------------------
  3008. Returns the result of dividing the single-precision floating-point value `a'
  3009. by the corresponding value `b'. The operation is performed according to the
  3010. IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3011. -------------------------------------------------------------------------------
  3012. *}
  3013. Function float32_div(a: float32rec;b: float32rec ): float32rec; compilerproc;
  3014. Var
  3015. aSign, bSign, zSign: flag;
  3016. aExp, bExp, zExp: int16;
  3017. aSig, bSig, zSig, rem0, rem1, term0, term1: bits32;
  3018. Begin
  3019. aSig := extractFloat32Frac( a.float32 );
  3020. aExp := extractFloat32Exp( a.float32 );
  3021. aSign := extractFloat32Sign( a.float32 );
  3022. bSig := extractFloat32Frac( b.float32 );
  3023. bExp := extractFloat32Exp( b.float32 );
  3024. bSign := extractFloat32Sign( b.float32 );
  3025. zSign := aSign xor bSign;
  3026. if ( aExp = $FF ) then
  3027. Begin
  3028. if ( aSig <> 0 ) then
  3029. Begin
  3030. float32_div.float32 := propagateFloat32NaN( a.float32, b.float32 );
  3031. exit;
  3032. End;
  3033. if ( bExp = $FF ) then
  3034. Begin
  3035. if ( bSig <> 0) then
  3036. Begin
  3037. float32_div.float32 := propagateFloat32NaN( a.float32, b.float32 );
  3038. End;
  3039. float_raise( float_flag_invalid );
  3040. float32_div.float32 := float32_default_nan;
  3041. exit;
  3042. End;
  3043. float32_div.float32 := packFloat32( zSign, $FF, 0 );
  3044. exit;
  3045. End;
  3046. if ( bExp = $FF ) then
  3047. Begin
  3048. if ( bSig <> 0) then
  3049. Begin
  3050. float32_div.float32 := propagateFloat32NaN( a.float32, b.float32 );
  3051. exit;
  3052. End;
  3053. float32_div.float32 := packFloat32( zSign, 0, 0 );
  3054. exit;
  3055. End;
  3056. if ( bExp = 0 ) Then
  3057. Begin
  3058. if ( bSig = 0 ) Then
  3059. Begin
  3060. if ( ( aExp OR aSig ) = 0 ) then
  3061. Begin
  3062. float_raise( float_flag_invalid );
  3063. float32_div.float32 := float32_default_nan;
  3064. exit;
  3065. End;
  3066. float_raise( float_flag_divbyzero );
  3067. float32_div.float32 := packFloat32( zSign, $FF, 0 );
  3068. exit;
  3069. End;
  3070. normalizeFloat32Subnormal( bSig, bExp, bSig );
  3071. End;
  3072. if ( aExp = 0 ) Then
  3073. Begin
  3074. if ( aSig = 0 ) Then
  3075. Begin
  3076. float32_div.float32 := packFloat32( zSign, 0, 0 );
  3077. exit;
  3078. End;
  3079. normalizeFloat32Subnormal( aSig, aExp, aSig );
  3080. End;
  3081. zExp := aExp - bExp + $7D;
  3082. aSig := ( aSig OR $00800000 ) shl 7;
  3083. bSig := ( bSig OR $00800000 ) shl 8;
  3084. if ( bSig <= ( aSig + aSig ) ) then
  3085. Begin
  3086. aSig := aSig shr 1;
  3087. Inc(zExp);
  3088. End;
  3089. zSig := estimateDiv64To32( aSig, 0, bSig );
  3090. if ( ( zSig and $3F ) <= 2 ) then
  3091. Begin
  3092. mul32To64( bSig, zSig, term0, term1 );
  3093. sub64( aSig, 0, term0, term1, rem0, rem1 );
  3094. while ( sbits32 (rem0) < 0 ) do
  3095. Begin
  3096. Dec(zSig);
  3097. add64( rem0, rem1, 0, bSig, rem0, rem1 );
  3098. End;
  3099. zSig := zSig or bits32( rem1 <> 0 );
  3100. End;
  3101. float32_div.float32 := roundAndPackFloat32( zSign, zExp, zSig );
  3102. End;
  3103. {*
  3104. -------------------------------------------------------------------------------
  3105. Returns the remainder of the single-precision floating-point value `a'
  3106. with respect to the corresponding value `b'. The operation is performed
  3107. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3108. -------------------------------------------------------------------------------
  3109. *}
  3110. Function float32_rem(a: float32rec; b: float32rec ):float32rec; compilerproc;
  3111. Var
  3112. aSign, bSign, zSign: flag;
  3113. aExp, bExp, expDiff: int16;
  3114. aSig, bSig, q, allZero, alternateASig: bits32;
  3115. sigMean: sbits32;
  3116. Begin
  3117. aSig := extractFloat32Frac( a.float32 );
  3118. aExp := extractFloat32Exp( a.float32 );
  3119. aSign := extractFloat32Sign( a.float32 );
  3120. bSig := extractFloat32Frac( b.float32 );
  3121. bExp := extractFloat32Exp( b.float32 );
  3122. bSign := extractFloat32Sign( b.float32 );
  3123. if ( aExp = $FF ) then
  3124. Begin
  3125. if ( (aSig<>0) OR ( ( bExp = $FF ) AND (bSig <>0)) ) then
  3126. Begin
  3127. float32_rem.float32 := propagateFloat32NaN( a.float32, b.float32 );
  3128. exit;
  3129. End;
  3130. float_raise( float_flag_invalid );
  3131. float32_rem.float32 := float32_default_nan;
  3132. exit;
  3133. End;
  3134. if ( bExp = $FF ) then
  3135. Begin
  3136. if ( bSig <> 0 ) then
  3137. Begin
  3138. float32_rem.float32 := propagateFloat32NaN( a.float32, b.float32 );
  3139. exit;
  3140. End;
  3141. float32_rem := a;
  3142. exit;
  3143. End;
  3144. if ( bExp = 0 ) then
  3145. Begin
  3146. if ( bSig = 0 ) then
  3147. Begin
  3148. float_raise( float_flag_invalid );
  3149. float32_rem.float32 := float32_default_nan;
  3150. exit;
  3151. End;
  3152. normalizeFloat32Subnormal( bSig, bExp, bSig );
  3153. End;
  3154. if ( aExp = 0 ) then
  3155. Begin
  3156. if ( aSig = 0 ) then
  3157. Begin
  3158. float32_rem := a;
  3159. exit;
  3160. End;
  3161. normalizeFloat32Subnormal( aSig, aExp, aSig );
  3162. End;
  3163. expDiff := aExp - bExp;
  3164. aSig := ( aSig OR $00800000 ) shl 8;
  3165. bSig := ( bSig OR $00800000 ) shl 8;
  3166. if ( expDiff < 0 ) then
  3167. Begin
  3168. if ( expDiff < -1 ) then
  3169. Begin
  3170. float32_rem := a;
  3171. exit;
  3172. End;
  3173. aSig := aSig shr 1;
  3174. End;
  3175. q := bits32( bSig <= aSig );
  3176. if ( q <> 0) then
  3177. aSig := aSig - bSig;
  3178. expDiff := expDiff - 32;
  3179. while ( 0 < expDiff ) do
  3180. Begin
  3181. q := estimateDiv64To32( aSig, 0, bSig );
  3182. if (2 < q) then
  3183. q := q - 2
  3184. else
  3185. q := 0;
  3186. aSig := - ( ( bSig shr 2 ) * q );
  3187. expDiff := expDiff - 30;
  3188. End;
  3189. expDiff := expDiff + 32;
  3190. if ( 0 < expDiff ) then
  3191. Begin
  3192. q := estimateDiv64To32( aSig, 0, bSig );
  3193. if (2 < q) then
  3194. q := q - 2
  3195. else
  3196. q := 0;
  3197. q := q shr (32 - expDiff);
  3198. bSig := bSig shr 2;
  3199. aSig := ( ( aSig shr 1 ) shl ( expDiff - 1 ) ) - bSig * q;
  3200. End
  3201. else
  3202. Begin
  3203. aSig := aSig shr 2;
  3204. bSig := bSig shr 2;
  3205. End;
  3206. Repeat
  3207. alternateASig := aSig;
  3208. Inc(q);
  3209. aSig := aSig - bSig;
  3210. Until not ( 0 <= sbits32 (aSig) );
  3211. sigMean := aSig + alternateASig;
  3212. if ( ( sigMean < 0 ) OR ( ( sigMean = 0 ) AND (( q and 1 )<>0) ) ) then
  3213. Begin
  3214. aSig := alternateASig;
  3215. End;
  3216. zSign := flag( sbits32 (aSig) < 0 );
  3217. if ( zSign<>0 ) then
  3218. aSig := - aSig;
  3219. float32_rem.float32 := normalizeRoundAndPackFloat32( aSign xor zSign, bExp, aSig );
  3220. End;
  3221. {*
  3222. -------------------------------------------------------------------------------
  3223. Returns the square root of the single-precision floating-point value `a'.
  3224. The operation is performed according to the IEC/IEEE Standard for Binary
  3225. Floating-Point Arithmetic.
  3226. -------------------------------------------------------------------------------
  3227. *}
  3228. Function float32_sqrt(a: float32rec ): float32rec;compilerproc;
  3229. Var
  3230. aSign : flag;
  3231. aExp, zExp : int16;
  3232. aSig, zSig, rem0, rem1, term0, term1: bits32;
  3233. label roundAndPack;
  3234. Begin
  3235. aSig := extractFloat32Frac( a.float32 );
  3236. aExp := extractFloat32Exp( a.float32 );
  3237. aSign := extractFloat32Sign( a.float32 );
  3238. if ( aExp = $FF ) then
  3239. Begin
  3240. if ( aSig <> 0) then
  3241. Begin
  3242. float32_sqrt.float32 := propagateFloat32NaN( a.float32, 0 );
  3243. exit;
  3244. End;
  3245. if ( aSign = 0) then
  3246. Begin
  3247. float32_sqrt := a;
  3248. exit;
  3249. End;
  3250. float_raise( float_flag_invalid );
  3251. float32_sqrt.float32 := float32_default_nan;
  3252. exit;
  3253. End;
  3254. if ( aSign <> 0) then
  3255. Begin
  3256. if ( ( aExp OR aSig ) = 0 ) then
  3257. Begin
  3258. float32_sqrt := a;
  3259. exit;
  3260. End;
  3261. float_raise( float_flag_invalid );
  3262. float32_sqrt.float32 := float32_default_nan;
  3263. exit;
  3264. End;
  3265. if ( aExp = 0 ) then
  3266. Begin
  3267. if ( aSig = 0 ) then
  3268. Begin
  3269. float32_sqrt.float32 := 0;
  3270. exit;
  3271. End;
  3272. normalizeFloat32Subnormal( aSig, aExp, aSig );
  3273. End;
  3274. zExp := ( ( aExp - $7F ) shr 1 ) + $7E;
  3275. aSig := ( aSig OR $00800000 ) shl 8;
  3276. zSig := estimateSqrt32( aExp, aSig ) + 2;
  3277. if ( ( zSig and $7F ) <= 5 ) then
  3278. Begin
  3279. if ( zSig < 2 ) then
  3280. Begin
  3281. zSig := $7FFFFFFF;
  3282. goto roundAndPack;
  3283. End
  3284. else
  3285. Begin
  3286. aSig := aSig shr (aExp and 1);
  3287. mul32To64( zSig, zSig, term0, term1 );
  3288. sub64( aSig, 0, term0, term1, rem0, rem1 );
  3289. while ( sbits32 (rem0) < 0 ) do
  3290. Begin
  3291. Dec(zSig);
  3292. shortShift64Left( 0, zSig, 1, term0, term1 );
  3293. term1 := term1 or 1;
  3294. add64( rem0, rem1, term0, term1, rem0, rem1 );
  3295. End;
  3296. zSig := zSig OR bits32( ( rem0 OR rem1 ) <> 0 );
  3297. End;
  3298. End;
  3299. shift32RightJamming( zSig, 1, zSig );
  3300. roundAndPack:
  3301. float32_sqrt.float32 := roundAndPackFloat32( 0, zExp, zSig );
  3302. End;
  3303. {*
  3304. -------------------------------------------------------------------------------
  3305. Returns 1 if the single-precision floating-point value `a' is equal to
  3306. the corresponding value `b', and 0 otherwise. The comparison is performed
  3307. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3308. -------------------------------------------------------------------------------
  3309. *}
  3310. Function float32_eq( a:float32rec; b:float32rec): flag; compilerproc;
  3311. Begin
  3312. if ((( extractFloat32Exp( a.float32 ) = $FF ) AND (extractFloat32Frac( a.float32 )<>0))
  3313. OR ( ( extractFloat32Exp( b.float32 ) = $FF ) AND (extractFloat32Frac( b.float32 )<>0) )
  3314. ) then
  3315. Begin
  3316. if ( (float32_is_signaling_nan( a.float32 )<>0) OR (float32_is_signaling_nan( b.float32 )<>0) ) then
  3317. Begin
  3318. float_raise( float_flag_invalid );
  3319. End;
  3320. float32_eq := 0;
  3321. exit;
  3322. End;
  3323. float32_eq := flag( a.float32 = b.float32 ) OR flag( bits32 ( ( a.float32 OR b.float32 ) shl 1 ) = 0 );
  3324. End;
  3325. {*
  3326. -------------------------------------------------------------------------------
  3327. Returns 1 if the single-precision floating-point value `a' is less than
  3328. or equal to the corresponding value `b', and 0 otherwise. The comparison
  3329. is performed according to the IEC/IEEE Standard for Binary Floating-Point
  3330. Arithmetic.
  3331. -------------------------------------------------------------------------------
  3332. *}
  3333. Function float32_le( a: float32rec; b : float32rec ):flag;compilerproc;
  3334. var
  3335. aSign, bSign: flag;
  3336. Begin
  3337. if ( ( ( extractFloat32Exp( a.float32 ) = $FF ) AND (extractFloat32Frac( a.float32 )<>0) )
  3338. OR ( ( extractFloat32Exp( b.float32 ) = $FF ) AND (extractFloat32Frac( b.float32 )<>0) )
  3339. ) then
  3340. Begin
  3341. float_raise( float_flag_invalid );
  3342. float32_le := 0;
  3343. exit;
  3344. End;
  3345. aSign := extractFloat32Sign( a.float32 );
  3346. bSign := extractFloat32Sign( b.float32 );
  3347. if ( aSign <> bSign ) then
  3348. Begin
  3349. float32_le := aSign OR flag( bits32 ( ( a.float32 OR b.float32 ) shl 1 ) = 0 );
  3350. exit;
  3351. End;
  3352. float32_le := flag(flag( a.float32 = b.float32 ) OR flag( aSign xor flag( a.float32 < b.float32 ) ));
  3353. End;
  3354. {*
  3355. -------------------------------------------------------------------------------
  3356. Returns 1 if the single-precision floating-point value `a' is less than
  3357. the corresponding value `b', and 0 otherwise. The comparison is performed
  3358. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3359. -------------------------------------------------------------------------------
  3360. *}
  3361. Function float32_lt( a:float32rec ; b : float32rec): flag; compilerproc;
  3362. var
  3363. aSign, bSign: flag;
  3364. Begin
  3365. if ( ( ( extractFloat32Exp( a.float32 ) = $FF ) AND (extractFloat32Frac( a.float32 ) <>0))
  3366. OR ( ( extractFloat32Exp( b.float32 ) = $FF ) AND (extractFloat32Frac( b.float32 ) <>0) )
  3367. ) then
  3368. Begin
  3369. float_raise( float_flag_invalid );
  3370. float32_lt :=0;
  3371. exit;
  3372. End;
  3373. aSign := extractFloat32Sign( a.float32 );
  3374. bSign := extractFloat32Sign( b.float32 );
  3375. if ( aSign <> bSign ) then
  3376. Begin
  3377. float32_lt := aSign AND flag( bits32 ( ( a.float32 OR b.float32 ) shl 1 ) <> 0 );
  3378. exit;
  3379. End;
  3380. float32_lt := flag(flag( a.float32 <> b.float32 ) AND flag( aSign xor flag( a.float32 < b.float32 ) ));
  3381. End;
  3382. {*
  3383. -------------------------------------------------------------------------------
  3384. Returns 1 if the single-precision floating-point value `a' is equal to
  3385. the corresponding value `b', and 0 otherwise. The invalid exception is
  3386. raised if either operand is a NaN. Otherwise, the comparison is performed
  3387. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3388. -------------------------------------------------------------------------------
  3389. *}
  3390. Function float32_eq_signaling( a: float32; b: float32) : flag;
  3391. Begin
  3392. if ( ( ( extractFloat32Exp( a ) = $FF ) AND (extractFloat32Frac( a ) <> 0))
  3393. OR ( ( extractFloat32Exp( b ) = $FF ) AND (extractFloat32Frac( b ) <> 0))
  3394. ) then
  3395. Begin
  3396. float_raise( float_flag_invalid );
  3397. float32_eq_signaling := 0;
  3398. exit;
  3399. End;
  3400. float32_eq_signaling := (flag( a = b ) OR flag( bits32 ( ( a OR b ) shl 1 ) = 0 ));
  3401. End;
  3402. {*
  3403. -------------------------------------------------------------------------------
  3404. Returns 1 if the single-precision floating-point value `a' is less than or
  3405. equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
  3406. cause an exception. Otherwise, the comparison is performed according to the
  3407. IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  3408. -------------------------------------------------------------------------------
  3409. *}
  3410. Function float32_le_quiet( a: float32 ; b : float32 ): flag;
  3411. Var
  3412. aSign, bSign: flag;
  3413. aExp, bExp: int16;
  3414. Begin
  3415. if ( ( ( extractFloat32Exp( a ) = $FF ) AND (extractFloat32Frac( a )<>0) )
  3416. OR ( ( extractFloat32Exp( b ) = $FF ) AND (extractFloat32Frac( b )<>0) )
  3417. ) then
  3418. Begin
  3419. if ( (float32_is_signaling_nan( a )<>0) OR (float32_is_signaling_nan( b )<>0) ) then
  3420. Begin
  3421. float_raise( float_flag_invalid );
  3422. End;
  3423. float32_le_quiet := 0;
  3424. exit;
  3425. End;
  3426. aSign := extractFloat32Sign( a );
  3427. bSign := extractFloat32Sign( b );
  3428. if ( aSign <> bSign ) then
  3429. Begin
  3430. float32_le_quiet := aSign OR flag( bits32 ( ( a OR b ) shl 1 ) = 0 );
  3431. exit;
  3432. End;
  3433. float32_le_quiet := flag(flag( a = b ) OR flag( aSign xor flag( a < b ) ));
  3434. End;
  3435. {*
  3436. -------------------------------------------------------------------------------
  3437. Returns 1 if the single-precision floating-point value `a' is less than
  3438. the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  3439. exception. Otherwise, the comparison is performed according to the IEC/IEEE
  3440. Standard for Binary Floating-Point Arithmetic.
  3441. -------------------------------------------------------------------------------
  3442. *}
  3443. Function float32_lt_quiet( a: float32 ; b: float32 ): flag;
  3444. Var
  3445. aSign, bSign: flag;
  3446. Begin
  3447. if ( ( ( extractFloat32Exp( a ) = $FF ) AND (extractFloat32Frac( a )<>0) )
  3448. OR ( ( extractFloat32Exp( b ) = $FF ) AND (extractFloat32Frac( b )<>0) )
  3449. ) then
  3450. Begin
  3451. if ( (float32_is_signaling_nan( a )<>0) OR (float32_is_signaling_nan( b )<>0) ) then
  3452. Begin
  3453. float_raise( float_flag_invalid );
  3454. End;
  3455. float32_lt_quiet := 0;
  3456. exit;
  3457. End;
  3458. aSign := extractFloat32Sign( a );
  3459. bSign := extractFloat32Sign( b );
  3460. if ( aSign <> bSign ) then
  3461. Begin
  3462. float32_lt_quiet := aSign AND flag( bits32 ( ( a OR b ) shl 1 ) <> 0 );
  3463. exit;
  3464. End;
  3465. float32_lt_quiet := flag(flag( a <> b ) AND ( aSign xor flag( a < b ) ));
  3466. End;
  3467. {*
  3468. -------------------------------------------------------------------------------
  3469. Returns the result of converting the double-precision floating-point value
  3470. `a' to the 32-bit two's complement integer format. The conversion is
  3471. performed according to the IEC/IEEE Standard for Binary Floating-Point
  3472. Arithmetic---which means in particular that the conversion is rounded
  3473. according to the current rounding mode. If `a' is a NaN, the largest
  3474. positive integer is returned. Otherwise, if the conversion overflows, the
  3475. largest integer with the same sign as `a' is returned.
  3476. -------------------------------------------------------------------------------
  3477. *}
  3478. Function float64_to_int32(a: float64): int32;{$ifdef fpc} [public,Alias:'FLOAT64_TO_INT32'];compilerproc;{$endif}
  3479. var
  3480. aSign: flag;
  3481. aExp, shiftCount: int16;
  3482. aSig0, aSig1, absZ, aSigExtra: bits32;
  3483. z: int32;
  3484. roundingMode: int8;
  3485. label invalid;
  3486. Begin
  3487. aSig1 := extractFloat64Frac1( a );
  3488. aSig0 := extractFloat64Frac0( a );
  3489. aExp := extractFloat64Exp( a );
  3490. aSign := extractFloat64Sign( a );
  3491. shiftCount := aExp - $413;
  3492. if ( 0 <= shiftCount ) then
  3493. Begin
  3494. if ( $41E < aExp ) then
  3495. Begin
  3496. if ( ( aExp = $7FF ) AND (( aSig0 OR aSig1 )<>0) ) then
  3497. aSign := 0;
  3498. goto invalid;
  3499. End;
  3500. shortShift64Left(
  3501. aSig0 OR $00100000, aSig1, shiftCount, absZ, aSigExtra );
  3502. if ( $80000000 < absZ ) then
  3503. goto invalid;
  3504. End
  3505. else
  3506. Begin
  3507. aSig1 := flag( aSig1 <> 0 );
  3508. if ( aExp < $3FE ) then
  3509. Begin
  3510. aSigExtra := aExp OR aSig0 OR aSig1;
  3511. absZ := 0;
  3512. End
  3513. else
  3514. Begin
  3515. aSig0 := aSig0 OR $00100000;
  3516. aSigExtra := ( aSig0 shl ( shiftCount and 31 ) ) OR aSig1;
  3517. absZ := aSig0 shr ( - shiftCount );
  3518. End;
  3519. End;
  3520. roundingMode := float_rounding_mode;
  3521. if ( roundingMode = float_round_nearest_even ) then
  3522. Begin
  3523. if ( sbits32(aSigExtra) < 0 ) then
  3524. Begin
  3525. Inc(absZ);
  3526. if ( bits32 ( aSigExtra shl 1 ) = 0 ) then
  3527. absZ := absZ and not 1;
  3528. End;
  3529. if aSign <> 0 then
  3530. z := - absZ
  3531. else
  3532. z := absZ;
  3533. End
  3534. else
  3535. Begin
  3536. aSigExtra := bits32( aSigExtra <> 0 );
  3537. if ( aSign <> 0) then
  3538. Begin
  3539. z := - ( absZ
  3540. + ( int32( roundingMode = float_round_down ) and aSigExtra ) );
  3541. End
  3542. else
  3543. Begin
  3544. z := absZ + ( int32( roundingMode = float_round_up ) and aSigExtra );
  3545. End
  3546. End;
  3547. if ( (( aSign xor flag( z < 0 ) )<>0) AND (z<>0) ) then
  3548. Begin
  3549. invalid:
  3550. float_raise( float_flag_invalid );
  3551. if (aSign <> 0 ) then
  3552. float64_to_int32 := sbits32 ($80000000)
  3553. else
  3554. float64_to_int32 := $7FFFFFFF;
  3555. exit;
  3556. End;
  3557. if ( aSigExtra <> 0) then
  3558. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  3559. float64_to_int32 := z;
  3560. End;
  3561. {*
  3562. -------------------------------------------------------------------------------
  3563. Returns the result of converting the double-precision floating-point value
  3564. `a' to the 32-bit two's complement integer format. The conversion is
  3565. performed according to the IEC/IEEE Standard for Binary Floating-Point
  3566. Arithmetic, except that the conversion is always rounded toward zero.
  3567. If `a' is a NaN, the largest positive integer is returned. Otherwise, if
  3568. the conversion overflows, the largest integer with the same sign as `a' is
  3569. returned.
  3570. -------------------------------------------------------------------------------
  3571. *}
  3572. Function float64_to_int32_round_to_zero(a: float64 ): int32;
  3573. {$ifdef fpc} [public,Alias:'FLOAT64_TO_INT32_ROUND_TO_ZERO'];compilerproc;{$endif}
  3574. Var
  3575. aSign: flag;
  3576. aExp, shiftCount: int16;
  3577. aSig0, aSig1, absZ, aSigExtra: bits32;
  3578. z: int32;
  3579. label invalid;
  3580. Begin
  3581. aSig1 := extractFloat64Frac1( a );
  3582. aSig0 := extractFloat64Frac0( a );
  3583. aExp := extractFloat64Exp( a );
  3584. aSign := extractFloat64Sign( a );
  3585. shiftCount := aExp - $413;
  3586. if ( 0 <= shiftCount ) then
  3587. Begin
  3588. if ( $41E < aExp ) then
  3589. Begin
  3590. if ( ( aExp = $7FF ) AND (( aSig0 OR aSig1 )<>0) ) then
  3591. aSign := 0;
  3592. goto invalid;
  3593. End;
  3594. shortShift64Left(
  3595. aSig0 OR $00100000, aSig1, shiftCount, absZ, aSigExtra );
  3596. End
  3597. else
  3598. Begin
  3599. if ( aExp < $3FF ) then
  3600. Begin
  3601. if ( aExp OR aSig0 OR aSig1 )<>0 then
  3602. Begin
  3603. softfloat_exception_flags :=
  3604. softfloat_exception_flags or float_flag_inexact;
  3605. End;
  3606. float64_to_int32_round_to_zero := 0;
  3607. exit;
  3608. End;
  3609. aSig0 := aSig0 or $00100000;
  3610. aSigExtra := ( aSig0 shl ( shiftCount and 31 ) ) OR aSig1;
  3611. absZ := aSig0 shr ( - shiftCount );
  3612. End;
  3613. if aSign <> 0 then
  3614. z := - absZ
  3615. else
  3616. z := absZ;
  3617. if ( (( aSign xor flag( z < 0 )) <> 0) AND (z<>0) ) then
  3618. Begin
  3619. invalid:
  3620. float_raise( float_flag_invalid );
  3621. if (aSign <> 0) then
  3622. float64_to_int32_round_to_zero := sbits32 ($80000000)
  3623. else
  3624. float64_to_int32_round_to_zero := $7FFFFFFF;
  3625. exit;
  3626. End;
  3627. if ( aSigExtra <> 0) then
  3628. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  3629. float64_to_int32_round_to_zero := z;
  3630. End;
  3631. {*
  3632. -------------------------------------------------------------------------------
  3633. Returns the result of converting the double-precision floating-point value
  3634. `a' to the single-precision floating-point format. The conversion is
  3635. performed according to the IEC/IEEE Standard for Binary Floating-Point
  3636. Arithmetic.
  3637. -------------------------------------------------------------------------------
  3638. *}
  3639. Function float64_to_float32(a: float64 ): float32rec;compilerproc;
  3640. Var
  3641. aSign: flag;
  3642. aExp: int16;
  3643. aSig0, aSig1, zSig: bits32;
  3644. allZero: bits32;
  3645. tmp : CommonNanT;
  3646. Begin
  3647. aSig1 := extractFloat64Frac1( a );
  3648. aSig0 := extractFloat64Frac0( a );
  3649. aExp := extractFloat64Exp( a );
  3650. aSign := extractFloat64Sign( a );
  3651. if ( aExp = $7FF ) then
  3652. Begin
  3653. if ( aSig0 OR aSig1 ) <> 0 then
  3654. Begin
  3655. float64ToCommonNaN( a, tmp );
  3656. float64_to_float32.float32 := commonNaNToFloat32( tmp );
  3657. exit;
  3658. End;
  3659. float64_to_float32.float32 := packFloat32( aSign, $FF, 0 );
  3660. exit;
  3661. End;
  3662. shift64RightJamming( aSig0, aSig1, 22, allZero, zSig );
  3663. if ( aExp <> 0) then
  3664. zSig := zSig OR $40000000;
  3665. float64_to_float32.float32 := roundAndPackFloat32( aSign, aExp - $381, zSig );
  3666. End;
  3667. {*
  3668. -------------------------------------------------------------------------------
  3669. Rounds the double-precision floating-point value `a' to an integer,
  3670. and returns the result as a double-precision floating-point value. The
  3671. operation is performed according to the IEC/IEEE Standard for Binary
  3672. Floating-Point Arithmetic.
  3673. -------------------------------------------------------------------------------
  3674. *}
  3675. function float64_round_to_int(a: float64) : Float64;{$ifdef fpc} [public,Alias:'FLOAT64_ROUND_TO_INT'];compilerproc;{$endif}
  3676. Var
  3677. aSign: flag;
  3678. aExp: int16;
  3679. lastBitMask, roundBitsMask: bits32;
  3680. roundingMode: int8;
  3681. z: float64;
  3682. Begin
  3683. aExp := extractFloat64Exp( a );
  3684. if ( $413 <= aExp ) then
  3685. Begin
  3686. if ( $433 <= aExp ) then
  3687. Begin
  3688. if ( ( aExp = $7FF )
  3689. AND
  3690. (
  3691. ( extractFloat64Frac0( a ) OR extractFloat64Frac1( a )
  3692. ) <>0)
  3693. ) then
  3694. Begin
  3695. propagateFloat64NaN( a, a, result );
  3696. exit;
  3697. End;
  3698. result := a;
  3699. exit;
  3700. End;
  3701. lastBitMask := 1;
  3702. lastBitMask := ( lastBitMask shl ( $432 - aExp ) ) shl 1;
  3703. roundBitsMask := lastBitMask - 1;
  3704. z := a;
  3705. roundingMode := float_rounding_mode;
  3706. if ( roundingMode = float_round_nearest_even ) then
  3707. Begin
  3708. if ( lastBitMask <> 0) then
  3709. Begin
  3710. add64( z.high, z.low, 0, lastBitMask shr 1, z.high, z.low );
  3711. if ( ( z.low and roundBitsMask ) = 0 ) then
  3712. z.low := z.low and not lastBitMask;
  3713. End
  3714. else
  3715. Begin
  3716. if ( sbits32 (z.low) < 0 ) then
  3717. Begin
  3718. Inc(z.high);
  3719. if ( bits32 ( z.low shl 1 ) = 0 ) then
  3720. z.high := z.high and not 1;
  3721. End;
  3722. End;
  3723. End
  3724. else if ( roundingMode <> float_round_to_zero ) then
  3725. Begin
  3726. if ( extractFloat64Sign( z )
  3727. xor flag( roundingMode = float_round_up ) )<> 0 then
  3728. Begin
  3729. add64( z.high, z.low, 0, roundBitsMask, z.high, z.low );
  3730. End;
  3731. End;
  3732. z.low := z.low and not roundBitsMask;
  3733. End
  3734. else
  3735. Begin
  3736. if ( aExp <= $3FE ) then
  3737. Begin
  3738. if ( ( ( bits32 ( a.high shl 1 ) ) OR a.low ) = 0 ) then
  3739. Begin
  3740. result := a;
  3741. exit;
  3742. End;
  3743. softfloat_exception_flags := softfloat_exception_flags or
  3744. float_flag_inexact;
  3745. aSign := extractFloat64Sign( a );
  3746. case ( float_rounding_mode ) of
  3747. float_round_nearest_even:
  3748. Begin
  3749. if ( ( aExp = $3FE )
  3750. AND ( (extractFloat64Frac0( a ) OR extractFloat64Frac1( a ) )<>0)
  3751. ) then
  3752. Begin
  3753. packFloat64( aSign, $3FF, 0, 0, result );
  3754. exit;
  3755. End;
  3756. End;
  3757. float_round_down:
  3758. Begin
  3759. if aSign<>0 then
  3760. packFloat64( 1, $3FF, 0, 0, result )
  3761. else
  3762. packFloat64( 0, 0, 0, 0, result );
  3763. exit;
  3764. End;
  3765. float_round_up:
  3766. Begin
  3767. if aSign <> 0 then
  3768. packFloat64( 1, 0, 0, 0, result )
  3769. else
  3770. packFloat64( 0, $3FF, 0, 0, result );
  3771. exit;
  3772. End;
  3773. end;
  3774. packFloat64( aSign, 0, 0, 0, result );
  3775. exit;
  3776. End;
  3777. lastBitMask := 1;
  3778. lastBitMask := lastBitMask shl ($413 - aExp);
  3779. roundBitsMask := lastBitMask - 1;
  3780. z.low := 0;
  3781. z.high := a.high;
  3782. roundingMode := float_rounding_mode;
  3783. if ( roundingMode = float_round_nearest_even ) then
  3784. Begin
  3785. z.high := z.high + lastBitMask shr 1;
  3786. if ( ( ( z.high and roundBitsMask ) OR a.low ) = 0 ) then
  3787. Begin
  3788. z.high := z.high and not lastBitMask;
  3789. End;
  3790. End
  3791. else if ( roundingMode <> float_round_to_zero ) then
  3792. Begin
  3793. if ( extractFloat64Sign( z )
  3794. xor flag( roundingMode = float_round_up ) )<> 0 then
  3795. Begin
  3796. z.high := z.high or bits32( a.low <> 0 );
  3797. z.high := z.high + roundBitsMask;
  3798. End;
  3799. End;
  3800. z.high := z.high and not roundBitsMask;
  3801. End;
  3802. if ( ( z.low <> a.low ) OR ( z.high <> a.high ) ) then
  3803. Begin
  3804. softfloat_exception_flags :=
  3805. softfloat_exception_flags or float_flag_inexact;
  3806. End;
  3807. result := z;
  3808. End;
  3809. {*
  3810. -------------------------------------------------------------------------------
  3811. Returns the result of adding the absolute values of the double-precision
  3812. floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
  3813. before being returned. `zSign' is ignored if the result is a NaN.
  3814. The addition is performed according to the IEC/IEEE Standard for Binary
  3815. Floating-Point Arithmetic.
  3816. -------------------------------------------------------------------------------
  3817. *}
  3818. Procedure addFloat64Sigs( a:float64 ; b: float64 ; zSign:flag; Var out: float64 );
  3819. Var
  3820. aExp, bExp, zExp: int16;
  3821. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2: bits32;
  3822. expDiff: int16;
  3823. label shiftRight1;
  3824. label roundAndPack;
  3825. Begin
  3826. aSig1 := extractFloat64Frac1( a );
  3827. aSig0 := extractFloat64Frac0( a );
  3828. aExp := extractFloat64Exp( a );
  3829. bSig1 := extractFloat64Frac1( b );
  3830. bSig0 := extractFloat64Frac0( b );
  3831. bExp := extractFloat64Exp( b );
  3832. expDiff := aExp - bExp;
  3833. if ( 0 < expDiff ) then
  3834. Begin
  3835. if ( aExp = $7FF ) then
  3836. Begin
  3837. if ( aSig0 OR aSig1 ) <> 0 then
  3838. Begin
  3839. propagateFloat64NaN( a, b, out );
  3840. exit;
  3841. end;
  3842. out := a;
  3843. exit;
  3844. End;
  3845. if ( bExp = 0 ) then
  3846. Begin
  3847. Dec(expDiff);
  3848. End
  3849. else
  3850. Begin
  3851. bSig0 := bSig0 or $00100000;
  3852. End;
  3853. shift64ExtraRightJamming(
  3854. bSig0, bSig1, 0, expDiff, bSig0, bSig1, zSig2 );
  3855. zExp := aExp;
  3856. End
  3857. else if ( expDiff < 0 ) then
  3858. Begin
  3859. if ( bExp = $7FF ) then
  3860. Begin
  3861. if ( bSig0 OR bSig1 ) <> 0 then
  3862. Begin
  3863. propagateFloat64NaN( a, b, out );
  3864. exit;
  3865. End;
  3866. packFloat64( zSign, $7FF, 0, 0, out );
  3867. End;
  3868. if ( aExp = 0 ) then
  3869. Begin
  3870. Inc(expDiff);
  3871. End
  3872. else
  3873. Begin
  3874. aSig0 := aSig0 or $00100000;
  3875. End;
  3876. shift64ExtraRightJamming(
  3877. aSig0, aSig1, 0, - expDiff, aSig0, aSig1, zSig2 );
  3878. zExp := bExp;
  3879. End
  3880. else
  3881. Begin
  3882. if ( aExp = $7FF ) then
  3883. Begin
  3884. if ( aSig0 OR aSig1 OR bSig0 OR bSig1 ) <> 0 then
  3885. Begin
  3886. propagateFloat64NaN( a, b, out );
  3887. exit;
  3888. End;
  3889. out := a;
  3890. exit;
  3891. End;
  3892. add64( aSig0, aSig1, bSig0, bSig1, zSig0, zSig1 );
  3893. if ( aExp = 0 ) then
  3894. Begin
  3895. packFloat64( zSign, 0, zSig0, zSig1, out );
  3896. exit;
  3897. End;
  3898. zSig2 := 0;
  3899. zSig0 := zSig0 or $00200000;
  3900. zExp := aExp;
  3901. goto shiftRight1;
  3902. End;
  3903. aSig0 := aSig0 or $00100000;
  3904. add64( aSig0, aSig1, bSig0, bSig1, zSig0, zSig1 );
  3905. Dec(zExp);
  3906. if ( zSig0 < $00200000 ) then
  3907. goto roundAndPack;
  3908. Inc(zExp);
  3909. shiftRight1:
  3910. shift64ExtraRightJamming( zSig0, zSig1, zSig2, 1, zSig0, zSig1, zSig2 );
  3911. roundAndPack:
  3912. roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2, out );
  3913. End;
  3914. {*
  3915. -------------------------------------------------------------------------------
  3916. Returns the result of subtracting the absolute values of the double-
  3917. precision floating-point values `a' and `b'. If `zSign' is 1, the
  3918. difference is negated before being returned. `zSign' is ignored if the
  3919. result is a NaN. The subtraction is performed according to the IEC/IEEE
  3920. Standard for Binary Floating-Point Arithmetic.
  3921. -------------------------------------------------------------------------------
  3922. *}
  3923. Procedure subFloat64Sigs( a:float64; b: float64 ; zSign:flag; Var out: float64 );
  3924. Var
  3925. aExp, bExp, zExp: int16;
  3926. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1: bits32;
  3927. expDiff: int16;
  3928. z: float64;
  3929. label aExpBigger;
  3930. label bExpBigger;
  3931. label aBigger;
  3932. label bBigger;
  3933. label normalizeRoundAndPack;
  3934. Begin
  3935. aSig1 := extractFloat64Frac1( a );
  3936. aSig0 := extractFloat64Frac0( a );
  3937. aExp := extractFloat64Exp( a );
  3938. bSig1 := extractFloat64Frac1( b );
  3939. bSig0 := extractFloat64Frac0( b );
  3940. bExp := extractFloat64Exp( b );
  3941. expDiff := aExp - bExp;
  3942. shortShift64Left( aSig0, aSig1, 10, aSig0, aSig1 );
  3943. shortShift64Left( bSig0, bSig1, 10, bSig0, bSig1 );
  3944. if ( 0 < expDiff ) then goto aExpBigger;
  3945. if ( expDiff < 0 ) then goto bExpBigger;
  3946. if ( aExp = $7FF ) then
  3947. Begin
  3948. if ( aSig0 OR aSig1 OR bSig0 OR bSig1 ) <> 0 then
  3949. Begin
  3950. propagateFloat64NaN( a, b, out );
  3951. exit;
  3952. End;
  3953. float_raise( float_flag_invalid );
  3954. z.low := float64_default_nan_low;
  3955. z.high := float64_default_nan_high;
  3956. out := z;
  3957. exit;
  3958. End;
  3959. if ( aExp = 0 ) then
  3960. Begin
  3961. aExp := 1;
  3962. bExp := 1;
  3963. End;
  3964. if ( bSig0 < aSig0 ) then goto aBigger;
  3965. if ( aSig0 < bSig0 ) then goto bBigger;
  3966. if ( bSig1 < aSig1 ) then goto aBigger;
  3967. if ( aSig1 < bSig1 ) then goto bBigger;
  3968. packFloat64( flag(float_rounding_mode = float_round_down), 0, 0, 0 , out);
  3969. exit;
  3970. bExpBigger:
  3971. if ( bExp = $7FF ) then
  3972. Begin
  3973. if ( bSig0 OR bSig1 ) <> 0 then
  3974. Begin
  3975. propagateFloat64NaN( a, b, out );
  3976. exit;
  3977. End;
  3978. packFloat64( zSign xor 1, $7FF, 0, 0, out );
  3979. exit;
  3980. End;
  3981. if ( aExp = 0 ) then
  3982. Begin
  3983. Inc(expDiff);
  3984. End
  3985. else
  3986. Begin
  3987. aSig0 := aSig0 or $40000000;
  3988. End;
  3989. shift64RightJamming( aSig0, aSig1, - expDiff, aSig0, aSig1 );
  3990. bSig0 := bSig0 or $40000000;
  3991. bBigger:
  3992. sub64( bSig0, bSig1, aSig0, aSig1, zSig0, zSig1 );
  3993. zExp := bExp;
  3994. zSign := zSign xor 1;
  3995. goto normalizeRoundAndPack;
  3996. aExpBigger:
  3997. if ( aExp = $7FF ) then
  3998. Begin
  3999. if ( aSig0 OR aSig1 ) <> 0 then
  4000. Begin
  4001. propagateFloat64NaN( a, b, out );
  4002. exit;
  4003. End;
  4004. out := a;
  4005. exit;
  4006. End;
  4007. if ( bExp = 0 ) then
  4008. Begin
  4009. Dec(expDiff);
  4010. End
  4011. else
  4012. Begin
  4013. bSig0 := bSig0 or $40000000;
  4014. End;
  4015. shift64RightJamming( bSig0, bSig1, expDiff, bSig0, bSig1 );
  4016. aSig0 := aSig0 or $40000000;
  4017. aBigger:
  4018. sub64( aSig0, aSig1, bSig0, bSig1, zSig0, zSig1 );
  4019. zExp := aExp;
  4020. normalizeRoundAndPack:
  4021. Dec(zExp);
  4022. normalizeRoundAndPackFloat64( zSign, zExp - 10, zSig0, zSig1, out );
  4023. End;
  4024. {*
  4025. -------------------------------------------------------------------------------
  4026. Returns the result of adding the double-precision floating-point values `a'
  4027. and `b'. The operation is performed according to the IEC/IEEE Standard for
  4028. Binary Floating-Point Arithmetic.
  4029. -------------------------------------------------------------------------------
  4030. *}
  4031. Function float64_add( a: float64; b : float64) : Float64;
  4032. {$ifdef fpc}[public,Alias:'FLOAT64_ADD'];compilerproc;{$endif}
  4033. Var
  4034. aSign, bSign: flag;
  4035. Begin
  4036. aSign := extractFloat64Sign( a );
  4037. bSign := extractFloat64Sign( b );
  4038. if ( aSign = bSign ) then
  4039. Begin
  4040. addFloat64Sigs( a, b, aSign, result );
  4041. End
  4042. else
  4043. Begin
  4044. subFloat64Sigs( a, b, aSign, result );
  4045. End;
  4046. End;
  4047. {*
  4048. -------------------------------------------------------------------------------
  4049. Returns the result of subtracting the double-precision floating-point values
  4050. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  4051. for Binary Floating-Point Arithmetic.
  4052. -------------------------------------------------------------------------------
  4053. *}
  4054. Function float64_sub(a: float64; b : float64) : Float64;
  4055. {$ifdef fpc}[public,Alias:'FLOAT64_SUB'];compilerproc;{$endif}
  4056. Var
  4057. aSign, bSign: flag;
  4058. Begin
  4059. aSign := extractFloat64Sign( a );
  4060. bSign := extractFloat64Sign( b );
  4061. if ( aSign = bSign ) then
  4062. Begin
  4063. subFloat64Sigs( a, b, aSign, result );
  4064. End
  4065. else
  4066. Begin
  4067. addFloat64Sigs( a, b, aSign, result );
  4068. End;
  4069. End;
  4070. {*
  4071. -------------------------------------------------------------------------------
  4072. Returns the result of multiplying the double-precision floating-point values
  4073. `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  4074. for Binary Floating-Point Arithmetic.
  4075. -------------------------------------------------------------------------------
  4076. *}
  4077. Function float64_mul( a: float64; b:float64) : Float64;
  4078. {$ifdef fpc}[public,Alias:'FLOAT64_MUL'];compilerproc;{$endif}
  4079. Var
  4080. aSign, bSign, zSign: flag;
  4081. aExp, bExp, zExp: int16;
  4082. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2, zSig3: bits32;
  4083. z: float64;
  4084. label invalid;
  4085. Begin
  4086. aSig1 := extractFloat64Frac1( a );
  4087. aSig0 := extractFloat64Frac0( a );
  4088. aExp := extractFloat64Exp( a );
  4089. aSign := extractFloat64Sign( a );
  4090. bSig1 := extractFloat64Frac1( b );
  4091. bSig0 := extractFloat64Frac0( b );
  4092. bExp := extractFloat64Exp( b );
  4093. bSign := extractFloat64Sign( b );
  4094. zSign := aSign xor bSign;
  4095. if ( aExp = $7FF ) then
  4096. Begin
  4097. if ( (( aSig0 OR aSig1 ) <>0)
  4098. OR ( ( bExp = $7FF ) AND (( bSig0 OR bSig1 )<>0) ) ) then
  4099. Begin
  4100. propagateFloat64NaN( a, b, result );
  4101. exit;
  4102. End;
  4103. if ( ( bExp OR bSig0 OR bSig1 ) = 0 ) then goto invalid;
  4104. packFloat64( zSign, $7FF, 0, 0, result );
  4105. exit;
  4106. End;
  4107. if ( bExp = $7FF ) then
  4108. Begin
  4109. if ( bSig0 OR bSig1 )<> 0 then
  4110. Begin
  4111. propagateFloat64NaN( a, b, result );
  4112. exit;
  4113. End;
  4114. if ( ( aExp OR aSig0 OR aSig1 ) = 0 ) then
  4115. Begin
  4116. invalid:
  4117. float_raise( float_flag_invalid );
  4118. z.low := float64_default_nan_low;
  4119. z.high := float64_default_nan_high;
  4120. result := z;
  4121. exit;
  4122. End;
  4123. packFloat64( zSign, $7FF, 0, 0, result );
  4124. exit;
  4125. End;
  4126. if ( aExp = 0 ) then
  4127. Begin
  4128. if ( ( aSig0 OR aSig1 ) = 0 ) then
  4129. Begin
  4130. packFloat64( zSign, 0, 0, 0, result );
  4131. exit;
  4132. End;
  4133. normalizeFloat64Subnormal( aSig0, aSig1, aExp, aSig0, aSig1 );
  4134. End;
  4135. if ( bExp = 0 ) then
  4136. Begin
  4137. if ( ( bSig0 OR bSig1 ) = 0 ) then
  4138. Begin
  4139. packFloat64( zSign, 0, 0, 0, result );
  4140. exit;
  4141. End;
  4142. normalizeFloat64Subnormal( bSig0, bSig1, bExp, bSig0, bSig1 );
  4143. End;
  4144. zExp := aExp + bExp - $400;
  4145. aSig0 := aSig0 or $00100000;
  4146. shortShift64Left( bSig0, bSig1, 12, bSig0, bSig1 );
  4147. mul64To128( aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2, zSig3 );
  4148. add64( zSig0, zSig1, aSig0, aSig1, zSig0, zSig1 );
  4149. zSig2 := zSig2 or flag( zSig3 <> 0 );
  4150. if ( $00200000 <= zSig0 ) then
  4151. Begin
  4152. shift64ExtraRightJamming(
  4153. zSig0, zSig1, zSig2, 1, zSig0, zSig1, zSig2 );
  4154. Inc(zExp);
  4155. End;
  4156. roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2, result );
  4157. End;
  4158. {*
  4159. -------------------------------------------------------------------------------
  4160. Returns the result of dividing the double-precision floating-point value `a'
  4161. by the corresponding value `b'. The operation is performed according to the
  4162. IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4163. -------------------------------------------------------------------------------
  4164. *}
  4165. Function float64_div(a: float64; b : float64) : Float64;
  4166. {$ifdef fpc}[public,Alias:'FLOAT64_DIV'];compilerproc;{$endif}
  4167. Var
  4168. aSign, bSign, zSign: flag;
  4169. aExp, bExp, zExp: int16;
  4170. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2: bits32;
  4171. rem0, rem1, rem2, rem3, term0, term1, term2, term3: bits32;
  4172. z: float64;
  4173. label invalid;
  4174. Begin
  4175. aSig1 := extractFloat64Frac1( a );
  4176. aSig0 := extractFloat64Frac0( a );
  4177. aExp := extractFloat64Exp( a );
  4178. aSign := extractFloat64Sign( a );
  4179. bSig1 := extractFloat64Frac1( b );
  4180. bSig0 := extractFloat64Frac0( b );
  4181. bExp := extractFloat64Exp( b );
  4182. bSign := extractFloat64Sign( b );
  4183. zSign := aSign xor bSign;
  4184. if ( aExp = $7FF ) then
  4185. Begin
  4186. if ( aSig0 OR aSig1 )<> 0 then
  4187. Begin
  4188. propagateFloat64NaN( a, b, result );
  4189. exit;
  4190. end;
  4191. if ( bExp = $7FF ) then
  4192. Begin
  4193. if ( bSig0 OR bSig1 )<>0 then
  4194. Begin
  4195. propagateFloat64NaN( a, b, result );
  4196. exit;
  4197. End;
  4198. goto invalid;
  4199. End;
  4200. packFloat64( zSign, $7FF, 0, 0, result );
  4201. exit;
  4202. End;
  4203. if ( bExp = $7FF ) then
  4204. Begin
  4205. if ( bSig0 OR bSig1 )<> 0 then
  4206. Begin
  4207. propagateFloat64NaN( a, b, result );
  4208. exit;
  4209. End;
  4210. packFloat64( zSign, 0, 0, 0, result );
  4211. exit;
  4212. End;
  4213. if ( bExp = 0 ) then
  4214. Begin
  4215. if ( ( bSig0 OR bSig1 ) = 0 ) then
  4216. Begin
  4217. if ( ( aExp OR aSig0 OR aSig1 ) = 0 ) then
  4218. Begin
  4219. invalid:
  4220. float_raise( float_flag_invalid );
  4221. z.low := float64_default_nan_low;
  4222. z.high := float64_default_nan_high;
  4223. result := z;
  4224. exit;
  4225. End;
  4226. float_raise( float_flag_divbyzero );
  4227. packFloat64( zSign, $7FF, 0, 0, result );
  4228. exit;
  4229. End;
  4230. normalizeFloat64Subnormal( bSig0, bSig1, bExp, bSig0, bSig1 );
  4231. End;
  4232. if ( aExp = 0 ) then
  4233. Begin
  4234. if ( ( aSig0 OR aSig1 ) = 0 ) then
  4235. Begin
  4236. packFloat64( zSign, 0, 0, 0, result );
  4237. exit;
  4238. End;
  4239. normalizeFloat64Subnormal( aSig0, aSig1, aExp, aSig0, aSig1 );
  4240. End;
  4241. zExp := aExp - bExp + $3FD;
  4242. shortShift64Left( aSig0 OR $00100000, aSig1, 11, aSig0, aSig1 );
  4243. shortShift64Left( bSig0 OR $00100000, bSig1, 11, bSig0, bSig1 );
  4244. if ( le64( bSig0, bSig1, aSig0, aSig1 )<>0 ) then
  4245. Begin
  4246. shift64Right( aSig0, aSig1, 1, aSig0, aSig1 );
  4247. Inc(zExp);
  4248. End;
  4249. zSig0 := estimateDiv64To32( aSig0, aSig1, bSig0 );
  4250. mul64By32To96( bSig0, bSig1, zSig0, term0, term1, term2 );
  4251. sub96( aSig0, aSig1, 0, term0, term1, term2, rem0, rem1, rem2 );
  4252. while ( sbits32 (rem0) < 0 ) do
  4253. Begin
  4254. Dec(zSig0);
  4255. add96( rem0, rem1, rem2, 0, bSig0, bSig1, rem0, rem1, rem2 );
  4256. End;
  4257. zSig1 := estimateDiv64To32( rem1, rem2, bSig0 );
  4258. if ( ( zSig1 and $3FF ) <= 4 ) then
  4259. Begin
  4260. mul64By32To96( bSig0, bSig1, zSig1, term1, term2, term3 );
  4261. sub96( rem1, rem2, 0, term1, term2, term3, rem1, rem2, rem3 );
  4262. while ( sbits32 (rem1) < 0 ) do
  4263. Begin
  4264. Dec(zSig1);
  4265. add96( rem1, rem2, rem3, 0, bSig0, bSig1, rem1, rem2, rem3 );
  4266. End;
  4267. zSig1 := zSig1 or flag( ( rem1 OR rem2 OR rem3 ) <> 0 );
  4268. End;
  4269. shift64ExtraRightJamming( zSig0, zSig1, 0, 11, zSig0, zSig1, zSig2 );
  4270. roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2, result );
  4271. End;
  4272. {*
  4273. -------------------------------------------------------------------------------
  4274. Returns the remainder of the double-precision floating-point value `a'
  4275. with respect to the corresponding value `b'. The operation is performed
  4276. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4277. -------------------------------------------------------------------------------
  4278. *}
  4279. Function float64_rem(a: float64; b : float64) : float64;
  4280. {$ifdef fpc}[public,Alias:'FLOAT64_REM'];compilerproc;{$endif}
  4281. Var
  4282. aSign, bSign, zSign: flag;
  4283. aExp, bExp, expDiff: int16;
  4284. aSig0, aSig1, bSig0, bSig1, q, term0, term1, term2: bits32;
  4285. allZero, alternateASig0, alternateASig1, sigMean1: bits32;
  4286. sigMean0: sbits32;
  4287. z: float64;
  4288. label invalid;
  4289. Begin
  4290. aSig1 := extractFloat64Frac1( a );
  4291. aSig0 := extractFloat64Frac0( a );
  4292. aExp := extractFloat64Exp( a );
  4293. aSign := extractFloat64Sign( a );
  4294. bSig1 := extractFloat64Frac1( b );
  4295. bSig0 := extractFloat64Frac0( b );
  4296. bExp := extractFloat64Exp( b );
  4297. bSign := extractFloat64Sign( b );
  4298. if ( aExp = $7FF ) then
  4299. Begin
  4300. if ((( aSig0 OR aSig1 )<>0)
  4301. OR ( ( bExp = $7FF ) AND (( bSig0 OR bSig1 )<>0) ) ) then
  4302. Begin
  4303. propagateFloat64NaN( a, b, result );
  4304. exit;
  4305. End;
  4306. goto invalid;
  4307. End;
  4308. if ( bExp = $7FF ) then
  4309. Begin
  4310. if ( bSig0 OR bSig1 ) <> 0 then
  4311. Begin
  4312. propagateFloat64NaN( a, b, result );
  4313. exit;
  4314. End;
  4315. result := a;
  4316. exit;
  4317. End;
  4318. if ( bExp = 0 ) then
  4319. Begin
  4320. if ( ( bSig0 OR bSig1 ) = 0 ) then
  4321. Begin
  4322. invalid:
  4323. float_raise( float_flag_invalid );
  4324. z.low := float64_default_nan_low;
  4325. z.high := float64_default_nan_high;
  4326. result := z;
  4327. exit;
  4328. End;
  4329. normalizeFloat64Subnormal( bSig0, bSig1, bExp, bSig0, bSig1 );
  4330. End;
  4331. if ( aExp = 0 ) then
  4332. Begin
  4333. if ( ( aSig0 OR aSig1 ) = 0 ) then
  4334. Begin
  4335. result := a;
  4336. exit;
  4337. End;
  4338. normalizeFloat64Subnormal( aSig0, aSig1, aExp, aSig0, aSig1 );
  4339. End;
  4340. expDiff := aExp - bExp;
  4341. if ( expDiff < -1 ) then
  4342. Begin
  4343. result := a;
  4344. exit;
  4345. End;
  4346. shortShift64Left(
  4347. aSig0 OR $00100000, aSig1, 11 - flag( expDiff < 0 ), aSig0, aSig1 );
  4348. shortShift64Left( bSig0 OR $00100000, bSig1, 11, bSig0, bSig1 );
  4349. q := le64( bSig0, bSig1, aSig0, aSig1 );
  4350. if ( q )<>0 then
  4351. sub64( aSig0, aSig1, bSig0, bSig1, aSig0, aSig1 );
  4352. expDiff := expDiff - 32;
  4353. while ( 0 < expDiff ) do
  4354. Begin
  4355. q := estimateDiv64To32( aSig0, aSig1, bSig0 );
  4356. if 4 < q then
  4357. q:= q - 4
  4358. else
  4359. q := 0;
  4360. mul64By32To96( bSig0, bSig1, q, term0, term1, term2 );
  4361. shortShift96Left( term0, term1, term2, 29, term1, term2, allZero );
  4362. shortShift64Left( aSig0, aSig1, 29, aSig0, allZero );
  4363. sub64( aSig0, 0, term1, term2, aSig0, aSig1 );
  4364. expDiff := expDiff - 29;
  4365. End;
  4366. if ( -32 < expDiff ) then
  4367. Begin
  4368. q := estimateDiv64To32( aSig0, aSig1, bSig0 );
  4369. if 4 < q then
  4370. q := q - 4
  4371. else
  4372. q := 0;
  4373. q := q shr (- expDiff);
  4374. shift64Right( bSig0, bSig1, 8, bSig0, bSig1 );
  4375. expDiff := expDiff + 24;
  4376. if ( expDiff < 0 ) then
  4377. Begin
  4378. shift64Right( aSig0, aSig1, - expDiff, aSig0, aSig1 );
  4379. End
  4380. else
  4381. Begin
  4382. shortShift64Left( aSig0, aSig1, expDiff, aSig0, aSig1 );
  4383. End;
  4384. mul64By32To96( bSig0, bSig1, q, term0, term1, term2 );
  4385. sub64( aSig0, aSig1, term1, term2, aSig0, aSig1 );
  4386. End
  4387. else
  4388. Begin
  4389. shift64Right( aSig0, aSig1, 8, aSig0, aSig1 );
  4390. shift64Right( bSig0, bSig1, 8, bSig0, bSig1 );
  4391. End;
  4392. Repeat
  4393. alternateASig0 := aSig0;
  4394. alternateASig1 := aSig1;
  4395. Inc(q);
  4396. sub64( aSig0, aSig1, bSig0, bSig1, aSig0, aSig1 );
  4397. Until not ( 0 <= sbits32 (aSig0) );
  4398. add64(
  4399. aSig0, aSig1, alternateASig0, alternateASig1, bits32(sigMean0), sigMean1 );
  4400. if ( ( sigMean0 < 0 )
  4401. OR ( ( ( sigMean0 OR sigMean1 ) = 0 ) AND (( q AND 1 )<>0) ) ) then
  4402. Begin
  4403. aSig0 := alternateASig0;
  4404. aSig1 := alternateASig1;
  4405. End;
  4406. zSign := flag( sbits32 (aSig0) < 0 );
  4407. if ( zSign <> 0 ) then
  4408. sub64( 0, 0, aSig0, aSig1, aSig0, aSig1 );
  4409. normalizeRoundAndPackFloat64( aSign xor zSign, bExp - 4, aSig0, aSig1, result );
  4410. End;
  4411. {*
  4412. -------------------------------------------------------------------------------
  4413. Returns the square root of the double-precision floating-point value `a'.
  4414. The operation is performed according to the IEC/IEEE Standard for Binary
  4415. Floating-Point Arithmetic.
  4416. -------------------------------------------------------------------------------
  4417. *}
  4418. Procedure float64_sqrt( a: float64; var out: float64 );
  4419. {$ifdef fpc}[public,Alias:'FLOAT64_SQRT'];compilerproc;{$endif}
  4420. Var
  4421. aSign: flag;
  4422. aExp, zExp: int16;
  4423. aSig0, aSig1, zSig0, zSig1, zSig2, doubleZSig0: bits32;
  4424. rem0, rem1, rem2, rem3, term0, term1, term2, term3: bits32;
  4425. z: float64;
  4426. label invalid;
  4427. Begin
  4428. aSig1 := extractFloat64Frac1( a );
  4429. aSig0 := extractFloat64Frac0( a );
  4430. aExp := extractFloat64Exp( a );
  4431. aSign := extractFloat64Sign( a );
  4432. if ( aExp = $7FF ) then
  4433. Begin
  4434. if ( aSig0 OR aSig1 ) <> 0 then
  4435. Begin
  4436. propagateFloat64NaN( a, a, out );
  4437. exit;
  4438. End;
  4439. if ( aSign = 0) then
  4440. Begin
  4441. out := a;
  4442. exit;
  4443. End;
  4444. goto invalid;
  4445. End;
  4446. if ( aSign <> 0 ) then
  4447. Begin
  4448. if ( ( aExp OR aSig0 OR aSig1 ) = 0 ) then
  4449. Begin
  4450. out := a;
  4451. exit;
  4452. End;
  4453. invalid:
  4454. float_raise( float_flag_invalid );
  4455. z.low := float64_default_nan_low;
  4456. z.high := float64_default_nan_high;
  4457. out := z;
  4458. exit;
  4459. End;
  4460. if ( aExp = 0 ) then
  4461. Begin
  4462. if ( ( aSig0 OR aSig1 ) = 0 ) then
  4463. Begin
  4464. packFloat64( 0, 0, 0, 0, out );
  4465. exit;
  4466. End;
  4467. normalizeFloat64Subnormal( aSig0, aSig1, aExp, aSig0, aSig1 );
  4468. End;
  4469. zExp := ( ( aExp - $3FF ) shr 1 ) + $3FE;
  4470. aSig0 := aSig0 or $00100000;
  4471. shortShift64Left( aSig0, aSig1, 11, term0, term1 );
  4472. zSig0 := ( estimateSqrt32( aExp, term0 ) shr 1 ) + 1;
  4473. if ( zSig0 = 0 ) then
  4474. zSig0 := $7FFFFFFF;
  4475. doubleZSig0 := zSig0 + zSig0;
  4476. shortShift64Left( aSig0, aSig1, 9 - ( aExp and 1 ), aSig0, aSig1 );
  4477. mul32To64( zSig0, zSig0, term0, term1 );
  4478. sub64( aSig0, aSig1, term0, term1, rem0, rem1 );
  4479. while ( sbits32 (rem0) < 0 ) do
  4480. Begin
  4481. Dec(zSig0);
  4482. doubleZSig0 := doubleZSig0 - 2;
  4483. add64( rem0, rem1, 0, doubleZSig0 OR 1, rem0, rem1 );
  4484. End;
  4485. zSig1 := estimateDiv64To32( rem1, 0, doubleZSig0 );
  4486. if ( ( zSig1 and $1FF ) <= 5 ) then
  4487. Begin
  4488. if ( zSig1 = 0 ) then
  4489. zSig1 := 1;
  4490. mul32To64( doubleZSig0, zSig1, term1, term2 );
  4491. sub64( rem1, 0, term1, term2, rem1, rem2 );
  4492. mul32To64( zSig1, zSig1, term2, term3 );
  4493. sub96( rem1, rem2, 0, 0, term2, term3, rem1, rem2, rem3 );
  4494. while ( sbits32 (rem1) < 0 ) do
  4495. Begin
  4496. Dec(zSig1);
  4497. shortShift64Left( 0, zSig1, 1, term2, term3 );
  4498. term3 := term3 or 1;
  4499. term2 := term2 or doubleZSig0;
  4500. add96( rem1, rem2, rem3, 0, term2, term3, rem1, rem2, rem3 );
  4501. End;
  4502. zSig1 := zSig1 or bits32( ( rem1 OR rem2 OR rem3 ) <> 0 );
  4503. End;
  4504. shift64ExtraRightJamming( zSig0, zSig1, 0, 10, zSig0, zSig1, zSig2 );
  4505. roundAndPackFloat64( 0, zExp, zSig0, zSig1, zSig2, out );
  4506. End;
  4507. {*
  4508. -------------------------------------------------------------------------------
  4509. Returns 1 if the double-precision floating-point value `a' is equal to
  4510. the corresponding value `b', and 0 otherwise. The comparison is performed
  4511. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4512. -------------------------------------------------------------------------------
  4513. *}
  4514. Function float64_eq(a: float64; b: float64): flag;
  4515. {$ifdef fpc}[public,Alias:'FLOAT64_EQ'];compilerproc;{$endif}
  4516. Begin
  4517. if
  4518. (
  4519. ( extractFloat64Exp( a ) = $7FF )
  4520. AND
  4521. (
  4522. (extractFloat64Frac0( a ) OR extractFloat64Frac1( a )) <>0
  4523. )
  4524. )
  4525. OR (
  4526. ( extractFloat64Exp( b ) = $7FF )
  4527. AND (
  4528. (extractFloat64Frac0( b ) OR (extractFloat64Frac1( b )) <> 0
  4529. )
  4530. )
  4531. ) then
  4532. Begin
  4533. if ( (float64_is_signaling_nan( a )<>0) OR (float64_is_signaling_nan( b )<>0) ) then
  4534. float_raise( float_flag_invalid );
  4535. float64_eq := 0;
  4536. exit;
  4537. End;
  4538. float64_eq := flag(
  4539. ( a.low = b.low )
  4540. AND ( ( a.high = b.high )
  4541. OR ( ( a.low = 0 )
  4542. AND ( bits32 ( ( a.high OR b.high ) shl 1 ) = 0 ) )
  4543. ));
  4544. End;
  4545. {*
  4546. -------------------------------------------------------------------------------
  4547. Returns 1 if the double-precision floating-point value `a' is less than
  4548. or equal to the corresponding value `b', and 0 otherwise. The comparison
  4549. is performed according to the IEC/IEEE Standard for Binary Floating-Point
  4550. Arithmetic.
  4551. -------------------------------------------------------------------------------
  4552. *}
  4553. Function float64_le(a: float64;b: float64): flag;
  4554. {$ifdef fpc}[public,Alias:'FLOAT64_LE'];compilerproc;{$endif}
  4555. Var
  4556. aSign, bSign: flag;
  4557. Begin
  4558. if
  4559. (
  4560. ( extractFloat64Exp( a ) = $7FF )
  4561. AND
  4562. (
  4563. (extractFloat64Frac0( a ) OR extractFloat64Frac1( a )) <>0
  4564. )
  4565. )
  4566. OR (
  4567. ( extractFloat64Exp( b ) = $7FF )
  4568. AND (
  4569. (extractFloat64Frac0( b ) OR (extractFloat64Frac1( b )) <> 0
  4570. )
  4571. )
  4572. ) then
  4573. Begin
  4574. float_raise( float_flag_invalid );
  4575. float64_le := 0;
  4576. exit;
  4577. End;
  4578. aSign := extractFloat64Sign( a );
  4579. bSign := extractFloat64Sign( b );
  4580. if ( aSign <> bSign ) then
  4581. Begin
  4582. float64_le := flag(
  4583. (aSign <> 0)
  4584. OR ( ( ( bits32 ( ( a.high OR b.high ) shl 1 ) ) OR a.low OR b.low )
  4585. = 0 ));
  4586. exit;
  4587. End;
  4588. if aSign <> 0 then
  4589. float64_le := le64( b.high, b.low, a.high, a.low )
  4590. else
  4591. float64_le := le64( a.high, a.low, b.high, b.low );
  4592. End;
  4593. {*
  4594. -------------------------------------------------------------------------------
  4595. Returns 1 if the double-precision floating-point value `a' is less than
  4596. the corresponding value `b', and 0 otherwise. The comparison is performed
  4597. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4598. -------------------------------------------------------------------------------
  4599. *}
  4600. Function float64_lt(a: float64;b: float64): flag;
  4601. {$ifdef fpc}[public,Alias:'FLOAT64_LT'];compilerproc;{$endif}
  4602. Var
  4603. aSign, bSign: flag;
  4604. Begin
  4605. if
  4606. (
  4607. ( extractFloat64Exp( a ) = $7FF )
  4608. AND
  4609. (
  4610. (extractFloat64Frac0( a ) OR extractFloat64Frac1( a )) <>0
  4611. )
  4612. )
  4613. OR (
  4614. ( extractFloat64Exp( b ) = $7FF )
  4615. AND (
  4616. (extractFloat64Frac0( b ) OR (extractFloat64Frac1( b )) <> 0
  4617. )
  4618. )
  4619. ) then
  4620. Begin
  4621. float_raise( float_flag_invalid );
  4622. float64_lt := 0;
  4623. exit;
  4624. End;
  4625. aSign := extractFloat64Sign( a );
  4626. bSign := extractFloat64Sign( b );
  4627. if ( aSign <> bSign ) then
  4628. Begin
  4629. float64_lt := flag(
  4630. (aSign <> 0)
  4631. AND ( ( ( bits32 ( ( a.high OR b.high ) shl 1 ) ) OR a.low OR b.low )
  4632. <> 0 ));
  4633. exit;
  4634. End;
  4635. if aSign <> 0 then
  4636. float64_lt := lt64( b.high, b.low, a.high, a.low )
  4637. else
  4638. float64_lt := lt64( a.high, a.low, b.high, b.low );
  4639. End;
  4640. {*
  4641. -------------------------------------------------------------------------------
  4642. Returns 1 if the double-precision floating-point value `a' is equal to
  4643. the corresponding value `b', and 0 otherwise. The invalid exception is
  4644. raised if either operand is a NaN. Otherwise, the comparison is performed
  4645. according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4646. -------------------------------------------------------------------------------
  4647. *}
  4648. Function float64_eq_signaling( a: float64; b: float64): flag;
  4649. Begin
  4650. if
  4651. (
  4652. ( extractFloat64Exp( a ) = $7FF )
  4653. AND
  4654. (
  4655. (extractFloat64Frac0( a ) OR extractFloat64Frac1( a )) <>0
  4656. )
  4657. )
  4658. OR (
  4659. ( extractFloat64Exp( b ) = $7FF )
  4660. AND (
  4661. (extractFloat64Frac0( b ) OR (extractFloat64Frac1( b )) <> 0
  4662. )
  4663. )
  4664. ) then
  4665. Begin
  4666. float_raise( float_flag_invalid );
  4667. float64_eq_signaling := 0;
  4668. exit;
  4669. End;
  4670. float64_eq_signaling := flag(
  4671. ( a.low = b.low )
  4672. AND ( ( a.high = b.high )
  4673. OR ( ( a.low = 0 )
  4674. AND ( bits32 ( ( a.high OR b.high ) shl 1 ) = 0 ) )
  4675. ));
  4676. End;
  4677. {*
  4678. -------------------------------------------------------------------------------
  4679. Returns 1 if the double-precision floating-point value `a' is less than or
  4680. equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
  4681. cause an exception. Otherwise, the comparison is performed according to the
  4682. IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4683. -------------------------------------------------------------------------------
  4684. *}
  4685. Function float64_le_quiet(a: float64 ; b: float64 ): flag;
  4686. Var
  4687. aSign, bSign : flag;
  4688. Begin
  4689. if
  4690. (
  4691. ( extractFloat64Exp( a ) = $7FF )
  4692. AND
  4693. (
  4694. (extractFloat64Frac0( a ) OR extractFloat64Frac1( a )) <>0
  4695. )
  4696. )
  4697. OR (
  4698. ( extractFloat64Exp( b ) = $7FF )
  4699. AND (
  4700. (extractFloat64Frac0( b ) OR (extractFloat64Frac1( b )) <> 0
  4701. )
  4702. )
  4703. ) then
  4704. Begin
  4705. if ( (float64_is_signaling_nan( a )<>0) OR (float64_is_signaling_nan( b )<>0) ) then
  4706. float_raise( float_flag_invalid );
  4707. float64_le_quiet := 0;
  4708. exit;
  4709. End;
  4710. aSign := extractFloat64Sign( a );
  4711. bSign := extractFloat64Sign( b );
  4712. if ( aSign <> bSign ) then
  4713. Begin
  4714. float64_le_quiet := flag
  4715. ((aSign <> 0)
  4716. OR ( ( ( bits32 ( ( a.high OR b.high ) shl 1 ) ) OR a.low OR b.low )
  4717. = 0 ));
  4718. exit;
  4719. End;
  4720. if aSign <> 0 then
  4721. float64_le_quiet := le64( b.high, b.low, a.high, a.low )
  4722. else
  4723. float64_le_quiet := le64( a.high, a.low, b.high, b.low );
  4724. End;
  4725. {*
  4726. -------------------------------------------------------------------------------
  4727. Returns 1 if the double-precision floating-point value `a' is less than
  4728. the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  4729. exception. Otherwise, the comparison is performed according to the IEC/IEEE
  4730. Standard for Binary Floating-Point Arithmetic.
  4731. -------------------------------------------------------------------------------
  4732. *}
  4733. Function float64_lt_quiet(a: float64; b: float64 ): Flag;
  4734. Var
  4735. aSign, bSign: flag;
  4736. Begin
  4737. if
  4738. (
  4739. ( extractFloat64Exp( a ) = $7FF )
  4740. AND
  4741. (
  4742. (extractFloat64Frac0( a ) OR extractFloat64Frac1( a )) <>0
  4743. )
  4744. )
  4745. OR (
  4746. ( extractFloat64Exp( b ) = $7FF )
  4747. AND (
  4748. (extractFloat64Frac0( b ) OR (extractFloat64Frac1( b )) <> 0
  4749. )
  4750. )
  4751. ) then
  4752. Begin
  4753. if ( (float64_is_signaling_nan( a )<>0) OR (float64_is_signaling_nan( b )<>0) ) then
  4754. float_raise( float_flag_invalid );
  4755. float64_lt_quiet := 0;
  4756. exit;
  4757. End;
  4758. aSign := extractFloat64Sign( a );
  4759. bSign := extractFloat64Sign( b );
  4760. if ( aSign <> bSign ) then
  4761. Begin
  4762. float64_lt_quiet := flag(
  4763. (aSign<>0)
  4764. AND ( ( ( bits32 ( ( a.high OR b.high ) shl 1 ) ) OR a.low OR b.low )
  4765. <> 0 ));
  4766. exit;
  4767. End;
  4768. If aSign <> 0 then
  4769. float64_lt_quiet := lt64( b.high, b.low, a.high, a.low )
  4770. else
  4771. float64_lt_quiet := lt64( a.high, a.low, b.high, b.low );
  4772. End;
  4773. {*----------------------------------------------------------------------------
  4774. | Returns the result of converting the 64-bit two's complement integer `a'
  4775. | to the single-precision floating-point format. The conversion is performed
  4776. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4777. *----------------------------------------------------------------------------*}
  4778. function int64_to_float32( a: int64 ): float32rec; compilerproc;
  4779. var
  4780. zSign : flag;
  4781. absA : uint64;
  4782. shiftCount: int8;
  4783. zSig : bits32;
  4784. intval : int64rec;
  4785. Begin
  4786. if ( a = 0 ) then
  4787. begin
  4788. int64_to_float32.float32 := 0;
  4789. exit;
  4790. end;
  4791. if a < 0 then
  4792. zSign := flag(TRUE)
  4793. else
  4794. zSign := flag(FALSE);
  4795. if zSign<>0 then
  4796. absA := -a
  4797. else
  4798. absA := a;
  4799. shiftCount := countLeadingZeros64( absA ) - 40;
  4800. if ( 0 <= shiftCount ) then
  4801. begin
  4802. int64_to_float32.float32:= packFloat32( zSign, $95 - shiftCount, absA shl shiftCount );
  4803. end
  4804. else
  4805. begin
  4806. shiftCount := shiftCount + 7;
  4807. if ( shiftCount < 0 ) then
  4808. begin
  4809. intval.low := int64rec(AbsA).low;
  4810. intval.high := int64rec(AbsA).high;
  4811. shift64RightJamming( intval.low, intval.high, - shiftCount,
  4812. intval.low, intval.high);
  4813. int64rec(absA).low := intval.low;
  4814. int64rec(absA).high := intval.high;
  4815. end
  4816. else
  4817. absA := absA shl shiftCount;
  4818. int64_to_float32.float32:=roundAndPackFloat32( zSign, $9C - shiftCount, absA );
  4819. end;
  4820. End;
  4821. {*----------------------------------------------------------------------------
  4822. | Returns the result of converting the 64-bit two's complement integer `a'
  4823. | to the double-precision floating-point format. The conversion is performed
  4824. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  4825. *----------------------------------------------------------------------------*}
  4826. function int64_to_float64( a: int64 ): float64;
  4827. {$ifdef fpc}[public,Alias:'INT64_TO_FLOAT64'];compilerproc;{$endif}
  4828. var
  4829. zSign : flag;
  4830. float_result : float64;
  4831. intval : int64rec;
  4832. AbsA : bits64;
  4833. shiftcount : int8;
  4834. zSig0, zSig1 : bits32;
  4835. Begin
  4836. if ( a = 0 ) then
  4837. Begin
  4838. packFloat64( 0, 0, 0, 0, float_result );
  4839. exit;
  4840. end;
  4841. zSign := flag( a < 0 );
  4842. if ZSign<>0 then
  4843. AbsA := -a
  4844. else
  4845. AbsA := a;
  4846. shiftCount := countLeadingZeros64( absA ) - 11;
  4847. if ( 0 <= shiftCount ) then
  4848. Begin
  4849. absA := absA shl shiftcount;
  4850. zSig0:=int64rec(absA).high;
  4851. zSig1:=int64rec(absA).low;
  4852. End
  4853. else
  4854. Begin
  4855. shift64Right( absA, 0, - shiftCount, zSig0, zSig1 );
  4856. End;
  4857. packFloat64( zSign, $432 - shiftCount, zSig0, zSig1, float_result );
  4858. int64_to_float64:= float_result;
  4859. End;
  4860. {*----------------------------------------------------------------------------
  4861. | Returns 1 if the 128-bit value formed by concatenating `a0' and `a1'
  4862. | is equal to the 128-bit value formed by concatenating `b0' and `b1'.
  4863. | Otherwise, returns 0.
  4864. *----------------------------------------------------------------------------*}
  4865. function eq128( a0: bits64; a1: bits64; b0: bits64; b1 : bits64): flag;inline;
  4866. begin
  4867. result := ord(( a0 = b0 ) and ( a1 = b1 ));
  4868. end;
  4869. {*----------------------------------------------------------------------------
  4870. | Adds the 128-bit value formed by concatenating `a0' and `a1' to the 128-bit
  4871. | value formed by concatenating `b0' and `b1'. Addition is modulo 2^128, so
  4872. | any carry out is lost. The result is broken into two 64-bit pieces which
  4873. | are stored at the locations pointed to by `z0Ptr' and `z1Ptr'.
  4874. *----------------------------------------------------------------------------*}
  4875. procedure add128(a0: bits64; a1: bits64; b0: bits64; b1: bits64; var z0Ptr: bits64; var z1Ptr : bits64);inline;
  4876. var
  4877. z1: bits64;
  4878. begin
  4879. z1 := a1 + b1;
  4880. z1Ptr := z1;
  4881. z0Ptr := a0 + b0 + ord( z1 < a1 );
  4882. end;
  4883. {*----------------------------------------------------------------------------
  4884. | Shifts the 192-bit value formed by concatenating `a0', `a1', and `a2' right
  4885. | by 64 _plus_ the number of bits given in `count'. The shifted result is
  4886. | at most 128 nonzero bits; these are broken into two 64-bit pieces which are
  4887. | stored at the locations pointed to by `z0Ptr' and `z1Ptr'. The bits shifted
  4888. | off form a third 64-bit result as follows: The _last_ bit shifted off is
  4889. | the most-significant bit of the extra result, and the other 63 bits of the
  4890. | extra result are all zero if and only if _all_but_the_last_ bits shifted off
  4891. | were all zero. This extra result is stored in the location pointed to by
  4892. | `z2Ptr'. The value of `count' can be arbitrarily large.
  4893. | (This routine makes more sense if `a0', `a1', and `a2' are considered
  4894. | to form a fixed-point value with binary point between `a1' and `a2'. This
  4895. | fixed-point value is shifted right by the number of bits given in `count',
  4896. | and the integer part of the result is returned at the locations pointed to
  4897. | by `z0Ptr' and `z1Ptr'. The fractional part of the result may be slightly
  4898. | corrupted as described above, and is returned at the location pointed to by
  4899. | `z2Ptr'.)
  4900. *----------------------------------------------------------------------------*}
  4901. procedure shift128ExtraRightJamming(
  4902. a0: bits64;
  4903. a1: bits64;
  4904. a2: bits64;
  4905. count: int16;
  4906. var z0Ptr: bits64;
  4907. var z1Ptr: bits64;
  4908. var z2Ptr: bits64);
  4909. var
  4910. z0, z1, z2: bits64;
  4911. negCount: int8;
  4912. begin
  4913. negCount := ( - count ) and 63;
  4914. if ( count = 0 ) then
  4915. begin
  4916. z2 := a2;
  4917. z1 := a1;
  4918. z0 := a0;
  4919. end
  4920. else begin
  4921. if ( count < 64 ) then
  4922. begin
  4923. z2 := a1 shr negCount;
  4924. z1 := ( a0 shl negCount ) or ( a1 shr count );
  4925. z0 := a0 shr count;
  4926. end
  4927. else begin
  4928. if ( count = 64 ) then
  4929. begin
  4930. z2 := a1;
  4931. z1 := a0;
  4932. end
  4933. else begin
  4934. a2 := a2 or a1;
  4935. if ( count < 128 ) then
  4936. begin
  4937. z2 := a0 shl negCount;
  4938. z1 := a0 shr ( count and 63 );
  4939. end
  4940. else begin
  4941. if ( count = 128 ) then
  4942. z2 := a0
  4943. else
  4944. z2 := ord( a0 <> 0 );
  4945. z1 := 0;
  4946. end;
  4947. end;
  4948. z0 := 0;
  4949. end;
  4950. z2 := z2 or ord( a2 <> 0 );
  4951. end;
  4952. z2Ptr := z2;
  4953. z1Ptr := z1;
  4954. z0Ptr := z0;
  4955. end;
  4956. {*----------------------------------------------------------------------------
  4957. | Shifts the 128-bit value formed by concatenating `a0' and `a1' right by 64
  4958. | _plus_ the number of bits given in `count'. The shifted result is at most
  4959. | 64 nonzero bits; this is stored at the location pointed to by `z0Ptr'. The
  4960. | bits shifted off form a second 64-bit result as follows: The _last_ bit
  4961. | shifted off is the most-significant bit of the extra result, and the other
  4962. | 63 bits of the extra result are all zero if and only if _all_but_the_last_
  4963. | bits shifted off were all zero. This extra result is stored in the location
  4964. | pointed to by `z1Ptr'. The value of `count' can be arbitrarily large.
  4965. | (This routine makes more sense if `a0' and `a1' are considered to form
  4966. | a fixed-point value with binary point between `a0' and `a1'. This fixed-
  4967. | point value is shifted right by the number of bits given in `count', and
  4968. | the integer part of the result is returned at the location pointed to by
  4969. | `z0Ptr'. The fractional part of the result may be slightly corrupted as
  4970. | described above, and is returned at the location pointed to by `z1Ptr'.)
  4971. *----------------------------------------------------------------------------*}
  4972. procedure shift64ExtraRightJamming(a0: bits64; a1: bits64; count: int16; var z0Ptr: bits64; var z1Ptr : bits64);
  4973. var
  4974. z0, z1: bits64;
  4975. negCount: int8;
  4976. begin
  4977. negCount := ( - count ) and 63;
  4978. if ( count = 0 ) then
  4979. begin
  4980. z1 := a1;
  4981. z0 := a0;
  4982. end
  4983. else if ( count < 64 ) then
  4984. begin
  4985. z1 := ( a0 shl negCount ) or ord( a1 <> 0 );
  4986. z0 := a0 shr count;
  4987. end
  4988. else begin
  4989. if ( count = 64 ) then
  4990. begin
  4991. z1 := a0 or ord( a1 <> 0 );
  4992. end
  4993. else begin
  4994. z1 := ord( ( a0 or a1 ) <> 0 );
  4995. end;
  4996. z0 := 0;
  4997. end;
  4998. z1Ptr := z1;
  4999. z0Ptr := z0;
  5000. end;
  5001. {$ifdef FPC_SOFTFLOAT_FLOATX80}
  5002. {*----------------------------------------------------------------------------
  5003. | Returns the fraction bits of the extended double-precision floating-point
  5004. | value `a'.
  5005. *----------------------------------------------------------------------------*}
  5006. function extractFloatx80Frac(a : floatx80): bits64;inline;
  5007. begin
  5008. result:=a.low;
  5009. end;
  5010. {*----------------------------------------------------------------------------
  5011. | Returns the exponent bits of the extended double-precision floating-point
  5012. | value `a'.
  5013. *----------------------------------------------------------------------------*}
  5014. function extractFloatx80Exp(a : floatx80): int32;inline;
  5015. begin
  5016. result:=a.high and $7FFF;
  5017. end;
  5018. {*----------------------------------------------------------------------------
  5019. | Returns the sign bit of the extended double-precision floating-point value
  5020. | `a'.
  5021. *----------------------------------------------------------------------------*}
  5022. function extractFloatx80Sign(a : floatx80): flag;inline;
  5023. begin
  5024. result:=a.high shr 15;
  5025. end;
  5026. {*----------------------------------------------------------------------------
  5027. | Normalizes the subnormal extended double-precision floating-point value
  5028. | represented by the denormalized significand `aSig'. The normalized exponent
  5029. | and significand are stored at the locations pointed to by `zExpPtr' and
  5030. | `zSigPtr', respectively.
  5031. *----------------------------------------------------------------------------*}
  5032. procedure normalizeFloatx80Subnormal( aSig: bits64; var zExpPtr: int32; var zSigPtr : bits64);
  5033. var
  5034. shiftCount: int8;
  5035. begin
  5036. shiftCount := countLeadingZeros64( aSig );
  5037. zSigPtr := aSig shl shiftCount;
  5038. zExpPtr := 1 - shiftCount;
  5039. end;
  5040. {*----------------------------------------------------------------------------
  5041. | Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
  5042. | extended double-precision floating-point value, returning the result.
  5043. *----------------------------------------------------------------------------*}
  5044. function packFloatx80( zSign: flag; zExp: int32; zSig : bits64): floatx80;
  5045. var
  5046. z: floatx80;
  5047. begin
  5048. z.low := zSig;
  5049. z.high := ( bits16(zSign) shl 15 ) + zExp;
  5050. result:=z;
  5051. end;
  5052. {*----------------------------------------------------------------------------
  5053. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  5054. | and extended significand formed by the concatenation of `zSig0' and `zSig1',
  5055. | and returns the proper extended double-precision floating-point value
  5056. | corresponding to the abstract input. Ordinarily, the abstract value is
  5057. | rounded and packed into the extended double-precision format, with the
  5058. | inexact exception raised if the abstract input cannot be represented
  5059. | exactly. However, if the abstract value is too large, the overflow and
  5060. | inexact exceptions are raised and an infinity or maximal finite value is
  5061. | returned. If the abstract value is too small, the input value is rounded to
  5062. | a subnormal number, and the underflow and inexact exceptions are raised if
  5063. | the abstract input cannot be represented exactly as a subnormal extended
  5064. | double-precision floating-point number.
  5065. | If `roundingPrecision' is 32 or 64, the result is rounded to the same
  5066. | number of bits as single or double precision, respectively. Otherwise, the
  5067. | result is rounded to the full precision of the extended double-precision
  5068. | format.
  5069. | The input significand must be normalized or smaller. If the input
  5070. | significand is not normalized, `zExp' must be 0; in that case, the result
  5071. | returned is a subnormal number, and it must not require rounding. The
  5072. | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
  5073. | Floating-Point Arithmetic.
  5074. *----------------------------------------------------------------------------*}
  5075. function roundAndPackFloatx80(roundingPrecision: int8; zSign: flag; zExp: int32; zSig0: bits64; zSig1: bits64): floatx80;
  5076. var
  5077. roundingMode: int8;
  5078. roundNearestEven, increment, isTiny: flag;
  5079. roundIncrement, roundMask, roundBits: int64;
  5080. label
  5081. precision80;
  5082. begin
  5083. roundingMode := float_rounding_mode;
  5084. roundNearestEven := flag( roundingMode = float_round_nearest_even );
  5085. if ( roundingPrecision = 80 ) then
  5086. goto precision80;
  5087. if ( roundingPrecision = 64 ) then
  5088. begin
  5089. roundIncrement := int64( $0000000000000400 );
  5090. roundMask := int64( $00000000000007FF );
  5091. end
  5092. else if ( roundingPrecision = 32 ) then
  5093. begin
  5094. roundIncrement := int64( $0000008000000000 );
  5095. roundMask := int64( $000000FFFFFFFFFF );
  5096. end
  5097. else begin
  5098. goto precision80;
  5099. end;
  5100. zSig0 := zSig0 or ord( zSig1 <> 0 );
  5101. if ( not (roundNearestEven<>0) ) then
  5102. begin
  5103. if ( roundingMode = float_round_to_zero ) then
  5104. begin
  5105. roundIncrement := 0;
  5106. end
  5107. else begin
  5108. roundIncrement := roundMask;
  5109. if ( zSign<>0 ) then
  5110. begin
  5111. if ( roundingMode = float_round_up ) then
  5112. roundIncrement := 0;
  5113. end
  5114. else begin
  5115. if ( roundingMode = float_round_down ) then
  5116. roundIncrement := 0;
  5117. end;
  5118. end;
  5119. end;
  5120. roundBits := zSig0 and roundMask;
  5121. if ( $7FFD <= (bits32) ( zExp - 1 ) ) begin
  5122. if ( ( $7FFE < zExp )
  5123. or ( ( zExp = $7FFE ) and ( zSig0 + roundIncrement < zSig0 ) )
  5124. ) begin
  5125. goto overflow;
  5126. end;
  5127. if ( zExp <= 0 ) begin
  5128. isTiny =
  5129. ( float_detect_tininess = float_tininess_before_rounding )
  5130. or ( zExp < 0 )
  5131. or ( zSig0 <= zSig0 + roundIncrement );
  5132. shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
  5133. zExp := 0;
  5134. roundBits := zSig0 and roundMask;
  5135. if ( isTiny and roundBits ) float_raise( float_flag_underflow );
  5136. if ( roundBits ) softfloat_exception_flags |= float_flag_inexact;
  5137. zSig0 += roundIncrement;
  5138. if ( (sbits64) zSig0 < 0 ) zExp := 1;
  5139. roundIncrement := roundMask + 1;
  5140. if ( roundNearestEven and ( roundBits shl 1 = roundIncrement ) ) begin
  5141. roundMask |= roundIncrement;
  5142. end;
  5143. zSig0 &= ~ roundMask;
  5144. result:=packFloatx80( zSign, zExp, zSig0 );
  5145. end;
  5146. end;
  5147. if ( roundBits ) softfloat_exception_flags |= float_flag_inexact;
  5148. zSig0 += roundIncrement;
  5149. if ( zSig0 < roundIncrement ) begin
  5150. ++zExp;
  5151. zSig0 := LIT64( $8000000000000000 );
  5152. end;
  5153. roundIncrement := roundMask + 1;
  5154. if ( roundNearestEven and ( roundBits shl 1 = roundIncrement ) ) begin
  5155. roundMask |= roundIncrement;
  5156. end;
  5157. zSig0 &= ~ roundMask;
  5158. if ( zSig0 = 0 ) zExp := 0;
  5159. result:=packFloatx80( zSign, zExp, zSig0 );
  5160. precision80:
  5161. increment := ( (sbits64) zSig1 < 0 );
  5162. if ( ! roundNearestEven ) begin
  5163. if ( roundingMode = float_round_to_zero ) begin
  5164. increment := 0;
  5165. end;
  5166. else begin
  5167. if ( zSign ) begin
  5168. increment := ( roundingMode = float_round_down ) and zSig1;
  5169. end;
  5170. else begin
  5171. increment := ( roundingMode = float_round_up ) and zSig1;
  5172. end;
  5173. end;
  5174. end;
  5175. if ( $7FFD <= (bits32) ( zExp - 1 ) ) begin
  5176. if ( ( $7FFE < zExp )
  5177. or ( ( zExp = $7FFE )
  5178. and ( zSig0 = LIT64( $FFFFFFFFFFFFFFFF ) )
  5179. and increment
  5180. )
  5181. ) begin
  5182. roundMask := 0;
  5183. overflow:
  5184. float_raise( float_flag_overflow or float_flag_inexact );
  5185. if ( ( roundingMode = float_round_to_zero )
  5186. or ( zSign and ( roundingMode = float_round_up ) )
  5187. or ( ! zSign and ( roundingMode = float_round_down ) )
  5188. ) begin
  5189. result:=packFloatx80( zSign, $7FFE, ~ roundMask );
  5190. end;
  5191. result:=packFloatx80( zSign, $7FFF, LIT64( $8000000000000000 ) );
  5192. end;
  5193. if ( zExp <= 0 ) begin
  5194. isTiny =
  5195. ( float_detect_tininess = float_tininess_before_rounding )
  5196. or ( zExp < 0 )
  5197. or ! increment
  5198. or ( zSig0 < LIT64( $FFFFFFFFFFFFFFFF ) );
  5199. shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
  5200. zExp := 0;
  5201. if ( isTiny and zSig1 ) float_raise( float_flag_underflow );
  5202. if ( zSig1 ) softfloat_exception_flags |= float_flag_inexact;
  5203. if ( roundNearestEven ) begin
  5204. increment := ( (sbits64) zSig1 < 0 );
  5205. end;
  5206. else begin
  5207. if ( zSign ) begin
  5208. increment := ( roundingMode = float_round_down ) and zSig1;
  5209. end;
  5210. else begin
  5211. increment := ( roundingMode = float_round_up ) and zSig1;
  5212. end;
  5213. end;
  5214. if ( increment ) begin
  5215. ++zSig0;
  5216. zSig0 &=
  5217. ~ ( ( (bits64) ( zSig1 shl 1 ) = 0 ) and roundNearestEven );
  5218. if ( (sbits64) zSig0 < 0 ) zExp := 1;
  5219. end;
  5220. result:=packFloatx80( zSign, zExp, zSig0 );
  5221. end;
  5222. end;
  5223. if ( zSig1 ) softfloat_exception_flags |= float_flag_inexact;
  5224. if ( increment ) begin
  5225. ++zSig0;
  5226. if ( zSig0 = 0 ) begin
  5227. ++zExp;
  5228. zSig0 := LIT64( $8000000000000000 );
  5229. end;
  5230. else begin
  5231. zSig0 &= ~ ( ( (bits64) ( zSig1 shl 1 ) = 0 ) and roundNearestEven );
  5232. end;
  5233. end;
  5234. else begin
  5235. if ( zSig0 = 0 ) zExp := 0;
  5236. end;
  5237. result:=packFloatx80( zSign, zExp, zSig0 );
  5238. end;
  5239. {*----------------------------------------------------------------------------
  5240. | Takes an abstract floating-point value having sign `zSign', exponent
  5241. | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
  5242. | and returns the proper extended double-precision floating-point value
  5243. | corresponding to the abstract input. This routine is just like
  5244. | `roundAndPackFloatx80' except that the input significand does not have to be
  5245. | normalized.
  5246. *----------------------------------------------------------------------------*}
  5247. function normalizeRoundAndPackFloatx80(roundingPrecision: int8; zSign: flag; zExp: int32; zSig0: bits64; zSig1: bits64): floatx80;
  5248. var
  5249. shiftCount: int8;
  5250. begin
  5251. if ( zSig0 = 0 ) begin
  5252. zSig0 := zSig1;
  5253. zSig1 := 0;
  5254. zExp -= 64;
  5255. end;
  5256. shiftCount := countLeadingZeros64( zSig0 );
  5257. shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
  5258. zExp := eExp - shiftCount;
  5259. return
  5260. roundAndPackFloatx80( roundingPrecision, zSign, zExp, zSig0, zSig1 );
  5261. end;
  5262. {*----------------------------------------------------------------------------
  5263. | Returns the result of converting the extended double-precision floating-
  5264. | point value `a' to the 32-bit two's complement integer format. The
  5265. | conversion is performed according to the IEC/IEEE Standard for Binary
  5266. | Floating-Point Arithmetic---which means in particular that the conversion
  5267. | is rounded according to the current rounding mode. If `a' is a NaN, the
  5268. | largest positive integer is returned. Otherwise, if the conversion
  5269. | overflows, the largest integer with the same sign as `a' is returned.
  5270. *----------------------------------------------------------------------------*}
  5271. function floatx80_to_int32(a: floatx80): int32;
  5272. var
  5273. aSign: flag;
  5274. aExp, shiftCount: int32;
  5275. aSig: bits64;
  5276. begin
  5277. aSig := extractFloatx80Frac( a );
  5278. aExp := extractFloatx80Exp( a );
  5279. aSign := extractFloatx80Sign( a );
  5280. if ( ( aExp = $7FFF ) and (bits64) ( aSig shl 1 ) ) aSign := 0;
  5281. shiftCount := $4037 - aExp;
  5282. if ( shiftCount <= 0 ) shiftCount := 1;
  5283. shift64RightJamming( aSig, shiftCount, &aSig );
  5284. result := roundAndPackInt32( aSign, aSig );
  5285. end;
  5286. {*----------------------------------------------------------------------------
  5287. | Returns the result of converting the extended double-precision floating-
  5288. | point value `a' to the 32-bit two's complement integer format. The
  5289. | conversion is performed according to the IEC/IEEE Standard for Binary
  5290. | Floating-Point Arithmetic, except that the conversion is always rounded
  5291. | toward zero. If `a' is a NaN, the largest positive integer is returned.
  5292. | Otherwise, if the conversion overflows, the largest integer with the same
  5293. | sign as `a' is returned.
  5294. *----------------------------------------------------------------------------*}
  5295. function floatx80_to_int32_round_to_zero(a: floatx80): int32;
  5296. var
  5297. aSign: flag;
  5298. aExp, shiftCount: int32;
  5299. aSig, savedASig: bits64;
  5300. z: int32;
  5301. begin
  5302. aSig := extractFloatx80Frac( a );
  5303. aExp := extractFloatx80Exp( a );
  5304. aSign := extractFloatx80Sign( a );
  5305. if ( $401E < aExp ) begin
  5306. if ( ( aExp = $7FFF ) and (bits64) ( aSig shl 1 ) ) aSign := 0;
  5307. goto invalid;
  5308. end;
  5309. else if ( aExp < $3FFF ) begin
  5310. if ( aExp or aSig ) softfloat_exception_flags or= float_flag_inexact;
  5311. result := 0;
  5312. end;
  5313. shiftCount := $403E - aExp;
  5314. savedASig := aSig;
  5315. aSig >>= shiftCount;
  5316. z := aSig;
  5317. if ( aSign ) z := - z;
  5318. if ( ( z < 0 ) xor aSign ) begin
  5319. invalid:
  5320. float_raise( float_flag_invalid );
  5321. result := aSign ? (sbits32) $80000000 : $7FFFFFFF;
  5322. end;
  5323. if ( ( aSig shl shiftCount ) <> savedASig ) begin
  5324. softfloat_exception_flags or= float_flag_inexact;
  5325. end;
  5326. result := z;
  5327. end;
  5328. {*----------------------------------------------------------------------------
  5329. | Returns the result of converting the extended double-precision floating-
  5330. | point value `a' to the 64-bit two's complement integer format. The
  5331. | conversion is performed according to the IEC/IEEE Standard for Binary
  5332. | Floating-Point Arithmetic---which means in particular that the conversion
  5333. | is rounded according to the current rounding mode. If `a' is a NaN,
  5334. | the largest positive integer is returned. Otherwise, if the conversion
  5335. | overflows, the largest integer with the same sign as `a' is returned.
  5336. *----------------------------------------------------------------------------*}
  5337. function floatx80_to_int64(a: floatx80): int64;
  5338. var
  5339. aSign: flag;
  5340. aExp, shiftCount: int32;
  5341. aSig, aSigExtra: bits64;
  5342. begin
  5343. aSig := extractFloatx80Frac( a );
  5344. aExp := extractFloatx80Exp( a );
  5345. aSign := extractFloatx80Sign( a );
  5346. shiftCount := $403E - aExp;
  5347. if ( shiftCount <= 0 ) begin
  5348. if ( shiftCount ) begin
  5349. float_raise( float_flag_invalid );
  5350. if ( ! aSign
  5351. or ( ( aExp = $7FFF )
  5352. and ( aSig <> LIT64( $8000000000000000 ) ) )
  5353. ) begin
  5354. result := LIT64( $7FFFFFFFFFFFFFFF );
  5355. end;
  5356. result := (sbits64) LIT64( $8000000000000000 );
  5357. end;
  5358. aSigExtra := 0;
  5359. end;
  5360. else begin
  5361. shift64ExtraRightJamming( aSig, 0, shiftCount, &aSig, &aSigExtra );
  5362. end;
  5363. result := roundAndPackInt64( aSign, aSig, aSigExtra );
  5364. end;
  5365. {*----------------------------------------------------------------------------
  5366. | Returns the result of converting the extended double-precision floating-
  5367. | point value `a' to the 64-bit two's complement integer format. The
  5368. | conversion is performed according to the IEC/IEEE Standard for Binary
  5369. | Floating-Point Arithmetic, except that the conversion is always rounded
  5370. | toward zero. If `a' is a NaN, the largest positive integer is returned.
  5371. | Otherwise, if the conversion overflows, the largest integer with the same
  5372. | sign as `a' is returned.
  5373. *----------------------------------------------------------------------------*}
  5374. function floatx80_to_int64_round_to_zero(a: floatx80): int64;
  5375. var
  5376. aSign: flag;
  5377. aExp, shiftCount: int32;
  5378. aSig: bits64;
  5379. z: int64;
  5380. begin
  5381. aSig := extractFloatx80Frac( a );
  5382. aExp := extractFloatx80Exp( a );
  5383. aSign := extractFloatx80Sign( a );
  5384. shiftCount := aExp - $403E;
  5385. if ( 0 <= shiftCount ) begin
  5386. aSig &= LIT64( $7FFFFFFFFFFFFFFF );
  5387. if ( ( a.high <> $C03E ) or aSig ) begin
  5388. float_raise( float_flag_invalid );
  5389. if ( ! aSign or ( ( aExp = $7FFF ) and aSig ) ) begin
  5390. result := LIT64( $7FFFFFFFFFFFFFFF );
  5391. end;
  5392. end;
  5393. result := (sbits64) LIT64( $8000000000000000 );
  5394. end;
  5395. else if ( aExp < $3FFF ) begin
  5396. if ( aExp or aSig ) softfloat_exception_flags or= float_flag_inexact;
  5397. result := 0;
  5398. end;
  5399. z := aSig>>( - shiftCount );
  5400. if ( (bits64) ( aSig shl ( shiftCount and 63 ) ) ) begin
  5401. softfloat_exception_flags or= float_flag_inexact;
  5402. end;
  5403. if ( aSign ) z := - z;
  5404. result := z;
  5405. end;
  5406. {*----------------------------------------------------------------------------
  5407. | Returns the result of converting the extended double-precision floating-
  5408. | point value `a' to the single-precision floating-point format. The
  5409. | conversion is performed according to the IEC/IEEE Standard for Binary
  5410. | Floating-Point Arithmetic.
  5411. *----------------------------------------------------------------------------*}
  5412. function floatx80_to_float32(a: floatx80): float32;
  5413. var
  5414. aSign: flag;
  5415. aExp: int32;
  5416. aSig: bits64;
  5417. begin
  5418. aSig := extractFloatx80Frac( a );
  5419. aExp := extractFloatx80Exp( a );
  5420. aSign := extractFloatx80Sign( a );
  5421. if ( aExp = $7FFF ) begin
  5422. if ( (bits64) ( aSig shl 1 ) ) begin
  5423. result := commonNaNToFloat32( floatx80ToCommonNaN( a ) );
  5424. end;
  5425. result := packFloat32( aSign, $FF, 0 );
  5426. end;
  5427. shift64RightJamming( aSig, 33, &aSig );
  5428. if ( aExp or aSig ) aExp -= $3F81;
  5429. result := roundAndPackFloat32( aSign, aExp, aSig );
  5430. end;
  5431. {*----------------------------------------------------------------------------
  5432. | Returns the result of converting the extended double-precision floating-
  5433. | point value `a' to the double-precision floating-point format. The
  5434. | conversion is performed according to the IEC/IEEE Standard for Binary
  5435. | Floating-Point Arithmetic.
  5436. *----------------------------------------------------------------------------*}
  5437. function floatx80_to_float64(a: floatx80): float64;
  5438. var
  5439. aSign: flag;
  5440. aExp: int32;
  5441. aSig, zSig: bits64;
  5442. begin
  5443. aSig := extractFloatx80Frac( a );
  5444. aExp := extractFloatx80Exp( a );
  5445. aSign := extractFloatx80Sign( a );
  5446. if ( aExp = $7FFF ) begin
  5447. if ( (bits64) ( aSig shl 1 ) ) begin
  5448. result := commonNaNToFloat64( floatx80ToCommonNaN( a ) );
  5449. end;
  5450. result := packFloat64( aSign, $7FF, 0 );
  5451. end;
  5452. shift64RightJamming( aSig, 1, &zSig );
  5453. if ( aExp or aSig ) aExp -= $3C01;
  5454. result := roundAndPackFloat64( aSign, aExp, zSig );
  5455. end;
  5456. {$ifdef FPC_SOFTFLOAT_FLOAT128}
  5457. {*----------------------------------------------------------------------------
  5458. | Returns the result of converting the extended double-precision floating-
  5459. | point value `a' to the quadruple-precision floating-point format. The
  5460. | conversion is performed according to the IEC/IEEE Standard for Binary
  5461. | Floating-Point Arithmetic.
  5462. *----------------------------------------------------------------------------*}
  5463. function floatx80_to_float128(a: floatx80): float128;
  5464. var
  5465. aSign: flag;
  5466. aExp: int16;
  5467. aSig, zSig0, zSig1: bits64;
  5468. begin
  5469. aSig := extractFloatx80Frac( a );
  5470. aExp := extractFloatx80Exp( a );
  5471. aSign := extractFloatx80Sign( a );
  5472. if ( ( aExp = $7FFF ) and (bits64) ( aSig shl 1 ) ) begin
  5473. result := commonNaNToFloat128( floatx80ToCommonNaN( a ) );
  5474. end;
  5475. shift128Right( aSig shl 1, 0, 16, &zSig0, &zSig1 );
  5476. result := packFloat128( aSign, aExp, zSig0, zSig1 );
  5477. end;
  5478. {$endif FPC_SOFTFLOAT_FLOAT128}
  5479. {*----------------------------------------------------------------------------
  5480. | Rounds the extended double-precision floating-point value `a' to an integer,
  5481. | and Returns the result as an extended quadruple-precision floating-point
  5482. | value. The operation is performed according to the IEC/IEEE Standard for
  5483. | Binary Floating-Point Arithmetic.
  5484. *----------------------------------------------------------------------------*}
  5485. function floatx80_round_to_int(a: floatx80): floatx80;
  5486. var
  5487. aSign: flag;
  5488. aExp: int32;
  5489. lastBitMask, roundBitsMask: bits64;
  5490. roundingMode: int8;
  5491. z: floatx80;
  5492. begin
  5493. aExp := extractFloatx80Exp( a );
  5494. if ( $403E <= aExp ) begin
  5495. if ( ( aExp = $7FFF ) and (bits64) ( extractFloatx80Frac( a ) shl 1 ) ) begin
  5496. result := propagateFloatx80NaN( a, a );
  5497. end;
  5498. result := a;
  5499. end;
  5500. if ( aExp < $3FFF ) begin
  5501. if ( ( aExp = 0 )
  5502. and ( (bits64) ( extractFloatx80Frac( a ) shl 1 ) = 0 ) ) begin
  5503. result := a;
  5504. end;
  5505. softfloat_exception_flags or= float_flag_inexact;
  5506. aSign := extractFloatx80Sign( a );
  5507. switch ( float_rounding_mode ) begin
  5508. case float_round_nearest_even:
  5509. if ( ( aExp = $3FFE ) and (bits64) ( extractFloatx80Frac( a ) shl 1 )
  5510. ) begin
  5511. result :=
  5512. packFloatx80( aSign, $3FFF, LIT64( $8000000000000000 ) );
  5513. end;
  5514. break;
  5515. case float_round_down:
  5516. result :=
  5517. aSign ?
  5518. packFloatx80( 1, $3FFF, LIT64( $8000000000000000 ) )
  5519. : packFloatx80( 0, 0, 0 );
  5520. case float_round_up:
  5521. result :=
  5522. aSign ? packFloatx80( 1, 0, 0 )
  5523. : packFloatx80( 0, $3FFF, LIT64( $8000000000000000 ) );
  5524. end;
  5525. result := packFloatx80( aSign, 0, 0 );
  5526. end;
  5527. lastBitMask := 1;
  5528. lastBitMask shl = $403E - aExp;
  5529. roundBitsMask := lastBitMask - 1;
  5530. z := a;
  5531. roundingMode := float_rounding_mode;
  5532. if ( roundingMode = float_round_nearest_even ) begin
  5533. z.low += lastBitMask>>1;
  5534. if ( ( z.low and roundBitsMask ) = 0 ) z.low &= ~ lastBitMask;
  5535. end;
  5536. else if ( roundingMode <> float_round_to_zero ) begin
  5537. if ( extractFloatx80Sign( z ) xor ( roundingMode = float_round_up ) ) begin
  5538. z.low += roundBitsMask;
  5539. end;
  5540. end;
  5541. z.low &= ~ roundBitsMask;
  5542. if ( z.low = 0 ) begin
  5543. ++z.high;
  5544. z.low := LIT64( $8000000000000000 );
  5545. end;
  5546. if ( z.low <> a.low ) softfloat_exception_flags or= float_flag_inexact;
  5547. result := z;
  5548. end;
  5549. {*----------------------------------------------------------------------------
  5550. | Returns the result of adding the absolute values of the extended double-
  5551. | precision floating-point values `a' and `b'. If `zSign' is 1, the sum is
  5552. | negated before being returned. `zSign' is ignored if the result is a NaN.
  5553. | The addition is performed according to the IEC/IEEE Standard for Binary
  5554. | Floating-Point Arithmetic.
  5555. *----------------------------------------------------------------------------*}
  5556. function addFloatx80Sigs(a: floatx80; b: floatx80; zSign : flag): floatx80;
  5557. var
  5558. aExp, bExp, zExp: int32;
  5559. aSig, bSig, zSig0, zSig1: bits64;
  5560. expDiff: int32;
  5561. begin
  5562. aSig := extractFloatx80Frac( a );
  5563. aExp := extractFloatx80Exp( a );
  5564. bSig := extractFloatx80Frac( b );
  5565. bExp := extractFloatx80Exp( b );
  5566. expDiff := aExp - bExp;
  5567. if ( 0 < expDiff ) begin
  5568. if ( aExp = $7FFF ) begin
  5569. if ( (bits64) ( aSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5570. result := a;
  5571. end;
  5572. if ( bExp = 0 ) --expDiff;
  5573. shift64ExtraRightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  5574. zExp := aExp;
  5575. end;
  5576. else if ( expDiff < 0 ) begin
  5577. if ( bExp = $7FFF ) begin
  5578. if ( (bits64) ( bSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5579. result := packFloatx80( zSign, $7FFF, LIT64( $8000000000000000 ) );
  5580. end;
  5581. if ( aExp = 0 ) ++expDiff;
  5582. shift64ExtraRightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  5583. zExp := bExp;
  5584. end;
  5585. else begin
  5586. if ( aExp = $7FFF ) begin
  5587. if ( (bits64) ( ( aSig or bSig ) shl 1 ) ) begin
  5588. result := propagateFloatx80NaN( a, b );
  5589. end;
  5590. result := a;
  5591. end;
  5592. zSig1 := 0;
  5593. zSig0 := aSig + bSig;
  5594. if ( aExp = 0 ) begin
  5595. normalizeFloatx80Subnormal( zSig0, &zExp, &zSig0 );
  5596. goto roundAndPack;
  5597. end;
  5598. zExp := aExp;
  5599. goto shiftRight1;
  5600. end;
  5601. zSig0 := aSig + bSig;
  5602. if ( (sbits64) zSig0 < 0 ) goto roundAndPack;
  5603. shiftRight1:
  5604. shift64ExtraRightJamming( zSig0, zSig1, 1, &zSig0, &zSig1 );
  5605. zSig0 or= LIT64( $8000000000000000 );
  5606. ++zExp;
  5607. roundAndPack:
  5608. result :=
  5609. roundAndPackFloatx80(
  5610. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  5611. end;
  5612. {*----------------------------------------------------------------------------
  5613. | Returns the result of subtracting the absolute values of the extended
  5614. | double-precision floating-point values `a' and `b'. If `zSign' is 1, the
  5615. | difference is negated before being returned. `zSign' is ignored if the
  5616. | result is a NaN. The subtraction is performed according to the IEC/IEEE
  5617. | Standard for Binary Floating-Point Arithmetic.
  5618. *----------------------------------------------------------------------------*}
  5619. function subFloatx80Sigs(a: floatx80; b: floatx80; zSign : flag): floatx80;
  5620. var
  5621. aExp, bExp, zExp: int32;
  5622. aSig, bSig, zSig0, zSig1: bits64;
  5623. expDiff: int32;
  5624. z: floatx80;
  5625. begin
  5626. aSig := extractFloatx80Frac( a );
  5627. aExp := extractFloatx80Exp( a );
  5628. bSig := extractFloatx80Frac( b );
  5629. bExp := extractFloatx80Exp( b );
  5630. expDiff := aExp - bExp;
  5631. if ( 0 < expDiff ) goto aExpBigger;
  5632. if ( expDiff < 0 ) goto bExpBigger;
  5633. if ( aExp = $7FFF ) begin
  5634. if ( (bits64) ( ( aSig or bSig ) shl 1 ) ) begin
  5635. result := propagateFloatx80NaN( a, b );
  5636. end;
  5637. float_raise( float_flag_invalid );
  5638. z.low := floatx80_default_nan_low;
  5639. z.high := floatx80_default_nan_high;
  5640. result := z;
  5641. end;
  5642. if ( aExp = 0 ) begin
  5643. aExp := 1;
  5644. bExp := 1;
  5645. end;
  5646. zSig1 := 0;
  5647. if ( bSig < aSig ) goto aBigger;
  5648. if ( aSig < bSig ) goto bBigger;
  5649. result := packFloatx80( float_rounding_mode = float_round_down, 0, 0 );
  5650. bExpBigger:
  5651. if ( bExp = $7FFF ) begin
  5652. if ( (bits64) ( bSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5653. result := packFloatx80( zSign xor 1, $7FFF, LIT64( $8000000000000000 ) );
  5654. end;
  5655. if ( aExp = 0 ) ++expDiff;
  5656. shift128RightJamming( aSig, 0, - expDiff, &aSig, &zSig1 );
  5657. bBigger:
  5658. sub128( bSig, 0, aSig, zSig1, &zSig0, &zSig1 );
  5659. zExp := bExp;
  5660. zSign xor = 1;
  5661. goto normalizeRoundAndPack;
  5662. aExpBigger:
  5663. if ( aExp = $7FFF ) begin
  5664. if ( (bits64) ( aSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5665. result := a;
  5666. end;
  5667. if ( bExp = 0 ) --expDiff;
  5668. shift128RightJamming( bSig, 0, expDiff, &bSig, &zSig1 );
  5669. aBigger:
  5670. sub128( aSig, 0, bSig, zSig1, &zSig0, &zSig1 );
  5671. zExp := aExp;
  5672. normalizeRoundAndPack:
  5673. result :=
  5674. normalizeRoundAndPackFloatx80(
  5675. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  5676. end;
  5677. {*----------------------------------------------------------------------------
  5678. | Returns the result of adding the extended double-precision floating-point
  5679. | values `a' and `b'. The operation is performed according to the IEC/IEEE
  5680. | Standard for Binary Floating-Point Arithmetic.
  5681. *----------------------------------------------------------------------------*}
  5682. function floatx80_add(a: floatx80; b: floatx80): floatx80;
  5683. var
  5684. aSign, bSign: flag;
  5685. begin
  5686. aSign := extractFloatx80Sign( a );
  5687. bSign := extractFloatx80Sign( b );
  5688. if ( aSign = bSign ) begin
  5689. result := addFloatx80Sigs( a, b, aSign );
  5690. end;
  5691. else begin
  5692. result := subFloatx80Sigs( a, b, aSign );
  5693. end;
  5694. end;
  5695. {*----------------------------------------------------------------------------
  5696. | Returns the result of subtracting the extended double-precision floating-
  5697. | point values `a' and `b'. The operation is performed according to the
  5698. | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  5699. *----------------------------------------------------------------------------*}
  5700. function floatx80_sub(a: floatx80; b: floatx80 ): floatx80;
  5701. var
  5702. aSign, bSign: flag;
  5703. begin
  5704. aSign := extractFloatx80Sign( a );
  5705. bSign := extractFloatx80Sign( b );
  5706. if ( aSign = bSign ) begin
  5707. result := subFloatx80Sigs( a, b, aSign );
  5708. end;
  5709. else begin
  5710. result := addFloatx80Sigs( a, b, aSign );
  5711. end;
  5712. end;
  5713. {*----------------------------------------------------------------------------
  5714. | Returns the result of multiplying the extended double-precision floating-
  5715. | point values `a' and `b'. The operation is performed according to the
  5716. | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  5717. *----------------------------------------------------------------------------*}
  5718. function floatx80_mul(a: floatx80; b: floatx80): floatx80;
  5719. var
  5720. aSign, bSign, zSign: flag;
  5721. aExp, bExp, zExp: int32;
  5722. aSig, bSig, zSig0, zSig1: bits64;
  5723. z: floatx80;
  5724. begin
  5725. aSig := extractFloatx80Frac( a );
  5726. aExp := extractFloatx80Exp( a );
  5727. aSign := extractFloatx80Sign( a );
  5728. bSig := extractFloatx80Frac( b );
  5729. bExp := extractFloatx80Exp( b );
  5730. bSign := extractFloatx80Sign( b );
  5731. zSign := aSign xor bSign;
  5732. if ( aExp = $7FFF ) begin
  5733. if ( (bits64) ( aSig shl 1 )
  5734. or ( ( bExp = $7FFF ) and (bits64) ( bSig shl 1 ) ) ) begin
  5735. result := propagateFloatx80NaN( a, b );
  5736. end;
  5737. if ( ( bExp or bSig ) = 0 ) goto invalid;
  5738. result := packFloatx80( zSign, $7FFF, LIT64( $8000000000000000 ) );
  5739. end;
  5740. if ( bExp = $7FFF ) begin
  5741. if ( (bits64) ( bSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5742. if ( ( aExp or aSig ) = 0 ) begin
  5743. invalid:
  5744. float_raise( float_flag_invalid );
  5745. z.low := floatx80_default_nan_low;
  5746. z.high := floatx80_default_nan_high;
  5747. result := z;
  5748. end;
  5749. result := packFloatx80( zSign, $7FFF, LIT64( $8000000000000000 ) );
  5750. end;
  5751. if ( aExp = 0 ) begin
  5752. if ( aSig = 0 ) result := packFloatx80( zSign, 0, 0 );
  5753. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  5754. end;
  5755. if ( bExp = 0 ) begin
  5756. if ( bSig = 0 ) result := packFloatx80( zSign, 0, 0 );
  5757. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  5758. end;
  5759. zExp := aExp + bExp - $3FFE;
  5760. mul64To128( aSig, bSig, &zSig0, &zSig1 );
  5761. if ( 0 < (sbits64) zSig0 ) begin
  5762. shortShift128Left( zSig0, zSig1, 1, &zSig0, &zSig1 );
  5763. --zExp;
  5764. end;
  5765. result :=
  5766. roundAndPackFloatx80(
  5767. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  5768. end;
  5769. {*----------------------------------------------------------------------------
  5770. | Returns the result of dividing the extended double-precision floating-point
  5771. | value `a' by the corresponding value `b'. The operation is performed
  5772. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  5773. *----------------------------------------------------------------------------*}
  5774. function floatx80_div(a: floatx80; b: floatx80 ): floatx80;
  5775. var
  5776. aSign, bSign, zSign: flag;
  5777. aExp, bExp, zExp: int32;
  5778. aSig, bSig, zSig0, zSig1: bits64;
  5779. rem0, rem1, rem2, term0, term1, term2: bits64;
  5780. z: floatx80;
  5781. begin
  5782. aSig := extractFloatx80Frac( a );
  5783. aExp := extractFloatx80Exp( a );
  5784. aSign := extractFloatx80Sign( a );
  5785. bSig := extractFloatx80Frac( b );
  5786. bExp := extractFloatx80Exp( b );
  5787. bSign := extractFloatx80Sign( b );
  5788. zSign := aSign xor bSign;
  5789. if ( aExp = $7FFF ) begin
  5790. if ( (bits64) ( aSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5791. if ( bExp = $7FFF ) begin
  5792. if ( (bits64) ( bSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5793. goto invalid;
  5794. end;
  5795. result := packFloatx80( zSign, $7FFF, LIT64( $8000000000000000 ) );
  5796. end;
  5797. if ( bExp = $7FFF ) begin
  5798. if ( (bits64) ( bSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5799. result := packFloatx80( zSign, 0, 0 );
  5800. end;
  5801. if ( bExp = 0 ) begin
  5802. if ( bSig = 0 ) begin
  5803. if ( ( aExp or aSig ) = 0 ) begin
  5804. invalid:
  5805. float_raise( float_flag_invalid );
  5806. z.low := floatx80_default_nan_low;
  5807. z.high := floatx80_default_nan_high;
  5808. result := z;
  5809. end;
  5810. float_raise( float_flag_divbyzero );
  5811. result := packFloatx80( zSign, $7FFF, LIT64( $8000000000000000 ) );
  5812. end;
  5813. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  5814. end;
  5815. if ( aExp = 0 ) begin
  5816. if ( aSig = 0 ) result := packFloatx80( zSign, 0, 0 );
  5817. normalizeFloatx80Subnormal( aSig, &aExp, &aSig );
  5818. end;
  5819. zExp := aExp - bExp + $3FFE;
  5820. rem1 := 0;
  5821. if ( bSig <= aSig ) begin
  5822. shift128Right( aSig, 0, 1, &aSig, &rem1 );
  5823. ++zExp;
  5824. end;
  5825. zSig0 := estimateDiv128To64( aSig, rem1, bSig );
  5826. mul64To128( bSig, zSig0, &term0, &term1 );
  5827. sub128( aSig, rem1, term0, term1, &rem0, &rem1 );
  5828. while ( (sbits64) rem0 < 0 ) begin
  5829. --zSig0;
  5830. add128( rem0, rem1, 0, bSig, &rem0, &rem1 );
  5831. end;
  5832. zSig1 := estimateDiv128To64( rem1, 0, bSig );
  5833. if ( (bits64) ( zSig1 shl 1 ) <= 8 ) begin
  5834. mul64To128( bSig, zSig1, &term1, &term2 );
  5835. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  5836. while ( (sbits64) rem1 < 0 ) begin
  5837. --zSig1;
  5838. add128( rem1, rem2, 0, bSig, &rem1, &rem2 );
  5839. end;
  5840. zSig1 or= ( ( rem1 or rem2 ) <> 0 );
  5841. end;
  5842. result :=
  5843. roundAndPackFloatx80(
  5844. floatx80_rounding_precision, zSign, zExp, zSig0, zSig1 );
  5845. end;
  5846. {*----------------------------------------------------------------------------
  5847. | Returns the remainder of the extended double-precision floating-point value
  5848. | `a' with respect to the corresponding value `b'. The operation is performed
  5849. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  5850. *----------------------------------------------------------------------------*}
  5851. function floatx80_rem(a: floatx80; b: floatx80 ): floatx80;
  5852. var
  5853. aSign, bSign, zSign: flag;
  5854. aExp, bExp, expDiff: int32;
  5855. aSig0, aSig1, bSig: bits64;
  5856. q, term0, term1, alternateASig0, alternateASig1: bits64;
  5857. z: floatx80;
  5858. begin
  5859. aSig0 := extractFloatx80Frac( a );
  5860. aExp := extractFloatx80Exp( a );
  5861. aSign := extractFloatx80Sign( a );
  5862. bSig := extractFloatx80Frac( b );
  5863. bExp := extractFloatx80Exp( b );
  5864. bSign := extractFloatx80Sign( b );
  5865. if ( aExp = $7FFF ) begin
  5866. if ( (bits64) ( aSig0 shl 1 )
  5867. or ( ( bExp = $7FFF ) and (bits64) ( bSig shl 1 ) ) ) begin
  5868. result := propagateFloatx80NaN( a, b );
  5869. end;
  5870. goto invalid;
  5871. end;
  5872. if ( bExp = $7FFF ) begin
  5873. if ( (bits64) ( bSig shl 1 ) ) result := propagateFloatx80NaN( a, b );
  5874. result := a;
  5875. end;
  5876. if ( bExp = 0 ) begin
  5877. if ( bSig = 0 ) begin
  5878. invalid:
  5879. float_raise( float_flag_invalid );
  5880. z.low := floatx80_default_nan_low;
  5881. z.high := floatx80_default_nan_high;
  5882. result := z;
  5883. end;
  5884. normalizeFloatx80Subnormal( bSig, &bExp, &bSig );
  5885. end;
  5886. if ( aExp = 0 ) begin
  5887. if ( (bits64) ( aSig0 shl 1 ) = 0 ) result := a;
  5888. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  5889. end;
  5890. bSig or= LIT64( $8000000000000000 );
  5891. zSign := aSign;
  5892. expDiff := aExp - bExp;
  5893. aSig1 := 0;
  5894. if ( expDiff < 0 ) begin
  5895. if ( expDiff < -1 ) result := a;
  5896. shift128Right( aSig0, 0, 1, &aSig0, &aSig1 );
  5897. expDiff := 0;
  5898. end;
  5899. q := ( bSig <= aSig0 );
  5900. if ( q ) aSig0 -= bSig;
  5901. expDiff -= 64;
  5902. while ( 0 < expDiff ) begin
  5903. q := estimateDiv128To64( aSig0, aSig1, bSig );
  5904. q := ( 2 < q ) ? q - 2 : 0;
  5905. mul64To128( bSig, q, &term0, &term1 );
  5906. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  5907. shortShift128Left( aSig0, aSig1, 62, &aSig0, &aSig1 );
  5908. expDiff -= 62;
  5909. end;
  5910. expDiff += 64;
  5911. if ( 0 < expDiff ) begin
  5912. q := estimateDiv128To64( aSig0, aSig1, bSig );
  5913. q := ( 2 < q ) ? q - 2 : 0;
  5914. q >>= 64 - expDiff;
  5915. mul64To128( bSig, q shl ( 64 - expDiff ), &term0, &term1 );
  5916. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  5917. shortShift128Left( 0, bSig, 64 - expDiff, &term0, &term1 );
  5918. while ( le128( term0, term1, aSig0, aSig1 ) ) begin
  5919. ++q;
  5920. sub128( aSig0, aSig1, term0, term1, &aSig0, &aSig1 );
  5921. end;
  5922. end;
  5923. else begin
  5924. term1 := 0;
  5925. term0 := bSig;
  5926. end;
  5927. sub128( term0, term1, aSig0, aSig1, &alternateASig0, &alternateASig1 );
  5928. if ( lt128( alternateASig0, alternateASig1, aSig0, aSig1 )
  5929. or ( eq128( alternateASig0, alternateASig1, aSig0, aSig1 )
  5930. and ( q and 1 ) )
  5931. ) begin
  5932. aSig0 := alternateASig0;
  5933. aSig1 := alternateASig1;
  5934. zSign := ! zSign;
  5935. end;
  5936. result :=
  5937. normalizeRoundAndPackFloatx80(
  5938. 80, zSign, bExp + expDiff, aSig0, aSig1 );
  5939. end;
  5940. {*----------------------------------------------------------------------------
  5941. | Returns the square root of the extended double-precision floating-point
  5942. | value `a'. The operation is performed according to the IEC/IEEE Standard
  5943. | for Binary Floating-Point Arithmetic.
  5944. *----------------------------------------------------------------------------*}
  5945. function floatx80_sqrt(a: floatx80): floatx80;
  5946. var
  5947. aSign: flag;
  5948. aExp, zExp: int32;
  5949. aSig0, aSig1, zSig0, zSig1, doubleZSig0: bits64;
  5950. rem0, rem1, rem2, rem3, term0, term1, term2, term3: bits64;
  5951. z: floatx80;
  5952. label
  5953. invalid;
  5954. begin
  5955. aSig0 := extractFloatx80Frac( a );
  5956. aExp := extractFloatx80Exp( a );
  5957. aSign := extractFloatx80Sign( a );
  5958. if ( aExp = $7FFF ) begin
  5959. if ( (bits64) ( aSig0 shl 1 ) ) result := propagateFloatx80NaN( a, a );
  5960. if ( ! aSign ) result := a;
  5961. goto invalid;
  5962. end;
  5963. if ( aSign ) begin
  5964. if ( ( aExp or aSig0 ) = 0 ) result := a;
  5965. invalid:
  5966. float_raise( float_flag_invalid );
  5967. z.low := floatx80_default_nan_low;
  5968. z.high := floatx80_default_nan_high;
  5969. result := z;
  5970. end;
  5971. if ( aExp = 0 ) begin
  5972. if ( aSig0 = 0 ) result := packFloatx80( 0, 0, 0 );
  5973. normalizeFloatx80Subnormal( aSig0, &aExp, &aSig0 );
  5974. end;
  5975. zExp := ( ( aExp - $3FFF )>>1 ) + $3FFF;
  5976. zSig0 := estimateSqrt32( aExp, aSig0>>32 );
  5977. shift128Right( aSig0, 0, 2 + ( aExp and 1 ), &aSig0, &aSig1 );
  5978. zSig0 := estimateDiv128To64( aSig0, aSig1, zSig0 shl 32 ) + ( zSig0 shl 30 );
  5979. doubleZSig0 := zSig0 shl 1;
  5980. mul64To128( zSig0, zSig0, &term0, &term1 );
  5981. sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
  5982. while ( (sbits64) rem0 < 0 ) begin
  5983. --zSig0;
  5984. doubleZSig0 -= 2;
  5985. add128( rem0, rem1, zSig0>>63, doubleZSig0 or 1, &rem0, &rem1 );
  5986. end;
  5987. zSig1 := estimateDiv128To64( rem1, 0, doubleZSig0 );
  5988. if ( ( zSig1 and LIT64( $3FFFFFFFFFFFFFFF ) ) <= 5 ) begin
  5989. if ( zSig1 = 0 ) zSig1 := 1;
  5990. mul64To128( doubleZSig0, zSig1, &term1, &term2 );
  5991. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  5992. mul64To128( zSig1, zSig1, &term2, &term3 );
  5993. sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
  5994. while ( (sbits64) rem1 < 0 ) begin
  5995. --zSig1;
  5996. shortShift128Left( 0, zSig1, 1, &term2, &term3 );
  5997. term3 or= 1;
  5998. term2 or= doubleZSig0;
  5999. add192( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 );
  6000. end;
  6001. zSig1 or= ( ( rem1 or rem2 or rem3 ) <> 0 );
  6002. end;
  6003. shortShift128Left( 0, zSig1, 1, &zSig0, &zSig1 );
  6004. zSig0 or= doubleZSig0;
  6005. result :=
  6006. roundAndPackFloatx80(
  6007. floatx80_rounding_precision, 0, zExp, zSig0, zSig1 );
  6008. end;
  6009. {*----------------------------------------------------------------------------
  6010. | Returns 1 if the extended double-precision floating-point value `a' is
  6011. | equal to the corresponding value `b', and 0 otherwise. The comparison is
  6012. | performed according to the IEC/IEEE Standard for Binary Floating-Point
  6013. | Arithmetic.
  6014. *----------------------------------------------------------------------------*}
  6015. function floatx80_eq(a: floatx80; b: floatx80 ): flag;
  6016. begin
  6017. if ( ( ( extractFloatx80Exp( a ) = $7FFF )
  6018. and (bits64) ( extractFloatx80Frac( a ) shl 1 ) )
  6019. or ( ( extractFloatx80Exp( b ) = $7FFF )
  6020. and (bits64) ( extractFloatx80Frac( b ) shl 1 ) )
  6021. ) begin
  6022. if ( floatx80_is_signaling_nan( a )
  6023. or floatx80_is_signaling_nan( b ) ) begin
  6024. float_raise( float_flag_invalid );
  6025. end;
  6026. result := 0;
  6027. end;
  6028. result :=
  6029. ( a.low = b.low )
  6030. and ( ( a.high = b.high )
  6031. or ( ( a.low = 0 )
  6032. and ( (bits16) ( ( a.high or b.high ) shl 1 ) = 0 ) )
  6033. );
  6034. end;
  6035. {*----------------------------------------------------------------------------
  6036. | Returns 1 if the extended double-precision floating-point value `a' is
  6037. | less than or equal to the corresponding value `b', and 0 otherwise. The
  6038. | comparison is performed according to the IEC/IEEE Standard for Binary
  6039. | Floating-Point Arithmetic.
  6040. *----------------------------------------------------------------------------*}
  6041. function floatx80_le(a: floatx80; b: floatx80 ): flag;
  6042. var
  6043. aSign, bSign: flag;
  6044. begin
  6045. if ( ( ( extractFloatx80Exp( a ) = $7FFF )
  6046. and (bits64) ( extractFloatx80Frac( a ) shl 1 ) )
  6047. or ( ( extractFloatx80Exp( b ) = $7FFF )
  6048. and (bits64) ( extractFloatx80Frac( b ) shl 1 ) )
  6049. ) begin
  6050. float_raise( float_flag_invalid );
  6051. result := 0;
  6052. end;
  6053. aSign := extractFloatx80Sign( a );
  6054. bSign := extractFloatx80Sign( b );
  6055. if ( aSign <> bSign ) begin
  6056. result :=
  6057. aSign
  6058. or ( ( ( (bits16) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  6059. = 0 );
  6060. end;
  6061. result :=
  6062. aSign ? le128( b.high, b.low, a.high, a.low )
  6063. : le128( a.high, a.low, b.high, b.low );
  6064. end;
  6065. {*----------------------------------------------------------------------------
  6066. | Returns 1 if the extended double-precision floating-point value `a' is
  6067. | less than the corresponding value `b', and 0 otherwise. The comparison
  6068. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6069. | Arithmetic.
  6070. *----------------------------------------------------------------------------*}
  6071. function floatx80_lt(a: floatx80; b: floatx80 ): flag;
  6072. var
  6073. aSign, bSign: flag;
  6074. begin
  6075. if ( ( ( extractFloatx80Exp( a ) = $7FFF )
  6076. and (bits64) ( extractFloatx80Frac( a ) shl 1 ) )
  6077. or ( ( extractFloatx80Exp( b ) = $7FFF )
  6078. and (bits64) ( extractFloatx80Frac( b ) shl 1 ) )
  6079. ) begin
  6080. float_raise( float_flag_invalid );
  6081. result := 0;
  6082. end;
  6083. aSign := extractFloatx80Sign( a );
  6084. bSign := extractFloatx80Sign( b );
  6085. if ( aSign <> bSign ) begin
  6086. result :=
  6087. aSign
  6088. and ( ( ( (bits16) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  6089. <> 0 );
  6090. end;
  6091. result :=
  6092. aSign ? lt128( b.high, b.low, a.high, a.low )
  6093. : lt128( a.high, a.low, b.high, b.low );
  6094. end;
  6095. {*----------------------------------------------------------------------------
  6096. | Returns 1 if the extended double-precision floating-point value `a' is equal
  6097. | to the corresponding value `b', and 0 otherwise. The invalid exception is
  6098. | raised if either operand is a NaN. Otherwise, the comparison is performed
  6099. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  6100. *----------------------------------------------------------------------------*}
  6101. function floatx80_eq_signaling(a: floatx80; b: floatx80 ): flag;
  6102. begin
  6103. if ( ( ( extractFloatx80Exp( a ) = $7FFF )
  6104. and (bits64) ( extractFloatx80Frac( a ) shl 1 ) )
  6105. or ( ( extractFloatx80Exp( b ) = $7FFF )
  6106. and (bits64) ( extractFloatx80Frac( b ) shl 1 ) )
  6107. ) begin
  6108. float_raise( float_flag_invalid );
  6109. result := 0;
  6110. end;
  6111. result :=
  6112. ( a.low = b.low )
  6113. and ( ( a.high = b.high )
  6114. or ( ( a.low = 0 )
  6115. and ( (bits16) ( ( a.high or b.high ) shl 1 ) = 0 ) )
  6116. );
  6117. end;
  6118. {*----------------------------------------------------------------------------
  6119. | Returns 1 if the extended double-precision floating-point value `a' is less
  6120. | than or equal to the corresponding value `b', and 0 otherwise. Quiet NaNs
  6121. | do not cause an exception. Otherwise, the comparison is performed according
  6122. | to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  6123. *----------------------------------------------------------------------------*}
  6124. function floatx80_le_quiet(a: floatx80; b: floatx80 ): flag;
  6125. var
  6126. aSign, bSign: flag;
  6127. begin
  6128. if ( ( ( extractFloatx80Exp( a ) = $7FFF )
  6129. and (bits64) ( extractFloatx80Frac( a ) shl 1 ) )
  6130. or ( ( extractFloatx80Exp( b ) = $7FFF )
  6131. and (bits64) ( extractFloatx80Frac( b ) shl 1 ) )
  6132. ) begin
  6133. if ( floatx80_is_signaling_nan( a )
  6134. or floatx80_is_signaling_nan( b ) ) begin
  6135. float_raise( float_flag_invalid );
  6136. end;
  6137. result := 0;
  6138. end;
  6139. aSign := extractFloatx80Sign( a );
  6140. bSign := extractFloatx80Sign( b );
  6141. if ( aSign <> bSign ) begin
  6142. result :=
  6143. aSign
  6144. or ( ( ( (bits16) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  6145. = 0 );
  6146. end;
  6147. result :=
  6148. aSign ? le128( b.high, b.low, a.high, a.low )
  6149. : le128( a.high, a.low, b.high, b.low );
  6150. end;
  6151. {*----------------------------------------------------------------------------
  6152. | Returns 1 if the extended double-precision floating-point value `a' is less
  6153. | than the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause
  6154. | an exception. Otherwise, the comparison is performed according to the
  6155. | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  6156. *----------------------------------------------------------------------------*}
  6157. function floatx80_lt_quiet(a: floatx80; b: floatx80 ): flag;
  6158. var
  6159. aSign, bSign: flag;
  6160. begin
  6161. if ( ( ( extractFloatx80Exp( a ) = $7FFF )
  6162. and (bits64) ( extractFloatx80Frac( a ) shl 1 ) )
  6163. or ( ( extractFloatx80Exp( b ) = $7FFF )
  6164. and (bits64) ( extractFloatx80Frac( b ) shl 1 ) )
  6165. ) begin
  6166. if ( floatx80_is_signaling_nan( a )
  6167. or floatx80_is_signaling_nan( b ) ) begin
  6168. float_raise( float_flag_invalid );
  6169. end;
  6170. result := 0;
  6171. end;
  6172. aSign := extractFloatx80Sign( a );
  6173. bSign := extractFloatx80Sign( b );
  6174. if ( aSign <> bSign ) begin
  6175. result :=
  6176. aSign
  6177. and ( ( ( (bits16) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  6178. <> 0 );
  6179. end;
  6180. result :=
  6181. aSign ? lt128( b.high, b.low, a.high, a.low )
  6182. : lt128( a.high, a.low, b.high, b.low );
  6183. end;
  6184. {$endif FPC_SOFTFLOAT_FLOATX80}
  6185. {$ifdef FPC_SOFTFLOAT_FLOAT128}
  6186. {*----------------------------------------------------------------------------
  6187. | Returns the least-significant 64 fraction bits of the quadruple-precision
  6188. | floating-point value `a'.
  6189. *----------------------------------------------------------------------------*}
  6190. function extractFloat128Frac1(a : float128): bits64;
  6191. begin
  6192. result:=a.low;
  6193. end;
  6194. {*----------------------------------------------------------------------------
  6195. | Returns the most-significant 48 fraction bits of the quadruple-precision
  6196. | floating-point value `a'.
  6197. *----------------------------------------------------------------------------*}
  6198. function extractFloat128Frac0(a : float128): bits64;
  6199. begin
  6200. result:=a.high and int64($0000FFFFFFFFFFFF);
  6201. end;
  6202. {*----------------------------------------------------------------------------
  6203. | Returns the exponent bits of the quadruple-precision floating-point value
  6204. | `a'.
  6205. *----------------------------------------------------------------------------*}
  6206. function extractFloat128Exp(a : float128): int32;
  6207. begin
  6208. result:=( a.high shr 48 ) and $7FFF;
  6209. end;
  6210. {*----------------------------------------------------------------------------
  6211. | Returns the sign bit of the quadruple-precision floating-point value `a'.
  6212. *----------------------------------------------------------------------------*}
  6213. function extractFloat128Sign(a : float128): flag;
  6214. begin
  6215. result:=a.high shr 63;
  6216. end;
  6217. {*----------------------------------------------------------------------------
  6218. | Normalizes the subnormal quadruple-precision floating-point value
  6219. | represented by the denormalized significand formed by the concatenation of
  6220. | `aSig0' and `aSig1'. The normalized exponent is stored at the location
  6221. | pointed to by `zExpPtr'. The most significant 49 bits of the normalized
  6222. | significand are stored at the location pointed to by `zSig0Ptr', and the
  6223. | least significant 64 bits of the normalized significand are stored at the
  6224. | location pointed to by `zSig1Ptr'.
  6225. *----------------------------------------------------------------------------*}
  6226. procedure normalizeFloat128Subnormal(
  6227. aSig0: bits64;
  6228. aSig1: bits64;
  6229. var zExpPtr: int32;
  6230. var zSig0Ptr: bits64;
  6231. var zSig1Ptr: bits64);
  6232. var
  6233. shiftCount: int8;
  6234. begin
  6235. if ( aSig0 = 0 ) then
  6236. begin
  6237. shiftCount := countLeadingZeros64( aSig1 ) - 15;
  6238. if ( shiftCount < 0 ) then
  6239. begin
  6240. zSig0Ptr := aSig1 shr ( - shiftCount );
  6241. zSig1Ptr := aSig1 shl ( shiftCount and 63 );
  6242. end
  6243. else begin
  6244. zSig0Ptr := aSig1 shl shiftCount;
  6245. zSig1Ptr := 0;
  6246. end;
  6247. zExpPtr := - shiftCount - 63;
  6248. end
  6249. else begin
  6250. shiftCount := countLeadingZeros64( aSig0 ) - 15;
  6251. shortShift128Left( aSig0, aSig1, shiftCount, zSig0Ptr, zSig1Ptr );
  6252. zExpPtr := 1 - shiftCount;
  6253. end;
  6254. end;
  6255. {*----------------------------------------------------------------------------
  6256. | Packs the sign `zSign', the exponent `zExp', and the significand formed
  6257. | by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
  6258. | floating-point value, returning the result. After being shifted into the
  6259. | proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
  6260. | added together to form the most significant 32 bits of the result. This
  6261. | means that any integer portion of `zSig0' will be added into the exponent.
  6262. | Since a properly normalized significand will have an integer portion equal
  6263. | to 1, the `zExp' input should be 1 less than the desired result exponent
  6264. | whenever `zSig0' and `zSig1' concatenated form a complete, normalized
  6265. | significand.
  6266. *----------------------------------------------------------------------------*}
  6267. function packFloat128( zSign: flag; zExp: int32; zSig0: bits64; zSig1: bits64) : float128;
  6268. var
  6269. z: float128;
  6270. begin
  6271. z.low := zSig1;
  6272. z.high := ( ( bits64(zSign) ) shl 63 ) + ( ( bits64(zExp) ) shl 48 ) + zSig0;
  6273. result:=z;
  6274. end;
  6275. {*----------------------------------------------------------------------------
  6276. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  6277. | and extended significand formed by the concatenation of `zSig0', `zSig1',
  6278. | and `zSig2', and returns the proper quadruple-precision floating-point value
  6279. | corresponding to the abstract input. Ordinarily, the abstract value is
  6280. | simply rounded and packed into the quadruple-precision format, with the
  6281. | inexact exception raised if the abstract input cannot be represented
  6282. | exactly. However, if the abstract value is too large, the overflow and
  6283. | inexact exceptions are raised and an infinity or maximal finite value is
  6284. | returned. If the abstract value is too small, the input value is rounded to
  6285. | a subnormal number, and the underflow and inexact exceptions are raised if
  6286. | the abstract input cannot be represented exactly as a subnormal quadruple-
  6287. | precision floating-point number.
  6288. | The input significand must be normalized or smaller. If the input
  6289. | significand is not normalized, `zExp' must be 0; in that case, the result
  6290. | returned is a subnormal number, and it must not require rounding. In the
  6291. | usual case that the input significand is normalized, `zExp' must be 1 less
  6292. | than the ``true'' floating-point exponent. The handling of underflow and
  6293. | overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  6294. *----------------------------------------------------------------------------*}
  6295. function roundAndPackFloat128(zSign: flag; zExp: int32; zSig0: bits64; zSig1: bits64; zSig2: bits64): float128;
  6296. var
  6297. roundingMode: int8;
  6298. roundNearestEven, increment, isTiny: flag;
  6299. begin
  6300. roundingMode := float_rounding_mode;
  6301. roundNearestEven := ord( roundingMode = float_round_nearest_even );
  6302. increment := ord( sbits64(zSig2) < 0 );
  6303. if ( roundNearestEven=0 ) then
  6304. begin
  6305. if ( roundingMode = float_round_to_zero ) then
  6306. begin
  6307. increment := 0;
  6308. end
  6309. else begin
  6310. if ( zSign<>0 ) then
  6311. begin
  6312. increment := ord( roundingMode = float_round_down ) and zSig2;
  6313. end
  6314. else begin
  6315. increment := ord( roundingMode = float_round_up ) and zSig2;
  6316. end;
  6317. end;
  6318. end;
  6319. if ( $7FFD <= bits32(zExp) ) then
  6320. begin
  6321. if ( ord( $7FFD < zExp )
  6322. or ( ord( zExp = $7FFD )
  6323. and eq128(
  6324. int64( $0001FFFFFFFFFFFF ),
  6325. int64( $FFFFFFFFFFFFFFFF ),
  6326. zSig0,
  6327. zSig1
  6328. )
  6329. and increment
  6330. )
  6331. )<>0 then
  6332. begin
  6333. float_raise( float_flag_overflow or float_flag_inexact );
  6334. if ( ord( roundingMode = float_round_to_zero )
  6335. or ( zSign and ord( roundingMode = float_round_up ) )
  6336. or ( not(zSign) and ord( roundingMode = float_round_down ) )
  6337. )<>0 then
  6338. begin
  6339. result :=
  6340. packFloat128(
  6341. zSign,
  6342. $7FFE,
  6343. int64( $0000FFFFFFFFFFFF ),
  6344. int64( $FFFFFFFFFFFFFFFF )
  6345. );
  6346. end;
  6347. result:=packFloat128( zSign, $7FFF, 0, 0 );
  6348. end;
  6349. if ( zExp < 0 ) then
  6350. begin
  6351. isTiny :=
  6352. ord(( float_detect_tininess = float_tininess_before_rounding )
  6353. or ( zExp < -1 )
  6354. or not( increment<>0 )
  6355. or boolean(lt128(
  6356. zSig0,
  6357. zSig1,
  6358. int64( $0001FFFFFFFFFFFF ),
  6359. int64( $FFFFFFFFFFFFFFFF )
  6360. )));
  6361. shift128ExtraRightJamming(
  6362. zSig0, zSig1, zSig2, - zExp, zSig0, zSig1, zSig2 );
  6363. zExp := 0;
  6364. if ( isTiny and zSig2 )<>0 then
  6365. float_raise( float_flag_underflow );
  6366. if ( roundNearestEven<>0 ) then
  6367. begin
  6368. increment := ord( sbits64(zSig2) < 0 );
  6369. end
  6370. else begin
  6371. if ( zSign<>0 ) then
  6372. begin
  6373. increment := ord( roundingMode = float_round_down ) and zSig2;
  6374. end
  6375. else begin
  6376. increment := ord( roundingMode = float_round_up ) and zSig2;
  6377. end;
  6378. end;
  6379. end;
  6380. end;
  6381. if ( zSig2<>0 ) then
  6382. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6383. if ( increment<>0 ) then
  6384. begin
  6385. add128( zSig0, zSig1, 0, 1, zSig0, zSig1 );
  6386. zSig1 := zSig1 and not( ord( zSig2 + zSig2 = 0 ) and roundNearestEven );
  6387. end
  6388. else begin
  6389. if ( ( zSig0 or zSig1 ) = 0 ) then
  6390. zExp := 0;
  6391. end;
  6392. result:=packFloat128( zSign, zExp, zSig0, zSig1 );
  6393. end;
  6394. {*----------------------------------------------------------------------------
  6395. | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
  6396. | and significand formed by the concatenation of `zSig0' and `zSig1', and
  6397. | returns the proper quadruple-precision floating-point value corresponding
  6398. | to the abstract input. This routine is just like `roundAndPackFloat128'
  6399. | except that the input significand has fewer bits and does not have to be
  6400. | normalized. In all cases, `zExp' must be 1 less than the ``true'' floating-
  6401. | point exponent.
  6402. *----------------------------------------------------------------------------*}
  6403. function normalizeRoundAndPackFloat128(zSign: flag; zExp: int32; zSig0: bits64; zSig1: bits64): float128;
  6404. var
  6405. shiftCount: int8;
  6406. zSig2: bits64;
  6407. begin
  6408. if ( zSig0 = 0 ) then
  6409. begin
  6410. zSig0 := zSig1;
  6411. zSig1 := 0;
  6412. dec(zExp, 64);
  6413. end;
  6414. shiftCount := countLeadingZeros64( zSig0 ) - 15;
  6415. if ( 0 <= shiftCount ) then
  6416. begin
  6417. zSig2 := 0;
  6418. shortShift128Left( zSig0, zSig1, shiftCount, zSig0, zSig1 );
  6419. end
  6420. else begin
  6421. shift128ExtraRightJamming(
  6422. zSig0, zSig1, 0, - shiftCount, zSig0, zSig1, zSig2 );
  6423. end;
  6424. dec(zExp, shiftCount);
  6425. result:=roundAndPackFloat128( zSign, zExp, zSig0, zSig1, zSig2 );
  6426. end;
  6427. {*----------------------------------------------------------------------------
  6428. | Returns the result of converting the quadruple-precision floating-point
  6429. | value `a' to the 32-bit two's complement integer format. The conversion
  6430. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6431. | Arithmetic---which means in particular that the conversion is rounded
  6432. | according to the current rounding mode. If `a' is a NaN, the largest
  6433. | positive integer is returned. Otherwise, if the conversion overflows, the
  6434. | largest integer with the same sign as `a' is returned.
  6435. *----------------------------------------------------------------------------*}
  6436. function float128_to_int32(a: float128): int32;
  6437. var
  6438. aSign: flag;
  6439. aExp, shiftCount: int32;
  6440. aSig0, aSig1: bits64;
  6441. begin
  6442. aSig1 := extractFloat128Frac1( a );
  6443. aSig0 := extractFloat128Frac0( a );
  6444. aExp := extractFloat128Exp( a );
  6445. aSign := extractFloat128Sign( a );
  6446. if ( ord( aExp = $7FFF ) and ( aSig0 or aSig1 ) )<>0 then
  6447. aSign := 0;
  6448. if ( aExp<>0 ) then
  6449. aSig0 := aSig0 or int64( $0001000000000000 );
  6450. aSig0 := aSig0 or ord( aSig1 <> 0 );
  6451. shiftCount := $4028 - aExp;
  6452. if ( 0 < shiftCount ) then
  6453. shift64RightJamming( aSig0, shiftCount, aSig0 );
  6454. result := roundAndPackInt32( aSign, aSig0 );
  6455. end;
  6456. {*----------------------------------------------------------------------------
  6457. | Returns the result of converting the quadruple-precision floating-point
  6458. | value `a' to the 32-bit two's complement integer format. The conversion
  6459. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6460. | Arithmetic, except that the conversion is always rounded toward zero. If
  6461. | `a' is a NaN, the largest positive integer is returned. Otherwise, if the
  6462. | conversion overflows, the largest integer with the same sign as `a' is
  6463. | returned.
  6464. *----------------------------------------------------------------------------*}
  6465. function float128_to_int32_round_to_zero(a: float128): int32;
  6466. var
  6467. aSign: flag;
  6468. aExp, shiftCount: int32;
  6469. aSig0, aSig1, savedASig: bits64;
  6470. z: int32;
  6471. label
  6472. invalid;
  6473. begin
  6474. aSig1 := extractFloat128Frac1( a );
  6475. aSig0 := extractFloat128Frac0( a );
  6476. aExp := extractFloat128Exp( a );
  6477. aSign := extractFloat128Sign( a );
  6478. aSig0 := aSig0 or ord( aSig1 <> 0 );
  6479. if ( $401E < aExp ) then
  6480. begin
  6481. if ( ord( aExp = $7FFF ) and aSig0 )<>0 then
  6482. aSign := 0;
  6483. goto invalid;
  6484. end
  6485. else if ( aExp < $3FFF ) then
  6486. begin
  6487. if ( aExp or aSig0 )<>0 then
  6488. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6489. result := 0;
  6490. exit;
  6491. end;
  6492. aSig0 := aSig0 or int64( $0001000000000000 );
  6493. shiftCount := $402F - aExp;
  6494. savedASig := aSig0;
  6495. aSig0 := aSig0 shr shiftCount;
  6496. z := aSig0;
  6497. if ( aSign )<>0 then
  6498. z := - z;
  6499. if ( ord( z < 0 ) xor aSign )<>0 then
  6500. begin
  6501. invalid:
  6502. float_raise( float_flag_invalid );
  6503. if aSign<>0 then
  6504. result:=$80000000
  6505. else
  6506. result:=$7FFFFFFF;
  6507. exit;
  6508. end;
  6509. if ( ( aSig0 shl shiftCount ) <> savedASig ) then
  6510. begin
  6511. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6512. end;
  6513. result := z;
  6514. end;
  6515. {*----------------------------------------------------------------------------
  6516. | Returns the result of converting the quadruple-precision floating-point
  6517. | value `a' to the 64-bit two's complement integer format. The conversion
  6518. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6519. | Arithmetic---which means in particular that the conversion is rounded
  6520. | according to the current rounding mode. If `a' is a NaN, the largest
  6521. | positive integer is returned. Otherwise, if the conversion overflows, the
  6522. | largest integer with the same sign as `a' is returned.
  6523. *----------------------------------------------------------------------------*}
  6524. function float128_to_int64(a: float128): int64;
  6525. var
  6526. aSign: flag;
  6527. aExp, shiftCount: int32;
  6528. aSig0, aSig1: bits64;
  6529. begin
  6530. aSig1 := extractFloat128Frac1( a );
  6531. aSig0 := extractFloat128Frac0( a );
  6532. aExp := extractFloat128Exp( a );
  6533. aSign := extractFloat128Sign( a );
  6534. if ( aExp<>0 ) then
  6535. aSig0 := aSig0 or int64( $0001000000000000 );
  6536. shiftCount := $402F - aExp;
  6537. if ( shiftCount <= 0 ) then
  6538. begin
  6539. if ( $403E < aExp ) then
  6540. begin
  6541. float_raise( float_flag_invalid );
  6542. if ( (aSign=0)
  6543. or ( ( aExp = $7FFF )
  6544. and ( (aSig1<>0) or ( aSig0 <> int64( $0001000000000000 ) ) )
  6545. )
  6546. ) then
  6547. begin
  6548. result := int64( $7FFFFFFFFFFFFFFF );
  6549. end;
  6550. result := int64( $8000000000000000 );
  6551. end;
  6552. shortShift128Left( aSig0, aSig1, - shiftCount, aSig0, aSig1 );
  6553. end
  6554. else begin
  6555. shift64ExtraRightJamming( aSig0, aSig1, shiftCount, aSig0, aSig1 );
  6556. end;
  6557. result := roundAndPackInt64( aSign, aSig0, aSig1 );
  6558. end;
  6559. {*----------------------------------------------------------------------------
  6560. | Returns the result of converting the quadruple-precision floating-point
  6561. | value `a' to the 64-bit two's complement integer format. The conversion
  6562. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6563. | Arithmetic, except that the conversion is always rounded toward zero.
  6564. | If `a' is a NaN, the largest positive integer is returned. Otherwise, if
  6565. | the conversion overflows, the largest integer with the same sign as `a' is
  6566. | returned.
  6567. *----------------------------------------------------------------------------*}
  6568. function float128_to_int64_round_to_zero(a: float128): int64;
  6569. var
  6570. aSign: flag;
  6571. aExp, shiftCount: int32;
  6572. aSig0, aSig1: bits64;
  6573. z: int64;
  6574. begin
  6575. aSig1 := extractFloat128Frac1( a );
  6576. aSig0 := extractFloat128Frac0( a );
  6577. aExp := extractFloat128Exp( a );
  6578. aSign := extractFloat128Sign( a );
  6579. if ( aExp<>0 ) then
  6580. aSig0 := aSig0 or int64( $0001000000000000 );
  6581. shiftCount := aExp - $402F;
  6582. if ( 0 < shiftCount ) then
  6583. begin
  6584. if ( $403E <= aExp ) then
  6585. begin
  6586. aSig0 := aSig0 and int64( $0000FFFFFFFFFFFF );
  6587. if ( ( a.high = int64( $C03E000000000000 ) )
  6588. and ( aSig1 < int64( $0002000000000000 ) ) ) then
  6589. begin
  6590. if ( aSig1<>0 ) then
  6591. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6592. end
  6593. else begin
  6594. float_raise( float_flag_invalid );
  6595. if ( (aSign=0) or ( ( aExp = $7FFF ) and (( aSig0 or aSig1 )<>0) ) ) then
  6596. begin
  6597. result := int64( $7FFFFFFFFFFFFFFF );
  6598. exit;
  6599. end;
  6600. end;
  6601. result := int64( $8000000000000000 );
  6602. exit;
  6603. end;
  6604. z := ( aSig0 shl shiftCount ) or ( aSig1>>( ( - shiftCount ) and 63 ) );
  6605. if ( int64( aSig1 shl shiftCount )<>0 ) then
  6606. begin
  6607. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6608. end;
  6609. end
  6610. else begin
  6611. if ( aExp < $3FFF ) then
  6612. begin
  6613. if ( aExp or aSig0 or aSig1 )<>0 then
  6614. begin
  6615. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6616. end;
  6617. result := 0;
  6618. exit;
  6619. end;
  6620. z := aSig0 shr ( - shiftCount );
  6621. if ( (aSig1<>0)
  6622. or ( (shiftCount<>0) and (int64( aSig0 shl ( shiftCount and 63 ) )<>0) ) ) then
  6623. begin
  6624. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6625. end;
  6626. end;
  6627. if ( aSign<>0 ) then
  6628. z := - z;
  6629. result := z;
  6630. end;
  6631. {*----------------------------------------------------------------------------
  6632. | Returns the result of converting the quadruple-precision floating-point
  6633. | value `a' to the single-precision floating-point format. The conversion
  6634. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6635. | Arithmetic.
  6636. *----------------------------------------------------------------------------*}
  6637. function float128_to_float32(a: float128): float32;
  6638. var
  6639. aSign: flag;
  6640. aExp: int32;
  6641. aSig0, aSig1: bits64;
  6642. zSig: bits32;
  6643. begin
  6644. aSig1 := extractFloat128Frac1( a );
  6645. aSig0 := extractFloat128Frac0( a );
  6646. aExp := extractFloat128Exp( a );
  6647. aSign := extractFloat128Sign( a );
  6648. if ( aExp = $7FFF ) then
  6649. begin
  6650. if ( aSig0 or aSig1 )<>0 then
  6651. begin
  6652. result := commonNaNToFloat32( float128ToCommonNaN( a ) );
  6653. exit;
  6654. end;
  6655. result := packFloat32( aSign, $FF, 0 );
  6656. exit;
  6657. end;
  6658. aSig0 := aSig0 or ord( aSig1 <> 0 );
  6659. shift64RightJamming( aSig0, 18, aSig0 );
  6660. zSig := aSig0;
  6661. if ( aExp or zSig )<>0 then
  6662. begin
  6663. zSig := zSig or $40000000;
  6664. dec(aExp,$3F81);
  6665. end;
  6666. result := roundAndPackFloat32( aSign, aExp, zSig );
  6667. end;
  6668. {*----------------------------------------------------------------------------
  6669. | Returns the result of converting the quadruple-precision floating-point
  6670. | value `a' to the double-precision floating-point format. The conversion
  6671. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  6672. | Arithmetic.
  6673. *----------------------------------------------------------------------------*}
  6674. function float128_to_float64(a: float128): float64;
  6675. var
  6676. aSign: flag;
  6677. aExp: int32;
  6678. aSig0, aSig1: bits64;
  6679. begin
  6680. aSig1 := extractFloat128Frac1( a );
  6681. aSig0 := extractFloat128Frac0( a );
  6682. aExp := extractFloat128Exp( a );
  6683. aSign := extractFloat128Sign( a );
  6684. if ( aExp = $7FFF ) then
  6685. begin
  6686. if ( aSig0 or aSig1 )<>0 then
  6687. begin
  6688. commonNaNToFloat64( float128ToCommonNaN( a ),result);
  6689. exit;
  6690. end;
  6691. result:=packFloat64( aSign, $7FF, 0);
  6692. exit;
  6693. end;
  6694. shortShift128Left( aSig0, aSig1, 14, aSig0, aSig1 );
  6695. aSig0 := aSig0 or ord( aSig1 <> 0 );
  6696. if ( aExp or aSig0 )<>0 then
  6697. begin
  6698. aSig0 := aSig0 or int64( $4000000000000000 );
  6699. dec(aExp,$3C01);
  6700. end;
  6701. result := roundAndPackFloat64( aSign, aExp, aSig0 );
  6702. end;
  6703. {$ifdef FPC_SOFTFLOAT_FLOATX80}
  6704. {*----------------------------------------------------------------------------
  6705. | Returns the result of converting the quadruple-precision floating-point
  6706. | value `a' to the extended double-precision floating-point format. The
  6707. | conversion is performed according to the IEC/IEEE Standard for Binary
  6708. | Floating-Point Arithmetic.
  6709. *----------------------------------------------------------------------------*}
  6710. function float128_to_floatx80(a: float128): floatx80;
  6711. var
  6712. aSign: flag;
  6713. aExp: int32;
  6714. aSig0, aSig1: bits64;
  6715. begin
  6716. aSig1 := extractFloat128Frac1( a );
  6717. aSig0 := extractFloat128Frac0( a );
  6718. aExp := extractFloat128Exp( a );
  6719. aSign := extractFloat128Sign( a );
  6720. if ( aExp = $7FFF ) begin
  6721. if ( aSig0 or aSig1 ) begin
  6722. result := commonNaNToFloatx80( float128ToCommonNaN( a ) );
  6723. end;
  6724. result := packFloatx80( aSign, $7FFF, int64( $8000000000000000 ) );
  6725. end;
  6726. if ( aExp = 0 ) begin
  6727. if ( ( aSig0 or aSig1 ) = 0 ) result := packFloatx80( aSign, 0, 0 );
  6728. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  6729. end;
  6730. else begin
  6731. aSig0 or= int64( $0001000000000000 );
  6732. end;
  6733. shortShift128Left( aSig0, aSig1, 15, &aSig0, &aSig1 );
  6734. result := roundAndPackFloatx80( 80, aSign, aExp, aSig0, aSig1 );
  6735. end;
  6736. {$endif FPC_SOFTFLOAT_FLOATX80}
  6737. {*----------------------------------------------------------------------------
  6738. | Rounds the quadruple-precision floating-point value `a' to an integer, and
  6739. | Returns the result as a quadruple-precision floating-point value. The
  6740. | operation is performed according to the IEC/IEEE Standard for Binary
  6741. | Floating-Point Arithmetic.
  6742. *----------------------------------------------------------------------------*}
  6743. function float128_round_to_int(a: float128): float128;
  6744. var
  6745. aSign: flag;
  6746. aExp: int32;
  6747. lastBitMask, roundBitsMask: bits64;
  6748. roundingMode: int8;
  6749. z: float128;
  6750. begin
  6751. aExp := extractFloat128Exp( a );
  6752. if ( $402F <= aExp ) then
  6753. begin
  6754. if ( $406F <= aExp ) then
  6755. begin
  6756. if ( ( aExp = $7FFF )
  6757. and (( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) )<>0)
  6758. ) then
  6759. begin
  6760. result := propagateFloat128NaN( a, a );
  6761. exit;
  6762. end;
  6763. result := a;
  6764. exit;
  6765. end;
  6766. lastBitMask := 1;
  6767. lastBitMask := ( lastBitMask shl ( $406E - aExp ) ) shl 1;
  6768. roundBitsMask := lastBitMask - 1;
  6769. z := a;
  6770. roundingMode := float_rounding_mode;
  6771. if ( roundingMode = float_round_nearest_even ) then
  6772. begin
  6773. if ( lastBitMask )<>0 then
  6774. begin
  6775. add128( z.high, z.low, 0, lastBitMask shr 1, z.high, z.low );
  6776. if ( ( z.low and roundBitsMask ) = 0 ) then
  6777. z.low := z.low and not(lastBitMask);
  6778. end
  6779. else begin
  6780. if ( sbits64(z.low) < 0 ) then
  6781. begin
  6782. inc(z.high);
  6783. if ( bits64( z.low shl 1 ) = 0 ) then
  6784. z.high := z.high and not(1);
  6785. end;
  6786. end;
  6787. end
  6788. else if ( roundingMode <> float_round_to_zero ) then
  6789. begin
  6790. if ( extractFloat128Sign( z )
  6791. xor ord( roundingMode = float_round_up ) )<>0 then
  6792. begin
  6793. add128( z.high, z.low, 0, roundBitsMask, z.high, z.low );
  6794. end;
  6795. end;
  6796. z.low := z.low and not(roundBitsMask);
  6797. end
  6798. else begin
  6799. if ( aExp < $3FFF ) then
  6800. begin
  6801. if ( ( ( bits64( a.high shl 1 ) ) or a.low ) = 0 ) then
  6802. begin
  6803. result := a;
  6804. exit;
  6805. end;
  6806. softfloat_exception_flags := softfloat_exception_flags or float_flag_inexact;
  6807. aSign := extractFloat128Sign( a );
  6808. case float_rounding_mode of
  6809. float_round_nearest_even:
  6810. if ( ( aExp = $3FFE )
  6811. and ( extractFloat128Frac0( a )
  6812. or extractFloat128Frac1( a ) )
  6813. ) begin
  6814. begin
  6815. result := packFloat128( aSign, $3FFF, 0, 0 );
  6816. exit;
  6817. end;
  6818. end;
  6819. float_round_down:
  6820. begin
  6821. result :=
  6822. aSign ? packFloat128( 1, $3FFF, 0, 0 )
  6823. : packFloat128( 0, 0, 0, 0 );
  6824. end;
  6825. float_round_up:
  6826. begin
  6827. result :=
  6828. aSign ? packFloat128( 1, 0, 0, 0 )
  6829. : packFloat128( 0, $3FFF, 0, 0 );
  6830. exit;
  6831. end;
  6832. end;
  6833. result := packFloat128( aSign, 0, 0, 0 );
  6834. exit;
  6835. end;
  6836. lastBitMask := 1;
  6837. lastBitMask shl = $402F - aExp;
  6838. roundBitsMask := lastBitMask - 1;
  6839. z.low := 0;
  6840. z.high := a.high;
  6841. roundingMode := float_rounding_mode;
  6842. if ( roundingMode = float_round_nearest_even ) begin
  6843. z.high += lastBitMask>>1;
  6844. if ( ( ( z.high and roundBitsMask ) or a.low ) = 0 ) begin
  6845. z.high &= ~ lastBitMask;
  6846. end;
  6847. end;
  6848. else if ( roundingMode <> float_round_to_zero ) begin
  6849. if ( extractFloat128Sign( z )
  6850. xor ( roundingMode = float_round_up ) ) begin
  6851. z.high or= ( a.low <> 0 );
  6852. z.high += roundBitsMask;
  6853. end;
  6854. end;
  6855. z.high &= ~ roundBitsMask;
  6856. end;
  6857. if ( ( z.low <> a.low ) or ( z.high <> a.high ) ) begin
  6858. softfloat_exception_flags or= float_flag_inexact;
  6859. end;
  6860. result := z;
  6861. end;
  6862. {*----------------------------------------------------------------------------
  6863. | Returns the result of adding the absolute values of the quadruple-precision
  6864. | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated
  6865. | before being returned. `zSign' is ignored if the result is a NaN.
  6866. | The addition is performed according to the IEC/IEEE Standard for Binary
  6867. | Floating-Point Arithmetic.
  6868. *----------------------------------------------------------------------------*}
  6869. function addFloat128Sigs( float128 a, float128 b, flag zSign ): float128;
  6870. var
  6871. aExp, bExp, zExp: int32;
  6872. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2: bits64;
  6873. expDiff: int32;
  6874. begin
  6875. aSig1 := extractFloat128Frac1( a );
  6876. aSig0 := extractFloat128Frac0( a );
  6877. aExp := extractFloat128Exp( a );
  6878. bSig1 := extractFloat128Frac1( b );
  6879. bSig0 := extractFloat128Frac0( b );
  6880. bExp := extractFloat128Exp( b );
  6881. expDiff := aExp - bExp;
  6882. if ( 0 < expDiff ) begin
  6883. if ( aExp = $7FFF ) begin
  6884. if ( aSig0 or aSig1 ) result := propagateFloat128NaN( a, b );
  6885. result := a;
  6886. end;
  6887. if ( bExp = 0 ) begin
  6888. --expDiff;
  6889. end;
  6890. else begin
  6891. bSig0 or= int64( $0001000000000000 );
  6892. end;
  6893. shift128ExtraRightJamming(
  6894. bSig0, bSig1, 0, expDiff, &bSig0, &bSig1, &zSig2 );
  6895. zExp := aExp;
  6896. end;
  6897. else if ( expDiff < 0 ) begin
  6898. if ( bExp = $7FFF ) begin
  6899. if ( bSig0 or bSig1 ) result := propagateFloat128NaN( a, b );
  6900. result := packFloat128( zSign, $7FFF, 0, 0 );
  6901. end;
  6902. if ( aExp = 0 ) begin
  6903. ++expDiff;
  6904. end;
  6905. else begin
  6906. aSig0 or= int64( $0001000000000000 );
  6907. end;
  6908. shift128ExtraRightJamming(
  6909. aSig0, aSig1, 0, - expDiff, &aSig0, &aSig1, &zSig2 );
  6910. zExp := bExp;
  6911. end;
  6912. else begin
  6913. if ( aExp = $7FFF ) begin
  6914. if ( aSig0 or aSig1 or bSig0 or bSig1 ) begin
  6915. result := propagateFloat128NaN( a, b );
  6916. end;
  6917. result := a;
  6918. end;
  6919. add128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
  6920. if ( aExp = 0 ) result := packFloat128( zSign, 0, zSig0, zSig1 );
  6921. zSig2 := 0;
  6922. zSig0 or= int64( $0002000000000000 );
  6923. zExp := aExp;
  6924. goto shiftRight1;
  6925. end;
  6926. aSig0 or= int64( $0001000000000000 );
  6927. add128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
  6928. --zExp;
  6929. if ( zSig0 < int64( $0002000000000000 ) ) goto roundAndPack;
  6930. ++zExp;
  6931. shiftRight1:
  6932. shift128ExtraRightJamming(
  6933. zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 );
  6934. roundAndPack:
  6935. result := roundAndPackFloat128( zSign, zExp, zSig0, zSig1, zSig2 );
  6936. end;
  6937. {*----------------------------------------------------------------------------
  6938. | Returns the result of subtracting the absolute values of the quadruple-
  6939. | precision floating-point values `a' and `b'. If `zSign' is 1, the
  6940. | difference is negated before being returned. `zSign' is ignored if the
  6941. | result is a NaN. The subtraction is performed according to the IEC/IEEE
  6942. | Standard for Binary Floating-Point Arithmetic.
  6943. *----------------------------------------------------------------------------*}
  6944. function subFloat128Sigs( float128 a, float128 b, flag zSign ): float128;
  6945. var
  6946. aExp, bExp, zExp: int32;
  6947. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1: bits64;
  6948. expDiff: int32;
  6949. z: float128;
  6950. begin
  6951. aSig1 := extractFloat128Frac1( a );
  6952. aSig0 := extractFloat128Frac0( a );
  6953. aExp := extractFloat128Exp( a );
  6954. bSig1 := extractFloat128Frac1( b );
  6955. bSig0 := extractFloat128Frac0( b );
  6956. bExp := extractFloat128Exp( b );
  6957. expDiff := aExp - bExp;
  6958. shortShift128Left( aSig0, aSig1, 14, &aSig0, &aSig1 );
  6959. shortShift128Left( bSig0, bSig1, 14, &bSig0, &bSig1 );
  6960. if ( 0 < expDiff ) goto aExpBigger;
  6961. if ( expDiff < 0 ) goto bExpBigger;
  6962. if ( aExp = $7FFF ) begin
  6963. if ( aSig0 or aSig1 or bSig0 or bSig1 ) begin
  6964. result := propagateFloat128NaN( a, b );
  6965. end;
  6966. float_raise( float_flag_invalid );
  6967. z.low := float128_default_nan_low;
  6968. z.high := float128_default_nan_high;
  6969. result := z;
  6970. end;
  6971. if ( aExp = 0 ) begin
  6972. aExp := 1;
  6973. bExp := 1;
  6974. end;
  6975. if ( bSig0 < aSig0 ) goto aBigger;
  6976. if ( aSig0 < bSig0 ) goto bBigger;
  6977. if ( bSig1 < aSig1 ) goto aBigger;
  6978. if ( aSig1 < bSig1 ) goto bBigger;
  6979. result := packFloat128( float_rounding_mode = float_round_down, 0, 0, 0 );
  6980. bExpBigger:
  6981. if ( bExp = $7FFF ) begin
  6982. if ( bSig0 or bSig1 ) result := propagateFloat128NaN( a, b );
  6983. result := packFloat128( zSign xor 1, $7FFF, 0, 0 );
  6984. end;
  6985. if ( aExp = 0 ) begin
  6986. ++expDiff;
  6987. end;
  6988. else begin
  6989. aSig0 or= int64( $4000000000000000 );
  6990. end;
  6991. shift128RightJamming( aSig0, aSig1, - expDiff, &aSig0, &aSig1 );
  6992. bSig0 or= int64( $4000000000000000 );
  6993. bBigger:
  6994. sub128( bSig0, bSig1, aSig0, aSig1, &zSig0, &zSig1 );
  6995. zExp := bExp;
  6996. zSign xor = 1;
  6997. goto normalizeRoundAndPack;
  6998. aExpBigger:
  6999. if ( aExp = $7FFF ) begin
  7000. if ( aSig0 or aSig1 ) result := propagateFloat128NaN( a, b );
  7001. result := a;
  7002. end;
  7003. if ( bExp = 0 ) begin
  7004. --expDiff;
  7005. end;
  7006. else begin
  7007. bSig0 or= int64( $4000000000000000 );
  7008. end;
  7009. shift128RightJamming( bSig0, bSig1, expDiff, &bSig0, &bSig1 );
  7010. aSig0 or= int64( $4000000000000000 );
  7011. aBigger:
  7012. sub128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 );
  7013. zExp := aExp;
  7014. normalizeRoundAndPack:
  7015. --zExp;
  7016. result := normalizeRoundAndPackFloat128( zSign, zExp - 14, zSig0, zSig1 );
  7017. end;
  7018. {*----------------------------------------------------------------------------
  7019. | Returns the result of adding the quadruple-precision floating-point values
  7020. | `a' and `b'. The operation is performed according to the IEC/IEEE Standard
  7021. | for Binary Floating-Point Arithmetic.
  7022. *----------------------------------------------------------------------------*}
  7023. function float128_add(a: float128; b: float128): float128;
  7024. var
  7025. aSign, bSign: flag;
  7026. begin
  7027. aSign := extractFloat128Sign( a );
  7028. bSign := extractFloat128Sign( b );
  7029. if ( aSign = bSign ) begin
  7030. result := addFloat128Sigs( a, b, aSign );
  7031. end;
  7032. else begin
  7033. result := subFloat128Sigs( a, b, aSign );
  7034. end;
  7035. end;
  7036. {*----------------------------------------------------------------------------
  7037. | Returns the result of subtracting the quadruple-precision floating-point
  7038. | values `a' and `b'. The operation is performed according to the IEC/IEEE
  7039. | Standard for Binary Floating-Point Arithmetic.
  7040. *----------------------------------------------------------------------------*}
  7041. function float128_sub(a: float128; b: float128): float128;
  7042. var
  7043. aSign, bSign: flag;
  7044. begin
  7045. aSign := extractFloat128Sign( a );
  7046. bSign := extractFloat128Sign( b );
  7047. if ( aSign = bSign ) begin
  7048. result := subFloat128Sigs( a, b, aSign );
  7049. end;
  7050. else begin
  7051. result := addFloat128Sigs( a, b, aSign );
  7052. end;
  7053. end;
  7054. {*----------------------------------------------------------------------------
  7055. | Returns the result of multiplying the quadruple-precision floating-point
  7056. | values `a' and `b'. The operation is performed according to the IEC/IEEE
  7057. | Standard for Binary Floating-Point Arithmetic.
  7058. *----------------------------------------------------------------------------*}
  7059. function float128_mul(a: float128; b: float128): float128;
  7060. var
  7061. aSign, bSign, zSign: flag;
  7062. aExp, bExp, zExp: int32;
  7063. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2, zSig3: bits64;
  7064. z: float128;
  7065. begin
  7066. aSig1 := extractFloat128Frac1( a );
  7067. aSig0 := extractFloat128Frac0( a );
  7068. aExp := extractFloat128Exp( a );
  7069. aSign := extractFloat128Sign( a );
  7070. bSig1 := extractFloat128Frac1( b );
  7071. bSig0 := extractFloat128Frac0( b );
  7072. bExp := extractFloat128Exp( b );
  7073. bSign := extractFloat128Sign( b );
  7074. zSign := aSign xor bSign;
  7075. if ( aExp = $7FFF ) begin
  7076. if ( ( aSig0 or aSig1 )
  7077. or ( ( bExp = $7FFF ) and ( bSig0 or bSig1 ) ) ) begin
  7078. result := propagateFloat128NaN( a, b );
  7079. end;
  7080. if ( ( bExp or bSig0 or bSig1 ) = 0 ) goto invalid;
  7081. result := packFloat128( zSign, $7FFF, 0, 0 );
  7082. end;
  7083. if ( bExp = $7FFF ) begin
  7084. if ( bSig0 or bSig1 ) result := propagateFloat128NaN( a, b );
  7085. if ( ( aExp or aSig0 or aSig1 ) = 0 ) begin
  7086. invalid:
  7087. float_raise( float_flag_invalid );
  7088. z.low := float128_default_nan_low;
  7089. z.high := float128_default_nan_high;
  7090. result := z;
  7091. end;
  7092. result := packFloat128( zSign, $7FFF, 0, 0 );
  7093. end;
  7094. if ( aExp = 0 ) begin
  7095. if ( ( aSig0 or aSig1 ) = 0 ) result := packFloat128( zSign, 0, 0, 0 );
  7096. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  7097. end;
  7098. if ( bExp = 0 ) begin
  7099. if ( ( bSig0 or bSig1 ) = 0 ) result := packFloat128( zSign, 0, 0, 0 );
  7100. normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
  7101. end;
  7102. zExp := aExp + bExp - $4000;
  7103. aSig0 or= int64( $0001000000000000 );
  7104. shortShift128Left( bSig0, bSig1, 16, &bSig0, &bSig1 );
  7105. mul128To256( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1, &zSig2, &zSig3 );
  7106. add128( zSig0, zSig1, aSig0, aSig1, &zSig0, &zSig1 );
  7107. zSig2 or= ( zSig3 <> 0 );
  7108. if ( int64( $0002000000000000 ) <= zSig0 ) begin
  7109. shift128ExtraRightJamming(
  7110. zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 );
  7111. ++zExp;
  7112. end;
  7113. result := roundAndPackFloat128( zSign, zExp, zSig0, zSig1, zSig2 );
  7114. end;
  7115. {*----------------------------------------------------------------------------
  7116. | Returns the result of dividing the quadruple-precision floating-point value
  7117. | `a' by the corresponding value `b'. The operation is performed according to
  7118. | the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  7119. *----------------------------------------------------------------------------*}
  7120. function float128_div(a: float128; b: float128): float128;
  7121. var
  7122. aSign, bSign, zSign: flag;
  7123. aExp, bExp, zExp: int32;
  7124. aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2: bits64;
  7125. rem0, rem1, rem2, rem3, term0, term1, term2, term3: bits64;
  7126. z: float128;
  7127. begin
  7128. aSig1 := extractFloat128Frac1( a );
  7129. aSig0 := extractFloat128Frac0( a );
  7130. aExp := extractFloat128Exp( a );
  7131. aSign := extractFloat128Sign( a );
  7132. bSig1 := extractFloat128Frac1( b );
  7133. bSig0 := extractFloat128Frac0( b );
  7134. bExp := extractFloat128Exp( b );
  7135. bSign := extractFloat128Sign( b );
  7136. zSign := aSign xor bSign;
  7137. if ( aExp = $7FFF ) begin
  7138. if ( aSig0 or aSig1 ) result := propagateFloat128NaN( a, b );
  7139. if ( bExp = $7FFF ) begin
  7140. if ( bSig0 or bSig1 ) result := propagateFloat128NaN( a, b );
  7141. goto invalid;
  7142. end;
  7143. result := packFloat128( zSign, $7FFF, 0, 0 );
  7144. end;
  7145. if ( bExp = $7FFF ) begin
  7146. if ( bSig0 or bSig1 ) result := propagateFloat128NaN( a, b );
  7147. result := packFloat128( zSign, 0, 0, 0 );
  7148. end;
  7149. if ( bExp = 0 ) begin
  7150. if ( ( bSig0 or bSig1 ) = 0 ) begin
  7151. if ( ( aExp or aSig0 or aSig1 ) = 0 ) begin
  7152. invalid:
  7153. float_raise( float_flag_invalid );
  7154. z.low := float128_default_nan_low;
  7155. z.high := float128_default_nan_high;
  7156. result := z;
  7157. end;
  7158. float_raise( float_flag_divbyzero );
  7159. result := packFloat128( zSign, $7FFF, 0, 0 );
  7160. end;
  7161. normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
  7162. end;
  7163. if ( aExp = 0 ) begin
  7164. if ( ( aSig0 or aSig1 ) = 0 ) result := packFloat128( zSign, 0, 0, 0 );
  7165. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  7166. end;
  7167. zExp := aExp - bExp + $3FFD;
  7168. shortShift128Left(
  7169. aSig0 or int64( $0001000000000000 ), aSig1, 15, &aSig0, &aSig1 );
  7170. shortShift128Left(
  7171. bSig0 or int64( $0001000000000000 ), bSig1, 15, &bSig0, &bSig1 );
  7172. if ( le128( bSig0, bSig1, aSig0, aSig1 ) ) begin
  7173. shift128Right( aSig0, aSig1, 1, &aSig0, &aSig1 );
  7174. ++zExp;
  7175. end;
  7176. zSig0 := estimateDiv128To64( aSig0, aSig1, bSig0 );
  7177. mul128By64To192( bSig0, bSig1, zSig0, &term0, &term1, &term2 );
  7178. sub192( aSig0, aSig1, 0, term0, term1, term2, &rem0, &rem1, &rem2 );
  7179. while ( (sbits64) rem0 < 0 ) begin
  7180. --zSig0;
  7181. add192( rem0, rem1, rem2, 0, bSig0, bSig1, &rem0, &rem1, &rem2 );
  7182. end;
  7183. zSig1 := estimateDiv128To64( rem1, rem2, bSig0 );
  7184. if ( ( zSig1 and $3FFF ) <= 4 ) begin
  7185. mul128By64To192( bSig0, bSig1, zSig1, &term1, &term2, &term3 );
  7186. sub192( rem1, rem2, 0, term1, term2, term3, &rem1, &rem2, &rem3 );
  7187. while ( (sbits64) rem1 < 0 ) begin
  7188. --zSig1;
  7189. add192( rem1, rem2, rem3, 0, bSig0, bSig1, &rem1, &rem2, &rem3 );
  7190. end;
  7191. zSig1 or= ( ( rem1 or rem2 or rem3 ) <> 0 );
  7192. end;
  7193. shift128ExtraRightJamming( zSig0, zSig1, 0, 15, &zSig0, &zSig1, &zSig2 );
  7194. result := roundAndPackFloat128( zSign, zExp, zSig0, zSig1, zSig2 );
  7195. end;
  7196. {*----------------------------------------------------------------------------
  7197. | Returns the remainder of the quadruple-precision floating-point value `a'
  7198. | with respect to the corresponding value `b'. The operation is performed
  7199. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  7200. *----------------------------------------------------------------------------*}
  7201. function float128_rem(a: float128; b: float128): float128;
  7202. var
  7203. aSign, bSign, zSign: flag;
  7204. aExp, bExp, expDiff: int32;
  7205. aSig0, aSig1, bSig0, bSig1, q, term0, term1, term2: bits64;
  7206. allZero, alternateASig0, alternateASig1, sigMean1: bits64;
  7207. sigMean0: sbits64;
  7208. z: float128;
  7209. begin
  7210. aSig1 := extractFloat128Frac1( a );
  7211. aSig0 := extractFloat128Frac0( a );
  7212. aExp := extractFloat128Exp( a );
  7213. aSign := extractFloat128Sign( a );
  7214. bSig1 := extractFloat128Frac1( b );
  7215. bSig0 := extractFloat128Frac0( b );
  7216. bExp := extractFloat128Exp( b );
  7217. bSign := extractFloat128Sign( b );
  7218. if ( aExp = $7FFF ) begin
  7219. if ( ( aSig0 or aSig1 )
  7220. or ( ( bExp = $7FFF ) and ( bSig0 or bSig1 ) ) ) begin
  7221. result := propagateFloat128NaN( a, b );
  7222. end;
  7223. goto invalid;
  7224. end;
  7225. if ( bExp = $7FFF ) begin
  7226. if ( bSig0 or bSig1 ) result := propagateFloat128NaN( a, b );
  7227. result := a;
  7228. end;
  7229. if ( bExp = 0 ) begin
  7230. if ( ( bSig0 or bSig1 ) = 0 ) begin
  7231. invalid:
  7232. float_raise( float_flag_invalid );
  7233. z.low := float128_default_nan_low;
  7234. z.high := float128_default_nan_high;
  7235. result := z;
  7236. end;
  7237. normalizeFloat128Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 );
  7238. end;
  7239. if ( aExp = 0 ) begin
  7240. if ( ( aSig0 or aSig1 ) = 0 ) result := a;
  7241. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  7242. end;
  7243. expDiff := aExp - bExp;
  7244. if ( expDiff < -1 ) result := a;
  7245. shortShift128Left(
  7246. aSig0 or int64( $0001000000000000 ),
  7247. aSig1,
  7248. 15 - ( expDiff < 0 ),
  7249. &aSig0,
  7250. &aSig1
  7251. );
  7252. shortShift128Left(
  7253. bSig0 or int64( $0001000000000000 ), bSig1, 15, &bSig0, &bSig1 );
  7254. q := le128( bSig0, bSig1, aSig0, aSig1 );
  7255. if ( q ) sub128( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 );
  7256. expDiff -= 64;
  7257. while ( 0 < expDiff ) begin
  7258. q := estimateDiv128To64( aSig0, aSig1, bSig0 );
  7259. q := ( 4 < q ) ? q - 4 : 0;
  7260. mul128By64To192( bSig0, bSig1, q, &term0, &term1, &term2 );
  7261. shortShift192Left( term0, term1, term2, 61, &term1, &term2, &allZero );
  7262. shortShift128Left( aSig0, aSig1, 61, &aSig0, &allZero );
  7263. sub128( aSig0, 0, term1, term2, &aSig0, &aSig1 );
  7264. expDiff -= 61;
  7265. end;
  7266. if ( -64 < expDiff ) begin
  7267. q := estimateDiv128To64( aSig0, aSig1, bSig0 );
  7268. q := ( 4 < q ) ? q - 4 : 0;
  7269. q >>= - expDiff;
  7270. shift128Right( bSig0, bSig1, 12, &bSig0, &bSig1 );
  7271. expDiff += 52;
  7272. if ( expDiff < 0 ) begin
  7273. shift128Right( aSig0, aSig1, - expDiff, &aSig0, &aSig1 );
  7274. end;
  7275. else begin
  7276. shortShift128Left( aSig0, aSig1, expDiff, &aSig0, &aSig1 );
  7277. end;
  7278. mul128By64To192( bSig0, bSig1, q, &term0, &term1, &term2 );
  7279. sub128( aSig0, aSig1, term1, term2, &aSig0, &aSig1 );
  7280. end;
  7281. else begin
  7282. shift128Right( aSig0, aSig1, 12, &aSig0, &aSig1 );
  7283. shift128Right( bSig0, bSig1, 12, &bSig0, &bSig1 );
  7284. end;
  7285. do begin
  7286. alternateASig0 := aSig0;
  7287. alternateASig1 := aSig1;
  7288. ++q;
  7289. sub128( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 );
  7290. end; while ( 0 <= (sbits64) aSig0 );
  7291. add128(
  7292. aSig0, aSig1, alternateASig0, alternateASig1, &sigMean0, &sigMean1 );
  7293. if ( ( sigMean0 < 0 )
  7294. or ( ( ( sigMean0 or sigMean1 ) = 0 ) and ( q and 1 ) ) ) begin
  7295. aSig0 := alternateASig0;
  7296. aSig1 := alternateASig1;
  7297. end;
  7298. zSign := ( (sbits64) aSig0 < 0 );
  7299. if ( zSign ) sub128( 0, 0, aSig0, aSig1, &aSig0, &aSig1 );
  7300. result :=
  7301. normalizeRoundAndPackFloat128( aSign xor zSign, bExp - 4, aSig0, aSig1 );
  7302. end;
  7303. {*----------------------------------------------------------------------------
  7304. | Returns the square root of the quadruple-precision floating-point value `a'.
  7305. | The operation is performed according to the IEC/IEEE Standard for Binary
  7306. | Floating-Point Arithmetic.
  7307. *----------------------------------------------------------------------------*}
  7308. function float128_sqrt(a: float128): float128;
  7309. var
  7310. aSign: flag;
  7311. aExp, zExp: int32;
  7312. aSig0, aSig1, zSig0, zSig1, zSig2, doubleZSig0: bits64;
  7313. rem0, rem1, rem2, rem3, term0, term1, term2, term3: bits64;
  7314. z: float128;
  7315. label
  7316. invalid;
  7317. begin
  7318. aSig1 := extractFloat128Frac1( a );
  7319. aSig0 := extractFloat128Frac0( a );
  7320. aExp := extractFloat128Exp( a );
  7321. aSign := extractFloat128Sign( a );
  7322. if ( aExp = $7FFF ) begin
  7323. if ( aSig0 or aSig1 ) result := propagateFloat128NaN( a, a );
  7324. if ( ! aSign ) result := a;
  7325. goto invalid;
  7326. end;
  7327. if ( aSign ) begin
  7328. if ( ( aExp or aSig0 or aSig1 ) = 0 ) result := a;
  7329. invalid:
  7330. float_raise( float_flag_invalid );
  7331. z.low := float128_default_nan_low;
  7332. z.high := float128_default_nan_high;
  7333. result := z;
  7334. end;
  7335. if ( aExp = 0 ) begin
  7336. if ( ( aSig0 or aSig1 ) = 0 ) result := packFloat128( 0, 0, 0, 0 );
  7337. normalizeFloat128Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 );
  7338. end;
  7339. zExp := ( ( aExp - $3FFF )>>1 ) + $3FFE;
  7340. aSig0 := aSig0 or int64( $0001000000000000 );
  7341. zSig0 := estimateSqrt32( aExp, aSig0>>17 );
  7342. shortShift128Left( aSig0, aSig1, 13 - ( aExp and 1 ), &aSig0, &aSig1 );
  7343. zSig0 := estimateDiv128To64( aSig0, aSig1, zSig0 shl 32 ) + ( zSig0 shl 30 );
  7344. doubleZSig0 := zSig0 shl 1;
  7345. mul64To128( zSig0, zSig0, &term0, &term1 );
  7346. sub128( aSig0, aSig1, term0, term1, &rem0, &rem1 );
  7347. while ( (sbits64) rem0 < 0 ) begin
  7348. --zSig0;
  7349. doubleZSig0 -= 2;
  7350. add128( rem0, rem1, zSig0>>63, doubleZSig0 or 1, &rem0, &rem1 );
  7351. end;
  7352. zSig1 := estimateDiv128To64( rem1, 0, doubleZSig0 );
  7353. if ( ( zSig1 and $1FFF ) <= 5 ) begin
  7354. if ( zSig1 = 0 ) zSig1 := 1;
  7355. mul64To128( doubleZSig0, zSig1, &term1, &term2 );
  7356. sub128( rem1, 0, term1, term2, &rem1, &rem2 );
  7357. mul64To128( zSig1, zSig1, &term2, &term3 );
  7358. sub192( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 );
  7359. while ( (sbits64) rem1 < 0 ) begin
  7360. --zSig1;
  7361. shortShift128Left( 0, zSig1, 1, &term2, &term3 );
  7362. term3 or= 1;
  7363. term2 or= doubleZSig0;
  7364. add192( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 );
  7365. end;
  7366. zSig1 or= ( ( rem1 or rem2 or rem3 ) <> 0 );
  7367. end;
  7368. shift128ExtraRightJamming( zSig0, zSig1, 0, 14, &zSig0, &zSig1, &zSig2 );
  7369. result := roundAndPackFloat128( 0, zExp, zSig0, zSig1, zSig2 );
  7370. end;
  7371. {*----------------------------------------------------------------------------
  7372. | Returns 1 if the quadruple-precision floating-point value `a' is equal to
  7373. | the corresponding value `b', and 0 otherwise. The comparison is performed
  7374. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  7375. *----------------------------------------------------------------------------*}
  7376. function float128_eq(a: float128; b: float128): flag;
  7377. begin
  7378. if ( ( ( extractFloat128Exp( a ) = $7FFF )
  7379. and ( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) ) )
  7380. or ( ( extractFloat128Exp( b ) = $7FFF )
  7381. and ( extractFloat128Frac0( b ) or extractFloat128Frac1( b ) ) )
  7382. ) begin
  7383. if ( float128_is_signaling_nan( a )
  7384. or float128_is_signaling_nan( b ) ) begin
  7385. float_raise( float_flag_invalid );
  7386. end;
  7387. result := 0;
  7388. end;
  7389. result :=
  7390. ( a.low = b.low )
  7391. and ( ( a.high = b.high )
  7392. or ( ( a.low = 0 )
  7393. and ( (bits64) ( ( a.high or b.high ) shl 1 ) = 0 ) )
  7394. );
  7395. end;
  7396. {*----------------------------------------------------------------------------
  7397. | Returns 1 if the quadruple-precision floating-point value `a' is less than
  7398. | or equal to the corresponding value `b', and 0 otherwise. The comparison
  7399. | is performed according to the IEC/IEEE Standard for Binary Floating-Point
  7400. | Arithmetic.
  7401. *----------------------------------------------------------------------------*}
  7402. function float128_le(a: float128; b: float128): flag;
  7403. var
  7404. aSign, bSign: flag;
  7405. begin
  7406. if ( ( ( extractFloat128Exp( a ) = $7FFF )
  7407. and ( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) ) )
  7408. or ( ( extractFloat128Exp( b ) = $7FFF )
  7409. and ( extractFloat128Frac0( b ) or extractFloat128Frac1( b ) ) )
  7410. ) begin
  7411. float_raise( float_flag_invalid );
  7412. result := 0;
  7413. end;
  7414. aSign := extractFloat128Sign( a );
  7415. bSign := extractFloat128Sign( b );
  7416. if ( aSign <> bSign ) begin
  7417. result :=
  7418. aSign
  7419. or ( ( ( (bits64) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  7420. = 0 );
  7421. end;
  7422. result :=
  7423. aSign ? le128( b.high, b.low, a.high, a.low )
  7424. : le128( a.high, a.low, b.high, b.low );
  7425. end;
  7426. {*----------------------------------------------------------------------------
  7427. | Returns 1 if the quadruple-precision floating-point value `a' is less than
  7428. | the corresponding value `b', and 0 otherwise. The comparison is performed
  7429. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  7430. *----------------------------------------------------------------------------*}
  7431. function float128_lt(a: float128; b: float128): flag;
  7432. var
  7433. aSign, bSign: flag;
  7434. begin
  7435. if ( ( ( extractFloat128Exp( a ) = $7FFF )
  7436. and ( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) ) )
  7437. or ( ( extractFloat128Exp( b ) = $7FFF )
  7438. and ( extractFloat128Frac0( b ) or extractFloat128Frac1( b ) ) )
  7439. ) begin
  7440. float_raise( float_flag_invalid );
  7441. result := 0;
  7442. end;
  7443. aSign := extractFloat128Sign( a );
  7444. bSign := extractFloat128Sign( b );
  7445. if ( aSign <> bSign ) begin
  7446. result :=
  7447. aSign
  7448. and ( ( ( (bits64) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  7449. <> 0 );
  7450. end;
  7451. result :=
  7452. aSign ? lt128( b.high, b.low, a.high, a.low )
  7453. : lt128( a.high, a.low, b.high, b.low );
  7454. end;
  7455. {*----------------------------------------------------------------------------
  7456. | Returns 1 if the quadruple-precision floating-point value `a' is equal to
  7457. | the corresponding value `b', and 0 otherwise. The invalid exception is
  7458. | raised if either operand is a NaN. Otherwise, the comparison is performed
  7459. | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  7460. *----------------------------------------------------------------------------*}
  7461. function float128_eq_signaling(a: float128; b: float128): flag;
  7462. begin
  7463. if ( ( ( extractFloat128Exp( a ) = $7FFF )
  7464. and ( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) ) )
  7465. or ( ( extractFloat128Exp( b ) = $7FFF )
  7466. and ( extractFloat128Frac0( b ) or extractFloat128Frac1( b ) ) )
  7467. ) begin
  7468. float_raise( float_flag_invalid );
  7469. result := 0;
  7470. end;
  7471. result :=
  7472. ( a.low = b.low )
  7473. and ( ( a.high = b.high )
  7474. or ( ( a.low = 0 )
  7475. and ( (bits64) ( ( a.high or b.high ) shl 1 ) = 0 ) )
  7476. );
  7477. end;
  7478. {*----------------------------------------------------------------------------
  7479. | Returns 1 if the quadruple-precision floating-point value `a' is less than
  7480. | or equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not
  7481. | cause an exception. Otherwise, the comparison is performed according to the
  7482. | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
  7483. *----------------------------------------------------------------------------*}
  7484. function float128_le_quiet(a: float128; b: float128): flag;
  7485. var
  7486. aSign, bSign: flag;
  7487. begin
  7488. if ( ( ( extractFloat128Exp( a ) = $7FFF )
  7489. and ( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) ) )
  7490. or ( ( extractFloat128Exp( b ) = $7FFF )
  7491. and ( extractFloat128Frac0( b ) or extractFloat128Frac1( b ) ) )
  7492. ) begin
  7493. if ( float128_is_signaling_nan( a )
  7494. or float128_is_signaling_nan( b ) ) begin
  7495. float_raise( float_flag_invalid );
  7496. end;
  7497. result := 0;
  7498. end;
  7499. aSign := extractFloat128Sign( a );
  7500. bSign := extractFloat128Sign( b );
  7501. if ( aSign <> bSign ) begin
  7502. result :=
  7503. aSign
  7504. or ( ( ( (bits64) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  7505. = 0 );
  7506. end;
  7507. result :=
  7508. aSign ? le128( b.high, b.low, a.high, a.low )
  7509. : le128( a.high, a.low, b.high, b.low );
  7510. end;
  7511. {*----------------------------------------------------------------------------
  7512. | Returns 1 if the quadruple-precision floating-point value `a' is less than
  7513. | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an
  7514. | exception. Otherwise, the comparison is performed according to the IEC/IEEE
  7515. | Standard for Binary Floating-Point Arithmetic.
  7516. *----------------------------------------------------------------------------*}
  7517. function float128_lt_quiet(a: float128; b: float128): flag;
  7518. var
  7519. aSign, bSign: flag;
  7520. begin
  7521. if ( ( ( extractFloat128Exp( a ) = $7FFF )
  7522. and ( extractFloat128Frac0( a ) or extractFloat128Frac1( a ) ) )
  7523. or ( ( extractFloat128Exp( b ) = $7FFF )
  7524. and ( extractFloat128Frac0( b ) or extractFloat128Frac1( b ) ) )
  7525. ) begin
  7526. if ( float128_is_signaling_nan( a )
  7527. or float128_is_signaling_nan( b ) ) begin
  7528. float_raise( float_flag_invalid );
  7529. end;
  7530. result := 0;
  7531. end;
  7532. aSign := extractFloat128Sign( a );
  7533. bSign := extractFloat128Sign( b );
  7534. if ( aSign <> bSign ) begin
  7535. result :=
  7536. aSign
  7537. and ( ( ( (bits64) ( ( a.high or b.high ) shl 1 ) ) or a.low or b.low )
  7538. <> 0 );
  7539. end;
  7540. result :=
  7541. aSign ? lt128( b.high, b.low, a.high, a.low )
  7542. : lt128( a.high, a.low, b.high, b.low );
  7543. end;
  7544. {$endif FPC_SOFTFLOAT_FLOAT128}
  7545. {$endif not(defined(fpc_softfpu_interface))}
  7546. {$if not(defined(fpc_softfpu_interface)) and not(defined(fpc_softfpu_implementation))}
  7547. end.
  7548. {$endif not(defined(fpc_softfpu_interface)) and not(defined(fpc_softfpu_implementation))}