zend_strtod.c 92 KB

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