int256 source pure
Package int256 implements 256-bit signed integer arithmetic for GnoSwap.
View source
int256
256-bit signed integer arithmetic for GnoSwap.
Overview
Fixed-size 256-bit signed integer library optimized for AMM calculations with overflow detection.
Features
- Fixed 256-bit size (predictable gas costs)
- Two's complement representation
- Overflow detection on all operations
- AMM-optimized functions
- Range: -(2^255) to 2^255-1
Usage
1import i256 "gno.land/p/gnoswap/int256"
2
3// Create values
4a := i256.NewInt(100)
5b := i256.MustFromDecimal("-1000")
6
7// Arithmetic with overflow detection
8result, overflow := new(i256.Int).AddOverflow(a, b)
9if overflow {
10 // Handle overflow
11}
Implementation
Built on uint256 for underlying arithmetic.
Package int256 implements 256-bit signed integer arithmetic for GnoSwap.
This package provides an Int type that represents a 256-bit signed integer using two's complement representation. It supports the full range from -(2^255) to 2^255-1, with arithmetic operations that detect overflow.
The implementation follows Ethereum's int256 semantics, ensuring compatibility for cross-chain DeFi protocols. Operations are optimized for common AMM calculations including tick math and price computations.
Critical operations like Add, Sub, and Mul return overflow flags, enabling safe handling of edge cases in financial calculations.
8
func FromUint256
FromUint256 converts a uint256 to int256. Panics if the uint256 value is greater than MaxInt256 (2^255 - 1).
func MaxInt256
func MinInt256
func MustFromDecimal
func NewInt
func One
func Zero
1
type Int
arrayMethods on Int
func Abs
method on Intfunc Add
method on Intfunc AddOverflow
method on Intfunc And
method on Intfunc BitLen
method on Intfunc Clear
method on Intfunc Clone
method on Intfunc Cmp
method on Intfunc Div
method on Intfunc Eq
method on Intfunc Gt
method on Intfunc Gte
method on Intfunc Int64
method on Intfunc IsInt64
method on Intfunc IsMinI256
method on Intfunc IsNeg
method on Intfunc IsOne
method on Intfunc IsPositive
method on Intfunc IsUint64
method on Intfunc IsZero
method on Intfunc Lsh
method on Intfunc Lt
method on Intfunc Lte
method on Intfunc Mul
method on Intfunc MulOverflow
method on Intfunc Neg
method on Intfunc NegOverflow
method on Intfunc Neq
method on Intfunc Not
method on Intfunc Or
method on Intfunc Rem
method on Intfunc Rsh
method on Intfunc Set
method on Intfunc SetAllBitsOne
method on Intfunc SetBytes32
method on Intfunc SetInt64
method on Intfunc SetOne
method on Intfunc SetString
method on Intfunc SetUint64
method on Intfunc Sign
method on Intfunc Sub
method on Intfunc SubOverflow
method on Intfunc ToString
method on Intfunc Uint64
method on Intfunc Xor
method on Int6
- encoding/binary stdlib
- errors stdlib
- gno.land/p/gnoswap/uint256 package
- math stdlib
- math/bits stdlib
- strconv stdlib