Skip to content

Commit

Permalink
simd: implement integer to integer narrowing ops
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed May 25, 2023
1 parent 9e9ece2 commit 12e347e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
43 changes: 43 additions & 0 deletions lib/insn_impl_simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,46 @@ SIMD_OP1(f32x4_convert_i32x4_s, CONVERT_32_s)
SIMD_OP1(f32x4_convert_i32x4_u, CONVERT_32_u)
SIMD_OP1(f64x2_convert_low_i32x4_s, CONVERT_LOW_64_s)
SIMD_OP1(f64x2_convert_low_i32x4_u, CONVERT_LOW_64_u)

#define NARROW_SAT_s(LS, a) \
((a >= INT##LS##_MAX) ? INT##LS##_MAX \
: (a <= INT##LS##_MIN) ? INT##LS##_MIN \
: a)

#define __NARROW_s1(LS, FROM, a, b, c, I) \
do { \
if (I < 128 / LS / 2) { \
LANEPTRi##LS(a)[I] = NARROW_SAT_s( \
LS, (int##FROM##_t)LANEPTRi##FROM(b)[I * 2]); \
} else { \
LANEPTRi##LS(a)[I] = NARROW_SAT_s( \
LS, (int##FROM##_t)LANEPTRi##FROM( \
c)[(I - 128 / LS / 2) * 2]); \
} \
} while (0)

#define __NARROW_u1(LS, FROM, a, b, c, I) \
do { \
if (I < 128 / LS / 2) { \
LANEPTRi##LS(a)[I] = LANEPTRi##FROM(b)[I * 2]; \
} else { \
LANEPTRi##LS(a)[I] = \
LANEPTRi##FROM(c)[(I - 128 / LS / 2) * 2]; \
} \
} while (0)

#define _NARROW_s1(LS, FROM, a, b, c, I) __NARROW_s1(LS, FROM, a, b, c, I)
#define NARROW_s1(LS, a, b, c, I) _NARROW_s1(LS, DBL##LS, a, b, c, I)

#define _NARROW_u1(LS, FROM, a, b, c, I) __NARROW_u1(LS, FROM, a, b, c, I)
#define NARROW_u1(LS, a, b, c, I) _NARROW_u1(LS, DBL##LS, a, b, c, I)

#define NARROW_8_s(a, b, c) FOREACH_LANES3(8, a, b, c, NARROW_s1)
#define NARROW_8_u(a, b, c) FOREACH_LANES3(8, a, b, c, NARROW_u1)
#define NARROW_16_s(a, b, c) FOREACH_LANES3(16, a, b, c, NARROW_s1)
#define NARROW_16_u(a, b, c) FOREACH_LANES3(16, a, b, c, NARROW_u1)

SIMD_OP2(i8x16_narrow_i16x8_s, NARROW_8_s)
SIMD_OP2(i8x16_narrow_i16x8_u, NARROW_8_u)
SIMD_OP2(i16x8_narrow_i16x8_s, NARROW_16_s)
SIMD_OP2(i16x8_narrow_i16x8_u, NARROW_16_u)
4 changes: 1 addition & 3 deletions lib/insn_list_simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,10 @@ INSTRUCTION(0x61, "i8x16.neg", i8x16_neg, 0)

INSTRUCTION(0x63, "i8x16.all_true", i8x16_all_true, 0)
INSTRUCTION(0x64, "i8x16.bitmask", i8x16_bitmask, 0)
#if 0
/* https://github.com/WebAssembly/simd/blob/main/proposals/simd/SIMD.md#integer-to-integer-narrowing */
INSTRUCTION(0x65, "i8x16.narrow_i16x8_s", i8x16_narrow_i16x8_s, 0)
INSTRUCTION(0x66, "i8x16.narrow_i16x8_u", i8x16_narrow_i16x8_u, 0)

#endif
INSTRUCTION(0x6b, "i8x16.shl", i8x16_shl, 0)
INSTRUCTION(0x6c, "i8x16.shr_s", i8x16_shr_s, 0)
INSTRUCTION(0x6d, "i8x16.shr_u", i8x16_shr_u, 0)
Expand All @@ -161,10 +159,10 @@ INSTRUCTION(0x81, "i16x8.neg", i16x8_neg, 0)

INSTRUCTION(0x83, "i16x8.all_true", i16x8_all_true, 0)
INSTRUCTION(0x84, "i16x8.bitmask", i16x8_bitmask, 0)
#if 0
INSTRUCTION(0x85, "i16x8.narrow_i16x8_s", i16x8_narrow_i16x8_s, 0)
INSTRUCTION(0x86, "i16x8.narrow_i16x8_u", i16x8_narrow_i16x8_u, 0)

#if 0
INSTRUCTION(0x87, "i16x8.extend_low_i8x16_s", i16x8_extend_low_i8x16_s, 0)
INSTRUCTION(0x88, "i16x8.extend_high_i8x16_s", i16x8_extend_high_i8x16_s, 0)
INSTRUCTION(0x89, "i16x8.extend_low_i8x16_u", i16x8_extend_low_i8x16_u, 0)
Expand Down

0 comments on commit 12e347e

Please sign in to comment.