github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/common/number/int.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package number
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/vntchain/go-vnt/common"
    23  )
    24  
    25  var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
    26  var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
    27  var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
    28  
    29  func limitUnsigned256(x *Number) *Number {
    30  	x.num.And(x.num, tt256m1)
    31  	return x
    32  }
    33  
    34  func limitSigned256(x *Number) *Number {
    35  	if x.num.Cmp(tt255) < 0 {
    36  		return x
    37  	}
    38  	x.num.Sub(x.num, tt256)
    39  	return x
    40  }
    41  
    42  // Initialiser is a Number function
    43  type Initialiser func(n int64) *Number
    44  
    45  // A Number represents a generic integer with a bounding function limiter. Limit is called after each operations
    46  // to give "fake" bounded integers. New types of Number can be created through NewInitialiser returning a lambda
    47  // with the new Initialiser.
    48  type Number struct {
    49  	num   *big.Int
    50  	limit func(n *Number) *Number
    51  }
    52  
    53  // NewInitialiser returns a new initialiser for a new *Number without having to expose certain fields
    54  func NewInitialiser(limiter func(*Number) *Number) Initialiser {
    55  	return func(n int64) *Number {
    56  		return &Number{big.NewInt(n), limiter}
    57  	}
    58  }
    59  
    60  // Uint256 returns a Number with a UNSIGNED limiter up to 256 bits
    61  func Uint256(n int64) *Number {
    62  	return &Number{big.NewInt(n), limitUnsigned256}
    63  }
    64  
    65  // Int256 returns Number with a SIGNED limiter up to 256 bits
    66  func Int256(n int64) *Number {
    67  	return &Number{big.NewInt(n), limitSigned256}
    68  }
    69  
    70  // Big returns a Number with a SIGNED unlimited size
    71  func Big(n int64) *Number {
    72  	return &Number{big.NewInt(n), func(x *Number) *Number { return x }}
    73  }
    74  
    75  // Add sets i to sum of x+y
    76  func (i *Number) Add(x, y *Number) *Number {
    77  	i.num.Add(x.num, y.num)
    78  	return i.limit(i)
    79  }
    80  
    81  // Sub sets i to difference of x-y
    82  func (i *Number) Sub(x, y *Number) *Number {
    83  	i.num.Sub(x.num, y.num)
    84  	return i.limit(i)
    85  }
    86  
    87  // Mul sets i to product of x*y
    88  func (i *Number) Mul(x, y *Number) *Number {
    89  	i.num.Mul(x.num, y.num)
    90  	return i.limit(i)
    91  }
    92  
    93  // Div sets i to the quotient prodject of x/y
    94  func (i *Number) Div(x, y *Number) *Number {
    95  	i.num.Div(x.num, y.num)
    96  	return i.limit(i)
    97  }
    98  
    99  // Mod sets i to x % y
   100  func (i *Number) Mod(x, y *Number) *Number {
   101  	i.num.Mod(x.num, y.num)
   102  	return i.limit(i)
   103  }
   104  
   105  // Lsh sets i to x << s
   106  func (i *Number) Lsh(x *Number, s uint) *Number {
   107  	i.num.Lsh(x.num, s)
   108  	return i.limit(i)
   109  }
   110  
   111  // Pow sets i to x^y
   112  func (i *Number) Pow(x, y *Number) *Number {
   113  	i.num.Exp(x.num, y.num, big.NewInt(0))
   114  	return i.limit(i)
   115  }
   116  
   117  // Setters
   118  
   119  // Set sets x to i
   120  func (i *Number) Set(x *Number) *Number {
   121  	i.num.Set(x.num)
   122  	return i.limit(i)
   123  }
   124  
   125  // SetBytes sets x bytes to i
   126  func (i *Number) SetBytes(x []byte) *Number {
   127  	i.num.SetBytes(x)
   128  	return i.limit(i)
   129  }
   130  
   131  // Cmp compares x and y and returns:
   132  //
   133  //     -1 if x <  y
   134  //     0 if x == y
   135  //     +1 if x >  y
   136  func (i *Number) Cmp(x *Number) int {
   137  	return i.num.Cmp(x.num)
   138  }
   139  
   140  // Getters
   141  
   142  // String returns the string representation of i
   143  func (i *Number) String() string {
   144  	return i.num.String()
   145  }
   146  
   147  // Bytes returns the byte representation of i
   148  func (i *Number) Bytes() []byte {
   149  	return i.num.Bytes()
   150  }
   151  
   152  // Uint64 returns the Uint64 representation of x. If x cannot be represented in an int64, the result is undefined.
   153  func (i *Number) Uint64() uint64 {
   154  	return i.num.Uint64()
   155  }
   156  
   157  // Int64 returns the int64 representation of x. If x cannot be represented in an int64, the result is undefined.
   158  func (i *Number) Int64() int64 {
   159  	return i.num.Int64()
   160  }
   161  
   162  // Int256 returns the signed version of i
   163  func (i *Number) Int256() *Number {
   164  	return Int(0).Set(i)
   165  }
   166  
   167  // Uint256 returns the unsigned version of i
   168  func (i *Number) Uint256() *Number {
   169  	return Uint(0).Set(i)
   170  }
   171  
   172  // FirstBitSet returns the index of the first bit that's set to 1
   173  func (i *Number) FirstBitSet() int {
   174  	for j := 0; j < i.num.BitLen(); j++ {
   175  		if i.num.Bit(j) > 0 {
   176  			return j
   177  		}
   178  	}
   179  
   180  	return i.num.BitLen()
   181  }
   182  
   183  // Variables
   184  
   185  var (
   186  	Zero       = Uint(0)
   187  	One        = Uint(1)
   188  	Two        = Uint(2)
   189  	MaxUint256 = Uint(0).SetBytes(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
   190  
   191  	MinOne = Int(-1)
   192  
   193  	// "typedefs"
   194  	Uint = Uint256
   195  	Int  = Int256
   196  )