zend_strtod.c 90 KB

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