decimal.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. // ParseDecimal128 parse a string and return the corresponding value as
  134. // a decimal128
  135. func ParseDecimal128(s string) (Decimal128, error) {
  136. orig := s
  137. if s == "" {
  138. return dErr(orig)
  139. }
  140. neg := s[0] == '-'
  141. if neg || s[0] == '+' {
  142. s = s[1:]
  143. }
  144. if (len(s) == 3 || len(s) == 8) && (s[0] == 'N' || s[0] == 'n' || s[0] == 'I' || s[0] == 'i') {
  145. if s == "NaN" || s == "nan" || strings.EqualFold(s, "nan") {
  146. return dNaN, nil
  147. }
  148. if s == "Inf" || s == "inf" || strings.EqualFold(s, "inf") || strings.EqualFold(s, "infinity") {
  149. if neg {
  150. return dNegInf, nil
  151. }
  152. return dPosInf, nil
  153. }
  154. return dErr(orig)
  155. }
  156. var h, l uint64
  157. var e int
  158. var add, ovr uint32
  159. var mul uint32 = 1
  160. var dot = -1
  161. var digits = 0
  162. var i = 0
  163. for i < len(s) {
  164. c := s[i]
  165. if mul == 1e9 {
  166. h, l, ovr = muladd(h, l, mul, add)
  167. mul, add = 1, 0
  168. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  169. return dErr(orig)
  170. }
  171. }
  172. if c >= '0' && c <= '9' {
  173. i++
  174. if c > '0' || digits > 0 {
  175. digits++
  176. }
  177. if digits > 34 {
  178. if c == '0' {
  179. // Exact rounding.
  180. e++
  181. continue
  182. }
  183. return dErr(orig)
  184. }
  185. mul *= 10
  186. add *= 10
  187. add += uint32(c - '0')
  188. continue
  189. }
  190. if c == '.' {
  191. i++
  192. if dot >= 0 || i == 1 && len(s) == 1 {
  193. return dErr(orig)
  194. }
  195. if i == len(s) {
  196. break
  197. }
  198. if s[i] < '0' || s[i] > '9' || e > 0 {
  199. return dErr(orig)
  200. }
  201. dot = i
  202. continue
  203. }
  204. break
  205. }
  206. if i == 0 {
  207. return dErr(orig)
  208. }
  209. if mul > 1 {
  210. h, l, ovr = muladd(h, l, mul, add)
  211. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  212. return dErr(orig)
  213. }
  214. }
  215. if dot >= 0 {
  216. e += dot - i
  217. }
  218. if i+1 < len(s) && (s[i] == 'E' || s[i] == 'e') {
  219. i++
  220. eneg := s[i] == '-'
  221. if eneg || s[i] == '+' {
  222. i++
  223. if i == len(s) {
  224. return dErr(orig)
  225. }
  226. }
  227. n := 0
  228. for i < len(s) && n < 1e4 {
  229. c := s[i]
  230. i++
  231. if c < '0' || c > '9' {
  232. return dErr(orig)
  233. }
  234. n *= 10
  235. n += int(c - '0')
  236. }
  237. if eneg {
  238. n = -n
  239. }
  240. e += n
  241. for e < -6176 {
  242. // Subnormal.
  243. var div uint32 = 1
  244. for div < 1e9 && e < -6176 {
  245. div *= 10
  246. e++
  247. }
  248. var rem uint32
  249. h, l, rem = divmod(h, l, div)
  250. if rem > 0 {
  251. return dErr(orig)
  252. }
  253. }
  254. for e > 6111 {
  255. // Clamped.
  256. var mul uint32 = 1
  257. for mul < 1e9 && e > 6111 {
  258. mul *= 10
  259. e--
  260. }
  261. h, l, ovr = muladd(h, l, mul, 0)
  262. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  263. return dErr(orig)
  264. }
  265. }
  266. if e < -6176 || e > 6111 {
  267. return dErr(orig)
  268. }
  269. }
  270. if i < len(s) {
  271. return dErr(orig)
  272. }
  273. h |= uint64(e+6176) & uint64(1<<14-1) << 49
  274. if neg {
  275. h |= 1 << 63
  276. }
  277. return Decimal128{h, l}, nil
  278. }
  279. func muladd(h, l uint64, mul uint32, add uint32) (resh, resl uint64, overflow uint32) {
  280. mul64 := uint64(mul)
  281. a := mul64 * (l & (1<<32 - 1))
  282. b := a>>32 + mul64*(l>>32)
  283. c := b>>32 + mul64*(h&(1<<32-1))
  284. d := c>>32 + mul64*(h>>32)
  285. a = a&(1<<32-1) + uint64(add)
  286. b = b&(1<<32-1) + a>>32
  287. c = c&(1<<32-1) + b>>32
  288. d = d&(1<<32-1) + c>>32
  289. return (d<<32 | c&(1<<32-1)), (b<<32 | a&(1<<32-1)), uint32(d >> 32)
  290. }