mathutil.go 16 KB

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