float.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "strconv"
  6. )
  7. // func parseFloat(b []byte, bitsize int) (f float64, err error) {
  8. // if bitsize == 32 {
  9. // return parseFloat32(b)
  10. // } else {
  11. // return parseFloat64(b)
  12. // }
  13. // }
  14. func parseFloat32(b []byte) (f float32, err error) {
  15. return parseFloat32_custom(b)
  16. // return parseFloat32_strconv(b)
  17. }
  18. func parseFloat64(b []byte) (f float64, err error) {
  19. return parseFloat64_custom(b)
  20. // return parseFloat64_strconv(b)
  21. }
  22. func parseFloat32_strconv(b []byte) (f float32, err error) {
  23. f64, err := strconv.ParseFloat(stringView(b), 32)
  24. f = float32(f64)
  25. return
  26. }
  27. func parseFloat64_strconv(b []byte) (f float64, err error) {
  28. return strconv.ParseFloat(stringView(b), 64)
  29. }
  30. // ------ parseFloat custom below --------
  31. // We assume that a lot of floating point numbers in json files will be
  32. // those that are handwritten, and with defined precision (in terms of number
  33. // of digits after decimal point), etc.
  34. //
  35. // We further assume that this ones can be written in exact format.
  36. //
  37. // strconv.ParseFloat has some unnecessary overhead which we can do without
  38. // for the common case:
  39. //
  40. // - expensive char-by-char check to see if underscores are in right place
  41. // - testing for and skipping underscores
  42. // - check if the string matches ignorecase +/- inf, +/- infinity, nan
  43. // - support for base 16 (0xFFFF...)
  44. //
  45. // The functions below will try a fast-path for floats which can be decoded
  46. // without any loss of precision, meaning they:
  47. //
  48. // - fits within the significand bits of the 32-bits or 64-bits
  49. // - exponent fits within the exponent value
  50. // - there is no truncation (any extra numbers are all trailing zeros)
  51. //
  52. // To figure out what the values are for maxMantDigits, use this idea below:
  53. //
  54. // 2^23 = 838 8608 (between 10^ 6 and 10^ 7) (significand bits of uint32)
  55. // 2^32 = 42 9496 7296 (between 10^ 9 and 10^10) (full uint32)
  56. // 2^52 = 4503 5996 2737 0496 (between 10^15 and 10^16) (significand bits of uint64)
  57. // 2^64 = 1844 6744 0737 0955 1616 (between 10^19 and 10^20) (full uint64)
  58. //
  59. // Note: we only allow for up to what can comfortably fit into the significand
  60. // ignoring the exponent, and we only try to parse iff significand fits.
  61. const (
  62. thousand = 1000
  63. million = thousand * thousand
  64. billion = thousand * million
  65. trillion = thousand * billion
  66. quadrillion = thousand * trillion
  67. quintillion = thousand * quadrillion
  68. )
  69. // Exact powers of 10.
  70. var uint64pow10 = [...]uint64{
  71. 1, 10, 100,
  72. 1 * thousand, 10 * thousand, 100 * thousand,
  73. 1 * million, 10 * million, 100 * million,
  74. 1 * billion, 10 * billion, 100 * billion,
  75. 1 * trillion, 10 * trillion, 100 * trillion,
  76. 1 * quadrillion, 10 * quadrillion, 100 * quadrillion,
  77. 1 * quintillion, 10 * quintillion,
  78. }
  79. var float64pow10 = [...]float64{
  80. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  81. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  82. 1e20, 1e21, 1e22,
  83. }
  84. var float32pow10 = [...]float32{
  85. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10,
  86. }
  87. type floatinfo struct {
  88. mantbits uint8
  89. // expbits uint8 // (unused)
  90. // bias int16 // (unused)
  91. is32bit bool
  92. exactPow10 int8 // Exact powers of ten are <= 10^N (32: 10, 64: 22)
  93. exactInts int8 // Exact integers are <= 10^N (for non-float, set to 0)
  94. // maxMantDigits int8 // 10^19 fits in uint64, while 10^9 fits in uint32
  95. // mantCutoff uint64
  96. }
  97. // var fi32 = floatinfo{23, 8, -127, 10, 7, 9, fUint32Cutoff}
  98. // var fi64 = floatinfo{52, 11, -1023, 22, 15, 19, fUint64Cutoff}
  99. // var fi64u = floatinfo{64, 0, -1023, 19, 0, 19, fUint64Cutoff}
  100. // var fi64i = floatinfo{63, 0, -1023, 19, 0, 19, fUint64Cutoff}
  101. var fi32 = floatinfo{23, true, 10, 7}
  102. var fi64 = floatinfo{52, false, 22, 15}
  103. var fi64u = floatinfo{0, false, 19, 0}
  104. var fi64i = floatinfo{0, false, 19, 0}
  105. const fMaxMultiplierForExactPow10_64 = 1e15
  106. const fMaxMultiplierForExactPow10_32 = 1e7
  107. const fUint64Cutoff = (1<<64-1)/10 + 1
  108. const fUint32Cutoff = (1<<32-1)/10 + 1
  109. const fBase = 10
  110. func strconvParseErr(b []byte, fn string) error {
  111. return &strconv.NumError{
  112. Func: fn,
  113. Err: strconv.ErrSyntax,
  114. Num: string(b),
  115. }
  116. }
  117. func parseFloat32_reader(r readFloatResult) (f float32, fail bool) {
  118. // parseFloatDebug(b, 32, false, exp, trunc, ok)
  119. f = float32(r.mantissa)
  120. if r.exp != 0 {
  121. if r.exp < 0 { // int / 10^k
  122. f /= float32pow10[uint8(-r.exp)]
  123. } else { // exp > 0
  124. if r.exp > fi32.exactPow10 {
  125. f *= float32pow10[r.exp-fi32.exactPow10]
  126. if f > fMaxMultiplierForExactPow10_32 { // exponent too large - outside range
  127. fail = true
  128. return // ok = false
  129. }
  130. f *= float32pow10[fi32.exactPow10]
  131. } else {
  132. f *= float32pow10[uint8(r.exp)]
  133. }
  134. }
  135. }
  136. if r.neg {
  137. f = -f
  138. }
  139. return
  140. }
  141. func parseFloat32_custom(b []byte) (f float32, err error) {
  142. r := readFloat(b, fi32)
  143. if r.bad {
  144. return 0, strconvParseErr(b, "ParseFloat")
  145. }
  146. if r.ok {
  147. f, r.bad = parseFloat32_reader(r)
  148. if r.bad {
  149. goto FALLBACK
  150. }
  151. return
  152. }
  153. FALLBACK:
  154. return parseFloat32_strconv(b)
  155. }
  156. func parseFloat64_reader(r readFloatResult) (f float64, fail bool) {
  157. f = float64(r.mantissa)
  158. if r.exp != 0 {
  159. if r.exp < 0 { // int / 10^k
  160. f /= float64pow10[-uint8(r.exp)]
  161. } else { // exp > 0
  162. if r.exp > fi64.exactPow10 {
  163. f *= float64pow10[r.exp-fi64.exactPow10]
  164. if f > fMaxMultiplierForExactPow10_64 { // exponent too large - outside range
  165. fail = true
  166. return
  167. }
  168. f *= float64pow10[fi64.exactPow10]
  169. } else {
  170. f *= float64pow10[uint8(r.exp)]
  171. }
  172. }
  173. }
  174. if r.neg {
  175. f = -f
  176. }
  177. return
  178. }
  179. func parseFloat64_custom(b []byte) (f float64, err error) {
  180. r := readFloat(b, fi64)
  181. if r.bad {
  182. return 0, strconvParseErr(b, "ParseFloat")
  183. }
  184. if r.ok {
  185. f, r.bad = parseFloat64_reader(r)
  186. if r.bad {
  187. goto FALLBACK
  188. }
  189. return
  190. }
  191. FALLBACK:
  192. return parseFloat64_strconv(b)
  193. }
  194. func parseUint64_simple(b []byte) (n uint64, ok bool) {
  195. var i int
  196. var n1 uint64
  197. var c uint8
  198. LOOP:
  199. if i < len(b) {
  200. c = b[i]
  201. if c < '0' || c > '9' {
  202. return
  203. }
  204. // unsigned integers don't overflow well on multiplication, so check cutoff here
  205. // e.g. (maxUint64-5)*10 doesn't overflow well ...
  206. if n >= fUint64Cutoff {
  207. return
  208. }
  209. n *= fBase
  210. n1 = n + uint64(c-'0')
  211. if n1 < n {
  212. return
  213. }
  214. n = n1
  215. i++
  216. goto LOOP
  217. }
  218. ok = true
  219. return
  220. }
  221. func parseUint64_reader(r readFloatResult) (f uint64, fail bool) {
  222. f = r.mantissa
  223. if r.exp != 0 {
  224. if r.exp < 0 { // int / 10^k
  225. if f%uint64pow10[uint8(-r.exp)] != 0 {
  226. fail = true
  227. } else {
  228. f /= uint64pow10[uint8(-r.exp)]
  229. }
  230. } else { // exp > 0
  231. f *= uint64pow10[uint8(r.exp)]
  232. }
  233. }
  234. return
  235. }
  236. func parseInt64_reader(r readFloatResult) (v int64, fail bool) {
  237. // xdebugf("parseint64_reader: r: %#v", r)
  238. if r.exp == 0 {
  239. } else if r.exp < 0 { // int / 10^k
  240. if r.mantissa%uint64pow10[uint8(-r.exp)] != 0 {
  241. // fail = true
  242. return 0, true
  243. }
  244. r.mantissa /= uint64pow10[uint8(-r.exp)]
  245. } else { // exp > 0
  246. r.mantissa *= uint64pow10[uint8(r.exp)]
  247. }
  248. if chkOvf.Uint2Int(r.mantissa, r.neg) {
  249. fail = true
  250. } else if r.neg {
  251. v = -int64(r.mantissa)
  252. } else {
  253. v = int64(r.mantissa)
  254. }
  255. return
  256. }
  257. // parseNumber will return an integer if only composed of [-]?[0-9]+
  258. // Else it will return a float.
  259. func parseNumber(b []byte, z *fauxUnion, preferSignedInt bool) (err error) {
  260. var ok, neg bool
  261. var f uint64
  262. // var b1 []byte
  263. // if b[0] == '-' {
  264. // neg = true
  265. // b1 = b[1:]
  266. // } else {
  267. // b1 = b
  268. // }
  269. // f, ok = parseUint64_simple(b1)
  270. if b[0] == '-' {
  271. neg = true
  272. f, ok = parseUint64_simple(b[1:])
  273. } else {
  274. f, ok = parseUint64_simple(b)
  275. }
  276. if ok {
  277. if neg {
  278. z.v = valueTypeInt
  279. if chkOvf.Uint2Int(f, neg) {
  280. return strconvParseErr(b, "ParseInt")
  281. }
  282. z.i = -int64(f)
  283. } else if preferSignedInt {
  284. z.v = valueTypeInt
  285. if chkOvf.Uint2Int(f, neg) {
  286. return strconvParseErr(b, "ParseInt")
  287. }
  288. z.i = int64(f)
  289. } else {
  290. z.v = valueTypeUint
  291. z.u = f
  292. }
  293. return
  294. }
  295. z.v = valueTypeFloat
  296. z.f, err = parseFloat64_custom(b)
  297. return
  298. }
  299. type readFloatResult struct {
  300. mantissa uint64
  301. exp int8
  302. neg, sawdot, sawexp, trunc, bad bool
  303. ok bool
  304. }
  305. func readFloat(s []byte, y floatinfo) (r readFloatResult) {
  306. // defer func() { xdebugf("readFloat: %#v", r) }()
  307. var i uint // uint, so that we eliminate bounds checking
  308. var slen = uint(len(s))
  309. if slen == 0 {
  310. // read an empty string as the zero value
  311. // r.bad = true
  312. r.ok = true
  313. return
  314. }
  315. if s[0] == '-' {
  316. r.neg = true
  317. i++
  318. }
  319. // we considered punting early if string has length > maxMantDigits, but this doesn't account
  320. // for trailing 0's e.g. 700000000000000000000 can be encoded exactly as it is 7e20
  321. var mantCutoffIsUint64Cutoff bool
  322. var mantCutoff uint64
  323. if y.mantbits != 0 {
  324. mantCutoff = 1<<y.mantbits - 1
  325. } else if y.is32bit {
  326. mantCutoff = fUint32Cutoff
  327. } else {
  328. mantCutoffIsUint64Cutoff = true
  329. mantCutoff = fUint64Cutoff
  330. }
  331. var nd, ndMant, dp int8
  332. var xu uint64
  333. var c uint8
  334. for ; i < slen; i++ {
  335. c = s[i]
  336. if c == '.' {
  337. if r.sawdot {
  338. r.bad = true
  339. return
  340. }
  341. r.sawdot = true
  342. dp = nd
  343. } else if c == 'e' || c == 'E' {
  344. r.sawexp = true
  345. break
  346. } else if c == '0' {
  347. if nd == 0 {
  348. dp--
  349. continue
  350. }
  351. nd++
  352. if r.mantissa < mantCutoff {
  353. r.mantissa *= fBase
  354. ndMant++
  355. }
  356. } else if c >= '1' && c <= '9' { // !(c < '0' || c > '9') { //
  357. nd++
  358. if mantCutoffIsUint64Cutoff && r.mantissa < fUint64Cutoff {
  359. // mantissa = (mantissa << 1) + (mantissa << 3) + uint64(c-'0')
  360. r.mantissa *= fBase
  361. xu = r.mantissa + uint64(c-'0')
  362. if xu < r.mantissa {
  363. r.trunc = true
  364. return
  365. }
  366. r.mantissa = xu
  367. ndMant++
  368. } else if r.mantissa < mantCutoff {
  369. // mantissa = (mantissa << 1) + (mantissa << 3) + uint64(c-'0')
  370. r.mantissa = r.mantissa*fBase + uint64(c-'0')
  371. ndMant++
  372. } else {
  373. r.trunc = true
  374. return
  375. }
  376. } else {
  377. r.bad = true
  378. return
  379. }
  380. }
  381. if !r.sawdot {
  382. dp = nd
  383. }
  384. if r.sawexp {
  385. i++
  386. if i < slen {
  387. var eneg bool
  388. if s[i] == '+' {
  389. i++
  390. } else if s[i] == '-' {
  391. i++
  392. eneg = true
  393. }
  394. if i < slen {
  395. // for exact match, exponent is 1 or 2 digits (float64: -22 to 37, float32: -1 to 17).
  396. // exit quick if exponent is more than 2 digits.
  397. if i+2 < slen {
  398. return
  399. }
  400. var e int8
  401. if s[i] < '0' || s[i] > '9' {
  402. r.bad = true
  403. return
  404. }
  405. e = int8(s[i] - '0')
  406. i++
  407. if i < slen {
  408. if s[i] < '0' || s[i] > '9' {
  409. r.bad = true
  410. return
  411. }
  412. e = e*fBase + int8(s[i]-'0') // (e << 1) + (e << 3) + int8(s[i]-'0')
  413. i++
  414. }
  415. if eneg {
  416. dp -= e
  417. } else {
  418. dp += e
  419. }
  420. }
  421. }
  422. }
  423. if r.mantissa != 0 {
  424. r.exp = dp - ndMant
  425. // do not set ok=true for cases we cannot handle
  426. if r.exp < -y.exactPow10 ||
  427. r.exp > y.exactInts+y.exactPow10 ||
  428. y.mantbits != 0 && r.mantissa>>y.mantbits != 0 {
  429. return
  430. }
  431. }
  432. r.ok = true
  433. return
  434. }
  435. /*
  436. func parseUint64(b []byte) (f uint64, err error) {
  437. if b[0] == '-' {
  438. return 0, strconvParseErr(b, "ParseUint")
  439. }
  440. f, ok := parseUint64_simple(b)
  441. if !ok {
  442. return parseUint64_custom(b)
  443. }
  444. return
  445. }
  446. func parseInt64(b []byte) (v int64, err error) {
  447. // defer func() { xdebug2f("parseInt64: %s, return: %d, %v", b, v, err) }()
  448. // xdebugf("parseInt64: %s", b)
  449. var ok, neg bool
  450. var f uint64
  451. var r readFloatResult
  452. if b[0] == '-' {
  453. neg = true
  454. b = b[1:]
  455. }
  456. f, ok = parseUint64_simple(b)
  457. // xdebugf("parseuint64_simple: %v, %v", f, ok)
  458. if ok {
  459. if chkOvf.Uint2Int(f, neg) {
  460. goto ERROR
  461. }
  462. if neg {
  463. v = -int64(f)
  464. } else {
  465. v = int64(f)
  466. }
  467. return
  468. }
  469. r = readFloat(b, fi64i)
  470. // xdebugf("readFloat ok: %v", r.ok)
  471. if r.ok {
  472. r.neg = neg
  473. v, r.bad = parseInt64_reader(r)
  474. if r.bad {
  475. goto ERROR
  476. }
  477. return
  478. }
  479. ERROR:
  480. err = strconvParseErr(b, "ParseInt")
  481. return
  482. }
  483. func parseUint64_custom(b []byte) (f uint64, err error) {
  484. r := readFloat(b, fi64u)
  485. if r.ok {
  486. f, r.bad = parseUint64_reader(r)
  487. if r.bad {
  488. err = strconvParseErr(b, "ParseUint")
  489. }
  490. return
  491. }
  492. err = strconvParseErr(b, "ParseUint")
  493. return
  494. }
  495. func parseUint64_simple(b []byte) (n uint64, ok bool) {
  496. for _, c := range b {
  497. if c < '0' || c > '9' {
  498. return
  499. }
  500. // unsigned integers don't overflow well on multiplication, so check cutoff here
  501. // e.g. (maxUint64-5)*10 doesn't overflow well ...
  502. if n >= uint64Cutoff {
  503. return
  504. }
  505. n *= 10
  506. n1 := n + uint64(c-'0')
  507. if n1 < n {
  508. return
  509. }
  510. n = n1
  511. }
  512. ok = true
  513. return
  514. }
  515. */