Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149323
149249
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
326027
325953
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
280141
280067
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
139529
139455
Original file line number Diff line number Diff line change
@@ -1 +1 @@
297501
297427
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19986
19961
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181644
181594
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135933
135883
2 changes: 1 addition & 1 deletion .forge-snapshots/removeLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115761
115711
Original file line number Diff line number Diff line change
@@ -1 +1 @@
102765
102728
2 changes: 1 addition & 1 deletion .forge-snapshots/simple addLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165257
165220
Original file line number Diff line number Diff line change
@@ -1 +1 @@
96504
96479
2 changes: 1 addition & 1 deletion .forge-snapshots/simple removeLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88544
88519
56 changes: 48 additions & 8 deletions src/libraries/SqrtPriceMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,30 @@ library SqrtPriceMath {
pure
returns (int256 amount0)
{
unchecked {
return liquidity < 0
? getAmount0Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(-liquidity), false).toInt256()
: -getAmount0Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(liquidity), true).toInt256();
/**
* Equivalent to:
* amount0 = liquidity < 0
* ? getAmount0Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(-liquidity), false).toInt256()
* : -getAmount0Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(liquidity), true).toInt256();
*/
bool roundUp;
uint128 liquidityAbs;
assembly {
// mask = 0 if liquidity >= 0 else -1
let mask := sar(255, liquidity)
// roundUp = 1 if liquidity >= 0 else 0
roundUp := iszero(mask)
liquidityAbs := xor(mask, add(mask, liquidity))
}
// amount0Abs = liquidity / sqrt(lower) - liquidity / sqrt(upper) < type(uint224).max
// always fits in 224 bits, no need for toInt256()
uint256 amount0Abs = getAmount0Delta(sqrtPriceAX96, sqrtPriceBX96, liquidityAbs, roundUp);
assembly {
// mask = -1 if liquidity >= 0 else 0
let mask := sub(0, roundUp)
// If liquidity < 0, amount0 = |amount0| = 0 ^ |amount0|
// If liquidity >= 0, amount0 = -|amount0| = ~|amount0| + 1 = (-1) ^ |amount0| - (-1)
amount0 := sub(xor(amount0Abs, mask), mask)
}
}

Expand All @@ -276,10 +296,30 @@ library SqrtPriceMath {
pure
returns (int256 amount1)
{
unchecked {
return liquidity < 0
? getAmount1Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(-liquidity), false).toInt256()
: -getAmount1Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(liquidity), true).toInt256();
/**
* Equivalent to:
* amount1 = liquidity < 0
* ? getAmount1Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(-liquidity), false).toInt256()
* : -getAmount1Delta(sqrtPriceAX96, sqrtPriceBX96, uint128(liquidity), true).toInt256();
*/
bool roundUp;
uint128 liquidityAbs;
assembly {
// mask = 0 if liquidity >= 0 else -1
let mask := sar(255, liquidity)
// roundUp = 1 if liquidity >= 0 else 0
roundUp := iszero(mask)
liquidityAbs := xor(mask, add(mask, liquidity))
}
// amount1Abs = liquidity * (sqrt(upper) - sqrt(lower)) < type(uint192).max
// always fits in 192 bits, no need for toInt256()
uint256 amount1Abs = getAmount1Delta(sqrtPriceAX96, sqrtPriceBX96, liquidityAbs, roundUp);
assembly {
// mask = -1 if liquidity >= 0 else 0
let mask := sub(0, roundUp)
// If liquidity < 0, amount1 = |amount1| = 0 ^ |amount1|
// If liquidity >= 0, amount1 = -|amount1| = ~|amount1| + 1 = (-1) ^ |amount1| - (-1)
amount1 := sub(xor(amount1Abs, mask), mask)
}
}
}