dtoa.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359
  1. /****************************************************************
  2. *
  3. * The author of this software is David M. Gay.
  4. *
  5. * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose without fee is hereby granted, provided that this entire notice
  9. * is included in all copies of any software which is or includes a copy
  10. * or modification of this software and in all copies of the supporting
  11. * documentation for such software.
  12. *
  13. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  14. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
  15. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  16. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  17. *
  18. ***************************************************************/
  19. /* Please send bug reports to David M. Gay (dmg at acm dot org,
  20. * with " at " changed at "@" and " dot " changed to "."). */
  21. /* On a machine with IEEE extended-precision registers, it is
  22. * necessary to specify double-precision (53-bit) rounding precision
  23. * before invoking strtod or dtoa. If the machine uses (the equivalent
  24. * of) Intel 80x87 arithmetic, the call
  25. * _control87(PC_53, MCW_PC);
  26. * does this with many compilers. Whether this or another call is
  27. * appropriate depends on the compiler; for this to work, it may be
  28. * necessary to #include "float.h" or another system-dependent header
  29. * file.
  30. */
  31. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  32. *
  33. * This strtod returns a nearest machine number to the input decimal
  34. * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
  35. * broken by the IEEE round-even rule. Otherwise ties are broken by
  36. * biased rounding (add half and chop).
  37. *
  38. * Inspired loosely by William D. Clinger's paper "How to Read Floating
  39. * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  40. *
  41. * Modifications:
  42. *
  43. * 1. We only require IEEE, IBM, or VAX double-precision
  44. * arithmetic (not IEEE double-extended).
  45. * 2. We get by with floating-point arithmetic in a case that
  46. * Clinger missed -- when we're computing d * 10^n
  47. * for a small integer d and the integer n is not too
  48. * much larger than 22 (the maximum integer k for which
  49. * we can represent 10^k exactly), we may be able to
  50. * compute (d*10^k) * 10^(e-k) with just one roundoff.
  51. * 3. Rather than a bit-at-a-time adjustment of the binary
  52. * result in the hard case, we use floating-point
  53. * arithmetic to determine the adjustment to within
  54. * one bit; only in really hard cases do we need to
  55. * compute a second residual.
  56. * 4. Because of 3., we don't need a large table of powers of 10
  57. * for ten-to-e (just some small tables, e.g. of 10^k
  58. * for 0 <= k <= 22).
  59. */
  60. /*
  61. * #define IEEE_8087 for IEEE-arithmetic machines where the least
  62. * significant byte has the lowest address.
  63. * #define IEEE_MC68k for IEEE-arithmetic machines where the most
  64. * significant byte has the lowest address.
  65. * #define Long int on machines with 32-bit ints and 64-bit longs.
  66. * #define IBM for IBM mainframe-style floating-point arithmetic.
  67. * #define VAX for VAX-style floating-point arithmetic (D_floating).
  68. * #define No_leftright to omit left-right logic in fast floating-point
  69. * computation of dtoa. This will cause dtoa modes 4 and 5 to be
  70. * treated the same as modes 2 and 3 for some inputs.
  71. * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
  72. * and strtod and dtoa should round accordingly. Unless Trust_FLT_ROUNDS
  73. * is also #defined, fegetround() will be queried for the rounding mode.
  74. * Note that both FLT_ROUNDS and fegetround() are specified by the C99
  75. * standard (and are specified to be consistent, with fesetround()
  76. * affecting the value of FLT_ROUNDS), but that some (Linux) systems
  77. * do not work correctly in this regard, so using fegetround() is more
  78. * portable than using FLT_ROUNDS directly.
  79. * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
  80. * and Honor_FLT_ROUNDS is not #defined.
  81. * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  82. * that use extended-precision instructions to compute rounded
  83. * products and quotients) with IBM.
  84. * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
  85. * that rounds toward +Infinity.
  86. * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
  87. * rounding when the underlying floating-point arithmetic uses
  88. * unbiased rounding. This prevent using ordinary floating-point
  89. * arithmetic when the result could be computed with one rounding error.
  90. * #define Inaccurate_Divide for IEEE-format with correctly rounded
  91. * products but inaccurate quotients, e.g., for Intel i860.
  92. * #define NO_LONG_LONG on machines that do not have a "long long"
  93. * integer type (of >= 64 bits). On such machines, you can
  94. * #define Just_16 to store 16 bits per 32-bit Long when doing
  95. * high-precision integer arithmetic. Whether this speeds things
  96. * up or slows things down depends on the machine and the number
  97. * being converted. If long long is available and the name is
  98. * something other than "long long", #define Llong to be the name,
  99. * and if "unsigned Llong" does not work as an unsigned version of
  100. * Llong, #define #ULLong to be the corresponding unsigned type.
  101. * #define KR_headers for old-style C function headers.
  102. * #define Bad_float_h if your system lacks a float.h or if it does not
  103. * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
  104. * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
  105. * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
  106. * if memory is available and otherwise does something you deem
  107. * appropriate. If MALLOC is undefined, malloc will be invoked
  108. * directly -- and assumed always to succeed. Similarly, if you
  109. * want something other than the system's free() to be called to
  110. * recycle memory acquired from MALLOC, #define FREE to be the
  111. * name of the alternate routine. (FREE or free is only called in
  112. * pathological cases, e.g., in a dtoa call after a dtoa return in
  113. * mode 3 with thousands of digits requested.)
  114. * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
  115. * memory allocations from a private pool of memory when possible.
  116. * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
  117. * unless #defined to be a different length. This default length
  118. * suffices to get rid of MALLOC calls except for unusual cases,
  119. * such as decimal-to-binary conversion of a very long string of
  120. * digits. The longest string dtoa can return is about 751 bytes
  121. * long. For conversions by strtod of strings of 800 digits and
  122. * all dtoa conversions in single-threaded executions with 8-byte
  123. * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
  124. * pointers, PRIVATE_MEM >= 7112 appears adequate.
  125. * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
  126. * #defined automatically on IEEE systems. On such systems,
  127. * when INFNAN_CHECK is #defined, strtod checks
  128. * for Infinity and NaN (case insensitively). On some systems
  129. * (e.g., some HP systems), it may be necessary to #define NAN_WORD0
  130. * appropriately -- to the most significant word of a quiet NaN.
  131. * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
  132. * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
  133. * strtod also accepts (case insensitively) strings of the form
  134. * NaN(x), where x is a string of hexadecimal digits and spaces;
  135. * if there is only one string of hexadecimal digits, it is taken
  136. * for the 52 fraction bits of the resulting NaN; if there are two
  137. * or more strings of hex digits, the first is for the high 20 bits,
  138. * the second and subsequent for the low 32 bits, with intervening
  139. * white space ignored; but if this results in none of the 52
  140. * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
  141. * and NAN_WORD1 are used instead.
  142. * #define MULTIPLE_THREADS if the system offers preemptively scheduled
  143. * multiple threads. In this case, you must provide (or suitably
  144. * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
  145. * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
  146. * in pow5mult, ensures lazy evaluation of only one copy of high
  147. * powers of 5; omitting this lock would introduce a small
  148. * probability of wasting memory, but would otherwise be harmless.)
  149. * You must also invoke freedtoa(s) to free the value s returned by
  150. * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
  151. * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
  152. * avoids underflows on inputs whose result does not underflow.
  153. * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
  154. * floating-point numbers and flushes underflows to zero rather
  155. * than implementing gradual underflow, then you must also #define
  156. * Sudden_Underflow.
  157. * #define USE_LOCALE to use the current locale's decimal_point value.
  158. * #define SET_INEXACT if IEEE arithmetic is being used and extra
  159. * computation should be done to set the inexact flag when the
  160. * result is inexact and avoid setting inexact when the result
  161. * is exact. In this case, dtoa.c must be compiled in
  162. * an environment, perhaps provided by #include "dtoa.c" in a
  163. * suitable wrapper, that defines two functions,
  164. * int get_inexact(void);
  165. * void clear_inexact(void);
  166. * such that get_inexact() returns a nonzero value if the
  167. * inexact bit is already set, and clear_inexact() sets the
  168. * inexact bit to 0. When SET_INEXACT is #defined, strtod
  169. * also does extra computations to set the underflow and overflow
  170. * flags when appropriate (i.e., when the result is tiny and
  171. * inexact or when it is a numeric value rounded to +-infinity).
  172. * #define NO_ERRNO if strtod should not assign errno = ERANGE when
  173. * the result overflows to +-Infinity or underflows to 0.
  174. * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
  175. * values by strtod.
  176. * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
  177. * to disable logic for "fast" testing of very long input strings
  178. * to strtod. This testing proceeds by initially truncating the
  179. * input string, then if necessary comparing the whole string with
  180. * a decimal expansion to decide close cases. This logic is only
  181. * used for input more than STRTOD_DIGLIM digits long (default 40).
  182. */
  183. #include "dtoa_config.h"
  184. #ifndef Long
  185. #define Long long
  186. #endif
  187. #ifndef ULong
  188. typedef unsigned Long ULong;
  189. #endif
  190. #ifdef DEBUG
  191. #include "stdio.h"
  192. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  193. #endif
  194. #include "stdlib.h"
  195. #include "string.h"
  196. #ifdef USE_LOCALE
  197. #include "locale.h"
  198. #endif
  199. #ifdef Honor_FLT_ROUNDS
  200. #ifndef Trust_FLT_ROUNDS
  201. #include <fenv.h>
  202. #endif
  203. #endif
  204. #ifdef MALLOC
  205. #ifdef KR_headers
  206. extern char *MALLOC();
  207. #else
  208. extern void *MALLOC(size_t);
  209. #endif
  210. #else
  211. #define MALLOC malloc
  212. #endif
  213. #ifndef Omit_Private_Memory
  214. #ifndef PRIVATE_MEM
  215. #define PRIVATE_MEM 2304
  216. #endif
  217. #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
  218. static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
  219. #endif
  220. #undef IEEE_Arith
  221. #undef Avoid_Underflow
  222. #ifdef IEEE_MC68k
  223. #define IEEE_Arith
  224. #endif
  225. #ifdef IEEE_8087
  226. #define IEEE_Arith
  227. #endif
  228. #ifdef IEEE_Arith
  229. #ifndef NO_INFNAN_CHECK
  230. #undef INFNAN_CHECK
  231. #define INFNAN_CHECK
  232. #endif
  233. #else
  234. #undef INFNAN_CHECK
  235. #define NO_STRTOD_BIGCOMP
  236. #endif
  237. #include "errno.h"
  238. #ifdef Bad_float_h
  239. #ifdef IEEE_Arith
  240. #define DBL_DIG 15
  241. #define DBL_MAX_10_EXP 308
  242. #define DBL_MAX_EXP 1024
  243. #define FLT_RADIX 2
  244. #endif /*IEEE_Arith*/
  245. #ifdef IBM
  246. #define DBL_DIG 16
  247. #define DBL_MAX_10_EXP 75
  248. #define DBL_MAX_EXP 63
  249. #define FLT_RADIX 16
  250. #define DBL_MAX 7.2370055773322621e+75
  251. #endif
  252. #ifdef VAX
  253. #define DBL_DIG 16
  254. #define DBL_MAX_10_EXP 38
  255. #define DBL_MAX_EXP 127
  256. #define FLT_RADIX 2
  257. #define DBL_MAX 1.7014118346046923e+38
  258. #endif
  259. #ifndef LONG_MAX
  260. #define LONG_MAX 2147483647
  261. #endif
  262. #else /* ifndef Bad_float_h */
  263. #include "float.h"
  264. #endif /* Bad_float_h */
  265. #ifndef __MATH_H__
  266. #include "math.h"
  267. #endif
  268. #ifdef __cplusplus
  269. extern "C" {
  270. #endif
  271. #ifndef CONST
  272. #ifdef KR_headers
  273. #define CONST /* blank */
  274. #else
  275. #define CONST const
  276. #endif
  277. #endif
  278. #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
  279. Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
  280. #endif
  281. typedef union { double d; ULong L[2]; } U;
  282. #ifdef IEEE_8087
  283. #define word0(x) (x)->L[1]
  284. #define word1(x) (x)->L[0]
  285. #else
  286. #define word0(x) (x)->L[0]
  287. #define word1(x) (x)->L[1]
  288. #endif
  289. #define dval(x) (x)->d
  290. #ifndef STRTOD_DIGLIM
  291. #define STRTOD_DIGLIM 40
  292. #endif
  293. #ifdef DIGLIM_DEBUG
  294. extern int strtod_diglim;
  295. #else
  296. #define strtod_diglim STRTOD_DIGLIM
  297. #endif
  298. /* The following definition of Storeinc is appropriate for MIPS processors.
  299. * An alternative that might be better on some machines is
  300. * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  301. */
  302. #if defined(IEEE_8087) + defined(VAX)
  303. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  304. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  305. #else
  306. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  307. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  308. #endif
  309. /* #define P DBL_MANT_DIG */
  310. /* Ten_pmax = floor(P*log(2)/log(5)) */
  311. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  312. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  313. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  314. #ifdef IEEE_Arith
  315. #define Exp_shift 20
  316. #define Exp_shift1 20
  317. #define Exp_msk1 0x100000
  318. #define Exp_msk11 0x100000
  319. #define Exp_mask 0x7ff00000
  320. #define P 53
  321. #define Nbits 53
  322. #define Bias 1023
  323. #define Emax 1023
  324. #define Emin (-1022)
  325. #define Exp_1 0x3ff00000
  326. #define Exp_11 0x3ff00000
  327. #define Ebits 11
  328. #define Frac_mask 0xfffff
  329. #define Frac_mask1 0xfffff
  330. #define Ten_pmax 22
  331. #define Bletch 0x10
  332. #define Bndry_mask 0xfffff
  333. #define Bndry_mask1 0xfffff
  334. #define LSB 1
  335. #define Sign_bit 0x80000000
  336. #define Log2P 1
  337. #define Tiny0 0
  338. #define Tiny1 1
  339. #define Quick_max 14
  340. #define Int_max 14
  341. #ifndef NO_IEEE_Scale
  342. #define Avoid_Underflow
  343. #ifdef Flush_Denorm /* debugging option */
  344. #undef Sudden_Underflow
  345. #endif
  346. #endif
  347. #ifndef Flt_Rounds
  348. #ifdef FLT_ROUNDS
  349. #define Flt_Rounds FLT_ROUNDS
  350. #else
  351. #define Flt_Rounds 1
  352. #endif
  353. #endif /*Flt_Rounds*/
  354. #ifdef Honor_FLT_ROUNDS
  355. #undef Check_FLT_ROUNDS
  356. #define Check_FLT_ROUNDS
  357. #else
  358. #define Rounding Flt_Rounds
  359. #endif
  360. #else /* ifndef IEEE_Arith */
  361. #undef Check_FLT_ROUNDS
  362. #undef Honor_FLT_ROUNDS
  363. #undef SET_INEXACT
  364. #undef Sudden_Underflow
  365. #define Sudden_Underflow
  366. #ifdef IBM
  367. #undef Flt_Rounds
  368. #define Flt_Rounds 0
  369. #define Exp_shift 24
  370. #define Exp_shift1 24
  371. #define Exp_msk1 0x1000000
  372. #define Exp_msk11 0x1000000
  373. #define Exp_mask 0x7f000000
  374. #define P 14
  375. #define Nbits 56
  376. #define Bias 65
  377. #define Emax 248
  378. #define Emin (-260)
  379. #define Exp_1 0x41000000
  380. #define Exp_11 0x41000000
  381. #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
  382. #define Frac_mask 0xffffff
  383. #define Frac_mask1 0xffffff
  384. #define Bletch 4
  385. #define Ten_pmax 22
  386. #define Bndry_mask 0xefffff
  387. #define Bndry_mask1 0xffffff
  388. #define LSB 1
  389. #define Sign_bit 0x80000000
  390. #define Log2P 4
  391. #define Tiny0 0x100000
  392. #define Tiny1 0
  393. #define Quick_max 14
  394. #define Int_max 15
  395. #else /* VAX */
  396. #undef Flt_Rounds
  397. #define Flt_Rounds 1
  398. #define Exp_shift 23
  399. #define Exp_shift1 7
  400. #define Exp_msk1 0x80
  401. #define Exp_msk11 0x800000
  402. #define Exp_mask 0x7f80
  403. #define P 56
  404. #define Nbits 56
  405. #define Bias 129
  406. #define Emax 126
  407. #define Emin (-129)
  408. #define Exp_1 0x40800000
  409. #define Exp_11 0x4080
  410. #define Ebits 8
  411. #define Frac_mask 0x7fffff
  412. #define Frac_mask1 0xffff007f
  413. #define Ten_pmax 24
  414. #define Bletch 2
  415. #define Bndry_mask 0xffff007f
  416. #define Bndry_mask1 0xffff007f
  417. #define LSB 0x10000
  418. #define Sign_bit 0x8000
  419. #define Log2P 1
  420. #define Tiny0 0x80
  421. #define Tiny1 0
  422. #define Quick_max 15
  423. #define Int_max 15
  424. #endif /* IBM, VAX */
  425. #endif /* IEEE_Arith */
  426. #ifndef IEEE_Arith
  427. #define ROUND_BIASED
  428. #else
  429. #ifdef ROUND_BIASED_without_Round_Up
  430. #undef ROUND_BIASED
  431. #define ROUND_BIASED
  432. #endif
  433. #endif
  434. #ifdef RND_PRODQUOT
  435. #define rounded_product(a,b) a = rnd_prod(a, b)
  436. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  437. #ifdef KR_headers
  438. extern double rnd_prod(), rnd_quot();
  439. #else
  440. extern double rnd_prod(double, double), rnd_quot(double, double);
  441. #endif
  442. #else
  443. #define rounded_product(a,b) a *= b
  444. #define rounded_quotient(a,b) a /= b
  445. #endif
  446. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  447. #define Big1 0xffffffff
  448. #ifndef Pack_32
  449. #define Pack_32
  450. #endif
  451. typedef struct BCinfo BCinfo;
  452. struct
  453. BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; };
  454. #ifdef KR_headers
  455. #define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
  456. #else
  457. #define FFFFFFFF 0xffffffffUL
  458. #endif
  459. #ifdef NO_LONG_LONG
  460. #undef ULLong
  461. #ifdef Just_16
  462. #undef Pack_32
  463. /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
  464. * This makes some inner loops simpler and sometimes saves work
  465. * during multiplications, but it often seems to make things slightly
  466. * slower. Hence the default is now to store 32 bits per Long.
  467. */
  468. #endif
  469. #else /* long long available */
  470. #ifndef Llong
  471. #define Llong long long
  472. #endif
  473. #ifndef ULLong
  474. #define ULLong unsigned Llong
  475. #endif
  476. #endif /* NO_LONG_LONG */
  477. #ifndef MULTIPLE_THREADS
  478. #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
  479. #define FREE_DTOA_LOCK(n) /*nothing*/
  480. #endif
  481. #define Kmax 7
  482. #ifdef __cplusplus
  483. extern "C" double fpconv_strtod(const char *s00, char **se);
  484. extern "C" char *dtoa(double d, int mode, int ndigits,
  485. int *decpt, int *sign, char **rve);
  486. #endif
  487. struct
  488. Bigint {
  489. struct Bigint *next;
  490. int k, maxwds, sign, wds;
  491. ULong x[1];
  492. };
  493. typedef struct Bigint Bigint;
  494. static Bigint *freelist[Kmax+1];
  495. static Bigint *
  496. Balloc
  497. #ifdef KR_headers
  498. (k) int k;
  499. #else
  500. (int k)
  501. #endif
  502. {
  503. int x;
  504. Bigint *rv;
  505. #ifndef Omit_Private_Memory
  506. unsigned int len;
  507. #endif
  508. ACQUIRE_DTOA_LOCK(0);
  509. /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
  510. /* but this case seems very unlikely. */
  511. if (k <= Kmax && (rv = freelist[k]))
  512. freelist[k] = rv->next;
  513. else {
  514. x = 1 << k;
  515. #ifdef Omit_Private_Memory
  516. rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
  517. #else
  518. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  519. /sizeof(double);
  520. if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
  521. rv = (Bigint*)pmem_next;
  522. pmem_next += len;
  523. }
  524. else
  525. rv = (Bigint*)MALLOC(len*sizeof(double));
  526. #endif
  527. rv->k = k;
  528. rv->maxwds = x;
  529. }
  530. FREE_DTOA_LOCK(0);
  531. rv->sign = rv->wds = 0;
  532. return rv;
  533. }
  534. static void
  535. Bfree
  536. #ifdef KR_headers
  537. (v) Bigint *v;
  538. #else
  539. (Bigint *v)
  540. #endif
  541. {
  542. if (v) {
  543. if (v->k > Kmax)
  544. #ifdef FREE
  545. FREE((void*)v);
  546. #else
  547. free((void*)v);
  548. #endif
  549. else {
  550. ACQUIRE_DTOA_LOCK(0);
  551. v->next = freelist[v->k];
  552. freelist[v->k] = v;
  553. FREE_DTOA_LOCK(0);
  554. }
  555. }
  556. }
  557. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  558. y->wds*sizeof(Long) + 2*sizeof(int))
  559. static Bigint *
  560. multadd
  561. #ifdef KR_headers
  562. (b, m, a) Bigint *b; int m, a;
  563. #else
  564. (Bigint *b, int m, int a) /* multiply by m and add a */
  565. #endif
  566. {
  567. int i, wds;
  568. #ifdef ULLong
  569. ULong *x;
  570. ULLong carry, y;
  571. #else
  572. ULong carry, *x, y;
  573. #ifdef Pack_32
  574. ULong xi, z;
  575. #endif
  576. #endif
  577. Bigint *b1;
  578. wds = b->wds;
  579. x = b->x;
  580. i = 0;
  581. carry = a;
  582. do {
  583. #ifdef ULLong
  584. y = *x * (ULLong)m + carry;
  585. carry = y >> 32;
  586. *x++ = y & FFFFFFFF;
  587. #else
  588. #ifdef Pack_32
  589. xi = *x;
  590. y = (xi & 0xffff) * m + carry;
  591. z = (xi >> 16) * m + (y >> 16);
  592. carry = z >> 16;
  593. *x++ = (z << 16) + (y & 0xffff);
  594. #else
  595. y = *x * m + carry;
  596. carry = y >> 16;
  597. *x++ = y & 0xffff;
  598. #endif
  599. #endif
  600. }
  601. while(++i < wds);
  602. if (carry) {
  603. if (wds >= b->maxwds) {
  604. b1 = Balloc(b->k+1);
  605. Bcopy(b1, b);
  606. Bfree(b);
  607. b = b1;
  608. }
  609. b->x[wds++] = carry;
  610. b->wds = wds;
  611. }
  612. return b;
  613. }
  614. static Bigint *
  615. s2b
  616. #ifdef KR_headers
  617. (s, nd0, nd, y9, dplen) CONST char *s; int nd0, nd, dplen; ULong y9;
  618. #else
  619. (const char *s, int nd0, int nd, ULong y9, int dplen)
  620. #endif
  621. {
  622. Bigint *b;
  623. int i, k;
  624. Long x, y;
  625. x = (nd + 8) / 9;
  626. for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  627. #ifdef Pack_32
  628. b = Balloc(k);
  629. b->x[0] = y9;
  630. b->wds = 1;
  631. #else
  632. b = Balloc(k+1);
  633. b->x[0] = y9 & 0xffff;
  634. b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  635. #endif
  636. i = 9;
  637. if (9 < nd0) {
  638. s += 9;
  639. do b = multadd(b, 10, *s++ - '0');
  640. while(++i < nd0);
  641. s += dplen;
  642. }
  643. else
  644. s += dplen + 9;
  645. for(; i < nd; i++)
  646. b = multadd(b, 10, *s++ - '0');
  647. return b;
  648. }
  649. static int
  650. hi0bits
  651. #ifdef KR_headers
  652. (x) ULong x;
  653. #else
  654. (ULong x)
  655. #endif
  656. {
  657. int k = 0;
  658. if (!(x & 0xffff0000)) {
  659. k = 16;
  660. x <<= 16;
  661. }
  662. if (!(x & 0xff000000)) {
  663. k += 8;
  664. x <<= 8;
  665. }
  666. if (!(x & 0xf0000000)) {
  667. k += 4;
  668. x <<= 4;
  669. }
  670. if (!(x & 0xc0000000)) {
  671. k += 2;
  672. x <<= 2;
  673. }
  674. if (!(x & 0x80000000)) {
  675. k++;
  676. if (!(x & 0x40000000))
  677. return 32;
  678. }
  679. return k;
  680. }
  681. static int
  682. lo0bits
  683. #ifdef KR_headers
  684. (y) ULong *y;
  685. #else
  686. (ULong *y)
  687. #endif
  688. {
  689. int k;
  690. ULong x = *y;
  691. if (x & 7) {
  692. if (x & 1)
  693. return 0;
  694. if (x & 2) {
  695. *y = x >> 1;
  696. return 1;
  697. }
  698. *y = x >> 2;
  699. return 2;
  700. }
  701. k = 0;
  702. if (!(x & 0xffff)) {
  703. k = 16;
  704. x >>= 16;
  705. }
  706. if (!(x & 0xff)) {
  707. k += 8;
  708. x >>= 8;
  709. }
  710. if (!(x & 0xf)) {
  711. k += 4;
  712. x >>= 4;
  713. }
  714. if (!(x & 0x3)) {
  715. k += 2;
  716. x >>= 2;
  717. }
  718. if (!(x & 1)) {
  719. k++;
  720. x >>= 1;
  721. if (!x)
  722. return 32;
  723. }
  724. *y = x;
  725. return k;
  726. }
  727. static Bigint *
  728. i2b
  729. #ifdef KR_headers
  730. (i) int i;
  731. #else
  732. (int i)
  733. #endif
  734. {
  735. Bigint *b;
  736. b = Balloc(1);
  737. b->x[0] = i;
  738. b->wds = 1;
  739. return b;
  740. }
  741. static Bigint *
  742. mult
  743. #ifdef KR_headers
  744. (a, b) Bigint *a, *b;
  745. #else
  746. (Bigint *a, Bigint *b)
  747. #endif
  748. {
  749. Bigint *c;
  750. int k, wa, wb, wc;
  751. ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  752. ULong y;
  753. #ifdef ULLong
  754. ULLong carry, z;
  755. #else
  756. ULong carry, z;
  757. #ifdef Pack_32
  758. ULong z2;
  759. #endif
  760. #endif
  761. if (a->wds < b->wds) {
  762. c = a;
  763. a = b;
  764. b = c;
  765. }
  766. k = a->k;
  767. wa = a->wds;
  768. wb = b->wds;
  769. wc = wa + wb;
  770. if (wc > a->maxwds)
  771. k++;
  772. c = Balloc(k);
  773. for(x = c->x, xa = x + wc; x < xa; x++)
  774. *x = 0;
  775. xa = a->x;
  776. xae = xa + wa;
  777. xb = b->x;
  778. xbe = xb + wb;
  779. xc0 = c->x;
  780. #ifdef ULLong
  781. for(; xb < xbe; xc0++) {
  782. if ((y = *xb++)) {
  783. x = xa;
  784. xc = xc0;
  785. carry = 0;
  786. do {
  787. z = *x++ * (ULLong)y + *xc + carry;
  788. carry = z >> 32;
  789. *xc++ = z & FFFFFFFF;
  790. }
  791. while(x < xae);
  792. *xc = carry;
  793. }
  794. }
  795. #else
  796. #ifdef Pack_32
  797. for(; xb < xbe; xb++, xc0++) {
  798. if (y = *xb & 0xffff) {
  799. x = xa;
  800. xc = xc0;
  801. carry = 0;
  802. do {
  803. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  804. carry = z >> 16;
  805. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  806. carry = z2 >> 16;
  807. Storeinc(xc, z2, z);
  808. }
  809. while(x < xae);
  810. *xc = carry;
  811. }
  812. if (y = *xb >> 16) {
  813. x = xa;
  814. xc = xc0;
  815. carry = 0;
  816. z2 = *xc;
  817. do {
  818. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  819. carry = z >> 16;
  820. Storeinc(xc, z, z2);
  821. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  822. carry = z2 >> 16;
  823. }
  824. while(x < xae);
  825. *xc = z2;
  826. }
  827. }
  828. #else
  829. for(; xb < xbe; xc0++) {
  830. if (y = *xb++) {
  831. x = xa;
  832. xc = xc0;
  833. carry = 0;
  834. do {
  835. z = *x++ * y + *xc + carry;
  836. carry = z >> 16;
  837. *xc++ = z & 0xffff;
  838. }
  839. while(x < xae);
  840. *xc = carry;
  841. }
  842. }
  843. #endif
  844. #endif
  845. for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  846. c->wds = wc;
  847. return c;
  848. }
  849. static Bigint *p5s;
  850. static Bigint *
  851. pow5mult
  852. #ifdef KR_headers
  853. (b, k) Bigint *b; int k;
  854. #else
  855. (Bigint *b, int k)
  856. #endif
  857. {
  858. Bigint *b1, *p5, *p51;
  859. int i;
  860. static int p05[3] = { 5, 25, 125 };
  861. if ((i = k & 3))
  862. b = multadd(b, p05[i-1], 0);
  863. if (!(k >>= 2))
  864. return b;
  865. if (!(p5 = p5s)) {
  866. /* first time */
  867. #ifdef MULTIPLE_THREADS
  868. ACQUIRE_DTOA_LOCK(1);
  869. if (!(p5 = p5s)) {
  870. p5 = p5s = i2b(625);
  871. p5->next = 0;
  872. }
  873. FREE_DTOA_LOCK(1);
  874. #else
  875. p5 = p5s = i2b(625);
  876. p5->next = 0;
  877. #endif
  878. }
  879. for(;;) {
  880. if (k & 1) {
  881. b1 = mult(b, p5);
  882. Bfree(b);
  883. b = b1;
  884. }
  885. if (!(k >>= 1))
  886. break;
  887. if (!(p51 = p5->next)) {
  888. #ifdef MULTIPLE_THREADS
  889. ACQUIRE_DTOA_LOCK(1);
  890. if (!(p51 = p5->next)) {
  891. p51 = p5->next = mult(p5,p5);
  892. p51->next = 0;
  893. }
  894. FREE_DTOA_LOCK(1);
  895. #else
  896. p51 = p5->next = mult(p5,p5);
  897. p51->next = 0;
  898. #endif
  899. }
  900. p5 = p51;
  901. }
  902. return b;
  903. }
  904. static Bigint *
  905. lshift
  906. #ifdef KR_headers
  907. (b, k) Bigint *b; int k;
  908. #else
  909. (Bigint *b, int k)
  910. #endif
  911. {
  912. int i, k1, n, n1;
  913. Bigint *b1;
  914. ULong *x, *x1, *xe, z;
  915. #ifdef Pack_32
  916. n = k >> 5;
  917. #else
  918. n = k >> 4;
  919. #endif
  920. k1 = b->k;
  921. n1 = n + b->wds + 1;
  922. for(i = b->maxwds; n1 > i; i <<= 1)
  923. k1++;
  924. b1 = Balloc(k1);
  925. x1 = b1->x;
  926. for(i = 0; i < n; i++)
  927. *x1++ = 0;
  928. x = b->x;
  929. xe = x + b->wds;
  930. #ifdef Pack_32
  931. if (k &= 0x1f) {
  932. k1 = 32 - k;
  933. z = 0;
  934. do {
  935. *x1++ = *x << k | z;
  936. z = *x++ >> k1;
  937. }
  938. while(x < xe);
  939. if ((*x1 = z))
  940. ++n1;
  941. }
  942. #else
  943. if (k &= 0xf) {
  944. k1 = 16 - k;
  945. z = 0;
  946. do {
  947. *x1++ = *x << k & 0xffff | z;
  948. z = *x++ >> k1;
  949. }
  950. while(x < xe);
  951. if (*x1 = z)
  952. ++n1;
  953. }
  954. #endif
  955. else do
  956. *x1++ = *x++;
  957. while(x < xe);
  958. b1->wds = n1 - 1;
  959. Bfree(b);
  960. return b1;
  961. }
  962. static int
  963. cmp
  964. #ifdef KR_headers
  965. (a, b) Bigint *a, *b;
  966. #else
  967. (Bigint *a, Bigint *b)
  968. #endif
  969. {
  970. ULong *xa, *xa0, *xb, *xb0;
  971. int i, j;
  972. i = a->wds;
  973. j = b->wds;
  974. #ifdef DEBUG
  975. if (i > 1 && !a->x[i-1])
  976. Bug("cmp called with a->x[a->wds-1] == 0");
  977. if (j > 1 && !b->x[j-1])
  978. Bug("cmp called with b->x[b->wds-1] == 0");
  979. #endif
  980. if (i -= j)
  981. return i;
  982. xa0 = a->x;
  983. xa = xa0 + j;
  984. xb0 = b->x;
  985. xb = xb0 + j;
  986. for(;;) {
  987. if (*--xa != *--xb)
  988. return *xa < *xb ? -1 : 1;
  989. if (xa <= xa0)
  990. break;
  991. }
  992. return 0;
  993. }
  994. static Bigint *
  995. diff
  996. #ifdef KR_headers
  997. (a, b) Bigint *a, *b;
  998. #else
  999. (Bigint *a, Bigint *b)
  1000. #endif
  1001. {
  1002. Bigint *c;
  1003. int i, wa, wb;
  1004. ULong *xa, *xae, *xb, *xbe, *xc;
  1005. #ifdef ULLong
  1006. ULLong borrow, y;
  1007. #else
  1008. ULong borrow, y;
  1009. #ifdef Pack_32
  1010. ULong z;
  1011. #endif
  1012. #endif
  1013. i = cmp(a,b);
  1014. if (!i) {
  1015. c = Balloc(0);
  1016. c->wds = 1;
  1017. c->x[0] = 0;
  1018. return c;
  1019. }
  1020. if (i < 0) {
  1021. c = a;
  1022. a = b;
  1023. b = c;
  1024. i = 1;
  1025. }
  1026. else
  1027. i = 0;
  1028. c = Balloc(a->k);
  1029. c->sign = i;
  1030. wa = a->wds;
  1031. xa = a->x;
  1032. xae = xa + wa;
  1033. wb = b->wds;
  1034. xb = b->x;
  1035. xbe = xb + wb;
  1036. xc = c->x;
  1037. borrow = 0;
  1038. #ifdef ULLong
  1039. do {
  1040. y = (ULLong)*xa++ - *xb++ - borrow;
  1041. borrow = y >> 32 & (ULong)1;
  1042. *xc++ = y & FFFFFFFF;
  1043. }
  1044. while(xb < xbe);
  1045. while(xa < xae) {
  1046. y = *xa++ - borrow;
  1047. borrow = y >> 32 & (ULong)1;
  1048. *xc++ = y & FFFFFFFF;
  1049. }
  1050. #else
  1051. #ifdef Pack_32
  1052. do {
  1053. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  1054. borrow = (y & 0x10000) >> 16;
  1055. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  1056. borrow = (z & 0x10000) >> 16;
  1057. Storeinc(xc, z, y);
  1058. }
  1059. while(xb < xbe);
  1060. while(xa < xae) {
  1061. y = (*xa & 0xffff) - borrow;
  1062. borrow = (y & 0x10000) >> 16;
  1063. z = (*xa++ >> 16) - borrow;
  1064. borrow = (z & 0x10000) >> 16;
  1065. Storeinc(xc, z, y);
  1066. }
  1067. #else
  1068. do {
  1069. y = *xa++ - *xb++ - borrow;
  1070. borrow = (y & 0x10000) >> 16;
  1071. *xc++ = y & 0xffff;
  1072. }
  1073. while(xb < xbe);
  1074. while(xa < xae) {
  1075. y = *xa++ - borrow;
  1076. borrow = (y & 0x10000) >> 16;
  1077. *xc++ = y & 0xffff;
  1078. }
  1079. #endif
  1080. #endif
  1081. while(!*--xc)
  1082. wa--;
  1083. c->wds = wa;
  1084. return c;
  1085. }
  1086. static double
  1087. ulp
  1088. #ifdef KR_headers
  1089. (x) U *x;
  1090. #else
  1091. (U *x)
  1092. #endif
  1093. {
  1094. Long L;
  1095. U u;
  1096. L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  1097. #ifndef Avoid_Underflow
  1098. #ifndef Sudden_Underflow
  1099. if (L > 0) {
  1100. #endif
  1101. #endif
  1102. #ifdef IBM
  1103. L |= Exp_msk1 >> 4;
  1104. #endif
  1105. word0(&u) = L;
  1106. word1(&u) = 0;
  1107. #ifndef Avoid_Underflow
  1108. #ifndef Sudden_Underflow
  1109. }
  1110. else {
  1111. L = -L >> Exp_shift;
  1112. if (L < Exp_shift) {
  1113. word0(&u) = 0x80000 >> L;
  1114. word1(&u) = 0;
  1115. }
  1116. else {
  1117. word0(&u) = 0;
  1118. L -= Exp_shift;
  1119. word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
  1120. }
  1121. }
  1122. #endif
  1123. #endif
  1124. return dval(&u);
  1125. }
  1126. static double
  1127. b2d
  1128. #ifdef KR_headers
  1129. (a, e) Bigint *a; int *e;
  1130. #else
  1131. (Bigint *a, int *e)
  1132. #endif
  1133. {
  1134. ULong *xa, *xa0, w, y, z;
  1135. int k;
  1136. U d;
  1137. #ifdef VAX
  1138. ULong d0, d1;
  1139. #else
  1140. #define d0 word0(&d)
  1141. #define d1 word1(&d)
  1142. #endif
  1143. xa0 = a->x;
  1144. xa = xa0 + a->wds;
  1145. y = *--xa;
  1146. #ifdef DEBUG
  1147. if (!y) Bug("zero y in b2d");
  1148. #endif
  1149. k = hi0bits(y);
  1150. *e = 32 - k;
  1151. #ifdef Pack_32
  1152. if (k < Ebits) {
  1153. d0 = Exp_1 | y >> (Ebits - k);
  1154. w = xa > xa0 ? *--xa : 0;
  1155. d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  1156. goto ret_d;
  1157. }
  1158. z = xa > xa0 ? *--xa : 0;
  1159. if (k -= Ebits) {
  1160. d0 = Exp_1 | y << k | z >> (32 - k);
  1161. y = xa > xa0 ? *--xa : 0;
  1162. d1 = z << k | y >> (32 - k);
  1163. }
  1164. else {
  1165. d0 = Exp_1 | y;
  1166. d1 = z;
  1167. }
  1168. #else
  1169. if (k < Ebits + 16) {
  1170. z = xa > xa0 ? *--xa : 0;
  1171. d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  1172. w = xa > xa0 ? *--xa : 0;
  1173. y = xa > xa0 ? *--xa : 0;
  1174. d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  1175. goto ret_d;
  1176. }
  1177. z = xa > xa0 ? *--xa : 0;
  1178. w = xa > xa0 ? *--xa : 0;
  1179. k -= Ebits + 16;
  1180. d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  1181. y = xa > xa0 ? *--xa : 0;
  1182. d1 = w << k + 16 | y << k;
  1183. #endif
  1184. ret_d:
  1185. #ifdef VAX
  1186. word0(&d) = d0 >> 16 | d0 << 16;
  1187. word1(&d) = d1 >> 16 | d1 << 16;
  1188. #else
  1189. #undef d0
  1190. #undef d1
  1191. #endif
  1192. return dval(&d);
  1193. }
  1194. static Bigint *
  1195. d2b
  1196. #ifdef KR_headers
  1197. (d, e, bits) U *d; int *e, *bits;
  1198. #else
  1199. (U *d, int *e, int *bits)
  1200. #endif
  1201. {
  1202. Bigint *b;
  1203. int de, k;
  1204. ULong *x, y, z;
  1205. #ifndef Sudden_Underflow
  1206. int i;
  1207. #endif
  1208. #ifdef VAX
  1209. ULong d0, d1;
  1210. d0 = word0(d) >> 16 | word0(d) << 16;
  1211. d1 = word1(d) >> 16 | word1(d) << 16;
  1212. #else
  1213. #define d0 word0(d)
  1214. #define d1 word1(d)
  1215. #endif
  1216. #ifdef Pack_32
  1217. b = Balloc(1);
  1218. #else
  1219. b = Balloc(2);
  1220. #endif
  1221. x = b->x;
  1222. z = d0 & Frac_mask;
  1223. d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
  1224. #ifdef Sudden_Underflow
  1225. de = (int)(d0 >> Exp_shift);
  1226. #ifndef IBM
  1227. z |= Exp_msk11;
  1228. #endif
  1229. #else
  1230. if ((de = (int)(d0 >> Exp_shift)))
  1231. z |= Exp_msk1;
  1232. #endif
  1233. #ifdef Pack_32
  1234. if ((y = d1)) {
  1235. if ((k = lo0bits(&y))) {
  1236. x[0] = y | z << (32 - k);
  1237. z >>= k;
  1238. }
  1239. else
  1240. x[0] = y;
  1241. #ifndef Sudden_Underflow
  1242. i =
  1243. #endif
  1244. b->wds = (x[1] = z) ? 2 : 1;
  1245. }
  1246. else {
  1247. k = lo0bits(&z);
  1248. x[0] = z;
  1249. #ifndef Sudden_Underflow
  1250. i =
  1251. #endif
  1252. b->wds = 1;
  1253. k += 32;
  1254. }
  1255. #else
  1256. if (y = d1) {
  1257. if (k = lo0bits(&y))
  1258. if (k >= 16) {
  1259. x[0] = y | z << 32 - k & 0xffff;
  1260. x[1] = z >> k - 16 & 0xffff;
  1261. x[2] = z >> k;
  1262. i = 2;
  1263. }
  1264. else {
  1265. x[0] = y & 0xffff;
  1266. x[1] = y >> 16 | z << 16 - k & 0xffff;
  1267. x[2] = z >> k & 0xffff;
  1268. x[3] = z >> k+16;
  1269. i = 3;
  1270. }
  1271. else {
  1272. x[0] = y & 0xffff;
  1273. x[1] = y >> 16;
  1274. x[2] = z & 0xffff;
  1275. x[3] = z >> 16;
  1276. i = 3;
  1277. }
  1278. }
  1279. else {
  1280. #ifdef DEBUG
  1281. if (!z)
  1282. Bug("Zero passed to d2b");
  1283. #endif
  1284. k = lo0bits(&z);
  1285. if (k >= 16) {
  1286. x[0] = z;
  1287. i = 0;
  1288. }
  1289. else {
  1290. x[0] = z & 0xffff;
  1291. x[1] = z >> 16;
  1292. i = 1;
  1293. }
  1294. k += 32;
  1295. }
  1296. while(!x[i])
  1297. --i;
  1298. b->wds = i + 1;
  1299. #endif
  1300. #ifndef Sudden_Underflow
  1301. if (de) {
  1302. #endif
  1303. #ifdef IBM
  1304. *e = (de - Bias - (P-1) << 2) + k;
  1305. *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1306. #else
  1307. *e = de - Bias - (P-1) + k;
  1308. *bits = P - k;
  1309. #endif
  1310. #ifndef Sudden_Underflow
  1311. }
  1312. else {
  1313. *e = de - Bias - (P-1) + 1 + k;
  1314. #ifdef Pack_32
  1315. *bits = 32*i - hi0bits(x[i-1]);
  1316. #else
  1317. *bits = (i+2)*16 - hi0bits(x[i]);
  1318. #endif
  1319. }
  1320. #endif
  1321. return b;
  1322. }
  1323. #undef d0
  1324. #undef d1
  1325. static double
  1326. ratio
  1327. #ifdef KR_headers
  1328. (a, b) Bigint *a, *b;
  1329. #else
  1330. (Bigint *a, Bigint *b)
  1331. #endif
  1332. {
  1333. U da, db;
  1334. int k, ka, kb;
  1335. dval(&da) = b2d(a, &ka);
  1336. dval(&db) = b2d(b, &kb);
  1337. #ifdef Pack_32
  1338. k = ka - kb + 32*(a->wds - b->wds);
  1339. #else
  1340. k = ka - kb + 16*(a->wds - b->wds);
  1341. #endif
  1342. #ifdef IBM
  1343. if (k > 0) {
  1344. word0(&da) += (k >> 2)*Exp_msk1;
  1345. if (k &= 3)
  1346. dval(&da) *= 1 << k;
  1347. }
  1348. else {
  1349. k = -k;
  1350. word0(&db) += (k >> 2)*Exp_msk1;
  1351. if (k &= 3)
  1352. dval(&db) *= 1 << k;
  1353. }
  1354. #else
  1355. if (k > 0)
  1356. word0(&da) += k*Exp_msk1;
  1357. else {
  1358. k = -k;
  1359. word0(&db) += k*Exp_msk1;
  1360. }
  1361. #endif
  1362. return dval(&da) / dval(&db);
  1363. }
  1364. static CONST double
  1365. tens[] = {
  1366. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1367. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1368. 1e20, 1e21, 1e22
  1369. #ifdef VAX
  1370. , 1e23, 1e24
  1371. #endif
  1372. };
  1373. static CONST double
  1374. #ifdef IEEE_Arith
  1375. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1376. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
  1377. #ifdef Avoid_Underflow
  1378. 9007199254740992.*9007199254740992.e-256
  1379. /* = 2^106 * 1e-256 */
  1380. #else
  1381. 1e-256
  1382. #endif
  1383. };
  1384. /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
  1385. /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
  1386. #define Scale_Bit 0x10
  1387. #define n_bigtens 5
  1388. #else
  1389. #ifdef IBM
  1390. bigtens[] = { 1e16, 1e32, 1e64 };
  1391. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1392. #define n_bigtens 3
  1393. #else
  1394. bigtens[] = { 1e16, 1e32 };
  1395. static CONST double tinytens[] = { 1e-16, 1e-32 };
  1396. #define n_bigtens 2
  1397. #endif
  1398. #endif
  1399. #undef Need_Hexdig
  1400. #ifdef INFNAN_CHECK
  1401. #ifndef No_Hex_NaN
  1402. #define Need_Hexdig
  1403. #endif
  1404. #endif
  1405. #ifndef Need_Hexdig
  1406. #ifndef NO_HEX_FP
  1407. #define Need_Hexdig
  1408. #endif
  1409. #endif
  1410. #ifdef Need_Hexdig /*{*/
  1411. static unsigned char hexdig[256];
  1412. static void
  1413. #ifdef KR_headers
  1414. htinit(h, s, inc) unsigned char *h; unsigned char *s; int inc;
  1415. #else
  1416. htinit(unsigned char *h, unsigned char *s, int inc)
  1417. #endif
  1418. {
  1419. int i, j;
  1420. for(i = 0; (j = s[i]) !=0; i++)
  1421. h[j] = i + inc;
  1422. }
  1423. static void
  1424. #ifdef KR_headers
  1425. hexdig_init()
  1426. #else
  1427. hexdig_init(void)
  1428. #endif
  1429. {
  1430. #define USC (unsigned char *)
  1431. htinit(hexdig, USC "0123456789", 0x10);
  1432. htinit(hexdig, USC "abcdef", 0x10 + 10);
  1433. htinit(hexdig, USC "ABCDEF", 0x10 + 10);
  1434. }
  1435. #endif /* } Need_Hexdig */
  1436. #ifdef INFNAN_CHECK
  1437. #ifndef NAN_WORD0
  1438. #define NAN_WORD0 0x7ff80000
  1439. #endif
  1440. #ifndef NAN_WORD1
  1441. #define NAN_WORD1 0
  1442. #endif
  1443. static int
  1444. match
  1445. #ifdef KR_headers
  1446. (sp, t) char **sp, *t;
  1447. #else
  1448. (const char **sp, const char *t)
  1449. #endif
  1450. {
  1451. int c, d;
  1452. CONST char *s = *sp;
  1453. while((d = *t++)) {
  1454. if ((c = *++s) >= 'A' && c <= 'Z')
  1455. c += 'a' - 'A';
  1456. if (c != d)
  1457. return 0;
  1458. }
  1459. *sp = s + 1;
  1460. return 1;
  1461. }
  1462. #ifndef No_Hex_NaN
  1463. static void
  1464. hexnan
  1465. #ifdef KR_headers
  1466. (rvp, sp) U *rvp; CONST char **sp;
  1467. #else
  1468. (U *rvp, const char **sp)
  1469. #endif
  1470. {
  1471. ULong c, x[2];
  1472. CONST char *s;
  1473. int c1, havedig, udx0, xshift;
  1474. if (!hexdig['0'])
  1475. hexdig_init();
  1476. x[0] = x[1] = 0;
  1477. havedig = xshift = 0;
  1478. udx0 = 1;
  1479. s = *sp;
  1480. /* allow optional initial 0x or 0X */
  1481. while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
  1482. ++s;
  1483. if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
  1484. s += 2;
  1485. while((c = *(CONST unsigned char*)++s)) {
  1486. if ((c1 = hexdig[c]))
  1487. c = c1 & 0xf;
  1488. else if (c <= ' ') {
  1489. if (udx0 && havedig) {
  1490. udx0 = 0;
  1491. xshift = 1;
  1492. }
  1493. continue;
  1494. }
  1495. #ifdef GDTOA_NON_PEDANTIC_NANCHECK
  1496. else if (/*(*/ c == ')' && havedig) {
  1497. *sp = s + 1;
  1498. break;
  1499. }
  1500. else
  1501. return; /* invalid form: don't change *sp */
  1502. #else
  1503. else {
  1504. do {
  1505. if (/*(*/ c == ')') {
  1506. *sp = s + 1;
  1507. break;
  1508. }
  1509. } while((c = *++s));
  1510. break;
  1511. }
  1512. #endif
  1513. havedig = 1;
  1514. if (xshift) {
  1515. xshift = 0;
  1516. x[0] = x[1];
  1517. x[1] = 0;
  1518. }
  1519. if (udx0)
  1520. x[0] = (x[0] << 4) | (x[1] >> 28);
  1521. x[1] = (x[1] << 4) | c;
  1522. }
  1523. if ((x[0] &= 0xfffff) || x[1]) {
  1524. word0(rvp) = Exp_mask | x[0];
  1525. word1(rvp) = x[1];
  1526. }
  1527. }
  1528. #endif /*No_Hex_NaN*/
  1529. #endif /* INFNAN_CHECK */
  1530. #ifdef Pack_32
  1531. #define ULbits 32
  1532. #define kshift 5
  1533. #define kmask 31
  1534. #else
  1535. #define ULbits 16
  1536. #define kshift 4
  1537. #define kmask 15
  1538. #endif
  1539. #if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/
  1540. static Bigint *
  1541. #ifdef KR_headers
  1542. increment(b) Bigint *b;
  1543. #else
  1544. increment(Bigint *b)
  1545. #endif
  1546. {
  1547. ULong *x, *xe;
  1548. Bigint *b1;
  1549. x = b->x;
  1550. xe = x + b->wds;
  1551. do {
  1552. if (*x < (ULong)0xffffffffL) {
  1553. ++*x;
  1554. return b;
  1555. }
  1556. *x++ = 0;
  1557. } while(x < xe);
  1558. {
  1559. if (b->wds >= b->maxwds) {
  1560. b1 = Balloc(b->k+1);
  1561. Bcopy(b1,b);
  1562. Bfree(b);
  1563. b = b1;
  1564. }
  1565. b->x[b->wds++] = 1;
  1566. }
  1567. return b;
  1568. }
  1569. #endif /*}*/
  1570. #ifndef NO_HEX_FP /*{*/
  1571. static void
  1572. #ifdef KR_headers
  1573. rshift(b, k) Bigint *b; int k;
  1574. #else
  1575. rshift(Bigint *b, int k)
  1576. #endif
  1577. {
  1578. ULong *x, *x1, *xe, y;
  1579. int n;
  1580. x = x1 = b->x;
  1581. n = k >> kshift;
  1582. if (n < b->wds) {
  1583. xe = x + b->wds;
  1584. x += n;
  1585. if (k &= kmask) {
  1586. n = 32 - k;
  1587. y = *x++ >> k;
  1588. while(x < xe) {
  1589. *x1++ = (y | (*x << n)) & 0xffffffff;
  1590. y = *x++ >> k;
  1591. }
  1592. if ((*x1 = y) !=0)
  1593. x1++;
  1594. }
  1595. else
  1596. while(x < xe)
  1597. *x1++ = *x++;
  1598. }
  1599. if ((b->wds = x1 - b->x) == 0)
  1600. b->x[0] = 0;
  1601. }
  1602. static ULong
  1603. #ifdef KR_headers
  1604. any_on(b, k) Bigint *b; int k;
  1605. #else
  1606. any_on(Bigint *b, int k)
  1607. #endif
  1608. {
  1609. int n, nwds;
  1610. ULong *x, *x0, x1, x2;
  1611. x = b->x;
  1612. nwds = b->wds;
  1613. n = k >> kshift;
  1614. if (n > nwds)
  1615. n = nwds;
  1616. else if (n < nwds && (k &= kmask)) {
  1617. x1 = x2 = x[n];
  1618. x1 >>= k;
  1619. x1 <<= k;
  1620. if (x1 != x2)
  1621. return 1;
  1622. }
  1623. x0 = x;
  1624. x += n;
  1625. while(x > x0)
  1626. if (*--x)
  1627. return 1;
  1628. return 0;
  1629. }
  1630. enum { /* rounding values: same as FLT_ROUNDS */
  1631. Round_zero = 0,
  1632. Round_near = 1,
  1633. Round_up = 2,
  1634. Round_down = 3
  1635. };
  1636. void
  1637. #ifdef KR_headers
  1638. gethex(sp, rvp, rounding, sign)
  1639. CONST char **sp; U *rvp; int rounding, sign;
  1640. #else
  1641. gethex( CONST char **sp, U *rvp, int rounding, int sign)
  1642. #endif
  1643. {
  1644. Bigint *b;
  1645. CONST unsigned char *decpt, *s0, *s, *s1;
  1646. Long e, e1;
  1647. ULong L, lostbits, *x;
  1648. int big, denorm, esign, havedig, k, n, nbits, up, zret;
  1649. #ifdef IBM
  1650. int j;
  1651. #endif
  1652. enum {
  1653. #ifdef IEEE_Arith /*{{*/
  1654. emax = 0x7fe - Bias - P + 1,
  1655. emin = Emin - P + 1
  1656. #else /*}{*/
  1657. emin = Emin - P,
  1658. #ifdef VAX
  1659. emax = 0x7ff - Bias - P + 1
  1660. #endif
  1661. #ifdef IBM
  1662. emax = 0x7f - Bias - P
  1663. #endif
  1664. #endif /*}}*/
  1665. };
  1666. #ifdef USE_LOCALE
  1667. int i;
  1668. #ifdef NO_LOCALE_CACHE
  1669. const unsigned char *decimalpoint = (unsigned char*)
  1670. localeconv()->decimal_point;
  1671. #else
  1672. const unsigned char *decimalpoint;
  1673. static unsigned char *decimalpoint_cache;
  1674. if (!(s0 = decimalpoint_cache)) {
  1675. s0 = (unsigned char*)localeconv()->decimal_point;
  1676. if ((decimalpoint_cache = (unsigned char*)
  1677. MALLOC(strlen((CONST char*)s0) + 1))) {
  1678. strcpy((char*)decimalpoint_cache, (CONST char*)s0);
  1679. s0 = decimalpoint_cache;
  1680. }
  1681. }
  1682. decimalpoint = s0;
  1683. #endif
  1684. #endif
  1685. if (!hexdig['0'])
  1686. hexdig_init();
  1687. havedig = 0;
  1688. s0 = *(CONST unsigned char **)sp + 2;
  1689. while(s0[havedig] == '0')
  1690. havedig++;
  1691. s0 += havedig;
  1692. s = s0;
  1693. decpt = 0;
  1694. zret = 0;
  1695. e = 0;
  1696. if (hexdig[*s])
  1697. havedig++;
  1698. else {
  1699. zret = 1;
  1700. #ifdef USE_LOCALE
  1701. for(i = 0; decimalpoint[i]; ++i) {
  1702. if (s[i] != decimalpoint[i])
  1703. goto pcheck;
  1704. }
  1705. decpt = s += i;
  1706. #else
  1707. if (*s != '.')
  1708. goto pcheck;
  1709. decpt = ++s;
  1710. #endif
  1711. if (!hexdig[*s])
  1712. goto pcheck;
  1713. while(*s == '0')
  1714. s++;
  1715. if (hexdig[*s])
  1716. zret = 0;
  1717. havedig = 1;
  1718. s0 = s;
  1719. }
  1720. while(hexdig[*s])
  1721. s++;
  1722. #ifdef USE_LOCALE
  1723. if (*s == *decimalpoint && !decpt) {
  1724. for(i = 1; decimalpoint[i]; ++i) {
  1725. if (s[i] != decimalpoint[i])
  1726. goto pcheck;
  1727. }
  1728. decpt = s += i;
  1729. #else
  1730. if (*s == '.' && !decpt) {
  1731. decpt = ++s;
  1732. #endif
  1733. while(hexdig[*s])
  1734. s++;
  1735. }/*}*/
  1736. if (decpt)
  1737. e = -(((Long)(s-decpt)) << 2);
  1738. pcheck:
  1739. s1 = s;
  1740. big = esign = 0;
  1741. switch(*s) {
  1742. case 'p':
  1743. case 'P':
  1744. switch(*++s) {
  1745. case '-':
  1746. esign = 1;
  1747. /* no break */
  1748. case '+':
  1749. s++;
  1750. }
  1751. if ((n = hexdig[*s]) == 0 || n > 0x19) {
  1752. s = s1;
  1753. break;
  1754. }
  1755. e1 = n - 0x10;
  1756. while((n = hexdig[*++s]) !=0 && n <= 0x19) {
  1757. if (e1 & 0xf8000000)
  1758. big = 1;
  1759. e1 = 10*e1 + n - 0x10;
  1760. }
  1761. if (esign)
  1762. e1 = -e1;
  1763. e += e1;
  1764. }
  1765. *sp = (char*)s;
  1766. if (!havedig)
  1767. *sp = (char*)s0 - 1;
  1768. if (zret)
  1769. goto retz1;
  1770. if (big) {
  1771. if (esign) {
  1772. #ifdef IEEE_Arith
  1773. switch(rounding) {
  1774. case Round_up:
  1775. if (sign)
  1776. break;
  1777. goto ret_tiny;
  1778. case Round_down:
  1779. if (!sign)
  1780. break;
  1781. goto ret_tiny;
  1782. }
  1783. #endif
  1784. goto retz;
  1785. #ifdef IEEE_Arith
  1786. ret_tiny:
  1787. #ifndef NO_ERRNO
  1788. errno = ERANGE;
  1789. #endif
  1790. word0(rvp) = 0;
  1791. word1(rvp) = 1;
  1792. return;
  1793. #endif /* IEEE_Arith */
  1794. }
  1795. switch(rounding) {
  1796. case Round_near:
  1797. goto ovfl1;
  1798. case Round_up:
  1799. if (!sign)
  1800. goto ovfl1;
  1801. goto ret_big;
  1802. case Round_down:
  1803. if (sign)
  1804. goto ovfl1;
  1805. goto ret_big;
  1806. }
  1807. ret_big:
  1808. word0(rvp) = Big0;
  1809. word1(rvp) = Big1;
  1810. return;
  1811. }
  1812. n = s1 - s0 - 1;
  1813. for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
  1814. k++;
  1815. b = Balloc(k);
  1816. x = b->x;
  1817. n = 0;
  1818. L = 0;
  1819. #ifdef USE_LOCALE
  1820. for(i = 0; decimalpoint[i+1]; ++i);
  1821. #endif
  1822. while(s1 > s0) {
  1823. #ifdef USE_LOCALE
  1824. if (*--s1 == decimalpoint[i]) {
  1825. s1 -= i;
  1826. continue;
  1827. }
  1828. #else
  1829. if (*--s1 == '.')
  1830. continue;
  1831. #endif
  1832. if (n == ULbits) {
  1833. *x++ = L;
  1834. L = 0;
  1835. n = 0;
  1836. }
  1837. L |= (hexdig[*s1] & 0x0f) << n;
  1838. n += 4;
  1839. }
  1840. *x++ = L;
  1841. b->wds = n = x - b->x;
  1842. n = ULbits*n - hi0bits(L);
  1843. nbits = Nbits;
  1844. lostbits = 0;
  1845. x = b->x;
  1846. if (n > nbits) {
  1847. n -= nbits;
  1848. if (any_on(b,n)) {
  1849. lostbits = 1;
  1850. k = n - 1;
  1851. if (x[k>>kshift] & 1 << (k & kmask)) {
  1852. lostbits = 2;
  1853. if (k > 0 && any_on(b,k))
  1854. lostbits = 3;
  1855. }
  1856. }
  1857. rshift(b, n);
  1858. e += n;
  1859. }
  1860. else if (n < nbits) {
  1861. n = nbits - n;
  1862. b = lshift(b, n);
  1863. e -= n;
  1864. x = b->x;
  1865. }
  1866. if (e > Emax) {
  1867. ovfl:
  1868. Bfree(b);
  1869. ovfl1:
  1870. #ifndef NO_ERRNO
  1871. errno = ERANGE;
  1872. #endif
  1873. word0(rvp) = Exp_mask;
  1874. word1(rvp) = 0;
  1875. return;
  1876. }
  1877. denorm = 0;
  1878. if (e < emin) {
  1879. denorm = 1;
  1880. n = emin - e;
  1881. if (n >= nbits) {
  1882. #ifdef IEEE_Arith /*{*/
  1883. switch (rounding) {
  1884. case Round_near:
  1885. if (n == nbits && (n < 2 || any_on(b,n-1)))
  1886. goto ret_tiny;
  1887. break;
  1888. case Round_up:
  1889. if (!sign)
  1890. goto ret_tiny;
  1891. break;
  1892. case Round_down:
  1893. if (sign)
  1894. goto ret_tiny;
  1895. }
  1896. #endif /* } IEEE_Arith */
  1897. Bfree(b);
  1898. retz:
  1899. #ifndef NO_ERRNO
  1900. errno = ERANGE;
  1901. #endif
  1902. retz1:
  1903. rvp->d = 0.;
  1904. return;
  1905. }
  1906. k = n - 1;
  1907. if (lostbits)
  1908. lostbits = 1;
  1909. else if (k > 0)
  1910. lostbits = any_on(b,k);
  1911. if (x[k>>kshift] & 1 << (k & kmask))
  1912. lostbits |= 2;
  1913. nbits -= n;
  1914. rshift(b,n);
  1915. e = emin;
  1916. }
  1917. if (lostbits) {
  1918. up = 0;
  1919. switch(rounding) {
  1920. case Round_zero:
  1921. break;
  1922. case Round_near:
  1923. if (lostbits & 2
  1924. && (lostbits & 1) | (x[0] & 1))
  1925. up = 1;
  1926. break;
  1927. case Round_up:
  1928. up = 1 - sign;
  1929. break;
  1930. case Round_down:
  1931. up = sign;
  1932. }
  1933. if (up) {
  1934. k = b->wds;
  1935. b = increment(b);
  1936. x = b->x;
  1937. if (denorm) {
  1938. #if 0
  1939. if (nbits == Nbits - 1
  1940. && x[nbits >> kshift] & 1 << (nbits & kmask))
  1941. denorm = 0; /* not currently used */
  1942. #endif
  1943. }
  1944. else if (b->wds > k
  1945. || ((n = nbits & kmask) !=0
  1946. && hi0bits(x[k-1]) < 32-n)) {
  1947. rshift(b,1);
  1948. if (++e > Emax)
  1949. goto ovfl;
  1950. }
  1951. }
  1952. }
  1953. #ifdef IEEE_Arith
  1954. if (denorm)
  1955. word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
  1956. else
  1957. word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
  1958. word1(rvp) = b->x[0];
  1959. #endif
  1960. #ifdef IBM
  1961. if ((j = e & 3)) {
  1962. k = b->x[0] & ((1 << j) - 1);
  1963. rshift(b,j);
  1964. if (k) {
  1965. switch(rounding) {
  1966. case Round_up:
  1967. if (!sign)
  1968. increment(b);
  1969. break;
  1970. case Round_down:
  1971. if (sign)
  1972. increment(b);
  1973. break;
  1974. case Round_near:
  1975. j = 1 << (j-1);
  1976. if (k & j && ((k & (j-1)) | lostbits))
  1977. increment(b);
  1978. }
  1979. }
  1980. }
  1981. e >>= 2;
  1982. word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);
  1983. word1(rvp) = b->x[0];
  1984. #endif
  1985. #ifdef VAX
  1986. /* The next two lines ignore swap of low- and high-order 2 bytes. */
  1987. /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
  1988. /* word1(rvp) = b->x[0]; */
  1989. word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);
  1990. word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);
  1991. #endif
  1992. Bfree(b);
  1993. }
  1994. #endif /*!NO_HEX_FP}*/
  1995. static int
  1996. #ifdef KR_headers
  1997. dshift(b, p2) Bigint *b; int p2;
  1998. #else
  1999. dshift(Bigint *b, int p2)
  2000. #endif
  2001. {
  2002. int rv = hi0bits(b->x[b->wds-1]) - 4;
  2003. if (p2 > 0)
  2004. rv -= p2;
  2005. return rv & kmask;
  2006. }
  2007. static int
  2008. quorem
  2009. #ifdef KR_headers
  2010. (b, S) Bigint *b, *S;
  2011. #else
  2012. (Bigint *b, Bigint *S)
  2013. #endif
  2014. {
  2015. int n;
  2016. ULong *bx, *bxe, q, *sx, *sxe;
  2017. #ifdef ULLong
  2018. ULLong borrow, carry, y, ys;
  2019. #else
  2020. ULong borrow, carry, y, ys;
  2021. #ifdef Pack_32
  2022. ULong si, z, zs;
  2023. #endif
  2024. #endif
  2025. n = S->wds;
  2026. #ifdef DEBUG
  2027. /*debug*/ if (b->wds > n)
  2028. /*debug*/ Bug("oversize b in quorem");
  2029. #endif
  2030. if (b->wds < n)
  2031. return 0;
  2032. sx = S->x;
  2033. sxe = sx + --n;
  2034. bx = b->x;
  2035. bxe = bx + n;
  2036. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  2037. #ifdef DEBUG
  2038. #ifdef NO_STRTOD_BIGCOMP
  2039. /*debug*/ if (q > 9)
  2040. #else
  2041. /* An oversized q is possible when quorem is called from bigcomp and */
  2042. /* the input is near, e.g., twice the smallest denormalized number. */
  2043. /*debug*/ if (q > 15)
  2044. #endif
  2045. /*debug*/ Bug("oversized quotient in quorem");
  2046. #endif
  2047. if (q) {
  2048. borrow = 0;
  2049. carry = 0;
  2050. do {
  2051. #ifdef ULLong
  2052. ys = *sx++ * (ULLong)q + carry;
  2053. carry = ys >> 32;
  2054. y = *bx - (ys & FFFFFFFF) - borrow;
  2055. borrow = y >> 32 & (ULong)1;
  2056. *bx++ = y & FFFFFFFF;
  2057. #else
  2058. #ifdef Pack_32
  2059. si = *sx++;
  2060. ys = (si & 0xffff) * q + carry;
  2061. zs = (si >> 16) * q + (ys >> 16);
  2062. carry = zs >> 16;
  2063. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  2064. borrow = (y & 0x10000) >> 16;
  2065. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  2066. borrow = (z & 0x10000) >> 16;
  2067. Storeinc(bx, z, y);
  2068. #else
  2069. ys = *sx++ * q + carry;
  2070. carry = ys >> 16;
  2071. y = *bx - (ys & 0xffff) - borrow;
  2072. borrow = (y & 0x10000) >> 16;
  2073. *bx++ = y & 0xffff;
  2074. #endif
  2075. #endif
  2076. }
  2077. while(sx <= sxe);
  2078. if (!*bxe) {
  2079. bx = b->x;
  2080. while(--bxe > bx && !*bxe)
  2081. --n;
  2082. b->wds = n;
  2083. }
  2084. }
  2085. if (cmp(b, S) >= 0) {
  2086. q++;
  2087. borrow = 0;
  2088. carry = 0;
  2089. bx = b->x;
  2090. sx = S->x;
  2091. do {
  2092. #ifdef ULLong
  2093. ys = *sx++ + carry;
  2094. carry = ys >> 32;
  2095. y = *bx - (ys & FFFFFFFF) - borrow;
  2096. borrow = y >> 32 & (ULong)1;
  2097. *bx++ = y & FFFFFFFF;
  2098. #else
  2099. #ifdef Pack_32
  2100. si = *sx++;
  2101. ys = (si & 0xffff) + carry;
  2102. zs = (si >> 16) + (ys >> 16);
  2103. carry = zs >> 16;
  2104. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  2105. borrow = (y & 0x10000) >> 16;
  2106. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  2107. borrow = (z & 0x10000) >> 16;
  2108. Storeinc(bx, z, y);
  2109. #else
  2110. ys = *sx++ + carry;
  2111. carry = ys >> 16;
  2112. y = *bx - (ys & 0xffff) - borrow;
  2113. borrow = (y & 0x10000) >> 16;
  2114. *bx++ = y & 0xffff;
  2115. #endif
  2116. #endif
  2117. }
  2118. while(sx <= sxe);
  2119. bx = b->x;
  2120. bxe = bx + n;
  2121. if (!*bxe) {
  2122. while(--bxe > bx && !*bxe)
  2123. --n;
  2124. b->wds = n;
  2125. }
  2126. }
  2127. return q;
  2128. }
  2129. #if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
  2130. static double
  2131. sulp
  2132. #ifdef KR_headers
  2133. (x, bc) U *x; BCinfo *bc;
  2134. #else
  2135. (U *x, BCinfo *bc)
  2136. #endif
  2137. {
  2138. U u;
  2139. double rv;
  2140. int i;
  2141. rv = ulp(x);
  2142. if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
  2143. return rv; /* Is there an example where i <= 0 ? */
  2144. word0(&u) = Exp_1 + (i << Exp_shift);
  2145. word1(&u) = 0;
  2146. return rv * u.d;
  2147. }
  2148. #endif /*}*/
  2149. #ifndef NO_STRTOD_BIGCOMP
  2150. static void
  2151. bigcomp
  2152. #ifdef KR_headers
  2153. (rv, s0, bc)
  2154. U *rv; CONST char *s0; BCinfo *bc;
  2155. #else
  2156. (U *rv, const char *s0, BCinfo *bc)
  2157. #endif
  2158. {
  2159. Bigint *b, *d;
  2160. int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
  2161. dsign = bc->dsign;
  2162. nd = bc->nd;
  2163. nd0 = bc->nd0;
  2164. p5 = nd + bc->e0 - 1;
  2165. speccase = 0;
  2166. #ifndef Sudden_Underflow
  2167. if (rv->d == 0.) { /* special case: value near underflow-to-zero */
  2168. /* threshold was rounded to zero */
  2169. b = i2b(1);
  2170. p2 = Emin - P + 1;
  2171. bbits = 1;
  2172. #ifdef Avoid_Underflow
  2173. word0(rv) = (P+2) << Exp_shift;
  2174. #else
  2175. word1(rv) = 1;
  2176. #endif
  2177. i = 0;
  2178. #ifdef Honor_FLT_ROUNDS
  2179. if (bc->rounding == 1)
  2180. #endif
  2181. {
  2182. speccase = 1;
  2183. --p2;
  2184. dsign = 0;
  2185. goto have_i;
  2186. }
  2187. }
  2188. else
  2189. #endif
  2190. b = d2b(rv, &p2, &bbits);
  2191. #ifdef Avoid_Underflow
  2192. p2 -= bc->scale;
  2193. #endif
  2194. /* floor(log2(rv)) == bbits - 1 + p2 */
  2195. /* Check for denormal case. */
  2196. i = P - bbits;
  2197. if (i > (j = P - Emin - 1 + p2)) {
  2198. #ifdef Sudden_Underflow
  2199. Bfree(b);
  2200. b = i2b(1);
  2201. p2 = Emin;
  2202. i = P - 1;
  2203. #ifdef Avoid_Underflow
  2204. word0(rv) = (1 + bc->scale) << Exp_shift;
  2205. #else
  2206. word0(rv) = Exp_msk1;
  2207. #endif
  2208. word1(rv) = 0;
  2209. #else
  2210. i = j;
  2211. #endif
  2212. }
  2213. #ifdef Honor_FLT_ROUNDS
  2214. if (bc->rounding != 1) {
  2215. if (i > 0)
  2216. b = lshift(b, i);
  2217. if (dsign)
  2218. b = increment(b);
  2219. }
  2220. else
  2221. #endif
  2222. {
  2223. b = lshift(b, ++i);
  2224. b->x[0] |= 1;
  2225. }
  2226. #ifndef Sudden_Underflow
  2227. have_i:
  2228. #endif
  2229. p2 -= p5 + i;
  2230. d = i2b(1);
  2231. /* Arrange for convenient computation of quotients:
  2232. * shift left if necessary so divisor has 4 leading 0 bits.
  2233. */
  2234. if (p5 > 0)
  2235. d = pow5mult(d, p5);
  2236. else if (p5 < 0)
  2237. b = pow5mult(b, -p5);
  2238. if (p2 > 0) {
  2239. b2 = p2;
  2240. d2 = 0;
  2241. }
  2242. else {
  2243. b2 = 0;
  2244. d2 = -p2;
  2245. }
  2246. i = dshift(d, d2);
  2247. if ((b2 += i) > 0)
  2248. b = lshift(b, b2);
  2249. if ((d2 += i) > 0)
  2250. d = lshift(d, d2);
  2251. /* Now b/d = exactly half-way between the two floating-point values */
  2252. /* on either side of the input string. Compute first digit of b/d. */
  2253. if (!(dig = quorem(b,d))) {
  2254. b = multadd(b, 10, 0); /* very unlikely */
  2255. dig = quorem(b,d);
  2256. }
  2257. /* Compare b/d with s0 */
  2258. for(i = 0; i < nd0; ) {
  2259. if ((dd = s0[i++] - '0' - dig))
  2260. goto ret;
  2261. if (!b->x[0] && b->wds == 1) {
  2262. if (i < nd)
  2263. dd = 1;
  2264. goto ret;
  2265. }
  2266. b = multadd(b, 10, 0);
  2267. dig = quorem(b,d);
  2268. }
  2269. for(j = bc->dp1; i++ < nd;) {
  2270. if ((dd = s0[j++] - '0' - dig))
  2271. goto ret;
  2272. if (!b->x[0] && b->wds == 1) {
  2273. if (i < nd)
  2274. dd = 1;
  2275. goto ret;
  2276. }
  2277. b = multadd(b, 10, 0);
  2278. dig = quorem(b,d);
  2279. }
  2280. if (b->x[0] || b->wds > 1)
  2281. dd = -1;
  2282. ret:
  2283. Bfree(b);
  2284. Bfree(d);
  2285. #ifdef Honor_FLT_ROUNDS
  2286. if (bc->rounding != 1) {
  2287. if (dd < 0) {
  2288. if (bc->rounding == 0) {
  2289. if (!dsign)
  2290. goto retlow1;
  2291. }
  2292. else if (dsign)
  2293. goto rethi1;
  2294. }
  2295. else if (dd > 0) {
  2296. if (bc->rounding == 0) {
  2297. if (dsign)
  2298. goto rethi1;
  2299. goto ret1;
  2300. }
  2301. if (!dsign)
  2302. goto rethi1;
  2303. dval(rv) += 2.*sulp(rv,bc);
  2304. }
  2305. else {
  2306. bc->inexact = 0;
  2307. if (dsign)
  2308. goto rethi1;
  2309. }
  2310. }
  2311. else
  2312. #endif
  2313. if (speccase) {
  2314. if (dd <= 0)
  2315. rv->d = 0.;
  2316. }
  2317. else if (dd < 0) {
  2318. if (!dsign) /* does not happen for round-near */
  2319. retlow1:
  2320. dval(rv) -= sulp(rv,bc);
  2321. }
  2322. else if (dd > 0) {
  2323. if (dsign) {
  2324. rethi1:
  2325. dval(rv) += sulp(rv,bc);
  2326. }
  2327. }
  2328. else {
  2329. /* Exact half-way case: apply round-even rule. */
  2330. if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
  2331. i = 1 - j;
  2332. if (i <= 31) {
  2333. if (word1(rv) & (0x1 << i))
  2334. goto odd;
  2335. }
  2336. else if (word0(rv) & (0x1 << (i-32)))
  2337. goto odd;
  2338. }
  2339. else if (word1(rv) & 1) {
  2340. odd:
  2341. if (dsign)
  2342. goto rethi1;
  2343. goto retlow1;
  2344. }
  2345. }
  2346. #ifdef Honor_FLT_ROUNDS
  2347. ret1:
  2348. #endif
  2349. return;
  2350. }
  2351. #endif /* NO_STRTOD_BIGCOMP */
  2352. double
  2353. fpconv_strtod
  2354. #ifdef KR_headers
  2355. (s00, se) CONST char *s00; char **se;
  2356. #else
  2357. (const char *s00, char **se)
  2358. #endif
  2359. {
  2360. int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
  2361. int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
  2362. CONST char *s, *s0, *s1;
  2363. double aadj, aadj1;
  2364. Long L;
  2365. U aadj2, adj, rv, rv0;
  2366. ULong y, z;
  2367. BCinfo bc;
  2368. Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
  2369. #ifdef Avoid_Underflow
  2370. ULong Lsb, Lsb1;
  2371. #endif
  2372. #ifdef SET_INEXACT
  2373. int oldinexact;
  2374. #endif
  2375. #ifndef NO_STRTOD_BIGCOMP
  2376. int req_bigcomp = 0;
  2377. #endif
  2378. #ifdef Honor_FLT_ROUNDS /*{*/
  2379. #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
  2380. bc.rounding = Flt_Rounds;
  2381. #else /*}{*/
  2382. bc.rounding = 1;
  2383. switch(fegetround()) {
  2384. case FE_TOWARDZERO: bc.rounding = 0; break;
  2385. case FE_UPWARD: bc.rounding = 2; break;
  2386. case FE_DOWNWARD: bc.rounding = 3;
  2387. }
  2388. #endif /*}}*/
  2389. #endif /*}*/
  2390. #ifdef USE_LOCALE
  2391. CONST char *s2;
  2392. #endif
  2393. sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
  2394. dval(&rv) = 0.;
  2395. for(s = s00;;s++) switch(*s) {
  2396. case '-':
  2397. sign = 1;
  2398. /* no break */
  2399. case '+':
  2400. if (*++s)
  2401. goto break2;
  2402. /* no break */
  2403. case 0:
  2404. goto ret0;
  2405. case '\t':
  2406. case '\n':
  2407. case '\v':
  2408. case '\f':
  2409. case '\r':
  2410. case ' ':
  2411. continue;
  2412. default:
  2413. goto break2;
  2414. }
  2415. break2:
  2416. if (*s == '0') {
  2417. #ifndef NO_HEX_FP /*{*/
  2418. switch(s[1]) {
  2419. case 'x':
  2420. case 'X':
  2421. #ifdef Honor_FLT_ROUNDS
  2422. gethex(&s, &rv, bc.rounding, sign);
  2423. #else
  2424. gethex(&s, &rv, 1, sign);
  2425. #endif
  2426. goto ret;
  2427. }
  2428. #endif /*}*/
  2429. nz0 = 1;
  2430. while(*++s == '0') ;
  2431. if (!*s)
  2432. goto ret;
  2433. }
  2434. s0 = s;
  2435. y = z = 0;
  2436. for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  2437. if (nd < 9)
  2438. y = 10*y + c - '0';
  2439. else if (nd < 16)
  2440. z = 10*z + c - '0';
  2441. nd0 = nd;
  2442. bc.dp0 = bc.dp1 = s - s0;
  2443. for(s1 = s; s1 > s0 && *--s1 == '0'; )
  2444. ++nz1;
  2445. #ifdef USE_LOCALE
  2446. s1 = localeconv()->decimal_point;
  2447. if (c == *s1) {
  2448. c = '.';
  2449. if (*++s1) {
  2450. s2 = s;
  2451. for(;;) {
  2452. if (*++s2 != *s1) {
  2453. c = 0;
  2454. break;
  2455. }
  2456. if (!*++s1) {
  2457. s = s2;
  2458. break;
  2459. }
  2460. }
  2461. }
  2462. }
  2463. #endif
  2464. if (c == '.') {
  2465. c = *++s;
  2466. bc.dp1 = s - s0;
  2467. bc.dplen = bc.dp1 - bc.dp0;
  2468. if (!nd) {
  2469. for(; c == '0'; c = *++s)
  2470. nz++;
  2471. if (c > '0' && c <= '9') {
  2472. bc.dp0 = s0 - s;
  2473. bc.dp1 = bc.dp0 + bc.dplen;
  2474. s0 = s;
  2475. nf += nz;
  2476. nz = 0;
  2477. goto have_dig;
  2478. }
  2479. goto dig_done;
  2480. }
  2481. for(; c >= '0' && c <= '9'; c = *++s) {
  2482. have_dig:
  2483. nz++;
  2484. if (c -= '0') {
  2485. nf += nz;
  2486. for(i = 1; i < nz; i++)
  2487. if (nd++ < 9)
  2488. y *= 10;
  2489. else if (nd <= DBL_DIG + 1)
  2490. z *= 10;
  2491. if (nd++ < 9)
  2492. y = 10*y + c;
  2493. else if (nd <= DBL_DIG + 1)
  2494. z = 10*z + c;
  2495. nz = nz1 = 0;
  2496. }
  2497. }
  2498. }
  2499. dig_done:
  2500. e = 0;
  2501. if (c == 'e' || c == 'E') {
  2502. if (!nd && !nz && !nz0) {
  2503. goto ret0;
  2504. }
  2505. s00 = s;
  2506. esign = 0;
  2507. switch(c = *++s) {
  2508. case '-':
  2509. esign = 1;
  2510. case '+':
  2511. c = *++s;
  2512. }
  2513. if (c >= '0' && c <= '9') {
  2514. while(c == '0')
  2515. c = *++s;
  2516. if (c > '0' && c <= '9') {
  2517. L = c - '0';
  2518. s1 = s;
  2519. while((c = *++s) >= '0' && c <= '9')
  2520. L = 10*L + c - '0';
  2521. if (s - s1 > 8 || L > 19999)
  2522. /* Avoid confusion from exponents
  2523. * so large that e might overflow.
  2524. */
  2525. e = 19999; /* safe for 16 bit ints */
  2526. else
  2527. e = (int)L;
  2528. if (esign)
  2529. e = -e;
  2530. }
  2531. else
  2532. e = 0;
  2533. }
  2534. else
  2535. s = s00;
  2536. }
  2537. if (!nd) {
  2538. if (!nz && !nz0) {
  2539. #ifdef INFNAN_CHECK
  2540. /* Check for Nan and Infinity */
  2541. if (!bc.dplen)
  2542. switch(c) {
  2543. case 'i':
  2544. case 'I':
  2545. if (match(&s,"nf")) {
  2546. --s;
  2547. if (!match(&s,"inity"))
  2548. ++s;
  2549. word0(&rv) = 0x7ff00000;
  2550. word1(&rv) = 0;
  2551. goto ret;
  2552. }
  2553. break;
  2554. case 'n':
  2555. case 'N':
  2556. if (match(&s, "an")) {
  2557. word0(&rv) = NAN_WORD0;
  2558. word1(&rv) = NAN_WORD1;
  2559. #ifndef No_Hex_NaN
  2560. if (*s == '(') /*)*/
  2561. hexnan(&rv, &s);
  2562. #endif
  2563. goto ret;
  2564. }
  2565. }
  2566. #endif /* INFNAN_CHECK */
  2567. ret0:
  2568. s = s00;
  2569. sign = 0;
  2570. }
  2571. goto ret;
  2572. }
  2573. bc.e0 = e1 = e -= nf;
  2574. /* Now we have nd0 digits, starting at s0, followed by a
  2575. * decimal point, followed by nd-nd0 digits. The number we're
  2576. * after is the integer represented by those digits times
  2577. * 10**e */
  2578. if (!nd0)
  2579. nd0 = nd;
  2580. k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  2581. dval(&rv) = y;
  2582. if (k > 9) {
  2583. #ifdef SET_INEXACT
  2584. if (k > DBL_DIG)
  2585. oldinexact = get_inexact();
  2586. #endif
  2587. dval(&rv) = tens[k - 9] * dval(&rv) + z;
  2588. }
  2589. bd0 = 0;
  2590. if (nd <= DBL_DIG
  2591. #ifndef RND_PRODQUOT
  2592. #ifndef Honor_FLT_ROUNDS
  2593. && Flt_Rounds == 1
  2594. #endif
  2595. #endif
  2596. ) {
  2597. if (!e)
  2598. goto ret;
  2599. #ifndef ROUND_BIASED_without_Round_Up
  2600. if (e > 0) {
  2601. if (e <= Ten_pmax) {
  2602. #ifdef VAX
  2603. goto vax_ovfl_check;
  2604. #else
  2605. #ifdef Honor_FLT_ROUNDS
  2606. /* round correctly FLT_ROUNDS = 2 or 3 */
  2607. if (sign) {
  2608. rv.d = -rv.d;
  2609. sign = 0;
  2610. }
  2611. #endif
  2612. /* rv = */ rounded_product(dval(&rv), tens[e]);
  2613. goto ret;
  2614. #endif
  2615. }
  2616. i = DBL_DIG - nd;
  2617. if (e <= Ten_pmax + i) {
  2618. /* A fancier test would sometimes let us do
  2619. * this for larger i values.
  2620. */
  2621. #ifdef Honor_FLT_ROUNDS
  2622. /* round correctly FLT_ROUNDS = 2 or 3 */
  2623. if (sign) {
  2624. rv.d = -rv.d;
  2625. sign = 0;
  2626. }
  2627. #endif
  2628. e -= i;
  2629. dval(&rv) *= tens[i];
  2630. #ifdef VAX
  2631. /* VAX exponent range is so narrow we must
  2632. * worry about overflow here...
  2633. */
  2634. vax_ovfl_check:
  2635. word0(&rv) -= P*Exp_msk1;
  2636. /* rv = */ rounded_product(dval(&rv), tens[e]);
  2637. if ((word0(&rv) & Exp_mask)
  2638. > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  2639. goto ovfl;
  2640. word0(&rv) += P*Exp_msk1;
  2641. #else
  2642. /* rv = */ rounded_product(dval(&rv), tens[e]);
  2643. #endif
  2644. goto ret;
  2645. }
  2646. }
  2647. #ifndef Inaccurate_Divide
  2648. else if (e >= -Ten_pmax) {
  2649. #ifdef Honor_FLT_ROUNDS
  2650. /* round correctly FLT_ROUNDS = 2 or 3 */
  2651. if (sign) {
  2652. rv.d = -rv.d;
  2653. sign = 0;
  2654. }
  2655. #endif
  2656. /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
  2657. goto ret;
  2658. }
  2659. #endif
  2660. #endif /* ROUND_BIASED_without_Round_Up */
  2661. }
  2662. e1 += nd - k;
  2663. #ifdef IEEE_Arith
  2664. #ifdef SET_INEXACT
  2665. bc.inexact = 1;
  2666. if (k <= DBL_DIG)
  2667. oldinexact = get_inexact();
  2668. #endif
  2669. #ifdef Avoid_Underflow
  2670. bc.scale = 0;
  2671. #endif
  2672. #ifdef Honor_FLT_ROUNDS
  2673. if (bc.rounding >= 2) {
  2674. if (sign)
  2675. bc.rounding = bc.rounding == 2 ? 0 : 2;
  2676. else
  2677. if (bc.rounding != 2)
  2678. bc.rounding = 0;
  2679. }
  2680. #endif
  2681. #endif /*IEEE_Arith*/
  2682. /* Get starting approximation = rv * 10**e1 */
  2683. if (e1 > 0) {
  2684. if ((i = e1 & 15))
  2685. dval(&rv) *= tens[i];
  2686. if (e1 &= ~15) {
  2687. if (e1 > DBL_MAX_10_EXP) {
  2688. ovfl:
  2689. /* Can't trust HUGE_VAL */
  2690. #ifdef IEEE_Arith
  2691. #ifdef Honor_FLT_ROUNDS
  2692. switch(bc.rounding) {
  2693. case 0: /* toward 0 */
  2694. case 3: /* toward -infinity */
  2695. word0(&rv) = Big0;
  2696. word1(&rv) = Big1;
  2697. break;
  2698. default:
  2699. word0(&rv) = Exp_mask;
  2700. word1(&rv) = 0;
  2701. }
  2702. #else /*Honor_FLT_ROUNDS*/
  2703. word0(&rv) = Exp_mask;
  2704. word1(&rv) = 0;
  2705. #endif /*Honor_FLT_ROUNDS*/
  2706. #ifdef SET_INEXACT
  2707. /* set overflow bit */
  2708. dval(&rv0) = 1e300;
  2709. dval(&rv0) *= dval(&rv0);
  2710. #endif
  2711. #else /*IEEE_Arith*/
  2712. word0(&rv) = Big0;
  2713. word1(&rv) = Big1;
  2714. #endif /*IEEE_Arith*/
  2715. range_err:
  2716. if (bd0) {
  2717. Bfree(bb);
  2718. Bfree(bd);
  2719. Bfree(bs);
  2720. Bfree(bd0);
  2721. Bfree(delta);
  2722. }
  2723. #ifndef NO_ERRNO
  2724. errno = ERANGE;
  2725. #endif
  2726. goto ret;
  2727. }
  2728. e1 >>= 4;
  2729. for(j = 0; e1 > 1; j++, e1 >>= 1)
  2730. if (e1 & 1)
  2731. dval(&rv) *= bigtens[j];
  2732. /* The last multiplication could overflow. */
  2733. word0(&rv) -= P*Exp_msk1;
  2734. dval(&rv) *= bigtens[j];
  2735. if ((z = word0(&rv) & Exp_mask)
  2736. > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  2737. goto ovfl;
  2738. if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  2739. /* set to largest number */
  2740. /* (Can't trust DBL_MAX) */
  2741. word0(&rv) = Big0;
  2742. word1(&rv) = Big1;
  2743. }
  2744. else
  2745. word0(&rv) += P*Exp_msk1;
  2746. }
  2747. }
  2748. else if (e1 < 0) {
  2749. e1 = -e1;
  2750. if ((i = e1 & 15))
  2751. dval(&rv) /= tens[i];
  2752. if (e1 >>= 4) {
  2753. if (e1 >= 1 << n_bigtens)
  2754. goto undfl;
  2755. #ifdef Avoid_Underflow
  2756. if (e1 & Scale_Bit)
  2757. bc.scale = 2*P;
  2758. for(j = 0; e1 > 0; j++, e1 >>= 1)
  2759. if (e1 & 1)
  2760. dval(&rv) *= tinytens[j];
  2761. if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
  2762. >> Exp_shift)) > 0) {
  2763. /* scaled rv is denormal; clear j low bits */
  2764. if (j >= 32) {
  2765. if (j > 54)
  2766. goto undfl;
  2767. word1(&rv) = 0;
  2768. if (j >= 53)
  2769. word0(&rv) = (P+2)*Exp_msk1;
  2770. else
  2771. word0(&rv) &= 0xffffffff << (j-32);
  2772. }
  2773. else
  2774. word1(&rv) &= 0xffffffff << j;
  2775. }
  2776. #else
  2777. for(j = 0; e1 > 1; j++, e1 >>= 1)
  2778. if (e1 & 1)
  2779. dval(&rv) *= tinytens[j];
  2780. /* The last multiplication could underflow. */
  2781. dval(&rv0) = dval(&rv);
  2782. dval(&rv) *= tinytens[j];
  2783. if (!dval(&rv)) {
  2784. dval(&rv) = 2.*dval(&rv0);
  2785. dval(&rv) *= tinytens[j];
  2786. #endif
  2787. if (!dval(&rv)) {
  2788. undfl:
  2789. dval(&rv) = 0.;
  2790. goto range_err;
  2791. }
  2792. #ifndef Avoid_Underflow
  2793. word0(&rv) = Tiny0;
  2794. word1(&rv) = Tiny1;
  2795. /* The refinement below will clean
  2796. * this approximation up.
  2797. */
  2798. }
  2799. #endif
  2800. }
  2801. }
  2802. /* Now the hard part -- adjusting rv to the correct value.*/
  2803. /* Put digits into bd: true value = bd * 10^e */
  2804. bc.nd = nd - nz1;
  2805. #ifndef NO_STRTOD_BIGCOMP
  2806. bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */
  2807. /* to silence an erroneous warning about bc.nd0 */
  2808. /* possibly not being initialized. */
  2809. if (nd > strtod_diglim) {
  2810. /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
  2811. /* minimum number of decimal digits to distinguish double values */
  2812. /* in IEEE arithmetic. */
  2813. i = j = 18;
  2814. if (i > nd0)
  2815. j += bc.dplen;
  2816. for(;;) {
  2817. if (--j < bc.dp1 && j >= bc.dp0)
  2818. j = bc.dp0 - 1;
  2819. if (s0[j] != '0')
  2820. break;
  2821. --i;
  2822. }
  2823. e += nd - i;
  2824. nd = i;
  2825. if (nd0 > nd)
  2826. nd0 = nd;
  2827. if (nd < 9) { /* must recompute y */
  2828. y = 0;
  2829. for(i = 0; i < nd0; ++i)
  2830. y = 10*y + s0[i] - '0';
  2831. for(j = bc.dp1; i < nd; ++i)
  2832. y = 10*y + s0[j++] - '0';
  2833. }
  2834. }
  2835. #endif
  2836. bd0 = s2b(s0, nd0, nd, y, bc.dplen);
  2837. for(;;) {
  2838. bd = Balloc(bd0->k);
  2839. Bcopy(bd, bd0);
  2840. bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
  2841. bs = i2b(1);
  2842. if (e >= 0) {
  2843. bb2 = bb5 = 0;
  2844. bd2 = bd5 = e;
  2845. }
  2846. else {
  2847. bb2 = bb5 = -e;
  2848. bd2 = bd5 = 0;
  2849. }
  2850. if (bbe >= 0)
  2851. bb2 += bbe;
  2852. else
  2853. bd2 -= bbe;
  2854. bs2 = bb2;
  2855. #ifdef Honor_FLT_ROUNDS
  2856. if (bc.rounding != 1)
  2857. bs2++;
  2858. #endif
  2859. #ifdef Avoid_Underflow
  2860. Lsb = LSB;
  2861. Lsb1 = 0;
  2862. j = bbe - bc.scale;
  2863. i = j + bbbits - 1; /* logb(rv) */
  2864. j = P + 1 - bbbits;
  2865. if (i < Emin) { /* denormal */
  2866. i = Emin - i;
  2867. j -= i;
  2868. if (i < 32)
  2869. Lsb <<= i;
  2870. else if (i < 52)
  2871. Lsb1 = Lsb << (i-32);
  2872. else
  2873. Lsb1 = Exp_mask;
  2874. }
  2875. #else /*Avoid_Underflow*/
  2876. #ifdef Sudden_Underflow
  2877. #ifdef IBM
  2878. j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  2879. #else
  2880. j = P + 1 - bbbits;
  2881. #endif
  2882. #else /*Sudden_Underflow*/
  2883. j = bbe;
  2884. i = j + bbbits - 1; /* logb(rv) */
  2885. if (i < Emin) /* denormal */
  2886. j += P - Emin;
  2887. else
  2888. j = P + 1 - bbbits;
  2889. #endif /*Sudden_Underflow*/
  2890. #endif /*Avoid_Underflow*/
  2891. bb2 += j;
  2892. bd2 += j;
  2893. #ifdef Avoid_Underflow
  2894. bd2 += bc.scale;
  2895. #endif
  2896. i = bb2 < bd2 ? bb2 : bd2;
  2897. if (i > bs2)
  2898. i = bs2;
  2899. if (i > 0) {
  2900. bb2 -= i;
  2901. bd2 -= i;
  2902. bs2 -= i;
  2903. }
  2904. if (bb5 > 0) {
  2905. bs = pow5mult(bs, bb5);
  2906. bb1 = mult(bs, bb);
  2907. Bfree(bb);
  2908. bb = bb1;
  2909. }
  2910. if (bb2 > 0)
  2911. bb = lshift(bb, bb2);
  2912. if (bd5 > 0)
  2913. bd = pow5mult(bd, bd5);
  2914. if (bd2 > 0)
  2915. bd = lshift(bd, bd2);
  2916. if (bs2 > 0)
  2917. bs = lshift(bs, bs2);
  2918. delta = diff(bb, bd);
  2919. bc.dsign = delta->sign;
  2920. delta->sign = 0;
  2921. i = cmp(delta, bs);
  2922. #ifndef NO_STRTOD_BIGCOMP /*{*/
  2923. if (bc.nd > nd && i <= 0) {
  2924. if (bc.dsign) {
  2925. /* Must use bigcomp(). */
  2926. req_bigcomp = 1;
  2927. break;
  2928. }
  2929. #ifdef Honor_FLT_ROUNDS
  2930. if (bc.rounding != 1) {
  2931. if (i < 0) {
  2932. req_bigcomp = 1;
  2933. break;
  2934. }
  2935. }
  2936. else
  2937. #endif
  2938. i = -1; /* Discarded digits make delta smaller. */
  2939. }
  2940. #endif /*}*/
  2941. #ifdef Honor_FLT_ROUNDS /*{*/
  2942. if (bc.rounding != 1) {
  2943. if (i < 0) {
  2944. /* Error is less than an ulp */
  2945. if (!delta->x[0] && delta->wds <= 1) {
  2946. /* exact */
  2947. #ifdef SET_INEXACT
  2948. bc.inexact = 0;
  2949. #endif
  2950. break;
  2951. }
  2952. if (bc.rounding) {
  2953. if (bc.dsign) {
  2954. adj.d = 1.;
  2955. goto apply_adj;
  2956. }
  2957. }
  2958. else if (!bc.dsign) {
  2959. adj.d = -1.;
  2960. if (!word1(&rv)
  2961. && !(word0(&rv) & Frac_mask)) {
  2962. y = word0(&rv) & Exp_mask;
  2963. #ifdef Avoid_Underflow
  2964. if (!bc.scale || y > 2*P*Exp_msk1)
  2965. #else
  2966. if (y)
  2967. #endif
  2968. {
  2969. delta = lshift(delta,Log2P);
  2970. if (cmp(delta, bs) <= 0)
  2971. adj.d = -0.5;
  2972. }
  2973. }
  2974. apply_adj:
  2975. #ifdef Avoid_Underflow /*{*/
  2976. if (bc.scale && (y = word0(&rv) & Exp_mask)
  2977. <= 2*P*Exp_msk1)
  2978. word0(&adj) += (2*P+1)*Exp_msk1 - y;
  2979. #else
  2980. #ifdef Sudden_Underflow
  2981. if ((word0(&rv) & Exp_mask) <=
  2982. P*Exp_msk1) {
  2983. word0(&rv) += P*Exp_msk1;
  2984. dval(&rv) += adj.d*ulp(dval(&rv));
  2985. word0(&rv) -= P*Exp_msk1;
  2986. }
  2987. else
  2988. #endif /*Sudden_Underflow*/
  2989. #endif /*Avoid_Underflow}*/
  2990. dval(&rv) += adj.d*ulp(&rv);
  2991. }
  2992. break;
  2993. }
  2994. adj.d = ratio(delta, bs);
  2995. if (adj.d < 1.)
  2996. adj.d = 1.;
  2997. if (adj.d <= 0x7ffffffe) {
  2998. /* adj = rounding ? ceil(adj) : floor(adj); */
  2999. y = adj.d;
  3000. if (y != adj.d) {
  3001. if (!((bc.rounding>>1) ^ bc.dsign))
  3002. y++;
  3003. adj.d = y;
  3004. }
  3005. }
  3006. #ifdef Avoid_Underflow /*{*/
  3007. if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
  3008. word0(&adj) += (2*P+1)*Exp_msk1 - y;
  3009. #else
  3010. #ifdef Sudden_Underflow
  3011. if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
  3012. word0(&rv) += P*Exp_msk1;
  3013. adj.d *= ulp(dval(&rv));
  3014. if (bc.dsign)
  3015. dval(&rv) += adj.d;
  3016. else
  3017. dval(&rv) -= adj.d;
  3018. word0(&rv) -= P*Exp_msk1;
  3019. goto cont;
  3020. }
  3021. #endif /*Sudden_Underflow*/
  3022. #endif /*Avoid_Underflow}*/
  3023. adj.d *= ulp(&rv);
  3024. if (bc.dsign) {
  3025. if (word0(&rv) == Big0 && word1(&rv) == Big1)
  3026. goto ovfl;
  3027. dval(&rv) += adj.d;
  3028. }
  3029. else
  3030. dval(&rv) -= adj.d;
  3031. goto cont;
  3032. }
  3033. #endif /*}Honor_FLT_ROUNDS*/
  3034. if (i < 0) {
  3035. /* Error is less than half an ulp -- check for
  3036. * special case of mantissa a power of two.
  3037. */
  3038. if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
  3039. #ifdef IEEE_Arith /*{*/
  3040. #ifdef Avoid_Underflow
  3041. || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
  3042. #else
  3043. || (word0(&rv) & Exp_mask) <= Exp_msk1
  3044. #endif
  3045. #endif /*}*/
  3046. ) {
  3047. #ifdef SET_INEXACT
  3048. if (!delta->x[0] && delta->wds <= 1)
  3049. bc.inexact = 0;
  3050. #endif
  3051. break;
  3052. }
  3053. if (!delta->x[0] && delta->wds <= 1) {
  3054. /* exact result */
  3055. #ifdef SET_INEXACT
  3056. bc.inexact = 0;
  3057. #endif
  3058. break;
  3059. }
  3060. delta = lshift(delta,Log2P);
  3061. if (cmp(delta, bs) > 0)
  3062. goto drop_down;
  3063. break;
  3064. }
  3065. if (i == 0) {
  3066. /* exactly half-way between */
  3067. if (bc.dsign) {
  3068. if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
  3069. && word1(&rv) == (
  3070. #ifdef Avoid_Underflow
  3071. (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
  3072. ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
  3073. #endif
  3074. 0xffffffff)) {
  3075. /*boundary case -- increment exponent*/
  3076. if (word0(&rv) == Big0 && word1(&rv) == Big1)
  3077. goto ovfl;
  3078. word0(&rv) = (word0(&rv) & Exp_mask)
  3079. + Exp_msk1
  3080. #ifdef IBM
  3081. | Exp_msk1 >> 4
  3082. #endif
  3083. ;
  3084. word1(&rv) = 0;
  3085. #ifdef Avoid_Underflow
  3086. bc.dsign = 0;
  3087. #endif
  3088. break;
  3089. }
  3090. }
  3091. else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
  3092. drop_down:
  3093. /* boundary case -- decrement exponent */
  3094. #ifdef Sudden_Underflow /*{{*/
  3095. L = word0(&rv) & Exp_mask;
  3096. #ifdef IBM
  3097. if (L < Exp_msk1)
  3098. #else
  3099. #ifdef Avoid_Underflow
  3100. if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
  3101. #else
  3102. if (L <= Exp_msk1)
  3103. #endif /*Avoid_Underflow*/
  3104. #endif /*IBM*/
  3105. {
  3106. if (bc.nd >nd) {
  3107. bc.uflchk = 1;
  3108. break;
  3109. }
  3110. goto undfl;
  3111. }
  3112. L -= Exp_msk1;
  3113. #else /*Sudden_Underflow}{*/
  3114. #ifdef Avoid_Underflow
  3115. if (bc.scale) {
  3116. L = word0(&rv) & Exp_mask;
  3117. if (L <= (2*P+1)*Exp_msk1) {
  3118. if (L > (P+2)*Exp_msk1)
  3119. /* round even ==> */
  3120. /* accept rv */
  3121. break;
  3122. /* rv = smallest denormal */
  3123. if (bc.nd >nd) {
  3124. bc.uflchk = 1;
  3125. break;
  3126. }
  3127. goto undfl;
  3128. }
  3129. }
  3130. #endif /*Avoid_Underflow*/
  3131. L = (word0(&rv) & Exp_mask) - Exp_msk1;
  3132. #endif /*Sudden_Underflow}}*/
  3133. word0(&rv) = L | Bndry_mask1;
  3134. word1(&rv) = 0xffffffff;
  3135. #ifdef IBM
  3136. goto cont;
  3137. #else
  3138. #ifndef NO_STRTOD_BIGCOMP
  3139. if (bc.nd > nd)
  3140. goto cont;
  3141. #endif
  3142. break;
  3143. #endif
  3144. }
  3145. #ifndef ROUND_BIASED
  3146. #ifdef Avoid_Underflow
  3147. if (Lsb1) {
  3148. if (!(word0(&rv) & Lsb1))
  3149. break;
  3150. }
  3151. else if (!(word1(&rv) & Lsb))
  3152. break;
  3153. #else
  3154. if (!(word1(&rv) & LSB))
  3155. break;
  3156. #endif
  3157. #endif
  3158. if (bc.dsign)
  3159. #ifdef Avoid_Underflow
  3160. dval(&rv) += sulp(&rv, &bc);
  3161. #else
  3162. dval(&rv) += ulp(&rv);
  3163. #endif
  3164. #ifndef ROUND_BIASED
  3165. else {
  3166. #ifdef Avoid_Underflow
  3167. dval(&rv) -= sulp(&rv, &bc);
  3168. #else
  3169. dval(&rv) -= ulp(&rv);
  3170. #endif
  3171. #ifndef Sudden_Underflow
  3172. if (!dval(&rv)) {
  3173. if (bc.nd >nd) {
  3174. bc.uflchk = 1;
  3175. break;
  3176. }
  3177. goto undfl;
  3178. }
  3179. #endif
  3180. }
  3181. #ifdef Avoid_Underflow
  3182. bc.dsign = 1 - bc.dsign;
  3183. #endif
  3184. #endif
  3185. break;
  3186. }
  3187. if ((aadj = ratio(delta, bs)) <= 2.) {
  3188. if (bc.dsign)
  3189. aadj = aadj1 = 1.;
  3190. else if (word1(&rv) || word0(&rv) & Bndry_mask) {
  3191. #ifndef Sudden_Underflow
  3192. if (word1(&rv) == Tiny1 && !word0(&rv)) {
  3193. if (bc.nd >nd) {
  3194. bc.uflchk = 1;
  3195. break;
  3196. }
  3197. goto undfl;
  3198. }
  3199. #endif
  3200. aadj = 1.;
  3201. aadj1 = -1.;
  3202. }
  3203. else {
  3204. /* special case -- power of FLT_RADIX to be */
  3205. /* rounded down... */
  3206. if (aadj < 2./FLT_RADIX)
  3207. aadj = 1./FLT_RADIX;
  3208. else
  3209. aadj *= 0.5;
  3210. aadj1 = -aadj;
  3211. }
  3212. }
  3213. else {
  3214. aadj *= 0.5;
  3215. aadj1 = bc.dsign ? aadj : -aadj;
  3216. #ifdef Check_FLT_ROUNDS
  3217. switch(bc.rounding) {
  3218. case 2: /* towards +infinity */
  3219. aadj1 -= 0.5;
  3220. break;
  3221. case 0: /* towards 0 */
  3222. case 3: /* towards -infinity */
  3223. aadj1 += 0.5;
  3224. }
  3225. #else
  3226. if (Flt_Rounds == 0)
  3227. aadj1 += 0.5;
  3228. #endif /*Check_FLT_ROUNDS*/
  3229. }
  3230. y = word0(&rv) & Exp_mask;
  3231. /* Check for overflow */
  3232. if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  3233. dval(&rv0) = dval(&rv);
  3234. word0(&rv) -= P*Exp_msk1;
  3235. adj.d = aadj1 * ulp(&rv);
  3236. dval(&rv) += adj.d;
  3237. if ((word0(&rv) & Exp_mask) >=
  3238. Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  3239. if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
  3240. goto ovfl;
  3241. word0(&rv) = Big0;
  3242. word1(&rv) = Big1;
  3243. goto cont;
  3244. }
  3245. else
  3246. word0(&rv) += P*Exp_msk1;
  3247. }
  3248. else {
  3249. #ifdef Avoid_Underflow
  3250. if (bc.scale && y <= 2*P*Exp_msk1) {
  3251. if (aadj <= 0x7fffffff) {
  3252. if ((z = aadj) <= 0)
  3253. z = 1;
  3254. aadj = z;
  3255. aadj1 = bc.dsign ? aadj : -aadj;
  3256. }
  3257. dval(&aadj2) = aadj1;
  3258. word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
  3259. aadj1 = dval(&aadj2);
  3260. adj.d = aadj1 * ulp(&rv);
  3261. dval(&rv) += adj.d;
  3262. if (rv.d == 0.)
  3263. #ifdef NO_STRTOD_BIGCOMP
  3264. goto undfl;
  3265. #else
  3266. {
  3267. if (bc.nd > nd)
  3268. bc.dsign = 1;
  3269. break;
  3270. }
  3271. #endif
  3272. }
  3273. else {
  3274. adj.d = aadj1 * ulp(&rv);
  3275. dval(&rv) += adj.d;
  3276. }
  3277. #else
  3278. #ifdef Sudden_Underflow
  3279. if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
  3280. dval(&rv0) = dval(&rv);
  3281. word0(&rv) += P*Exp_msk1;
  3282. adj.d = aadj1 * ulp(&rv);
  3283. dval(&rv) += adj.d;
  3284. #ifdef IBM
  3285. if ((word0(&rv) & Exp_mask) < P*Exp_msk1)
  3286. #else
  3287. if ((word0(&rv) & Exp_mask) <= P*Exp_msk1)
  3288. #endif
  3289. {
  3290. if (word0(&rv0) == Tiny0
  3291. && word1(&rv0) == Tiny1) {
  3292. if (bc.nd >nd) {
  3293. bc.uflchk = 1;
  3294. break;
  3295. }
  3296. goto undfl;
  3297. }
  3298. word0(&rv) = Tiny0;
  3299. word1(&rv) = Tiny1;
  3300. goto cont;
  3301. }
  3302. else
  3303. word0(&rv) -= P*Exp_msk1;
  3304. }
  3305. else {
  3306. adj.d = aadj1 * ulp(&rv);
  3307. dval(&rv) += adj.d;
  3308. }
  3309. #else /*Sudden_Underflow*/
  3310. /* Compute adj so that the IEEE rounding rules will
  3311. * correctly round rv + adj in some half-way cases.
  3312. * If rv * ulp(rv) is denormalized (i.e.,
  3313. * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  3314. * trouble from bits lost to denormalization;
  3315. * example: 1.2e-307 .
  3316. */
  3317. if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
  3318. aadj1 = (double)(int)(aadj + 0.5);
  3319. if (!bc.dsign)
  3320. aadj1 = -aadj1;
  3321. }
  3322. adj.d = aadj1 * ulp(&rv);
  3323. dval(&rv) += adj.d;
  3324. #endif /*Sudden_Underflow*/
  3325. #endif /*Avoid_Underflow*/
  3326. }
  3327. z = word0(&rv) & Exp_mask;
  3328. #ifndef SET_INEXACT
  3329. if (bc.nd == nd) {
  3330. #ifdef Avoid_Underflow
  3331. if (!bc.scale)
  3332. #endif
  3333. if (y == z) {
  3334. /* Can we stop now? */
  3335. L = (Long)aadj;
  3336. aadj -= L;
  3337. /* The tolerances below are conservative. */
  3338. if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
  3339. if (aadj < .4999999 || aadj > .5000001)
  3340. break;
  3341. }
  3342. else if (aadj < .4999999/FLT_RADIX)
  3343. break;
  3344. }
  3345. }
  3346. #endif
  3347. cont:
  3348. Bfree(bb);
  3349. Bfree(bd);
  3350. Bfree(bs);
  3351. Bfree(delta);
  3352. }
  3353. Bfree(bb);
  3354. Bfree(bd);
  3355. Bfree(bs);
  3356. Bfree(bd0);
  3357. Bfree(delta);
  3358. #ifndef NO_STRTOD_BIGCOMP
  3359. if (req_bigcomp) {
  3360. bd0 = 0;
  3361. bc.e0 += nz1;
  3362. bigcomp(&rv, s0, &bc);
  3363. y = word0(&rv) & Exp_mask;
  3364. if (y == Exp_mask)
  3365. goto ovfl;
  3366. if (y == 0 && rv.d == 0.)
  3367. goto undfl;
  3368. }
  3369. #endif
  3370. #ifdef SET_INEXACT
  3371. if (bc.inexact) {
  3372. if (!oldinexact) {
  3373. word0(&rv0) = Exp_1 + (70 << Exp_shift);
  3374. word1(&rv0) = 0;
  3375. dval(&rv0) += 1.;
  3376. }
  3377. }
  3378. else if (!oldinexact)
  3379. clear_inexact();
  3380. #endif
  3381. #ifdef Avoid_Underflow
  3382. if (bc.scale) {
  3383. word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
  3384. word1(&rv0) = 0;
  3385. dval(&rv) *= dval(&rv0);
  3386. #ifndef NO_ERRNO
  3387. /* try to avoid the bug of testing an 8087 register value */
  3388. #ifdef IEEE_Arith
  3389. if (!(word0(&rv) & Exp_mask))
  3390. #else
  3391. if (word0(&rv) == 0 && word1(&rv) == 0)
  3392. #endif
  3393. errno = ERANGE;
  3394. #endif
  3395. }
  3396. #endif /* Avoid_Underflow */
  3397. #ifdef SET_INEXACT
  3398. if (bc.inexact && !(word0(&rv) & Exp_mask)) {
  3399. /* set underflow bit */
  3400. dval(&rv0) = 1e-300;
  3401. dval(&rv0) *= dval(&rv0);
  3402. }
  3403. #endif
  3404. ret:
  3405. if (se)
  3406. *se = (char *)s;
  3407. return sign ? -dval(&rv) : dval(&rv);
  3408. }
  3409. #ifndef MULTIPLE_THREADS
  3410. static char *dtoa_result;
  3411. #endif
  3412. static char *
  3413. #ifdef KR_headers
  3414. rv_alloc(i) int i;
  3415. #else
  3416. rv_alloc(int i)
  3417. #endif
  3418. {
  3419. int j, k, *r;
  3420. j = sizeof(ULong);
  3421. for(k = 0;
  3422. sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
  3423. j <<= 1)
  3424. k++;
  3425. r = (int*)Balloc(k);
  3426. *r = k;
  3427. return
  3428. #ifndef MULTIPLE_THREADS
  3429. dtoa_result =
  3430. #endif
  3431. (char *)(r+1);
  3432. }
  3433. static char *
  3434. #ifdef KR_headers
  3435. nrv_alloc(s, rve, n) char *s, **rve; int n;
  3436. #else
  3437. nrv_alloc(const char *s, char **rve, int n)
  3438. #endif
  3439. {
  3440. char *rv, *t;
  3441. t = rv = rv_alloc(n);
  3442. while((*t = *s++)) t++;
  3443. if (rve)
  3444. *rve = t;
  3445. return rv;
  3446. }
  3447. /* freedtoa(s) must be used to free values s returned by dtoa
  3448. * when MULTIPLE_THREADS is #defined. It should be used in all cases,
  3449. * but for consistency with earlier versions of dtoa, it is optional
  3450. * when MULTIPLE_THREADS is not defined.
  3451. */
  3452. void
  3453. #ifdef KR_headers
  3454. freedtoa(s) char *s;
  3455. #else
  3456. freedtoa(char *s)
  3457. #endif
  3458. {
  3459. Bigint *b = (Bigint *)((int *)s - 1);
  3460. b->maxwds = 1 << (b->k = *(int*)b);
  3461. Bfree(b);
  3462. #ifndef MULTIPLE_THREADS
  3463. if (s == dtoa_result)
  3464. dtoa_result = 0;
  3465. #endif
  3466. }
  3467. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  3468. *
  3469. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  3470. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
  3471. *
  3472. * Modifications:
  3473. * 1. Rather than iterating, we use a simple numeric overestimate
  3474. * to determine k = floor(log10(d)). We scale relevant
  3475. * quantities using O(log2(k)) rather than O(k) multiplications.
  3476. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  3477. * try to generate digits strictly left to right. Instead, we
  3478. * compute with fewer bits and propagate the carry if necessary
  3479. * when rounding the final digit up. This is often faster.
  3480. * 3. Under the assumption that input will be rounded nearest,
  3481. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  3482. * That is, we allow equality in stopping tests when the
  3483. * round-nearest rule will give the same floating-point value
  3484. * as would satisfaction of the stopping test with strict
  3485. * inequality.
  3486. * 4. We remove common factors of powers of 2 from relevant
  3487. * quantities.
  3488. * 5. When converting floating-point integers less than 1e16,
  3489. * we use floating-point arithmetic rather than resorting
  3490. * to multiple-precision integers.
  3491. * 6. When asked to produce fewer than 15 digits, we first try
  3492. * to get by with floating-point arithmetic; we resort to
  3493. * multiple-precision integer arithmetic only if we cannot
  3494. * guarantee that the floating-point calculation has given
  3495. * the correctly rounded result. For k requested digits and
  3496. * "uniformly" distributed input, the probability is
  3497. * something like 10^(k-15) that we must resort to the Long
  3498. * calculation.
  3499. */
  3500. char *
  3501. dtoa
  3502. #ifdef KR_headers
  3503. (dd, mode, ndigits, decpt, sign, rve)
  3504. double dd; int mode, ndigits, *decpt, *sign; char **rve;
  3505. #else
  3506. (double dd, int mode, int ndigits, int *decpt, int *sign, char **rve)
  3507. #endif
  3508. {
  3509. /* Arguments ndigits, decpt, sign are similar to those
  3510. of ecvt and fcvt; trailing zeros are suppressed from
  3511. the returned string. If not null, *rve is set to point
  3512. to the end of the return value. If d is +-Infinity or NaN,
  3513. then *decpt is set to 9999.
  3514. mode:
  3515. 0 ==> shortest string that yields d when read in
  3516. and rounded to nearest.
  3517. 1 ==> like 0, but with Steele & White stopping rule;
  3518. e.g. with IEEE P754 arithmetic , mode 0 gives
  3519. 1e23 whereas mode 1 gives 9.999999999999999e22.
  3520. 2 ==> max(1,ndigits) significant digits. This gives a
  3521. return value similar to that of ecvt, except
  3522. that trailing zeros are suppressed.
  3523. 3 ==> through ndigits past the decimal point. This
  3524. gives a return value similar to that from fcvt,
  3525. except that trailing zeros are suppressed, and
  3526. ndigits can be negative.
  3527. 4,5 ==> similar to 2 and 3, respectively, but (in
  3528. round-nearest mode) with the tests of mode 0 to
  3529. possibly return a shorter string that rounds to d.
  3530. With IEEE arithmetic and compilation with
  3531. -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
  3532. as modes 2 and 3 when FLT_ROUNDS != 1.
  3533. 6-9 ==> Debugging modes similar to mode - 4: don't try
  3534. fast floating-point estimate (if applicable).
  3535. Values of mode other than 0-9 are treated as mode 0.
  3536. Sufficient space is allocated to the return value
  3537. to hold the suppressed trailing zeros.
  3538. */
  3539. int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  3540. j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  3541. spec_case, try_quick;
  3542. Long L;
  3543. #ifndef Sudden_Underflow
  3544. int denorm;
  3545. ULong x;
  3546. #endif
  3547. Bigint *b, *b1, *delta, *mlo, *mhi, *S;
  3548. U d2, eps, u;
  3549. double ds;
  3550. char *s, *s0;
  3551. #ifndef No_leftright
  3552. #ifdef IEEE_Arith
  3553. U eps1;
  3554. #endif
  3555. #endif
  3556. #ifdef SET_INEXACT
  3557. int inexact, oldinexact;
  3558. #endif
  3559. #ifdef Honor_FLT_ROUNDS /*{*/
  3560. int Rounding;
  3561. #ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
  3562. Rounding = Flt_Rounds;
  3563. #else /*}{*/
  3564. Rounding = 1;
  3565. switch(fegetround()) {
  3566. case FE_TOWARDZERO: Rounding = 0; break;
  3567. case FE_UPWARD: Rounding = 2; break;
  3568. case FE_DOWNWARD: Rounding = 3;
  3569. }
  3570. #endif /*}}*/
  3571. #endif /*}*/
  3572. #ifndef MULTIPLE_THREADS
  3573. if (dtoa_result) {
  3574. freedtoa(dtoa_result);
  3575. dtoa_result = 0;
  3576. }
  3577. #endif
  3578. u.d = dd;
  3579. if (word0(&u) & Sign_bit) {
  3580. /* set sign for everything, including 0's and NaNs */
  3581. *sign = 1;
  3582. word0(&u) &= ~Sign_bit; /* clear sign bit */
  3583. }
  3584. else
  3585. *sign = 0;
  3586. #if defined(IEEE_Arith) + defined(VAX)
  3587. #ifdef IEEE_Arith
  3588. if ((word0(&u) & Exp_mask) == Exp_mask)
  3589. #else
  3590. if (word0(&u) == 0x8000)
  3591. #endif
  3592. {
  3593. /* Infinity or NaN */
  3594. *decpt = 9999;
  3595. #ifdef IEEE_Arith
  3596. if (!word1(&u) && !(word0(&u) & 0xfffff))
  3597. return nrv_alloc("Infinity", rve, 8);
  3598. #endif
  3599. return nrv_alloc("NaN", rve, 3);
  3600. }
  3601. #endif
  3602. #ifdef IBM
  3603. dval(&u) += 0; /* normalize */
  3604. #endif
  3605. if (!dval(&u)) {
  3606. *decpt = 1;
  3607. return nrv_alloc("0", rve, 1);
  3608. }
  3609. #ifdef SET_INEXACT
  3610. try_quick = oldinexact = get_inexact();
  3611. inexact = 1;
  3612. #endif
  3613. #ifdef Honor_FLT_ROUNDS
  3614. if (Rounding >= 2) {
  3615. if (*sign)
  3616. Rounding = Rounding == 2 ? 0 : 2;
  3617. else
  3618. if (Rounding != 2)
  3619. Rounding = 0;
  3620. }
  3621. #endif
  3622. b = d2b(&u, &be, &bbits);
  3623. #ifdef Sudden_Underflow
  3624. i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  3625. #else
  3626. if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
  3627. #endif
  3628. dval(&d2) = dval(&u);
  3629. word0(&d2) &= Frac_mask1;
  3630. word0(&d2) |= Exp_11;
  3631. #ifdef IBM
  3632. if (j = 11 - hi0bits(word0(&d2) & Frac_mask))
  3633. dval(&d2) /= 1 << j;
  3634. #endif
  3635. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  3636. * log10(x) = log(x) / log(10)
  3637. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  3638. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  3639. *
  3640. * This suggests computing an approximation k to log10(d) by
  3641. *
  3642. * k = (i - Bias)*0.301029995663981
  3643. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  3644. *
  3645. * We want k to be too large rather than too small.
  3646. * The error in the first-order Taylor series approximation
  3647. * is in our favor, so we just round up the constant enough
  3648. * to compensate for any error in the multiplication of
  3649. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  3650. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  3651. * adding 1e-13 to the constant term more than suffices.
  3652. * Hence we adjust the constant term to 0.1760912590558.
  3653. * (We could get a more accurate k by invoking log10,
  3654. * but this is probably not worthwhile.)
  3655. */
  3656. i -= Bias;
  3657. #ifdef IBM
  3658. i <<= 2;
  3659. i += j;
  3660. #endif
  3661. #ifndef Sudden_Underflow
  3662. denorm = 0;
  3663. }
  3664. else {
  3665. /* d is denormalized */
  3666. i = bbits + be + (Bias + (P-1) - 1);
  3667. x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
  3668. : word1(&u) << (32 - i);
  3669. dval(&d2) = x;
  3670. word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
  3671. i -= (Bias + (P-1) - 1) + 1;
  3672. denorm = 1;
  3673. }
  3674. #endif
  3675. ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  3676. k = (int)ds;
  3677. if (ds < 0. && ds != k)
  3678. k--; /* want k = floor(ds) */
  3679. k_check = 1;
  3680. if (k >= 0 && k <= Ten_pmax) {
  3681. if (dval(&u) < tens[k])
  3682. k--;
  3683. k_check = 0;
  3684. }
  3685. j = bbits - i - 1;
  3686. if (j >= 0) {
  3687. b2 = 0;
  3688. s2 = j;
  3689. }
  3690. else {
  3691. b2 = -j;
  3692. s2 = 0;
  3693. }
  3694. if (k >= 0) {
  3695. b5 = 0;
  3696. s5 = k;
  3697. s2 += k;
  3698. }
  3699. else {
  3700. b2 -= k;
  3701. b5 = -k;
  3702. s5 = 0;
  3703. }
  3704. if (mode < 0 || mode > 9)
  3705. mode = 0;
  3706. #ifndef SET_INEXACT
  3707. #ifdef Check_FLT_ROUNDS
  3708. try_quick = Rounding == 1;
  3709. #else
  3710. try_quick = 1;
  3711. #endif
  3712. #endif /*SET_INEXACT*/
  3713. if (mode > 5) {
  3714. mode -= 4;
  3715. try_quick = 0;
  3716. }
  3717. leftright = 1;
  3718. ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
  3719. /* silence erroneous "gcc -Wall" warning. */
  3720. switch(mode) {
  3721. case 0:
  3722. case 1:
  3723. i = 18;
  3724. ndigits = 0;
  3725. break;
  3726. case 2:
  3727. leftright = 0;
  3728. /* no break */
  3729. case 4:
  3730. if (ndigits <= 0)
  3731. ndigits = 1;
  3732. ilim = ilim1 = i = ndigits;
  3733. break;
  3734. case 3:
  3735. leftright = 0;
  3736. /* no break */
  3737. case 5:
  3738. i = ndigits + k + 1;
  3739. ilim = i;
  3740. ilim1 = i - 1;
  3741. if (i <= 0)
  3742. i = 1;
  3743. }
  3744. s = s0 = rv_alloc(i);
  3745. #ifdef Honor_FLT_ROUNDS
  3746. if (mode > 1 && Rounding != 1)
  3747. leftright = 0;
  3748. #endif
  3749. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  3750. /* Try to get by with floating-point arithmetic. */
  3751. i = 0;
  3752. dval(&d2) = dval(&u);
  3753. k0 = k;
  3754. ilim0 = ilim;
  3755. ieps = 2; /* conservative */
  3756. if (k > 0) {
  3757. ds = tens[k&0xf];
  3758. j = k >> 4;
  3759. if (j & Bletch) {
  3760. /* prevent overflows */
  3761. j &= Bletch - 1;
  3762. dval(&u) /= bigtens[n_bigtens-1];
  3763. ieps++;
  3764. }
  3765. for(; j; j >>= 1, i++)
  3766. if (j & 1) {
  3767. ieps++;
  3768. ds *= bigtens[i];
  3769. }
  3770. dval(&u) /= ds;
  3771. }
  3772. else if ((j1 = -k)) {
  3773. dval(&u) *= tens[j1 & 0xf];
  3774. for(j = j1 >> 4; j; j >>= 1, i++)
  3775. if (j & 1) {
  3776. ieps++;
  3777. dval(&u) *= bigtens[i];
  3778. }
  3779. }
  3780. if (k_check && dval(&u) < 1. && ilim > 0) {
  3781. if (ilim1 <= 0)
  3782. goto fast_failed;
  3783. ilim = ilim1;
  3784. k--;
  3785. dval(&u) *= 10.;
  3786. ieps++;
  3787. }
  3788. dval(&eps) = ieps*dval(&u) + 7.;
  3789. word0(&eps) -= (P-1)*Exp_msk1;
  3790. if (ilim == 0) {
  3791. S = mhi = 0;
  3792. dval(&u) -= 5.;
  3793. if (dval(&u) > dval(&eps))
  3794. goto one_digit;
  3795. if (dval(&u) < -dval(&eps))
  3796. goto no_digits;
  3797. goto fast_failed;
  3798. }
  3799. #ifndef No_leftright
  3800. if (leftright) {
  3801. /* Use Steele & White method of only
  3802. * generating digits needed.
  3803. */
  3804. dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
  3805. #ifdef IEEE_Arith
  3806. if (k0 < 0 && j1 >= 307) {
  3807. eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
  3808. word0(&eps1) -= Exp_msk1 * (Bias+P-1);
  3809. dval(&eps1) *= tens[j1 & 0xf];
  3810. for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
  3811. if (j & 1)
  3812. dval(&eps1) *= bigtens[i];
  3813. if (eps.d < eps1.d)
  3814. eps.d = eps1.d;
  3815. }
  3816. #endif
  3817. for(i = 0;;) {
  3818. L = dval(&u);
  3819. dval(&u) -= L;
  3820. *s++ = '0' + (int)L;
  3821. if (1. - dval(&u) < dval(&eps))
  3822. goto bump_up;
  3823. if (dval(&u) < dval(&eps))
  3824. goto ret1;
  3825. if (++i >= ilim)
  3826. break;
  3827. dval(&eps) *= 10.;
  3828. dval(&u) *= 10.;
  3829. }
  3830. }
  3831. else {
  3832. #endif
  3833. /* Generate ilim digits, then fix them up. */
  3834. dval(&eps) *= tens[ilim-1];
  3835. for(i = 1;; i++, dval(&u) *= 10.) {
  3836. L = (Long)(dval(&u));
  3837. if (!(dval(&u) -= L))
  3838. ilim = i;
  3839. *s++ = '0' + (int)L;
  3840. if (i == ilim) {
  3841. if (dval(&u) > 0.5 + dval(&eps))
  3842. goto bump_up;
  3843. else if (dval(&u) < 0.5 - dval(&eps)) {
  3844. while(*--s == '0');
  3845. s++;
  3846. goto ret1;
  3847. }
  3848. break;
  3849. }
  3850. }
  3851. #ifndef No_leftright
  3852. }
  3853. #endif
  3854. fast_failed:
  3855. s = s0;
  3856. dval(&u) = dval(&d2);
  3857. k = k0;
  3858. ilim = ilim0;
  3859. }
  3860. /* Do we have a "small" integer? */
  3861. if (be >= 0 && k <= Int_max) {
  3862. /* Yes. */
  3863. ds = tens[k];
  3864. if (ndigits < 0 && ilim <= 0) {
  3865. S = mhi = 0;
  3866. if (ilim < 0 || dval(&u) <= 5*ds)
  3867. goto no_digits;
  3868. goto one_digit;
  3869. }
  3870. for(i = 1;; i++, dval(&u) *= 10.) {
  3871. L = (Long)(dval(&u) / ds);
  3872. dval(&u) -= L*ds;
  3873. #ifdef Check_FLT_ROUNDS
  3874. /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  3875. if (dval(&u) < 0) {
  3876. L--;
  3877. dval(&u) += ds;
  3878. }
  3879. #endif
  3880. *s++ = '0' + (int)L;
  3881. if (!dval(&u)) {
  3882. #ifdef SET_INEXACT
  3883. inexact = 0;
  3884. #endif
  3885. break;
  3886. }
  3887. if (i == ilim) {
  3888. #ifdef Honor_FLT_ROUNDS
  3889. if (mode > 1)
  3890. switch(Rounding) {
  3891. case 0: goto ret1;
  3892. case 2: goto bump_up;
  3893. }
  3894. #endif
  3895. dval(&u) += dval(&u);
  3896. #ifdef ROUND_BIASED
  3897. if (dval(&u) >= ds)
  3898. #else
  3899. if (dval(&u) > ds || (dval(&u) == ds && L & 1))
  3900. #endif
  3901. {
  3902. bump_up:
  3903. while(*--s == '9')
  3904. if (s == s0) {
  3905. k++;
  3906. *s = '0';
  3907. break;
  3908. }
  3909. ++*s++;
  3910. }
  3911. break;
  3912. }
  3913. }
  3914. goto ret1;
  3915. }
  3916. m2 = b2;
  3917. m5 = b5;
  3918. mhi = mlo = 0;
  3919. if (leftright) {
  3920. i =
  3921. #ifndef Sudden_Underflow
  3922. denorm ? be + (Bias + (P-1) - 1 + 1) :
  3923. #endif
  3924. #ifdef IBM
  3925. 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  3926. #else
  3927. 1 + P - bbits;
  3928. #endif
  3929. b2 += i;
  3930. s2 += i;
  3931. mhi = i2b(1);
  3932. }
  3933. if (m2 > 0 && s2 > 0) {
  3934. i = m2 < s2 ? m2 : s2;
  3935. b2 -= i;
  3936. m2 -= i;
  3937. s2 -= i;
  3938. }
  3939. if (b5 > 0) {
  3940. if (leftright) {
  3941. if (m5 > 0) {
  3942. mhi = pow5mult(mhi, m5);
  3943. b1 = mult(mhi, b);
  3944. Bfree(b);
  3945. b = b1;
  3946. }
  3947. if ((j = b5 - m5))
  3948. b = pow5mult(b, j);
  3949. }
  3950. else
  3951. b = pow5mult(b, b5);
  3952. }
  3953. S = i2b(1);
  3954. if (s5 > 0)
  3955. S = pow5mult(S, s5);
  3956. /* Check for special case that d is a normalized power of 2. */
  3957. spec_case = 0;
  3958. if ((mode < 2 || leftright)
  3959. #ifdef Honor_FLT_ROUNDS
  3960. && Rounding == 1
  3961. #endif
  3962. ) {
  3963. if (!word1(&u) && !(word0(&u) & Bndry_mask)
  3964. #ifndef Sudden_Underflow
  3965. && word0(&u) & (Exp_mask & ~Exp_msk1)
  3966. #endif
  3967. ) {
  3968. /* The special case */
  3969. b2 += Log2P;
  3970. s2 += Log2P;
  3971. spec_case = 1;
  3972. }
  3973. }
  3974. /* Arrange for convenient computation of quotients:
  3975. * shift left if necessary so divisor has 4 leading 0 bits.
  3976. *
  3977. * Perhaps we should just compute leading 28 bits of S once
  3978. * and for all and pass them and a shift to quorem, so it
  3979. * can do shifts and ors to compute the numerator for q.
  3980. */
  3981. i = dshift(S, s2);
  3982. b2 += i;
  3983. m2 += i;
  3984. s2 += i;
  3985. if (b2 > 0)
  3986. b = lshift(b, b2);
  3987. if (s2 > 0)
  3988. S = lshift(S, s2);
  3989. if (k_check) {
  3990. if (cmp(b,S) < 0) {
  3991. k--;
  3992. b = multadd(b, 10, 0); /* we botched the k estimate */
  3993. if (leftright)
  3994. mhi = multadd(mhi, 10, 0);
  3995. ilim = ilim1;
  3996. }
  3997. }
  3998. if (ilim <= 0 && (mode == 3 || mode == 5)) {
  3999. if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  4000. /* no digits, fcvt style */
  4001. no_digits:
  4002. k = -1 - ndigits;
  4003. goto ret;
  4004. }
  4005. one_digit:
  4006. *s++ = '1';
  4007. k++;
  4008. goto ret;
  4009. }
  4010. if (leftright) {
  4011. if (m2 > 0)
  4012. mhi = lshift(mhi, m2);
  4013. /* Compute mlo -- check for special case
  4014. * that d is a normalized power of 2.
  4015. */
  4016. mlo = mhi;
  4017. if (spec_case) {
  4018. mhi = Balloc(mhi->k);
  4019. Bcopy(mhi, mlo);
  4020. mhi = lshift(mhi, Log2P);
  4021. }
  4022. for(i = 1;;i++) {
  4023. dig = quorem(b,S) + '0';
  4024. /* Do we yet have the shortest decimal string
  4025. * that will round to d?
  4026. */
  4027. j = cmp(b, mlo);
  4028. delta = diff(S, mhi);
  4029. j1 = delta->sign ? 1 : cmp(b, delta);
  4030. Bfree(delta);
  4031. #ifndef ROUND_BIASED
  4032. if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
  4033. #ifdef Honor_FLT_ROUNDS
  4034. && Rounding >= 1
  4035. #endif
  4036. ) {
  4037. if (dig == '9')
  4038. goto round_9_up;
  4039. if (j > 0)
  4040. dig++;
  4041. #ifdef SET_INEXACT
  4042. else if (!b->x[0] && b->wds <= 1)
  4043. inexact = 0;
  4044. #endif
  4045. *s++ = dig;
  4046. goto ret;
  4047. }
  4048. #endif
  4049. if (j < 0 || (j == 0 && mode != 1
  4050. #ifndef ROUND_BIASED
  4051. && !(word1(&u) & 1)
  4052. #endif
  4053. )) {
  4054. if (!b->x[0] && b->wds <= 1) {
  4055. #ifdef SET_INEXACT
  4056. inexact = 0;
  4057. #endif
  4058. goto accept_dig;
  4059. }
  4060. #ifdef Honor_FLT_ROUNDS
  4061. if (mode > 1)
  4062. switch(Rounding) {
  4063. case 0: goto accept_dig;
  4064. case 2: goto keep_dig;
  4065. }
  4066. #endif /*Honor_FLT_ROUNDS*/
  4067. if (j1 > 0) {
  4068. b = lshift(b, 1);
  4069. j1 = cmp(b, S);
  4070. #ifdef ROUND_BIASED
  4071. if (j1 >= 0 /*)*/
  4072. #else
  4073. if ((j1 > 0 || (j1 == 0 && dig & 1))
  4074. #endif
  4075. && dig++ == '9')
  4076. goto round_9_up;
  4077. }
  4078. accept_dig:
  4079. *s++ = dig;
  4080. goto ret;
  4081. }
  4082. if (j1 > 0) {
  4083. #ifdef Honor_FLT_ROUNDS
  4084. if (!Rounding)
  4085. goto accept_dig;
  4086. #endif
  4087. if (dig == '9') { /* possible if i == 1 */
  4088. round_9_up:
  4089. *s++ = '9';
  4090. goto roundoff;
  4091. }
  4092. *s++ = dig + 1;
  4093. goto ret;
  4094. }
  4095. #ifdef Honor_FLT_ROUNDS
  4096. keep_dig:
  4097. #endif
  4098. *s++ = dig;
  4099. if (i == ilim)
  4100. break;
  4101. b = multadd(b, 10, 0);
  4102. if (mlo == mhi)
  4103. mlo = mhi = multadd(mhi, 10, 0);
  4104. else {
  4105. mlo = multadd(mlo, 10, 0);
  4106. mhi = multadd(mhi, 10, 0);
  4107. }
  4108. }
  4109. }
  4110. else
  4111. for(i = 1;; i++) {
  4112. *s++ = dig = quorem(b,S) + '0';
  4113. if (!b->x[0] && b->wds <= 1) {
  4114. #ifdef SET_INEXACT
  4115. inexact = 0;
  4116. #endif
  4117. goto ret;
  4118. }
  4119. if (i >= ilim)
  4120. break;
  4121. b = multadd(b, 10, 0);
  4122. }
  4123. /* Round off last digit */
  4124. #ifdef Honor_FLT_ROUNDS
  4125. switch(Rounding) {
  4126. case 0: goto trimzeros;
  4127. case 2: goto roundoff;
  4128. }
  4129. #endif
  4130. b = lshift(b, 1);
  4131. j = cmp(b, S);
  4132. #ifdef ROUND_BIASED
  4133. if (j >= 0)
  4134. #else
  4135. if (j > 0 || (j == 0 && dig & 1))
  4136. #endif
  4137. {
  4138. roundoff:
  4139. while(*--s == '9')
  4140. if (s == s0) {
  4141. k++;
  4142. *s++ = '1';
  4143. goto ret;
  4144. }
  4145. ++*s++;
  4146. }
  4147. else {
  4148. #ifdef Honor_FLT_ROUNDS
  4149. trimzeros:
  4150. #endif
  4151. while(*--s == '0');
  4152. s++;
  4153. }
  4154. ret:
  4155. Bfree(S);
  4156. if (mhi) {
  4157. if (mlo && mlo != mhi)
  4158. Bfree(mlo);
  4159. Bfree(mhi);
  4160. }
  4161. ret1:
  4162. #ifdef SET_INEXACT
  4163. if (inexact) {
  4164. if (!oldinexact) {
  4165. word0(&u) = Exp_1 + (70 << Exp_shift);
  4166. word1(&u) = 0;
  4167. dval(&u) += 1.;
  4168. }
  4169. }
  4170. else if (!oldinexact)
  4171. clear_inexact();
  4172. #endif
  4173. Bfree(b);
  4174. *s = 0;
  4175. *decpt = k + 1;
  4176. if (rve)
  4177. *rve = s;
  4178. return s0;
  4179. }
  4180. #ifdef __cplusplus
  4181. }
  4182. #endif