real.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124
  1. /* real.c - software floating point emulation.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Stephen L. Moshier (moshier@world.std.com).
  4. Re-written by Richard Henderson <rth@redhat.com>
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "tm.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "diagnostic-core.h"
  32. #include "real.h"
  33. #include "realmpfr.h"
  34. #include "tm_p.h"
  35. #include "dfp.h"
  36. #include "wide-int.h"
  37. #include "rtl.h"
  38. #include "options.h"
  39. /* The floating point model used internally is not exactly IEEE 754
  40. compliant, and close to the description in the ISO C99 standard,
  41. section 5.2.4.2.2 Characteristics of floating types.
  42. Specifically
  43. x = s * b^e * \sum_{k=1}^p f_k * b^{-k}
  44. where
  45. s = sign (+- 1)
  46. b = base or radix, here always 2
  47. e = exponent
  48. p = precision (the number of base-b digits in the significand)
  49. f_k = the digits of the significand.
  50. We differ from typical IEEE 754 encodings in that the entire
  51. significand is fractional. Normalized significands are in the
  52. range [0.5, 1.0).
  53. A requirement of the model is that P be larger than the largest
  54. supported target floating-point type by at least 2 bits. This gives
  55. us proper rounding when we truncate to the target type. In addition,
  56. E must be large enough to hold the smallest supported denormal number
  57. in a normalized form.
  58. Both of these requirements are easily satisfied. The largest target
  59. significand is 113 bits; we store at least 160. The smallest
  60. denormal number fits in 17 exponent bits; we store 26. */
  61. /* Used to classify two numbers simultaneously. */
  62. #define CLASS2(A, B) ((A) << 2 | (B))
  63. #if HOST_BITS_PER_LONG != 64 && HOST_BITS_PER_LONG != 32
  64. #error "Some constant folding done by hand to avoid shift count warnings"
  65. #endif
  66. static void get_zero (REAL_VALUE_TYPE *, int);
  67. static void get_canonical_qnan (REAL_VALUE_TYPE *, int);
  68. static void get_canonical_snan (REAL_VALUE_TYPE *, int);
  69. static void get_inf (REAL_VALUE_TYPE *, int);
  70. static bool sticky_rshift_significand (REAL_VALUE_TYPE *,
  71. const REAL_VALUE_TYPE *, unsigned int);
  72. static void rshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  73. unsigned int);
  74. static void lshift_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  75. unsigned int);
  76. static void lshift_significand_1 (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  77. static bool add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *,
  78. const REAL_VALUE_TYPE *);
  79. static bool sub_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  80. const REAL_VALUE_TYPE *, int);
  81. static void neg_significand (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  82. static int cmp_significands (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  83. static int cmp_significand_0 (const REAL_VALUE_TYPE *);
  84. static void set_significand_bit (REAL_VALUE_TYPE *, unsigned int);
  85. static void clear_significand_bit (REAL_VALUE_TYPE *, unsigned int);
  86. static bool test_significand_bit (REAL_VALUE_TYPE *, unsigned int);
  87. static void clear_significand_below (REAL_VALUE_TYPE *, unsigned int);
  88. static bool div_significands (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  89. const REAL_VALUE_TYPE *);
  90. static void normalize (REAL_VALUE_TYPE *);
  91. static bool do_add (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  92. const REAL_VALUE_TYPE *, int);
  93. static bool do_multiply (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  94. const REAL_VALUE_TYPE *);
  95. static bool do_divide (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *,
  96. const REAL_VALUE_TYPE *);
  97. static int do_compare (const REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *, int);
  98. static void do_fix_trunc (REAL_VALUE_TYPE *, const REAL_VALUE_TYPE *);
  99. static unsigned long rtd_divmod (REAL_VALUE_TYPE *, REAL_VALUE_TYPE *);
  100. static void decimal_from_integer (REAL_VALUE_TYPE *);
  101. static void decimal_integer_string (char *, const REAL_VALUE_TYPE *,
  102. size_t);
  103. static const REAL_VALUE_TYPE * ten_to_ptwo (int);
  104. static const REAL_VALUE_TYPE * ten_to_mptwo (int);
  105. static const REAL_VALUE_TYPE * real_digit (int);
  106. static void times_pten (REAL_VALUE_TYPE *, int);
  107. static void round_for_format (const struct real_format *, REAL_VALUE_TYPE *);
  108. /* Initialize R with a positive zero. */
  109. static inline void
  110. get_zero (REAL_VALUE_TYPE *r, int sign)
  111. {
  112. memset (r, 0, sizeof (*r));
  113. r->sign = sign;
  114. }
  115. /* Initialize R with the canonical quiet NaN. */
  116. static inline void
  117. get_canonical_qnan (REAL_VALUE_TYPE *r, int sign)
  118. {
  119. memset (r, 0, sizeof (*r));
  120. r->cl = rvc_nan;
  121. r->sign = sign;
  122. r->canonical = 1;
  123. }
  124. static inline void
  125. get_canonical_snan (REAL_VALUE_TYPE *r, int sign)
  126. {
  127. memset (r, 0, sizeof (*r));
  128. r->cl = rvc_nan;
  129. r->sign = sign;
  130. r->signalling = 1;
  131. r->canonical = 1;
  132. }
  133. static inline void
  134. get_inf (REAL_VALUE_TYPE *r, int sign)
  135. {
  136. memset (r, 0, sizeof (*r));
  137. r->cl = rvc_inf;
  138. r->sign = sign;
  139. }
  140. /* Right-shift the significand of A by N bits; put the result in the
  141. significand of R. If any one bits are shifted out, return true. */
  142. static bool
  143. sticky_rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  144. unsigned int n)
  145. {
  146. unsigned long sticky = 0;
  147. unsigned int i, ofs = 0;
  148. if (n >= HOST_BITS_PER_LONG)
  149. {
  150. for (i = 0, ofs = n / HOST_BITS_PER_LONG; i < ofs; ++i)
  151. sticky |= a->sig[i];
  152. n &= HOST_BITS_PER_LONG - 1;
  153. }
  154. if (n != 0)
  155. {
  156. sticky |= a->sig[ofs] & (((unsigned long)1 << n) - 1);
  157. for (i = 0; i < SIGSZ; ++i)
  158. {
  159. r->sig[i]
  160. = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
  161. | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
  162. << (HOST_BITS_PER_LONG - n)));
  163. }
  164. }
  165. else
  166. {
  167. for (i = 0; ofs + i < SIGSZ; ++i)
  168. r->sig[i] = a->sig[ofs + i];
  169. for (; i < SIGSZ; ++i)
  170. r->sig[i] = 0;
  171. }
  172. return sticky != 0;
  173. }
  174. /* Right-shift the significand of A by N bits; put the result in the
  175. significand of R. */
  176. static void
  177. rshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  178. unsigned int n)
  179. {
  180. unsigned int i, ofs = n / HOST_BITS_PER_LONG;
  181. n &= HOST_BITS_PER_LONG - 1;
  182. if (n != 0)
  183. {
  184. for (i = 0; i < SIGSZ; ++i)
  185. {
  186. r->sig[i]
  187. = (((ofs + i >= SIGSZ ? 0 : a->sig[ofs + i]) >> n)
  188. | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[ofs + i + 1])
  189. << (HOST_BITS_PER_LONG - n)));
  190. }
  191. }
  192. else
  193. {
  194. for (i = 0; ofs + i < SIGSZ; ++i)
  195. r->sig[i] = a->sig[ofs + i];
  196. for (; i < SIGSZ; ++i)
  197. r->sig[i] = 0;
  198. }
  199. }
  200. /* Left-shift the significand of A by N bits; put the result in the
  201. significand of R. */
  202. static void
  203. lshift_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  204. unsigned int n)
  205. {
  206. unsigned int i, ofs = n / HOST_BITS_PER_LONG;
  207. n &= HOST_BITS_PER_LONG - 1;
  208. if (n == 0)
  209. {
  210. for (i = 0; ofs + i < SIGSZ; ++i)
  211. r->sig[SIGSZ-1-i] = a->sig[SIGSZ-1-i-ofs];
  212. for (; i < SIGSZ; ++i)
  213. r->sig[SIGSZ-1-i] = 0;
  214. }
  215. else
  216. for (i = 0; i < SIGSZ; ++i)
  217. {
  218. r->sig[SIGSZ-1-i]
  219. = (((ofs + i >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs]) << n)
  220. | ((ofs + i + 1 >= SIGSZ ? 0 : a->sig[SIGSZ-1-i-ofs-1])
  221. >> (HOST_BITS_PER_LONG - n)));
  222. }
  223. }
  224. /* Likewise, but N is specialized to 1. */
  225. static inline void
  226. lshift_significand_1 (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
  227. {
  228. unsigned int i;
  229. for (i = SIGSZ - 1; i > 0; --i)
  230. r->sig[i] = (a->sig[i] << 1) | (a->sig[i-1] >> (HOST_BITS_PER_LONG - 1));
  231. r->sig[0] = a->sig[0] << 1;
  232. }
  233. /* Add the significands of A and B, placing the result in R. Return
  234. true if there was carry out of the most significant word. */
  235. static inline bool
  236. add_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  237. const REAL_VALUE_TYPE *b)
  238. {
  239. bool carry = false;
  240. int i;
  241. for (i = 0; i < SIGSZ; ++i)
  242. {
  243. unsigned long ai = a->sig[i];
  244. unsigned long ri = ai + b->sig[i];
  245. if (carry)
  246. {
  247. carry = ri < ai;
  248. carry |= ++ri == 0;
  249. }
  250. else
  251. carry = ri < ai;
  252. r->sig[i] = ri;
  253. }
  254. return carry;
  255. }
  256. /* Subtract the significands of A and B, placing the result in R. CARRY is
  257. true if there's a borrow incoming to the least significant word.
  258. Return true if there was borrow out of the most significant word. */
  259. static inline bool
  260. sub_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  261. const REAL_VALUE_TYPE *b, int carry)
  262. {
  263. int i;
  264. for (i = 0; i < SIGSZ; ++i)
  265. {
  266. unsigned long ai = a->sig[i];
  267. unsigned long ri = ai - b->sig[i];
  268. if (carry)
  269. {
  270. carry = ri > ai;
  271. carry |= ~--ri == 0;
  272. }
  273. else
  274. carry = ri > ai;
  275. r->sig[i] = ri;
  276. }
  277. return carry;
  278. }
  279. /* Negate the significand A, placing the result in R. */
  280. static inline void
  281. neg_significand (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
  282. {
  283. bool carry = true;
  284. int i;
  285. for (i = 0; i < SIGSZ; ++i)
  286. {
  287. unsigned long ri, ai = a->sig[i];
  288. if (carry)
  289. {
  290. if (ai)
  291. {
  292. ri = -ai;
  293. carry = false;
  294. }
  295. else
  296. ri = ai;
  297. }
  298. else
  299. ri = ~ai;
  300. r->sig[i] = ri;
  301. }
  302. }
  303. /* Compare significands. Return tri-state vs zero. */
  304. static inline int
  305. cmp_significands (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
  306. {
  307. int i;
  308. for (i = SIGSZ - 1; i >= 0; --i)
  309. {
  310. unsigned long ai = a->sig[i];
  311. unsigned long bi = b->sig[i];
  312. if (ai > bi)
  313. return 1;
  314. if (ai < bi)
  315. return -1;
  316. }
  317. return 0;
  318. }
  319. /* Return true if A is nonzero. */
  320. static inline int
  321. cmp_significand_0 (const REAL_VALUE_TYPE *a)
  322. {
  323. int i;
  324. for (i = SIGSZ - 1; i >= 0; --i)
  325. if (a->sig[i])
  326. return 1;
  327. return 0;
  328. }
  329. /* Set bit N of the significand of R. */
  330. static inline void
  331. set_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
  332. {
  333. r->sig[n / HOST_BITS_PER_LONG]
  334. |= (unsigned long)1 << (n % HOST_BITS_PER_LONG);
  335. }
  336. /* Clear bit N of the significand of R. */
  337. static inline void
  338. clear_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
  339. {
  340. r->sig[n / HOST_BITS_PER_LONG]
  341. &= ~((unsigned long)1 << (n % HOST_BITS_PER_LONG));
  342. }
  343. /* Test bit N of the significand of R. */
  344. static inline bool
  345. test_significand_bit (REAL_VALUE_TYPE *r, unsigned int n)
  346. {
  347. /* ??? Compiler bug here if we return this expression directly.
  348. The conversion to bool strips the "&1" and we wind up testing
  349. e.g. 2 != 0 -> true. Seen in gcc version 3.2 20020520. */
  350. int t = (r->sig[n / HOST_BITS_PER_LONG] >> (n % HOST_BITS_PER_LONG)) & 1;
  351. return t;
  352. }
  353. /* Clear bits 0..N-1 of the significand of R. */
  354. static void
  355. clear_significand_below (REAL_VALUE_TYPE *r, unsigned int n)
  356. {
  357. int i, w = n / HOST_BITS_PER_LONG;
  358. for (i = 0; i < w; ++i)
  359. r->sig[i] = 0;
  360. r->sig[w] &= ~(((unsigned long)1 << (n % HOST_BITS_PER_LONG)) - 1);
  361. }
  362. /* Divide the significands of A and B, placing the result in R. Return
  363. true if the division was inexact. */
  364. static inline bool
  365. div_significands (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  366. const REAL_VALUE_TYPE *b)
  367. {
  368. REAL_VALUE_TYPE u;
  369. int i, bit = SIGNIFICAND_BITS - 1;
  370. unsigned long msb, inexact;
  371. u = *a;
  372. memset (r->sig, 0, sizeof (r->sig));
  373. msb = 0;
  374. goto start;
  375. do
  376. {
  377. msb = u.sig[SIGSZ-1] & SIG_MSB;
  378. lshift_significand_1 (&u, &u);
  379. start:
  380. if (msb || cmp_significands (&u, b) >= 0)
  381. {
  382. sub_significands (&u, &u, b, 0);
  383. set_significand_bit (r, bit);
  384. }
  385. }
  386. while (--bit >= 0);
  387. for (i = 0, inexact = 0; i < SIGSZ; i++)
  388. inexact |= u.sig[i];
  389. return inexact != 0;
  390. }
  391. /* Adjust the exponent and significand of R such that the most
  392. significant bit is set. We underflow to zero and overflow to
  393. infinity here, without denormals. (The intermediate representation
  394. exponent is large enough to handle target denormals normalized.) */
  395. static void
  396. normalize (REAL_VALUE_TYPE *r)
  397. {
  398. int shift = 0, exp;
  399. int i, j;
  400. if (r->decimal)
  401. return;
  402. /* Find the first word that is nonzero. */
  403. for (i = SIGSZ - 1; i >= 0; i--)
  404. if (r->sig[i] == 0)
  405. shift += HOST_BITS_PER_LONG;
  406. else
  407. break;
  408. /* Zero significand flushes to zero. */
  409. if (i < 0)
  410. {
  411. r->cl = rvc_zero;
  412. SET_REAL_EXP (r, 0);
  413. return;
  414. }
  415. /* Find the first bit that is nonzero. */
  416. for (j = 0; ; j++)
  417. if (r->sig[i] & ((unsigned long)1 << (HOST_BITS_PER_LONG - 1 - j)))
  418. break;
  419. shift += j;
  420. if (shift > 0)
  421. {
  422. exp = REAL_EXP (r) - shift;
  423. if (exp > MAX_EXP)
  424. get_inf (r, r->sign);
  425. else if (exp < -MAX_EXP)
  426. get_zero (r, r->sign);
  427. else
  428. {
  429. SET_REAL_EXP (r, exp);
  430. lshift_significand (r, r, shift);
  431. }
  432. }
  433. }
  434. /* Calculate R = A + (SUBTRACT_P ? -B : B). Return true if the
  435. result may be inexact due to a loss of precision. */
  436. static bool
  437. do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  438. const REAL_VALUE_TYPE *b, int subtract_p)
  439. {
  440. int dexp, sign, exp;
  441. REAL_VALUE_TYPE t;
  442. bool inexact = false;
  443. /* Determine if we need to add or subtract. */
  444. sign = a->sign;
  445. subtract_p = (sign ^ b->sign) ^ subtract_p;
  446. switch (CLASS2 (a->cl, b->cl))
  447. {
  448. case CLASS2 (rvc_zero, rvc_zero):
  449. /* -0 + -0 = -0, -0 - +0 = -0; all other cases yield +0. */
  450. get_zero (r, sign & !subtract_p);
  451. return false;
  452. case CLASS2 (rvc_zero, rvc_normal):
  453. case CLASS2 (rvc_zero, rvc_inf):
  454. case CLASS2 (rvc_zero, rvc_nan):
  455. /* 0 + ANY = ANY. */
  456. case CLASS2 (rvc_normal, rvc_nan):
  457. case CLASS2 (rvc_inf, rvc_nan):
  458. case CLASS2 (rvc_nan, rvc_nan):
  459. /* ANY + NaN = NaN. */
  460. case CLASS2 (rvc_normal, rvc_inf):
  461. /* R + Inf = Inf. */
  462. *r = *b;
  463. r->sign = sign ^ subtract_p;
  464. return false;
  465. case CLASS2 (rvc_normal, rvc_zero):
  466. case CLASS2 (rvc_inf, rvc_zero):
  467. case CLASS2 (rvc_nan, rvc_zero):
  468. /* ANY + 0 = ANY. */
  469. case CLASS2 (rvc_nan, rvc_normal):
  470. case CLASS2 (rvc_nan, rvc_inf):
  471. /* NaN + ANY = NaN. */
  472. case CLASS2 (rvc_inf, rvc_normal):
  473. /* Inf + R = Inf. */
  474. *r = *a;
  475. return false;
  476. case CLASS2 (rvc_inf, rvc_inf):
  477. if (subtract_p)
  478. /* Inf - Inf = NaN. */
  479. get_canonical_qnan (r, 0);
  480. else
  481. /* Inf + Inf = Inf. */
  482. *r = *a;
  483. return false;
  484. case CLASS2 (rvc_normal, rvc_normal):
  485. break;
  486. default:
  487. gcc_unreachable ();
  488. }
  489. /* Swap the arguments such that A has the larger exponent. */
  490. dexp = REAL_EXP (a) - REAL_EXP (b);
  491. if (dexp < 0)
  492. {
  493. const REAL_VALUE_TYPE *t;
  494. t = a, a = b, b = t;
  495. dexp = -dexp;
  496. sign ^= subtract_p;
  497. }
  498. exp = REAL_EXP (a);
  499. /* If the exponents are not identical, we need to shift the
  500. significand of B down. */
  501. if (dexp > 0)
  502. {
  503. /* If the exponents are too far apart, the significands
  504. do not overlap, which makes the subtraction a noop. */
  505. if (dexp >= SIGNIFICAND_BITS)
  506. {
  507. *r = *a;
  508. r->sign = sign;
  509. return true;
  510. }
  511. inexact |= sticky_rshift_significand (&t, b, dexp);
  512. b = &t;
  513. }
  514. if (subtract_p)
  515. {
  516. if (sub_significands (r, a, b, inexact))
  517. {
  518. /* We got a borrow out of the subtraction. That means that
  519. A and B had the same exponent, and B had the larger
  520. significand. We need to swap the sign and negate the
  521. significand. */
  522. sign ^= 1;
  523. neg_significand (r, r);
  524. }
  525. }
  526. else
  527. {
  528. if (add_significands (r, a, b))
  529. {
  530. /* We got carry out of the addition. This means we need to
  531. shift the significand back down one bit and increase the
  532. exponent. */
  533. inexact |= sticky_rshift_significand (r, r, 1);
  534. r->sig[SIGSZ-1] |= SIG_MSB;
  535. if (++exp > MAX_EXP)
  536. {
  537. get_inf (r, sign);
  538. return true;
  539. }
  540. }
  541. }
  542. r->cl = rvc_normal;
  543. r->sign = sign;
  544. SET_REAL_EXP (r, exp);
  545. /* Zero out the remaining fields. */
  546. r->signalling = 0;
  547. r->canonical = 0;
  548. r->decimal = 0;
  549. /* Re-normalize the result. */
  550. normalize (r);
  551. /* Special case: if the subtraction results in zero, the result
  552. is positive. */
  553. if (r->cl == rvc_zero)
  554. r->sign = 0;
  555. else
  556. r->sig[0] |= inexact;
  557. return inexact;
  558. }
  559. /* Calculate R = A * B. Return true if the result may be inexact. */
  560. static bool
  561. do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  562. const REAL_VALUE_TYPE *b)
  563. {
  564. REAL_VALUE_TYPE u, t, *rr;
  565. unsigned int i, j, k;
  566. int sign = a->sign ^ b->sign;
  567. bool inexact = false;
  568. switch (CLASS2 (a->cl, b->cl))
  569. {
  570. case CLASS2 (rvc_zero, rvc_zero):
  571. case CLASS2 (rvc_zero, rvc_normal):
  572. case CLASS2 (rvc_normal, rvc_zero):
  573. /* +-0 * ANY = 0 with appropriate sign. */
  574. get_zero (r, sign);
  575. return false;
  576. case CLASS2 (rvc_zero, rvc_nan):
  577. case CLASS2 (rvc_normal, rvc_nan):
  578. case CLASS2 (rvc_inf, rvc_nan):
  579. case CLASS2 (rvc_nan, rvc_nan):
  580. /* ANY * NaN = NaN. */
  581. *r = *b;
  582. r->sign = sign;
  583. return false;
  584. case CLASS2 (rvc_nan, rvc_zero):
  585. case CLASS2 (rvc_nan, rvc_normal):
  586. case CLASS2 (rvc_nan, rvc_inf):
  587. /* NaN * ANY = NaN. */
  588. *r = *a;
  589. r->sign = sign;
  590. return false;
  591. case CLASS2 (rvc_zero, rvc_inf):
  592. case CLASS2 (rvc_inf, rvc_zero):
  593. /* 0 * Inf = NaN */
  594. get_canonical_qnan (r, sign);
  595. return false;
  596. case CLASS2 (rvc_inf, rvc_inf):
  597. case CLASS2 (rvc_normal, rvc_inf):
  598. case CLASS2 (rvc_inf, rvc_normal):
  599. /* Inf * Inf = Inf, R * Inf = Inf */
  600. get_inf (r, sign);
  601. return false;
  602. case CLASS2 (rvc_normal, rvc_normal):
  603. break;
  604. default:
  605. gcc_unreachable ();
  606. }
  607. if (r == a || r == b)
  608. rr = &t;
  609. else
  610. rr = r;
  611. get_zero (rr, 0);
  612. /* Collect all the partial products. Since we don't have sure access
  613. to a widening multiply, we split each long into two half-words.
  614. Consider the long-hand form of a four half-word multiplication:
  615. A B C D
  616. * E F G H
  617. --------------
  618. DE DF DG DH
  619. CE CF CG CH
  620. BE BF BG BH
  621. AE AF AG AH
  622. We construct partial products of the widened half-word products
  623. that are known to not overlap, e.g. DF+DH. Each such partial
  624. product is given its proper exponent, which allows us to sum them
  625. and obtain the finished product. */
  626. for (i = 0; i < SIGSZ * 2; ++i)
  627. {
  628. unsigned long ai = a->sig[i / 2];
  629. if (i & 1)
  630. ai >>= HOST_BITS_PER_LONG / 2;
  631. else
  632. ai &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
  633. if (ai == 0)
  634. continue;
  635. for (j = 0; j < 2; ++j)
  636. {
  637. int exp = (REAL_EXP (a) - (2*SIGSZ-1-i)*(HOST_BITS_PER_LONG/2)
  638. + (REAL_EXP (b) - (1-j)*(HOST_BITS_PER_LONG/2)));
  639. if (exp > MAX_EXP)
  640. {
  641. get_inf (r, sign);
  642. return true;
  643. }
  644. if (exp < -MAX_EXP)
  645. {
  646. /* Would underflow to zero, which we shouldn't bother adding. */
  647. inexact = true;
  648. continue;
  649. }
  650. memset (&u, 0, sizeof (u));
  651. u.cl = rvc_normal;
  652. SET_REAL_EXP (&u, exp);
  653. for (k = j; k < SIGSZ * 2; k += 2)
  654. {
  655. unsigned long bi = b->sig[k / 2];
  656. if (k & 1)
  657. bi >>= HOST_BITS_PER_LONG / 2;
  658. else
  659. bi &= ((unsigned long)1 << (HOST_BITS_PER_LONG / 2)) - 1;
  660. u.sig[k / 2] = ai * bi;
  661. }
  662. normalize (&u);
  663. inexact |= do_add (rr, rr, &u, 0);
  664. }
  665. }
  666. rr->sign = sign;
  667. if (rr != r)
  668. *r = t;
  669. return inexact;
  670. }
  671. /* Calculate R = A / B. Return true if the result may be inexact. */
  672. static bool
  673. do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a,
  674. const REAL_VALUE_TYPE *b)
  675. {
  676. int exp, sign = a->sign ^ b->sign;
  677. REAL_VALUE_TYPE t, *rr;
  678. bool inexact;
  679. switch (CLASS2 (a->cl, b->cl))
  680. {
  681. case CLASS2 (rvc_zero, rvc_zero):
  682. /* 0 / 0 = NaN. */
  683. case CLASS2 (rvc_inf, rvc_inf):
  684. /* Inf / Inf = NaN. */
  685. get_canonical_qnan (r, sign);
  686. return false;
  687. case CLASS2 (rvc_zero, rvc_normal):
  688. case CLASS2 (rvc_zero, rvc_inf):
  689. /* 0 / ANY = 0. */
  690. case CLASS2 (rvc_normal, rvc_inf):
  691. /* R / Inf = 0. */
  692. get_zero (r, sign);
  693. return false;
  694. case CLASS2 (rvc_normal, rvc_zero):
  695. /* R / 0 = Inf. */
  696. case CLASS2 (rvc_inf, rvc_zero):
  697. /* Inf / 0 = Inf. */
  698. get_inf (r, sign);
  699. return false;
  700. case CLASS2 (rvc_zero, rvc_nan):
  701. case CLASS2 (rvc_normal, rvc_nan):
  702. case CLASS2 (rvc_inf, rvc_nan):
  703. case CLASS2 (rvc_nan, rvc_nan):
  704. /* ANY / NaN = NaN. */
  705. *r = *b;
  706. r->sign = sign;
  707. return false;
  708. case CLASS2 (rvc_nan, rvc_zero):
  709. case CLASS2 (rvc_nan, rvc_normal):
  710. case CLASS2 (rvc_nan, rvc_inf):
  711. /* NaN / ANY = NaN. */
  712. *r = *a;
  713. r->sign = sign;
  714. return false;
  715. case CLASS2 (rvc_inf, rvc_normal):
  716. /* Inf / R = Inf. */
  717. get_inf (r, sign);
  718. return false;
  719. case CLASS2 (rvc_normal, rvc_normal):
  720. break;
  721. default:
  722. gcc_unreachable ();
  723. }
  724. if (r == a || r == b)
  725. rr = &t;
  726. else
  727. rr = r;
  728. /* Make sure all fields in the result are initialized. */
  729. get_zero (rr, 0);
  730. rr->cl = rvc_normal;
  731. rr->sign = sign;
  732. exp = REAL_EXP (a) - REAL_EXP (b) + 1;
  733. if (exp > MAX_EXP)
  734. {
  735. get_inf (r, sign);
  736. return true;
  737. }
  738. if (exp < -MAX_EXP)
  739. {
  740. get_zero (r, sign);
  741. return true;
  742. }
  743. SET_REAL_EXP (rr, exp);
  744. inexact = div_significands (rr, a, b);
  745. /* Re-normalize the result. */
  746. normalize (rr);
  747. rr->sig[0] |= inexact;
  748. if (rr != r)
  749. *r = t;
  750. return inexact;
  751. }
  752. /* Return a tri-state comparison of A vs B. Return NAN_RESULT if
  753. one of the two operands is a NaN. */
  754. static int
  755. do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
  756. int nan_result)
  757. {
  758. int ret;
  759. switch (CLASS2 (a->cl, b->cl))
  760. {
  761. case CLASS2 (rvc_zero, rvc_zero):
  762. /* Sign of zero doesn't matter for compares. */
  763. return 0;
  764. case CLASS2 (rvc_normal, rvc_zero):
  765. /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
  766. if (a->decimal)
  767. return decimal_do_compare (a, b, nan_result);
  768. /* Fall through. */
  769. case CLASS2 (rvc_inf, rvc_zero):
  770. case CLASS2 (rvc_inf, rvc_normal):
  771. return (a->sign ? -1 : 1);
  772. case CLASS2 (rvc_inf, rvc_inf):
  773. return -a->sign - -b->sign;
  774. case CLASS2 (rvc_zero, rvc_normal):
  775. /* Decimal float zero is special and uses rvc_normal, not rvc_zero. */
  776. if (b->decimal)
  777. return decimal_do_compare (a, b, nan_result);
  778. /* Fall through. */
  779. case CLASS2 (rvc_zero, rvc_inf):
  780. case CLASS2 (rvc_normal, rvc_inf):
  781. return (b->sign ? 1 : -1);
  782. case CLASS2 (rvc_zero, rvc_nan):
  783. case CLASS2 (rvc_normal, rvc_nan):
  784. case CLASS2 (rvc_inf, rvc_nan):
  785. case CLASS2 (rvc_nan, rvc_nan):
  786. case CLASS2 (rvc_nan, rvc_zero):
  787. case CLASS2 (rvc_nan, rvc_normal):
  788. case CLASS2 (rvc_nan, rvc_inf):
  789. return nan_result;
  790. case CLASS2 (rvc_normal, rvc_normal):
  791. break;
  792. default:
  793. gcc_unreachable ();
  794. }
  795. if (a->sign != b->sign)
  796. return -a->sign - -b->sign;
  797. if (a->decimal || b->decimal)
  798. return decimal_do_compare (a, b, nan_result);
  799. if (REAL_EXP (a) > REAL_EXP (b))
  800. ret = 1;
  801. else if (REAL_EXP (a) < REAL_EXP (b))
  802. ret = -1;
  803. else
  804. ret = cmp_significands (a, b);
  805. return (a->sign ? -ret : ret);
  806. }
  807. /* Return A truncated to an integral value toward zero. */
  808. static void
  809. do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
  810. {
  811. *r = *a;
  812. switch (r->cl)
  813. {
  814. case rvc_zero:
  815. case rvc_inf:
  816. case rvc_nan:
  817. break;
  818. case rvc_normal:
  819. if (r->decimal)
  820. {
  821. decimal_do_fix_trunc (r, a);
  822. return;
  823. }
  824. if (REAL_EXP (r) <= 0)
  825. get_zero (r, r->sign);
  826. else if (REAL_EXP (r) < SIGNIFICAND_BITS)
  827. clear_significand_below (r, SIGNIFICAND_BITS - REAL_EXP (r));
  828. break;
  829. default:
  830. gcc_unreachable ();
  831. }
  832. }
  833. /* Perform the binary or unary operation described by CODE.
  834. For a unary operation, leave OP1 NULL. This function returns
  835. true if the result may be inexact due to loss of precision. */
  836. bool
  837. real_arithmetic (REAL_VALUE_TYPE *r, int icode, const REAL_VALUE_TYPE *op0,
  838. const REAL_VALUE_TYPE *op1)
  839. {
  840. enum tree_code code = (enum tree_code) icode;
  841. if (op0->decimal || (op1 && op1->decimal))
  842. return decimal_real_arithmetic (r, code, op0, op1);
  843. switch (code)
  844. {
  845. case PLUS_EXPR:
  846. /* Clear any padding areas in *r if it isn't equal to one of the
  847. operands so that we can later do bitwise comparisons later on. */
  848. if (r != op0 && r != op1)
  849. memset (r, '\0', sizeof (*r));
  850. return do_add (r, op0, op1, 0);
  851. case MINUS_EXPR:
  852. if (r != op0 && r != op1)
  853. memset (r, '\0', sizeof (*r));
  854. return do_add (r, op0, op1, 1);
  855. case MULT_EXPR:
  856. if (r != op0 && r != op1)
  857. memset (r, '\0', sizeof (*r));
  858. return do_multiply (r, op0, op1);
  859. case RDIV_EXPR:
  860. if (r != op0 && r != op1)
  861. memset (r, '\0', sizeof (*r));
  862. return do_divide (r, op0, op1);
  863. case MIN_EXPR:
  864. if (op1->cl == rvc_nan)
  865. *r = *op1;
  866. else if (do_compare (op0, op1, -1) < 0)
  867. *r = *op0;
  868. else
  869. *r = *op1;
  870. break;
  871. case MAX_EXPR:
  872. if (op1->cl == rvc_nan)
  873. *r = *op1;
  874. else if (do_compare (op0, op1, 1) < 0)
  875. *r = *op1;
  876. else
  877. *r = *op0;
  878. break;
  879. case NEGATE_EXPR:
  880. *r = *op0;
  881. r->sign ^= 1;
  882. break;
  883. case ABS_EXPR:
  884. *r = *op0;
  885. r->sign = 0;
  886. break;
  887. case FIX_TRUNC_EXPR:
  888. do_fix_trunc (r, op0);
  889. break;
  890. default:
  891. gcc_unreachable ();
  892. }
  893. return false;
  894. }
  895. REAL_VALUE_TYPE
  896. real_value_negate (const REAL_VALUE_TYPE *op0)
  897. {
  898. REAL_VALUE_TYPE r;
  899. real_arithmetic (&r, NEGATE_EXPR, op0, NULL);
  900. return r;
  901. }
  902. REAL_VALUE_TYPE
  903. real_value_abs (const REAL_VALUE_TYPE *op0)
  904. {
  905. REAL_VALUE_TYPE r;
  906. real_arithmetic (&r, ABS_EXPR, op0, NULL);
  907. return r;
  908. }
  909. bool
  910. real_compare (int icode, const REAL_VALUE_TYPE *op0,
  911. const REAL_VALUE_TYPE *op1)
  912. {
  913. enum tree_code code = (enum tree_code) icode;
  914. switch (code)
  915. {
  916. case LT_EXPR:
  917. return do_compare (op0, op1, 1) < 0;
  918. case LE_EXPR:
  919. return do_compare (op0, op1, 1) <= 0;
  920. case GT_EXPR:
  921. return do_compare (op0, op1, -1) > 0;
  922. case GE_EXPR:
  923. return do_compare (op0, op1, -1) >= 0;
  924. case EQ_EXPR:
  925. return do_compare (op0, op1, -1) == 0;
  926. case NE_EXPR:
  927. return do_compare (op0, op1, -1) != 0;
  928. case UNORDERED_EXPR:
  929. return op0->cl == rvc_nan || op1->cl == rvc_nan;
  930. case ORDERED_EXPR:
  931. return op0->cl != rvc_nan && op1->cl != rvc_nan;
  932. case UNLT_EXPR:
  933. return do_compare (op0, op1, -1) < 0;
  934. case UNLE_EXPR:
  935. return do_compare (op0, op1, -1) <= 0;
  936. case UNGT_EXPR:
  937. return do_compare (op0, op1, 1) > 0;
  938. case UNGE_EXPR:
  939. return do_compare (op0, op1, 1) >= 0;
  940. case UNEQ_EXPR:
  941. return do_compare (op0, op1, 0) == 0;
  942. case LTGT_EXPR:
  943. return do_compare (op0, op1, 0) != 0;
  944. default:
  945. gcc_unreachable ();
  946. }
  947. }
  948. /* Return floor log2(R). */
  949. int
  950. real_exponent (const REAL_VALUE_TYPE *r)
  951. {
  952. switch (r->cl)
  953. {
  954. case rvc_zero:
  955. return 0;
  956. case rvc_inf:
  957. case rvc_nan:
  958. return (unsigned int)-1 >> 1;
  959. case rvc_normal:
  960. return REAL_EXP (r);
  961. default:
  962. gcc_unreachable ();
  963. }
  964. }
  965. /* R = OP0 * 2**EXP. */
  966. void
  967. real_ldexp (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0, int exp)
  968. {
  969. *r = *op0;
  970. switch (r->cl)
  971. {
  972. case rvc_zero:
  973. case rvc_inf:
  974. case rvc_nan:
  975. break;
  976. case rvc_normal:
  977. exp += REAL_EXP (op0);
  978. if (exp > MAX_EXP)
  979. get_inf (r, r->sign);
  980. else if (exp < -MAX_EXP)
  981. get_zero (r, r->sign);
  982. else
  983. SET_REAL_EXP (r, exp);
  984. break;
  985. default:
  986. gcc_unreachable ();
  987. }
  988. }
  989. /* Determine whether a floating-point value X is infinite. */
  990. bool
  991. real_isinf (const REAL_VALUE_TYPE *r)
  992. {
  993. return (r->cl == rvc_inf);
  994. }
  995. /* Determine whether a floating-point value X is a NaN. */
  996. bool
  997. real_isnan (const REAL_VALUE_TYPE *r)
  998. {
  999. return (r->cl == rvc_nan);
  1000. }
  1001. /* Determine whether a floating-point value X is finite. */
  1002. bool
  1003. real_isfinite (const REAL_VALUE_TYPE *r)
  1004. {
  1005. return (r->cl != rvc_nan) && (r->cl != rvc_inf);
  1006. }
  1007. /* Determine whether a floating-point value X is negative. */
  1008. bool
  1009. real_isneg (const REAL_VALUE_TYPE *r)
  1010. {
  1011. return r->sign;
  1012. }
  1013. /* Determine whether a floating-point value X is minus zero. */
  1014. bool
  1015. real_isnegzero (const REAL_VALUE_TYPE *r)
  1016. {
  1017. return r->sign && r->cl == rvc_zero;
  1018. }
  1019. /* Compare two floating-point objects for bitwise identity. */
  1020. bool
  1021. real_identical (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b)
  1022. {
  1023. int i;
  1024. if (a->cl != b->cl)
  1025. return false;
  1026. if (a->sign != b->sign)
  1027. return false;
  1028. switch (a->cl)
  1029. {
  1030. case rvc_zero:
  1031. case rvc_inf:
  1032. return true;
  1033. case rvc_normal:
  1034. if (a->decimal != b->decimal)
  1035. return false;
  1036. if (REAL_EXP (a) != REAL_EXP (b))
  1037. return false;
  1038. break;
  1039. case rvc_nan:
  1040. if (a->signalling != b->signalling)
  1041. return false;
  1042. /* The significand is ignored for canonical NaNs. */
  1043. if (a->canonical || b->canonical)
  1044. return a->canonical == b->canonical;
  1045. break;
  1046. default:
  1047. gcc_unreachable ();
  1048. }
  1049. for (i = 0; i < SIGSZ; ++i)
  1050. if (a->sig[i] != b->sig[i])
  1051. return false;
  1052. return true;
  1053. }
  1054. /* Try to change R into its exact multiplicative inverse in machine
  1055. mode MODE. Return true if successful. */
  1056. bool
  1057. exact_real_inverse (machine_mode mode, REAL_VALUE_TYPE *r)
  1058. {
  1059. const REAL_VALUE_TYPE *one = real_digit (1);
  1060. REAL_VALUE_TYPE u;
  1061. int i;
  1062. if (r->cl != rvc_normal)
  1063. return false;
  1064. /* Check for a power of two: all significand bits zero except the MSB. */
  1065. for (i = 0; i < SIGSZ-1; ++i)
  1066. if (r->sig[i] != 0)
  1067. return false;
  1068. if (r->sig[SIGSZ-1] != SIG_MSB)
  1069. return false;
  1070. /* Find the inverse and truncate to the required mode. */
  1071. do_divide (&u, one, r);
  1072. real_convert (&u, mode, &u);
  1073. /* The rounding may have overflowed. */
  1074. if (u.cl != rvc_normal)
  1075. return false;
  1076. for (i = 0; i < SIGSZ-1; ++i)
  1077. if (u.sig[i] != 0)
  1078. return false;
  1079. if (u.sig[SIGSZ-1] != SIG_MSB)
  1080. return false;
  1081. *r = u;
  1082. return true;
  1083. }
  1084. /* Return true if arithmetic on values in IMODE that were promoted
  1085. from values in TMODE is equivalent to direct arithmetic on values
  1086. in TMODE. */
  1087. bool
  1088. real_can_shorten_arithmetic (machine_mode imode, machine_mode tmode)
  1089. {
  1090. const struct real_format *tfmt, *ifmt;
  1091. tfmt = REAL_MODE_FORMAT (tmode);
  1092. ifmt = REAL_MODE_FORMAT (imode);
  1093. /* These conditions are conservative rather than trying to catch the
  1094. exact boundary conditions; the main case to allow is IEEE float
  1095. and double. */
  1096. return (ifmt->b == tfmt->b
  1097. && ifmt->p > 2 * tfmt->p
  1098. && ifmt->emin < 2 * tfmt->emin - tfmt->p - 2
  1099. && ifmt->emin < tfmt->emin - tfmt->emax - tfmt->p - 2
  1100. && ifmt->emax > 2 * tfmt->emax + 2
  1101. && ifmt->emax > tfmt->emax - tfmt->emin + tfmt->p + 2
  1102. && ifmt->round_towards_zero == tfmt->round_towards_zero
  1103. && (ifmt->has_sign_dependent_rounding
  1104. == tfmt->has_sign_dependent_rounding)
  1105. && ifmt->has_nans >= tfmt->has_nans
  1106. && ifmt->has_inf >= tfmt->has_inf
  1107. && ifmt->has_signed_zero >= tfmt->has_signed_zero
  1108. && !MODE_COMPOSITE_P (tmode)
  1109. && !MODE_COMPOSITE_P (imode));
  1110. }
  1111. /* Render R as an integer. */
  1112. HOST_WIDE_INT
  1113. real_to_integer (const REAL_VALUE_TYPE *r)
  1114. {
  1115. unsigned HOST_WIDE_INT i;
  1116. switch (r->cl)
  1117. {
  1118. case rvc_zero:
  1119. underflow:
  1120. return 0;
  1121. case rvc_inf:
  1122. case rvc_nan:
  1123. overflow:
  1124. i = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
  1125. if (!r->sign)
  1126. i--;
  1127. return i;
  1128. case rvc_normal:
  1129. if (r->decimal)
  1130. return decimal_real_to_integer (r);
  1131. if (REAL_EXP (r) <= 0)
  1132. goto underflow;
  1133. /* Only force overflow for unsigned overflow. Signed overflow is
  1134. undefined, so it doesn't matter what we return, and some callers
  1135. expect to be able to use this routine for both signed and
  1136. unsigned conversions. */
  1137. if (REAL_EXP (r) > HOST_BITS_PER_WIDE_INT)
  1138. goto overflow;
  1139. if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
  1140. i = r->sig[SIGSZ-1];
  1141. else
  1142. {
  1143. gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
  1144. i = r->sig[SIGSZ-1];
  1145. i = i << (HOST_BITS_PER_LONG - 1) << 1;
  1146. i |= r->sig[SIGSZ-2];
  1147. }
  1148. i >>= HOST_BITS_PER_WIDE_INT - REAL_EXP (r);
  1149. if (r->sign)
  1150. i = -i;
  1151. return i;
  1152. default:
  1153. gcc_unreachable ();
  1154. }
  1155. }
  1156. /* Likewise, but producing a wide-int of PRECISION. If the value cannot
  1157. be represented in precision, *FAIL is set to TRUE. */
  1158. wide_int
  1159. real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
  1160. {
  1161. HOST_WIDE_INT val[2 * WIDE_INT_MAX_ELTS];
  1162. int exp;
  1163. int words, w;
  1164. wide_int result;
  1165. switch (r->cl)
  1166. {
  1167. case rvc_zero:
  1168. underflow:
  1169. return wi::zero (precision);
  1170. case rvc_inf:
  1171. case rvc_nan:
  1172. overflow:
  1173. *fail = true;
  1174. if (r->sign)
  1175. return wi::set_bit_in_zero (precision - 1, precision);
  1176. else
  1177. return ~wi::set_bit_in_zero (precision - 1, precision);
  1178. case rvc_normal:
  1179. if (r->decimal)
  1180. return decimal_real_to_integer (r, fail, precision);
  1181. exp = REAL_EXP (r);
  1182. if (exp <= 0)
  1183. goto underflow;
  1184. /* Only force overflow for unsigned overflow. Signed overflow is
  1185. undefined, so it doesn't matter what we return, and some callers
  1186. expect to be able to use this routine for both signed and
  1187. unsigned conversions. */
  1188. if (exp > precision)
  1189. goto overflow;
  1190. /* Put the significand into a wide_int that has precision W, which
  1191. is the smallest HWI-multiple that has at least PRECISION bits.
  1192. This ensures that the top bit of the significand is in the
  1193. top bit of the wide_int. */
  1194. words = (precision + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT;
  1195. w = words * HOST_BITS_PER_WIDE_INT;
  1196. #if (HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG)
  1197. for (int i = 0; i < words; i++)
  1198. {
  1199. int j = SIGSZ - words + i;
  1200. val[i] = (j < 0) ? 0 : r->sig[j];
  1201. }
  1202. #else
  1203. gcc_assert (HOST_BITS_PER_WIDE_INT == 2 * HOST_BITS_PER_LONG);
  1204. for (int i = 0; i < words; i++)
  1205. {
  1206. int j = SIGSZ - (words * 2) + (i * 2);
  1207. if (j < 0)
  1208. val[i] = 0;
  1209. else
  1210. val[i] = r->sig[j];
  1211. j += 1;
  1212. if (j >= 0)
  1213. val[i] |= (unsigned HOST_WIDE_INT) r->sig[j] << HOST_BITS_PER_LONG;
  1214. }
  1215. #endif
  1216. /* Shift the value into place and truncate to the desired precision. */
  1217. result = wide_int::from_array (val, words, w);
  1218. result = wi::lrshift (result, w - exp);
  1219. result = wide_int::from (result, precision, UNSIGNED);
  1220. if (r->sign)
  1221. return -result;
  1222. else
  1223. return result;
  1224. default:
  1225. gcc_unreachable ();
  1226. }
  1227. }
  1228. /* A subroutine of real_to_decimal. Compute the quotient and remainder
  1229. of NUM / DEN. Return the quotient and place the remainder in NUM.
  1230. It is expected that NUM / DEN are close enough that the quotient is
  1231. small. */
  1232. static unsigned long
  1233. rtd_divmod (REAL_VALUE_TYPE *num, REAL_VALUE_TYPE *den)
  1234. {
  1235. unsigned long q, msb;
  1236. int expn = REAL_EXP (num), expd = REAL_EXP (den);
  1237. if (expn < expd)
  1238. return 0;
  1239. q = msb = 0;
  1240. goto start;
  1241. do
  1242. {
  1243. msb = num->sig[SIGSZ-1] & SIG_MSB;
  1244. q <<= 1;
  1245. lshift_significand_1 (num, num);
  1246. start:
  1247. if (msb || cmp_significands (num, den) >= 0)
  1248. {
  1249. sub_significands (num, num, den, 0);
  1250. q |= 1;
  1251. }
  1252. }
  1253. while (--expn >= expd);
  1254. SET_REAL_EXP (num, expd);
  1255. normalize (num);
  1256. return q;
  1257. }
  1258. /* Render R as a decimal floating point constant. Emit DIGITS significant
  1259. digits in the result, bounded by BUF_SIZE. If DIGITS is 0, choose the
  1260. maximum for the representation. If CROP_TRAILING_ZEROS, strip trailing
  1261. zeros. If MODE is VOIDmode, round to nearest value. Otherwise, round
  1262. to a string that, when parsed back in mode MODE, yields the same value. */
  1263. #define M_LOG10_2 0.30102999566398119521
  1264. void
  1265. real_to_decimal_for_mode (char *str, const REAL_VALUE_TYPE *r_orig,
  1266. size_t buf_size, size_t digits,
  1267. int crop_trailing_zeros, machine_mode mode)
  1268. {
  1269. const struct real_format *fmt = NULL;
  1270. const REAL_VALUE_TYPE *one, *ten;
  1271. REAL_VALUE_TYPE r, pten, u, v;
  1272. int dec_exp, cmp_one, digit;
  1273. size_t max_digits;
  1274. char *p, *first, *last;
  1275. bool sign;
  1276. bool round_up;
  1277. if (mode != VOIDmode)
  1278. {
  1279. fmt = REAL_MODE_FORMAT (mode);
  1280. gcc_assert (fmt);
  1281. }
  1282. r = *r_orig;
  1283. switch (r.cl)
  1284. {
  1285. case rvc_zero:
  1286. strcpy (str, (r.sign ? "-0.0" : "0.0"));
  1287. return;
  1288. case rvc_normal:
  1289. break;
  1290. case rvc_inf:
  1291. strcpy (str, (r.sign ? "-Inf" : "+Inf"));
  1292. return;
  1293. case rvc_nan:
  1294. /* ??? Print the significand as well, if not canonical? */
  1295. sprintf (str, "%c%cNaN", (r_orig->sign ? '-' : '+'),
  1296. (r_orig->signalling ? 'S' : 'Q'));
  1297. return;
  1298. default:
  1299. gcc_unreachable ();
  1300. }
  1301. if (r.decimal)
  1302. {
  1303. decimal_real_to_decimal (str, &r, buf_size, digits, crop_trailing_zeros);
  1304. return;
  1305. }
  1306. /* Bound the number of digits printed by the size of the representation. */
  1307. max_digits = SIGNIFICAND_BITS * M_LOG10_2;
  1308. if (digits == 0 || digits > max_digits)
  1309. digits = max_digits;
  1310. /* Estimate the decimal exponent, and compute the length of the string it
  1311. will print as. Be conservative and add one to account for possible
  1312. overflow or rounding error. */
  1313. dec_exp = REAL_EXP (&r) * M_LOG10_2;
  1314. for (max_digits = 1; dec_exp ; max_digits++)
  1315. dec_exp /= 10;
  1316. /* Bound the number of digits printed by the size of the output buffer. */
  1317. max_digits = buf_size - 1 - 1 - 2 - max_digits - 1;
  1318. gcc_assert (max_digits <= buf_size);
  1319. if (digits > max_digits)
  1320. digits = max_digits;
  1321. one = real_digit (1);
  1322. ten = ten_to_ptwo (0);
  1323. sign = r.sign;
  1324. r.sign = 0;
  1325. dec_exp = 0;
  1326. pten = *one;
  1327. cmp_one = do_compare (&r, one, 0);
  1328. if (cmp_one > 0)
  1329. {
  1330. int m;
  1331. /* Number is greater than one. Convert significand to an integer
  1332. and strip trailing decimal zeros. */
  1333. u = r;
  1334. SET_REAL_EXP (&u, SIGNIFICAND_BITS - 1);
  1335. /* Largest M, such that 10**2**M fits within SIGNIFICAND_BITS. */
  1336. m = floor_log2 (max_digits);
  1337. /* Iterate over the bits of the possible powers of 10 that might
  1338. be present in U and eliminate them. That is, if we find that
  1339. 10**2**M divides U evenly, keep the division and increase
  1340. DEC_EXP by 2**M. */
  1341. do
  1342. {
  1343. REAL_VALUE_TYPE t;
  1344. do_divide (&t, &u, ten_to_ptwo (m));
  1345. do_fix_trunc (&v, &t);
  1346. if (cmp_significands (&v, &t) == 0)
  1347. {
  1348. u = t;
  1349. dec_exp += 1 << m;
  1350. }
  1351. }
  1352. while (--m >= 0);
  1353. /* Revert the scaling to integer that we performed earlier. */
  1354. SET_REAL_EXP (&u, REAL_EXP (&u) + REAL_EXP (&r)
  1355. - (SIGNIFICAND_BITS - 1));
  1356. r = u;
  1357. /* Find power of 10. Do this by dividing out 10**2**M when
  1358. this is larger than the current remainder. Fill PTEN with
  1359. the power of 10 that we compute. */
  1360. if (REAL_EXP (&r) > 0)
  1361. {
  1362. m = floor_log2 ((int)(REAL_EXP (&r) * M_LOG10_2)) + 1;
  1363. do
  1364. {
  1365. const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
  1366. if (do_compare (&u, ptentwo, 0) >= 0)
  1367. {
  1368. do_divide (&u, &u, ptentwo);
  1369. do_multiply (&pten, &pten, ptentwo);
  1370. dec_exp += 1 << m;
  1371. }
  1372. }
  1373. while (--m >= 0);
  1374. }
  1375. else
  1376. /* We managed to divide off enough tens in the above reduction
  1377. loop that we've now got a negative exponent. Fall into the
  1378. less-than-one code to compute the proper value for PTEN. */
  1379. cmp_one = -1;
  1380. }
  1381. if (cmp_one < 0)
  1382. {
  1383. int m;
  1384. /* Number is less than one. Pad significand with leading
  1385. decimal zeros. */
  1386. v = r;
  1387. while (1)
  1388. {
  1389. /* Stop if we'd shift bits off the bottom. */
  1390. if (v.sig[0] & 7)
  1391. break;
  1392. do_multiply (&u, &v, ten);
  1393. /* Stop if we're now >= 1. */
  1394. if (REAL_EXP (&u) > 0)
  1395. break;
  1396. v = u;
  1397. dec_exp -= 1;
  1398. }
  1399. r = v;
  1400. /* Find power of 10. Do this by multiplying in P=10**2**M when
  1401. the current remainder is smaller than 1/P. Fill PTEN with the
  1402. power of 10 that we compute. */
  1403. m = floor_log2 ((int)(-REAL_EXP (&r) * M_LOG10_2)) + 1;
  1404. do
  1405. {
  1406. const REAL_VALUE_TYPE *ptentwo = ten_to_ptwo (m);
  1407. const REAL_VALUE_TYPE *ptenmtwo = ten_to_mptwo (m);
  1408. if (do_compare (&v, ptenmtwo, 0) <= 0)
  1409. {
  1410. do_multiply (&v, &v, ptentwo);
  1411. do_multiply (&pten, &pten, ptentwo);
  1412. dec_exp -= 1 << m;
  1413. }
  1414. }
  1415. while (--m >= 0);
  1416. /* Invert the positive power of 10 that we've collected so far. */
  1417. do_divide (&pten, one, &pten);
  1418. }
  1419. p = str;
  1420. if (sign)
  1421. *p++ = '-';
  1422. first = p++;
  1423. /* At this point, PTEN should contain the nearest power of 10 smaller
  1424. than R, such that this division produces the first digit.
  1425. Using a divide-step primitive that returns the complete integral
  1426. remainder avoids the rounding error that would be produced if
  1427. we were to use do_divide here and then simply multiply by 10 for
  1428. each subsequent digit. */
  1429. digit = rtd_divmod (&r, &pten);
  1430. /* Be prepared for error in that division via underflow ... */
  1431. if (digit == 0 && cmp_significand_0 (&r))
  1432. {
  1433. /* Multiply by 10 and try again. */
  1434. do_multiply (&r, &r, ten);
  1435. digit = rtd_divmod (&r, &pten);
  1436. dec_exp -= 1;
  1437. gcc_assert (digit != 0);
  1438. }
  1439. /* ... or overflow. */
  1440. if (digit == 10)
  1441. {
  1442. *p++ = '1';
  1443. if (--digits > 0)
  1444. *p++ = '0';
  1445. dec_exp += 1;
  1446. }
  1447. else
  1448. {
  1449. gcc_assert (digit <= 10);
  1450. *p++ = digit + '0';
  1451. }
  1452. /* Generate subsequent digits. */
  1453. while (--digits > 0)
  1454. {
  1455. do_multiply (&r, &r, ten);
  1456. digit = rtd_divmod (&r, &pten);
  1457. *p++ = digit + '0';
  1458. }
  1459. last = p;
  1460. /* Generate one more digit with which to do rounding. */
  1461. do_multiply (&r, &r, ten);
  1462. digit = rtd_divmod (&r, &pten);
  1463. /* Round the result. */
  1464. if (fmt && fmt->round_towards_zero)
  1465. {
  1466. /* If the format uses round towards zero when parsing the string
  1467. back in, we need to always round away from zero here. */
  1468. if (cmp_significand_0 (&r))
  1469. digit++;
  1470. round_up = digit > 0;
  1471. }
  1472. else
  1473. {
  1474. if (digit == 5)
  1475. {
  1476. /* Round to nearest. If R is nonzero there are additional
  1477. nonzero digits to be extracted. */
  1478. if (cmp_significand_0 (&r))
  1479. digit++;
  1480. /* Round to even. */
  1481. else if ((p[-1] - '0') & 1)
  1482. digit++;
  1483. }
  1484. round_up = digit > 5;
  1485. }
  1486. if (round_up)
  1487. {
  1488. while (p > first)
  1489. {
  1490. digit = *--p;
  1491. if (digit == '9')
  1492. *p = '0';
  1493. else
  1494. {
  1495. *p = digit + 1;
  1496. break;
  1497. }
  1498. }
  1499. /* Carry out of the first digit. This means we had all 9's and
  1500. now have all 0's. "Prepend" a 1 by overwriting the first 0. */
  1501. if (p == first)
  1502. {
  1503. first[1] = '1';
  1504. dec_exp++;
  1505. }
  1506. }
  1507. /* Insert the decimal point. */
  1508. first[0] = first[1];
  1509. first[1] = '.';
  1510. /* If requested, drop trailing zeros. Never crop past "1.0". */
  1511. if (crop_trailing_zeros)
  1512. while (last > first + 3 && last[-1] == '0')
  1513. last--;
  1514. /* Append the exponent. */
  1515. sprintf (last, "e%+d", dec_exp);
  1516. #ifdef ENABLE_CHECKING
  1517. /* Verify that we can read the original value back in. */
  1518. if (mode != VOIDmode)
  1519. {
  1520. real_from_string (&r, str);
  1521. real_convert (&r, mode, &r);
  1522. gcc_assert (real_identical (&r, r_orig));
  1523. }
  1524. #endif
  1525. }
  1526. /* Likewise, except always uses round-to-nearest. */
  1527. void
  1528. real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig, size_t buf_size,
  1529. size_t digits, int crop_trailing_zeros)
  1530. {
  1531. real_to_decimal_for_mode (str, r_orig, buf_size,
  1532. digits, crop_trailing_zeros, VOIDmode);
  1533. }
  1534. /* Render R as a hexadecimal floating point constant. Emit DIGITS
  1535. significant digits in the result, bounded by BUF_SIZE. If DIGITS is 0,
  1536. choose the maximum for the representation. If CROP_TRAILING_ZEROS,
  1537. strip trailing zeros. */
  1538. void
  1539. real_to_hexadecimal (char *str, const REAL_VALUE_TYPE *r, size_t buf_size,
  1540. size_t digits, int crop_trailing_zeros)
  1541. {
  1542. int i, j, exp = REAL_EXP (r);
  1543. char *p, *first;
  1544. char exp_buf[16];
  1545. size_t max_digits;
  1546. switch (r->cl)
  1547. {
  1548. case rvc_zero:
  1549. exp = 0;
  1550. break;
  1551. case rvc_normal:
  1552. break;
  1553. case rvc_inf:
  1554. strcpy (str, (r->sign ? "-Inf" : "+Inf"));
  1555. return;
  1556. case rvc_nan:
  1557. /* ??? Print the significand as well, if not canonical? */
  1558. sprintf (str, "%c%cNaN", (r->sign ? '-' : '+'),
  1559. (r->signalling ? 'S' : 'Q'));
  1560. return;
  1561. default:
  1562. gcc_unreachable ();
  1563. }
  1564. if (r->decimal)
  1565. {
  1566. /* Hexadecimal format for decimal floats is not interesting. */
  1567. strcpy (str, "N/A");
  1568. return;
  1569. }
  1570. if (digits == 0)
  1571. digits = SIGNIFICAND_BITS / 4;
  1572. /* Bound the number of digits printed by the size of the output buffer. */
  1573. sprintf (exp_buf, "p%+d", exp);
  1574. max_digits = buf_size - strlen (exp_buf) - r->sign - 4 - 1;
  1575. gcc_assert (max_digits <= buf_size);
  1576. if (digits > max_digits)
  1577. digits = max_digits;
  1578. p = str;
  1579. if (r->sign)
  1580. *p++ = '-';
  1581. *p++ = '0';
  1582. *p++ = 'x';
  1583. *p++ = '0';
  1584. *p++ = '.';
  1585. first = p;
  1586. for (i = SIGSZ - 1; i >= 0; --i)
  1587. for (j = HOST_BITS_PER_LONG - 4; j >= 0; j -= 4)
  1588. {
  1589. *p++ = "0123456789abcdef"[(r->sig[i] >> j) & 15];
  1590. if (--digits == 0)
  1591. goto out;
  1592. }
  1593. out:
  1594. if (crop_trailing_zeros)
  1595. while (p > first + 1 && p[-1] == '0')
  1596. p--;
  1597. sprintf (p, "p%+d", exp);
  1598. }
  1599. /* Initialize R from a decimal or hexadecimal string. The string is
  1600. assumed to have been syntax checked already. Return -1 if the
  1601. value underflows, +1 if overflows, and 0 otherwise. */
  1602. int
  1603. real_from_string (REAL_VALUE_TYPE *r, const char *str)
  1604. {
  1605. int exp = 0;
  1606. bool sign = false;
  1607. get_zero (r, 0);
  1608. if (*str == '-')
  1609. {
  1610. sign = true;
  1611. str++;
  1612. }
  1613. else if (*str == '+')
  1614. str++;
  1615. if (!strncmp (str, "QNaN", 4))
  1616. {
  1617. get_canonical_qnan (r, sign);
  1618. return 0;
  1619. }
  1620. else if (!strncmp (str, "SNaN", 4))
  1621. {
  1622. get_canonical_snan (r, sign);
  1623. return 0;
  1624. }
  1625. else if (!strncmp (str, "Inf", 3))
  1626. {
  1627. get_inf (r, sign);
  1628. return 0;
  1629. }
  1630. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  1631. {
  1632. /* Hexadecimal floating point. */
  1633. int pos = SIGNIFICAND_BITS - 4, d;
  1634. str += 2;
  1635. while (*str == '0')
  1636. str++;
  1637. while (1)
  1638. {
  1639. d = hex_value (*str);
  1640. if (d == _hex_bad)
  1641. break;
  1642. if (pos >= 0)
  1643. {
  1644. r->sig[pos / HOST_BITS_PER_LONG]
  1645. |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
  1646. pos -= 4;
  1647. }
  1648. else if (d)
  1649. /* Ensure correct rounding by setting last bit if there is
  1650. a subsequent nonzero digit. */
  1651. r->sig[0] |= 1;
  1652. exp += 4;
  1653. str++;
  1654. }
  1655. if (*str == '.')
  1656. {
  1657. str++;
  1658. if (pos == SIGNIFICAND_BITS - 4)
  1659. {
  1660. while (*str == '0')
  1661. str++, exp -= 4;
  1662. }
  1663. while (1)
  1664. {
  1665. d = hex_value (*str);
  1666. if (d == _hex_bad)
  1667. break;
  1668. if (pos >= 0)
  1669. {
  1670. r->sig[pos / HOST_BITS_PER_LONG]
  1671. |= (unsigned long) d << (pos % HOST_BITS_PER_LONG);
  1672. pos -= 4;
  1673. }
  1674. else if (d)
  1675. /* Ensure correct rounding by setting last bit if there is
  1676. a subsequent nonzero digit. */
  1677. r->sig[0] |= 1;
  1678. str++;
  1679. }
  1680. }
  1681. /* If the mantissa is zero, ignore the exponent. */
  1682. if (!cmp_significand_0 (r))
  1683. goto is_a_zero;
  1684. if (*str == 'p' || *str == 'P')
  1685. {
  1686. bool exp_neg = false;
  1687. str++;
  1688. if (*str == '-')
  1689. {
  1690. exp_neg = true;
  1691. str++;
  1692. }
  1693. else if (*str == '+')
  1694. str++;
  1695. d = 0;
  1696. while (ISDIGIT (*str))
  1697. {
  1698. d *= 10;
  1699. d += *str - '0';
  1700. if (d > MAX_EXP)
  1701. {
  1702. /* Overflowed the exponent. */
  1703. if (exp_neg)
  1704. goto underflow;
  1705. else
  1706. goto overflow;
  1707. }
  1708. str++;
  1709. }
  1710. if (exp_neg)
  1711. d = -d;
  1712. exp += d;
  1713. }
  1714. r->cl = rvc_normal;
  1715. SET_REAL_EXP (r, exp);
  1716. normalize (r);
  1717. }
  1718. else
  1719. {
  1720. /* Decimal floating point. */
  1721. const char *cstr = str;
  1722. mpfr_t m;
  1723. bool inexact;
  1724. while (*cstr == '0')
  1725. cstr++;
  1726. if (*cstr == '.')
  1727. {
  1728. cstr++;
  1729. while (*cstr == '0')
  1730. cstr++;
  1731. }
  1732. /* If the mantissa is zero, ignore the exponent. */
  1733. if (!ISDIGIT (*cstr))
  1734. goto is_a_zero;
  1735. /* Nonzero value, possibly overflowing or underflowing. */
  1736. mpfr_init2 (m, SIGNIFICAND_BITS);
  1737. inexact = mpfr_strtofr (m, str, NULL, 10, GMP_RNDZ);
  1738. /* The result should never be a NaN, and because the rounding is
  1739. toward zero should never be an infinity. */
  1740. gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m));
  1741. if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4)
  1742. {
  1743. mpfr_clear (m);
  1744. goto underflow;
  1745. }
  1746. else if (mpfr_get_exp (m) > MAX_EXP - 4)
  1747. {
  1748. mpfr_clear (m);
  1749. goto overflow;
  1750. }
  1751. else
  1752. {
  1753. real_from_mpfr (r, m, NULL_TREE, GMP_RNDZ);
  1754. /* 1 to 3 bits may have been shifted off (with a sticky bit)
  1755. because the hex digits used in real_from_mpfr did not
  1756. start with a digit 8 to f, but the exponent bounds above
  1757. should have avoided underflow or overflow. */
  1758. gcc_assert (r->cl == rvc_normal);
  1759. /* Set a sticky bit if mpfr_strtofr was inexact. */
  1760. r->sig[0] |= inexact;
  1761. mpfr_clear (m);
  1762. }
  1763. }
  1764. r->sign = sign;
  1765. return 0;
  1766. is_a_zero:
  1767. get_zero (r, sign);
  1768. return 0;
  1769. underflow:
  1770. get_zero (r, sign);
  1771. return -1;
  1772. overflow:
  1773. get_inf (r, sign);
  1774. return 1;
  1775. }
  1776. /* Legacy. Similar, but return the result directly. */
  1777. REAL_VALUE_TYPE
  1778. real_from_string2 (const char *s, machine_mode mode)
  1779. {
  1780. REAL_VALUE_TYPE r;
  1781. real_from_string (&r, s);
  1782. if (mode != VOIDmode)
  1783. real_convert (&r, mode, &r);
  1784. return r;
  1785. }
  1786. /* Initialize R from string S and desired MODE. */
  1787. void
  1788. real_from_string3 (REAL_VALUE_TYPE *r, const char *s, machine_mode mode)
  1789. {
  1790. if (DECIMAL_FLOAT_MODE_P (mode))
  1791. decimal_real_from_string (r, s);
  1792. else
  1793. real_from_string (r, s);
  1794. if (mode != VOIDmode)
  1795. real_convert (r, mode, r);
  1796. }
  1797. /* Initialize R from the wide_int VAL_IN. The MODE is not VOIDmode,*/
  1798. void
  1799. real_from_integer (REAL_VALUE_TYPE *r, machine_mode mode,
  1800. const wide_int_ref &val_in, signop sgn)
  1801. {
  1802. if (val_in == 0)
  1803. get_zero (r, 0);
  1804. else
  1805. {
  1806. unsigned int len = val_in.get_precision ();
  1807. int i, j, e = 0;
  1808. int maxbitlen = MAX_BITSIZE_MODE_ANY_INT + HOST_BITS_PER_WIDE_INT;
  1809. const unsigned int realmax = (SIGNIFICAND_BITS / HOST_BITS_PER_WIDE_INT
  1810. * HOST_BITS_PER_WIDE_INT);
  1811. memset (r, 0, sizeof (*r));
  1812. r->cl = rvc_normal;
  1813. r->sign = wi::neg_p (val_in, sgn);
  1814. /* We have to ensure we can negate the largest negative number. */
  1815. wide_int val = wide_int::from (val_in, maxbitlen, sgn);
  1816. if (r->sign)
  1817. val = -val;
  1818. /* Ensure a multiple of HOST_BITS_PER_WIDE_INT, ceiling, as elt
  1819. won't work with precisions that are not a multiple of
  1820. HOST_BITS_PER_WIDE_INT. */
  1821. len += HOST_BITS_PER_WIDE_INT - 1;
  1822. /* Ensure we can represent the largest negative number. */
  1823. len += 1;
  1824. len = len/HOST_BITS_PER_WIDE_INT * HOST_BITS_PER_WIDE_INT;
  1825. /* Cap the size to the size allowed by real.h. */
  1826. if (len > realmax)
  1827. {
  1828. HOST_WIDE_INT cnt_l_z;
  1829. cnt_l_z = wi::clz (val);
  1830. if (maxbitlen - cnt_l_z > realmax)
  1831. {
  1832. e = maxbitlen - cnt_l_z - realmax;
  1833. /* This value is too large, we must shift it right to
  1834. preserve all the bits we can, and then bump the
  1835. exponent up by that amount. */
  1836. val = wi::lrshift (val, e);
  1837. }
  1838. len = realmax;
  1839. }
  1840. /* Clear out top bits so elt will work with precisions that aren't
  1841. a multiple of HOST_BITS_PER_WIDE_INT. */
  1842. val = wide_int::from (val, len, sgn);
  1843. len = len / HOST_BITS_PER_WIDE_INT;
  1844. SET_REAL_EXP (r, len * HOST_BITS_PER_WIDE_INT + e);
  1845. j = SIGSZ - 1;
  1846. if (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT)
  1847. for (i = len - 1; i >= 0; i--)
  1848. {
  1849. r->sig[j--] = val.elt (i);
  1850. if (j < 0)
  1851. break;
  1852. }
  1853. else
  1854. {
  1855. gcc_assert (HOST_BITS_PER_LONG*2 == HOST_BITS_PER_WIDE_INT);
  1856. for (i = len - 1; i >= 0; i--)
  1857. {
  1858. HOST_WIDE_INT e = val.elt (i);
  1859. r->sig[j--] = e >> (HOST_BITS_PER_LONG - 1) >> 1;
  1860. if (j < 0)
  1861. break;
  1862. r->sig[j--] = e;
  1863. if (j < 0)
  1864. break;
  1865. }
  1866. }
  1867. normalize (r);
  1868. }
  1869. if (DECIMAL_FLOAT_MODE_P (mode))
  1870. decimal_from_integer (r);
  1871. else if (mode != VOIDmode)
  1872. real_convert (r, mode, r);
  1873. }
  1874. /* Render R, an integral value, as a floating point constant with no
  1875. specified exponent. */
  1876. static void
  1877. decimal_integer_string (char *str, const REAL_VALUE_TYPE *r_orig,
  1878. size_t buf_size)
  1879. {
  1880. int dec_exp, digit, digits;
  1881. REAL_VALUE_TYPE r, pten;
  1882. char *p;
  1883. bool sign;
  1884. r = *r_orig;
  1885. if (r.cl == rvc_zero)
  1886. {
  1887. strcpy (str, "0.");
  1888. return;
  1889. }
  1890. sign = r.sign;
  1891. r.sign = 0;
  1892. dec_exp = REAL_EXP (&r) * M_LOG10_2;
  1893. digits = dec_exp + 1;
  1894. gcc_assert ((digits + 2) < (int)buf_size);
  1895. pten = *real_digit (1);
  1896. times_pten (&pten, dec_exp);
  1897. p = str;
  1898. if (sign)
  1899. *p++ = '-';
  1900. digit = rtd_divmod (&r, &pten);
  1901. gcc_assert (digit >= 0 && digit <= 9);
  1902. *p++ = digit + '0';
  1903. while (--digits > 0)
  1904. {
  1905. times_pten (&r, 1);
  1906. digit = rtd_divmod (&r, &pten);
  1907. *p++ = digit + '0';
  1908. }
  1909. *p++ = '.';
  1910. *p++ = '\0';
  1911. }
  1912. /* Convert a real with an integral value to decimal float. */
  1913. static void
  1914. decimal_from_integer (REAL_VALUE_TYPE *r)
  1915. {
  1916. char str[256];
  1917. decimal_integer_string (str, r, sizeof (str) - 1);
  1918. decimal_real_from_string (r, str);
  1919. }
  1920. /* Returns 10**2**N. */
  1921. static const REAL_VALUE_TYPE *
  1922. ten_to_ptwo (int n)
  1923. {
  1924. static REAL_VALUE_TYPE tens[EXP_BITS];
  1925. gcc_assert (n >= 0);
  1926. gcc_assert (n < EXP_BITS);
  1927. if (tens[n].cl == rvc_zero)
  1928. {
  1929. if (n < (HOST_BITS_PER_WIDE_INT == 64 ? 5 : 4))
  1930. {
  1931. HOST_WIDE_INT t = 10;
  1932. int i;
  1933. for (i = 0; i < n; ++i)
  1934. t *= t;
  1935. real_from_integer (&tens[n], VOIDmode, t, UNSIGNED);
  1936. }
  1937. else
  1938. {
  1939. const REAL_VALUE_TYPE *t = ten_to_ptwo (n - 1);
  1940. do_multiply (&tens[n], t, t);
  1941. }
  1942. }
  1943. return &tens[n];
  1944. }
  1945. /* Returns 10**(-2**N). */
  1946. static const REAL_VALUE_TYPE *
  1947. ten_to_mptwo (int n)
  1948. {
  1949. static REAL_VALUE_TYPE tens[EXP_BITS];
  1950. gcc_assert (n >= 0);
  1951. gcc_assert (n < EXP_BITS);
  1952. if (tens[n].cl == rvc_zero)
  1953. do_divide (&tens[n], real_digit (1), ten_to_ptwo (n));
  1954. return &tens[n];
  1955. }
  1956. /* Returns N. */
  1957. static const REAL_VALUE_TYPE *
  1958. real_digit (int n)
  1959. {
  1960. static REAL_VALUE_TYPE num[10];
  1961. gcc_assert (n >= 0);
  1962. gcc_assert (n <= 9);
  1963. if (n > 0 && num[n].cl == rvc_zero)
  1964. real_from_integer (&num[n], VOIDmode, n, UNSIGNED);
  1965. return &num[n];
  1966. }
  1967. /* Multiply R by 10**EXP. */
  1968. static void
  1969. times_pten (REAL_VALUE_TYPE *r, int exp)
  1970. {
  1971. REAL_VALUE_TYPE pten, *rr;
  1972. bool negative = (exp < 0);
  1973. int i;
  1974. if (negative)
  1975. {
  1976. exp = -exp;
  1977. pten = *real_digit (1);
  1978. rr = &pten;
  1979. }
  1980. else
  1981. rr = r;
  1982. for (i = 0; exp > 0; ++i, exp >>= 1)
  1983. if (exp & 1)
  1984. do_multiply (rr, rr, ten_to_ptwo (i));
  1985. if (negative)
  1986. do_divide (r, r, &pten);
  1987. }
  1988. /* Returns the special REAL_VALUE_TYPE corresponding to 'e'. */
  1989. const REAL_VALUE_TYPE *
  1990. dconst_e_ptr (void)
  1991. {
  1992. static REAL_VALUE_TYPE value;
  1993. /* Initialize mathematical constants for constant folding builtins.
  1994. These constants need to be given to at least 160 bits precision. */
  1995. if (value.cl == rvc_zero)
  1996. {
  1997. mpfr_t m;
  1998. mpfr_init2 (m, SIGNIFICAND_BITS);
  1999. mpfr_set_ui (m, 1, GMP_RNDN);
  2000. mpfr_exp (m, m, GMP_RNDN);
  2001. real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
  2002. mpfr_clear (m);
  2003. }
  2004. return &value;
  2005. }
  2006. /* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */
  2007. const REAL_VALUE_TYPE *
  2008. dconst_third_ptr (void)
  2009. {
  2010. static REAL_VALUE_TYPE value;
  2011. /* Initialize mathematical constants for constant folding builtins.
  2012. These constants need to be given to at least 160 bits precision. */
  2013. if (value.cl == rvc_zero)
  2014. {
  2015. real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3));
  2016. }
  2017. return &value;
  2018. }
  2019. /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */
  2020. const REAL_VALUE_TYPE *
  2021. dconst_sqrt2_ptr (void)
  2022. {
  2023. static REAL_VALUE_TYPE value;
  2024. /* Initialize mathematical constants for constant folding builtins.
  2025. These constants need to be given to at least 160 bits precision. */
  2026. if (value.cl == rvc_zero)
  2027. {
  2028. mpfr_t m;
  2029. mpfr_init2 (m, SIGNIFICAND_BITS);
  2030. mpfr_sqrt_ui (m, 2, GMP_RNDN);
  2031. real_from_mpfr (&value, m, NULL_TREE, GMP_RNDN);
  2032. mpfr_clear (m);
  2033. }
  2034. return &value;
  2035. }
  2036. /* Fills R with +Inf. */
  2037. void
  2038. real_inf (REAL_VALUE_TYPE *r)
  2039. {
  2040. get_inf (r, 0);
  2041. }
  2042. /* Fills R with a NaN whose significand is described by STR. If QUIET,
  2043. we force a QNaN, else we force an SNaN. The string, if not empty,
  2044. is parsed as a number and placed in the significand. Return true
  2045. if the string was successfully parsed. */
  2046. bool
  2047. real_nan (REAL_VALUE_TYPE *r, const char *str, int quiet,
  2048. machine_mode mode)
  2049. {
  2050. const struct real_format *fmt;
  2051. fmt = REAL_MODE_FORMAT (mode);
  2052. gcc_assert (fmt);
  2053. if (*str == 0)
  2054. {
  2055. if (quiet)
  2056. get_canonical_qnan (r, 0);
  2057. else
  2058. get_canonical_snan (r, 0);
  2059. }
  2060. else
  2061. {
  2062. int base = 10, d;
  2063. memset (r, 0, sizeof (*r));
  2064. r->cl = rvc_nan;
  2065. /* Parse akin to strtol into the significand of R. */
  2066. while (ISSPACE (*str))
  2067. str++;
  2068. if (*str == '-')
  2069. str++;
  2070. else if (*str == '+')
  2071. str++;
  2072. if (*str == '0')
  2073. {
  2074. str++;
  2075. if (*str == 'x' || *str == 'X')
  2076. {
  2077. base = 16;
  2078. str++;
  2079. }
  2080. else
  2081. base = 8;
  2082. }
  2083. while ((d = hex_value (*str)) < base)
  2084. {
  2085. REAL_VALUE_TYPE u;
  2086. switch (base)
  2087. {
  2088. case 8:
  2089. lshift_significand (r, r, 3);
  2090. break;
  2091. case 16:
  2092. lshift_significand (r, r, 4);
  2093. break;
  2094. case 10:
  2095. lshift_significand_1 (&u, r);
  2096. lshift_significand (r, r, 3);
  2097. add_significands (r, r, &u);
  2098. break;
  2099. default:
  2100. gcc_unreachable ();
  2101. }
  2102. get_zero (&u, 0);
  2103. u.sig[0] = d;
  2104. add_significands (r, r, &u);
  2105. str++;
  2106. }
  2107. /* Must have consumed the entire string for success. */
  2108. if (*str != 0)
  2109. return false;
  2110. /* Shift the significand into place such that the bits
  2111. are in the most significant bits for the format. */
  2112. lshift_significand (r, r, SIGNIFICAND_BITS - fmt->pnan);
  2113. /* Our MSB is always unset for NaNs. */
  2114. r->sig[SIGSZ-1] &= ~SIG_MSB;
  2115. /* Force quiet or signalling NaN. */
  2116. r->signalling = !quiet;
  2117. }
  2118. return true;
  2119. }
  2120. /* Fills R with the largest finite value representable in mode MODE.
  2121. If SIGN is nonzero, R is set to the most negative finite value. */
  2122. void
  2123. real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
  2124. {
  2125. const struct real_format *fmt;
  2126. int np2;
  2127. fmt = REAL_MODE_FORMAT (mode);
  2128. gcc_assert (fmt);
  2129. memset (r, 0, sizeof (*r));
  2130. if (fmt->b == 10)
  2131. decimal_real_maxval (r, sign, mode);
  2132. else
  2133. {
  2134. r->cl = rvc_normal;
  2135. r->sign = sign;
  2136. SET_REAL_EXP (r, fmt->emax);
  2137. np2 = SIGNIFICAND_BITS - fmt->p;
  2138. memset (r->sig, -1, SIGSZ * sizeof (unsigned long));
  2139. clear_significand_below (r, np2);
  2140. if (fmt->pnan < fmt->p)
  2141. /* This is an IBM extended double format made up of two IEEE
  2142. doubles. The value of the long double is the sum of the
  2143. values of the two parts. The most significant part is
  2144. required to be the value of the long double rounded to the
  2145. nearest double. Rounding means we need a slightly smaller
  2146. value for LDBL_MAX. */
  2147. clear_significand_bit (r, SIGNIFICAND_BITS - fmt->pnan - 1);
  2148. }
  2149. }
  2150. /* Fills R with 2**N. */
  2151. void
  2152. real_2expN (REAL_VALUE_TYPE *r, int n, machine_mode fmode)
  2153. {
  2154. memset (r, 0, sizeof (*r));
  2155. n++;
  2156. if (n > MAX_EXP)
  2157. r->cl = rvc_inf;
  2158. else if (n < -MAX_EXP)
  2159. ;
  2160. else
  2161. {
  2162. r->cl = rvc_normal;
  2163. SET_REAL_EXP (r, n);
  2164. r->sig[SIGSZ-1] = SIG_MSB;
  2165. }
  2166. if (DECIMAL_FLOAT_MODE_P (fmode))
  2167. decimal_real_convert (r, fmode, r);
  2168. }
  2169. static void
  2170. round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
  2171. {
  2172. int p2, np2, i, w;
  2173. int emin2m1, emax2;
  2174. bool round_up = false;
  2175. if (r->decimal)
  2176. {
  2177. if (fmt->b == 10)
  2178. {
  2179. decimal_round_for_format (fmt, r);
  2180. return;
  2181. }
  2182. /* FIXME. We can come here via fp_easy_constant
  2183. (e.g. -O0 on '_Decimal32 x = 1.0 + 2.0dd'), but have not
  2184. investigated whether this convert needs to be here, or
  2185. something else is missing. */
  2186. decimal_real_convert (r, DFmode, r);
  2187. }
  2188. p2 = fmt->p;
  2189. emin2m1 = fmt->emin - 1;
  2190. emax2 = fmt->emax;
  2191. np2 = SIGNIFICAND_BITS - p2;
  2192. switch (r->cl)
  2193. {
  2194. underflow:
  2195. get_zero (r, r->sign);
  2196. case rvc_zero:
  2197. if (!fmt->has_signed_zero)
  2198. r->sign = 0;
  2199. return;
  2200. overflow:
  2201. get_inf (r, r->sign);
  2202. case rvc_inf:
  2203. return;
  2204. case rvc_nan:
  2205. clear_significand_below (r, np2);
  2206. return;
  2207. case rvc_normal:
  2208. break;
  2209. default:
  2210. gcc_unreachable ();
  2211. }
  2212. /* Check the range of the exponent. If we're out of range,
  2213. either underflow or overflow. */
  2214. if (REAL_EXP (r) > emax2)
  2215. goto overflow;
  2216. else if (REAL_EXP (r) <= emin2m1)
  2217. {
  2218. int diff;
  2219. if (!fmt->has_denorm)
  2220. {
  2221. /* Don't underflow completely until we've had a chance to round. */
  2222. if (REAL_EXP (r) < emin2m1)
  2223. goto underflow;
  2224. }
  2225. else
  2226. {
  2227. diff = emin2m1 - REAL_EXP (r) + 1;
  2228. if (diff > p2)
  2229. goto underflow;
  2230. /* De-normalize the significand. */
  2231. r->sig[0] |= sticky_rshift_significand (r, r, diff);
  2232. SET_REAL_EXP (r, REAL_EXP (r) + diff);
  2233. }
  2234. }
  2235. if (!fmt->round_towards_zero)
  2236. {
  2237. /* There are P2 true significand bits, followed by one guard bit,
  2238. followed by one sticky bit, followed by stuff. Fold nonzero
  2239. stuff into the sticky bit. */
  2240. unsigned long sticky;
  2241. bool guard, lsb;
  2242. sticky = 0;
  2243. for (i = 0, w = (np2 - 1) / HOST_BITS_PER_LONG; i < w; ++i)
  2244. sticky |= r->sig[i];
  2245. sticky |= r->sig[w]
  2246. & (((unsigned long)1 << ((np2 - 1) % HOST_BITS_PER_LONG)) - 1);
  2247. guard = test_significand_bit (r, np2 - 1);
  2248. lsb = test_significand_bit (r, np2);
  2249. /* Round to even. */
  2250. round_up = guard && (sticky || lsb);
  2251. }
  2252. if (round_up)
  2253. {
  2254. REAL_VALUE_TYPE u;
  2255. get_zero (&u, 0);
  2256. set_significand_bit (&u, np2);
  2257. if (add_significands (r, r, &u))
  2258. {
  2259. /* Overflow. Means the significand had been all ones, and
  2260. is now all zeros. Need to increase the exponent, and
  2261. possibly re-normalize it. */
  2262. SET_REAL_EXP (r, REAL_EXP (r) + 1);
  2263. if (REAL_EXP (r) > emax2)
  2264. goto overflow;
  2265. r->sig[SIGSZ-1] = SIG_MSB;
  2266. }
  2267. }
  2268. /* Catch underflow that we deferred until after rounding. */
  2269. if (REAL_EXP (r) <= emin2m1)
  2270. goto underflow;
  2271. /* Clear out trailing garbage. */
  2272. clear_significand_below (r, np2);
  2273. }
  2274. /* Extend or truncate to a new mode. */
  2275. void
  2276. real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
  2277. const REAL_VALUE_TYPE *a)
  2278. {
  2279. const struct real_format *fmt;
  2280. fmt = REAL_MODE_FORMAT (mode);
  2281. gcc_assert (fmt);
  2282. *r = *a;
  2283. if (a->decimal || fmt->b == 10)
  2284. decimal_real_convert (r, mode, a);
  2285. round_for_format (fmt, r);
  2286. /* round_for_format de-normalizes denormals. Undo just that part. */
  2287. if (r->cl == rvc_normal)
  2288. normalize (r);
  2289. }
  2290. /* Legacy. Likewise, except return the struct directly. */
  2291. REAL_VALUE_TYPE
  2292. real_value_truncate (machine_mode mode, REAL_VALUE_TYPE a)
  2293. {
  2294. REAL_VALUE_TYPE r;
  2295. real_convert (&r, mode, &a);
  2296. return r;
  2297. }
  2298. /* Return true if truncating to MODE is exact. */
  2299. bool
  2300. exact_real_truncate (machine_mode mode, const REAL_VALUE_TYPE *a)
  2301. {
  2302. const struct real_format *fmt;
  2303. REAL_VALUE_TYPE t;
  2304. int emin2m1;
  2305. fmt = REAL_MODE_FORMAT (mode);
  2306. gcc_assert (fmt);
  2307. /* Don't allow conversion to denormals. */
  2308. emin2m1 = fmt->emin - 1;
  2309. if (REAL_EXP (a) <= emin2m1)
  2310. return false;
  2311. /* After conversion to the new mode, the value must be identical. */
  2312. real_convert (&t, mode, a);
  2313. return real_identical (&t, a);
  2314. }
  2315. /* Write R to the given target format. Place the words of the result
  2316. in target word order in BUF. There are always 32 bits in each
  2317. long, no matter the size of the host long.
  2318. Legacy: return word 0 for implementing REAL_VALUE_TO_TARGET_SINGLE. */
  2319. long
  2320. real_to_target_fmt (long *buf, const REAL_VALUE_TYPE *r_orig,
  2321. const struct real_format *fmt)
  2322. {
  2323. REAL_VALUE_TYPE r;
  2324. long buf1;
  2325. r = *r_orig;
  2326. round_for_format (fmt, &r);
  2327. if (!buf)
  2328. buf = &buf1;
  2329. (*fmt->encode) (fmt, buf, &r);
  2330. return *buf;
  2331. }
  2332. /* Similar, but look up the format from MODE. */
  2333. long
  2334. real_to_target (long *buf, const REAL_VALUE_TYPE *r, machine_mode mode)
  2335. {
  2336. const struct real_format *fmt;
  2337. fmt = REAL_MODE_FORMAT (mode);
  2338. gcc_assert (fmt);
  2339. return real_to_target_fmt (buf, r, fmt);
  2340. }
  2341. /* Read R from the given target format. Read the words of the result
  2342. in target word order in BUF. There are always 32 bits in each
  2343. long, no matter the size of the host long. */
  2344. void
  2345. real_from_target_fmt (REAL_VALUE_TYPE *r, const long *buf,
  2346. const struct real_format *fmt)
  2347. {
  2348. (*fmt->decode) (fmt, r, buf);
  2349. }
  2350. /* Similar, but look up the format from MODE. */
  2351. void
  2352. real_from_target (REAL_VALUE_TYPE *r, const long *buf, machine_mode mode)
  2353. {
  2354. const struct real_format *fmt;
  2355. fmt = REAL_MODE_FORMAT (mode);
  2356. gcc_assert (fmt);
  2357. (*fmt->decode) (fmt, r, buf);
  2358. }
  2359. /* Return the number of bits of the largest binary value that the
  2360. significand of MODE will hold. */
  2361. /* ??? Legacy. Should get access to real_format directly. */
  2362. int
  2363. significand_size (machine_mode mode)
  2364. {
  2365. const struct real_format *fmt;
  2366. fmt = REAL_MODE_FORMAT (mode);
  2367. if (fmt == NULL)
  2368. return 0;
  2369. if (fmt->b == 10)
  2370. {
  2371. /* Return the size in bits of the largest binary value that can be
  2372. held by the decimal coefficient for this mode. This is one more
  2373. than the number of bits required to hold the largest coefficient
  2374. of this mode. */
  2375. double log2_10 = 3.3219281;
  2376. return fmt->p * log2_10;
  2377. }
  2378. return fmt->p;
  2379. }
  2380. /* Return a hash value for the given real value. */
  2381. /* ??? The "unsigned int" return value is intended to be hashval_t,
  2382. but I didn't want to pull hashtab.h into real.h. */
  2383. unsigned int
  2384. real_hash (const REAL_VALUE_TYPE *r)
  2385. {
  2386. unsigned int h;
  2387. size_t i;
  2388. h = r->cl | (r->sign << 2);
  2389. switch (r->cl)
  2390. {
  2391. case rvc_zero:
  2392. case rvc_inf:
  2393. return h;
  2394. case rvc_normal:
  2395. h |= REAL_EXP (r) << 3;
  2396. break;
  2397. case rvc_nan:
  2398. if (r->signalling)
  2399. h ^= (unsigned int)-1;
  2400. if (r->canonical)
  2401. return h;
  2402. break;
  2403. default:
  2404. gcc_unreachable ();
  2405. }
  2406. if (sizeof (unsigned long) > sizeof (unsigned int))
  2407. for (i = 0; i < SIGSZ; ++i)
  2408. {
  2409. unsigned long s = r->sig[i];
  2410. h ^= s ^ (s >> (HOST_BITS_PER_LONG / 2));
  2411. }
  2412. else
  2413. for (i = 0; i < SIGSZ; ++i)
  2414. h ^= r->sig[i];
  2415. return h;
  2416. }
  2417. /* IEEE single-precision format. */
  2418. static void encode_ieee_single (const struct real_format *fmt,
  2419. long *, const REAL_VALUE_TYPE *);
  2420. static void decode_ieee_single (const struct real_format *,
  2421. REAL_VALUE_TYPE *, const long *);
  2422. static void
  2423. encode_ieee_single (const struct real_format *fmt, long *buf,
  2424. const REAL_VALUE_TYPE *r)
  2425. {
  2426. unsigned long image, sig, exp;
  2427. unsigned long sign = r->sign;
  2428. bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
  2429. image = sign << 31;
  2430. sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
  2431. switch (r->cl)
  2432. {
  2433. case rvc_zero:
  2434. break;
  2435. case rvc_inf:
  2436. if (fmt->has_inf)
  2437. image |= 255 << 23;
  2438. else
  2439. image |= 0x7fffffff;
  2440. break;
  2441. case rvc_nan:
  2442. if (fmt->has_nans)
  2443. {
  2444. if (r->canonical)
  2445. sig = (fmt->canonical_nan_lsbs_set ? (1 << 22) - 1 : 0);
  2446. if (r->signalling == fmt->qnan_msb_set)
  2447. sig &= ~(1 << 22);
  2448. else
  2449. sig |= 1 << 22;
  2450. if (sig == 0)
  2451. sig = 1 << 21;
  2452. image |= 255 << 23;
  2453. image |= sig;
  2454. }
  2455. else
  2456. image |= 0x7fffffff;
  2457. break;
  2458. case rvc_normal:
  2459. /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
  2460. whereas the intermediate representation is 0.F x 2**exp.
  2461. Which means we're off by one. */
  2462. if (denormal)
  2463. exp = 0;
  2464. else
  2465. exp = REAL_EXP (r) + 127 - 1;
  2466. image |= exp << 23;
  2467. image |= sig;
  2468. break;
  2469. default:
  2470. gcc_unreachable ();
  2471. }
  2472. buf[0] = image;
  2473. }
  2474. static void
  2475. decode_ieee_single (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  2476. const long *buf)
  2477. {
  2478. unsigned long image = buf[0] & 0xffffffff;
  2479. bool sign = (image >> 31) & 1;
  2480. int exp = (image >> 23) & 0xff;
  2481. memset (r, 0, sizeof (*r));
  2482. image <<= HOST_BITS_PER_LONG - 24;
  2483. image &= ~SIG_MSB;
  2484. if (exp == 0)
  2485. {
  2486. if (image && fmt->has_denorm)
  2487. {
  2488. r->cl = rvc_normal;
  2489. r->sign = sign;
  2490. SET_REAL_EXP (r, -126);
  2491. r->sig[SIGSZ-1] = image << 1;
  2492. normalize (r);
  2493. }
  2494. else if (fmt->has_signed_zero)
  2495. r->sign = sign;
  2496. }
  2497. else if (exp == 255 && (fmt->has_nans || fmt->has_inf))
  2498. {
  2499. if (image)
  2500. {
  2501. r->cl = rvc_nan;
  2502. r->sign = sign;
  2503. r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
  2504. ^ fmt->qnan_msb_set);
  2505. r->sig[SIGSZ-1] = image;
  2506. }
  2507. else
  2508. {
  2509. r->cl = rvc_inf;
  2510. r->sign = sign;
  2511. }
  2512. }
  2513. else
  2514. {
  2515. r->cl = rvc_normal;
  2516. r->sign = sign;
  2517. SET_REAL_EXP (r, exp - 127 + 1);
  2518. r->sig[SIGSZ-1] = image | SIG_MSB;
  2519. }
  2520. }
  2521. const struct real_format ieee_single_format =
  2522. {
  2523. encode_ieee_single,
  2524. decode_ieee_single,
  2525. 2,
  2526. 24,
  2527. 24,
  2528. -125,
  2529. 128,
  2530. 31,
  2531. 31,
  2532. false,
  2533. true,
  2534. true,
  2535. true,
  2536. true,
  2537. true,
  2538. true,
  2539. false,
  2540. "ieee_single"
  2541. };
  2542. const struct real_format mips_single_format =
  2543. {
  2544. encode_ieee_single,
  2545. decode_ieee_single,
  2546. 2,
  2547. 24,
  2548. 24,
  2549. -125,
  2550. 128,
  2551. 31,
  2552. 31,
  2553. false,
  2554. true,
  2555. true,
  2556. true,
  2557. true,
  2558. true,
  2559. false,
  2560. true,
  2561. "mips_single"
  2562. };
  2563. const struct real_format motorola_single_format =
  2564. {
  2565. encode_ieee_single,
  2566. decode_ieee_single,
  2567. 2,
  2568. 24,
  2569. 24,
  2570. -125,
  2571. 128,
  2572. 31,
  2573. 31,
  2574. false,
  2575. true,
  2576. true,
  2577. true,
  2578. true,
  2579. true,
  2580. true,
  2581. true,
  2582. "motorola_single"
  2583. };
  2584. /* SPU Single Precision (Extended-Range Mode) format is the same as IEEE
  2585. single precision with the following differences:
  2586. - Infinities are not supported. Instead MAX_FLOAT or MIN_FLOAT
  2587. are generated.
  2588. - NaNs are not supported.
  2589. - The range of non-zero numbers in binary is
  2590. (001)[1.]000...000 to (255)[1.]111...111.
  2591. - Denormals can be represented, but are treated as +0.0 when
  2592. used as an operand and are never generated as a result.
  2593. - -0.0 can be represented, but a zero result is always +0.0.
  2594. - the only supported rounding mode is trunction (towards zero). */
  2595. const struct real_format spu_single_format =
  2596. {
  2597. encode_ieee_single,
  2598. decode_ieee_single,
  2599. 2,
  2600. 24,
  2601. 24,
  2602. -125,
  2603. 129,
  2604. 31,
  2605. 31,
  2606. true,
  2607. false,
  2608. false,
  2609. false,
  2610. true,
  2611. true,
  2612. false,
  2613. false,
  2614. "spu_single"
  2615. };
  2616. /* IEEE double-precision format. */
  2617. static void encode_ieee_double (const struct real_format *fmt,
  2618. long *, const REAL_VALUE_TYPE *);
  2619. static void decode_ieee_double (const struct real_format *,
  2620. REAL_VALUE_TYPE *, const long *);
  2621. static void
  2622. encode_ieee_double (const struct real_format *fmt, long *buf,
  2623. const REAL_VALUE_TYPE *r)
  2624. {
  2625. unsigned long image_lo, image_hi, sig_lo, sig_hi, exp;
  2626. bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
  2627. image_hi = r->sign << 31;
  2628. image_lo = 0;
  2629. if (HOST_BITS_PER_LONG == 64)
  2630. {
  2631. sig_hi = r->sig[SIGSZ-1];
  2632. sig_lo = (sig_hi >> (64 - 53)) & 0xffffffff;
  2633. sig_hi = (sig_hi >> (64 - 53 + 1) >> 31) & 0xfffff;
  2634. }
  2635. else
  2636. {
  2637. sig_hi = r->sig[SIGSZ-1];
  2638. sig_lo = r->sig[SIGSZ-2];
  2639. sig_lo = (sig_hi << 21) | (sig_lo >> 11);
  2640. sig_hi = (sig_hi >> 11) & 0xfffff;
  2641. }
  2642. switch (r->cl)
  2643. {
  2644. case rvc_zero:
  2645. break;
  2646. case rvc_inf:
  2647. if (fmt->has_inf)
  2648. image_hi |= 2047 << 20;
  2649. else
  2650. {
  2651. image_hi |= 0x7fffffff;
  2652. image_lo = 0xffffffff;
  2653. }
  2654. break;
  2655. case rvc_nan:
  2656. if (fmt->has_nans)
  2657. {
  2658. if (r->canonical)
  2659. {
  2660. if (fmt->canonical_nan_lsbs_set)
  2661. {
  2662. sig_hi = (1 << 19) - 1;
  2663. sig_lo = 0xffffffff;
  2664. }
  2665. else
  2666. {
  2667. sig_hi = 0;
  2668. sig_lo = 0;
  2669. }
  2670. }
  2671. if (r->signalling == fmt->qnan_msb_set)
  2672. sig_hi &= ~(1 << 19);
  2673. else
  2674. sig_hi |= 1 << 19;
  2675. if (sig_hi == 0 && sig_lo == 0)
  2676. sig_hi = 1 << 18;
  2677. image_hi |= 2047 << 20;
  2678. image_hi |= sig_hi;
  2679. image_lo = sig_lo;
  2680. }
  2681. else
  2682. {
  2683. image_hi |= 0x7fffffff;
  2684. image_lo = 0xffffffff;
  2685. }
  2686. break;
  2687. case rvc_normal:
  2688. /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
  2689. whereas the intermediate representation is 0.F x 2**exp.
  2690. Which means we're off by one. */
  2691. if (denormal)
  2692. exp = 0;
  2693. else
  2694. exp = REAL_EXP (r) + 1023 - 1;
  2695. image_hi |= exp << 20;
  2696. image_hi |= sig_hi;
  2697. image_lo = sig_lo;
  2698. break;
  2699. default:
  2700. gcc_unreachable ();
  2701. }
  2702. if (FLOAT_WORDS_BIG_ENDIAN)
  2703. buf[0] = image_hi, buf[1] = image_lo;
  2704. else
  2705. buf[0] = image_lo, buf[1] = image_hi;
  2706. }
  2707. static void
  2708. decode_ieee_double (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  2709. const long *buf)
  2710. {
  2711. unsigned long image_hi, image_lo;
  2712. bool sign;
  2713. int exp;
  2714. if (FLOAT_WORDS_BIG_ENDIAN)
  2715. image_hi = buf[0], image_lo = buf[1];
  2716. else
  2717. image_lo = buf[0], image_hi = buf[1];
  2718. image_lo &= 0xffffffff;
  2719. image_hi &= 0xffffffff;
  2720. sign = (image_hi >> 31) & 1;
  2721. exp = (image_hi >> 20) & 0x7ff;
  2722. memset (r, 0, sizeof (*r));
  2723. image_hi <<= 32 - 21;
  2724. image_hi |= image_lo >> 21;
  2725. image_hi &= 0x7fffffff;
  2726. image_lo <<= 32 - 21;
  2727. if (exp == 0)
  2728. {
  2729. if ((image_hi || image_lo) && fmt->has_denorm)
  2730. {
  2731. r->cl = rvc_normal;
  2732. r->sign = sign;
  2733. SET_REAL_EXP (r, -1022);
  2734. if (HOST_BITS_PER_LONG == 32)
  2735. {
  2736. image_hi = (image_hi << 1) | (image_lo >> 31);
  2737. image_lo <<= 1;
  2738. r->sig[SIGSZ-1] = image_hi;
  2739. r->sig[SIGSZ-2] = image_lo;
  2740. }
  2741. else
  2742. {
  2743. image_hi = (image_hi << 31 << 2) | (image_lo << 1);
  2744. r->sig[SIGSZ-1] = image_hi;
  2745. }
  2746. normalize (r);
  2747. }
  2748. else if (fmt->has_signed_zero)
  2749. r->sign = sign;
  2750. }
  2751. else if (exp == 2047 && (fmt->has_nans || fmt->has_inf))
  2752. {
  2753. if (image_hi || image_lo)
  2754. {
  2755. r->cl = rvc_nan;
  2756. r->sign = sign;
  2757. r->signalling = ((image_hi >> 30) & 1) ^ fmt->qnan_msb_set;
  2758. if (HOST_BITS_PER_LONG == 32)
  2759. {
  2760. r->sig[SIGSZ-1] = image_hi;
  2761. r->sig[SIGSZ-2] = image_lo;
  2762. }
  2763. else
  2764. r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo;
  2765. }
  2766. else
  2767. {
  2768. r->cl = rvc_inf;
  2769. r->sign = sign;
  2770. }
  2771. }
  2772. else
  2773. {
  2774. r->cl = rvc_normal;
  2775. r->sign = sign;
  2776. SET_REAL_EXP (r, exp - 1023 + 1);
  2777. if (HOST_BITS_PER_LONG == 32)
  2778. {
  2779. r->sig[SIGSZ-1] = image_hi | SIG_MSB;
  2780. r->sig[SIGSZ-2] = image_lo;
  2781. }
  2782. else
  2783. r->sig[SIGSZ-1] = (image_hi << 31 << 1) | image_lo | SIG_MSB;
  2784. }
  2785. }
  2786. const struct real_format ieee_double_format =
  2787. {
  2788. encode_ieee_double,
  2789. decode_ieee_double,
  2790. 2,
  2791. 53,
  2792. 53,
  2793. -1021,
  2794. 1024,
  2795. 63,
  2796. 63,
  2797. false,
  2798. true,
  2799. true,
  2800. true,
  2801. true,
  2802. true,
  2803. true,
  2804. false,
  2805. "ieee_double"
  2806. };
  2807. const struct real_format mips_double_format =
  2808. {
  2809. encode_ieee_double,
  2810. decode_ieee_double,
  2811. 2,
  2812. 53,
  2813. 53,
  2814. -1021,
  2815. 1024,
  2816. 63,
  2817. 63,
  2818. false,
  2819. true,
  2820. true,
  2821. true,
  2822. true,
  2823. true,
  2824. false,
  2825. true,
  2826. "mips_double"
  2827. };
  2828. const struct real_format motorola_double_format =
  2829. {
  2830. encode_ieee_double,
  2831. decode_ieee_double,
  2832. 2,
  2833. 53,
  2834. 53,
  2835. -1021,
  2836. 1024,
  2837. 63,
  2838. 63,
  2839. false,
  2840. true,
  2841. true,
  2842. true,
  2843. true,
  2844. true,
  2845. true,
  2846. true,
  2847. "motorola_double"
  2848. };
  2849. /* IEEE extended real format. This comes in three flavors: Intel's as
  2850. a 12 byte image, Intel's as a 16 byte image, and Motorola's. Intel
  2851. 12- and 16-byte images may be big- or little endian; Motorola's is
  2852. always big endian. */
  2853. /* Helper subroutine which converts from the internal format to the
  2854. 12-byte little-endian Intel format. Functions below adjust this
  2855. for the other possible formats. */
  2856. static void
  2857. encode_ieee_extended (const struct real_format *fmt, long *buf,
  2858. const REAL_VALUE_TYPE *r)
  2859. {
  2860. unsigned long image_hi, sig_hi, sig_lo;
  2861. bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
  2862. image_hi = r->sign << 15;
  2863. sig_hi = sig_lo = 0;
  2864. switch (r->cl)
  2865. {
  2866. case rvc_zero:
  2867. break;
  2868. case rvc_inf:
  2869. if (fmt->has_inf)
  2870. {
  2871. image_hi |= 32767;
  2872. /* Intel requires the explicit integer bit to be set, otherwise
  2873. it considers the value a "pseudo-infinity". Motorola docs
  2874. say it doesn't care. */
  2875. sig_hi = 0x80000000;
  2876. }
  2877. else
  2878. {
  2879. image_hi |= 32767;
  2880. sig_lo = sig_hi = 0xffffffff;
  2881. }
  2882. break;
  2883. case rvc_nan:
  2884. if (fmt->has_nans)
  2885. {
  2886. image_hi |= 32767;
  2887. if (r->canonical)
  2888. {
  2889. if (fmt->canonical_nan_lsbs_set)
  2890. {
  2891. sig_hi = (1 << 30) - 1;
  2892. sig_lo = 0xffffffff;
  2893. }
  2894. }
  2895. else if (HOST_BITS_PER_LONG == 32)
  2896. {
  2897. sig_hi = r->sig[SIGSZ-1];
  2898. sig_lo = r->sig[SIGSZ-2];
  2899. }
  2900. else
  2901. {
  2902. sig_lo = r->sig[SIGSZ-1];
  2903. sig_hi = sig_lo >> 31 >> 1;
  2904. sig_lo &= 0xffffffff;
  2905. }
  2906. if (r->signalling == fmt->qnan_msb_set)
  2907. sig_hi &= ~(1 << 30);
  2908. else
  2909. sig_hi |= 1 << 30;
  2910. if ((sig_hi & 0x7fffffff) == 0 && sig_lo == 0)
  2911. sig_hi = 1 << 29;
  2912. /* Intel requires the explicit integer bit to be set, otherwise
  2913. it considers the value a "pseudo-nan". Motorola docs say it
  2914. doesn't care. */
  2915. sig_hi |= 0x80000000;
  2916. }
  2917. else
  2918. {
  2919. image_hi |= 32767;
  2920. sig_lo = sig_hi = 0xffffffff;
  2921. }
  2922. break;
  2923. case rvc_normal:
  2924. {
  2925. int exp = REAL_EXP (r);
  2926. /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
  2927. whereas the intermediate representation is 0.F x 2**exp.
  2928. Which means we're off by one.
  2929. Except for Motorola, which consider exp=0 and explicit
  2930. integer bit set to continue to be normalized. In theory
  2931. this discrepancy has been taken care of by the difference
  2932. in fmt->emin in round_for_format. */
  2933. if (denormal)
  2934. exp = 0;
  2935. else
  2936. {
  2937. exp += 16383 - 1;
  2938. gcc_assert (exp >= 0);
  2939. }
  2940. image_hi |= exp;
  2941. if (HOST_BITS_PER_LONG == 32)
  2942. {
  2943. sig_hi = r->sig[SIGSZ-1];
  2944. sig_lo = r->sig[SIGSZ-2];
  2945. }
  2946. else
  2947. {
  2948. sig_lo = r->sig[SIGSZ-1];
  2949. sig_hi = sig_lo >> 31 >> 1;
  2950. sig_lo &= 0xffffffff;
  2951. }
  2952. }
  2953. break;
  2954. default:
  2955. gcc_unreachable ();
  2956. }
  2957. buf[0] = sig_lo, buf[1] = sig_hi, buf[2] = image_hi;
  2958. }
  2959. /* Convert from the internal format to the 12-byte Motorola format
  2960. for an IEEE extended real. */
  2961. static void
  2962. encode_ieee_extended_motorola (const struct real_format *fmt, long *buf,
  2963. const REAL_VALUE_TYPE *r)
  2964. {
  2965. long intermed[3];
  2966. encode_ieee_extended (fmt, intermed, r);
  2967. if (r->cl == rvc_inf)
  2968. /* For infinity clear the explicit integer bit again, so that the
  2969. format matches the canonical infinity generated by the FPU. */
  2970. intermed[1] = 0;
  2971. /* Motorola chips are assumed always to be big-endian. Also, the
  2972. padding in a Motorola extended real goes between the exponent and
  2973. the mantissa. At this point the mantissa is entirely within
  2974. elements 0 and 1 of intermed, and the exponent entirely within
  2975. element 2, so all we have to do is swap the order around, and
  2976. shift element 2 left 16 bits. */
  2977. buf[0] = intermed[2] << 16;
  2978. buf[1] = intermed[1];
  2979. buf[2] = intermed[0];
  2980. }
  2981. /* Convert from the internal format to the 12-byte Intel format for
  2982. an IEEE extended real. */
  2983. static void
  2984. encode_ieee_extended_intel_96 (const struct real_format *fmt, long *buf,
  2985. const REAL_VALUE_TYPE *r)
  2986. {
  2987. if (FLOAT_WORDS_BIG_ENDIAN)
  2988. {
  2989. /* All the padding in an Intel-format extended real goes at the high
  2990. end, which in this case is after the mantissa, not the exponent.
  2991. Therefore we must shift everything down 16 bits. */
  2992. long intermed[3];
  2993. encode_ieee_extended (fmt, intermed, r);
  2994. buf[0] = ((intermed[2] << 16) | ((unsigned long)(intermed[1] & 0xFFFF0000) >> 16));
  2995. buf[1] = ((intermed[1] << 16) | ((unsigned long)(intermed[0] & 0xFFFF0000) >> 16));
  2996. buf[2] = (intermed[0] << 16);
  2997. }
  2998. else
  2999. /* encode_ieee_extended produces what we want directly. */
  3000. encode_ieee_extended (fmt, buf, r);
  3001. }
  3002. /* Convert from the internal format to the 16-byte Intel format for
  3003. an IEEE extended real. */
  3004. static void
  3005. encode_ieee_extended_intel_128 (const struct real_format *fmt, long *buf,
  3006. const REAL_VALUE_TYPE *r)
  3007. {
  3008. /* All the padding in an Intel-format extended real goes at the high end. */
  3009. encode_ieee_extended_intel_96 (fmt, buf, r);
  3010. buf[3] = 0;
  3011. }
  3012. /* As above, we have a helper function which converts from 12-byte
  3013. little-endian Intel format to internal format. Functions below
  3014. adjust for the other possible formats. */
  3015. static void
  3016. decode_ieee_extended (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  3017. const long *buf)
  3018. {
  3019. unsigned long image_hi, sig_hi, sig_lo;
  3020. bool sign;
  3021. int exp;
  3022. sig_lo = buf[0], sig_hi = buf[1], image_hi = buf[2];
  3023. sig_lo &= 0xffffffff;
  3024. sig_hi &= 0xffffffff;
  3025. image_hi &= 0xffffffff;
  3026. sign = (image_hi >> 15) & 1;
  3027. exp = image_hi & 0x7fff;
  3028. memset (r, 0, sizeof (*r));
  3029. if (exp == 0)
  3030. {
  3031. if ((sig_hi || sig_lo) && fmt->has_denorm)
  3032. {
  3033. r->cl = rvc_normal;
  3034. r->sign = sign;
  3035. /* When the IEEE format contains a hidden bit, we know that
  3036. it's zero at this point, and so shift up the significand
  3037. and decrease the exponent to match. In this case, Motorola
  3038. defines the explicit integer bit to be valid, so we don't
  3039. know whether the msb is set or not. */
  3040. SET_REAL_EXP (r, fmt->emin);
  3041. if (HOST_BITS_PER_LONG == 32)
  3042. {
  3043. r->sig[SIGSZ-1] = sig_hi;
  3044. r->sig[SIGSZ-2] = sig_lo;
  3045. }
  3046. else
  3047. r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
  3048. normalize (r);
  3049. }
  3050. else if (fmt->has_signed_zero)
  3051. r->sign = sign;
  3052. }
  3053. else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
  3054. {
  3055. /* See above re "pseudo-infinities" and "pseudo-nans".
  3056. Short summary is that the MSB will likely always be
  3057. set, and that we don't care about it. */
  3058. sig_hi &= 0x7fffffff;
  3059. if (sig_hi || sig_lo)
  3060. {
  3061. r->cl = rvc_nan;
  3062. r->sign = sign;
  3063. r->signalling = ((sig_hi >> 30) & 1) ^ fmt->qnan_msb_set;
  3064. if (HOST_BITS_PER_LONG == 32)
  3065. {
  3066. r->sig[SIGSZ-1] = sig_hi;
  3067. r->sig[SIGSZ-2] = sig_lo;
  3068. }
  3069. else
  3070. r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
  3071. }
  3072. else
  3073. {
  3074. r->cl = rvc_inf;
  3075. r->sign = sign;
  3076. }
  3077. }
  3078. else
  3079. {
  3080. r->cl = rvc_normal;
  3081. r->sign = sign;
  3082. SET_REAL_EXP (r, exp - 16383 + 1);
  3083. if (HOST_BITS_PER_LONG == 32)
  3084. {
  3085. r->sig[SIGSZ-1] = sig_hi;
  3086. r->sig[SIGSZ-2] = sig_lo;
  3087. }
  3088. else
  3089. r->sig[SIGSZ-1] = (sig_hi << 31 << 1) | sig_lo;
  3090. }
  3091. }
  3092. /* Convert from the internal format to the 12-byte Motorola format
  3093. for an IEEE extended real. */
  3094. static void
  3095. decode_ieee_extended_motorola (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  3096. const long *buf)
  3097. {
  3098. long intermed[3];
  3099. /* Motorola chips are assumed always to be big-endian. Also, the
  3100. padding in a Motorola extended real goes between the exponent and
  3101. the mantissa; remove it. */
  3102. intermed[0] = buf[2];
  3103. intermed[1] = buf[1];
  3104. intermed[2] = (unsigned long)buf[0] >> 16;
  3105. decode_ieee_extended (fmt, r, intermed);
  3106. }
  3107. /* Convert from the internal format to the 12-byte Intel format for
  3108. an IEEE extended real. */
  3109. static void
  3110. decode_ieee_extended_intel_96 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  3111. const long *buf)
  3112. {
  3113. if (FLOAT_WORDS_BIG_ENDIAN)
  3114. {
  3115. /* All the padding in an Intel-format extended real goes at the high
  3116. end, which in this case is after the mantissa, not the exponent.
  3117. Therefore we must shift everything up 16 bits. */
  3118. long intermed[3];
  3119. intermed[0] = (((unsigned long)buf[2] >> 16) | (buf[1] << 16));
  3120. intermed[1] = (((unsigned long)buf[1] >> 16) | (buf[0] << 16));
  3121. intermed[2] = ((unsigned long)buf[0] >> 16);
  3122. decode_ieee_extended (fmt, r, intermed);
  3123. }
  3124. else
  3125. /* decode_ieee_extended produces what we want directly. */
  3126. decode_ieee_extended (fmt, r, buf);
  3127. }
  3128. /* Convert from the internal format to the 16-byte Intel format for
  3129. an IEEE extended real. */
  3130. static void
  3131. decode_ieee_extended_intel_128 (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  3132. const long *buf)
  3133. {
  3134. /* All the padding in an Intel-format extended real goes at the high end. */
  3135. decode_ieee_extended_intel_96 (fmt, r, buf);
  3136. }
  3137. const struct real_format ieee_extended_motorola_format =
  3138. {
  3139. encode_ieee_extended_motorola,
  3140. decode_ieee_extended_motorola,
  3141. 2,
  3142. 64,
  3143. 64,
  3144. -16382,
  3145. 16384,
  3146. 95,
  3147. 95,
  3148. false,
  3149. true,
  3150. true,
  3151. true,
  3152. true,
  3153. true,
  3154. true,
  3155. true,
  3156. "ieee_extended_motorola"
  3157. };
  3158. const struct real_format ieee_extended_intel_96_format =
  3159. {
  3160. encode_ieee_extended_intel_96,
  3161. decode_ieee_extended_intel_96,
  3162. 2,
  3163. 64,
  3164. 64,
  3165. -16381,
  3166. 16384,
  3167. 79,
  3168. 79,
  3169. false,
  3170. true,
  3171. true,
  3172. true,
  3173. true,
  3174. true,
  3175. true,
  3176. false,
  3177. "ieee_extended_intel_96"
  3178. };
  3179. const struct real_format ieee_extended_intel_128_format =
  3180. {
  3181. encode_ieee_extended_intel_128,
  3182. decode_ieee_extended_intel_128,
  3183. 2,
  3184. 64,
  3185. 64,
  3186. -16381,
  3187. 16384,
  3188. 79,
  3189. 79,
  3190. false,
  3191. true,
  3192. true,
  3193. true,
  3194. true,
  3195. true,
  3196. true,
  3197. false,
  3198. "ieee_extended_intel_128"
  3199. };
  3200. /* The following caters to i386 systems that set the rounding precision
  3201. to 53 bits instead of 64, e.g. FreeBSD. */
  3202. const struct real_format ieee_extended_intel_96_round_53_format =
  3203. {
  3204. encode_ieee_extended_intel_96,
  3205. decode_ieee_extended_intel_96,
  3206. 2,
  3207. 53,
  3208. 53,
  3209. -16381,
  3210. 16384,
  3211. 79,
  3212. 79,
  3213. false,
  3214. true,
  3215. true,
  3216. true,
  3217. true,
  3218. true,
  3219. true,
  3220. false,
  3221. "ieee_extended_intel_96_round_53"
  3222. };
  3223. /* IBM 128-bit extended precision format: a pair of IEEE double precision
  3224. numbers whose sum is equal to the extended precision value. The number
  3225. with greater magnitude is first. This format has the same magnitude
  3226. range as an IEEE double precision value, but effectively 106 bits of
  3227. significand precision. Infinity and NaN are represented by their IEEE
  3228. double precision value stored in the first number, the second number is
  3229. +0.0 or -0.0 for Infinity and don't-care for NaN. */
  3230. static void encode_ibm_extended (const struct real_format *fmt,
  3231. long *, const REAL_VALUE_TYPE *);
  3232. static void decode_ibm_extended (const struct real_format *,
  3233. REAL_VALUE_TYPE *, const long *);
  3234. static void
  3235. encode_ibm_extended (const struct real_format *fmt, long *buf,
  3236. const REAL_VALUE_TYPE *r)
  3237. {
  3238. REAL_VALUE_TYPE u, normr, v;
  3239. const struct real_format *base_fmt;
  3240. base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
  3241. /* Renormalize R before doing any arithmetic on it. */
  3242. normr = *r;
  3243. if (normr.cl == rvc_normal)
  3244. normalize (&normr);
  3245. /* u = IEEE double precision portion of significand. */
  3246. u = normr;
  3247. round_for_format (base_fmt, &u);
  3248. encode_ieee_double (base_fmt, &buf[0], &u);
  3249. if (u.cl == rvc_normal)
  3250. {
  3251. do_add (&v, &normr, &u, 1);
  3252. /* Call round_for_format since we might need to denormalize. */
  3253. round_for_format (base_fmt, &v);
  3254. encode_ieee_double (base_fmt, &buf[2], &v);
  3255. }
  3256. else
  3257. {
  3258. /* Inf, NaN, 0 are all representable as doubles, so the
  3259. least-significant part can be 0.0. */
  3260. buf[2] = 0;
  3261. buf[3] = 0;
  3262. }
  3263. }
  3264. static void
  3265. decode_ibm_extended (const struct real_format *fmt ATTRIBUTE_UNUSED, REAL_VALUE_TYPE *r,
  3266. const long *buf)
  3267. {
  3268. REAL_VALUE_TYPE u, v;
  3269. const struct real_format *base_fmt;
  3270. base_fmt = fmt->qnan_msb_set ? &ieee_double_format : &mips_double_format;
  3271. decode_ieee_double (base_fmt, &u, &buf[0]);
  3272. if (u.cl != rvc_zero && u.cl != rvc_inf && u.cl != rvc_nan)
  3273. {
  3274. decode_ieee_double (base_fmt, &v, &buf[2]);
  3275. do_add (r, &u, &v, 0);
  3276. }
  3277. else
  3278. *r = u;
  3279. }
  3280. const struct real_format ibm_extended_format =
  3281. {
  3282. encode_ibm_extended,
  3283. decode_ibm_extended,
  3284. 2,
  3285. 53 + 53,
  3286. 53,
  3287. -1021 + 53,
  3288. 1024,
  3289. 127,
  3290. -1,
  3291. false,
  3292. true,
  3293. true,
  3294. true,
  3295. true,
  3296. true,
  3297. true,
  3298. false,
  3299. "ibm_extended"
  3300. };
  3301. const struct real_format mips_extended_format =
  3302. {
  3303. encode_ibm_extended,
  3304. decode_ibm_extended,
  3305. 2,
  3306. 53 + 53,
  3307. 53,
  3308. -1021 + 53,
  3309. 1024,
  3310. 127,
  3311. -1,
  3312. false,
  3313. true,
  3314. true,
  3315. true,
  3316. true,
  3317. true,
  3318. false,
  3319. true,
  3320. "mips_extended"
  3321. };
  3322. /* IEEE quad precision format. */
  3323. static void encode_ieee_quad (const struct real_format *fmt,
  3324. long *, const REAL_VALUE_TYPE *);
  3325. static void decode_ieee_quad (const struct real_format *,
  3326. REAL_VALUE_TYPE *, const long *);
  3327. static void
  3328. encode_ieee_quad (const struct real_format *fmt, long *buf,
  3329. const REAL_VALUE_TYPE *r)
  3330. {
  3331. unsigned long image3, image2, image1, image0, exp;
  3332. bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
  3333. REAL_VALUE_TYPE u;
  3334. image3 = r->sign << 31;
  3335. image2 = 0;
  3336. image1 = 0;
  3337. image0 = 0;
  3338. rshift_significand (&u, r, SIGNIFICAND_BITS - 113);
  3339. switch (r->cl)
  3340. {
  3341. case rvc_zero:
  3342. break;
  3343. case rvc_inf:
  3344. if (fmt->has_inf)
  3345. image3 |= 32767 << 16;
  3346. else
  3347. {
  3348. image3 |= 0x7fffffff;
  3349. image2 = 0xffffffff;
  3350. image1 = 0xffffffff;
  3351. image0 = 0xffffffff;
  3352. }
  3353. break;
  3354. case rvc_nan:
  3355. if (fmt->has_nans)
  3356. {
  3357. image3 |= 32767 << 16;
  3358. if (r->canonical)
  3359. {
  3360. if (fmt->canonical_nan_lsbs_set)
  3361. {
  3362. image3 |= 0x7fff;
  3363. image2 = image1 = image0 = 0xffffffff;
  3364. }
  3365. }
  3366. else if (HOST_BITS_PER_LONG == 32)
  3367. {
  3368. image0 = u.sig[0];
  3369. image1 = u.sig[1];
  3370. image2 = u.sig[2];
  3371. image3 |= u.sig[3] & 0xffff;
  3372. }
  3373. else
  3374. {
  3375. image0 = u.sig[0];
  3376. image1 = image0 >> 31 >> 1;
  3377. image2 = u.sig[1];
  3378. image3 |= (image2 >> 31 >> 1) & 0xffff;
  3379. image0 &= 0xffffffff;
  3380. image2 &= 0xffffffff;
  3381. }
  3382. if (r->signalling == fmt->qnan_msb_set)
  3383. image3 &= ~0x8000;
  3384. else
  3385. image3 |= 0x8000;
  3386. if (((image3 & 0xffff) | image2 | image1 | image0) == 0)
  3387. image3 |= 0x4000;
  3388. }
  3389. else
  3390. {
  3391. image3 |= 0x7fffffff;
  3392. image2 = 0xffffffff;
  3393. image1 = 0xffffffff;
  3394. image0 = 0xffffffff;
  3395. }
  3396. break;
  3397. case rvc_normal:
  3398. /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
  3399. whereas the intermediate representation is 0.F x 2**exp.
  3400. Which means we're off by one. */
  3401. if (denormal)
  3402. exp = 0;
  3403. else
  3404. exp = REAL_EXP (r) + 16383 - 1;
  3405. image3 |= exp << 16;
  3406. if (HOST_BITS_PER_LONG == 32)
  3407. {
  3408. image0 = u.sig[0];
  3409. image1 = u.sig[1];
  3410. image2 = u.sig[2];
  3411. image3 |= u.sig[3] & 0xffff;
  3412. }
  3413. else
  3414. {
  3415. image0 = u.sig[0];
  3416. image1 = image0 >> 31 >> 1;
  3417. image2 = u.sig[1];
  3418. image3 |= (image2 >> 31 >> 1) & 0xffff;
  3419. image0 &= 0xffffffff;
  3420. image2 &= 0xffffffff;
  3421. }
  3422. break;
  3423. default:
  3424. gcc_unreachable ();
  3425. }
  3426. if (FLOAT_WORDS_BIG_ENDIAN)
  3427. {
  3428. buf[0] = image3;
  3429. buf[1] = image2;
  3430. buf[2] = image1;
  3431. buf[3] = image0;
  3432. }
  3433. else
  3434. {
  3435. buf[0] = image0;
  3436. buf[1] = image1;
  3437. buf[2] = image2;
  3438. buf[3] = image3;
  3439. }
  3440. }
  3441. static void
  3442. decode_ieee_quad (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  3443. const long *buf)
  3444. {
  3445. unsigned long image3, image2, image1, image0;
  3446. bool sign;
  3447. int exp;
  3448. if (FLOAT_WORDS_BIG_ENDIAN)
  3449. {
  3450. image3 = buf[0];
  3451. image2 = buf[1];
  3452. image1 = buf[2];
  3453. image0 = buf[3];
  3454. }
  3455. else
  3456. {
  3457. image0 = buf[0];
  3458. image1 = buf[1];
  3459. image2 = buf[2];
  3460. image3 = buf[3];
  3461. }
  3462. image0 &= 0xffffffff;
  3463. image1 &= 0xffffffff;
  3464. image2 &= 0xffffffff;
  3465. sign = (image3 >> 31) & 1;
  3466. exp = (image3 >> 16) & 0x7fff;
  3467. image3 &= 0xffff;
  3468. memset (r, 0, sizeof (*r));
  3469. if (exp == 0)
  3470. {
  3471. if ((image3 | image2 | image1 | image0) && fmt->has_denorm)
  3472. {
  3473. r->cl = rvc_normal;
  3474. r->sign = sign;
  3475. SET_REAL_EXP (r, -16382 + (SIGNIFICAND_BITS - 112));
  3476. if (HOST_BITS_PER_LONG == 32)
  3477. {
  3478. r->sig[0] = image0;
  3479. r->sig[1] = image1;
  3480. r->sig[2] = image2;
  3481. r->sig[3] = image3;
  3482. }
  3483. else
  3484. {
  3485. r->sig[0] = (image1 << 31 << 1) | image0;
  3486. r->sig[1] = (image3 << 31 << 1) | image2;
  3487. }
  3488. normalize (r);
  3489. }
  3490. else if (fmt->has_signed_zero)
  3491. r->sign = sign;
  3492. }
  3493. else if (exp == 32767 && (fmt->has_nans || fmt->has_inf))
  3494. {
  3495. if (image3 | image2 | image1 | image0)
  3496. {
  3497. r->cl = rvc_nan;
  3498. r->sign = sign;
  3499. r->signalling = ((image3 >> 15) & 1) ^ fmt->qnan_msb_set;
  3500. if (HOST_BITS_PER_LONG == 32)
  3501. {
  3502. r->sig[0] = image0;
  3503. r->sig[1] = image1;
  3504. r->sig[2] = image2;
  3505. r->sig[3] = image3;
  3506. }
  3507. else
  3508. {
  3509. r->sig[0] = (image1 << 31 << 1) | image0;
  3510. r->sig[1] = (image3 << 31 << 1) | image2;
  3511. }
  3512. lshift_significand (r, r, SIGNIFICAND_BITS - 113);
  3513. }
  3514. else
  3515. {
  3516. r->cl = rvc_inf;
  3517. r->sign = sign;
  3518. }
  3519. }
  3520. else
  3521. {
  3522. r->cl = rvc_normal;
  3523. r->sign = sign;
  3524. SET_REAL_EXP (r, exp - 16383 + 1);
  3525. if (HOST_BITS_PER_LONG == 32)
  3526. {
  3527. r->sig[0] = image0;
  3528. r->sig[1] = image1;
  3529. r->sig[2] = image2;
  3530. r->sig[3] = image3;
  3531. }
  3532. else
  3533. {
  3534. r->sig[0] = (image1 << 31 << 1) | image0;
  3535. r->sig[1] = (image3 << 31 << 1) | image2;
  3536. }
  3537. lshift_significand (r, r, SIGNIFICAND_BITS - 113);
  3538. r->sig[SIGSZ-1] |= SIG_MSB;
  3539. }
  3540. }
  3541. const struct real_format ieee_quad_format =
  3542. {
  3543. encode_ieee_quad,
  3544. decode_ieee_quad,
  3545. 2,
  3546. 113,
  3547. 113,
  3548. -16381,
  3549. 16384,
  3550. 127,
  3551. 127,
  3552. false,
  3553. true,
  3554. true,
  3555. true,
  3556. true,
  3557. true,
  3558. true,
  3559. false,
  3560. "ieee_quad"
  3561. };
  3562. const struct real_format mips_quad_format =
  3563. {
  3564. encode_ieee_quad,
  3565. decode_ieee_quad,
  3566. 2,
  3567. 113,
  3568. 113,
  3569. -16381,
  3570. 16384,
  3571. 127,
  3572. 127,
  3573. false,
  3574. true,
  3575. true,
  3576. true,
  3577. true,
  3578. true,
  3579. false,
  3580. true,
  3581. "mips_quad"
  3582. };
  3583. /* Descriptions of VAX floating point formats can be found beginning at
  3584. http://h71000.www7.hp.com/doc/73FINAL/4515/4515pro_013.html#f_floating_point_format
  3585. The thing to remember is that they're almost IEEE, except for word
  3586. order, exponent bias, and the lack of infinities, nans, and denormals.
  3587. We don't implement the H_floating format here, simply because neither
  3588. the VAX or Alpha ports use it. */
  3589. static void encode_vax_f (const struct real_format *fmt,
  3590. long *, const REAL_VALUE_TYPE *);
  3591. static void decode_vax_f (const struct real_format *,
  3592. REAL_VALUE_TYPE *, const long *);
  3593. static void encode_vax_d (const struct real_format *fmt,
  3594. long *, const REAL_VALUE_TYPE *);
  3595. static void decode_vax_d (const struct real_format *,
  3596. REAL_VALUE_TYPE *, const long *);
  3597. static void encode_vax_g (const struct real_format *fmt,
  3598. long *, const REAL_VALUE_TYPE *);
  3599. static void decode_vax_g (const struct real_format *,
  3600. REAL_VALUE_TYPE *, const long *);
  3601. static void
  3602. encode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
  3603. const REAL_VALUE_TYPE *r)
  3604. {
  3605. unsigned long sign, exp, sig, image;
  3606. sign = r->sign << 15;
  3607. switch (r->cl)
  3608. {
  3609. case rvc_zero:
  3610. image = 0;
  3611. break;
  3612. case rvc_inf:
  3613. case rvc_nan:
  3614. image = 0xffff7fff | sign;
  3615. break;
  3616. case rvc_normal:
  3617. sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 24)) & 0x7fffff;
  3618. exp = REAL_EXP (r) + 128;
  3619. image = (sig << 16) & 0xffff0000;
  3620. image |= sign;
  3621. image |= exp << 7;
  3622. image |= sig >> 16;
  3623. break;
  3624. default:
  3625. gcc_unreachable ();
  3626. }
  3627. buf[0] = image;
  3628. }
  3629. static void
  3630. decode_vax_f (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3631. REAL_VALUE_TYPE *r, const long *buf)
  3632. {
  3633. unsigned long image = buf[0] & 0xffffffff;
  3634. int exp = (image >> 7) & 0xff;
  3635. memset (r, 0, sizeof (*r));
  3636. if (exp != 0)
  3637. {
  3638. r->cl = rvc_normal;
  3639. r->sign = (image >> 15) & 1;
  3640. SET_REAL_EXP (r, exp - 128);
  3641. image = ((image & 0x7f) << 16) | ((image >> 16) & 0xffff);
  3642. r->sig[SIGSZ-1] = (image << (HOST_BITS_PER_LONG - 24)) | SIG_MSB;
  3643. }
  3644. }
  3645. static void
  3646. encode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
  3647. const REAL_VALUE_TYPE *r)
  3648. {
  3649. unsigned long image0, image1, sign = r->sign << 15;
  3650. switch (r->cl)
  3651. {
  3652. case rvc_zero:
  3653. image0 = image1 = 0;
  3654. break;
  3655. case rvc_inf:
  3656. case rvc_nan:
  3657. image0 = 0xffff7fff | sign;
  3658. image1 = 0xffffffff;
  3659. break;
  3660. case rvc_normal:
  3661. /* Extract the significand into straight hi:lo. */
  3662. if (HOST_BITS_PER_LONG == 64)
  3663. {
  3664. image0 = r->sig[SIGSZ-1];
  3665. image1 = (image0 >> (64 - 56)) & 0xffffffff;
  3666. image0 = (image0 >> (64 - 56 + 1) >> 31) & 0x7fffff;
  3667. }
  3668. else
  3669. {
  3670. image0 = r->sig[SIGSZ-1];
  3671. image1 = r->sig[SIGSZ-2];
  3672. image1 = (image0 << 24) | (image1 >> 8);
  3673. image0 = (image0 >> 8) & 0xffffff;
  3674. }
  3675. /* Rearrange the half-words of the significand to match the
  3676. external format. */
  3677. image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff007f;
  3678. image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
  3679. /* Add the sign and exponent. */
  3680. image0 |= sign;
  3681. image0 |= (REAL_EXP (r) + 128) << 7;
  3682. break;
  3683. default:
  3684. gcc_unreachable ();
  3685. }
  3686. if (FLOAT_WORDS_BIG_ENDIAN)
  3687. buf[0] = image1, buf[1] = image0;
  3688. else
  3689. buf[0] = image0, buf[1] = image1;
  3690. }
  3691. static void
  3692. decode_vax_d (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3693. REAL_VALUE_TYPE *r, const long *buf)
  3694. {
  3695. unsigned long image0, image1;
  3696. int exp;
  3697. if (FLOAT_WORDS_BIG_ENDIAN)
  3698. image1 = buf[0], image0 = buf[1];
  3699. else
  3700. image0 = buf[0], image1 = buf[1];
  3701. image0 &= 0xffffffff;
  3702. image1 &= 0xffffffff;
  3703. exp = (image0 >> 7) & 0xff;
  3704. memset (r, 0, sizeof (*r));
  3705. if (exp != 0)
  3706. {
  3707. r->cl = rvc_normal;
  3708. r->sign = (image0 >> 15) & 1;
  3709. SET_REAL_EXP (r, exp - 128);
  3710. /* Rearrange the half-words of the external format into
  3711. proper ascending order. */
  3712. image0 = ((image0 & 0x7f) << 16) | ((image0 >> 16) & 0xffff);
  3713. image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
  3714. if (HOST_BITS_PER_LONG == 64)
  3715. {
  3716. image0 = (image0 << 31 << 1) | image1;
  3717. image0 <<= 64 - 56;
  3718. image0 |= SIG_MSB;
  3719. r->sig[SIGSZ-1] = image0;
  3720. }
  3721. else
  3722. {
  3723. r->sig[SIGSZ-1] = image0;
  3724. r->sig[SIGSZ-2] = image1;
  3725. lshift_significand (r, r, 2*HOST_BITS_PER_LONG - 56);
  3726. r->sig[SIGSZ-1] |= SIG_MSB;
  3727. }
  3728. }
  3729. }
  3730. static void
  3731. encode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
  3732. const REAL_VALUE_TYPE *r)
  3733. {
  3734. unsigned long image0, image1, sign = r->sign << 15;
  3735. switch (r->cl)
  3736. {
  3737. case rvc_zero:
  3738. image0 = image1 = 0;
  3739. break;
  3740. case rvc_inf:
  3741. case rvc_nan:
  3742. image0 = 0xffff7fff | sign;
  3743. image1 = 0xffffffff;
  3744. break;
  3745. case rvc_normal:
  3746. /* Extract the significand into straight hi:lo. */
  3747. if (HOST_BITS_PER_LONG == 64)
  3748. {
  3749. image0 = r->sig[SIGSZ-1];
  3750. image1 = (image0 >> (64 - 53)) & 0xffffffff;
  3751. image0 = (image0 >> (64 - 53 + 1) >> 31) & 0xfffff;
  3752. }
  3753. else
  3754. {
  3755. image0 = r->sig[SIGSZ-1];
  3756. image1 = r->sig[SIGSZ-2];
  3757. image1 = (image0 << 21) | (image1 >> 11);
  3758. image0 = (image0 >> 11) & 0xfffff;
  3759. }
  3760. /* Rearrange the half-words of the significand to match the
  3761. external format. */
  3762. image0 = ((image0 << 16) | (image0 >> 16)) & 0xffff000f;
  3763. image1 = ((image1 << 16) | (image1 >> 16)) & 0xffffffff;
  3764. /* Add the sign and exponent. */
  3765. image0 |= sign;
  3766. image0 |= (REAL_EXP (r) + 1024) << 4;
  3767. break;
  3768. default:
  3769. gcc_unreachable ();
  3770. }
  3771. if (FLOAT_WORDS_BIG_ENDIAN)
  3772. buf[0] = image1, buf[1] = image0;
  3773. else
  3774. buf[0] = image0, buf[1] = image1;
  3775. }
  3776. static void
  3777. decode_vax_g (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3778. REAL_VALUE_TYPE *r, const long *buf)
  3779. {
  3780. unsigned long image0, image1;
  3781. int exp;
  3782. if (FLOAT_WORDS_BIG_ENDIAN)
  3783. image1 = buf[0], image0 = buf[1];
  3784. else
  3785. image0 = buf[0], image1 = buf[1];
  3786. image0 &= 0xffffffff;
  3787. image1 &= 0xffffffff;
  3788. exp = (image0 >> 4) & 0x7ff;
  3789. memset (r, 0, sizeof (*r));
  3790. if (exp != 0)
  3791. {
  3792. r->cl = rvc_normal;
  3793. r->sign = (image0 >> 15) & 1;
  3794. SET_REAL_EXP (r, exp - 1024);
  3795. /* Rearrange the half-words of the external format into
  3796. proper ascending order. */
  3797. image0 = ((image0 & 0xf) << 16) | ((image0 >> 16) & 0xffff);
  3798. image1 = ((image1 & 0xffff) << 16) | ((image1 >> 16) & 0xffff);
  3799. if (HOST_BITS_PER_LONG == 64)
  3800. {
  3801. image0 = (image0 << 31 << 1) | image1;
  3802. image0 <<= 64 - 53;
  3803. image0 |= SIG_MSB;
  3804. r->sig[SIGSZ-1] = image0;
  3805. }
  3806. else
  3807. {
  3808. r->sig[SIGSZ-1] = image0;
  3809. r->sig[SIGSZ-2] = image1;
  3810. lshift_significand (r, r, 64 - 53);
  3811. r->sig[SIGSZ-1] |= SIG_MSB;
  3812. }
  3813. }
  3814. }
  3815. const struct real_format vax_f_format =
  3816. {
  3817. encode_vax_f,
  3818. decode_vax_f,
  3819. 2,
  3820. 24,
  3821. 24,
  3822. -127,
  3823. 127,
  3824. 15,
  3825. 15,
  3826. false,
  3827. false,
  3828. false,
  3829. false,
  3830. false,
  3831. false,
  3832. false,
  3833. false,
  3834. "vax_f"
  3835. };
  3836. const struct real_format vax_d_format =
  3837. {
  3838. encode_vax_d,
  3839. decode_vax_d,
  3840. 2,
  3841. 56,
  3842. 56,
  3843. -127,
  3844. 127,
  3845. 15,
  3846. 15,
  3847. false,
  3848. false,
  3849. false,
  3850. false,
  3851. false,
  3852. false,
  3853. false,
  3854. false,
  3855. "vax_d"
  3856. };
  3857. const struct real_format vax_g_format =
  3858. {
  3859. encode_vax_g,
  3860. decode_vax_g,
  3861. 2,
  3862. 53,
  3863. 53,
  3864. -1023,
  3865. 1023,
  3866. 15,
  3867. 15,
  3868. false,
  3869. false,
  3870. false,
  3871. false,
  3872. false,
  3873. false,
  3874. false,
  3875. false,
  3876. "vax_g"
  3877. };
  3878. /* Encode real R into a single precision DFP value in BUF. */
  3879. static void
  3880. encode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3881. long *buf ATTRIBUTE_UNUSED,
  3882. const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
  3883. {
  3884. encode_decimal32 (fmt, buf, r);
  3885. }
  3886. /* Decode a single precision DFP value in BUF into a real R. */
  3887. static void
  3888. decode_decimal_single (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3889. REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
  3890. const long *buf ATTRIBUTE_UNUSED)
  3891. {
  3892. decode_decimal32 (fmt, r, buf);
  3893. }
  3894. /* Encode real R into a double precision DFP value in BUF. */
  3895. static void
  3896. encode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3897. long *buf ATTRIBUTE_UNUSED,
  3898. const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
  3899. {
  3900. encode_decimal64 (fmt, buf, r);
  3901. }
  3902. /* Decode a double precision DFP value in BUF into a real R. */
  3903. static void
  3904. decode_decimal_double (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3905. REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
  3906. const long *buf ATTRIBUTE_UNUSED)
  3907. {
  3908. decode_decimal64 (fmt, r, buf);
  3909. }
  3910. /* Encode real R into a quad precision DFP value in BUF. */
  3911. static void
  3912. encode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3913. long *buf ATTRIBUTE_UNUSED,
  3914. const REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED)
  3915. {
  3916. encode_decimal128 (fmt, buf, r);
  3917. }
  3918. /* Decode a quad precision DFP value in BUF into a real R. */
  3919. static void
  3920. decode_decimal_quad (const struct real_format *fmt ATTRIBUTE_UNUSED,
  3921. REAL_VALUE_TYPE *r ATTRIBUTE_UNUSED,
  3922. const long *buf ATTRIBUTE_UNUSED)
  3923. {
  3924. decode_decimal128 (fmt, r, buf);
  3925. }
  3926. /* Single precision decimal floating point (IEEE 754). */
  3927. const struct real_format decimal_single_format =
  3928. {
  3929. encode_decimal_single,
  3930. decode_decimal_single,
  3931. 10,
  3932. 7,
  3933. 7,
  3934. -94,
  3935. 97,
  3936. 31,
  3937. 31,
  3938. false,
  3939. true,
  3940. true,
  3941. true,
  3942. true,
  3943. true,
  3944. true,
  3945. false,
  3946. "decimal_single"
  3947. };
  3948. /* Double precision decimal floating point (IEEE 754). */
  3949. const struct real_format decimal_double_format =
  3950. {
  3951. encode_decimal_double,
  3952. decode_decimal_double,
  3953. 10,
  3954. 16,
  3955. 16,
  3956. -382,
  3957. 385,
  3958. 63,
  3959. 63,
  3960. false,
  3961. true,
  3962. true,
  3963. true,
  3964. true,
  3965. true,
  3966. true,
  3967. false,
  3968. "decimal_double"
  3969. };
  3970. /* Quad precision decimal floating point (IEEE 754). */
  3971. const struct real_format decimal_quad_format =
  3972. {
  3973. encode_decimal_quad,
  3974. decode_decimal_quad,
  3975. 10,
  3976. 34,
  3977. 34,
  3978. -6142,
  3979. 6145,
  3980. 127,
  3981. 127,
  3982. false,
  3983. true,
  3984. true,
  3985. true,
  3986. true,
  3987. true,
  3988. true,
  3989. false,
  3990. "decimal_quad"
  3991. };
  3992. /* Encode half-precision floats. This routine is used both for the IEEE
  3993. ARM alternative encodings. */
  3994. static void
  3995. encode_ieee_half (const struct real_format *fmt, long *buf,
  3996. const REAL_VALUE_TYPE *r)
  3997. {
  3998. unsigned long image, sig, exp;
  3999. unsigned long sign = r->sign;
  4000. bool denormal = (r->sig[SIGSZ-1] & SIG_MSB) == 0;
  4001. image = sign << 15;
  4002. sig = (r->sig[SIGSZ-1] >> (HOST_BITS_PER_LONG - 11)) & 0x3ff;
  4003. switch (r->cl)
  4004. {
  4005. case rvc_zero:
  4006. break;
  4007. case rvc_inf:
  4008. if (fmt->has_inf)
  4009. image |= 31 << 10;
  4010. else
  4011. image |= 0x7fff;
  4012. break;
  4013. case rvc_nan:
  4014. if (fmt->has_nans)
  4015. {
  4016. if (r->canonical)
  4017. sig = (fmt->canonical_nan_lsbs_set ? (1 << 9) - 1 : 0);
  4018. if (r->signalling == fmt->qnan_msb_set)
  4019. sig &= ~(1 << 9);
  4020. else
  4021. sig |= 1 << 9;
  4022. if (sig == 0)
  4023. sig = 1 << 8;
  4024. image |= 31 << 10;
  4025. image |= sig;
  4026. }
  4027. else
  4028. image |= 0x3ff;
  4029. break;
  4030. case rvc_normal:
  4031. /* Recall that IEEE numbers are interpreted as 1.F x 2**exp,
  4032. whereas the intermediate representation is 0.F x 2**exp.
  4033. Which means we're off by one. */
  4034. if (denormal)
  4035. exp = 0;
  4036. else
  4037. exp = REAL_EXP (r) + 15 - 1;
  4038. image |= exp << 10;
  4039. image |= sig;
  4040. break;
  4041. default:
  4042. gcc_unreachable ();
  4043. }
  4044. buf[0] = image;
  4045. }
  4046. /* Decode half-precision floats. This routine is used both for the IEEE
  4047. ARM alternative encodings. */
  4048. static void
  4049. decode_ieee_half (const struct real_format *fmt, REAL_VALUE_TYPE *r,
  4050. const long *buf)
  4051. {
  4052. unsigned long image = buf[0] & 0xffff;
  4053. bool sign = (image >> 15) & 1;
  4054. int exp = (image >> 10) & 0x1f;
  4055. memset (r, 0, sizeof (*r));
  4056. image <<= HOST_BITS_PER_LONG - 11;
  4057. image &= ~SIG_MSB;
  4058. if (exp == 0)
  4059. {
  4060. if (image && fmt->has_denorm)
  4061. {
  4062. r->cl = rvc_normal;
  4063. r->sign = sign;
  4064. SET_REAL_EXP (r, -14);
  4065. r->sig[SIGSZ-1] = image << 1;
  4066. normalize (r);
  4067. }
  4068. else if (fmt->has_signed_zero)
  4069. r->sign = sign;
  4070. }
  4071. else if (exp == 31 && (fmt->has_nans || fmt->has_inf))
  4072. {
  4073. if (image)
  4074. {
  4075. r->cl = rvc_nan;
  4076. r->sign = sign;
  4077. r->signalling = (((image >> (HOST_BITS_PER_LONG - 2)) & 1)
  4078. ^ fmt->qnan_msb_set);
  4079. r->sig[SIGSZ-1] = image;
  4080. }
  4081. else
  4082. {
  4083. r->cl = rvc_inf;
  4084. r->sign = sign;
  4085. }
  4086. }
  4087. else
  4088. {
  4089. r->cl = rvc_normal;
  4090. r->sign = sign;
  4091. SET_REAL_EXP (r, exp - 15 + 1);
  4092. r->sig[SIGSZ-1] = image | SIG_MSB;
  4093. }
  4094. }
  4095. /* Half-precision format, as specified in IEEE 754R. */
  4096. const struct real_format ieee_half_format =
  4097. {
  4098. encode_ieee_half,
  4099. decode_ieee_half,
  4100. 2,
  4101. 11,
  4102. 11,
  4103. -13,
  4104. 16,
  4105. 15,
  4106. 15,
  4107. false,
  4108. true,
  4109. true,
  4110. true,
  4111. true,
  4112. true,
  4113. true,
  4114. false,
  4115. "ieee_half"
  4116. };
  4117. /* ARM's alternative half-precision format, similar to IEEE but with
  4118. no reserved exponent value for NaNs and infinities; rather, it just
  4119. extends the range of exponents by one. */
  4120. const struct real_format arm_half_format =
  4121. {
  4122. encode_ieee_half,
  4123. decode_ieee_half,
  4124. 2,
  4125. 11,
  4126. 11,
  4127. -13,
  4128. 17,
  4129. 15,
  4130. 15,
  4131. false,
  4132. true,
  4133. false,
  4134. false,
  4135. true,
  4136. true,
  4137. false,
  4138. false,
  4139. "arm_half"
  4140. };
  4141. /* A synthetic "format" for internal arithmetic. It's the size of the
  4142. internal significand minus the two bits needed for proper rounding.
  4143. The encode and decode routines exist only to satisfy our paranoia
  4144. harness. */
  4145. static void encode_internal (const struct real_format *fmt,
  4146. long *, const REAL_VALUE_TYPE *);
  4147. static void decode_internal (const struct real_format *,
  4148. REAL_VALUE_TYPE *, const long *);
  4149. static void
  4150. encode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED, long *buf,
  4151. const REAL_VALUE_TYPE *r)
  4152. {
  4153. memcpy (buf, r, sizeof (*r));
  4154. }
  4155. static void
  4156. decode_internal (const struct real_format *fmt ATTRIBUTE_UNUSED,
  4157. REAL_VALUE_TYPE *r, const long *buf)
  4158. {
  4159. memcpy (r, buf, sizeof (*r));
  4160. }
  4161. const struct real_format real_internal_format =
  4162. {
  4163. encode_internal,
  4164. decode_internal,
  4165. 2,
  4166. SIGNIFICAND_BITS - 2,
  4167. SIGNIFICAND_BITS - 2,
  4168. -MAX_EXP,
  4169. MAX_EXP,
  4170. -1,
  4171. -1,
  4172. false,
  4173. false,
  4174. true,
  4175. true,
  4176. false,
  4177. true,
  4178. true,
  4179. false,
  4180. "real_internal"
  4181. };
  4182. /* Calculate X raised to the integer exponent N in mode MODE and store
  4183. the result in R. Return true if the result may be inexact due to
  4184. loss of precision. The algorithm is the classic "left-to-right binary
  4185. method" described in section 4.6.3 of Donald Knuth's "Seminumerical
  4186. Algorithms", "The Art of Computer Programming", Volume 2. */
  4187. bool
  4188. real_powi (REAL_VALUE_TYPE *r, machine_mode mode,
  4189. const REAL_VALUE_TYPE *x, HOST_WIDE_INT n)
  4190. {
  4191. unsigned HOST_WIDE_INT bit;
  4192. REAL_VALUE_TYPE t;
  4193. bool inexact = false;
  4194. bool init = false;
  4195. bool neg;
  4196. int i;
  4197. if (n == 0)
  4198. {
  4199. *r = dconst1;
  4200. return false;
  4201. }
  4202. else if (n < 0)
  4203. {
  4204. /* Don't worry about overflow, from now on n is unsigned. */
  4205. neg = true;
  4206. n = -n;
  4207. }
  4208. else
  4209. neg = false;
  4210. t = *x;
  4211. bit = (unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1);
  4212. for (i = 0; i < HOST_BITS_PER_WIDE_INT; i++)
  4213. {
  4214. if (init)
  4215. {
  4216. inexact |= do_multiply (&t, &t, &t);
  4217. if (n & bit)
  4218. inexact |= do_multiply (&t, &t, x);
  4219. }
  4220. else if (n & bit)
  4221. init = true;
  4222. bit >>= 1;
  4223. }
  4224. if (neg)
  4225. inexact |= do_divide (&t, &dconst1, &t);
  4226. real_convert (r, mode, &t);
  4227. return inexact;
  4228. }
  4229. /* Round X to the nearest integer not larger in absolute value, i.e.
  4230. towards zero, placing the result in R in mode MODE. */
  4231. void
  4232. real_trunc (REAL_VALUE_TYPE *r, machine_mode mode,
  4233. const REAL_VALUE_TYPE *x)
  4234. {
  4235. do_fix_trunc (r, x);
  4236. if (mode != VOIDmode)
  4237. real_convert (r, mode, r);
  4238. }
  4239. /* Round X to the largest integer not greater in value, i.e. round
  4240. down, placing the result in R in mode MODE. */
  4241. void
  4242. real_floor (REAL_VALUE_TYPE *r, machine_mode mode,
  4243. const REAL_VALUE_TYPE *x)
  4244. {
  4245. REAL_VALUE_TYPE t;
  4246. do_fix_trunc (&t, x);
  4247. if (! real_identical (&t, x) && x->sign)
  4248. do_add (&t, &t, &dconstm1, 0);
  4249. if (mode != VOIDmode)
  4250. real_convert (r, mode, &t);
  4251. else
  4252. *r = t;
  4253. }
  4254. /* Round X to the smallest integer not less then argument, i.e. round
  4255. up, placing the result in R in mode MODE. */
  4256. void
  4257. real_ceil (REAL_VALUE_TYPE *r, machine_mode mode,
  4258. const REAL_VALUE_TYPE *x)
  4259. {
  4260. REAL_VALUE_TYPE t;
  4261. do_fix_trunc (&t, x);
  4262. if (! real_identical (&t, x) && ! x->sign)
  4263. do_add (&t, &t, &dconst1, 0);
  4264. if (mode != VOIDmode)
  4265. real_convert (r, mode, &t);
  4266. else
  4267. *r = t;
  4268. }
  4269. /* Round X to the nearest integer, but round halfway cases away from
  4270. zero. */
  4271. void
  4272. real_round (REAL_VALUE_TYPE *r, machine_mode mode,
  4273. const REAL_VALUE_TYPE *x)
  4274. {
  4275. do_add (r, x, &dconsthalf, x->sign);
  4276. do_fix_trunc (r, r);
  4277. if (mode != VOIDmode)
  4278. real_convert (r, mode, r);
  4279. }
  4280. /* Set the sign of R to the sign of X. */
  4281. void
  4282. real_copysign (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *x)
  4283. {
  4284. r->sign = x->sign;
  4285. }
  4286. /* Check whether the real constant value given is an integer. */
  4287. bool
  4288. real_isinteger (const REAL_VALUE_TYPE *c, machine_mode mode)
  4289. {
  4290. REAL_VALUE_TYPE cint;
  4291. real_trunc (&cint, mode, c);
  4292. return real_identical (c, &cint);
  4293. }
  4294. /* Write into BUF the maximum representable finite floating-point
  4295. number, (1 - b**-p) * b**emax for a given FP format FMT as a hex
  4296. float string. LEN is the size of BUF, and the buffer must be large
  4297. enough to contain the resulting string. */
  4298. void
  4299. get_max_float (const struct real_format *fmt, char *buf, size_t len)
  4300. {
  4301. int i, n;
  4302. char *p;
  4303. strcpy (buf, "0x0.");
  4304. n = fmt->p;
  4305. for (i = 0, p = buf + 4; i + 3 < n; i += 4)
  4306. *p++ = 'f';
  4307. if (i < n)
  4308. *p++ = "08ce"[n - i];
  4309. sprintf (p, "p%d", fmt->emax);
  4310. if (fmt->pnan < fmt->p)
  4311. {
  4312. /* This is an IBM extended double format made up of two IEEE
  4313. doubles. The value of the long double is the sum of the
  4314. values of the two parts. The most significant part is
  4315. required to be the value of the long double rounded to the
  4316. nearest double. Rounding means we need a slightly smaller
  4317. value for LDBL_MAX. */
  4318. buf[4 + fmt->pnan / 4] = "7bde"[fmt->pnan % 4];
  4319. }
  4320. gcc_assert (strlen (buf) < len);
  4321. }
  4322. /* True if mode M has a NaN representation and
  4323. the treatment of NaN operands is important. */
  4324. bool
  4325. HONOR_NANS (machine_mode m)
  4326. {
  4327. return MODE_HAS_NANS (m) && !flag_finite_math_only;
  4328. }
  4329. bool
  4330. HONOR_NANS (const_tree t)
  4331. {
  4332. return HONOR_NANS (element_mode (t));
  4333. }
  4334. bool
  4335. HONOR_NANS (const_rtx x)
  4336. {
  4337. return HONOR_NANS (GET_MODE (x));
  4338. }
  4339. /* Like HONOR_NANs, but true if we honor signaling NaNs (or sNaNs). */
  4340. bool
  4341. HONOR_SNANS (machine_mode m)
  4342. {
  4343. return flag_signaling_nans && HONOR_NANS (m);
  4344. }
  4345. bool
  4346. HONOR_SNANS (const_tree t)
  4347. {
  4348. return HONOR_SNANS (element_mode (t));
  4349. }
  4350. bool
  4351. HONOR_SNANS (const_rtx x)
  4352. {
  4353. return HONOR_SNANS (GET_MODE (x));
  4354. }
  4355. /* As for HONOR_NANS, but true if the mode can represent infinity and
  4356. the treatment of infinite values is important. */
  4357. bool
  4358. HONOR_INFINITIES (machine_mode m)
  4359. {
  4360. return MODE_HAS_INFINITIES (m) && !flag_finite_math_only;
  4361. }
  4362. bool
  4363. HONOR_INFINITIES (const_tree t)
  4364. {
  4365. return HONOR_INFINITIES (element_mode (t));
  4366. }
  4367. bool
  4368. HONOR_INFINITIES (const_rtx x)
  4369. {
  4370. return HONOR_INFINITIES (GET_MODE (x));
  4371. }
  4372. /* Like HONOR_NANS, but true if the given mode distinguishes between
  4373. positive and negative zero, and the sign of zero is important. */
  4374. bool
  4375. HONOR_SIGNED_ZEROS (machine_mode m)
  4376. {
  4377. return MODE_HAS_SIGNED_ZEROS (m) && flag_signed_zeros;
  4378. }
  4379. bool
  4380. HONOR_SIGNED_ZEROS (const_tree t)
  4381. {
  4382. return HONOR_SIGNED_ZEROS (element_mode (t));
  4383. }
  4384. bool
  4385. HONOR_SIGNED_ZEROS (const_rtx x)
  4386. {
  4387. return HONOR_SIGNED_ZEROS (GET_MODE (x));
  4388. }
  4389. /* Like HONOR_NANS, but true if given mode supports sign-dependent rounding,
  4390. and the rounding mode is important. */
  4391. bool
  4392. HONOR_SIGN_DEPENDENT_ROUNDING (machine_mode m)
  4393. {
  4394. return MODE_HAS_SIGN_DEPENDENT_ROUNDING (m) && flag_rounding_math;
  4395. }
  4396. bool
  4397. HONOR_SIGN_DEPENDENT_ROUNDING (const_tree t)
  4398. {
  4399. return HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (t));
  4400. }
  4401. bool
  4402. HONOR_SIGN_DEPENDENT_ROUNDING (const_rtx x)
  4403. {
  4404. return HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (x));
  4405. }