本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
6.4KB

  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Based on gopkg.in/mgo.v2/bson by Gustavo Niemeyer
  8. // See THIRD-PARTY-NOTICES for original license terms.
  9. package primitive
  10. import (
  11. "fmt"
  12. "strconv"
  13. "strings"
  14. )
  15. // Decimal128 holds decimal128 BSON values.
  16. type Decimal128 struct {
  17. h, l uint64
  18. }
  19. // NewDecimal128 creates a Decimal128 using the provide high and low uint64s.
  20. func NewDecimal128(h, l uint64) Decimal128 {
  21. return Decimal128{h: h, l: l}
  22. }
  23. // GetBytes returns the underlying bytes of the BSON decimal value as two uint16 values. The first
  24. // contains the most first 8 bytes of the value and the second contains the latter.
  25. func (d Decimal128) GetBytes() (uint64, uint64) {
  26. return d.h, d.l
  27. }
  28. // String returns a string representation of the decimal value.
  29. func (d Decimal128) String() string {
  30. var pos int // positive sign
  31. var e int // exponent
  32. var h, l uint64 // significand high/low
  33. if d.h>>63&1 == 0 {
  34. pos = 1
  35. }
  36. switch d.h >> 58 & (1<<5 - 1) {
  37. case 0x1F:
  38. return "NaN"
  39. case 0x1E:
  40. return "-Infinity"[pos:]
  41. }
  42. l = d.l
  43. if d.h>>61&3 == 3 {
  44. // Bits: 1*sign 2*ignored 14*exponent 111*significand.
  45. // Implicit 0b100 prefix in significand.
  46. e = int(d.h>>47&(1<<14-1)) - 6176
  47. //h = 4<<47 | d.h&(1<<47-1)
  48. // Spec says all of these values are out of range.
  49. h, l = 0, 0
  50. } else {
  51. // Bits: 1*sign 14*exponent 113*significand
  52. e = int(d.h>>49&(1<<14-1)) - 6176
  53. h = d.h & (1<<49 - 1)
  54. }
  55. // Would be handled by the logic below, but that's trivial and common.
  56. if h == 0 && l == 0 && e == 0 {
  57. return "-0"[pos:]
  58. }
  59. var repr [48]byte // Loop 5 times over 9 digits plus dot, negative sign, and leading zero.
  60. var last = len(repr)
  61. var i = len(repr)
  62. var dot = len(repr) + e
  63. var rem uint32
  64. Loop:
  65. for d9 := 0; d9 < 5; d9++ {
  66. h, l, rem = divmod(h, l, 1e9)
  67. for d1 := 0; d1 < 9; d1++ {
  68. // Handle "-0.0", "0.00123400", "-1.00E-6", "1.050E+3", etc.
  69. if i < len(repr) && (dot == i || l == 0 && h == 0 && rem > 0 && rem < 10 && (dot < i-6 || e > 0)) {
  70. e += len(repr) - i
  71. i--
  72. repr[i] = '.'
  73. last = i - 1
  74. dot = len(repr) // Unmark.
  75. }
  76. c := '0' + byte(rem%10)
  77. rem /= 10
  78. i--
  79. repr[i] = c
  80. // Handle "0E+3", "1E+3", etc.
  81. if l == 0 && h == 0 && rem == 0 && i == len(repr)-1 && (dot < i-5 || e > 0) {
  82. last = i
  83. break Loop
  84. }
  85. if c != '0' {
  86. last = i
  87. }
  88. // Break early. Works without it, but why.
  89. if dot > i && l == 0 && h == 0 && rem == 0 {
  90. break Loop
  91. }
  92. }
  93. }
  94. repr[last-1] = '-'
  95. last--
  96. if e > 0 {
  97. return string(repr[last+pos:]) + "E+" + strconv.Itoa(e)
  98. }
  99. if e < 0 {
  100. return string(repr[last+pos:]) + "E" + strconv.Itoa(e)
  101. }
  102. return string(repr[last+pos:])
  103. }
  104. func divmod(h, l uint64, div uint32) (qh, ql uint64, rem uint32) {
  105. div64 := uint64(div)
  106. a := h >> 32
  107. aq := a / div64
  108. ar := a % div64
  109. b := ar<<32 + h&(1<<32-1)
  110. bq := b / div64
  111. br := b % div64
  112. c := br<<32 + l>>32
  113. cq := c / div64
  114. cr := c % div64
  115. d := cr<<32 + l&(1<<32-1)
  116. dq := d / div64
  117. dr := d % div64
  118. return (aq<<32 | bq), (cq<<32 | dq), uint32(dr)
  119. }
  120. var dNaN = Decimal128{0x1F << 58, 0}
  121. var dPosInf = Decimal128{0x1E << 58, 0}
  122. var dNegInf = Decimal128{0x3E << 58, 0}
  123. func dErr(s string) (Decimal128, error) {
  124. return dNaN, fmt.Errorf("cannot parse %q as a decimal128", s)
  125. }
  126. //ParseDecimal128 takes the given string and attempts to parse it into a valid
  127. // Decimal128 value.
  128. func ParseDecimal128(s string) (Decimal128, error) {
  129. orig := s
  130. if s == "" {
  131. return dErr(orig)
  132. }
  133. neg := s[0] == '-'
  134. if neg || s[0] == '+' {
  135. s = s[1:]
  136. }
  137. if (len(s) == 3 || len(s) == 8) && (s[0] == 'N' || s[0] == 'n' || s[0] == 'I' || s[0] == 'i') {
  138. if s == "NaN" || s == "nan" || strings.EqualFold(s, "nan") {
  139. return dNaN, nil
  140. }
  141. if s == "Inf" || s == "inf" || strings.EqualFold(s, "inf") || strings.EqualFold(s, "infinity") {
  142. if neg {
  143. return dNegInf, nil
  144. }
  145. return dPosInf, nil
  146. }
  147. return dErr(orig)
  148. }
  149. var h, l uint64
  150. var e int
  151. var add, ovr uint32
  152. var mul uint32 = 1
  153. var dot = -1
  154. var digits = 0
  155. var i = 0
  156. for i < len(s) {
  157. c := s[i]
  158. if mul == 1e9 {
  159. h, l, ovr = muladd(h, l, mul, add)
  160. mul, add = 1, 0
  161. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  162. return dErr(orig)
  163. }
  164. }
  165. if c >= '0' && c <= '9' {
  166. i++
  167. if c > '0' || digits > 0 {
  168. digits++
  169. }
  170. if digits > 34 {
  171. if c == '0' {
  172. // Exact rounding.
  173. e++
  174. continue
  175. }
  176. return dErr(orig)
  177. }
  178. mul *= 10
  179. add *= 10
  180. add += uint32(c - '0')
  181. continue
  182. }
  183. if c == '.' {
  184. i++
  185. if dot >= 0 || i == 1 && len(s) == 1 {
  186. return dErr(orig)
  187. }
  188. if i == len(s) {
  189. break
  190. }
  191. if s[i] < '0' || s[i] > '9' || e > 0 {
  192. return dErr(orig)
  193. }
  194. dot = i
  195. continue
  196. }
  197. break
  198. }
  199. if i == 0 {
  200. return dErr(orig)
  201. }
  202. if mul > 1 {
  203. h, l, ovr = muladd(h, l, mul, add)
  204. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  205. return dErr(orig)
  206. }
  207. }
  208. if dot >= 0 {
  209. e += dot - i
  210. }
  211. if i+1 < len(s) && (s[i] == 'E' || s[i] == 'e') {
  212. i++
  213. eneg := s[i] == '-'
  214. if eneg || s[i] == '+' {
  215. i++
  216. if i == len(s) {
  217. return dErr(orig)
  218. }
  219. }
  220. n := 0
  221. for i < len(s) && n < 1e4 {
  222. c := s[i]
  223. i++
  224. if c < '0' || c > '9' {
  225. return dErr(orig)
  226. }
  227. n *= 10
  228. n += int(c - '0')
  229. }
  230. if eneg {
  231. n = -n
  232. }
  233. e += n
  234. for e < -6176 {
  235. // Subnormal.
  236. var div uint32 = 1
  237. for div < 1e9 && e < -6176 {
  238. div *= 10
  239. e++
  240. }
  241. var rem uint32
  242. h, l, rem = divmod(h, l, div)
  243. if rem > 0 {
  244. return dErr(orig)
  245. }
  246. }
  247. for e > 6111 {
  248. // Clamped.
  249. var mul uint32 = 1
  250. for mul < 1e9 && e > 6111 {
  251. mul *= 10
  252. e--
  253. }
  254. h, l, ovr = muladd(h, l, mul, 0)
  255. if ovr > 0 || h&((1<<15-1)<<49) > 0 {
  256. return dErr(orig)
  257. }
  258. }
  259. if e < -6176 || e > 6111 {
  260. return dErr(orig)
  261. }
  262. }
  263. if i < len(s) {
  264. return dErr(orig)
  265. }
  266. h |= uint64(e+6176) & uint64(1<<14-1) << 49
  267. if neg {
  268. h |= 1 << 63
  269. }
  270. return Decimal128{h, l}, nil
  271. }
  272. func muladd(h, l uint64, mul uint32, add uint32) (resh, resl uint64, overflow uint32) {
  273. mul64 := uint64(mul)
  274. a := mul64 * (l & (1<<32 - 1))
  275. b := a>>32 + mul64*(l>>32)
  276. c := b>>32 + mul64*(h&(1<<32-1))
  277. d := c>>32 + mul64*(h>>32)
  278. a = a&(1<<32-1) + uint64(add)
  279. b = b&(1<<32-1) + a>>32
  280. c = c&(1<<32-1) + b>>32
  281. d = d&(1<<32-1) + c>>32
  282. return (d<<32 | c&(1<<32-1)), (b<<32 | a&(1<<32-1)), uint32(d >> 32)
  283. }
上海开阖软件有限公司 沪ICP备12045867号-1