schubfach.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. ## Copyright 2020 Alexander Bolz
  2. ##
  3. ## Distributed under the Boost Software License, Version 1.0.
  4. ## (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
  5. ## --------------------------------------------------------------------------------------------------
  6. ## This file contains an implementation of the Schubfach algorithm as described in
  7. ##
  8. ## [1] Raffaello Giulietti, "The Schubfach way to render doubles",
  9. ## https://drive.google.com/open?id=1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN
  10. ## --------------------------------------------------------------------------------------------------
  11. import std/private/digitsutils
  12. template sf_Assert(x: untyped): untyped =
  13. assert(x)
  14. ## ==================================================================================================
  15. ##
  16. ## ==================================================================================================
  17. type
  18. ValueType = float32
  19. BitsType = uint32
  20. Single {.bycopy.} = object
  21. bits: BitsType
  22. const
  23. significandSize: int32 = 24
  24. MaxExponent = 128
  25. exponentBias: int32 = MaxExponent - 1 + (significandSize - 1)
  26. maxIeeeExponent: BitsType = BitsType(2 * MaxExponent - 1)
  27. hiddenBit: BitsType = BitsType(1) shl (significandSize - 1)
  28. significandMask: BitsType = hiddenBit - 1
  29. exponentMask: BitsType = maxIeeeExponent shl (significandSize - 1)
  30. signMask: BitsType = not (not BitsType(0) shr 1)
  31. proc constructSingle(bits: BitsType): Single {.constructor.} =
  32. result.bits = bits
  33. proc constructSingle(value: ValueType): Single {.constructor.} =
  34. result.bits = cast[typeof(result.bits)](value)
  35. proc physicalSignificand(this: Single): BitsType {.noSideEffect.} =
  36. return this.bits and significandMask
  37. proc physicalExponent(this: Single): BitsType {.noSideEffect.} =
  38. return (this.bits and exponentMask) shr (significandSize - 1)
  39. proc isFinite(this: Single): bool {.noSideEffect.} =
  40. return (this.bits and exponentMask) != exponentMask
  41. proc isInf(this: Single): bool {.noSideEffect.} =
  42. return (this.bits and exponentMask) == exponentMask and
  43. (this.bits and significandMask) == 0
  44. proc isNaN(this: Single): bool {.noSideEffect.} =
  45. return (this.bits and exponentMask) == exponentMask and
  46. (this.bits and significandMask) != 0
  47. proc isZero(this: Single): bool {.noSideEffect.} =
  48. return (this.bits and not signMask) == 0
  49. proc signBit(this: Single): int {.noSideEffect.} =
  50. return int((this.bits and signMask) != 0)
  51. ## ==================================================================================================
  52. ## Returns floor(x / 2^n).
  53. ##
  54. ## Technically, right-shift of negative integers is implementation defined...
  55. ## Should easily be optimized into SAR (or equivalent) instruction.
  56. proc floorDivPow2(x: int32; n: int32): int32 {.inline.} =
  57. return x shr n
  58. ## Returns floor(log_10(2^e))
  59. ## static inline int32_t FloorLog10Pow2(int32_t e)
  60. ## {
  61. ## SF_ASSERT(e >= -1500);
  62. ## SF_ASSERT(e <= 1500);
  63. ## return FloorDivPow2(e * 1262611, 22);
  64. ## }
  65. ## Returns floor(log_10(3/4 2^e))
  66. ## static inline int32_t FloorLog10ThreeQuartersPow2(int32_t e)
  67. ## {
  68. ## SF_ASSERT(e >= -1500);
  69. ## SF_ASSERT(e <= 1500);
  70. ## return FloorDivPow2(e * 1262611 - 524031, 22);
  71. ## }
  72. ## Returns floor(log_2(10^e))
  73. proc floorLog2Pow10(e: int32): int32 {.inline.} =
  74. sf_Assert(e >= -1233)
  75. sf_Assert(e <= 1233)
  76. return floorDivPow2(e * 1741647, 19)
  77. const
  78. kMin: int32 = -31
  79. kMax: int32 = 45
  80. g: array[kMax - kMin + 1, uint64] = [0x81CEB32C4B43FCF5'u64, 0xA2425FF75E14FC32'u64,
  81. 0xCAD2F7F5359A3B3F'u64, 0xFD87B5F28300CA0E'u64, 0x9E74D1B791E07E49'u64,
  82. 0xC612062576589DDB'u64, 0xF79687AED3EEC552'u64, 0x9ABE14CD44753B53'u64,
  83. 0xC16D9A0095928A28'u64, 0xF1C90080BAF72CB2'u64, 0x971DA05074DA7BEF'u64,
  84. 0xBCE5086492111AEB'u64, 0xEC1E4A7DB69561A6'u64, 0x9392EE8E921D5D08'u64,
  85. 0xB877AA3236A4B44A'u64, 0xE69594BEC44DE15C'u64, 0x901D7CF73AB0ACDA'u64,
  86. 0xB424DC35095CD810'u64, 0xE12E13424BB40E14'u64, 0x8CBCCC096F5088CC'u64,
  87. 0xAFEBFF0BCB24AAFF'u64, 0xDBE6FECEBDEDD5BF'u64, 0x89705F4136B4A598'u64,
  88. 0xABCC77118461CEFD'u64, 0xD6BF94D5E57A42BD'u64, 0x8637BD05AF6C69B6'u64,
  89. 0xA7C5AC471B478424'u64, 0xD1B71758E219652C'u64, 0x83126E978D4FDF3C'u64,
  90. 0xA3D70A3D70A3D70B'u64, 0xCCCCCCCCCCCCCCCD'u64, 0x8000000000000000'u64,
  91. 0xA000000000000000'u64, 0xC800000000000000'u64, 0xFA00000000000000'u64,
  92. 0x9C40000000000000'u64, 0xC350000000000000'u64, 0xF424000000000000'u64,
  93. 0x9896800000000000'u64, 0xBEBC200000000000'u64, 0xEE6B280000000000'u64,
  94. 0x9502F90000000000'u64, 0xBA43B74000000000'u64, 0xE8D4A51000000000'u64,
  95. 0x9184E72A00000000'u64, 0xB5E620F480000000'u64, 0xE35FA931A0000000'u64,
  96. 0x8E1BC9BF04000000'u64, 0xB1A2BC2EC5000000'u64, 0xDE0B6B3A76400000'u64,
  97. 0x8AC7230489E80000'u64, 0xAD78EBC5AC620000'u64, 0xD8D726B7177A8000'u64,
  98. 0x878678326EAC9000'u64, 0xA968163F0A57B400'u64, 0xD3C21BCECCEDA100'u64,
  99. 0x84595161401484A0'u64, 0xA56FA5B99019A5C8'u64, 0xCECB8F27F4200F3A'u64,
  100. 0x813F3978F8940985'u64, 0xA18F07D736B90BE6'u64, 0xC9F2C9CD04674EDF'u64,
  101. 0xFC6F7C4045812297'u64, 0x9DC5ADA82B70B59E'u64, 0xC5371912364CE306'u64,
  102. 0xF684DF56C3E01BC7'u64, 0x9A130B963A6C115D'u64, 0xC097CE7BC90715B4'u64,
  103. 0xF0BDC21ABB48DB21'u64, 0x96769950B50D88F5'u64, 0xBC143FA4E250EB32'u64,
  104. 0xEB194F8E1AE525FE'u64, 0x92EFD1B8D0CF37BF'u64, 0xB7ABC627050305AE'u64,
  105. 0xE596B7B0C643C71A'u64, 0x8F7E32CE7BEA5C70'u64, 0xB35DBF821AE4F38C'u64]
  106. proc computePow10Single(k: int32): uint64 {.inline.} =
  107. ## There are unique beta and r such that 10^k = beta 2^r and
  108. ## 2^63 <= beta < 2^64, namely r = floor(log_2 10^k) - 63 and
  109. ## beta = 2^-r 10^k.
  110. ## Let g = ceil(beta), so (g-1) 2^r < 10^k <= g 2^r, with the latter
  111. ## value being a pretty good overestimate for 10^k.
  112. ## NB: Since for all the required exponents k, we have g < 2^64,
  113. ## all constants can be stored in 128-bit integers.
  114. sf_Assert(k >= kMin)
  115. sf_Assert(k <= kMax)
  116. return g[k - kMin]
  117. proc lo32(x: uint64): uint32 {.inline.} =
  118. return cast[uint32](x)
  119. proc hi32(x: uint64): uint32 {.inline.} =
  120. return cast[uint32](x shr 32)
  121. when defined(sizeof_Int128):
  122. proc roundToOdd(g: uint64; cp: uint32): uint32 {.inline.} =
  123. let p: uint128 = uint128(g) * cp
  124. let y1: uint32 = lo32(cast[uint64](p shr 64))
  125. let y0: uint32 = hi32(cast[uint64](p))
  126. return y1 or uint32(y0 > 1)
  127. elif defined(vcc) and defined(cpu64):
  128. proc umul128(x, y: uint64, z: ptr uint64): uint64 {.importc: "_umul128", header: "<intrin.h>".}
  129. proc roundToOdd(g: uint64; cpHi: uint32): uint32 {.inline.} =
  130. var p1: uint64 = 0
  131. var p0: uint64 = umul128(g, cpHi, addr(p1))
  132. let y1: uint32 = lo32(p1)
  133. let y0: uint32 = hi32(p0)
  134. return y1 or uint32(y0 > 1)
  135. else:
  136. proc roundToOdd(g: uint64; cp: uint32): uint32 {.inline.} =
  137. let b01: uint64 = uint64(lo32(g)) * cp
  138. let b11: uint64 = uint64(hi32(g)) * cp
  139. let hi: uint64 = b11 + hi32(b01)
  140. let y1: uint32 = hi32(hi)
  141. let y0: uint32 = lo32(hi)
  142. return y1 or uint32(y0 > 1)
  143. ## Returns whether value is divisible by 2^e2
  144. proc multipleOfPow2(value: uint32; e2: int32): bool {.inline.} =
  145. sf_Assert(e2 >= 0)
  146. sf_Assert(e2 <= 31)
  147. return (value and ((uint32(1) shl e2) - 1)) == 0
  148. type
  149. FloatingDecimal32 {.bycopy.} = object
  150. digits: uint32 ## num_digits <= 9
  151. exponent: int32
  152. proc toDecimal32(ieeeSignificand: uint32; ieeeExponent: uint32): FloatingDecimal32 {.
  153. inline.} =
  154. var c: uint32
  155. var q: int32
  156. if ieeeExponent != 0:
  157. c = hiddenBit or ieeeSignificand
  158. q = cast[int32](ieeeExponent) - exponentBias
  159. if 0 <= -q and -q < significandSize and multipleOfPow2(c, -q):
  160. return FloatingDecimal32(digits: c shr -q, exponent: 0'i32)
  161. else:
  162. c = ieeeSignificand
  163. q = 1 - exponentBias
  164. let isEven: bool = (c mod 2 == 0)
  165. let lowerBoundaryIsCloser: bool = (ieeeSignificand == 0 and ieeeExponent > 1)
  166. ## const int32_t qb = q - 2;
  167. let cbl: uint32 = 4 * c - 2 + uint32(lowerBoundaryIsCloser)
  168. let cb: uint32 = 4 * c
  169. let cbr: uint32 = 4 * c + 2
  170. ## (q * 1262611 ) >> 22 == floor(log_10( 2^q))
  171. ## (q * 1262611 - 524031) >> 22 == floor(log_10(3/4 2^q))
  172. sf_Assert(q >= -1500)
  173. sf_Assert(q <= 1500)
  174. let k: int32 = floorDivPow2(q * 1262611 - (if lowerBoundaryIsCloser: 524031 else: 0), 22)
  175. let h: int32 = q + floorLog2Pow10(-k) + 1
  176. sf_Assert(h >= 1)
  177. sf_Assert(h <= 4)
  178. let pow10: uint64 = computePow10Single(-k)
  179. let vbl: uint32 = roundToOdd(pow10, cbl shl h)
  180. let vb: uint32 = roundToOdd(pow10, cb shl h)
  181. let vbr: uint32 = roundToOdd(pow10, cbr shl h)
  182. let lower: uint32 = vbl + uint32(not isEven)
  183. let upper: uint32 = vbr - uint32(not isEven)
  184. ## See Figure 4 in [1].
  185. ## And the modifications in Figure 6.
  186. let s: uint32 = vb div 4
  187. ## NB: 4 * s == vb & ~3 == vb & -4
  188. if s >= 10:
  189. let sp: uint32 = s div 10
  190. ## = vb / 40
  191. let upInside: bool = lower <= 40 * sp
  192. let wpInside: bool = 40 * sp + 40 <= upper
  193. ## if (up_inside || wp_inside) // NB: At most one of u' and w' is in R_v.
  194. if upInside != wpInside:
  195. return FloatingDecimal32(digits: sp + uint32(wpInside), exponent: k + 1)
  196. let uInside: bool = lower <= 4 * s
  197. let wInside: bool = 4 * s + 4 <= upper
  198. if uInside != wInside:
  199. return FloatingDecimal32(digits: s + uint32(wInside), exponent: k)
  200. let mid: uint32 = 4 * s + 2
  201. ## = 2(s + t)
  202. let roundUp: bool = vb > mid or (vb == mid and (s and 1) != 0)
  203. return FloatingDecimal32(digits: s + uint32(roundUp), exponent: k)
  204. ## ==================================================================================================
  205. ## ToChars
  206. ## ==================================================================================================
  207. proc printDecimalDigitsBackwards(buf: var openArray[char]; pos: int; output: uint32): int32 {.inline.} =
  208. var output = output
  209. var pos = pos
  210. var tz: int32 = 0
  211. ## number of trailing zeros removed.
  212. var nd: int32 = 0
  213. ## number of decimal digits processed.
  214. ## At most 9 digits remaining
  215. if output >= 10000:
  216. let q: uint32 = output div 10000
  217. let r: uint32 = output mod 10000
  218. output = q
  219. dec(pos, 4)
  220. if r != 0:
  221. let rH: uint32 = r div 100
  222. let rL: uint32 = r mod 100
  223. utoa2Digits(buf, pos, rH)
  224. utoa2Digits(buf, pos + 2, rL)
  225. tz = trailingZeros2Digits(if rL == 0: rH else: rL) + (if rL == 0: 2 else: 0)
  226. else:
  227. tz = 4
  228. nd = 4
  229. if output >= 100:
  230. let q: uint32 = output div 100
  231. let r: uint32 = output mod 100
  232. output = q
  233. dec(pos, 2)
  234. utoa2Digits(buf, pos, r)
  235. if tz == nd:
  236. inc(tz, trailingZeros2Digits(r))
  237. inc(nd, 2)
  238. if output >= 100:
  239. let q2: uint32 = output div 100
  240. let r2: uint32 = output mod 100
  241. output = q2
  242. dec(pos, 2)
  243. utoa2Digits(buf, pos, r2)
  244. if tz == nd:
  245. inc(tz, trailingZeros2Digits(r2))
  246. inc(nd, 2)
  247. sf_Assert(output >= 1)
  248. sf_Assert(output <= 99)
  249. if output >= 10:
  250. let q: uint32 = output
  251. dec(pos, 2)
  252. utoa2Digits(buf, pos, q)
  253. if tz == nd:
  254. inc(tz, trailingZeros2Digits(q))
  255. else:
  256. let q: uint32 = output
  257. sf_Assert(q >= 1)
  258. sf_Assert(q <= 9)
  259. dec(pos)
  260. buf[pos] = chr(uint32('0') + q)
  261. return tz
  262. proc decimalLength(v: uint32): int32 {.inline.} =
  263. sf_Assert(v >= 1)
  264. sf_Assert(v <= 999999999'u)
  265. if v >= 100000000'u:
  266. return 9
  267. if v >= 10000000'u:
  268. return 8
  269. if v >= 1000000'u:
  270. return 7
  271. if v >= 100000'u:
  272. return 6
  273. if v >= 10000'u:
  274. return 5
  275. if v >= 1000'u:
  276. return 4
  277. if v >= 100'u:
  278. return 3
  279. if v >= 10'u:
  280. return 2
  281. return 1
  282. proc formatDigits(buffer: var openArray[char]; pos: int; digits: uint32; decimalExponent: int32;
  283. forceTrailingDotZero: bool = false): int {.inline.} =
  284. const
  285. minFixedDecimalPoint: int32 = -4
  286. maxFixedDecimalPoint: int32 = 9
  287. var pos = pos
  288. assert(minFixedDecimalPoint <= -1, "internal error")
  289. assert(maxFixedDecimalPoint >= 1, "internal error")
  290. sf_Assert(digits >= 1)
  291. sf_Assert(digits <= 999999999'u)
  292. sf_Assert(decimalExponent >= -99)
  293. sf_Assert(decimalExponent <= 99)
  294. var numDigits: int32 = decimalLength(digits)
  295. let decimalPoint: int32 = numDigits + decimalExponent
  296. let useFixed: bool = minFixedDecimalPoint <= decimalPoint and
  297. decimalPoint <= maxFixedDecimalPoint
  298. ## Prepare the buffer.
  299. ## Avoid calling memset/memcpy with variable arguments below...
  300. for i in 0..<32: buffer[pos+i] = '0'
  301. assert(minFixedDecimalPoint >= -30, "internal error")
  302. assert(maxFixedDecimalPoint <= 32, "internal error")
  303. var decimalDigitsPosition: int32
  304. if useFixed:
  305. if decimalPoint <= 0:
  306. ## 0.[000]digits
  307. decimalDigitsPosition = 2 - decimalPoint
  308. else:
  309. ## dig.its
  310. ## digits[000]
  311. decimalDigitsPosition = 0
  312. else:
  313. ## dE+123 or d.igitsE+123
  314. decimalDigitsPosition = 1
  315. var digitsEnd = pos + decimalDigitsPosition + numDigits
  316. let tz: int32 = printDecimalDigitsBackwards(buffer, digitsEnd, digits)
  317. dec(digitsEnd, tz)
  318. dec(numDigits, tz)
  319. ## decimal_exponent += tz; // => decimal_point unchanged.
  320. if useFixed:
  321. if decimalPoint <= 0:
  322. ## 0.[000]digits
  323. buffer[pos+1] = '.'
  324. pos = digitsEnd
  325. elif decimalPoint < numDigits:
  326. ## dig.its
  327. for i in countdown(7, 0):
  328. buffer[i + decimalPoint + 1] = buffer[i + decimalPoint]
  329. buffer[pos+decimalPoint] = '.'
  330. pos = digitsEnd + 1
  331. else:
  332. ## digits[000]
  333. inc(pos, decimalPoint)
  334. if forceTrailingDotZero:
  335. buffer[pos] = '.'
  336. buffer[pos+1] = '0'
  337. inc(pos, 2)
  338. else:
  339. buffer[pos] = buffer[pos+1]
  340. if numDigits == 1:
  341. ## dE+123
  342. inc(pos)
  343. else:
  344. ## d.igitsE+123
  345. buffer[pos+1] = '.'
  346. pos = digitsEnd
  347. let scientificExponent: int32 = decimalPoint - 1
  348. ## SF_ASSERT(scientific_exponent != 0);
  349. buffer[pos] = 'e'
  350. buffer[pos+1] = if scientificExponent < 0: '-' else: '+'
  351. inc(pos, 2)
  352. let k: uint32 = cast[uint32](if scientificExponent < 0: -scientificExponent else: scientificExponent)
  353. if k < 10:
  354. buffer[pos] = chr(uint32('0') + k)
  355. inc pos
  356. else:
  357. utoa2Digits(buffer, pos, k)
  358. inc(pos, 2)
  359. return pos
  360. proc float32ToChars*(buffer: var openArray[char]; v: float32; forceTrailingDotZero = false): int {.
  361. inline.} =
  362. let significand: uint32 = physicalSignificand(constructSingle(v))
  363. let exponent: uint32 = physicalExponent(constructSingle(v))
  364. var pos = 0
  365. if exponent != maxIeeeExponent:
  366. ## Finite
  367. buffer[pos] = '-'
  368. inc(pos, signBit(constructSingle(v)))
  369. if exponent != 0 or significand != 0:
  370. ## != 0
  371. let dec: auto = toDecimal32(significand, exponent)
  372. return formatDigits(buffer, pos, dec.digits, dec.exponent, forceTrailingDotZero)
  373. else:
  374. buffer[pos] = '0'
  375. buffer[pos+1] = '.'
  376. buffer[pos+2] = '0'
  377. buffer[pos+3] = ' '
  378. inc(pos, if forceTrailingDotZero: 3 else: 1)
  379. return pos
  380. if significand == 0:
  381. buffer[pos] = '-'
  382. inc(pos, signBit(constructSingle(v)))
  383. buffer[pos] = 'i'
  384. buffer[pos+1] = 'n'
  385. buffer[pos+2] = 'f'
  386. buffer[pos+3] = ' '
  387. return pos + 3
  388. else:
  389. buffer[pos] = 'n'
  390. buffer[pos+1] = 'a'
  391. buffer[pos+2] = 'n'
  392. buffer[pos+3] = ' '
  393. return pos + 3