1// Package gnsmath provides core mathematical operations for GnoSwap's concentrated liquidity AMM.2//3// ## Overview4//5// This package provides the fundamental calculations for concentrated liquidity,6// including tick conversion, liquidity math calculations, sqrt price math, swap7// calculations, and bit manipulation utilities. All operations use Q64.96,8// Q128.128, and Q160 fixed-point arithmetic where appropriate.9//10// The implementation follows Uniswap V3's mathematical model.11//12// ## Features13//14// - Bit Math: MSB/LSB calculations for tick bitmap operations15// - Tick Math: tick and Q64.96 sqrt-price conversions16// - Liquidity Math: liquidity and token amount math for price ranges17// - Sqrt Price Math: token amount conversions using Q64.96 format18// - Swap Math: single-step swap calculations with fee handling19// - Overflow Protection: explicit int256/uint256 overflow checks20// - Rounding Control: configurable rounding for AMM safety21//22// ## Core Concepts23//24// ### Q64.96 Fixed-Point Format25//26// Square root prices use Q64.96 representation:27// - sqrtPriceX96 = sqrt(token1/token0) * 2^9628// - Enables precise integer arithmetic without floating-point29//30// ### Rounding Directions31//32// - Round UP: amounts owed TO pool (deposits, exact input)33// - Round DOWN: amounts owed FROM pool (withdrawals, exact output)34//35// ## API36//37// ### Bit Math38//39// - BitMathMostSignificantBit(x *u256.Uint) uint840// - BitMathLeastSignificantBit(x *u256.Uint) uint841//42// ### Tick Math43//44// - TickMathGetSqrtRatioAtTick(tick int32) *u256.Uint45// - TickMathGetTickAtSqrtRatio(sqrtPriceX96 *u256.Uint) int32; accepts46// `[MinSqrtRatio, MaxSqrtRatio)` (the upper bound is exclusive)47//48// ### Liquidity Math49//50// - GetLiquidityForAmounts(sqrtRatioX96, sqrtRatioAX96, sqrtRatioBX96, amount0, amount1 *u256.Uint) *u256.Uint51// - GetAmountsForLiquidity(sqrtRatioX96, sqrtRatioAX96, sqrtRatioBX96, liquidity *u256.Uint) (*u256.Uint, *u256.Uint)52// - LiquidityMathAddDelta(x *u256.Uint, y *i256.Int) *u256.Uint53//54// ### Sqrt Price Math55//56// - GetAmount0Delta(sqrtRatioAX96, sqrtRatioBX96 *u256.Uint, liquidity *i256.Int) *i256.Int57// - GetAmount1Delta(sqrtRatioAX96, sqrtRatioBX96 *u256.Uint, liquidity *i256.Int) *i256.Int58//59// ### Swap Math60//61// - SwapMathComputeSwapStep(sqrtRatioCurrentX96, sqrtRatioTargetX96, liquidity *u256.Uint, amountRemaining *i256.Int, feePips uint64) (*u256.Uint, *u256.Uint, *u256.Uint, *u256.Uint)62package gnsmath63Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.