are static / static local SSE / AVX variables blocking a xmm / ymm register?
avx, c++, sse
Solution
Answering the question that should really be asked here: you should not be worrying about this at all. Zeroing a register via `xor` effectively costs nothing at all most of the time. Modern x86 processors recognize this idiom and handle the zeroing directly in register rename; no µop needs to issue at all. The only time this can slow you down is if you are bound by the front-end, but that is a rather rare situation to be in.
While variations on these questions might be worth pondering in other circumstances (and Mystical's comment gives some good leads on how to answer them yourself), you should really just use `setzero` and call it a day.
Problem
When using SSE intrinsics, often zero vectors are required. One way to avoid creating a zero variable inside a function whenever the function is called (each time effectively calling some xor vector instruction) would be to use a static local variable, as in ``` static inline __m128i negate(__m128i a) { static __m128i zero = __mm_setzero_si128(); return _mm_sub_epi16(zero, a); } ``` It seems the variable is only initialized when the function is called for the first time. (I checked this by calling a true function instead of the _mm_setzero_si128() intrinsic. It only seems to be possible in C++, not in C, by the way.) (1) However, once this initialization has happened: Does this block a xmm register for the rest of the program? (2) Even worse: If such a static local variable is used in multiple functions, would it block multiple xmm registers? (3) The other way round: If it is not blocking a xmm register, would the zero variable always be reloaded from memory when the function is called? Then the static local variable would be pointless since it would be faster to use _mm_setzero_si128(). As an alternative, I was thinking about putting zero into a global static variable that would be initialized at program start: ``` static __m128i zero = _mm_setzero_si128(); ``` (4) Would the global variable stay in a xmm register while the program runs? Thanks a lot for your help! (Since this also applies to AVX intrinsics, I also added the AVX tag.)