mathutil.go 30 KB

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