]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Remove switch statements in vector8_shift_{left,right}.
authorNathan Bossart <nathan@postgresql.org>
Mon, 6 Jul 2026 16:40:02 +0000 (11:40 -0500)
committerNathan Bossart <nathan@postgresql.org>
Mon, 6 Jul 2026 16:40:02 +0000 (11:40 -0500)
In commit ec8719ccbf, I added switch statements with all expected
shift counts to vector8_shift_{left,right} because vshlq_n_u32()
and vshrq_n_u32() require integer literals.  But we can use
vshlq_u32() instead for both cases, which does not require an
integer literal, thereby avoiding the need for the switch
statements.  This compiles to the same machine code on newer
versions of popular compilers.

Reviewed-by: John Naylor <johncnaylorls@gmail.com>
Discussion: https://postgr.es/m/akWxkA-mszMm57cV%40nathan

src/include/port/simd.h

index 50615aec7f4234f7daf33e29435fc1c47da80e21..25a26c81ccebc47a6618ea5c371a283c6ee59203 100644 (file)
@@ -548,10 +548,6 @@ vector8_pack_16(const Vector8 v1, const Vector8 v2)
 
 /*
  * Unsigned shift left of each 32-bit element in the vector by "i" bits.
- *
- * XXX AArch64 requires an integer literal, so we have to list all expected
- * values of "i" from all callers in a switch statement.  If you add a new
- * caller, be sure your expected values of "i" are handled.
  */
 #ifndef USE_NO_SIMD
 static inline Vector8
@@ -560,24 +556,13 @@ vector8_shift_left(const Vector8 v1, int i)
 #ifdef USE_SSE2
        return _mm_slli_epi32(v1, i);
 #elif defined(USE_NEON)
-       switch (i)
-       {
-               case 4:
-                       return (Vector8) vshlq_n_u32((Vector32) v1, 4);
-               default:
-                       Assert(false);
-                       return vector8_broadcast(0);
-       }
+       return (Vector8) vshlq_u32((Vector32) v1, vdupq_n_s32(i));
 #endif
 }
 #endif                                                 /* ! USE_NO_SIMD */
 
 /*
  * Unsigned shift right of each 32-bit element in the vector by "i" bits.
- *
- * XXX AArch64 requires an integer literal, so we have to list all expected
- * values of "i" from all callers in a switch statement.  If you add a new
- * caller, be sure your expected values of "i" are handled.
  */
 #ifndef USE_NO_SIMD
 static inline Vector8
@@ -586,16 +571,8 @@ vector8_shift_right(const Vector8 v1, int i)
 #ifdef USE_SSE2
        return _mm_srli_epi32(v1, i);
 #elif defined(USE_NEON)
-       switch (i)
-       {
-               case 4:
-                       return (Vector8) vshrq_n_u32((Vector32) v1, 4);
-               case 8:
-                       return (Vector8) vshrq_n_u32((Vector32) v1, 8);
-               default:
-                       Assert(false);
-                       return vector8_broadcast(0);
-       }
+       /* negative shift count means right shift */
+       return (Vector8) vshlq_u32((Vector32) v1, vdupq_n_s32(-i));
 #endif
 }
 #endif                                                 /* ! USE_NO_SIMD */