int128.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright 2020 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Some code is copied and adjusted from
  5. // https://github.com/lukechampine/uint128, the original LICENSE file
  6. // reproduced below in full as of 2021-01-19:
  7. /*
  8. The MIT License (MIT)
  9. Copyright (c) 2019 Luke Champine
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. */
  26. package libc // import "modernc.org/libc"
  27. import (
  28. mbits "math/bits"
  29. "modernc.org/mathutil"
  30. )
  31. type Int128 mathutil.Int128
  32. var (
  33. int128Minus1 = Int128{-1, -1}
  34. int128Plus1 = Int128{Lo: 1}
  35. )
  36. func Int128FromFloat32(n float32) Int128 { return Int128(mathutil.NewInt128FromFloat32(n)) }
  37. func Int128FromFloat64(n float64) Int128 { return Int128(mathutil.NewInt128FromFloat64(n)) }
  38. func Int128FromInt16(n int16) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  39. func Int128FromInt32(n int32) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  40. func Int128FromInt64(n int64) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  41. func Int128FromInt8(n int8) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  42. func Int128FromUint16(n uint16) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  43. func Int128FromUint32(n uint32) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  44. func Int128FromUint64(n uint64) Int128 { return Int128(mathutil.NewInt128FromUint64(n)) }
  45. func Int128FromUint8(n uint8) Int128 { return Int128(mathutil.NewInt128FromInt64(int64(n))) }
  46. func Int128FromUint128(n Uint128) Int128 { return Int128{Lo: int64(n.Lo), Hi: int64(n.Hi)} }
  47. func (n *Int128) LValueDec() { *n = n.Add(int128Minus1) }
  48. func (n *Int128) LValueInc() { *n = n.Add(int128Plus1) }
  49. func (n *Int128) LValueShl(c int32) { *n = n.Shl(c) }
  50. func (n *Int128) LValueShr(c int32) { *n = n.Shr(c) }
  51. func (n Int128) And(v Int128) Int128 { return Int128{n.Lo & v.Lo, n.Hi & v.Hi} }
  52. func (n Int128) Cmp(y Int128) int { return mathutil.Int128(n).Cmp(mathutil.Int128(y)) }
  53. func (n Int128) Int16() int16 { return int16(n.Lo) }
  54. func (n Int128) Int32() int32 { return int32(n.Lo) }
  55. func (n Int128) Int64() int64 { return int64(n.Lo) }
  56. func (n Int128) Int8() int8 { return int8(n.Lo) }
  57. func (n Int128) Or(v Int128) Int128 { return Int128{n.Lo | v.Lo, n.Hi | v.Hi} }
  58. func (n Int128) Uint128() (r Uint128) { return Uint128{uint64(n.Lo), uint64(n.Hi)} }
  59. func (n Int128) Uint16() uint16 { return uint16(n.Lo) }
  60. func (n Int128) Uint32() uint32 { return uint32(n.Lo) }
  61. func (n Int128) Uint64() uint64 { return uint64(n.Lo) }
  62. func (n Int128) Uint8() uint8 { return uint8(n.Lo) }
  63. func (n Int128) Xor(v Int128) Int128 { return Int128{n.Lo ^ v.Lo, n.Hi ^ v.Hi} }
  64. func (n Int128) Neg() Int128 {
  65. n.Lo ^= -1
  66. n.Hi ^= -1
  67. return n.Add(int128Plus1)
  68. }
  69. func (n Int128) Float32() float32 {
  70. switch n.Hi {
  71. case 0:
  72. return float32(uint64(n.Lo))
  73. case -1:
  74. return -float32(uint64(n.Lo))
  75. }
  76. if n.Hi < 0 {
  77. n = n.Neg()
  78. return -float32(n.Hi)*(1<<64) + float32(uint64(n.Lo))
  79. }
  80. return -float32(n.Hi)*(1<<64) + float32(uint64(n.Lo))
  81. }
  82. func (n Int128) Float64() float64 {
  83. switch n.Hi {
  84. case 0:
  85. return float64(uint64(n.Lo))
  86. case -1:
  87. return -float64(uint64(n.Lo))
  88. }
  89. if n.Hi < 0 {
  90. n = n.Neg()
  91. return -float64(n.Hi)*(1<<64) + float64(uint64(n.Lo))
  92. }
  93. return float64(n.Hi)*(1<<64) + float64(uint64(n.Lo))
  94. }
  95. func (n Int128) Add(m Int128) (r Int128) {
  96. r.Lo = n.Lo + m.Lo
  97. r.Hi = n.Hi + m.Hi
  98. if uint64(r.Lo) < uint64(n.Lo) {
  99. r.Hi++
  100. }
  101. return r
  102. }
  103. func (n Int128) Mul(m Int128) Int128 {
  104. hi, lo := mbits.Mul64(uint64(n.Lo), uint64(m.Lo))
  105. _, p1 := mbits.Mul64(uint64(n.Hi), uint64(m.Lo))
  106. _, p3 := mbits.Mul64(uint64(n.Lo), uint64(m.Hi))
  107. hi, _ = mbits.Add64(hi, p1, 0)
  108. hi, _ = mbits.Add64(hi, p3, 0)
  109. return Int128{int64(lo), int64(hi)}
  110. }
  111. func (n Int128) Shl(c int32) (r Int128) {
  112. if c > 64 {
  113. r.Lo = 0
  114. r.Hi = n.Lo << (c - 64)
  115. } else {
  116. r.Lo = n.Lo << c
  117. r.Hi = n.Hi<<c | n.Lo>>(64-c)
  118. }
  119. return r
  120. }
  121. func (n Int128) Shr(c int32) (r Int128) {
  122. if c > 64 {
  123. r.Lo = n.Hi >> (c - 64)
  124. switch {
  125. case n.Hi < 0:
  126. r.Hi = -1
  127. default:
  128. r.Hi = 0
  129. }
  130. } else {
  131. r.Lo = n.Lo>>c | n.Hi<<(64-c)
  132. r.Hi = n.Hi >> c
  133. }
  134. return r
  135. }
  136. type Uint128 mathutil.Uint128
  137. func Uint128FromFloat32(n float32) Uint128 { return Uint128(mathutil.NewUint128FromFloat32(n)) }
  138. func Uint128FromFloat64(n float64) Uint128 { return Uint128(mathutil.NewUint128FromFloat64(n)) }
  139. func Uint128FromInt128(n Int128) Uint128 { return Uint128{Lo: uint64(n.Lo), Hi: uint64(n.Hi)} }
  140. func Uint128FromInt16(n int16) Uint128 { return Uint128FromInt64(int64(n)) }
  141. func Uint128FromInt32(n int32) Uint128 { return Uint128FromInt64(int64(n)) }
  142. func Uint128FromInt8(n int8) Uint128 { return Uint128FromInt64(int64(n)) }
  143. func Uint128FromUint16(n uint16) Uint128 { return Uint128{Lo: uint64(n)} }
  144. func Uint128FromUint32(n uint32) Uint128 { return Uint128{Lo: uint64(n)} }
  145. func Uint128FromUint64(n uint64) Uint128 { return Uint128{Lo: n} }
  146. func Uint128FromUint8(n uint8) Uint128 { return Uint128{Lo: uint64(n)} }
  147. func Uint128FromInt64(n int64) (r Uint128) {
  148. r.Lo = uint64(n)
  149. if n < 0 {
  150. r.Hi = ^uint64(0)
  151. }
  152. return r
  153. }
  154. func (n *Uint128) LValueShl(c int32) { *n = n.Shl(c) }
  155. func (n *Uint128) LValueShr(c int32) { *n = n.Shr(c) }
  156. func (n Uint128) And(m Uint128) Uint128 { return Uint128{n.Lo & m.Lo, n.Hi & m.Hi} }
  157. func (n Uint128) Int128() Int128 { return Int128{int64(n.Lo), int64(n.Hi)} }
  158. func (n Uint128) Int16() int16 { return int16(n.Lo) }
  159. func (n Uint128) Int32() int32 { return int32(n.Lo) }
  160. func (n Uint128) Int64() int64 { return int64(n.Lo) }
  161. func (n Uint128) Int8() int8 { return int8(n.Lo) }
  162. func (n Uint128) Or(m Uint128) Uint128 { return Uint128{n.Lo | m.Lo, n.Hi | m.Hi} }
  163. func (n Uint128) Uint16() uint16 { return uint16(n.Lo) }
  164. func (n Uint128) Uint32() uint32 { return uint32(n.Lo) }
  165. func (n Uint128) Uint64() uint64 { return n.Lo }
  166. func (n Uint128) Uint8() uint8 { return uint8(n.Lo) }
  167. func (n Uint128) Xor(m Uint128) Uint128 { return Uint128{n.Lo ^ m.Lo, n.Hi ^ m.Hi} }
  168. func (n Uint128) Add(m Uint128) (r Uint128) {
  169. var carry uint64
  170. r.Lo, carry = mbits.Add64(n.Lo, m.Lo, 0)
  171. r.Hi, _ = mbits.Add64(n.Hi, m.Hi, carry)
  172. return r
  173. }
  174. func (n Uint128) Mul(m Uint128) Uint128 {
  175. hi, lo := mbits.Mul64(n.Lo, m.Lo)
  176. _, p1 := mbits.Mul64(n.Hi, m.Lo)
  177. _, p3 := mbits.Mul64(n.Lo, m.Hi)
  178. hi, _ = mbits.Add64(hi, p1, 0)
  179. hi, _ = mbits.Add64(hi, p3, 0)
  180. return Uint128{lo, hi}
  181. }
  182. func (n Uint128) Shr(c int32) (r Uint128) {
  183. if c > 64 {
  184. r.Lo = n.Hi >> (c - 64)
  185. r.Hi = 0
  186. } else {
  187. r.Lo = n.Lo>>c | n.Hi<<(64-c)
  188. r.Hi = n.Hi >> c
  189. }
  190. return r
  191. }
  192. func (n Uint128) mulOvf(m Uint128) (_ Uint128, ovf bool) {
  193. hi, lo := mbits.Mul64(n.Lo, m.Lo)
  194. p0, p1 := mbits.Mul64(n.Hi, m.Lo)
  195. p2, p3 := mbits.Mul64(n.Lo, m.Hi)
  196. hi, c0 := mbits.Add64(hi, p1, 0)
  197. hi, c1 := mbits.Add64(hi, p3, 0)
  198. ovf = p0 != 0 || p2 != 0 || c0 != 0 || c1 != 0
  199. return Uint128{lo, hi}, ovf
  200. }
  201. func (n Uint128) quoRem(m Uint128) (q, r Uint128) {
  202. if m.Hi == 0 {
  203. var r64 uint64
  204. q, r64 = n.quoRem64(m.Lo)
  205. r = Uint128FromUint64(r64)
  206. } else {
  207. // generate a "trial quotient," guaranteed to be within 1 of the actual
  208. // quotient, then adjust.
  209. nz := mbits.LeadingZeros64(m.Hi)
  210. v1 := m.Shl(int32(nz))
  211. u1 := n.Shr(1)
  212. tq, _ := mbits.Div64(u1.Hi, u1.Lo, v1.Hi)
  213. tq >>= 63 - nz
  214. if tq != 0 {
  215. tq--
  216. }
  217. q = Uint128FromUint64(tq)
  218. // calculate remainder using trial quotient, then adjust if remainder is
  219. // greater than divisor
  220. r = n.Sub(m.mul64(tq))
  221. if r.Cmp(m) >= 0 {
  222. q = q.add64(1)
  223. r = r.Sub(m)
  224. }
  225. }
  226. return
  227. }
  228. func (n Uint128) quoRem64(m uint64) (q Uint128, r uint64) {
  229. if n.Hi < m {
  230. q.Lo, r = mbits.Div64(n.Hi, n.Lo, m)
  231. } else {
  232. q.Hi, r = mbits.Div64(0, n.Hi, m)
  233. q.Lo, r = mbits.Div64(r, n.Lo, m)
  234. }
  235. return
  236. }
  237. func (n Uint128) Div(m Uint128) (r Uint128) {
  238. r, _ = n.quoRem(m)
  239. return r
  240. }
  241. func (n Uint128) Shl(c int32) (r Uint128) {
  242. if c > 64 {
  243. r.Lo = 0
  244. r.Hi = n.Lo << (c - 64)
  245. } else {
  246. r.Lo = n.Lo << c
  247. r.Hi = n.Hi<<c | n.Lo>>(64-c)
  248. }
  249. return
  250. }
  251. func (n Uint128) Sub(m Uint128) Uint128 {
  252. lo, borrow := mbits.Sub64(n.Lo, m.Lo, 0)
  253. hi, _ := mbits.Sub64(n.Hi, m.Hi, borrow)
  254. return Uint128{lo, hi}
  255. }
  256. func (n Uint128) mul64(m uint64) Uint128 {
  257. hi, lo := mbits.Mul64(n.Lo, m)
  258. _, p1 := mbits.Mul64(n.Hi, m)
  259. hi, _ = mbits.Add64(hi, p1, 0)
  260. return Uint128{lo, hi}
  261. }
  262. func (n Uint128) Cmp(m Uint128) int {
  263. if n == m {
  264. return 0
  265. } else if n.Hi < m.Hi || (n.Hi == m.Hi && n.Lo < m.Lo) {
  266. return -1
  267. } else {
  268. return 1
  269. }
  270. }
  271. func (n Uint128) add64(m uint64) Uint128 {
  272. lo, carry := mbits.Add64(n.Lo, m, 0)
  273. hi, _ := mbits.Add64(n.Hi, 0, carry)
  274. return Uint128{lo, hi}
  275. }
  276. func (n Uint128) Float32() float32 {
  277. if n.Hi == 0 {
  278. return float32(uint64(n.Lo))
  279. }
  280. return float32(n.Hi)*(1<<64) + float32(uint64(n.Lo))
  281. }
  282. func (n Uint128) Float64() float64 {
  283. if n.Hi == 0 {
  284. return float64(uint64(n.Lo))
  285. }
  286. return float64(n.Hi)*(1<<64) + float64(uint64(n.Lo))
  287. }