Clear a vector register with vpxor ymm0, ymm0, ymm0
Zero a 256-bit register with vpxor ymm0, ymm0, ymm0, which needs AVX2; NASM 2.16.03 example that proves the lanes are cleared.
XOR-ing a register with itself is the standard way to zero it, and the CPU recognises the pattern as a zeroing idiom, so the result does not wait for the register's old value. The 256-bit form has a catch: vpxor ymm0, ymm0, ymm0 is an integer instruction and exists only with AVX2. On an AVX-only CPU, such as Sandy Bridge, the same job is vxorps ymm0, ymm0, ymm0. This NASM 2.16.03 program loads eight 7s, clears the register and checks the last lane.
default rel
global _start
section .data align=32
sevens dd 7, 7, 7, 7, 7, 7, 7, 7
section .bss align=32
out resd 8
section .text
_start:
vmovdqu ymm0, [sevens]
vpxor ymm0, ymm0, ymm0
vmovdqu [out], ymm0
mov edi, [out + 28] ; lane 7
vzeroupper
mov eax, 60
syscall$ ./zero; echo $?
0Without the vpxor the status would be 7. The vzeroupper at the end clears the upper halves of all ymm registers, which avoids a penalty when old non-VEX SSE code runs next on some processors. Check for AVX2 at run time with cpuid before using the instruction; the kernel must also have enabled YMM state, which xgetbv reports.