mathutil.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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. // 2020-12-20 v1.2.1 fixes MulOverflowInt64.
  10. //
  11. // 2020-12-19 Added {Add,Sub,Mul}OverflowInt{8,16,32,64}
  12. //
  13. // 2018-10-21 Added BinaryLog
  14. //
  15. // 2018-04-25: New functions for determining Max/Min of nullable values. Ex:
  16. //
  17. // func MaxPtr(a, b *int) *int {
  18. // func MinPtr(a, b *int) *int {
  19. // func MaxBytePtr(a, b *byte) *byte {
  20. // func MinBytePtr(a, b *byte) *byte {
  21. // ...
  22. //
  23. // 2017-10-14: New variadic functions for Max/Min. Ex:
  24. //
  25. // func MaxVal(val int, vals ...int) int {
  26. // func MinVal(val int, vals ...int) int {
  27. // func MaxByteVal(val byte, vals ...byte) byte {
  28. // func MinByteVal(val byte, vals ...byte) byte {
  29. // ...
  30. //
  31. // 2016-10-10: New functions QuadPolyDiscriminant and QuadPolyFactors.
  32. //
  33. // 2013-12-13: The following functions have been REMOVED
  34. //
  35. // func Uint64ToBigInt(n uint64) *big.Int
  36. // func Uint64FromBigInt(n *big.Int) (uint64, bool)
  37. //
  38. // 2013-05-13: The following functions are now DEPRECATED
  39. //
  40. // func Uint64ToBigInt(n uint64) *big.Int
  41. // func Uint64FromBigInt(n *big.Int) (uint64, bool)
  42. //
  43. // These functions will be REMOVED with Go release 1.1+1.
  44. //
  45. // 2013-01-21: The following functions have been REMOVED
  46. //
  47. // func MaxInt() int
  48. // func MinInt() int
  49. // func MaxUint() uint
  50. // func UintPtrBits() int
  51. //
  52. // They are now replaced by untyped constants
  53. //
  54. // MaxInt
  55. // MinInt
  56. // MaxUint
  57. // UintPtrBits
  58. //
  59. // Additionally one more untyped constant was added
  60. //
  61. // IntBits
  62. //
  63. // This change breaks any existing code depending on the above removed
  64. // functions. They should have not been published in the first place, that was
  65. // unfortunate. Instead, defining such architecture and/or implementation
  66. // specific integer limits and bit widths as untyped constants improves
  67. // performance and allows for static dead code elimination if it depends on
  68. // these values. Thanks to minux for pointing it out in the mail list
  69. // (https://groups.google.com/d/msg/golang-nuts/tlPpLW6aJw8/NT3mpToH-a4J).
  70. //
  71. // 2012-12-12: The following functions will be DEPRECATED with Go release
  72. // 1.0.3+1 and REMOVED with Go release 1.0.3+2, b/c of
  73. // http://code.google.com/p/go/source/detail?r=954a79ee3ea8
  74. //
  75. // func Uint64ToBigInt(n uint64) *big.Int
  76. // func Uint64FromBigInt(n *big.Int) (uint64, bool)
  77. package mathutil // import "modernc.org/mathutil"
  78. import (
  79. "math"
  80. "math/big"
  81. )
  82. // Architecture and/or implementation specific integer limits and bit widths.
  83. const (
  84. MaxInt = 1<<(IntBits-1) - 1
  85. MinInt = -MaxInt - 1
  86. MaxUint = 1<<IntBits - 1
  87. IntBits = 1 << (^uint(0)>>32&1 + ^uint(0)>>16&1 + ^uint(0)>>8&1 + 3)
  88. UintPtrBits = 1 << (^uintptr(0)>>32&1 + ^uintptr(0)>>16&1 + ^uintptr(0)>>8&1 + 3)
  89. )
  90. var (
  91. _1 = big.NewInt(1)
  92. _2 = big.NewInt(2)
  93. )
  94. // GCDByte returns the greatest common divisor of a and b. Based on:
  95. // http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
  96. func GCDByte(a, b byte) byte {
  97. for b != 0 {
  98. a, b = b, a%b
  99. }
  100. return a
  101. }
  102. // GCDUint16 returns the greatest common divisor of a and b.
  103. func GCDUint16(a, b uint16) uint16 {
  104. for b != 0 {
  105. a, b = b, a%b
  106. }
  107. return a
  108. }
  109. // GCDUint32 returns the greatest common divisor of a and b.
  110. func GCDUint32(a, b uint32) uint32 {
  111. for b != 0 {
  112. a, b = b, a%b
  113. }
  114. return a
  115. }
  116. // GCDUint64 returns the greatest common divisor of a and b.
  117. func GCDUint64(a, b uint64) uint64 {
  118. for b != 0 {
  119. a, b = b, a%b
  120. }
  121. return a
  122. }
  123. // ISqrt returns floor(sqrt(n)). Typical run time is few hundreds of ns.
  124. func ISqrt(n uint32) (x uint32) {
  125. if n == 0 {
  126. return
  127. }
  128. if n >= math.MaxUint16*math.MaxUint16 {
  129. return math.MaxUint16
  130. }
  131. var px, nx uint32
  132. for x = n; ; px, x = x, nx {
  133. nx = (x + n/x) / 2
  134. if nx == x || nx == px {
  135. break
  136. }
  137. }
  138. return
  139. }
  140. // SqrtUint64 returns floor(sqrt(n)). Typical run time is about 0.5 µs.
  141. func SqrtUint64(n uint64) (x uint64) {
  142. if n == 0 {
  143. return
  144. }
  145. if n >= math.MaxUint32*math.MaxUint32 {
  146. return math.MaxUint32
  147. }
  148. var px, nx uint64
  149. for x = n; ; px, x = x, nx {
  150. nx = (x + n/x) / 2
  151. if nx == x || nx == px {
  152. break
  153. }
  154. }
  155. return
  156. }
  157. // SqrtBig returns floor(sqrt(n)). It panics on n < 0.
  158. func SqrtBig(n *big.Int) (x *big.Int) {
  159. switch n.Sign() {
  160. case -1:
  161. panic(-1)
  162. case 0:
  163. return big.NewInt(0)
  164. }
  165. var px, nx big.Int
  166. x = big.NewInt(0)
  167. x.SetBit(x, n.BitLen()/2+1, 1)
  168. for {
  169. nx.Rsh(nx.Add(x, nx.Div(n, x)), 1)
  170. if nx.Cmp(x) == 0 || nx.Cmp(&px) == 0 {
  171. break
  172. }
  173. px.Set(x)
  174. x.Set(&nx)
  175. }
  176. return
  177. }
  178. // Log2Byte returns log base 2 of n. It's the same as index of the highest
  179. // bit set in n. For n == 0 -1 is returned.
  180. func Log2Byte(n byte) int {
  181. return log2[n]
  182. }
  183. // Log2Uint16 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 Log2Uint16(n uint16) int {
  186. if b := n >> 8; b != 0 {
  187. return log2[b] + 8
  188. }
  189. return log2[n]
  190. }
  191. // Log2Uint32 returns log base 2 of n. It's the same as index of the highest
  192. // bit set in n. For n == 0 -1 is returned.
  193. func Log2Uint32(n uint32) int {
  194. if b := n >> 24; b != 0 {
  195. return log2[b] + 24
  196. }
  197. if b := n >> 16; b != 0 {
  198. return log2[b] + 16
  199. }
  200. if b := n >> 8; b != 0 {
  201. return log2[b] + 8
  202. }
  203. return log2[n]
  204. }
  205. // Log2Uint64 returns log base 2 of n. It's the same as index of the highest
  206. // bit set in n. For n == 0 -1 is returned.
  207. func Log2Uint64(n uint64) int {
  208. if b := n >> 56; b != 0 {
  209. return log2[b] + 56
  210. }
  211. if b := n >> 48; b != 0 {
  212. return log2[b] + 48
  213. }
  214. if b := n >> 40; b != 0 {
  215. return log2[b] + 40
  216. }
  217. if b := n >> 32; b != 0 {
  218. return log2[b] + 32
  219. }
  220. if b := n >> 24; b != 0 {
  221. return log2[b] + 24
  222. }
  223. if b := n >> 16; b != 0 {
  224. return log2[b] + 16
  225. }
  226. if b := n >> 8; b != 0 {
  227. return log2[b] + 8
  228. }
  229. return log2[n]
  230. }
  231. // ModPowByte computes (b^e)%m. It panics for m == 0 || b == e == 0.
  232. //
  233. // See also: http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method
  234. func ModPowByte(b, e, m byte) byte {
  235. if b == 0 && e == 0 {
  236. panic(0)
  237. }
  238. if m == 1 {
  239. return 0
  240. }
  241. r := uint16(1)
  242. for b, m := uint16(b), uint16(m); e > 0; b, e = b*b%m, e>>1 {
  243. if e&1 == 1 {
  244. r = r * b % m
  245. }
  246. }
  247. return byte(r)
  248. }
  249. // ModPowUint16 computes (b^e)%m. It panics for m == 0 || b == e == 0.
  250. func ModPowUint16(b, e, m uint16) uint16 {
  251. if b == 0 && e == 0 {
  252. panic(0)
  253. }
  254. if m == 1 {
  255. return 0
  256. }
  257. r := uint32(1)
  258. for b, m := uint32(b), uint32(m); e > 0; b, e = b*b%m, e>>1 {
  259. if e&1 == 1 {
  260. r = r * b % m
  261. }
  262. }
  263. return uint16(r)
  264. }
  265. // ModPowUint32 computes (b^e)%m. It panics for m == 0 || b == e == 0.
  266. func ModPowUint32(b, e, m uint32) uint32 {
  267. if b == 0 && e == 0 {
  268. panic(0)
  269. }
  270. if m == 1 {
  271. return 0
  272. }
  273. r := uint64(1)
  274. for b, m := uint64(b), uint64(m); e > 0; b, e = b*b%m, e>>1 {
  275. if e&1 == 1 {
  276. r = r * b % m
  277. }
  278. }
  279. return uint32(r)
  280. }
  281. // ModPowUint64 computes (b^e)%m. It panics for m == 0 || b == e == 0.
  282. func ModPowUint64(b, e, m uint64) (r uint64) {
  283. if b == 0 && e == 0 {
  284. panic(0)
  285. }
  286. if m == 1 {
  287. return 0
  288. }
  289. return modPowBigInt(big.NewInt(0).SetUint64(b), big.NewInt(0).SetUint64(e), big.NewInt(0).SetUint64(m)).Uint64()
  290. }
  291. func modPowBigInt(b, e, m *big.Int) (r *big.Int) {
  292. r = big.NewInt(1)
  293. for i, n := 0, e.BitLen(); i < n; i++ {
  294. if e.Bit(i) != 0 {
  295. r.Mod(r.Mul(r, b), m)
  296. }
  297. b.Mod(b.Mul(b, b), m)
  298. }
  299. return
  300. }
  301. // ModPowBigInt computes (b^e)%m. Returns nil for e < 0. It panics for m == 0 || b == e == 0.
  302. func ModPowBigInt(b, e, m *big.Int) (r *big.Int) {
  303. if b.Sign() == 0 && e.Sign() == 0 {
  304. panic(0)
  305. }
  306. if m.Cmp(_1) == 0 {
  307. return big.NewInt(0)
  308. }
  309. if e.Sign() < 0 {
  310. return
  311. }
  312. return modPowBigInt(big.NewInt(0).Set(b), big.NewInt(0).Set(e), m)
  313. }
  314. var uint64ToBigIntDelta big.Int
  315. func init() {
  316. uint64ToBigIntDelta.SetBit(&uint64ToBigIntDelta, 63, 1)
  317. }
  318. var uintptrBits int
  319. func init() {
  320. x := uint64(math.MaxUint64)
  321. uintptrBits = BitLenUintptr(uintptr(x))
  322. }
  323. // UintptrBits returns the bit width of an uintptr at the executing machine.
  324. func UintptrBits() int {
  325. return uintptrBits
  326. }
  327. // AddUint128_64 returns the uint128 sum of uint64 a and b.
  328. func AddUint128_64(a, b uint64) (hi uint64, lo uint64) {
  329. lo = a + b
  330. if lo < a {
  331. hi = 1
  332. }
  333. return hi, lo
  334. }
  335. // MulUint128_64 returns the uint128 bit product of uint64 a and b.
  336. func MulUint128_64(a, b uint64) (hi, lo uint64) {
  337. /*
  338. 2^(2 W) ahi bhi + 2^W alo bhi + 2^W ahi blo + alo blo
  339. FEDCBA98 76543210 FEDCBA98 76543210
  340. ---- alo*blo ----
  341. ---- alo*bhi ----
  342. ---- ahi*blo ----
  343. ---- ahi*bhi ----
  344. */
  345. const w = 32
  346. const m = 1<<w - 1
  347. ahi, bhi, alo, blo := a>>w, b>>w, a&m, b&m
  348. lo = alo * blo
  349. mid1 := alo * bhi
  350. mid2 := ahi * blo
  351. c1, lo := AddUint128_64(lo, mid1<<w)
  352. c2, lo := AddUint128_64(lo, mid2<<w)
  353. _, hi = AddUint128_64(ahi*bhi, mid1>>w+mid2>>w+c1+c2)
  354. return
  355. }
  356. // PowerizeBigInt returns (e, p) such that e is the smallest number for which p
  357. // == b^e is greater or equal n. For n < 0 or b < 2 (0, nil) is returned.
  358. //
  359. // NOTE: Run time for large values of n (above about 2^1e6 ~= 1e300000) can be
  360. // significant and/or unacceptabe. For any smaller values of n the function
  361. // typically performs in sub second time. For "small" values of n (cca bellow
  362. // 2^1e3 ~= 1e300) the same can be easily below 10 µs.
  363. //
  364. // A special (and trivial) case of b == 2 is handled separately and performs
  365. // much faster.
  366. func PowerizeBigInt(b, n *big.Int) (e uint32, p *big.Int) {
  367. switch {
  368. case b.Cmp(_2) < 0 || n.Sign() < 0:
  369. return
  370. case n.Sign() == 0 || n.Cmp(_1) == 0:
  371. return 0, big.NewInt(1)
  372. case b.Cmp(_2) == 0:
  373. p = big.NewInt(0)
  374. e = uint32(n.BitLen() - 1)
  375. p.SetBit(p, int(e), 1)
  376. if p.Cmp(n) < 0 {
  377. p.Mul(p, _2)
  378. e++
  379. }
  380. return
  381. }
  382. bw := b.BitLen()
  383. nw := n.BitLen()
  384. p = big.NewInt(1)
  385. var bb, r big.Int
  386. for {
  387. switch p.Cmp(n) {
  388. case -1:
  389. x := uint32((nw - p.BitLen()) / bw)
  390. if x == 0 {
  391. x = 1
  392. }
  393. e += x
  394. switch x {
  395. case 1:
  396. p.Mul(p, b)
  397. default:
  398. r.Set(_1)
  399. bb.Set(b)
  400. e := x
  401. for {
  402. if e&1 != 0 {
  403. r.Mul(&r, &bb)
  404. }
  405. if e >>= 1; e == 0 {
  406. break
  407. }
  408. bb.Mul(&bb, &bb)
  409. }
  410. p.Mul(p, &r)
  411. }
  412. case 0, 1:
  413. return
  414. }
  415. }
  416. }
  417. // PowerizeUint32BigInt returns (e, p) such that e is the smallest number for
  418. // which p == b^e is greater or equal n. For n < 0 or b < 2 (0, nil) is
  419. // returned.
  420. //
  421. // More info: see PowerizeBigInt.
  422. func PowerizeUint32BigInt(b uint32, n *big.Int) (e uint32, p *big.Int) {
  423. switch {
  424. case b < 2 || n.Sign() < 0:
  425. return
  426. case n.Sign() == 0 || n.Cmp(_1) == 0:
  427. return 0, big.NewInt(1)
  428. case b == 2:
  429. p = big.NewInt(0)
  430. e = uint32(n.BitLen() - 1)
  431. p.SetBit(p, int(e), 1)
  432. if p.Cmp(n) < 0 {
  433. p.Mul(p, _2)
  434. e++
  435. }
  436. return
  437. }
  438. var bb big.Int
  439. bb.SetInt64(int64(b))
  440. return PowerizeBigInt(&bb, n)
  441. }
  442. /*
  443. ProbablyPrimeUint32 returns true if n is prime or n is a pseudoprime to base a.
  444. It implements the Miller-Rabin primality test for one specific value of 'a' and
  445. k == 1.
  446. Wrt pseudocode shown at
  447. http://en.wikipedia.org/wiki/Miller-Rabin_primality_test#Algorithm_and_running_time
  448. Input: n > 3, an odd integer to be tested for primality;
  449. Input: k, a parameter that determines the accuracy of the test
  450. Output: composite if n is composite, otherwise probably prime
  451. write n − 1 as 2^s·d with d odd by factoring powers of 2 from n − 1
  452. LOOP: repeat k times:
  453. pick a random integer a in the range [2, n − 2]
  454. x ← a^d mod n
  455. if x = 1 or x = n − 1 then do next LOOP
  456. for r = 1 .. s − 1
  457. x ← x^2 mod n
  458. if x = 1 then return composite
  459. if x = n − 1 then do next LOOP
  460. return composite
  461. return probably prime
  462. ... this function behaves like passing 1 for 'k' and additionally a
  463. fixed/non-random 'a'. Otherwise it's the same algorithm.
  464. See also: http://mathworld.wolfram.com/Rabin-MillerStrongPseudoprimeTest.html
  465. */
  466. func ProbablyPrimeUint32(n, a uint32) bool {
  467. d, s := n-1, 0
  468. for ; d&1 == 0; d, s = d>>1, s+1 {
  469. }
  470. x := uint64(ModPowUint32(a, d, n))
  471. if x == 1 || uint32(x) == n-1 {
  472. return true
  473. }
  474. for ; s > 1; s-- {
  475. if x = x * x % uint64(n); x == 1 {
  476. return false
  477. }
  478. if uint32(x) == n-1 {
  479. return true
  480. }
  481. }
  482. return false
  483. }
  484. // ProbablyPrimeUint64_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 ProbablyPrimeUint64_32(n uint64, a uint32) bool {
  488. d, s := n-1, 0
  489. for ; d&1 == 0; d, s = d>>1, s+1 {
  490. }
  491. x := ModPowUint64(uint64(a), d, n)
  492. if x == 1 || x == n-1 {
  493. return true
  494. }
  495. bx, bn := big.NewInt(0).SetUint64(x), big.NewInt(0).SetUint64(n)
  496. for ; s > 1; s-- {
  497. if x = bx.Mod(bx.Mul(bx, bx), bn).Uint64(); x == 1 {
  498. return false
  499. }
  500. if x == n-1 {
  501. return true
  502. }
  503. }
  504. return false
  505. }
  506. // ProbablyPrimeBigInt_32 returns true if n is prime or n is a pseudoprime to
  507. // base a. It implements the Miller-Rabin primality test for one specific value
  508. // of 'a' and k == 1. See also ProbablyPrimeUint32.
  509. func ProbablyPrimeBigInt_32(n *big.Int, a uint32) bool {
  510. var d big.Int
  511. d.Set(n)
  512. d.Sub(&d, _1) // d <- n-1
  513. s := 0
  514. for ; d.Bit(s) == 0; s++ {
  515. }
  516. nMinus1 := big.NewInt(0).Set(&d)
  517. d.Rsh(&d, uint(s))
  518. x := ModPowBigInt(big.NewInt(int64(a)), &d, n)
  519. if x.Cmp(_1) == 0 || x.Cmp(nMinus1) == 0 {
  520. return true
  521. }
  522. for ; s > 1; s-- {
  523. if x = x.Mod(x.Mul(x, x), n); x.Cmp(_1) == 0 {
  524. return false
  525. }
  526. if x.Cmp(nMinus1) == 0 {
  527. return true
  528. }
  529. }
  530. return false
  531. }
  532. // ProbablyPrimeBigInt returns true if n is prime or n is a pseudoprime to base
  533. // a. It implements the Miller-Rabin primality test for one specific value of
  534. // 'a' and k == 1. See also ProbablyPrimeUint32.
  535. func ProbablyPrimeBigInt(n, a *big.Int) bool {
  536. var d big.Int
  537. d.Set(n)
  538. d.Sub(&d, _1) // d <- n-1
  539. s := 0
  540. for ; d.Bit(s) == 0; s++ {
  541. }
  542. nMinus1 := big.NewInt(0).Set(&d)
  543. d.Rsh(&d, uint(s))
  544. x := ModPowBigInt(a, &d, n)
  545. if x.Cmp(_1) == 0 || x.Cmp(nMinus1) == 0 {
  546. return true
  547. }
  548. for ; s > 1; s-- {
  549. if x = x.Mod(x.Mul(x, x), n); x.Cmp(_1) == 0 {
  550. return false
  551. }
  552. if x.Cmp(nMinus1) == 0 {
  553. return true
  554. }
  555. }
  556. return false
  557. }
  558. // Max returns the larger of a and b.
  559. func Max(a, b int) int {
  560. if a > b {
  561. return a
  562. }
  563. return b
  564. }
  565. // Min returns the smaller of a and b.
  566. func Min(a, b int) int {
  567. if a < b {
  568. return a
  569. }
  570. return b
  571. }
  572. // MaxPtr returns a pointer to the larger of a and b, or nil.
  573. func MaxPtr(a, b *int) *int {
  574. if a == nil {
  575. return b
  576. }
  577. if b == nil {
  578. return a
  579. }
  580. if *a > *b {
  581. return a
  582. }
  583. return b
  584. }
  585. // MinPtr returns a pointer to the smaller of a and b, or nil.
  586. func MinPtr(a, b *int) *int {
  587. if a == nil {
  588. return b
  589. }
  590. if b == nil {
  591. return a
  592. }
  593. if *a < *b {
  594. return a
  595. }
  596. return b
  597. }
  598. // MaxVal returns the largest argument passed.
  599. func MaxVal(val int, vals ...int) int {
  600. res := val
  601. for _, v := range vals {
  602. if v > res {
  603. res = v
  604. }
  605. }
  606. return res
  607. }
  608. // MinVal returns the smallest argument passed.
  609. func MinVal(val int, vals ...int) int {
  610. res := val
  611. for _, v := range vals {
  612. if v < res {
  613. res = v
  614. }
  615. }
  616. return res
  617. }
  618. // Clamp returns a value restricted between lo and hi.
  619. func Clamp(v, lo, hi int) int {
  620. return Min(Max(v, lo), hi)
  621. }
  622. // UMax returns the larger of a and b.
  623. func UMax(a, b uint) uint {
  624. if a > b {
  625. return a
  626. }
  627. return b
  628. }
  629. // UMin returns the smaller of a and b.
  630. func UMin(a, b uint) uint {
  631. if a < b {
  632. return a
  633. }
  634. return b
  635. }
  636. // UMaxPtr returns a pointer to the larger of a and b, or nil.
  637. func UMaxPtr(a, b *uint) *uint {
  638. if a == nil {
  639. return b
  640. }
  641. if b == nil {
  642. return a
  643. }
  644. if *a > *b {
  645. return a
  646. }
  647. return b
  648. }
  649. // UMinPtr returns a pointer to the smaller of a and b, or nil.
  650. func UMinPtr(a, b *uint) *uint {
  651. if a == nil {
  652. return b
  653. }
  654. if b == nil {
  655. return a
  656. }
  657. if *a < *b {
  658. return a
  659. }
  660. return b
  661. }
  662. // UMaxVal returns the largest argument passed.
  663. func UMaxVal(val uint, vals ...uint) uint {
  664. res := val
  665. for _, v := range vals {
  666. if v > res {
  667. res = v
  668. }
  669. }
  670. return res
  671. }
  672. // UMinVal returns the smallest argument passed.
  673. func UMinVal(val uint, vals ...uint) uint {
  674. res := val
  675. for _, v := range vals {
  676. if v < res {
  677. res = v
  678. }
  679. }
  680. return res
  681. }
  682. // UClamp returns a value restricted between lo and hi.
  683. func UClamp(v, lo, hi uint) uint {
  684. return UMin(UMax(v, lo), hi)
  685. }
  686. // MaxByte returns the larger of a and b.
  687. func MaxByte(a, b byte) byte {
  688. if a > b {
  689. return a
  690. }
  691. return b
  692. }
  693. // MinByte returns the smaller of a and b.
  694. func MinByte(a, b byte) byte {
  695. if a < b {
  696. return a
  697. }
  698. return b
  699. }
  700. // MaxBytePtr returns a pointer to the larger of a and b, or nil.
  701. func MaxBytePtr(a, b *byte) *byte {
  702. if a == nil {
  703. return b
  704. }
  705. if b == nil {
  706. return a
  707. }
  708. if *a > *b {
  709. return a
  710. }
  711. return b
  712. }
  713. // MinBytePtr returns a pointer to the smaller of a and b, or nil.
  714. func MinBytePtr(a, b *byte) *byte {
  715. if a == nil {
  716. return b
  717. }
  718. if b == nil {
  719. return a
  720. }
  721. if *a < *b {
  722. return a
  723. }
  724. return b
  725. }
  726. // MaxByteVal returns the largest argument passed.
  727. func MaxByteVal(val byte, vals ...byte) byte {
  728. res := val
  729. for _, v := range vals {
  730. if v > res {
  731. res = v
  732. }
  733. }
  734. return res
  735. }
  736. // MinByteVal returns the smallest argument passed.
  737. func MinByteVal(val byte, vals ...byte) byte {
  738. res := val
  739. for _, v := range vals {
  740. if v < res {
  741. res = v
  742. }
  743. }
  744. return res
  745. }
  746. // ClampByte returns a value restricted between lo and hi.
  747. func ClampByte(v, lo, hi byte) byte {
  748. return MinByte(MaxByte(v, lo), hi)
  749. }
  750. // MaxInt8 returns the larger of a and b.
  751. func MaxInt8(a, b int8) int8 {
  752. if a > b {
  753. return a
  754. }
  755. return b
  756. }
  757. // MinInt8 returns the smaller of a and b.
  758. func MinInt8(a, b int8) int8 {
  759. if a < b {
  760. return a
  761. }
  762. return b
  763. }
  764. // MaxInt8Ptr returns a pointer to the larger of a and b, or nil.
  765. func MaxInt8Ptr(a, b *int8) *int8 {
  766. if a == nil {
  767. return b
  768. }
  769. if b == nil {
  770. return a
  771. }
  772. if *a > *b {
  773. return a
  774. }
  775. return b
  776. }
  777. // MinInt8Ptr returns a pointer to the smaller of a and b, or nil.
  778. func MinInt8Ptr(a, b *int8) *int8 {
  779. if a == nil {
  780. return b
  781. }
  782. if b == nil {
  783. return a
  784. }
  785. if *a < *b {
  786. return a
  787. }
  788. return b
  789. }
  790. // MaxInt8Val returns the largest argument passed.
  791. func MaxInt8Val(val int8, vals ...int8) int8 {
  792. res := val
  793. for _, v := range vals {
  794. if v > res {
  795. res = v
  796. }
  797. }
  798. return res
  799. }
  800. // MinInt8Val returns the smallest argument passed.
  801. func MinInt8Val(val int8, vals ...int8) int8 {
  802. res := val
  803. for _, v := range vals {
  804. if v < res {
  805. res = v
  806. }
  807. }
  808. return res
  809. }
  810. // ClampInt8 returns a value restricted between lo and hi.
  811. func ClampInt8(v, lo, hi int8) int8 {
  812. return MinInt8(MaxInt8(v, lo), hi)
  813. }
  814. // MaxUint16 returns the larger of a and b.
  815. func MaxUint16(a, b uint16) uint16 {
  816. if a > b {
  817. return a
  818. }
  819. return b
  820. }
  821. // MinUint16 returns the smaller of a and b.
  822. func MinUint16(a, b uint16) uint16 {
  823. if a < b {
  824. return a
  825. }
  826. return b
  827. }
  828. // MaxUint16Ptr returns a pointer to the larger of a and b, or nil.
  829. func MaxUint16Ptr(a, b *uint16) *uint16 {
  830. if a == nil {
  831. return b
  832. }
  833. if b == nil {
  834. return a
  835. }
  836. if *a > *b {
  837. return a
  838. }
  839. return b
  840. }
  841. // MinUint16Ptr returns a pointer to the smaller of a and b, or nil.
  842. func MinUint16Ptr(a, b *uint16) *uint16 {
  843. if a == nil {
  844. return b
  845. }
  846. if b == nil {
  847. return a
  848. }
  849. if *a < *b {
  850. return a
  851. }
  852. return b
  853. }
  854. // MaxUint16Val returns the largest argument passed.
  855. func MaxUint16Val(val uint16, vals ...uint16) uint16 {
  856. res := val
  857. for _, v := range vals {
  858. if v > res {
  859. res = v
  860. }
  861. }
  862. return res
  863. }
  864. // MinUint16Val returns the smallest argument passed.
  865. func MinUint16Val(val uint16, vals ...uint16) uint16 {
  866. res := val
  867. for _, v := range vals {
  868. if v < res {
  869. res = v
  870. }
  871. }
  872. return res
  873. }
  874. // ClampUint16 returns a value restricted between lo and hi.
  875. func ClampUint16(v, lo, hi uint16) uint16 {
  876. return MinUint16(MaxUint16(v, lo), hi)
  877. }
  878. // MaxInt16 returns the larger of a and b.
  879. func MaxInt16(a, b int16) int16 {
  880. if a > b {
  881. return a
  882. }
  883. return b
  884. }
  885. // MinInt16 returns the smaller of a and b.
  886. func MinInt16(a, b int16) int16 {
  887. if a < b {
  888. return a
  889. }
  890. return b
  891. }
  892. // MaxInt16Ptr returns a pointer to the larger of a and b, or nil.
  893. func MaxInt16Ptr(a, b *int16) *int16 {
  894. if a == nil {
  895. return b
  896. }
  897. if b == nil {
  898. return a
  899. }
  900. if *a > *b {
  901. return a
  902. }
  903. return b
  904. }
  905. // MinInt16Ptr returns a pointer to the smaller of a and b, or nil.
  906. func MinInt16Ptr(a, b *int16) *int16 {
  907. if a == nil {
  908. return b
  909. }
  910. if b == nil {
  911. return a
  912. }
  913. if *a < *b {
  914. return a
  915. }
  916. return b
  917. }
  918. // MaxInt16Val returns the largest argument passed.
  919. func MaxInt16Val(val int16, vals ...int16) int16 {
  920. res := val
  921. for _, v := range vals {
  922. if v > res {
  923. res = v
  924. }
  925. }
  926. return res
  927. }
  928. // MinInt16Val returns the smallest argument passed.
  929. func MinInt16Val(val int16, vals ...int16) int16 {
  930. res := val
  931. for _, v := range vals {
  932. if v < res {
  933. res = v
  934. }
  935. }
  936. return res
  937. }
  938. // ClampInt16 returns a value restricted between lo and hi.
  939. func ClampInt16(v, lo, hi int16) int16 {
  940. return MinInt16(MaxInt16(v, lo), hi)
  941. }
  942. // MaxUint32 returns the larger of a and b.
  943. func MaxUint32(a, b uint32) uint32 {
  944. if a > b {
  945. return a
  946. }
  947. return b
  948. }
  949. // MinUint32 returns the smaller of a and b.
  950. func MinUint32(a, b uint32) uint32 {
  951. if a < b {
  952. return a
  953. }
  954. return b
  955. }
  956. // MaxUint32Ptr returns a pointer to the larger of a and b, or nil.
  957. func MaxUint32Ptr(a, b *uint32) *uint32 {
  958. if a == nil {
  959. return b
  960. }
  961. if b == nil {
  962. return a
  963. }
  964. if *a > *b {
  965. return a
  966. }
  967. return b
  968. }
  969. // MinUint32Ptr returns a pointer to the smaller of a and b, or nil.
  970. func MinUint32Ptr(a, b *uint32) *uint32 {
  971. if a == nil {
  972. return b
  973. }
  974. if b == nil {
  975. return a
  976. }
  977. if *a < *b {
  978. return a
  979. }
  980. return b
  981. }
  982. // MaxUint32Val returns the largest argument passed.
  983. func MaxUint32Val(val uint32, vals ...uint32) uint32 {
  984. res := val
  985. for _, v := range vals {
  986. if v > res {
  987. res = v
  988. }
  989. }
  990. return res
  991. }
  992. // MinUint32Val returns the smallest argument passed.
  993. func MinUint32Val(val uint32, vals ...uint32) uint32 {
  994. res := val
  995. for _, v := range vals {
  996. if v < res {
  997. res = v
  998. }
  999. }
  1000. return res
  1001. }
  1002. // ClampUint32 returns a value restricted between lo and hi.
  1003. func ClampUint32(v, lo, hi uint32) uint32 {
  1004. return MinUint32(MaxUint32(v, lo), hi)
  1005. }
  1006. // MaxInt32 returns the larger of a and b.
  1007. func MaxInt32(a, b int32) int32 {
  1008. if a > b {
  1009. return a
  1010. }
  1011. return b
  1012. }
  1013. // MinInt32 returns the smaller of a and b.
  1014. func MinInt32(a, b int32) int32 {
  1015. if a < b {
  1016. return a
  1017. }
  1018. return b
  1019. }
  1020. // MaxInt32Ptr returns a pointer to the larger of a and b, or nil.
  1021. func MaxInt32Ptr(a, b *int32) *int32 {
  1022. if a == nil {
  1023. return b
  1024. }
  1025. if b == nil {
  1026. return a
  1027. }
  1028. if *a > *b {
  1029. return a
  1030. }
  1031. return b
  1032. }
  1033. // MinInt32Ptr returns a pointer to the smaller of a and b, or nil.
  1034. func MinInt32Ptr(a, b *int32) *int32 {
  1035. if a == nil {
  1036. return b
  1037. }
  1038. if b == nil {
  1039. return a
  1040. }
  1041. if *a < *b {
  1042. return a
  1043. }
  1044. return b
  1045. }
  1046. // MaxInt32Val returns the largest argument passed.
  1047. func MaxInt32Val(val int32, vals ...int32) int32 {
  1048. res := val
  1049. for _, v := range vals {
  1050. if v > res {
  1051. res = v
  1052. }
  1053. }
  1054. return res
  1055. }
  1056. // MinInt32Val returns the smallest argument passed.
  1057. func MinInt32Val(val int32, vals ...int32) int32 {
  1058. res := val
  1059. for _, v := range vals {
  1060. if v < res {
  1061. res = v
  1062. }
  1063. }
  1064. return res
  1065. }
  1066. // ClampInt32 returns a value restricted between lo and hi.
  1067. func ClampInt32(v, lo, hi int32) int32 {
  1068. return MinInt32(MaxInt32(v, lo), hi)
  1069. }
  1070. // MaxUint64 returns the larger of a and b.
  1071. func MaxUint64(a, b uint64) uint64 {
  1072. if a > b {
  1073. return a
  1074. }
  1075. return b
  1076. }
  1077. // MinUint64 returns the smaller of a and b.
  1078. func MinUint64(a, b uint64) uint64 {
  1079. if a < b {
  1080. return a
  1081. }
  1082. return b
  1083. }
  1084. // MaxUint64Ptr returns a pointer to the larger of a and b, or nil.
  1085. func MaxUint64Ptr(a, b *uint64) *uint64 {
  1086. if a == nil {
  1087. return b
  1088. }
  1089. if b == nil {
  1090. return a
  1091. }
  1092. if *a > *b {
  1093. return a
  1094. }
  1095. return b
  1096. }
  1097. // MinUint64Ptr returns a pointer to the smaller of a and b, or nil.
  1098. func MinUint64Ptr(a, b *uint64) *uint64 {
  1099. if a == nil {
  1100. return b
  1101. }
  1102. if b == nil {
  1103. return a
  1104. }
  1105. if *a < *b {
  1106. return a
  1107. }
  1108. return b
  1109. }
  1110. // MaxUint64Val returns the largest argument passed.
  1111. func MaxUint64Val(val uint64, vals ...uint64) uint64 {
  1112. res := val
  1113. for _, v := range vals {
  1114. if v > res {
  1115. res = v
  1116. }
  1117. }
  1118. return res
  1119. }
  1120. // MinUint64Val returns the smallest argument passed.
  1121. func MinUint64Val(val uint64, vals ...uint64) uint64 {
  1122. res := val
  1123. for _, v := range vals {
  1124. if v < res {
  1125. res = v
  1126. }
  1127. }
  1128. return res
  1129. }
  1130. // ClampUint64 returns a value restricted between lo and hi.
  1131. func ClampUint64(v, lo, hi uint64) uint64 {
  1132. return MinUint64(MaxUint64(v, lo), hi)
  1133. }
  1134. // MaxInt64 returns the larger of a and b.
  1135. func MaxInt64(a, b int64) int64 {
  1136. if a > b {
  1137. return a
  1138. }
  1139. return b
  1140. }
  1141. // MinInt64 returns the smaller of a and b.
  1142. func MinInt64(a, b int64) int64 {
  1143. if a < b {
  1144. return a
  1145. }
  1146. return b
  1147. }
  1148. // MaxInt64Ptr returns a pointer to the larger of a and b, or nil.
  1149. func MaxInt64Ptr(a, b *int64) *int64 {
  1150. if a == nil {
  1151. return b
  1152. }
  1153. if b == nil {
  1154. return a
  1155. }
  1156. if *a > *b {
  1157. return a
  1158. }
  1159. return b
  1160. }
  1161. // MinInt64Ptr returns a pointer to the smaller of a and b, or nil.
  1162. func MinInt64Ptr(a, b *int64) *int64 {
  1163. if a == nil {
  1164. return b
  1165. }
  1166. if b == nil {
  1167. return a
  1168. }
  1169. if *a < *b {
  1170. return a
  1171. }
  1172. return b
  1173. }
  1174. // MaxInt64Val returns the largest argument passed.
  1175. func MaxInt64Val(val int64, vals ...int64) int64 {
  1176. res := val
  1177. for _, v := range vals {
  1178. if v > res {
  1179. res = v
  1180. }
  1181. }
  1182. return res
  1183. }
  1184. // MinInt64Val returns the smallest argument passed.
  1185. func MinInt64Val(val int64, vals ...int64) int64 {
  1186. res := val
  1187. for _, v := range vals {
  1188. if v < res {
  1189. res = v
  1190. }
  1191. }
  1192. return res
  1193. }
  1194. // ClampInt64 returns a value restricted between lo and hi.
  1195. func ClampInt64(v, lo, hi int64) int64 {
  1196. return MinInt64(MaxInt64(v, lo), hi)
  1197. }
  1198. // ToBase produces n in base b. For example
  1199. //
  1200. // ToBase(2047, 22) -> [1, 5, 4]
  1201. //
  1202. // 1 * 22^0 1
  1203. // 5 * 22^1 110
  1204. // 4 * 22^2 1936
  1205. // ----
  1206. // 2047
  1207. //
  1208. // ToBase panics for bases < 2.
  1209. func ToBase(n *big.Int, b int) []int {
  1210. var nn big.Int
  1211. nn.Set(n)
  1212. if b < 2 {
  1213. panic("invalid base")
  1214. }
  1215. k := 1
  1216. switch nn.Sign() {
  1217. case -1:
  1218. nn.Neg(&nn)
  1219. k = -1
  1220. case 0:
  1221. return []int{0}
  1222. }
  1223. bb := big.NewInt(int64(b))
  1224. var r []int
  1225. rem := big.NewInt(0)
  1226. for nn.Sign() != 0 {
  1227. nn.QuoRem(&nn, bb, rem)
  1228. r = append(r, k*int(rem.Int64()))
  1229. }
  1230. return r
  1231. }
  1232. // CheckAddInt64 returns the a+b and an indicator that the result is greater
  1233. // than math.MaxInt64.
  1234. func CheckAddInt64(a, b int64) (sum int64, gt bool) {
  1235. return a + b, a > 0 && b > math.MaxInt64-a || a < 0 && b < math.MinInt64-a
  1236. }
  1237. // CheckSubInt64 returns a-b and an indicator that the result is less than than
  1238. // math.MinInt64.
  1239. func CheckSubInt64(a, b int64) (sum int64, lt bool) {
  1240. return a - b, a > 0 && a-math.MaxInt64 > b || a < 0 && a-math.MinInt64 < b
  1241. }
  1242. // AddOverflowInt8 returns a + b and an indication whether the addition
  1243. // overflowed the int8 range.
  1244. func AddOverflowInt8(a, b int8) (r int8, ovf bool) {
  1245. r = a + b
  1246. if a > 0 && b > 0 {
  1247. return r, uint8(r) > math.MaxInt8
  1248. }
  1249. if a < 0 && b < 0 {
  1250. return r, uint8(r) <= math.MaxInt8
  1251. }
  1252. return r, false
  1253. }
  1254. // AddOverflowInt16 returns a + b and an indication whether the addition
  1255. // overflowed the int16 range.
  1256. func AddOverflowInt16(a, b int16) (r int16, ovf bool) {
  1257. r = a + b
  1258. if a > 0 && b > 0 {
  1259. return r, uint16(r) > math.MaxInt16
  1260. }
  1261. if a < 0 && b < 0 {
  1262. return r, uint16(r) <= math.MaxInt16
  1263. }
  1264. return r, false
  1265. }
  1266. // AddOverflowInt32 returns a + b and an indication whether the addition
  1267. // overflowed the int32 range.
  1268. func AddOverflowInt32(a, b int32) (r int32, ovf bool) {
  1269. r = a + b
  1270. if a > 0 && b > 0 {
  1271. return r, uint32(r) > math.MaxInt32
  1272. }
  1273. if a < 0 && b < 0 {
  1274. return r, uint32(r) <= math.MaxInt32
  1275. }
  1276. return r, false
  1277. }
  1278. // AddOverflowInt64 returns a + b and an indication whether the addition
  1279. // overflowed the int64 range.
  1280. func AddOverflowInt64(a, b int64) (r int64, ovf bool) {
  1281. r = a + b
  1282. if a > 0 && b > 0 {
  1283. return r, uint64(r) > math.MaxInt64
  1284. }
  1285. if a < 0 && b < 0 {
  1286. return r, uint64(r) <= math.MaxInt64
  1287. }
  1288. return r, false
  1289. }
  1290. // SubOverflowInt8 returns a - b and an indication whether the subtraction
  1291. // overflowed the int8 range.
  1292. func SubOverflowInt8(a, b int8) (r int8, ovf bool) {
  1293. r = a - b
  1294. if a >= 0 && b < 0 {
  1295. return r, uint8(r) >= math.MaxInt8+1
  1296. }
  1297. if a < 0 && b > 0 {
  1298. return r, uint8(r) <= math.MaxInt8
  1299. }
  1300. return r, false
  1301. }
  1302. // SubOverflowInt16 returns a - b and an indication whether the subtraction
  1303. // overflowed the int16 range.
  1304. func SubOverflowInt16(a, b int16) (r int16, ovf bool) {
  1305. r = a - b
  1306. if a >= 0 && b < 0 {
  1307. return r, uint16(r) >= math.MaxInt16+1
  1308. }
  1309. if a < 0 && b > 0 {
  1310. return r, uint16(r) <= math.MaxInt16
  1311. }
  1312. return r, false
  1313. }
  1314. // SubOverflowInt32 returns a - b and an indication whether the subtraction
  1315. // overflowed the int32 range.
  1316. func SubOverflowInt32(a, b int32) (r int32, ovf bool) {
  1317. r = a - b
  1318. if a >= 0 && b < 0 {
  1319. return r, uint32(r) >= math.MaxInt32+1
  1320. }
  1321. if a < 0 && b > 0 {
  1322. return r, uint32(r) <= math.MaxInt32
  1323. }
  1324. return r, false
  1325. }
  1326. // SubOverflowInt64 returns a - b and an indication whether the subtraction
  1327. // overflowed the int64 range.
  1328. func SubOverflowInt64(a, b int64) (r int64, ovf bool) {
  1329. r = a - b
  1330. if a >= 0 && b < 0 {
  1331. return r, uint64(r) >= math.MaxInt64+1
  1332. }
  1333. if a < 0 && b > 0 {
  1334. return r, uint64(r) <= math.MaxInt64
  1335. }
  1336. return r, false
  1337. }
  1338. // MulOverflowInt8 returns a * b and an indication whether the product
  1339. // overflowed the int8 range.
  1340. func MulOverflowInt8(a, b int8) (r int8, ovf bool) {
  1341. if a == 0 || b == 0 {
  1342. return 0, false
  1343. }
  1344. z := int16(a) * int16(b)
  1345. return int8(z), z < math.MinInt8 || z > math.MaxInt8
  1346. }
  1347. // MulOverflowInt16 returns a * b and an indication whether the product
  1348. // overflowed the int16 range.
  1349. func MulOverflowInt16(a, b int16) (r int16, ovf bool) {
  1350. if a == 0 || b == 0 {
  1351. return 0, false
  1352. }
  1353. z := int32(a) * int32(b)
  1354. return int16(z), z < math.MinInt16 || z > math.MaxInt16
  1355. }
  1356. // MulOverflowInt32 returns a * b and an indication whether the product
  1357. // overflowed the int32 range.
  1358. func MulOverflowInt32(a, b int32) (r int32, ovf bool) {
  1359. if a == 0 || b == 0 {
  1360. return 0, false
  1361. }
  1362. z := int64(a) * int64(b)
  1363. return int32(z), z < math.MinInt32 || z > math.MaxInt32
  1364. }
  1365. // MulOverflowInt64 returns a * b and an indication whether the product
  1366. // overflowed the int64 range.
  1367. func MulOverflowInt64(a, b int64) (r int64, ovf bool) {
  1368. // https://groups.google.com/g/golang-nuts/c/h5oSN5t3Au4/m/KaNQREhZh0QJ
  1369. const mostPositive = 1<<63 - 1
  1370. const mostNegative = -(mostPositive + 1)
  1371. r = a * b
  1372. if a == 0 || b == 0 || a == 1 || b == 1 {
  1373. return r, false
  1374. }
  1375. if a == mostNegative || b == mostNegative {
  1376. return r, true
  1377. }
  1378. return r, r/b != a
  1379. }