Skip to content

Commit 59cfa1a

Browse files
James Marshloganek
authored andcommitted
Fix spec tests when WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS is 0
1 parent 93feee8 commit 59cfa1a

File tree

2 files changed

+77
-49
lines changed

2 files changed

+77
-49
lines changed

core/iwasm/common/wasm_runtime_common.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -287,19 +287,33 @@ STORE_V128(void *addr, V128 value)
287287
if ((addr_ & (uintptr_t)15) == 0) {
288288
*(V128 *)addr = value;
289289
}
290+
else if ((addr_ & (uintptr_t)7) == 0) {
291+
u.val = value;
292+
((uint64 *)(addr))[0] = u.u64[0];
293+
((uint64 *)(addr))[1] = u.u64[1];
294+
}
295+
else if ((addr_ & (uintptr_t)3) == 0) {
296+
u.val = value;
297+
((uint32 *)addr)[0] = u.u32[0];
298+
((uint32 *)addr)[1] = u.u32[1];
299+
((uint32 *)addr)[2] = u.u32[2];
300+
((uint32 *)addr)[3] = u.u32[3];
301+
}
302+
else if ((addr_ & (uintptr_t)1) == 0) {
303+
u.val = value;
304+
((uint16 *)addr)[0] = u.u16[0];
305+
((uint16 *)addr)[1] = u.u16[1];
306+
((uint16 *)addr)[2] = u.u16[2];
307+
((uint16 *)addr)[3] = u.u16[3];
308+
((uint16 *)addr)[4] = u.u16[4];
309+
((uint16 *)addr)[5] = u.u16[5];
310+
((uint16 *)addr)[6] = u.u16[6];
311+
((uint16 *)addr)[7] = u.u16[7];
312+
}
290313
else {
291314
u.val = value;
292-
if ((addr_ & (uintptr_t)7) == 0) {
293-
((uint64 *)(addr))[0] = u.u64[0];
294-
((uint64 *)(addr))[1] = u.u64[1];
295-
}
296-
else {
297-
bh_assert((addr_ & (uintptr_t)3) == 0);
298-
((uint32 *)addr)[0] = u.u32[0];
299-
((uint32 *)addr)[1] = u.u32[1];
300-
((uint32 *)addr)[2] = u.u32[2];
301-
((uint32 *)addr)[3] = u.u32[3];
302-
}
315+
for (int i = 0; i < 16; i++)
316+
((uint8 *)addr)[i] = u.u8[i];
303317
}
304318
}
305319

@@ -322,13 +336,26 @@ LOAD_V128(void *addr)
322336
u.u64[0] = ((uint64 *)addr)[0];
323337
u.u64[1] = ((uint64 *)addr)[1];
324338
}
325-
else {
326-
bh_assert((addr1 & (uintptr_t)3) == 0);
339+
else if ((addr1 & (uintptr_t)3) == 0) {
327340
u.u32[0] = ((uint32 *)addr)[0];
328341
u.u32[1] = ((uint32 *)addr)[1];
329342
u.u32[2] = ((uint32 *)addr)[2];
330343
u.u32[3] = ((uint32 *)addr)[3];
331344
}
345+
else if ((addr1 & (uintptr_t)1) == 0) {
346+
u.u16[0] = ((uint16 *)addr)[0];
347+
u.u16[1] = ((uint16 *)addr)[1];
348+
u.u16[2] = ((uint16 *)addr)[2];
349+
u.u16[3] = ((uint16 *)addr)[3];
350+
u.u16[4] = ((uint16 *)addr)[4];
351+
u.u16[5] = ((uint16 *)addr)[5];
352+
u.u16[6] = ((uint16 *)addr)[6];
353+
u.u16[7] = ((uint16 *)addr)[7];
354+
}
355+
else {
356+
for (int i = 0; i < 16; i++)
357+
u.u8[i] = ((uint8 *)addr)[i];
358+
}
332359
return u.val;
333360
}
334361

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5843,60 +5843,58 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
58435843
PUT_V128_TO_ADDR(frame_lp + addr_ret, LOAD_V128(maddr));
58445844
break;
58455845
}
5846-
#define SIMD_LOAD_OP(simde_func, element_size, num_elements) \
5847-
do { \
5848-
uint32 offset, addr; \
5849-
offset = read_uint32(frame_ip); \
5850-
addr = GET_OPERAND(uint32, I32, 0); \
5851-
frame_ip += 2; \
5852-
addr_ret = GET_OFFSET(); \
5853-
CHECK_MEMORY_OVERFLOW(16); \
5854-
\
5855-
simde_v128_t simde_result = simde_func(maddr); \
5856-
\
5857-
V128 result; \
5858-
SIMDE_V128_TO_SIMD_V128(simde_result, result); \
5859-
PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
5860-
\
5846+
#define SIMD_LOAD_OP(simde_func) \
5847+
do { \
5848+
uint32 offset, addr; \
5849+
offset = read_uint32(frame_ip); \
5850+
addr = POP_I32(); \
5851+
addr_ret = GET_OFFSET(); \
5852+
CHECK_MEMORY_OVERFLOW(8); \
5853+
\
5854+
simde_v128_t simde_result = simde_func(maddr); \
5855+
\
5856+
V128 result; \
5857+
SIMDE_V128_TO_SIMD_V128(simde_result, result); \
5858+
PUT_V128_TO_ADDR(frame_lp + addr_ret, result); \
5859+
\
58615860
} while (0)
58625861
case SIMD_v128_load8x8_s:
58635862
{
5864-
SIMD_LOAD_OP(simde_wasm_i16x8_load8x8, 16, 8);
5863+
SIMD_LOAD_OP(simde_wasm_i16x8_load8x8);
58655864
break;
58665865
}
58675866
case SIMD_v128_load8x8_u:
58685867
{
5869-
SIMD_LOAD_OP(simde_wasm_u16x8_load8x8, 16, 8);
5868+
SIMD_LOAD_OP(simde_wasm_u16x8_load8x8);
58705869
break;
58715870
}
58725871
case SIMD_v128_load16x4_s:
58735872
{
5874-
SIMD_LOAD_OP(simde_wasm_i32x4_load16x4, 32, 4);
5873+
SIMD_LOAD_OP(simde_wasm_i32x4_load16x4);
58755874
break;
58765875
}
58775876
case SIMD_v128_load16x4_u:
58785877
{
5879-
SIMD_LOAD_OP(simde_wasm_u32x4_load16x4, 32, 4);
5878+
SIMD_LOAD_OP(simde_wasm_u32x4_load16x4);
58805879
break;
58815880
}
58825881
case SIMD_v128_load32x2_s:
58835882
{
5884-
SIMD_LOAD_OP(simde_wasm_i64x2_load32x2, 64, 2);
5883+
SIMD_LOAD_OP(simde_wasm_i64x2_load32x2);
58855884
break;
58865885
}
58875886
case SIMD_v128_load32x2_u:
58885887
{
5889-
SIMD_LOAD_OP(simde_wasm_u64x2_load32x2, 64, 2);
5888+
SIMD_LOAD_OP(simde_wasm_u64x2_load32x2);
58905889
break;
58915890
}
5892-
#define SIMD_LOAD_SPLAT_OP(simde_func) \
5891+
#define SIMD_LOAD_SPLAT_OP(simde_func, width) \
58935892
do { \
58945893
uint32 offset, addr; \
58955894
offset = read_uint32(frame_ip); \
5896-
addr = GET_OPERAND(uint32, I32, 0); \
5897-
frame_ip += 2; \
5895+
addr = POP_I32(); \
58985896
addr_ret = GET_OFFSET(); \
5899-
CHECK_MEMORY_OVERFLOW(4); \
5897+
CHECK_MEMORY_OVERFLOW(width / 8); \
59005898
\
59015899
simde_v128_t simde_result = simde_func(maddr); \
59025900
\
@@ -5908,22 +5906,22 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
59085906

59095907
case SIMD_v128_load8_splat:
59105908
{
5911-
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load8_splat);
5909+
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load8_splat, 8);
59125910
break;
59135911
}
59145912
case SIMD_v128_load16_splat:
59155913
{
5916-
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load16_splat);
5914+
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load16_splat, 16);
59175915
break;
59185916
}
59195917
case SIMD_v128_load32_splat:
59205918
{
5921-
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load32_splat);
5919+
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load32_splat, 32);
59225920
break;
59235921
}
59245922
case SIMD_v128_load64_splat:
59255923
{
5926-
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load64_splat);
5924+
SIMD_LOAD_SPLAT_OP(simde_wasm_v128_load64_splat, 64);
59275925
break;
59285926
}
59295927
case SIMD_v128_store:
@@ -5933,9 +5931,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
59335931
V128 data = POP_V128();
59345932
addr = POP_I32();
59355933

5936-
V128 data;
5937-
data = POP_V128();
5938-
59395934
CHECK_MEMORY_OVERFLOW(16);
59405935
STORE_V128(maddr, data);
59415936
break;
@@ -5952,7 +5947,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
59525947
PUT_V128_TO_ADDR(frame_lp + addr_ret, *(V128 *)orig_ip);
59535948
break;
59545949
}
5955-
// TODO: Add a faster SIMD implementation
5950+
/* TODO: Add a faster SIMD implementation */
59565951
case SIMD_v8x16_shuffle:
59575952
{
59585953
V128 indices;
@@ -6053,15 +6048,22 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
60536048
SIMD_SPLAT_OP_F64(simde_wasm_f64x2_splat);
60546049
break;
60556050
}
6051+
#if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
6052+
#define SIMD_LANE_HANDLE_UNALIGNED_ACCESS()
6053+
#else
6054+
#define SIMD_LANE_HANDLE_UNALIGNED_ACCESS() *frame_ip++;
6055+
#endif
60566056
#define SIMD_EXTRACT_LANE_OP(register, return_type, push_elem) \
60576057
do { \
60586058
uint8 lane = *frame_ip++; \
6059+
SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
60596060
V128 v = POP_V128(); \
60606061
push_elem((return_type)(v.register[lane])); \
60616062
} while (0)
60626063
#define SIMD_REPLACE_LANE_OP(register, return_type, pop_elem) \
60636064
do { \
60646065
uint8 lane = *frame_ip++; \
6066+
SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
60656067
return_type replacement = pop_elem(); \
60666068
V128 v = POP_V128(); \
60676069
v.register[lane] = replacement; \
@@ -6482,6 +6484,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
64826484
V128 vec = POP_V128(); \
64836485
addr = POP_I32(); \
64846486
int lane = *frame_ip++; \
6487+
SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
64856488
SIMD_LOAD_LANE_COMMON(vec, register, lane, width); \
64866489
} while (0)
64876490

@@ -6512,6 +6515,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
65126515
V128 vec = POP_V128(); \
65136516
addr = POP_I32(); \
65146517
int lane = *frame_ip++; \
6518+
SIMD_LANE_HANDLE_UNALIGNED_ACCESS(); \
65156519
CHECK_MEMORY_OVERFLOW(width / 8); \
65166520
if (width == 64) { \
65176521
STORE_I64(maddr, vec.register[lane]); \
@@ -6659,7 +6663,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
66596663
SIMD_SINGLE_OP(simde_wasm_f32x4_nearest);
66606664
break;
66616665
}
6662-
// TODO: Check count?
66636666
#define SIMD_LANE_SHIFT(simde_func) \
66646667
do { \
66656668
int32 count = POP_I32(); \
@@ -7131,8 +7134,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
71317134
SIMD_SINGLE_OP(simde_wasm_u64x2_extend_high_u32x4);
71327135
break;
71337136
}
7134-
7135-
// TODO: Verify count works
71367137
case SIMD_i64x2_shl:
71377138
{
71387139
SIMD_LANE_SHIFT(simde_wasm_i64x2_shl);

0 commit comments

Comments
 (0)