decimal.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // BSON library for Go
  2. //
  3. // Copyright (c) 2010-2012 - Gustavo Niemeyer <gustavo@niemeyer.net>
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // 1. Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // 2. Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  20. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. package bson
  27. import (
  28. "fmt"
  29. "strconv"
  30. "strings"
  31. )
  32. // Decimal128 holds decimal128 BSON values.
  33. type Decimal128 struct {
  34. h, l uint64
  35. }
  36. func (d Decimal128) String() string {
  37. var pos int // positive sign
  38. var e int // exponent
  39. var h, l uint64 // significand high/low
  40. if d.h>>63&1 == 0 {
  41. pos = 1
  42. }
  43. switch d.h >> 58 & (1<<5 - 1) {
  44. case 0x1F:
  45. return "NaN"
  46. case 0x1E:
  47. return "-Inf"[pos:]
  48. }
  49. l = d.l
  50. if d.h>>61&3 == 3 {
  51. // Bits: 1*sign 2*ignored 14*exponent 111*significand.
  52. // Implicit 0b100 prefix in significand.
  53. e = int(d.h>>47&(1<<14-1)) - 6176
  54. //h = 4<<47 | d.h&(1<<47-1)
  55. // Spec says all of these values are out of range.
  56. h, l = 0, 0
  57. } else {
  58. // Bits: 1*sign 14*exponent 113*significand
  59. e = int(d.h>>49&(1<<14-1)) - 6176
  60. h = d.h & (1<<49 - 1)
  61. }
  62. // Would be handled by the logic below, but that's trivial and common.
  63. if h == 0 && l == 0 && e == 0 {
  64. return "-0"[pos:]
  65. }
  66. var repr [48]byte // Loop 5 times over 9 digits plus dot, negative sign, and leading zero.
  67. var last = len(repr)
  68. var i = len(repr)
  69. var dot = len(repr) + e
  70. var rem uint32
  71. Loop:
  72. for d9 := 0; d9 < 5; d9++ {
  73. h, l, rem = divmod(h, l, 1e9)
  74. for d1 := 0; d1 < 9; d1++ {
  75. // Handle "-0.0", "0.00123400", "-1.00E-6", "1.050E+3", etc.
  76. if i < len(repr) && (dot == i || l == 0 && h == 0 && rem > 0 && rem < 10 && (dot < i-6 || e > 0)) {
  77. e += len(repr) - i
  78. i--
  79. repr[i] = '.'
  80. last = i - 1
  81. dot = len(repr) // Unmark.
  82. }
  83. c := '0' + byte(rem%10)
  84. rem /= 10
  85. i--
  86. repr[i] = c
  87. // Handle "0E+3", "1E+3", etc.
  88. if l == 0 && h == 0 && rem == 0 && i == len(repr)-1 && (dot < i-5 || e > 0) {
  89. last = i
  90. break Loop
  91. }
  92. if c != '0' {
  93. last = i
  94. }
  95. // Break early. Works without it, but why.
  96. if dot > i && l == 0 && h == 0 && rem == 0 {
  97. break Loop
  98. }
  99. }
  100. }
  101. repr[last-1] = '-'
  102. last--
  103. if e > 0 {
  104. return string(repr[last+pos:]) + "E+" + strconv.Itoa(e)
  105. }
  106. if e < 0 {
  107. return string(repr[last+pos:]) + "E" + strconv.Itoa(e)
  108. }
  109. return string(repr[last+pos:])
  110. }
  111. func divmod(h, l uint64, div uint32) (qh, ql uint64, rem uint32) {
  112. div64 := uint64(div)
  113. a := h >> 32
  114. aq := a / div64
  115. ar := a % div64
  116. b := ar<<32 + h&(1<<32-1)
  117. bq := b / div64
  118. br := b % div64
  119. c := br<<32 + l>>32
  120. cq := c / div64
  121. cr := c % div64
  122. d := cr<<32 + l&(1<<32-1)
  123. dq := d / div64
  124. dr := d % div64
  125. return (aq<<32 | bq), (cq<<32 | dq), uint32(dr)
  126. }
  127. var dNaN = Decimal128{0x1F << 58, 0}
  128. var dPosInf = Decimal128{0x1E << 58, 0}
  129. var dNegInf = Decimal128{0x3E << 58, 0}
  130. func dErr(s string) (Decimal128, error) {
  131. return dNaN, fmt.Errorf("cannot parse %q as a decimal128", s)
  132. }
  133. func ParseDecimal128(s string) (Decimal128, error) {
  134. orig := s
  135. if s == "" {
  136. return dErr(orig)
  137. }
  138. neg := s[0] == '-'
  139. if neg || s[0] == '+' {
  140. s = s[1:]
  141. }
  142. if (len(s) == 3 || len(s) == 8) && (s[0] == 'N' || s[0] == 'n' || s[0] == 'I' || s[0] == 'i') {
  143. if s == "NaN" || s == "nan" || strings.EqualFold(s, "nan") {
  144. return dNaN, nil
  145. }
  146. if s == "Inf" || s == "inf" || strings.EqualFold(s, "inf") || strings.EqualFold(s, "infinity") {
  147. if neg {
  148. return dNegInf, nil
  149. }
  150. return dPosInf, nil
  151. }
  152. return dErr(orig)
  153. }
  154. var h, l uint64
  155. var e int
  156. var add, ovr uint32
  157. var mul uint32 = 1
  158. var dot = -1
  159. var digits = 0
  160. var i = 0
  161. for i < len(s) {
  162. c := s[i]
  163. if mul == 1e9 {
  164. h, l, ovr = muladd(h, l, mul, add)
  165. mul, add = 1, 0
  166. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  167. return dErr(orig)
  168. }
  169. }
  170. if c >= '0' && c <= '9' {
  171. i++
  172. if c > '0' || digits > 0 {
  173. digits++
  174. }
  175. if digits > 34 {
  176. if c == '0' {
  177. // Exact rounding.
  178. e++
  179. continue
  180. }
  181. return dErr(orig)
  182. }
  183. mul *= 10
  184. add *= 10
  185. add += uint32(c - '0')
  186. continue
  187. }
  188. if c == '.' {
  189. i++
  190. if dot >= 0 || i == 1 && len(s) == 1 {
  191. return dErr(orig)
  192. }
  193. if i == len(s) {
  194. break
  195. }
  196. if s[i] < '0' || s[i] > '9' || e > 0 {
  197. return dErr(orig)
  198. }
  199. dot = i
  200. continue
  201. }
  202. break
  203. }
  204. if i == 0 {
  205. return dErr(orig)
  206. }
  207. if mul > 1 {
  208. h, l, ovr = muladd(h, l, mul, add)
  209. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  210. return dErr(orig)
  211. }
  212. }
  213. if dot >= 0 {
  214. e += dot - i
  215. }
  216. if i+1 < len(s) && (s[i] == 'E' || s[i] == 'e') {
  217. i++
  218. eneg := s[i] == '-'
  219. if eneg || s[i] == '+' {
  220. i++
  221. if i == len(s) {
  222. return dErr(orig)
  223. }
  224. }
  225. n := 0
  226. for i < len(s) && n < 1e4 {
  227. c := s[i]
  228. i++
  229. if c < '0' || c > '9' {
  230. return dErr(orig)
  231. }
  232. n *= 10
  233. n += int(c - '0')
  234. }
  235. if eneg {
  236. n = -n
  237. }
  238. e += n
  239. for e < -6176 {
  240. // Subnormal.
  241. var div uint32 = 1
  242. for div < 1e9 && e < -6176 {
  243. div *= 10
  244. e++
  245. }
  246. var rem uint32
  247. h, l, rem = divmod(h, l, div)
  248. if rem > 0 {
  249. return dErr(orig)
  250. }
  251. }
  252. for e > 6111 {
  253. // Clamped.
  254. var mul uint32 = 1
  255. for mul < 1e9 && e > 6111 {
  256. mul *= 10
  257. e--
  258. }
  259. h, l, ovr = muladd(h, l, mul, 0)
  260. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  261. return dErr(orig)
  262. }
  263. }
  264. if e < -6176 || e > 6111 {
  265. return dErr(orig)
  266. }
  267. }
  268. if i < len(s) {
  269. return dErr(orig)
  270. }
  271. h |= uint64(e+6176) & uint64(1<<14-1) << 49
  272. if neg {
  273. h |= 1 << 63
  274. }
  275. return Decimal128{h, l}, nil
  276. }
  277. func muladd(h, l uint64, mul uint32, add uint32) (resh, resl uint64, overflow uint32) {
  278. mul64 := uint64(mul)
  279. a := mul64 * (l & (1<<32 - 1))
  280. b := a>>32 + mul64*(l>>32)
  281. c := b>>32 + mul64*(h&(1<<32-1))
  282. d := c>>32 + mul64*(h>>32)
  283. a = a&(1<<32-1) + uint64(add)
  284. b = b&(1<<32-1) + a>>32
  285. c = c&(1<<32-1) + b>>32
  286. d = d&(1<<32-1) + c>>32
  287. return (d<<32 | c&(1<<32-1)), (b<<32 | a&(1<<32-1)), uint32(d >> 32)
  288. }