Signed integer division using two’s complement representation
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode:0x05Introduced: Frontier (EVM genesis)SDIV performs signed integer division on two 256-bit values interpreted as two’s complement signed integers. The result is truncated toward zero (not toward negative infinity like some languages).Like DIV, division by zero returns 0. Additionally, SDIV has special handling for the edge case of dividing the minimum signed integer by -1.
Specification
Stack Input:
a (top - signed dividend)b (signed divisor)
Stack Output:
a / b (if b ≠ 0 and not MIN_INT/-1)MIN_INT (if a = MIN_INT and b = -1)0 (if b = 0)
// Calculate price change (can be negative)function priceChange(int256 oldPrice, int256 newPrice) pure returns (int256) { return newPrice - oldPrice; // Can be negative}// Average of signed valuesfunction signedAverage(int256 a, int256 b) pure returns (int256) { // Must handle negative results correctly assembly { let sum := add(a, b) let result := sdiv(sum, 2) mstore(0, result) return(0, 32) }}
Directional Calculations
// Calculate slope (can be negative)function slope(int256 y2, int256 y1, int256 x2, int256 x1) pure returns (int256) { require(x2 != x1, "vertical line"); int256 dy = y2 - y1; int256 dx = x2 - x1; assembly { let result := sdiv(dy, dx) mstore(0, result) return(0, 32) }}
Fixed-Point Signed Math
// Signed fixed-point divisionint256 constant FIXED_POINT = 1e18;function signedWdiv(int256 x, int256 y) pure returns (int256) { require(y != 0, "division by zero"); assembly { let result := sdiv(mul(x, FIXED_POINT), y) mstore(0, result) return(0, 32) }}
Implementation
TypeScript
/** * SDIV opcode (0x05) - Signed integer division */export function sdiv(frame: FrameType): EvmError | null { // Consume gas (GasFastStep = 5) frame.gasRemaining -= 5n; if (frame.gasRemaining < 0n) { frame.gasRemaining = 0n; return { type: "OutOfGas" }; } // Pop operands if (frame.stack.length < 2) return { type: "StackUnderflow" }; const a = frame.stack.pop(); const b = frame.stack.pop(); let result: bigint; if (b === 0n) { result = 0n; } else { const MIN_INT = 1n << 255n; const MAX_UINT = (1n << 256n) - 1n; // Special case: MIN_INT / -1 would overflow if (a === MIN_INT && b === MAX_UINT) { result = MIN_INT; } else { // Convert to signed, divide, convert back const aSigned = a < MIN_INT ? a : a - (1n << 256n); const bSigned = b < MIN_INT ? b : b - (1n << 256n); const quotient = aSigned / bSigned; // BigInt division truncates toward zero result = quotient < 0n ? (1n << 256n) + quotient : quotient; } } // Push result if (frame.stack.length >= 1024) return { type: "StackOverflow" }; frame.stack.push(result); // Increment PC frame.pc += 1; return null;}
// Unsigned vs signed division give different resultsuint256 a = type(uint256).max; // Max uint = -1 signeduint256 b = 2;// Unsigned: MAX / 2 = 2^255 - 1uint256 unsignedResult;assembly { unsignedResult := div(a, b) }// Signed: -1 / 2 = 0 (truncate toward zero)uint256 signedResult;assembly { signedResult := sdiv(a, b) }// Results are different!
MIN_INT Overflow
// MIN_INT / -1 special caseint256 MIN = type(int256).min; // -2^255int256 result = MIN / -1; // In Solidity, this reverts!// But in assembly (raw SDIV):assembly { // Returns MIN_INT, does not revert result := sdiv(MIN, sub(0, 1))}
// Solidity 0.8.0+ automatically checksfunction safeSdiv(int256 a, int256 b) pure returns (int256) { return a / b; // Reverts on MIN_INT / -1 or b = 0}// Explicit checks for assembly usagefunction assemblySdiv(int256 a, int256 b) pure returns (int256) { require(b != 0, "division by zero"); require(!(a == type(int256).min && b == -1), "overflow"); int256 result; assembly { result := sdiv(a, b) } return result;}
Benchmarks
SDIV performance identical to DIV:Execution time:
ADD: 1.0x
MUL: 1.2x
SDIV: 2.5x (same as DIV)
Gas cost:
5 gas per signed division
No overhead for sign handling
~200,000 signed divisions per million gas
References
Yellow Paper - Section 9.1 (Arithmetic Operations)