Full name: 32-bit arithmetic logic unit instructions (ALU32) versus 64-bit arithmetic logic unit instructions (ALU64).
Definition
ALU32 and ALU64 are the two classes of arithmetic and logic instructions in eBPF §2.1 (extended Berkeley Packet Filter), an instruction set with 64-bit registers. ALU stands for arithmetic logic unit, the part of a processor that computes. An ALU64 instruction computes with all 64 bits of its operands, the values it works on, and writes a 64-bit result. An ALU32 instruction, except a 64-bit byte swap, computes with the low 32 bits, writes the result into the low half of the destination register and sets the high half to zero.
The idea
Register R0 holds 4,294,967,295, in hexadecimal 0x00000000ffffffff, and the next instruction adds 1. The sum, 4,294,967,296, needs 33 bits. An ALU64 addition keeps it; an ALU32 addition keeps only the low 32 bits, all zeros, so R0 becomes 0 and a following test “is R0 zero?” takes the other branch. The width of a computation belongs to the instruction, not to the register.
Picture the register as a row of 64 switches in two halves. An ALU64 instruction works on the whole row. An ALU32 instruction works on the low half as if the high half did not exist, then turns the whole high half off. The 32-bit class is useful because C programs use 32-bit types heavily, because a 32-bit processor needs two instructions for a 64-bit addition, and because x86-64 and 64-bit Arm processors clear a register’s upper half whenever they write its 32-bit form, so an ALU32 instruction becomes one processor instruction there.
How it works
The opcode §2.8, the first byte of every eBPF instruction, ends in a 3-bit class field: class 4, which RFC 9669 calls ALU, is ALU32, and class 7, ALU64, is ALU64. The remaining bits choose the operation in the same way in both, so adding a constant is opcode 0x04 in ALU32 and 0x07 in ALU64, and adding a register is 0x0c and 0x0f. Assembly notation writes w0 += 1 for the ALU32 form, with w0 naming the low 32 bits of register R0, and r0 += 1 for the ALU64 form.
An ALU32 instruction works in three steps.
Take the low 32 bits of the destination register and of the source operand, which is a second register or the 32-bit constant inside the instruction, called the immediate.
Compute on 32 bits. A result that needs more bits wraps around: only its low 32 bits are kept. This is unsigned modular arithmetic §4.16 with modulus .
Write the result into the low half of the destination and set the high half to zero. Filling new high bits with zeros is called zero extension §4.19.
An ALU64 instruction computes on all 64 bits, modulo . Its immediate is still 32 bits long, so the immediate is first sign-extended: its highest bit is copied into 32 new high bits, which keeps its signed value. The constant , stored as 0xffffffff, becomes 0xffffffffffffffff, which is as a signed 64-bit number.
RFC 9669 adds rules for a few operations.
Shifts use the shift amount modulo 32 in ALU32 and modulo 64 in ALU64.
Division by zero sets the destination to zero; modulo by zero leaves an ALU64 destination unchanged, and in ALU32 keeps the low half and sets the high half to zero.
The sign-extending move
MOVSXin ALU32 extends an 8-bit or 16-bit value to 32 bits, then sets the high half to zero.The byte swap instruction,
END, has its own width rule. It works on the destination register alone, and its immediate gives the width: 16, 32 or 64 bits. It takes that many low bits of the register and converts them between byte orders §4.21: in classALU, between the byte order of the machine that runs the program and little-endian order (lowest byte first) or big-endian order (highest byte first), as one bit of the opcode chooses; in classALU64, where that bit must be 0, it always reverses the order of their bytes. A result of 16 or 32 bits is zero-extended to 64 bits, while a 64-bit byte swap writes all 64 bits, even in classALU.Every runtime, the software that runs eBPF programs, must implement the conformance group
base32, which holds, among other instructions, the ALU32 instructions other than multiplication, division and modulo. The ALU64 instructions are in the optional groupbase64, and multiplication, division and modulo in the optional groupsdivmul32anddivmul64. Byte swaps are sorted by width, not by class: width 64 is inbase64, and widths 16 and 32 are inbase32.
Worked example
Registers are shown as 16 hexadecimal digits, which is 64 bits. Case 1, a carry. R0 holds 0x00000000ffffffff, which is . The instruction w0 += 1, bytes 04 00 00 00 01 00 00 00, adds in the low half: . The remainder after division by is 0, so R0 becomes 0. The instruction r0 += 1, bytes 07 00 00 00 01 00 00 00, keeps the whole sum, because , so R0 becomes 0x0000000100000000. A following if r0 == 0 jump is taken only after the ALU32 addition.
Case 2, old high bits. R1 holds 0xffffffff00000005. w1 += 1 computes in the low half and clears the high half, so R1 becomes 6. r1 += 1 gives 0xffffffff00000006. An interpreter §2.13 that forgot to clear the high half would also give 0xffffffff00000006 for w1 += 1, and a test that inspects only the low half, 6 either way, would not notice.
Case 3, a negative result. R2 holds 0. w2 -= 1 computes , which wraps to on 32 bits. Zero extension makes R2 equal to 0x00000000ffffffff, the 64-bit number 4,294,967,295, not . r2 -= 1 gives 0xffffffffffffffff, which is as a signed 64-bit number. Added to a memory address, the ALU64 result moves the address back by 1 byte; the ALU32 result moves it forward by 4,294,967,295 bytes, almost 4 GiB.
Stated precisely
Let be the value of the destination register before the instruction and the value of the source operand, each read as an unsigned 64-bit integer; an immediate is first sign-extended to 64 bits. Write for the remainder of after division by , a number from 0 to , and for the low 32 bits of . For addition, the new value of the destination is Subtraction, multiplication and the bitwise operations §4.50 AND, OR and XOR (exclusive or) follow the same pattern. Every ALU32 result, except that of a 64-bit byte swap, satisfies , which says that its high 32 bits are zero.
For these six operations the ALU32 result is the low half of the ALU64 result, because the low 32 bits of a sum, difference, product or bitwise result depend only on the low 32 bits of the inputs; computing in 64 bits and keeping the low half is correct for them. For shifts, division and modulo it is wrong. With equal to 0x0000000100000000, a 64-bit right shift by 1 gives 0x0000000080000000, low half 0x80000000, but the ALU32 shift starts from and gives 0. With and a left shift by 40, ALU32 shifts by and gives 256, but the low half of is 0.
These equations are the definitions in RFC 9669. An interpreter, or a JIT §2.14 (just-in-time) compiler, which translates the program into processor instructions just before it runs, is correct only if it produces the same 64 bits for every input.
Limits and common mistakes
In C, implement ALU32 with unsigned arithmetic, which wraps by definition, and convert each result to uint32_t, keeping its low 32 bits. Signed integer overflow §4.15 is undefined behavior §4.12 in C, so wrap-around in int32_t cannot be relied on, even when it seems to work. Declaring the operands uint32_t does not by itself make the arithmetic unsigned, because of integer promotion §4.18: before arithmetic, C converts a uint32_t operand to the signed type int whenever int can hold every uint32_t value. With the usual compilers for x86-64 and Arm processors, int has 32 bits, cannot hold every such value, and the arithmetic stays unsigned. With a 64-bit int, the operands become signed, and exceeds the largest int, : a signed overflow. Writing 1u * a * b instead of a * b, where 1u is the constant 1 of type unsigned int, keeps the arithmetic unsigned on every machine.
A verifier, a checker that examines a program before it runs, relies on the zero high half. The Linux eBPF verifier §3.3 tracks bounds for the whole register and for its low 32 bits and records a zero high half after a 32-bit arithmetic result, so an interpreter or compiler that leaves old high bits runs a program the verifier never approved. The verifier itself must apply the rule correctly: a verifier that derives wrong bounds for 32-bit instructions can accept programs that reach memory out of bounds.
Jumps split the same way: class JMP compares all 64 bits, and class JMP32 only the low 32. ALU32 does not make a register narrower; a later ALU64 instruction reads all 64 bits, zeros included.
Questions answered
What happens to the upper register bits after a 32-bit operation?
After an ordinary 32-bit (ALU32) operation, the upper 32 bits of the 64-bit destination register are set to zero. This is the rule of eBPF (extended Berkeley Packet Filter), an instruction set with 64-bit registers and two classes of arithmetic logic unit (ALU) instructions, ALU32 and ALU64. An ALU32 instruction computes on the low 32 bits of its operands, writes the 32-bit result into the low half of the destination and sets the high half to zero, which is called zero extension. So 0x00000000ffffffff plus 1 gives 0, and 0xffffffff00000005 plus 1 gives 6.
The high half is set to zero even when the 32-bit result is negative as a signed number: in ALU32 gives 0x00000000ffffffff, the 64-bit number 4,294,967,295, not the 64-bit . The ALU32 sign-extending move and ALU32 modulo by zero also leave a zero high half. The one exception in the ALU32 class is a 64-bit byte swap, an instruction that converts all 8 bytes of the register between byte orders; apart from it, the only arithmetic instructions that write all 64 bits from their own result are the ALU64 instructions.
Related entries: Unsigned modular arithmetic (§4.16), Interpreter semantic fidelity (§2.38), Signed versus unsigned comparison (§2.12), Truncation and extension (§4.19), Signed integer overflow (§4.15), Opcode (§2.8). Further reading: [1, 160].