EIP-616 SIMD Operations for the EVM

No call decision · EVM & execution · EIP status Stagnant · created 2017-04-25

A proposal to add SIMD (single instruction, multiple data) operations to the EVM, splitting a 256-bit value on the EVM stack into multiple smaller lanes and computing on them in parallel at once.

Why it is needed

Today, EVM operations treat an entire 256-bit stack item as a single value. But real CPUs come equipped with SIMD hardware that can split a register into multiple lanes and compute on them in parallel at once, and this structure fits well with the EVM's 256-bit-wide stack. According to the literature, SHA-512 could speed up by as much as 7x, elliptic curve scalar multiplication by 4x, BLAKE2b by 3-4x, and SHA-256 by 1.7-1.9x, which would be a significant benefit for contracts heavy in cryptographic operations. In addition, the EVM's current gas scheme does not properly reflect real computation cost. Measured against a 256-bit baseline algorithm, MUL takes 5-21x more cycles than ADD/SUB, and DIV takes 15-23x more, yet both are charged the same gas cost of 5. This proposal is motivated by splitting operations into smaller integer or vector units to achieve more accurate gas pricing and improved performance at the same time.

What changes

Today

After EIP-616

Who is affected

In depth

2-byte encoding

SIMD operations are encoded in 2 bytes: the opcode byte plus one type byte. The type byte consists of 1 bit for scalar kind (0 = unsigned integer, 1 = IEEE floating point), 1 reserved bit (0), 2 bits for lane width (log2 of byte count, MSB first), 1 reserved bit (0), and 3 bits for lane count (log2 of lane count, MSB first). This encoding can represent 8-64 bit integer lanes numbering from 2 to 32, while floating point supports only 32-bit and 64-bit. A type value of 0x7F denotes an ordinary existing 256-bit EVM integer. When lane count is 1, the operation becomes a single-scalar operation, so this spec also provides native-sized scalar operations. Floating point operations are not included in this initial proposal, but code space is reserved for future extension.

Default semantics of extended operations

All extended arithmetic, comparison, and logic operations except XSHUFFLE, XCAST, and XINDEX (XADD, XMUL, XSUB, XDIV, XSDIV, XMOD, XSMOD, XLT, XGT, XSLT, XSGT, XEQ, XISZERO, XAND, XOR, XXOR, XNOT, XSHL, XSHR, XSAR, XROL, XROR) have the same meaning as the corresponding operations in the existing 01-1F codes, except that the modulus differs by scalar type and the operation is applied element-wise, pairing up elements of the two source vectors. The two source vectors must have the same element type and the same element count. This can be checked either at contract creation time or at execution time.

XSHUFFLE

XSHUFFLE takes two vectors from the stack. One is the target vector to be reordered, and the other is the permutation mask. The mask must be an integer type and have the same element count as the target vector. Each value in the mask specifies which position's element to take from the target vector.

XCAST and memory access

XCAST converts stack items into a new vector in the type specified by the opcode's second byte. Elements are converted following C-language conventions, with missing elements padded with zero and excess elements discarded. If a stack item is not a vector, it is made into one by copying its bits into the corresponding bits of each element starting from the least significant bit; conversely, when converting a vector back into a 256-bit integer the same conversion is reversed, and floating-point NaN elements are normalized to a value with all bits set to one. Since MLOAD and MSTORE are only valid for 256-bit EVM integers, SIMD vectors must be converted with XCAST after loading and before storing.

XINDEX and floating-point handling

XINDEX has the same meaning as BYTE, but indexes individual elements of a vector instead of bytes. Floating-point values follow IEEE 754 rules, and since that specification does not define shift and rotate operations, shift and rotate on floating-point values are defined to have no effect. Also, except for XSHUFFLE and XCAST, extended operations are only valid between vectors of the same SIMD type.

Subroutine extension (BEGINSUBX)

If EIP-187 is adopted, a type-safe syntax is needed to declare subroutines that take vector arguments, so BEGINSUBX n_args, arg_types..., n_results, result_types... is proposed. It marks the single entry point of a subroutine: on entry it pops n_args items from the stack, and on return it pushes n_results items onto the stack. n_args and n_results are each given as a 1-byte immediate value, and arg_types and result_types use the same encoding as the type byte of SIMD opcodes and must match the values on the stack. A subroutine's bytecode ends at the next BEGINSUB, BEGINSUBX, or BEGINDATA instruction, or at the end of the bytecode.

Basis for gas repricing

Calculating cycles for 256-bit operations (N=8 digits) using Knuth's standard algorithms gives 86 cycles for addition, 99 for subtraction, 1883 for multiplication, and 2023 for division. If JUMPDEST's gas cost of 1 is taken as interpreter overhead, a gas cost of 5 for ADD/SUB is reasonable, but MUL takes 5 to 21 times more cycles than ADD/SUB, and DIV takes 15 to 23 times more, meaning the current gas pricing is too low. Conversely, on Intel/ARM SIMD hardware, addition, subtraction, and multiplication take fewer cycles than the interpreter overhead regardless of register width, so they are priced at gas 1, while division takes slightly more and is priced at gas 2.

Proposal status

This EIP is currently Stagnant, having seen no further progress since it was written in 2017. Within the specification itself, the BEGINSUBX subroutine extension presupposes the adoption of EIP-187, and that prerequisite itself has not yet been met.

Original EIP · ← EIPs