mathutil.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // Copyright (c) 2014 The mathutil 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. // Package mathutil provides utilities supplementing the standard 'math' and
  5. // 'math/rand' packages.
  6. //
  7. // Compatibility issues
  8. //
  9. // 2013-12-13: The following functions have been REMOVED
  10. //
  11. // func Uint64ToBigInt(n uint64) *big.Int
  12. // func Uint64FromBigInt(n *big.Int) (uint64, bool)
  13. //
  14. // 2013-05-13: The following functions are now DEPRECATED
  15. //
  16. // func Uint64ToBigInt(n uint64) *big.Int
  17. // func Uint64FromBigInt(n *big.Int) (uint64, bool)
  18. //
  19. // These functions will be REMOVED with Go release 1.1+1.
  20. //
  21. // 2013-01-21: The following functions have been REMOVED
  22. //
  23. // func MaxInt() int
  24. // func MinInt() int
  25. // func MaxUint() uint
  26. // func UintPtrBits() int
  27. //
  28. // They are now replaced by untyped constants
  29. //
  30. // MaxInt
  31. // MinInt
  32. // MaxUint
  33. // UintPtrBits
  34. //
  35. // Additionally one more untyped constant was added
  36. //
  37. // IntBits
  38. //
  39. // This change breaks any existing code depending on the above removed
  40. // functions. They should have not been published in the first place, that was
  41. // unfortunate. Instead, defining such architecture and/or implementation
  42. // specific integer limits and bit widths as untyped constants improves
  43. // performance and allows for static dead code elimination if it depends on
  44. // these values. Thanks to minux for pointing it out in the mail list
  45. // (https://groups.google.com/d/msg/golang-nuts/tlPpLW6aJw8/NT3mpToH-a4J).
  46. //
  47. // 2012-12-12: The following functions will be DEPRECATED with Go release
  48. // 1.0.3+1 and REMOVED with Go release 1.0.3+2, b/c of
  49. // http://code.google.com/p/go/source/detail?r=954a79ee3ea8
  50. //
  51. // func Uint64ToBigInt(n uint64) *big.Int
  52. // func Uint64FromBigInt(n *big.Int) (uint64, bool)
  53. package mathutil
  54. import (
  55. "math"
  56. "math/big"
  57. )
  58. // Architecture and/or implementation specific integer limits and bit widths.
  59. const (
  60. MaxInt = 1<<(IntBits-1) - 1
  61. MinInt = -MaxInt - 1
  62. MaxUint = 1<<IntBits - 1
  63. IntBits = 1 << (^uint(0)>>32&1 + ^uint(0)>>16&1 + ^uint(0)>>8&1 + 3)
  64. UintPtrBits = 1 << (^uintptr(0)>>32&1 + ^uintptr(0)>>16&1 + ^uintptr(0)>>8&1 + 3)
  65. )
  66. var (
  67. _1 = big.NewInt(1)
  68. _2 = big.NewInt(2)
  69. )
  70. // GCDByte returns the greatest common divisor of a and b. Based on:
  71. // http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
  72. func GCDByte(a, b byte) byte {
  73. for b != 0 {
  74. a, b = b, a%b
  75. }
  76. return a
  77. }
  78. // GCDUint16 returns the greatest common divisor of a and b.
  79. func GCDUint16(a, b uint16) uint16 {
  80. for b != 0 {
  81. a, b = b, a%b
  82. }
  83. return a
  84. }
  85. // GCD returns the greatest common divisor of a and b.
  86. func GCDUint32(a, b uint32) uint32 {
  87. for b != 0 {
  88. a, b = b, a%b
  89. }
  90. return a
  91. }
  92. // GCD64 returns the greatest common divisor of a and b.
  93. func GCDUint64(a, b uint64) uint64 {
  94. for b != 0 {
  95. a, b = b, a%b
  96. }
  97. return a
  98. }
  99. // ISqrt returns floor(sqrt(n)). Typical run time is few hundreds of ns.
  100. func ISqrt(n uint32) (x uint32) {
  101. if n == 0 {
  102. return
  103. }
  104. if n >= math.MaxUint16*math.MaxUint16 {
  105. return math.MaxUint16
  106. }
  107. var px, nx uint32
  108. for x = n; ; px, x = x, nx {
  109. nx = (x + n/x) / 2
  110. if nx == x || nx == px {
  111. break
  112. }
  113. }
  114. return
  115. }
  116. // SqrtUint64 returns floor(sqrt(n)). Typical run time is about 0.5 µs.
  117. func SqrtUint64(n uint64) (x uint64) {
  118. if n == 0 {
  119. return
  120. }
  121. if n >= math.MaxUint32*math.MaxUint32 {
  122. return math.MaxUint32
  123. }
  124. var px, nx uint64
  125. for x = n; ; px, x = x, nx {
  126. nx = (x + n/x) / 2
  127. if nx == x || nx == px {
  128. break
  129. }
  130. }
  131. return
  132. }
  133. // SqrtBig returns floor(sqrt(n)). It panics on n < 0.
  134. func SqrtBig(n *big.Int) (x *big.Int) {
  135. switch n.Sign() {
  136. case -1:
  137. panic(-1)
  138. case 0:
  139. return big.NewInt(0)
  140. }
  141. var px, nx big.Int
  142. x = big.NewInt(0)
  143. x.SetBit(x, n.BitLen()/2+1, 1)
  144. for {
  145. nx.Rsh(nx.Add(x, nx.Div(n, x)), 1)
  146. if nx.Cmp(x) == 0 || nx.Cmp(&px) == 0 {
  147. break
  148. }
  149. px.Set(x)
  150. x.Set(&nx)
  151. }
  152. return
  153. }
  154. // Log2Byte returns log base 2 of n. It's the same as index of the highest
  155. // bit set in n. For n == 0 -1 is returned.
  156. func Log2Byte(n byte) int {
  157. return log2[n]
  158. }
  159. // Log2Uint16 returns log base 2 of n. It's the same as index of the highest
  160. // bit set in n. For n == 0 -1 is returned.
  161. func Log2Uint16(n uint16) int {
  162. if b := n >> 8; b != 0 {
  163. return log2[b] + 8
  164. }
  165. return log2[n]
  166. }
  167. // Log2Uint32 returns log base 2 of n. It's the same as index of the highest
  168. // bit set in n. For n == 0 -1 is returned.
  169. func Log2Uint32(n uint32) int {
  170. if b := n >> 24; b != 0 {
  171. return log2[b] + 24
  172. }
  173. if b := n >> 16; b != 0 {
  174. return log2[b] + 16
  175. }
  176. if b := n >> 8; b != 0 {
  177. return log2[b] + 8
  178. }
  179. return log2[n]
  180. }
  181. // Log2Uint64 returns log base 2 of n. It's the same as index of the highest
  182. // bit set in n. For n == 0 -1 is returned.
  183. func Log2Uint64(n uint64) int {
  184. if b := n >> 56; b != 0 {
  185. return log2[b] + 56
  186. }
  187. if b := n >> 48; b != 0 {
  188. return log2[b] + 48
  189. }
  190. if b := n >> 40; b != 0 {
  191. return log2[b] + 40
  192. }
  193. if b := n >> 32; b != 0 {
  194. return log2[b] + 32
  195. }
  196. if b := n >> 24; b != 0 {
  197. return log2[b] + 24
  198. }
  199. if b := n >> 16; b != 0 {
  200. return log2[b] + 16
  201. }
  202. if b := n >> 8; b != 0 {
  203. return log2[b] + 8
  204. }
  205. return log2[n]
  206. }
  207. // ModPowByte computes (b^e)%m. It panics for m == 0 || b == e == 0.
  208. //
  209. // See also: http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method
  210. func ModPowByte(b, e, m byte) byte {
  211. if b == 0 && e == 0 {
  212. panic(0)
  213. }
  214. if m == 1 {
  215. return 0
  216. }
  217. r := uint16(1)
  218. for b, m := uint16(b), uint16(m); e > 0; b, e = b*b%m, e>>1 {
  219. if e&1 == 1 {
  220. r = r * b % m
  221. }
  222. }
  223. return byte(r)
  224. }
  225. // ModPowByte computes (b^e)%m. It panics for m == 0 || b == e == 0.
  226. func ModPowUint16(b, e, m uint16) uint16 {
  227. if b == 0 && e == 0 {
  228. panic(0)
  229. }
  230. if m == 1 {
  231. return 0
  232. }
  233. r := uint32(1)
  234. for b, m := uint32(b), uint32(m); e > 0; b, e = b*b%m, e>>1 {
  235. if e&1 == 1 {
  236. r = r * b % m
  237. }
  238. }
  239. return uint16(r)
  240. }
  241. // ModPowUint32 computes (b^e)%m. It panics for m == 0 || b == e == 0.
  242. func ModPowUint32(b, e, m uint32) uint32 {
  243. if b == 0 && e == 0 {
  244. panic(0)
  245. }
  246. if m == 1 {
  247. return 0
  248. }
  249. r := uint64(1)
  250. for b, m := uint64(b), uint64(m); e > 0; b, e = b*b%m, e>>1 {
  251. if e&1 == 1 {
  252. r = r * b % m
  253. }
  254. }
  255. return uint32(r)
  256. }
  257. // ModPowUint64 computes (b^e)%m. It panics for m == 0 || b == e == 0.
  258. func ModPowUint64(b, e, m uint64) (r uint64) {
  259. if b == 0 && e == 0 {
  260. panic(0)
  261. }
  262. if m == 1 {
  263. return 0
  264. }
  265. return modPowBigInt(big.NewInt(0).SetUint64(b), big.NewInt(0).SetUint64(e), big.NewInt(0).SetUint64(m)).Uint64()
  266. }
  267. func modPowBigInt(b, e, m *big.Int) (r *big.Int) {
  268. r = big.NewInt(1)
  269. for i, n := 0, e.BitLen(); i < n; i++ {
  270. if e.Bit(i) != 0 {
  271. r.Mod(r.Mul(r, b), m)
  272. }
  273. b.Mod(b.Mul(b, b), m)
  274. }
  275. return
  276. }
  277. // ModPowBigInt computes (b^e)%m. Returns nil for e < 0. It panics for m == 0 || b == e == 0.
  278. func ModPowBigInt(b, e, m *big.Int) (r *big.Int) {
  279. if b.Sign() == 0 && e.Sign() == 0 {
  280. panic(0)
  281. }
  282. if m.Cmp(_1) == 0 {
  283. return big.NewInt(0)
  284. }
  285. if e.Sign() < 0 {
  286. return
  287. }
  288. return modPowBigInt(big.NewInt(0).Set(b), big.NewInt(0).Set(e), m)
  289. }
  290. var uint64ToBigIntDelta big.Int
  291. func init() {
  292. uint64ToBigIntDelta.SetBit(&uint64ToBigIntDelta, 63, 1)
  293. }
  294. var uintptrBits int
  295. func init() {
  296. x := uint64(math.MaxUint64)
  297. uintptrBits = BitLenUintptr(uintptr(x))
  298. }
  299. // UintptrBits returns the bit width of an uintptr at the executing machine.
  300. func UintptrBits() int {
  301. return uintptrBits
  302. }
  303. // AddUint128_64 returns the uint128 sum of uint64 a and b.
  304. func AddUint128_64(a, b uint64) (hi uint64, lo uint64) {
  305. lo = a + b
  306. if lo < a {
  307. hi = 1
  308. }
  309. return
  310. }
  311. // MulUint128_64 returns the uint128 bit product of uint64 a and b.
  312. func MulUint128_64(a, b uint64) (hi, lo uint64) {
  313. /*
  314. 2^(2 W) ahi bhi + 2^W alo bhi + 2^W ahi blo + alo blo
  315. FEDCBA98 76543210 FEDCBA98 76543210
  316. ---- alo*blo ----
  317. ---- alo*bhi ----
  318. ---- ahi*blo ----
  319. ---- ahi*bhi ----
  320. */
  321. const w = 32
  322. const m = 1<<w - 1
  323. ahi, bhi, alo, blo := a>>w, b>>w, a&m, b&m
  324. lo = alo * blo
  325. mid1 := alo * bhi
  326. mid2 := ahi * blo
  327. c1, lo := AddUint128_64(lo, mid1<<w)
  328. c2, lo := AddUint128_64(lo, mid2<<w)
  329. _, hi = AddUint128_64(ahi*bhi, mid1>>w+mid2>>w+uint64(c1+c2))
  330. return
  331. }
  332. // PowerizeBigInt returns (e, p) such that e is the smallest number for which p
  333. // == b^e is greater or equal n. For n < 0 or b < 2 (0, nil) is returned.
  334. //
  335. // NOTE: Run time for large values of n (above about 2^1e6 ~= 1e300000) can be
  336. // significant and/or unacceptabe. For any smaller values of n the function
  337. // typically performs in sub second time. For "small" values of n (cca bellow
  338. // 2^1e3 ~= 1e300) the same can be easily below 10 µs.
  339. //
  340. // A special (and trivial) case of b == 2 is handled separately and performs
  341. // much faster.
  342. func PowerizeBigInt(b, n *big.Int) (e uint32, p *big.Int) {
  343. switch {
  344. case b.Cmp(_2) < 0 || n.Sign() < 0:
  345. return
  346. case n.Sign() == 0 || n.Cmp(_1) == 0:
  347. return 0, big.NewInt(1)
  348. case b.Cmp(_2) == 0:
  349. p = big.NewInt(0)
  350. e = uint32(n.BitLen() - 1)
  351. p.SetBit(p, int(e), 1)
  352. if p.Cmp(n) < 0 {
  353. p.Mul(p, _2)
  354. e++
  355. }
  356. return
  357. }
  358. bw := b.BitLen()
  359. nw := n.BitLen()
  360. p = big.NewInt(1)
  361. var bb, r big.Int
  362. for {
  363. switch p.Cmp(n) {
  364. case -1:
  365. x := uint32((nw - p.BitLen()) / bw)
  366. if x == 0 {
  367. x = 1
  368. }
  369. e += x
  370. switch x {
  371. case 1:
  372. p.Mul(p, b)
  373. default:
  374. r.Set(_1)
  375. bb.Set(b)
  376. e := x
  377. for {
  378. if e&1 != 0 {
  379. r.Mul(&r, &bb)
  380. }
  381. if e >>= 1; e == 0 {
  382. break
  383. }
  384. bb.Mul(&bb, &bb)
  385. }
  386. p.Mul(p, &r)
  387. }
  388. case 0, 1:
  389. return
  390. }
  391. }
  392. }
  393. // PowerizeUint32BigInt returns (e, p) such that e is the smallest number for
  394. // which p == b^e is greater or equal n. For n < 0 or b < 2 (0, nil) is
  395. // returned.
  396. //
  397. // More info: see PowerizeBigInt.
  398. func PowerizeUint32BigInt(b uint32, n *big.Int) (e uint32, p *big.Int) {
  399. switch {
  400. case b < 2 || n.Sign() < 0:
  401. return
  402. case n.Sign() == 0 || n.Cmp(_1) == 0:
  403. return 0, big.NewInt(1)
  404. case b == 2:
  405. p = big.NewInt(0)
  406. e = uint32(n.BitLen() - 1)
  407. p.SetBit(p, int(e), 1)
  408. if p.Cmp(n) < 0 {
  409. p.Mul(p, _2)
  410. e++
  411. }
  412. return
  413. }
  414. var bb big.Int
  415. bb.SetInt64(int64(b))
  416. return PowerizeBigInt(&bb, n)
  417. }
  418. /*
  419. ProbablyPrimeUint32 returns true if n is prime or n is a pseudoprime to base a.
  420. It implements the Miller-Rabin primality test for one specific value of 'a' and
  421. k == 1.
  422. Wrt pseudocode shown at
  423. http://en.wikipedia.org/wiki/Miller-Rabin_primality_test#Algorithm_and_running_time
  424. Input: n > 3, an odd integer to be tested for primality;
  425. Input: k, a parameter that determines the accuracy of the test
  426. Output: composite if n is composite, otherwise probably prime
  427. write n − 1 as 2^s·d with d odd by factoring powers of 2 from n − 1
  428. LOOP: repeat k times:
  429. pick a random integer a in the range [2, n − 2]
  430. x ← a^d mod n
  431. if x = 1 or x = n − 1 then do next LOOP
  432. for r = 1 .. s − 1
  433. x ← x^2 mod n
  434. if x = 1 then return composite
  435. if x = n − 1 then do next LOOP
  436. return composite
  437. return probably prime
  438. ... this function behaves like passing 1 for 'k' and additionally a
  439. fixed/non-random 'a'. Otherwise it's the same algorithm.
  440. See also: http://mathworld.wolfram.com/Rabin-MillerStrongPseudoprimeTest.html
  441. */
  442. func ProbablyPrimeUint32(n, a uint32) bool {
  443. d, s := n-1, 0
  444. for ; d&1 == 0; d, s = d>>1, s+1 {
  445. }
  446. x := uint64(ModPowUint32(a, d, n))
  447. if x == 1 || uint32(x) == n-1 {
  448. return true
  449. }
  450. for ; s > 1; s-- {
  451. if x = x * x % uint64(n); x == 1 {
  452. return false
  453. }
  454. if uint32(x) == n-1 {
  455. return true
  456. }
  457. }
  458. return false
  459. }
  460. // ProbablyPrimeUint64_32 returns true if n is prime or n is a pseudoprime to
  461. // base a. It implements the Miller-Rabin primality test for one specific value
  462. // of 'a' and k == 1. See also ProbablyPrimeUint32.
  463. func ProbablyPrimeUint64_32(n uint64, a uint32) bool {
  464. d, s := n-1, 0
  465. for ; d&1 == 0; d, s = d>>1, s+1 {
  466. }
  467. x := ModPowUint64(uint64(a), d, n)
  468. if x == 1 || x == n-1 {
  469. return true
  470. }
  471. bx, bn := big.NewInt(0).SetUint64(x), big.NewInt(0).SetUint64(n)
  472. for ; s > 1; s-- {
  473. if x = bx.Mod(bx.Mul(bx, bx), bn).Uint64(); x == 1 {
  474. return false
  475. }
  476. if x == n-1 {
  477. return true
  478. }
  479. }
  480. return false
  481. }
  482. // ProbablyPrimeBigInt_32 returns true if n is prime or n is a pseudoprime to
  483. // base a. It implements the Miller-Rabin primality test for one specific value
  484. // of 'a' and k == 1. See also ProbablyPrimeUint32.
  485. func ProbablyPrimeBigInt_32(n *big.Int, a uint32) bool {
  486. var d big.Int
  487. d.Set(n)
  488. d.Sub(&d, _1) // d <- n-1
  489. s := 0
  490. for ; d.Bit(s) == 0; s++ {
  491. }
  492. nMinus1 := big.NewInt(0).Set(&d)
  493. d.Rsh(&d, uint(s))
  494. x := ModPowBigInt(big.NewInt(int64(a)), &d, n)
  495. if x.Cmp(_1) == 0 || x.Cmp(nMinus1) == 0 {
  496. return true
  497. }
  498. for ; s > 1; s-- {
  499. if x = x.Mod(x.Mul(x, x), n); x.Cmp(_1) == 0 {
  500. return false
  501. }
  502. if x.Cmp(nMinus1) == 0 {
  503. return true
  504. }
  505. }
  506. return false
  507. }
  508. // ProbablyPrimeBigInt returns true if n is prime or n is a pseudoprime to base
  509. // a. It implements the Miller-Rabin primality test for one specific value of
  510. // 'a' and k == 1. See also ProbablyPrimeUint32.
  511. func ProbablyPrimeBigInt(n, a *big.Int) bool {
  512. var d big.Int
  513. d.Set(n)
  514. d.Sub(&d, _1) // d <- n-1
  515. s := 0
  516. for ; d.Bit(s) == 0; s++ {
  517. }
  518. nMinus1 := big.NewInt(0).Set(&d)
  519. d.Rsh(&d, uint(s))
  520. x := ModPowBigInt(a, &d, n)
  521. if x.Cmp(_1) == 0 || x.Cmp(nMinus1) == 0 {
  522. return true
  523. }
  524. for ; s > 1; s-- {
  525. if x = x.Mod(x.Mul(x, x), n); x.Cmp(_1) == 0 {
  526. return false
  527. }
  528. if x.Cmp(nMinus1) == 0 {
  529. return true
  530. }
  531. }
  532. return false
  533. }
  534. // Max returns the larger of a and b.
  535. func Max(a, b int) int {
  536. if a > b {
  537. return a
  538. }
  539. return b
  540. }
  541. // Min returns the smaller of a and b.
  542. func Min(a, b int) int {
  543. if a < b {
  544. return a
  545. }
  546. return b
  547. }
  548. // UMax returns the larger of a and b.
  549. func UMax(a, b uint) uint {
  550. if a > b {
  551. return a
  552. }
  553. return b
  554. }
  555. // UMin returns the smaller of a and b.
  556. func UMin(a, b uint) uint {
  557. if a < b {
  558. return a
  559. }
  560. return b
  561. }
  562. // MaxByte returns the larger of a and b.
  563. func MaxByte(a, b byte) byte {
  564. if a > b {
  565. return a
  566. }
  567. return b
  568. }
  569. // MinByte returns the smaller of a and b.
  570. func MinByte(a, b byte) byte {
  571. if a < b {
  572. return a
  573. }
  574. return b
  575. }
  576. // MaxInt8 returns the larger of a and b.
  577. func MaxInt8(a, b int8) int8 {
  578. if a > b {
  579. return a
  580. }
  581. return b
  582. }
  583. // MinInt8 returns the smaller of a and b.
  584. func MinInt8(a, b int8) int8 {
  585. if a < b {
  586. return a
  587. }
  588. return b
  589. }
  590. // MaxUint16 returns the larger of a and b.
  591. func MaxUint16(a, b uint16) uint16 {
  592. if a > b {
  593. return a
  594. }
  595. return b
  596. }
  597. // MinUint16 returns the smaller of a and b.
  598. func MinUint16(a, b uint16) uint16 {
  599. if a < b {
  600. return a
  601. }
  602. return b
  603. }
  604. // MaxInt16 returns the larger of a and b.
  605. func MaxInt16(a, b int16) int16 {
  606. if a > b {
  607. return a
  608. }
  609. return b
  610. }
  611. // MinInt16 returns the smaller of a and b.
  612. func MinInt16(a, b int16) int16 {
  613. if a < b {
  614. return a
  615. }
  616. return b
  617. }
  618. // MaxUint32 returns the larger of a and b.
  619. func MaxUint32(a, b uint32) uint32 {
  620. if a > b {
  621. return a
  622. }
  623. return b
  624. }
  625. // MinUint32 returns the smaller of a and b.
  626. func MinUint32(a, b uint32) uint32 {
  627. if a < b {
  628. return a
  629. }
  630. return b
  631. }
  632. // MaxInt32 returns the larger of a and b.
  633. func MaxInt32(a, b int32) int32 {
  634. if a > b {
  635. return a
  636. }
  637. return b
  638. }
  639. // MinInt32 returns the smaller of a and b.
  640. func MinInt32(a, b int32) int32 {
  641. if a < b {
  642. return a
  643. }
  644. return b
  645. }
  646. // MaxUint64 returns the larger of a and b.
  647. func MaxUint64(a, b uint64) uint64 {
  648. if a > b {
  649. return a
  650. }
  651. return b
  652. }
  653. // MinUint64 returns the smaller of a and b.
  654. func MinUint64(a, b uint64) uint64 {
  655. if a < b {
  656. return a
  657. }
  658. return b
  659. }
  660. // MaxInt64 returns the larger of a and b.
  661. func MaxInt64(a, b int64) int64 {
  662. if a > b {
  663. return a
  664. }
  665. return b
  666. }
  667. // MinInt64 returns the smaller of a and b.
  668. func MinInt64(a, b int64) int64 {
  669. if a < b {
  670. return a
  671. }
  672. return b
  673. }
  674. // ToBase produces n in base b. For example
  675. //
  676. // ToBase(2047, 22) -> [1, 5, 4]
  677. //
  678. // 1 * 22^0 1
  679. // 5 * 22^1 110
  680. // 4 * 22^2 1936
  681. // ----
  682. // 2047
  683. //
  684. // ToBase panics for bases < 2.
  685. func ToBase(n *big.Int, b int) []int {
  686. var nn big.Int
  687. nn.Set(n)
  688. if b < 2 {
  689. panic("invalid base")
  690. }
  691. k := 1
  692. switch nn.Sign() {
  693. case -1:
  694. nn.Neg(&nn)
  695. k = -1
  696. case 0:
  697. return []int{0}
  698. }
  699. bb := big.NewInt(int64(b))
  700. var r []int
  701. rem := big.NewInt(0)
  702. for nn.Sign() != 0 {
  703. nn.QuoRem(&nn, bb, rem)
  704. r = append(r, k*int(rem.Int64()))
  705. }
  706. return r
  707. }