How to generate an AVX-512 mask based on multiple comparisons of FP values in corresponding elements of vectors?
16:14 24 Apr 2024

I had nothing to do, so I decided to study AVX-512 and try to write something in assembler using it. I'm trying to optimize a piece of code that processes large amounts of data using AVX-512 instructions. The goal is to maximize the capabilities of vector registers and minimize the number of processor cycles.

The problem is this: I want to use masking to process only a portion of the elements in the zmm0 and zmm1 registers depending on a certain condition. However, AVX-512 instructions with masks (such as vaddps) require a mask in the k0-k7 register:

vmovups zmm0, [rsi] ; float[16] <= zmm0
vmovups zmm1, [rsi+64]
 
; some code here
 
vaddps zmm0, zmm0, zmm11 
vmovups [rdi], zmm0
 
add rsi, 128 ; ptr => next[data]
add rdi, 64 ; ptr => next[data] ?to: write

At the same time, the condition by which I want to mask the data is obtained by comparing the other two zmm registers.

So here's the question:

Is there any way to efficiently generate a mask in the k register based on a comparison of values in the zmm registers, and then use it for selective data processing using AVX-512 instructions? Or maybe there is another way to achieve the desired result using the AVX-512 without resorting to masks?

I remember that there is vpcmpd that compares the values of vector registers, and supposedly you can do something like k1 = zmm0 > zmm1 + k2 = zmm0 < zmm2, but honestly I have no idea how effective this can be; i tried, but due to lack of my knowledge, i threw away this idea.

assembly x86 cpu-registers avx512