From c0e6e55fc2ce9e385f97d61a4a099ac89d5a6643 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Thu, 3 Oct 2024 22:57:17 +0000 Subject: [PATCH 1/2] [FP16] Add end to end tests for conversion instructions. --- test/test_fp16.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/test/test_fp16.c b/test/test_fp16.c index d5a0d73ac99a1..c9c98de6af769 100644 --- a/test/test_fp16.c +++ b/test/test_fp16.c @@ -20,6 +20,28 @@ void print_f16x8(v128_t v1) { wasm_f16x8_extract_lane(v1, 6), wasm_f16x8_extract_lane(v1, 7)); } +void print_i16x8(v128_t v1) { + printf("[%d, %d, %d, %d, %d, %d, %d, %d]\n", + wasm_i16x8_extract_lane(v1, 0), + wasm_i16x8_extract_lane(v1, 1), + wasm_i16x8_extract_lane(v1, 2), + wasm_i16x8_extract_lane(v1, 3), + wasm_i16x8_extract_lane(v1, 4), + wasm_u16x8_extract_lane(v1, 5), + wasm_i16x8_extract_lane(v1, 6), + wasm_i16x8_extract_lane(v1, 7)); +} +void print_u16x8(v128_t v1) { + printf("[%d, %d, %d, %d, %d, %d, %d, %d]\n", + wasm_u16x8_extract_lane(v1, 0), + wasm_u16x8_extract_lane(v1, 1), + wasm_u16x8_extract_lane(v1, 2), + wasm_u16x8_extract_lane(v1, 3), + wasm_u16x8_extract_lane(v1, 4), + wasm_u16x8_extract_lane(v1, 5), + wasm_u16x8_extract_lane(v1, 6), + wasm_u16x8_extract_lane(v1, 7)); +} void assert_lanes_eq_uint16(v128_t v, uint16_t l0, @@ -40,6 +62,25 @@ void assert_lanes_eq_uint16(v128_t v, assert(wasm_u16x8_extract_lane(v, 7) == l7); } +void assert_lanes_eq_int16(v128_t v, + int16_t l0, + int16_t l1, + int16_t l2, + int16_t l3, + int16_t l4, + int16_t l5, + int16_t l6, + int16_t l7) { + assert(wasm_i16x8_extract_lane(v, 0) == l0); + assert(wasm_i16x8_extract_lane(v, 1) == l1); + assert(wasm_i16x8_extract_lane(v, 2) == l2); + assert(wasm_i16x8_extract_lane(v, 3) == l3); + assert(wasm_i16x8_extract_lane(v, 4) == l4); + assert(wasm_i16x8_extract_lane(v, 5) == l5); + assert(wasm_i16x8_extract_lane(v, 6) == l6); + assert(wasm_i16x8_extract_lane(v, 7) == l7); +} + void assert_lanes_eq_float(v128_t v, float l0, float l1, @@ -96,6 +137,46 @@ v128_t create_f16x8(float l0, return v; } +v128_t create_i16x8(int16_t l0, + int16_t l1, + int16_t l2, + int16_t l3, + int16_t l4, + int16_t l5, + int16_t l6, + int16_t l7) { + v128_t v; + v = wasm_i16x8_replace_lane(v, 0, l0); + v = wasm_i16x8_replace_lane(v, 1, l1); + v = wasm_i16x8_replace_lane(v, 2, l2); + v = wasm_i16x8_replace_lane(v, 3, l3); + v = wasm_i16x8_replace_lane(v, 4, l4); + v = wasm_i16x8_replace_lane(v, 5, l5); + v = wasm_i16x8_replace_lane(v, 6, l6); + v = wasm_i16x8_replace_lane(v, 7, l7); + return v; +} + +v128_t create_u16x8(uint16_t l0, + uint16_t l1, + uint16_t l2, + uint16_t l3, + uint16_t l4, + uint16_t l5, + uint16_t l6, + uint16_t l7) { + v128_t v; + v = wasm_u16x8_replace_lane(v, 0, l0); + v = wasm_u16x8_replace_lane(v, 1, l1); + v = wasm_u16x8_replace_lane(v, 2, l2); + v = wasm_u16x8_replace_lane(v, 3, l3); + v = wasm_u16x8_replace_lane(v, 4, l4); + v = wasm_u16x8_replace_lane(v, 5, l5); + v = wasm_u16x8_replace_lane(v, 6, l6); + v = wasm_u16x8_replace_lane(v, 7, l7); + return v; +} + int main() { v128_t a, b, c; a = wasm_f16x8_splat(2.0f); @@ -207,4 +288,18 @@ int main() { create_f16x8( 1e4, INFINITY, -1.0f, 0.0f, 1.0f, 1.5f, 4.0f, 1.0f), create_f16x8(INFINITY, INFINITY, -1.0f, 0.0f, 1.0f, 2.0f, 1.0f, -1.0f)); assert_lanes_eq_float(a, INFINITY, INFINITY, -2.0f, 0.0f, 0.0f, -0.25f, 9.0f, -2.0f); + + a = wasm_i16x8_trunc_sat_f16x8(create_f16x8(42.0f, -42.0f, NAN, INFINITY, -INFINITY, 65504.0f, -65504.0f, 0)); + assert_lanes_eq_int16(a, 42, -42, 0, 32767, -32768, 32767, -32768, 0); + + a = wasm_u16x8_trunc_sat_f16x8(create_f16x8(42.0f, -42.0f, NAN, INFINITY, -INFINITY, 65504.0f, -65504.0f, 0)); + assert_lanes_eq_uint16(a, 42, 0, 0, 65535, 0, 65504, 0, 0); + + // One confusing one below is 32767 which cannot be represented in FP16, so it becomes 32768. + a = wasm_f16x8_convert_i16x8(create_i16x8(0, 1, -1, 32767, -32768, 0, 0, 0)); + assert_lanes_eq_float(a, 0, 1.0f, -1.0f, 32768.0f, -32768, 0, 0, 0); + + // Another tricky one below is 65535, FP16 can't represent the max of uint16 so it becomes INFINITY. + a = wasm_f16x8_convert_u16x8(create_u16x8(0, 1, 65535, 65504U, 0, 0, 0, 0)); + assert_lanes_eq_float(a, 0, 1.0f, INFINITY, 65504.0f, 0, 0, 0, 0); } From 84c1ab8dc66e083be13c95139e88c2f26042ed38 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Thu, 3 Oct 2024 23:11:06 +0000 Subject: [PATCH 2/2] review comments --- test/test_fp16.c | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/test/test_fp16.c b/test/test_fp16.c index c9c98de6af769..a4e9ad7d60a8e 100644 --- a/test/test_fp16.c +++ b/test/test_fp16.c @@ -9,38 +9,38 @@ #define FALSE 0 // Not used below, but helpful for debugging. -void print_f16x8(v128_t v1) { +void print_f16x8(v128_t v) { printf("[%g, %g, %g, %g, %g, %g, %g, %g]\n", - wasm_f16x8_extract_lane(v1, 0), - wasm_f16x8_extract_lane(v1, 1), - wasm_f16x8_extract_lane(v1, 2), - wasm_f16x8_extract_lane(v1, 3), - wasm_f16x8_extract_lane(v1, 4), - wasm_f16x8_extract_lane(v1, 5), - wasm_f16x8_extract_lane(v1, 6), - wasm_f16x8_extract_lane(v1, 7)); + wasm_f16x8_extract_lane(v, 0), + wasm_f16x8_extract_lane(v, 1), + wasm_f16x8_extract_lane(v, 2), + wasm_f16x8_extract_lane(v, 3), + wasm_f16x8_extract_lane(v, 4), + wasm_f16x8_extract_lane(v, 5), + wasm_f16x8_extract_lane(v, 6), + wasm_f16x8_extract_lane(v, 7)); } -void print_i16x8(v128_t v1) { +void print_i16x8(v128_t v) { printf("[%d, %d, %d, %d, %d, %d, %d, %d]\n", - wasm_i16x8_extract_lane(v1, 0), - wasm_i16x8_extract_lane(v1, 1), - wasm_i16x8_extract_lane(v1, 2), - wasm_i16x8_extract_lane(v1, 3), - wasm_i16x8_extract_lane(v1, 4), - wasm_u16x8_extract_lane(v1, 5), - wasm_i16x8_extract_lane(v1, 6), - wasm_i16x8_extract_lane(v1, 7)); + wasm_i16x8_extract_lane(v, 0), + wasm_i16x8_extract_lane(v, 1), + wasm_i16x8_extract_lane(v, 2), + wasm_i16x8_extract_lane(v, 3), + wasm_i16x8_extract_lane(v, 4), + wasm_i16x8_extract_lane(v, 5), + wasm_i16x8_extract_lane(v, 6), + wasm_i16x8_extract_lane(v, 7)); } -void print_u16x8(v128_t v1) { +void print_u16x8(v128_t v) { printf("[%d, %d, %d, %d, %d, %d, %d, %d]\n", - wasm_u16x8_extract_lane(v1, 0), - wasm_u16x8_extract_lane(v1, 1), - wasm_u16x8_extract_lane(v1, 2), - wasm_u16x8_extract_lane(v1, 3), - wasm_u16x8_extract_lane(v1, 4), - wasm_u16x8_extract_lane(v1, 5), - wasm_u16x8_extract_lane(v1, 6), - wasm_u16x8_extract_lane(v1, 7)); + wasm_u16x8_extract_lane(v, 0), + wasm_u16x8_extract_lane(v, 1), + wasm_u16x8_extract_lane(v, 2), + wasm_u16x8_extract_lane(v, 3), + wasm_u16x8_extract_lane(v, 4), + wasm_u16x8_extract_lane(v, 5), + wasm_u16x8_extract_lane(v, 6), + wasm_u16x8_extract_lane(v, 7)); } void assert_lanes_eq_uint16(v128_t v,