github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/common/number/int.go (about)

     1  package number
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/neatio-net/neatio/utilities/common"
     7  )
     8  
     9  var tt256 = new(big.Int).Lsh(big.NewInt(1), 256)
    10  var tt256m1 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
    11  var tt255 = new(big.Int).Lsh(big.NewInt(1), 255)
    12  
    13  func limitUnsigned256(x *Number) *Number {
    14  	x.num.And(x.num, tt256m1)
    15  	return x
    16  }
    17  
    18  func limitSigned256(x *Number) *Number {
    19  	if x.num.Cmp(tt255) < 0 {
    20  		return x
    21  	} else {
    22  		x.num.Sub(x.num, tt256)
    23  		return x
    24  	}
    25  }
    26  
    27  type Initialiser func(n int64) *Number
    28  
    29  type Number struct {
    30  	num   *big.Int
    31  	limit func(n *Number) *Number
    32  }
    33  
    34  func NewInitialiser(limiter func(*Number) *Number) Initialiser {
    35  	return func(n int64) *Number {
    36  		return &Number{big.NewInt(n), limiter}
    37  	}
    38  }
    39  
    40  func Uint256(n int64) *Number {
    41  	return &Number{big.NewInt(n), limitUnsigned256}
    42  }
    43  
    44  func Int256(n int64) *Number {
    45  	return &Number{big.NewInt(n), limitSigned256}
    46  }
    47  
    48  func Big(n int64) *Number {
    49  	return &Number{big.NewInt(n), func(x *Number) *Number { return x }}
    50  }
    51  
    52  func (i *Number) Add(x, y *Number) *Number {
    53  	i.num.Add(x.num, y.num)
    54  	return i.limit(i)
    55  }
    56  
    57  func (i *Number) Sub(x, y *Number) *Number {
    58  	i.num.Sub(x.num, y.num)
    59  	return i.limit(i)
    60  }
    61  
    62  func (i *Number) Mul(x, y *Number) *Number {
    63  	i.num.Mul(x.num, y.num)
    64  	return i.limit(i)
    65  }
    66  
    67  func (i *Number) Div(x, y *Number) *Number {
    68  	i.num.Div(x.num, y.num)
    69  	return i.limit(i)
    70  }
    71  
    72  func (i *Number) Mod(x, y *Number) *Number {
    73  	i.num.Mod(x.num, y.num)
    74  	return i.limit(i)
    75  }
    76  
    77  func (i *Number) Lsh(x *Number, s uint) *Number {
    78  	i.num.Lsh(x.num, s)
    79  	return i.limit(i)
    80  }
    81  
    82  func (i *Number) Pow(x, y *Number) *Number {
    83  	i.num.Exp(x.num, y.num, big.NewInt(0))
    84  	return i.limit(i)
    85  }
    86  
    87  func (i *Number) Set(x *Number) *Number {
    88  	i.num.Set(x.num)
    89  	return i.limit(i)
    90  }
    91  
    92  func (i *Number) SetBytes(x []byte) *Number {
    93  	i.num.SetBytes(x)
    94  	return i.limit(i)
    95  }
    96  
    97  func (i *Number) Cmp(x *Number) int {
    98  	return i.num.Cmp(x.num)
    99  }
   100  
   101  func (i *Number) String() string {
   102  	return i.num.String()
   103  }
   104  
   105  func (i *Number) Bytes() []byte {
   106  	return i.num.Bytes()
   107  }
   108  
   109  func (i *Number) Uint64() uint64 {
   110  	return i.num.Uint64()
   111  }
   112  
   113  func (i *Number) Int64() int64 {
   114  	return i.num.Int64()
   115  }
   116  
   117  func (i *Number) Int256() *Number {
   118  	return Int(0).Set(i)
   119  }
   120  
   121  func (i *Number) Uint256() *Number {
   122  	return Uint(0).Set(i)
   123  }
   124  
   125  func (i *Number) FirstBitSet() int {
   126  	for j := 0; j < i.num.BitLen(); j++ {
   127  		if i.num.Bit(j) > 0 {
   128  			return j
   129  		}
   130  	}
   131  
   132  	return i.num.BitLen()
   133  }
   134  
   135  var (
   136  	Zero       = Uint(0)
   137  	One        = Uint(1)
   138  	Two        = Uint(2)
   139  	MaxUint256 = Uint(0).SetBytes(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"))
   140  
   141  	MinOne = Int(-1)
   142  
   143  	Uint = Uint256
   144  	Int  = Int256
   145  )