zend_strtod.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707
  1. /****************************************************************
  2. *
  3. * The author of this software is David M. Gay.
  4. *
  5. * Copyright (c) 1991 by AT&T.
  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 AT&T 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
  20. David M. Gay
  21. AT&T Bell Laboratories, Room 2C-463
  22. 600 Mountain Avenue
  23. Murray Hill, NJ 07974-2070
  24. U.S.A.
  25. dmg@research.att.com or research!dmg
  26. */
  27. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  28. *
  29. * This strtod returns a nearest machine number to the input decimal
  30. * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
  31. * broken by the IEEE round-even rule. Otherwise ties are broken by
  32. * biased rounding (add half and chop).
  33. *
  34. * Inspired loosely by William D. Clinger's paper "How to Read Floating
  35. * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  36. *
  37. * Modifications:
  38. *
  39. * 1. We only require IEEE, IBM, or VAX double-precision
  40. * arithmetic (not IEEE double-extended).
  41. * 2. We get by with floating-point arithmetic in a case that
  42. * Clinger missed -- when we're computing d * 10^n
  43. * for a small integer d and the integer n is not too
  44. * much larger than 22 (the maximum integer k for which
  45. * we can represent 10^k exactly), we may be able to
  46. * compute (d*10^k) * 10^(e-k) with just one roundoff.
  47. * 3. Rather than a bit-at-a-time adjustment of the binary
  48. * result in the hard case, we use floating-point
  49. * arithmetic to determine the adjustment to within
  50. * one bit; only in really hard cases do we need to
  51. * compute a second residual.
  52. * 4. Because of 3., we don't need a large table of powers of 10
  53. * for ten-to-e (just some small tables, e.g. of 10^k
  54. * for 0 <= k <= 22).
  55. */
  56. /*
  57. * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
  58. * significant byte has the lowest address.
  59. * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
  60. * significant byte has the lowest address.
  61. * #define Long int on machines with 32-bit ints and 64-bit longs.
  62. * #define Sudden_Underflow for IEEE-format machines without gradual
  63. * underflow (i.e., that flush to zero on underflow).
  64. * #define IBM for IBM mainframe-style floating-point arithmetic.
  65. * #define VAX for VAX-style floating-point arithmetic.
  66. * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
  67. * #define No_leftright to omit left-right logic in fast floating-point
  68. * computation of dtoa.
  69. * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
  70. * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  71. * that use extended-precision instructions to compute rounded
  72. * products and quotients) with IBM.
  73. * #define ROUND_BIASED for IEEE-format with biased rounding.
  74. * #define Inaccurate_Divide for IEEE-format with correctly rounded
  75. * products but inaccurate quotients, e.g., for Intel i860.
  76. * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
  77. * integer arithmetic. Whether this speeds things up or slows things
  78. * down depends on the machine and the number being converted.
  79. * #define KR_headers for old-style C function headers.
  80. * #define Bad_float_h if your system lacks a float.h or if it does not
  81. * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
  82. * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
  83. * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
  84. * if memory is available and otherwise does something you deem
  85. * appropriate. If MALLOC is undefined, malloc will be invoked
  86. * directly -- and assumed always to succeed.
  87. */
  88. /* $Id$ */
  89. #include <zend_operators.h>
  90. #include <zend_strtod.h>
  91. #ifdef ZTS
  92. #include <TSRM.h>
  93. #endif
  94. #include <stddef.h>
  95. #include <stdio.h>
  96. #include <ctype.h>
  97. #include <stdarg.h>
  98. #include <string.h>
  99. #include <stdlib.h>
  100. #include <math.h>
  101. #ifdef HAVE_LOCALE_H
  102. #include <locale.h>
  103. #endif
  104. #ifdef HAVE_SYS_TYPES_H
  105. #include <sys/types.h>
  106. #endif
  107. #if defined(HAVE_INTTYPES_H)
  108. #include <inttypes.h>
  109. #elif defined(HAVE_STDINT_H)
  110. #include <stdint.h>
  111. #endif
  112. #ifndef HAVE_INT32_T
  113. # if SIZEOF_INT == 4
  114. typedef int int32_t;
  115. # elif SIZEOF_LONG == 4
  116. typedef long int int32_t;
  117. # endif
  118. #endif
  119. #ifndef HAVE_UINT32_T
  120. # if SIZEOF_INT == 4
  121. typedef unsigned int uint32_t;
  122. # elif SIZEOF_LONG == 4
  123. typedef unsigned long int uint32_t;
  124. # endif
  125. #endif
  126. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  127. # if defined(__LITTLE_ENDIAN__)
  128. # undef WORDS_BIGENDIAN
  129. # else
  130. # if defined(__BIG_ENDIAN__)
  131. # define WORDS_BIGENDIAN
  132. # endif
  133. # endif
  134. #endif
  135. #ifdef WORDS_BIGENDIAN
  136. #define IEEE_BIG_ENDIAN
  137. #else
  138. #define IEEE_LITTLE_ENDIAN
  139. #endif
  140. #if defined(__arm__) && !defined(__VFP_FP__)
  141. /*
  142. * * Although the CPU is little endian the FP has different
  143. * * byte and word endianness. The byte order is still little endian
  144. * * but the word order is big endian.
  145. * */
  146. #define IEEE_BIG_ENDIAN
  147. #undef IEEE_LITTLE_ENDIAN
  148. #endif
  149. #ifdef __vax__
  150. #define VAX
  151. #undef IEEE_LITTLE_ENDIAN
  152. #endif
  153. #if defined(_MSC_VER)
  154. #define int32_t __int32
  155. #define uint32_t unsigned __int32
  156. #define IEEE_LITTLE_ENDIAN
  157. #endif
  158. #define Long int32_t
  159. #define ULong uint32_t
  160. #ifdef __cplusplus
  161. #include "malloc.h"
  162. #include "memory.h"
  163. #else
  164. #ifndef KR_headers
  165. #include "stdlib.h"
  166. #include "string.h"
  167. #include "locale.h"
  168. #else
  169. #include "malloc.h"
  170. #include "memory.h"
  171. #endif
  172. #endif
  173. #ifdef MALLOC
  174. #ifdef KR_headers
  175. extern char *MALLOC();
  176. #else
  177. extern void *MALLOC(size_t);
  178. #endif
  179. #else
  180. #define MALLOC malloc
  181. #endif
  182. #include "ctype.h"
  183. #include "errno.h"
  184. #ifdef Bad_float_h
  185. #ifdef IEEE_BIG_ENDIAN
  186. #define IEEE_ARITHMETIC
  187. #endif
  188. #ifdef IEEE_LITTLE_ENDIAN
  189. #define IEEE_ARITHMETIC
  190. #endif
  191. #ifdef IEEE_ARITHMETIC
  192. #define DBL_DIG 15
  193. #define DBL_MAX_10_EXP 308
  194. #define DBL_MAX_EXP 1024
  195. #define FLT_RADIX 2
  196. #define FLT_ROUNDS 1
  197. #define DBL_MAX 1.7976931348623157e+308
  198. #endif
  199. #ifdef IBM
  200. #define DBL_DIG 16
  201. #define DBL_MAX_10_EXP 75
  202. #define DBL_MAX_EXP 63
  203. #define FLT_RADIX 16
  204. #define FLT_ROUNDS 0
  205. #define DBL_MAX 7.2370055773322621e+75
  206. #endif
  207. #ifdef VAX
  208. #define DBL_DIG 16
  209. #define DBL_MAX_10_EXP 38
  210. #define DBL_MAX_EXP 127
  211. #define FLT_RADIX 2
  212. #define FLT_ROUNDS 1
  213. #define DBL_MAX 1.7014118346046923e+38
  214. #endif
  215. #ifndef LONG_MAX
  216. #define LONG_MAX 2147483647
  217. #endif
  218. #else
  219. #include "float.h"
  220. #endif
  221. #ifndef __MATH_H__
  222. #include "math.h"
  223. #endif
  224. BEGIN_EXTERN_C()
  225. #ifndef CONST
  226. #ifdef KR_headers
  227. #define CONST /* blank */
  228. #else
  229. #define CONST const
  230. #endif
  231. #endif
  232. #ifdef Unsigned_Shifts
  233. #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
  234. #else
  235. #define Sign_Extend(a,b) /*no-op*/
  236. #endif
  237. #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \
  238. defined(IBM) != 1
  239. #error "Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or IBM should be defined."
  240. #endif
  241. typedef union {
  242. double d;
  243. ULong ul[2];
  244. } _double;
  245. #define value(x) ((x).d)
  246. #ifdef IEEE_LITTLE_ENDIAN
  247. #define word0(x) ((x).ul[1])
  248. #define word1(x) ((x).ul[0])
  249. #else
  250. #define word0(x) ((x).ul[0])
  251. #define word1(x) ((x).ul[1])
  252. #endif
  253. /* The following definition of Storeinc is appropriate for MIPS processors.
  254. * An alternative that might be better on some machines is
  255. * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  256. */
  257. #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
  258. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  259. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  260. #else
  261. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  262. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  263. #endif
  264. /* #define P DBL_MANT_DIG */
  265. /* Ten_pmax = floor(P*log(2)/log(5)) */
  266. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  267. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  268. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  269. #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
  270. #define Exp_shift 20
  271. #define Exp_shift1 20
  272. #define Exp_msk1 0x100000
  273. #define Exp_msk11 0x100000
  274. #define Exp_mask 0x7ff00000
  275. #define P 53
  276. #define Bias 1023
  277. #define IEEE_Arith
  278. #define Emin (-1022)
  279. #define Exp_1 0x3ff00000
  280. #define Exp_11 0x3ff00000
  281. #define Ebits 11
  282. #define Frac_mask 0xfffff
  283. #define Frac_mask1 0xfffff
  284. #define Ten_pmax 22
  285. #define Bletch 0x10
  286. #define Bndry_mask 0xfffff
  287. #define Bndry_mask1 0xfffff
  288. #define LSB 1
  289. #define Sign_bit 0x80000000
  290. #define Log2P 1
  291. #define Tiny0 0
  292. #define Tiny1 1
  293. #define Quick_max 14
  294. #define Int_max 14
  295. #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
  296. #else
  297. #undef Sudden_Underflow
  298. #define Sudden_Underflow
  299. #ifdef IBM
  300. #define Exp_shift 24
  301. #define Exp_shift1 24
  302. #define Exp_msk1 0x1000000
  303. #define Exp_msk11 0x1000000
  304. #define Exp_mask 0x7f000000
  305. #define P 14
  306. #define Bias 65
  307. #define Exp_1 0x41000000
  308. #define Exp_11 0x41000000
  309. #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
  310. #define Frac_mask 0xffffff
  311. #define Frac_mask1 0xffffff
  312. #define Bletch 4
  313. #define Ten_pmax 22
  314. #define Bndry_mask 0xefffff
  315. #define Bndry_mask1 0xffffff
  316. #define LSB 1
  317. #define Sign_bit 0x80000000
  318. #define Log2P 4
  319. #define Tiny0 0x100000
  320. #define Tiny1 0
  321. #define Quick_max 14
  322. #define Int_max 15
  323. #else /* VAX */
  324. #define Exp_shift 23
  325. #define Exp_shift1 7
  326. #define Exp_msk1 0x80
  327. #define Exp_msk11 0x800000
  328. #define Exp_mask 0x7f80
  329. #define P 56
  330. #define Bias 129
  331. #define Exp_1 0x40800000
  332. #define Exp_11 0x4080
  333. #define Ebits 8
  334. #define Frac_mask 0x7fffff
  335. #define Frac_mask1 0xffff007f
  336. #define Ten_pmax 24
  337. #define Bletch 2
  338. #define Bndry_mask 0xffff007f
  339. #define Bndry_mask1 0xffff007f
  340. #define LSB 0x10000
  341. #define Sign_bit 0x8000
  342. #define Log2P 1
  343. #define Tiny0 0x80
  344. #define Tiny1 0
  345. #define Quick_max 15
  346. #define Int_max 15
  347. #endif
  348. #endif
  349. #ifndef IEEE_Arith
  350. #define ROUND_BIASED
  351. #endif
  352. #ifdef RND_PRODQUOT
  353. #define rounded_product(a,b) a = rnd_prod(a, b)
  354. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  355. #ifdef KR_headers
  356. extern double rnd_prod(), rnd_quot();
  357. #else
  358. extern double rnd_prod(double, double), rnd_quot(double, double);
  359. #endif
  360. #else
  361. #define rounded_product(a,b) a *= b
  362. #define rounded_quotient(a,b) a /= b
  363. #endif
  364. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  365. #define Big1 0xffffffff
  366. #ifndef Just_16
  367. /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
  368. * * This makes some inner loops simpler and sometimes saves work
  369. * * during multiplications, but it often seems to make things slightly
  370. * * slower. Hence the default is now to store 32 bits per Long.
  371. * */
  372. #ifndef Pack_32
  373. #define Pack_32
  374. #endif
  375. #endif
  376. #define Kmax 15
  377. struct Bigint {
  378. struct Bigint *next;
  379. int k, maxwds, sign, wds;
  380. ULong x[1];
  381. };
  382. typedef struct Bigint Bigint;
  383. /* static variables, multithreading fun! */
  384. static Bigint *freelist[Kmax+1];
  385. static Bigint *p5s;
  386. static void destroy_freelist(void);
  387. #ifdef ZTS
  388. static MUTEX_T dtoa_mutex;
  389. static MUTEX_T pow5mult_mutex;
  390. #define _THREAD_PRIVATE_MUTEX_LOCK(x) tsrm_mutex_lock(x);
  391. #define _THREAD_PRIVATE_MUTEX_UNLOCK(x) tsrm_mutex_unlock(x);
  392. #else
  393. #define _THREAD_PRIVATE_MUTEX_LOCK(x)
  394. #define _THREAD_PRIVATE_MUTEX_UNLOCK(x)
  395. #endif /* ZTS */
  396. #ifdef DEBUG
  397. static void Bug(const char *message) {
  398. fprintf(stderr, "%s\n", message);
  399. }
  400. #endif
  401. ZEND_API int zend_startup_strtod(void) /* {{{ */
  402. {
  403. #ifdef ZTS
  404. dtoa_mutex = tsrm_mutex_alloc();
  405. pow5mult_mutex = tsrm_mutex_alloc();
  406. #endif
  407. return 1;
  408. }
  409. /* }}} */
  410. ZEND_API int zend_shutdown_strtod(void) /* {{{ */
  411. {
  412. destroy_freelist();
  413. #ifdef ZTS
  414. tsrm_mutex_free(dtoa_mutex);
  415. dtoa_mutex = NULL;
  416. tsrm_mutex_free(pow5mult_mutex);
  417. pow5mult_mutex = NULL;
  418. #endif
  419. return 1;
  420. }
  421. /* }}} */
  422. static Bigint * Balloc(int k)
  423. {
  424. int x;
  425. Bigint *rv;
  426. if (k > Kmax) {
  427. zend_error(E_ERROR, "Balloc() allocation exceeds list boundary");
  428. }
  429. _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
  430. if ((rv = freelist[k])) {
  431. freelist[k] = rv->next;
  432. } else {
  433. x = 1 << k;
  434. rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
  435. if (!rv) {
  436. _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
  437. zend_error(E_ERROR, "Balloc() failed to allocate memory");
  438. }
  439. rv->k = k;
  440. rv->maxwds = x;
  441. }
  442. _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
  443. rv->sign = rv->wds = 0;
  444. return rv;
  445. }
  446. static void Bfree(Bigint *v)
  447. {
  448. if (v) {
  449. _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
  450. v->next = freelist[v->k];
  451. freelist[v->k] = v;
  452. _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
  453. }
  454. }
  455. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  456. y->wds*sizeof(Long) + 2*sizeof(int))
  457. /* return value is only used as a simple string, so mis-aligned parts
  458. * inside the Bigint are not at risk on strict align architectures
  459. */
  460. static char * rv_alloc(int i) {
  461. int j, k, *r;
  462. j = sizeof(ULong);
  463. for(k = 0;
  464. sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;
  465. j <<= 1) {
  466. k++;
  467. }
  468. r = (int*)Balloc(k);
  469. *r = k;
  470. return (char *)(r+1);
  471. }
  472. static char * nrv_alloc(char *s, char **rve, int n)
  473. {
  474. char *rv, *t;
  475. t = rv = rv_alloc(n);
  476. while((*t = *s++) !=0) {
  477. t++;
  478. }
  479. if (rve) {
  480. *rve = t;
  481. }
  482. return rv;
  483. }
  484. static Bigint * multadd(Bigint *b, int m, int a) /* multiply by m and add a */
  485. {
  486. int i, wds;
  487. ULong *x, y;
  488. #ifdef Pack_32
  489. ULong xi, z;
  490. #endif
  491. Bigint *b1;
  492. wds = b->wds;
  493. x = b->x;
  494. i = 0;
  495. do {
  496. #ifdef Pack_32
  497. xi = *x;
  498. y = (xi & 0xffff) * m + a;
  499. z = (xi >> 16) * m + (y >> 16);
  500. a = (int)(z >> 16);
  501. *x++ = (z << 16) + (y & 0xffff);
  502. #else
  503. y = *x * m + a;
  504. a = (int)(y >> 16);
  505. *x++ = y & 0xffff;
  506. #endif
  507. }
  508. while(++i < wds);
  509. if (a) {
  510. if (wds >= b->maxwds) {
  511. b1 = Balloc(b->k+1);
  512. Bcopy(b1, b);
  513. Bfree(b);
  514. b = b1;
  515. }
  516. b->x[wds++] = a;
  517. b->wds = wds;
  518. }
  519. return b;
  520. }
  521. static int hi0bits(ULong x)
  522. {
  523. int k = 0;
  524. if (!(x & 0xffff0000)) {
  525. k = 16;
  526. x <<= 16;
  527. }
  528. if (!(x & 0xff000000)) {
  529. k += 8;
  530. x <<= 8;
  531. }
  532. if (!(x & 0xf0000000)) {
  533. k += 4;
  534. x <<= 4;
  535. }
  536. if (!(x & 0xc0000000)) {
  537. k += 2;
  538. x <<= 2;
  539. }
  540. if (!(x & 0x80000000)) {
  541. k++;
  542. if (!(x & 0x40000000)) {
  543. return 32;
  544. }
  545. }
  546. return k;
  547. }
  548. static int lo0bits(ULong *y)
  549. {
  550. int k;
  551. ULong x = *y;
  552. if (x & 7) {
  553. if (x & 1) {
  554. return 0;
  555. }
  556. if (x & 2) {
  557. *y = x >> 1;
  558. return 1;
  559. }
  560. *y = x >> 2;
  561. return 2;
  562. }
  563. k = 0;
  564. if (!(x & 0xffff)) {
  565. k = 16;
  566. x >>= 16;
  567. }
  568. if (!(x & 0xff)) {
  569. k += 8;
  570. x >>= 8;
  571. }
  572. if (!(x & 0xf)) {
  573. k += 4;
  574. x >>= 4;
  575. }
  576. if (!(x & 0x3)) {
  577. k += 2;
  578. x >>= 2;
  579. }
  580. if (!(x & 1)) {
  581. k++;
  582. x >>= 1;
  583. if (!(x & 1)) {
  584. return 32;
  585. }
  586. }
  587. *y = x;
  588. return k;
  589. }
  590. static Bigint * i2b(int i)
  591. {
  592. Bigint *b;
  593. b = Balloc(1);
  594. b->x[0] = i;
  595. b->wds = 1;
  596. return b;
  597. }
  598. static Bigint * mult(Bigint *a, Bigint *b)
  599. {
  600. Bigint *c;
  601. int k, wa, wb, wc;
  602. ULong carry, y, z;
  603. ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  604. #ifdef Pack_32
  605. ULong z2;
  606. #endif
  607. if (a->wds < b->wds) {
  608. c = a;
  609. a = b;
  610. b = c;
  611. }
  612. k = a->k;
  613. wa = a->wds;
  614. wb = b->wds;
  615. wc = wa + wb;
  616. if (wc > a->maxwds) {
  617. k++;
  618. }
  619. c = Balloc(k);
  620. for(x = c->x, xa = x + wc; x < xa; x++) {
  621. *x = 0;
  622. }
  623. xa = a->x;
  624. xae = xa + wa;
  625. xb = b->x;
  626. xbe = xb + wb;
  627. xc0 = c->x;
  628. #ifdef Pack_32
  629. for(; xb < xbe; xb++, xc0++) {
  630. if ((y = *xb & 0xffff)) {
  631. x = xa;
  632. xc = xc0;
  633. carry = 0;
  634. do {
  635. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  636. carry = z >> 16;
  637. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  638. carry = z2 >> 16;
  639. Storeinc(xc, z2, z);
  640. }
  641. while(x < xae);
  642. *xc = carry;
  643. }
  644. if ((y = *xb >> 16)) {
  645. x = xa;
  646. xc = xc0;
  647. carry = 0;
  648. z2 = *xc;
  649. do {
  650. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  651. carry = z >> 16;
  652. Storeinc(xc, z, z2);
  653. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  654. carry = z2 >> 16;
  655. }
  656. while(x < xae);
  657. *xc = z2;
  658. }
  659. }
  660. #else
  661. for(; xb < xbe; xc0++) {
  662. if (y = *xb++) {
  663. x = xa;
  664. xc = xc0;
  665. carry = 0;
  666. do {
  667. z = *x++ * y + *xc + carry;
  668. carry = z >> 16;
  669. *xc++ = z & 0xffff;
  670. }
  671. while(x < xae);
  672. *xc = carry;
  673. }
  674. }
  675. #endif
  676. for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  677. c->wds = wc;
  678. return c;
  679. }
  680. static Bigint * s2b (CONST char *s, int nd0, int nd, ULong y9)
  681. {
  682. Bigint *b;
  683. int i, k;
  684. Long x, y;
  685. x = (nd + 8) / 9;
  686. for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  687. #ifdef Pack_32
  688. b = Balloc(k);
  689. b->x[0] = y9;
  690. b->wds = 1;
  691. #else
  692. b = Balloc(k+1);
  693. b->x[0] = y9 & 0xffff;
  694. b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  695. #endif
  696. i = 9;
  697. if (9 < nd0) {
  698. s += 9;
  699. do b = multadd(b, 10, *s++ - '0');
  700. while(++i < nd0);
  701. s++;
  702. } else {
  703. s += 10;
  704. }
  705. for(; i < nd; i++) {
  706. b = multadd(b, 10, *s++ - '0');
  707. }
  708. return b;
  709. }
  710. static Bigint * pow5mult(Bigint *b, int k)
  711. {
  712. Bigint *b1, *p5, *p51;
  713. int i;
  714. static int p05[3] = { 5, 25, 125 };
  715. _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
  716. if ((i = k & 3)) {
  717. b = multadd(b, p05[i-1], 0);
  718. }
  719. if (!(k >>= 2)) {
  720. _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
  721. return b;
  722. }
  723. if (!(p5 = p5s)) {
  724. /* first time */
  725. p5 = p5s = i2b(625);
  726. p5->next = 0;
  727. }
  728. for(;;) {
  729. if (k & 1) {
  730. b1 = mult(b, p5);
  731. Bfree(b);
  732. b = b1;
  733. }
  734. if (!(k >>= 1)) {
  735. break;
  736. }
  737. if (!(p51 = p5->next)) {
  738. if (!(p51 = p5->next)) {
  739. p51 = p5->next = mult(p5,p5);
  740. p51->next = 0;
  741. }
  742. }
  743. p5 = p51;
  744. }
  745. _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
  746. return b;
  747. }
  748. static Bigint *lshift(Bigint *b, int k)
  749. {
  750. int i, k1, n, n1;
  751. Bigint *b1;
  752. ULong *x, *x1, *xe, z;
  753. #ifdef Pack_32
  754. n = k >> 5;
  755. #else
  756. n = k >> 4;
  757. #endif
  758. k1 = b->k;
  759. n1 = n + b->wds + 1;
  760. for(i = b->maxwds; n1 > i; i <<= 1) {
  761. k1++;
  762. }
  763. b1 = Balloc(k1);
  764. x1 = b1->x;
  765. for(i = 0; i < n; i++) {
  766. *x1++ = 0;
  767. }
  768. x = b->x;
  769. xe = x + b->wds;
  770. #ifdef Pack_32
  771. if (k &= 0x1f) {
  772. k1 = 32 - k;
  773. z = 0;
  774. do {
  775. *x1++ = *x << k | z;
  776. z = *x++ >> k1;
  777. }
  778. while(x < xe);
  779. if ((*x1 = z)) {
  780. ++n1;
  781. }
  782. }
  783. #else
  784. if (k &= 0xf) {
  785. k1 = 16 - k;
  786. z = 0;
  787. do {
  788. *x1++ = *x << k & 0xffff | z;
  789. z = *x++ >> k1;
  790. }
  791. while(x < xe);
  792. if (*x1 = z) {
  793. ++n1;
  794. }
  795. }
  796. #endif
  797. else do
  798. *x1++ = *x++;
  799. while(x < xe);
  800. b1->wds = n1 - 1;
  801. Bfree(b);
  802. return b1;
  803. }
  804. static int cmp(Bigint *a, Bigint *b)
  805. {
  806. ULong *xa, *xa0, *xb, *xb0;
  807. int i, j;
  808. i = a->wds;
  809. j = b->wds;
  810. #ifdef DEBUG
  811. if (i > 1 && !a->x[i-1])
  812. Bug("cmp called with a->x[a->wds-1] == 0");
  813. if (j > 1 && !b->x[j-1])
  814. Bug("cmp called with b->x[b->wds-1] == 0");
  815. #endif
  816. if (i -= j)
  817. return i;
  818. xa0 = a->x;
  819. xa = xa0 + j;
  820. xb0 = b->x;
  821. xb = xb0 + j;
  822. for(;;) {
  823. if (*--xa != *--xb)
  824. return *xa < *xb ? -1 : 1;
  825. if (xa <= xa0)
  826. break;
  827. }
  828. return 0;
  829. }
  830. static Bigint * diff(Bigint *a, Bigint *b)
  831. {
  832. Bigint *c;
  833. int i, wa, wb;
  834. Long borrow, y; /* We need signed shifts here. */
  835. ULong *xa, *xae, *xb, *xbe, *xc;
  836. #ifdef Pack_32
  837. Long z;
  838. #endif
  839. i = cmp(a,b);
  840. if (!i) {
  841. c = Balloc(0);
  842. c->wds = 1;
  843. c->x[0] = 0;
  844. return c;
  845. }
  846. if (i < 0) {
  847. c = a;
  848. a = b;
  849. b = c;
  850. i = 1;
  851. } else {
  852. i = 0;
  853. }
  854. c = Balloc(a->k);
  855. c->sign = i;
  856. wa = a->wds;
  857. xa = a->x;
  858. xae = xa + wa;
  859. wb = b->wds;
  860. xb = b->x;
  861. xbe = xb + wb;
  862. xc = c->x;
  863. borrow = 0;
  864. #ifdef Pack_32
  865. do {
  866. y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
  867. borrow = y >> 16;
  868. Sign_Extend(borrow, y);
  869. z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
  870. borrow = z >> 16;
  871. Sign_Extend(borrow, z);
  872. Storeinc(xc, z, y);
  873. } while(xb < xbe);
  874. while(xa < xae) {
  875. y = (*xa & 0xffff) + borrow;
  876. borrow = y >> 16;
  877. Sign_Extend(borrow, y);
  878. z = (*xa++ >> 16) + borrow;
  879. borrow = z >> 16;
  880. Sign_Extend(borrow, z);
  881. Storeinc(xc, z, y);
  882. }
  883. #else
  884. do {
  885. y = *xa++ - *xb++ + borrow;
  886. borrow = y >> 16;
  887. Sign_Extend(borrow, y);
  888. *xc++ = y & 0xffff;
  889. } while(xb < xbe);
  890. while(xa < xae) {
  891. y = *xa++ + borrow;
  892. borrow = y >> 16;
  893. Sign_Extend(borrow, y);
  894. *xc++ = y & 0xffff;
  895. }
  896. #endif
  897. while(!*--xc) {
  898. wa--;
  899. }
  900. c->wds = wa;
  901. return c;
  902. }
  903. static double ulp (double _x)
  904. {
  905. volatile _double x;
  906. register Long L;
  907. volatile _double a;
  908. value(x) = _x;
  909. L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  910. #ifndef Sudden_Underflow
  911. if (L > 0) {
  912. #endif
  913. #ifdef IBM
  914. L |= Exp_msk1 >> 4;
  915. #endif
  916. word0(a) = L;
  917. word1(a) = 0;
  918. #ifndef Sudden_Underflow
  919. }
  920. else {
  921. L = -L >> Exp_shift;
  922. if (L < Exp_shift) {
  923. word0(a) = 0x80000 >> L;
  924. word1(a) = 0;
  925. }
  926. else {
  927. word0(a) = 0;
  928. L -= Exp_shift;
  929. word1(a) = L >= 31 ? 1 : 1 << (31 - L);
  930. }
  931. }
  932. #endif
  933. return value(a);
  934. }
  935. static double
  936. b2d
  937. #ifdef KR_headers
  938. (a, e) Bigint *a; int *e;
  939. #else
  940. (Bigint *a, int *e)
  941. #endif
  942. {
  943. ULong *xa, *xa0, w, y, z;
  944. int k;
  945. volatile _double d;
  946. #ifdef VAX
  947. ULong d0, d1;
  948. #else
  949. #define d0 word0(d)
  950. #define d1 word1(d)
  951. #endif
  952. xa0 = a->x;
  953. xa = xa0 + a->wds;
  954. y = *--xa;
  955. #ifdef DEBUG
  956. if (!y) Bug("zero y in b2d");
  957. #endif
  958. k = hi0bits(y);
  959. *e = 32 - k;
  960. #ifdef Pack_32
  961. if (k < Ebits) {
  962. d0 = Exp_1 | y >> (Ebits - k);
  963. w = xa > xa0 ? *--xa : 0;
  964. d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  965. goto ret_d;
  966. }
  967. z = xa > xa0 ? *--xa : 0;
  968. if (k -= Ebits) {
  969. d0 = Exp_1 | y << k | z >> (32 - k);
  970. y = xa > xa0 ? *--xa : 0;
  971. d1 = z << k | y >> (32 - k);
  972. }
  973. else {
  974. d0 = Exp_1 | y;
  975. d1 = z;
  976. }
  977. #else
  978. if (k < Ebits + 16) {
  979. z = xa > xa0 ? *--xa : 0;
  980. d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  981. w = xa > xa0 ? *--xa : 0;
  982. y = xa > xa0 ? *--xa : 0;
  983. d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  984. goto ret_d;
  985. }
  986. z = xa > xa0 ? *--xa : 0;
  987. w = xa > xa0 ? *--xa : 0;
  988. k -= Ebits + 16;
  989. d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  990. y = xa > xa0 ? *--xa : 0;
  991. d1 = w << k + 16 | y << k;
  992. #endif
  993. ret_d:
  994. #ifdef VAX
  995. word0(d) = d0 >> 16 | d0 << 16;
  996. word1(d) = d1 >> 16 | d1 << 16;
  997. #else
  998. #undef d0
  999. #undef d1
  1000. #endif
  1001. return value(d);
  1002. }
  1003. static Bigint * d2b(double _d, int *e, int *bits)
  1004. {
  1005. Bigint *b;
  1006. int de, i, k;
  1007. ULong *x, y, z;
  1008. volatile _double d;
  1009. #ifdef VAX
  1010. ULong d0, d1;
  1011. #endif
  1012. value(d) = _d;
  1013. #ifdef VAX
  1014. d0 = word0(d) >> 16 | word0(d) << 16;
  1015. d1 = word1(d) >> 16 | word1(d) << 16;
  1016. #else
  1017. #define d0 word0(d)
  1018. #define d1 word1(d)
  1019. #endif
  1020. #ifdef Pack_32
  1021. b = Balloc(1);
  1022. #else
  1023. b = Balloc(2);
  1024. #endif
  1025. x = b->x;
  1026. z = d0 & Frac_mask;
  1027. d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
  1028. #ifdef Sudden_Underflow
  1029. de = (int)(d0 >> Exp_shift);
  1030. #ifndef IBM
  1031. z |= Exp_msk11;
  1032. #endif
  1033. #else
  1034. if ((de = (int)(d0 >> Exp_shift)))
  1035. z |= Exp_msk1;
  1036. #endif
  1037. #ifdef Pack_32
  1038. if ((y = d1)) {
  1039. if ((k = lo0bits(&y))) {
  1040. x[0] = y | (z << (32 - k));
  1041. z >>= k;
  1042. } else {
  1043. x[0] = y;
  1044. }
  1045. i = b->wds = (x[1] = z) ? 2 : 1;
  1046. } else {
  1047. #ifdef DEBUG
  1048. if (!z)
  1049. Bug("Zero passed to d2b");
  1050. #endif
  1051. k = lo0bits(&z);
  1052. x[0] = z;
  1053. i = b->wds = 1;
  1054. k += 32;
  1055. }
  1056. #else
  1057. if (y = d1) {
  1058. if (k = lo0bits(&y)) {
  1059. if (k >= 16) {
  1060. x[0] = y | z << 32 - k & 0xffff;
  1061. x[1] = z >> k - 16 & 0xffff;
  1062. x[2] = z >> k;
  1063. i = 2;
  1064. } else {
  1065. x[0] = y & 0xffff;
  1066. x[1] = y >> 16 | z << 16 - k & 0xffff;
  1067. x[2] = z >> k & 0xffff;
  1068. x[3] = z >> k+16;
  1069. i = 3;
  1070. }
  1071. } else {
  1072. x[0] = y & 0xffff;
  1073. x[1] = y >> 16;
  1074. x[2] = z & 0xffff;
  1075. x[3] = z >> 16;
  1076. i = 3;
  1077. }
  1078. } else {
  1079. #ifdef DEBUG
  1080. if (!z)
  1081. Bug("Zero passed to d2b");
  1082. #endif
  1083. k = lo0bits(&z);
  1084. if (k >= 16) {
  1085. x[0] = z;
  1086. i = 0;
  1087. } else {
  1088. x[0] = z & 0xffff;
  1089. x[1] = z >> 16;
  1090. i = 1;
  1091. }
  1092. k += 32;
  1093. }
  1094. while(!x[i])
  1095. --i;
  1096. b->wds = i + 1;
  1097. #endif
  1098. #ifndef Sudden_Underflow
  1099. if (de) {
  1100. #endif
  1101. #ifdef IBM
  1102. *e = (de - Bias - (P-1) << 2) + k;
  1103. *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1104. #else
  1105. *e = de - Bias - (P-1) + k;
  1106. *bits = P - k;
  1107. #endif
  1108. #ifndef Sudden_Underflow
  1109. } else {
  1110. *e = de - Bias - (P-1) + 1 + k;
  1111. #ifdef Pack_32
  1112. *bits = 32*i - hi0bits(x[i-1]);
  1113. #else
  1114. *bits = (i+2)*16 - hi0bits(x[i]);
  1115. #endif
  1116. }
  1117. #endif
  1118. return b;
  1119. }
  1120. #undef d0
  1121. #undef d1
  1122. static double ratio (Bigint *a, Bigint *b)
  1123. {
  1124. volatile _double da, db;
  1125. int k, ka, kb;
  1126. value(da) = b2d(a, &ka);
  1127. value(db) = b2d(b, &kb);
  1128. #ifdef Pack_32
  1129. k = ka - kb + 32*(a->wds - b->wds);
  1130. #else
  1131. k = ka - kb + 16*(a->wds - b->wds);
  1132. #endif
  1133. #ifdef IBM
  1134. if (k > 0) {
  1135. word0(da) += (k >> 2)*Exp_msk1;
  1136. if (k &= 3) {
  1137. da *= 1 << k;
  1138. }
  1139. } else {
  1140. k = -k;
  1141. word0(db) += (k >> 2)*Exp_msk1;
  1142. if (k &= 3)
  1143. db *= 1 << k;
  1144. }
  1145. #else
  1146. if (k > 0) {
  1147. word0(da) += k*Exp_msk1;
  1148. } else {
  1149. k = -k;
  1150. word0(db) += k*Exp_msk1;
  1151. }
  1152. #endif
  1153. return value(da) / value(db);
  1154. }
  1155. static CONST double
  1156. tens[] = {
  1157. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1158. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1159. 1e20, 1e21, 1e22
  1160. #ifdef VAX
  1161. , 1e23, 1e24
  1162. #endif
  1163. };
  1164. #ifdef IEEE_Arith
  1165. static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1166. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
  1167. #define n_bigtens 5
  1168. #else
  1169. #ifdef IBM
  1170. static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
  1171. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1172. #define n_bigtens 3
  1173. #else
  1174. static CONST double bigtens[] = { 1e16, 1e32 };
  1175. static CONST double tinytens[] = { 1e-16, 1e-32 };
  1176. #define n_bigtens 2
  1177. #endif
  1178. #endif
  1179. static int quorem(Bigint *b, Bigint *S)
  1180. {
  1181. int n;
  1182. Long borrow, y;
  1183. ULong carry, q, ys;
  1184. ULong *bx, *bxe, *sx, *sxe;
  1185. #ifdef Pack_32
  1186. Long z;
  1187. ULong si, zs;
  1188. #endif
  1189. n = S->wds;
  1190. #ifdef DEBUG
  1191. /*debug*/ if (b->wds > n)
  1192. /*debug*/ Bug("oversize b in quorem");
  1193. #endif
  1194. if (b->wds < n)
  1195. return 0;
  1196. sx = S->x;
  1197. sxe = sx + --n;
  1198. bx = b->x;
  1199. bxe = bx + n;
  1200. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  1201. #ifdef DEBUG
  1202. /*debug*/ if (q > 9)
  1203. /*debug*/ Bug("oversized quotient in quorem");
  1204. #endif
  1205. if (q) {
  1206. borrow = 0;
  1207. carry = 0;
  1208. do {
  1209. #ifdef Pack_32
  1210. si = *sx++;
  1211. ys = (si & 0xffff) * q + carry;
  1212. zs = (si >> 16) * q + (ys >> 16);
  1213. carry = zs >> 16;
  1214. y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  1215. borrow = y >> 16;
  1216. Sign_Extend(borrow, y);
  1217. z = (*bx >> 16) - (zs & 0xffff) + borrow;
  1218. borrow = z >> 16;
  1219. Sign_Extend(borrow, z);
  1220. Storeinc(bx, z, y);
  1221. #else
  1222. ys = *sx++ * q + carry;
  1223. carry = ys >> 16;
  1224. y = *bx - (ys & 0xffff) + borrow;
  1225. borrow = y >> 16;
  1226. Sign_Extend(borrow, y);
  1227. *bx++ = y & 0xffff;
  1228. #endif
  1229. }
  1230. while(sx <= sxe);
  1231. if (!*bxe) {
  1232. bx = b->x;
  1233. while(--bxe > bx && !*bxe)
  1234. --n;
  1235. b->wds = n;
  1236. }
  1237. }
  1238. if (cmp(b, S) >= 0) {
  1239. q++;
  1240. borrow = 0;
  1241. carry = 0;
  1242. bx = b->x;
  1243. sx = S->x;
  1244. do {
  1245. #ifdef Pack_32
  1246. si = *sx++;
  1247. ys = (si & 0xffff) + carry;
  1248. zs = (si >> 16) + (ys >> 16);
  1249. carry = zs >> 16;
  1250. y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  1251. borrow = y >> 16;
  1252. Sign_Extend(borrow, y);
  1253. z = (*bx >> 16) - (zs & 0xffff) + borrow;
  1254. borrow = z >> 16;
  1255. Sign_Extend(borrow, z);
  1256. Storeinc(bx, z, y);
  1257. #else
  1258. ys = *sx++ + carry;
  1259. carry = ys >> 16;
  1260. y = *bx - (ys & 0xffff) + borrow;
  1261. borrow = y >> 16;
  1262. Sign_Extend(borrow, y);
  1263. *bx++ = y & 0xffff;
  1264. #endif
  1265. }
  1266. while(sx <= sxe);
  1267. bx = b->x;
  1268. bxe = bx + n;
  1269. if (!*bxe) {
  1270. while(--bxe > bx && !*bxe)
  1271. --n;
  1272. b->wds = n;
  1273. }
  1274. }
  1275. return q;
  1276. }
  1277. static void destroy_freelist(void)
  1278. {
  1279. int i;
  1280. Bigint *tmp;
  1281. _THREAD_PRIVATE_MUTEX_LOCK(dtoa_mutex);
  1282. for (i = 0; i <= Kmax; i++) {
  1283. Bigint **listp = &freelist[i];
  1284. while ((tmp = *listp) != NULL) {
  1285. *listp = tmp->next;
  1286. free(tmp);
  1287. }
  1288. freelist[i] = NULL;
  1289. }
  1290. _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa_mutex);
  1291. }
  1292. ZEND_API void zend_freedtoa(char *s)
  1293. {
  1294. Bigint *b = (Bigint *)((int *)s - 1);
  1295. b->maxwds = 1 << (b->k = *(int*)b);
  1296. Bfree(b);
  1297. }
  1298. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  1299. *
  1300. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  1301. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  1302. *
  1303. * Modifications:
  1304. * 1. Rather than iterating, we use a simple numeric overestimate
  1305. * to determine k = floor(log10(d)). We scale relevant
  1306. * quantities using O(log2(k)) rather than O(k) multiplications.
  1307. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  1308. * try to generate digits strictly left to right. Instead, we
  1309. * compute with fewer bits and propagate the carry if necessary
  1310. * when rounding the final digit up. This is often faster.
  1311. * 3. Under the assumption that input will be rounded nearest,
  1312. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  1313. * That is, we allow equality in stopping tests when the
  1314. * round-nearest rule will give the same floating-point value
  1315. * as would satisfaction of the stopping test with strict
  1316. * inequality.
  1317. * 4. We remove common factors of powers of 2 from relevant
  1318. * quantities.
  1319. * 5. When converting floating-point integers less than 1e16,
  1320. * we use floating-point arithmetic rather than resorting
  1321. * to multiple-precision integers.
  1322. * 6. When asked to produce fewer than 15 digits, we first try
  1323. * to get by with floating-point arithmetic; we resort to
  1324. * multiple-precision integer arithmetic only if we cannot
  1325. * guarantee that the floating-point calculation has given
  1326. * the correctly rounded result. For k requested digits and
  1327. * "uniformly" distributed input, the probability is
  1328. * something like 10^(k-15) that we must resort to the Long
  1329. * calculation.
  1330. */
  1331. ZEND_API char * zend_dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  1332. {
  1333. /* Arguments ndigits, decpt, sign are similar to those
  1334. of ecvt and fcvt; trailing zeros are suppressed from
  1335. the returned string. If not null, *rve is set to point
  1336. to the end of the return value. If d is +-Infinity or NaN,
  1337. then *decpt is set to 9999.
  1338. mode:
  1339. 0 ==> shortest string that yields d when read in
  1340. and rounded to nearest.
  1341. 1 ==> like 0, but with Steele & White stopping rule;
  1342. e.g. with IEEE P754 arithmetic , mode 0 gives
  1343. 1e23 whereas mode 1 gives 9.999999999999999e22.
  1344. 2 ==> max(1,ndigits) significant digits. This gives a
  1345. return value similar to that of ecvt, except
  1346. that trailing zeros are suppressed.
  1347. 3 ==> through ndigits past the decimal point. This
  1348. gives a return value similar to that from fcvt,
  1349. except that trailing zeros are suppressed, and
  1350. ndigits can be negative.
  1351. 4-9 should give the same return values as 2-3, i.e.,
  1352. 4 <= mode <= 9 ==> same return as mode
  1353. 2 + (mode & 1). These modes are mainly for
  1354. debugging; often they run slower but sometimes
  1355. faster than modes 2-3.
  1356. 4,5,8,9 ==> left-to-right digit generation.
  1357. 6-9 ==> don't try fast floating-point estimate
  1358. (if applicable).
  1359. Values of mode other than 0-9 are treated as mode 0.
  1360. Sufficient space is allocated to the return value
  1361. to hold the suppressed trailing zeros.
  1362. */
  1363. int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1,
  1364. j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  1365. spec_case = 0, try_quick;
  1366. Long L;
  1367. #ifndef Sudden_Underflow
  1368. int denorm;
  1369. ULong x;
  1370. #endif
  1371. Bigint *b, *b1, *delta, *mlo, *mhi, *S, *tmp;
  1372. double ds;
  1373. char *s, *s0;
  1374. volatile _double d, d2, eps;
  1375. value(d) = _d;
  1376. if (word0(d) & Sign_bit) {
  1377. /* set sign for everything, including 0's and NaNs */
  1378. *sign = 1;
  1379. word0(d) &= ~Sign_bit; /* clear sign bit */
  1380. }
  1381. else
  1382. *sign = 0;
  1383. #if defined(IEEE_Arith) + defined(VAX)
  1384. #ifdef IEEE_Arith
  1385. if ((word0(d) & Exp_mask) == Exp_mask)
  1386. #else
  1387. if (word0(d) == 0x8000)
  1388. #endif
  1389. {
  1390. /* Infinity or NaN */
  1391. *decpt = 9999;
  1392. #ifdef IEEE_Arith
  1393. if (!word1(d) && !(word0(d) & 0xfffff))
  1394. return nrv_alloc("Infinity", rve, 8);
  1395. #endif
  1396. return nrv_alloc("NaN", rve, 3);
  1397. }
  1398. #endif
  1399. #ifdef IBM
  1400. value(d) += 0; /* normalize */
  1401. #endif
  1402. if (!value(d)) {
  1403. *decpt = 1;
  1404. return nrv_alloc("0", rve, 1);
  1405. }
  1406. b = d2b(value(d), &be, &bbits);
  1407. #ifdef Sudden_Underflow
  1408. i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  1409. #else
  1410. if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
  1411. #endif
  1412. value(d2) = value(d);
  1413. word0(d2) &= Frac_mask1;
  1414. word0(d2) |= Exp_11;
  1415. #ifdef IBM
  1416. if (j = 11 - hi0bits(word0(d2) & Frac_mask))
  1417. value(d2) /= 1 << j;
  1418. #endif
  1419. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  1420. * log10(x) = log(x) / log(10)
  1421. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  1422. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  1423. *
  1424. * This suggests computing an approximation k to log10(d) by
  1425. *
  1426. * k = (i - Bias)*0.301029995663981
  1427. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  1428. *
  1429. * We want k to be too large rather than too small.
  1430. * The error in the first-order Taylor series approximation
  1431. * is in our favor, so we just round up the constant enough
  1432. * to compensate for any error in the multiplication of
  1433. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  1434. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  1435. * adding 1e-13 to the constant term more than suffices.
  1436. * Hence we adjust the constant term to 0.1760912590558.
  1437. * (We could get a more accurate k by invoking log10,
  1438. * but this is probably not worthwhile.)
  1439. */
  1440. i -= Bias;
  1441. #ifdef IBM
  1442. i <<= 2;
  1443. i += j;
  1444. #endif
  1445. #ifndef Sudden_Underflow
  1446. denorm = 0;
  1447. }
  1448. else {
  1449. /* d is denormalized */
  1450. i = bbits + be + (Bias + (P-1) - 1);
  1451. x = i > 32 ? (word0(d) << (64 - i)) | (word1(d) >> (i - 32))
  1452. : (word1(d) << (32 - i));
  1453. value(d2) = x;
  1454. word0(d2) -= 31*Exp_msk1; /* adjust exponent */
  1455. i -= (Bias + (P-1) - 1) + 1;
  1456. denorm = 1;
  1457. }
  1458. #endif
  1459. ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  1460. k = (int)ds;
  1461. if (ds < 0. && ds != k)
  1462. k--; /* want k = floor(ds) */
  1463. k_check = 1;
  1464. if (k >= 0 && k <= Ten_pmax) {
  1465. if (value(d) < tens[k])
  1466. k--;
  1467. k_check = 0;
  1468. }
  1469. j = bbits - i - 1;
  1470. if (j >= 0) {
  1471. b2 = 0;
  1472. s2 = j;
  1473. }
  1474. else {
  1475. b2 = -j;
  1476. s2 = 0;
  1477. }
  1478. if (k >= 0) {
  1479. b5 = 0;
  1480. s5 = k;
  1481. s2 += k;
  1482. }
  1483. else {
  1484. b2 -= k;
  1485. b5 = -k;
  1486. s5 = 0;
  1487. }
  1488. if (mode < 0 || mode > 9)
  1489. mode = 0;
  1490. try_quick = 1;
  1491. if (mode > 5) {
  1492. mode -= 4;
  1493. try_quick = 0;
  1494. }
  1495. leftright = 1;
  1496. switch(mode) {
  1497. case 0:
  1498. case 1:
  1499. ilim = ilim1 = -1;
  1500. i = 18;
  1501. ndigits = 0;
  1502. break;
  1503. case 2:
  1504. leftright = 0;
  1505. /* no break */
  1506. case 4:
  1507. if (ndigits <= 0)
  1508. ndigits = 1;
  1509. ilim = ilim1 = i = ndigits;
  1510. break;
  1511. case 3:
  1512. leftright = 0;
  1513. /* no break */
  1514. case 5:
  1515. i = ndigits + k + 1;
  1516. ilim = i;
  1517. ilim1 = i - 1;
  1518. if (i <= 0)
  1519. i = 1;
  1520. }
  1521. s = s0 = rv_alloc(i);
  1522. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  1523. /* Try to get by with floating-point arithmetic. */
  1524. i = 0;
  1525. value(d2) = value(d);
  1526. k0 = k;
  1527. ilim0 = ilim;
  1528. ieps = 2; /* conservative */
  1529. if (k > 0) {
  1530. ds = tens[k&0xf];
  1531. j = k >> 4;
  1532. if (j & Bletch) {
  1533. /* prevent overflows */
  1534. j &= Bletch - 1;
  1535. value(d) /= bigtens[n_bigtens-1];
  1536. ieps++;
  1537. }
  1538. for(; j; j >>= 1, i++)
  1539. if (j & 1) {
  1540. ieps++;
  1541. ds *= bigtens[i];
  1542. }
  1543. value(d) /= ds;
  1544. }
  1545. else if ((j1 = -k)) {
  1546. value(d) *= tens[j1 & 0xf];
  1547. for(j = j1 >> 4; j; j >>= 1, i++)
  1548. if (j & 1) {
  1549. ieps++;
  1550. value(d) *= bigtens[i];
  1551. }
  1552. }
  1553. if (k_check && value(d) < 1. && ilim > 0) {
  1554. if (ilim1 <= 0)
  1555. goto fast_failed;
  1556. ilim = ilim1;
  1557. k--;
  1558. value(d) *= 10.;
  1559. ieps++;
  1560. }
  1561. value(eps) = ieps*value(d) + 7.;
  1562. word0(eps) -= (P-1)*Exp_msk1;
  1563. if (ilim == 0) {
  1564. S = mhi = 0;
  1565. value(d) -= 5.;
  1566. if (value(d) > value(eps))
  1567. goto one_digit;
  1568. if (value(d) < -value(eps))
  1569. goto no_digits;
  1570. goto fast_failed;
  1571. }
  1572. #ifndef No_leftright
  1573. if (leftright) {
  1574. /* Use Steele & White method of only
  1575. * generating digits needed.
  1576. */
  1577. value(eps) = 0.5/tens[ilim-1] - value(eps);
  1578. for(i = 0;;) {
  1579. L = value(d);
  1580. value(d) -= L;
  1581. *s++ = '0' + (int)L;
  1582. if (value(d) < value(eps))
  1583. goto ret1;
  1584. if (1. - value(d) < value(eps))
  1585. goto bump_up;
  1586. if (++i >= ilim)
  1587. break;
  1588. value(eps) *= 10.;
  1589. value(d) *= 10.;
  1590. }
  1591. }
  1592. else {
  1593. #endif
  1594. /* Generate ilim digits, then fix them up. */
  1595. value(eps) *= tens[ilim-1];
  1596. for(i = 1;; i++, value(d) *= 10.) {
  1597. L = value(d);
  1598. value(d) -= L;
  1599. *s++ = '0' + (int)L;
  1600. if (i == ilim) {
  1601. if (value(d) > 0.5 + value(eps))
  1602. goto bump_up;
  1603. else if (value(d) < 0.5 - value(eps)) {
  1604. while(*--s == '0');
  1605. s++;
  1606. goto ret1;
  1607. }
  1608. break;
  1609. }
  1610. }
  1611. #ifndef No_leftright
  1612. }
  1613. #endif
  1614. fast_failed:
  1615. s = s0;
  1616. value(d) = value(d2);
  1617. k = k0;
  1618. ilim = ilim0;
  1619. }
  1620. /* Do we have a "small" integer? */
  1621. if (be >= 0 && k <= Int_max) {
  1622. /* Yes. */
  1623. ds = tens[k];
  1624. if (ndigits < 0 && ilim <= 0) {
  1625. S = mhi = 0;
  1626. if (ilim < 0 || value(d) <= 5*ds)
  1627. goto no_digits;
  1628. goto one_digit;
  1629. }
  1630. for(i = 1;; i++) {
  1631. L = value(d) / ds;
  1632. value(d) -= L*ds;
  1633. #ifdef Check_FLT_ROUNDS
  1634. /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  1635. if (value(d) < 0) {
  1636. L--;
  1637. value(d) += ds;
  1638. }
  1639. #endif
  1640. *s++ = '0' + (int)L;
  1641. if (i == ilim) {
  1642. value(d) += value(d);
  1643. if (value(d) > ds || (value(d) == ds && (L & 1))) {
  1644. bump_up:
  1645. while(*--s == '9')
  1646. if (s == s0) {
  1647. k++;
  1648. *s = '0';
  1649. break;
  1650. }
  1651. ++*s++;
  1652. }
  1653. break;
  1654. }
  1655. if (!(value(d) *= 10.))
  1656. break;
  1657. }
  1658. goto ret1;
  1659. }
  1660. m2 = b2;
  1661. m5 = b5;
  1662. mhi = mlo = 0;
  1663. if (leftright) {
  1664. if (mode < 2) {
  1665. i =
  1666. #ifndef Sudden_Underflow
  1667. denorm ? be + (Bias + (P-1) - 1 + 1) :
  1668. #endif
  1669. #ifdef IBM
  1670. 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  1671. #else
  1672. 1 + P - bbits;
  1673. #endif
  1674. }
  1675. else {
  1676. j = ilim - 1;
  1677. if (m5 >= j)
  1678. m5 -= j;
  1679. else {
  1680. s5 += j -= m5;
  1681. b5 += j;
  1682. m5 = 0;
  1683. }
  1684. if ((i = ilim) < 0) {
  1685. m2 -= i;
  1686. i = 0;
  1687. }
  1688. }
  1689. b2 += i;
  1690. s2 += i;
  1691. mhi = i2b(1);
  1692. }
  1693. if (m2 > 0 && s2 > 0) {
  1694. i = m2 < s2 ? m2 : s2;
  1695. b2 -= i;
  1696. m2 -= i;
  1697. s2 -= i;
  1698. }
  1699. if (b5 > 0) {
  1700. if (leftright) {
  1701. if (m5 > 0) {
  1702. mhi = pow5mult(mhi, m5);
  1703. b1 = mult(mhi, b);
  1704. Bfree(b);
  1705. b = b1;
  1706. }
  1707. if ((j = b5 - m5)) {
  1708. b = pow5mult(b, j);
  1709. }
  1710. } else {
  1711. b = pow5mult(b, b5);
  1712. }
  1713. }
  1714. S = i2b(1);
  1715. if (s5 > 0)
  1716. S = pow5mult(S, s5);
  1717. /* Check for special case that d is a normalized power of 2. */
  1718. if (mode < 2) {
  1719. if (!word1(d) && !(word0(d) & Bndry_mask)
  1720. #ifndef Sudden_Underflow
  1721. && word0(d) & Exp_mask
  1722. #endif
  1723. ) {
  1724. /* The special case */
  1725. b2 += Log2P;
  1726. s2 += Log2P;
  1727. spec_case = 1;
  1728. } else {
  1729. spec_case = 0;
  1730. }
  1731. }
  1732. /* Arrange for convenient computation of quotients:
  1733. * shift left if necessary so divisor has 4 leading 0 bits.
  1734. *
  1735. * Perhaps we should just compute leading 28 bits of S once
  1736. * and for all and pass them and a shift to quorem, so it
  1737. * can do shifts and ors to compute the numerator for q.
  1738. */
  1739. #ifdef Pack_32
  1740. if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
  1741. i = 32 - i;
  1742. #else
  1743. if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
  1744. i = 16 - i;
  1745. #endif
  1746. if (i > 4) {
  1747. i -= 4;
  1748. b2 += i;
  1749. m2 += i;
  1750. s2 += i;
  1751. }
  1752. else if (i < 4) {
  1753. i += 28;
  1754. b2 += i;
  1755. m2 += i;
  1756. s2 += i;
  1757. }
  1758. if (b2 > 0)
  1759. b = lshift(b, b2);
  1760. if (s2 > 0)
  1761. S = lshift(S, s2);
  1762. if (k_check) {
  1763. if (cmp(b,S) < 0) {
  1764. k--;
  1765. b = multadd(b, 10, 0); /* we botched the k estimate */
  1766. if (leftright)
  1767. mhi = multadd(mhi, 10, 0);
  1768. ilim = ilim1;
  1769. }
  1770. }
  1771. if (ilim <= 0 && mode > 2) {
  1772. if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  1773. /* no digits, fcvt style */
  1774. no_digits:
  1775. k = -1 - ndigits;
  1776. goto ret;
  1777. }
  1778. one_digit:
  1779. *s++ = '1';
  1780. k++;
  1781. goto ret;
  1782. }
  1783. if (leftright) {
  1784. if (m2 > 0)
  1785. mhi = lshift(mhi, m2);
  1786. /* Compute mlo -- check for special case
  1787. * that d is a normalized power of 2.
  1788. */
  1789. mlo = mhi;
  1790. if (spec_case) {
  1791. mhi = Balloc(mhi->k);
  1792. Bcopy(mhi, mlo);
  1793. mhi = lshift(mhi, Log2P);
  1794. }
  1795. for(i = 1;;i++) {
  1796. dig = quorem(b,S) + '0';
  1797. /* Do we yet have the shortest decimal string
  1798. * that will round to d?
  1799. */
  1800. j = cmp(b, mlo);
  1801. delta = diff(S, mhi);
  1802. j1 = delta->sign ? 1 : cmp(b, delta);
  1803. Bfree(delta);
  1804. #ifndef ROUND_BIASED
  1805. if (j1 == 0 && !mode && !(word1(d) & 1)) {
  1806. if (dig == '9')
  1807. goto round_9_up;
  1808. if (j > 0)
  1809. dig++;
  1810. *s++ = dig;
  1811. goto ret;
  1812. }
  1813. #endif
  1814. if (j < 0 || (j == 0 && !mode
  1815. #ifndef ROUND_BIASED
  1816. && !(word1(d) & 1)
  1817. #endif
  1818. )) {
  1819. if (j1 > 0) {
  1820. b = lshift(b, 1);
  1821. j1 = cmp(b, S);
  1822. if ((j1 > 0 || (j1 == 0 && (dig & 1)))
  1823. && dig++ == '9')
  1824. goto round_9_up;
  1825. }
  1826. *s++ = dig;
  1827. goto ret;
  1828. }
  1829. if (j1 > 0) {
  1830. if (dig == '9') { /* possible if i == 1 */
  1831. round_9_up:
  1832. *s++ = '9';
  1833. goto roundoff;
  1834. }
  1835. *s++ = dig + 1;
  1836. goto ret;
  1837. }
  1838. *s++ = dig;
  1839. if (i == ilim)
  1840. break;
  1841. b = multadd(b, 10, 0);
  1842. if (mlo == mhi)
  1843. mlo = mhi = multadd(mhi, 10, 0);
  1844. else {
  1845. mlo = multadd(mlo, 10, 0);
  1846. mhi = multadd(mhi, 10, 0);
  1847. }
  1848. }
  1849. }
  1850. else
  1851. for(i = 1;; i++) {
  1852. *s++ = dig = quorem(b,S) + '0';
  1853. if (i >= ilim)
  1854. break;
  1855. b = multadd(b, 10, 0);
  1856. }
  1857. /* Round off last digit */
  1858. b = lshift(b, 1);
  1859. j = cmp(b, S);
  1860. if (j > 0 || (j == 0 && (dig & 1))) {
  1861. roundoff:
  1862. while(*--s == '9')
  1863. if (s == s0) {
  1864. k++;
  1865. *s++ = '1';
  1866. goto ret;
  1867. }
  1868. ++*s++;
  1869. }
  1870. else {
  1871. while(*--s == '0');
  1872. s++;
  1873. }
  1874. ret:
  1875. Bfree(S);
  1876. if (mhi) {
  1877. if (mlo && mlo != mhi)
  1878. Bfree(mlo);
  1879. Bfree(mhi);
  1880. }
  1881. ret1:
  1882. _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
  1883. while (p5s) {
  1884. tmp = p5s;
  1885. p5s = p5s->next;
  1886. free(tmp);
  1887. }
  1888. _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
  1889. Bfree(b);
  1890. if (s == s0) { /* don't return empty string */
  1891. *s++ = '0';
  1892. k = 0;
  1893. }
  1894. *s = 0;
  1895. *decpt = k + 1;
  1896. if (rve)
  1897. *rve = s;
  1898. return s0;
  1899. }
  1900. ZEND_API double zend_strtod (CONST char *s00, CONST char **se)
  1901. {
  1902. int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
  1903. e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
  1904. CONST char *s, *s0, *s1;
  1905. volatile double aadj, aadj1, adj;
  1906. volatile _double rv, rv0;
  1907. Long L;
  1908. ULong y, z;
  1909. Bigint *bb, *bb1, *bd, *bd0, *bs, *delta, *tmp;
  1910. double result;
  1911. CONST char decimal_point = '.';
  1912. sign = nz0 = nz = 0;
  1913. value(rv) = 0.;
  1914. for(s = s00; isspace((unsigned char) *s); s++)
  1915. ;
  1916. if (*s == '-') {
  1917. sign = 1;
  1918. s++;
  1919. } else if (*s == '+') {
  1920. s++;
  1921. }
  1922. if (*s == '\0') {
  1923. s = s00;
  1924. goto ret;
  1925. }
  1926. if (*s == '0') {
  1927. nz0 = 1;
  1928. while(*++s == '0') ;
  1929. if (!*s)
  1930. goto ret;
  1931. }
  1932. s0 = s;
  1933. y = z = 0;
  1934. for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  1935. if (nd < 9)
  1936. y = 10*y + c - '0';
  1937. else if (nd < 16)
  1938. z = 10*z + c - '0';
  1939. nd0 = nd;
  1940. if (c == decimal_point) {
  1941. c = *++s;
  1942. if (!nd) {
  1943. for(; c == '0'; c = *++s)
  1944. nz++;
  1945. if (c > '0' && c <= '9') {
  1946. s0 = s;
  1947. nf += nz;
  1948. nz = 0;
  1949. goto have_dig;
  1950. }
  1951. goto dig_done;
  1952. }
  1953. for(; c >= '0' && c <= '9'; c = *++s) {
  1954. have_dig:
  1955. nz++;
  1956. if (c -= '0') {
  1957. nf += nz;
  1958. for(i = 1; i < nz; i++)
  1959. if (nd++ < 9)
  1960. y *= 10;
  1961. else if (nd <= DBL_DIG + 1)
  1962. z *= 10;
  1963. if (nd++ < 9)
  1964. y = 10*y + c;
  1965. else if (nd <= DBL_DIG + 1)
  1966. z = 10*z + c;
  1967. nz = 0;
  1968. }
  1969. }
  1970. }
  1971. dig_done:
  1972. e = 0;
  1973. if (c == 'e' || c == 'E') {
  1974. if (!nd && !nz && !nz0) {
  1975. s = s00;
  1976. goto ret;
  1977. }
  1978. s00 = s;
  1979. esign = 0;
  1980. switch(c = *++s) {
  1981. case '-':
  1982. esign = 1;
  1983. case '+':
  1984. c = *++s;
  1985. }
  1986. if (c >= '0' && c <= '9') {
  1987. while(c == '0')
  1988. c = *++s;
  1989. if (c > '0' && c <= '9') {
  1990. L = c - '0';
  1991. s1 = s;
  1992. while((c = *++s) >= '0' && c <= '9')
  1993. L = 10*L + c - '0';
  1994. if (s - s1 > 8 || L > 19999)
  1995. /* Avoid confusion from exponents
  1996. * so large that e might overflow.
  1997. */
  1998. e = 19999; /* safe for 16 bit ints */
  1999. else
  2000. e = (int)L;
  2001. if (esign)
  2002. e = -e;
  2003. }
  2004. else
  2005. e = 0;
  2006. }
  2007. else
  2008. s = s00;
  2009. }
  2010. if (!nd) {
  2011. if (!nz && !nz0)
  2012. s = s00;
  2013. goto ret;
  2014. }
  2015. e1 = e -= nf;
  2016. /* Now we have nd0 digits, starting at s0, followed by a
  2017. * decimal point, followed by nd-nd0 digits. The number we're
  2018. * after is the integer represented by those digits times
  2019. * 10**e */
  2020. if (!nd0)
  2021. nd0 = nd;
  2022. k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  2023. value(rv) = y;
  2024. if (k > 9)
  2025. value(rv) = tens[k - 9] * value(rv) + z;
  2026. bd0 = 0;
  2027. if (nd <= DBL_DIG
  2028. #ifndef RND_PRODQUOT
  2029. && FLT_ROUNDS == 1
  2030. #endif
  2031. ) {
  2032. if (!e)
  2033. goto ret;
  2034. if (e > 0) {
  2035. if (e <= Ten_pmax) {
  2036. #ifdef VAX
  2037. goto vax_ovfl_check;
  2038. #else
  2039. /* value(rv) = */ rounded_product(value(rv),
  2040. tens[e]);
  2041. goto ret;
  2042. #endif
  2043. }
  2044. i = DBL_DIG - nd;
  2045. if (e <= Ten_pmax + i) {
  2046. /* A fancier test would sometimes let us do
  2047. * this for larger i values.
  2048. */
  2049. e -= i;
  2050. value(rv) *= tens[i];
  2051. #ifdef VAX
  2052. /* VAX exponent range is so narrow we must
  2053. * worry about overflow here...
  2054. */
  2055. vax_ovfl_check:
  2056. word0(rv) -= P*Exp_msk1;
  2057. /* value(rv) = */ rounded_product(value(rv),
  2058. tens[e]);
  2059. if ((word0(rv) & Exp_mask)
  2060. > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  2061. goto ovfl;
  2062. word0(rv) += P*Exp_msk1;
  2063. #else
  2064. /* value(rv) = */ rounded_product(value(rv),
  2065. tens[e]);
  2066. #endif
  2067. goto ret;
  2068. }
  2069. }
  2070. #ifndef Inaccurate_Divide
  2071. else if (e >= -Ten_pmax) {
  2072. /* value(rv) = */ rounded_quotient(value(rv),
  2073. tens[-e]);
  2074. goto ret;
  2075. }
  2076. #endif
  2077. }
  2078. e1 += nd - k;
  2079. /* Get starting approximation = rv * 10**e1 */
  2080. if (e1 > 0) {
  2081. if ((i = e1 & 15))
  2082. value(rv) *= tens[i];
  2083. if (e1 &= ~15) {
  2084. if (e1 > DBL_MAX_10_EXP) {
  2085. ovfl:
  2086. errno = ERANGE;
  2087. #ifndef Bad_float_h
  2088. value(rv) = HUGE_VAL;
  2089. #else
  2090. /* Can't trust HUGE_VAL */
  2091. #ifdef IEEE_Arith
  2092. word0(rv) = Exp_mask;
  2093. word1(rv) = 0;
  2094. #else
  2095. word0(rv) = Big0;
  2096. word1(rv) = Big1;
  2097. #endif
  2098. #endif
  2099. if (bd0)
  2100. goto retfree;
  2101. goto ret;
  2102. }
  2103. if (e1 >>= 4) {
  2104. for(j = 0; e1 > 1; j++, e1 >>= 1)
  2105. if (e1 & 1)
  2106. value(rv) *= bigtens[j];
  2107. /* The last multiplication could overflow. */
  2108. word0(rv) -= P*Exp_msk1;
  2109. value(rv) *= bigtens[j];
  2110. if ((z = word0(rv) & Exp_mask)
  2111. > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  2112. goto ovfl;
  2113. if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  2114. /* set to largest number */
  2115. /* (Can't trust DBL_MAX) */
  2116. word0(rv) = Big0;
  2117. word1(rv) = Big1;
  2118. }
  2119. else
  2120. word0(rv) += P*Exp_msk1;
  2121. }
  2122. }
  2123. }
  2124. else if (e1 < 0) {
  2125. e1 = -e1;
  2126. if ((i = e1 & 15))
  2127. value(rv) /= tens[i];
  2128. if (e1 &= ~15) {
  2129. e1 >>= 4;
  2130. if (e1 >= 1 << n_bigtens)
  2131. goto undfl;
  2132. for(j = 0; e1 > 1; j++, e1 >>= 1)
  2133. if (e1 & 1)
  2134. value(rv) *= tinytens[j];
  2135. /* The last multiplication could underflow. */
  2136. value(rv0) = value(rv);
  2137. value(rv) *= tinytens[j];
  2138. if (!value(rv)) {
  2139. value(rv) = 2.*value(rv0);
  2140. value(rv) *= tinytens[j];
  2141. if (!value(rv)) {
  2142. undfl:
  2143. value(rv) = 0.;
  2144. errno = ERANGE;
  2145. if (bd0)
  2146. goto retfree;
  2147. goto ret;
  2148. }
  2149. word0(rv) = Tiny0;
  2150. word1(rv) = Tiny1;
  2151. /* The refinement below will clean
  2152. * this approximation up.
  2153. */
  2154. }
  2155. }
  2156. }
  2157. /* Now the hard part -- adjusting rv to the correct value.*/
  2158. /* Put digits into bd: true value = bd * 10^e */
  2159. bd0 = s2b(s0, nd0, nd, y);
  2160. for(;;) {
  2161. bd = Balloc(bd0->k);
  2162. Bcopy(bd, bd0);
  2163. bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
  2164. bs = i2b(1);
  2165. if (e >= 0) {
  2166. bb2 = bb5 = 0;
  2167. bd2 = bd5 = e;
  2168. }
  2169. else {
  2170. bb2 = bb5 = -e;
  2171. bd2 = bd5 = 0;
  2172. }
  2173. if (bbe >= 0)
  2174. bb2 += bbe;
  2175. else
  2176. bd2 -= bbe;
  2177. bs2 = bb2;
  2178. #ifdef Sudden_Underflow
  2179. #ifdef IBM
  2180. j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  2181. #else
  2182. j = P + 1 - bbbits;
  2183. #endif
  2184. #else
  2185. i = bbe + bbbits - 1; /* logb(rv) */
  2186. if (i < Emin) /* denormal */
  2187. j = bbe + (P-Emin);
  2188. else
  2189. j = P + 1 - bbbits;
  2190. #endif
  2191. bb2 += j;
  2192. bd2 += j;
  2193. i = bb2 < bd2 ? bb2 : bd2;
  2194. if (i > bs2)
  2195. i = bs2;
  2196. if (i > 0) {
  2197. bb2 -= i;
  2198. bd2 -= i;
  2199. bs2 -= i;
  2200. }
  2201. if (bb5 > 0) {
  2202. bs = pow5mult(bs, bb5);
  2203. bb1 = mult(bs, bb);
  2204. Bfree(bb);
  2205. bb = bb1;
  2206. }
  2207. if (bb2 > 0)
  2208. bb = lshift(bb, bb2);
  2209. if (bd5 > 0)
  2210. bd = pow5mult(bd, bd5);
  2211. if (bd2 > 0)
  2212. bd = lshift(bd, bd2);
  2213. if (bs2 > 0)
  2214. bs = lshift(bs, bs2);
  2215. delta = diff(bb, bd);
  2216. dsign = delta->sign;
  2217. delta->sign = 0;
  2218. i = cmp(delta, bs);
  2219. if (i < 0) {
  2220. /* Error is less than half an ulp -- check for
  2221. * special case of mantissa a power of two.
  2222. */
  2223. if (dsign || word1(rv) || word0(rv) & Bndry_mask)
  2224. break;
  2225. delta = lshift(delta,Log2P);
  2226. if (cmp(delta, bs) > 0)
  2227. goto drop_down;
  2228. break;
  2229. }
  2230. if (i == 0) {
  2231. /* exactly half-way between */
  2232. if (dsign) {
  2233. if ((word0(rv) & Bndry_mask1) == Bndry_mask1
  2234. && word1(rv) == 0xffffffff) {
  2235. /*boundary case -- increment exponent*/
  2236. word0(rv) = (word0(rv) & Exp_mask)
  2237. + Exp_msk1
  2238. #ifdef IBM
  2239. | Exp_msk1 >> 4
  2240. #endif
  2241. ;
  2242. word1(rv) = 0;
  2243. break;
  2244. }
  2245. }
  2246. else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
  2247. drop_down:
  2248. /* boundary case -- decrement exponent */
  2249. #ifdef Sudden_Underflow
  2250. L = word0(rv) & Exp_mask;
  2251. #ifdef IBM
  2252. if (L < Exp_msk1)
  2253. #else
  2254. if (L <= Exp_msk1)
  2255. #endif
  2256. goto undfl;
  2257. L -= Exp_msk1;
  2258. #else
  2259. L = (word0(rv) & Exp_mask) - Exp_msk1;
  2260. #endif
  2261. word0(rv) = L | Bndry_mask1;
  2262. word1(rv) = 0xffffffff;
  2263. #ifdef IBM
  2264. goto cont;
  2265. #else
  2266. break;
  2267. #endif
  2268. }
  2269. #ifndef ROUND_BIASED
  2270. if (!(word1(rv) & LSB))
  2271. break;
  2272. #endif
  2273. if (dsign)
  2274. value(rv) += ulp(value(rv));
  2275. #ifndef ROUND_BIASED
  2276. else {
  2277. value(rv) -= ulp(value(rv));
  2278. #ifndef Sudden_Underflow
  2279. if (!value(rv))
  2280. goto undfl;
  2281. #endif
  2282. }
  2283. #endif
  2284. break;
  2285. }
  2286. if ((aadj = ratio(delta, bs)) <= 2.) {
  2287. if (dsign)
  2288. aadj = aadj1 = 1.;
  2289. else if (word1(rv) || word0(rv) & Bndry_mask) {
  2290. #ifndef Sudden_Underflow
  2291. if (word1(rv) == Tiny1 && !word0(rv))
  2292. goto undfl;
  2293. #endif
  2294. aadj = 1.;
  2295. aadj1 = -1.;
  2296. }
  2297. else {
  2298. /* special case -- power of FLT_RADIX to be */
  2299. /* rounded down... */
  2300. if (aadj < 2./FLT_RADIX)
  2301. aadj = 1./FLT_RADIX;
  2302. else
  2303. aadj *= 0.5;
  2304. aadj1 = -aadj;
  2305. }
  2306. }
  2307. else {
  2308. aadj *= 0.5;
  2309. aadj1 = dsign ? aadj : -aadj;
  2310. #ifdef Check_FLT_ROUNDS
  2311. switch(FLT_ROUNDS) {
  2312. case 2: /* towards +infinity */
  2313. aadj1 -= 0.5;
  2314. break;
  2315. case 0: /* towards 0 */
  2316. case 3: /* towards -infinity */
  2317. aadj1 += 0.5;
  2318. }
  2319. #else
  2320. if (FLT_ROUNDS == 0)
  2321. aadj1 += 0.5;
  2322. #endif
  2323. }
  2324. y = word0(rv) & Exp_mask;
  2325. /* Check for overflow */
  2326. if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  2327. value(rv0) = value(rv);
  2328. word0(rv) -= P*Exp_msk1;
  2329. adj = aadj1 * ulp(value(rv));
  2330. value(rv) += adj;
  2331. if ((word0(rv) & Exp_mask) >=
  2332. Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  2333. if (word0(rv0) == Big0 && word1(rv0) == Big1)
  2334. goto ovfl;
  2335. word0(rv) = Big0;
  2336. word1(rv) = Big1;
  2337. goto cont;
  2338. }
  2339. else
  2340. word0(rv) += P*Exp_msk1;
  2341. }
  2342. else {
  2343. #ifdef Sudden_Underflow
  2344. if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
  2345. value(rv0) = value(rv);
  2346. word0(rv) += P*Exp_msk1;
  2347. adj = aadj1 * ulp(value(rv));
  2348. value(rv) += adj;
  2349. #ifdef IBM
  2350. if ((word0(rv) & Exp_mask) < P*Exp_msk1)
  2351. #else
  2352. if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
  2353. #endif
  2354. {
  2355. if (word0(rv0) == Tiny0
  2356. && word1(rv0) == Tiny1)
  2357. goto undfl;
  2358. word0(rv) = Tiny0;
  2359. word1(rv) = Tiny1;
  2360. goto cont;
  2361. }
  2362. else
  2363. word0(rv) -= P*Exp_msk1;
  2364. }
  2365. else {
  2366. adj = aadj1 * ulp(value(rv));
  2367. value(rv) += adj;
  2368. }
  2369. #else
  2370. /* Compute adj so that the IEEE rounding rules will
  2371. * correctly round rv + adj in some half-way cases.
  2372. * If rv * ulp(rv) is denormalized (i.e.,
  2373. * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  2374. * trouble from bits lost to denormalization;
  2375. * example: 1.2e-307 .
  2376. */
  2377. if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
  2378. aadj1 = (double)(int)(aadj + 0.5);
  2379. if (!dsign)
  2380. aadj1 = -aadj1;
  2381. }
  2382. adj = aadj1 * ulp(value(rv));
  2383. value(rv) += adj;
  2384. #endif
  2385. }
  2386. z = word0(rv) & Exp_mask;
  2387. if (y == z) {
  2388. /* Can we stop now? */
  2389. L = aadj;
  2390. aadj -= L;
  2391. /* The tolerances below are conservative. */
  2392. if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
  2393. if (aadj < .4999999 || aadj > .5000001)
  2394. break;
  2395. }
  2396. else if (aadj < .4999999/FLT_RADIX)
  2397. break;
  2398. }
  2399. cont:
  2400. Bfree(bb);
  2401. Bfree(bd);
  2402. Bfree(bs);
  2403. Bfree(delta);
  2404. }
  2405. retfree:
  2406. Bfree(bb);
  2407. Bfree(bd);
  2408. Bfree(bs);
  2409. Bfree(bd0);
  2410. Bfree(delta);
  2411. ret:
  2412. if (se)
  2413. *se = s;
  2414. result = sign ? -value(rv) : value(rv);
  2415. _THREAD_PRIVATE_MUTEX_LOCK(pow5mult_mutex);
  2416. while (p5s) {
  2417. tmp = p5s;
  2418. p5s = p5s->next;
  2419. free(tmp);
  2420. }
  2421. _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult_mutex);
  2422. return result;
  2423. }
  2424. ZEND_API double zend_hex_strtod(const char *str, const char **endptr)
  2425. {
  2426. const char *s = str;
  2427. char c;
  2428. int any = 0;
  2429. double value = 0;
  2430. if (s[0] == '\0' || s[1] == '\0') {
  2431. *endptr = str;
  2432. return 0.0;
  2433. }
  2434. if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
  2435. s += 2;
  2436. }
  2437. while ((c = *s++)) {
  2438. if (c >= '0' && c <= '9') {
  2439. c -= '0';
  2440. } else if (c >= 'A' && c <= 'F') {
  2441. c -= 'A' - 10;
  2442. } else if (c >= 'a' && c <= 'f') {
  2443. c -= 'a' - 10;
  2444. } else {
  2445. break;
  2446. }
  2447. any = 1;
  2448. value = value * 16 + c;
  2449. }
  2450. if (endptr != NULL) {
  2451. *endptr = any ? s - 1 : str;
  2452. }
  2453. return value;
  2454. }
  2455. ZEND_API double zend_oct_strtod(const char *str, const char **endptr)
  2456. {
  2457. const char *s = str;
  2458. char c;
  2459. double value = 0;
  2460. int any = 0;
  2461. if (strlen(str) < 1) {
  2462. *endptr = str;
  2463. return 0.0;
  2464. }
  2465. /* skip leading zero */
  2466. s++;
  2467. while ((c = *s++)) {
  2468. if (c < '0' || c > '7') {
  2469. /* break and return the current value if the number is not well-formed
  2470. * that's what Linux strtol() does
  2471. */
  2472. break;
  2473. }
  2474. value = value * 8 + c - '0';
  2475. any = 1;
  2476. }
  2477. if (endptr != NULL) {
  2478. *endptr = any ? s - 1 : str;
  2479. }
  2480. return value;
  2481. }
  2482. ZEND_API double zend_bin_strtod(const char *str, const char **endptr)
  2483. {
  2484. const char *s = str;
  2485. char c;
  2486. double value = 0;
  2487. int any = 0;
  2488. if (strlen(str) < 2) {
  2489. *endptr = str;
  2490. return 0.0;
  2491. }
  2492. if ('0' == *s && ('b' == s[1] || 'B' == s[1])) {
  2493. s += 2;
  2494. }
  2495. while ((c = *s++)) {
  2496. /*
  2497. * Verify the validity of the current character as a base-2 digit. In
  2498. * the event that an invalid digit is found, halt the conversion and
  2499. * return the portion which has been converted thus far.
  2500. */
  2501. if ('0' == c || '1' == c)
  2502. value = value * 2 + c - '0';
  2503. else
  2504. break;
  2505. any = 1;
  2506. }
  2507. /*
  2508. * As with many strtoX implementations, should the subject sequence be
  2509. * empty or not well-formed, no conversion is performed and the original
  2510. * value of str is stored in *endptr, provided that endptr is not a null
  2511. * pointer.
  2512. */
  2513. if (NULL != endptr) {
  2514. *endptr = (char *)(any ? s - 1 : str);
  2515. }
  2516. return value;
  2517. }
  2518. /*
  2519. * Local variables:
  2520. * tab-width: 4
  2521. * c-basic-offset: 4
  2522. * End:
  2523. * vim600: sw=4 ts=4 fdm=marker
  2524. * vim<600: sw=4 ts=4
  2525. */