Search code examples
pointersssesimd

Dereference pointers in XMM register (gather)


If I have some pointer or pointer-like values packed into an SSE or AVX register, is there any particularly efficient way to dereference them, into another such register? ("Particularly efficient" meaning "more efficient than just using memory for the values".) Is there any way to dereference them all without writing an intermediate copy of the register out to memory?

Edit for clarification: that means, assuming 32-bit pointers and SSE, to index into four arbitrary memory areas at once with the four sections of an XMM register and return four results at once to another register. Or as close to "at once" as possible. (/edit)

Edit2: thanks to PaulR's answer I guess the terminology I'm looking for is "gather", and the question therefore is "what's the best way to implement gather for systems pre-AVX2?".

I assume there isn't an instruction for this since ...well, one doesn't appear to exist as far as I can tell and anyway it doesn't seem to be what SSE is designed for at all.

("Pointer-like value" meaning something like an integer index into an array pretending to be the heap; mechanically very different but conceptually the same thing. If, say, one wanted to use 32-bit or even 16-bit values regardless of the native pointer size, to fit more values in a register.)


Two possible reason I can think of why one might want to do this:

  • thought it might be interesting to explore using the SSE registers for general-purpose... stuff, perhaps to have four identical 'threads' processing potentially completely unrelated/non-contiguous data, slicing through the registers "vertically" rather than "horizontally" (i.e. instead of the way they were designed to be used).

  • to build something like romcc if for some reason (probably not a good one), one didn't want to write anything to memory, and therefore would need more register storage.

This might sound like an XY problem, but it isn't, it's just curiosity/stupidity. I'll go looking for nails once I have my hammer.


Solution

  • The question is not entirely clear, but if you want to dereference vector register elements then the only instructions which might help you here are AVX2's gathered loads, e.g. _mm256_i32gather_epi32 et al. See the AVX2 section of the Intel Intrinsics Guide.

    SYNOPSIS
    
    __m256i _mm256_i32gather_epi32 (int const* base_addr, __m256i vindex, const int scale)
    #include "immintrin.h"
    Instruction: vpgatherdd ymm, vm32x, ymm
    CPUID Flag : AVX2
    
    DESCRIPTION
    
    Gather 32-bit integers from memory using 32-bit indices. 32-bit elements are loaded from addresses starting at base_addr and offset by each 32-bit element in vindex (each index is scaled by the factor in scale). Gathered elements are merged into dst. scale should be 1, 2, 4 or 8.
    
    OPERATION
    
    FOR j := 0 to 7
        i := j*32
        dst[i+31:i] := MEM[base_addr + SignExtend(vindex[i+31:i])*scale]
    ENDFOR
    dst[MAX:256] := 0