github.com/cznic/mathutil@v0.0.0-20181122101859-297441e03548/mathutil.go (about)

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