zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 885f40520e64b8a433ff437ba48ef7e87ad78e1b (tree)
parent 42446e6bf9cd224125fbbf334dd5a0712c94e8c5
Author: Paul Berg <paul@plutojl.org>
Date:   Wed, 14 Feb 2024 10:40:19 +0100

c_abi: add vector tests for floats

Diffstat:
Mtest/c_abi/cfuncs.c | 30++++++++++++++++++++++++++++++
Mtest/c_abi/main.zig | 28++++++++++++++++++++++++++++
2 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c @@ -236,6 +236,36 @@ struct SplitStructMixed zig_ret_split_struct_mixed(); struct BigStruct zig_big_struct_both(struct BigStruct); +typedef float Vector2Float __attribute__((ext_vector_type(2))); +typedef float Vector4Float __attribute__((ext_vector_type(4))); + +void c_vector_2_float(Vector2Float vec) { + assert_or_panic(vec[0] == 1.0); + assert_or_panic(vec[1] == 2.0); +} + +void c_vector_4_float(Vector4Float vec) { + assert_or_panic(vec[0] == 1.0); + assert_or_panic(vec[1] == 2.0); + assert_or_panic(vec[2] == 3.0); + assert_or_panic(vec[3] == 4.0); +} + +Vector2Float c_ret_vector_2_float(void) { + return (Vector2Float){ + 1.0, + 2.0, + }; +} +Vector4Float c_ret_vector_4_float(void) { + return (Vector4Float){ + 1.0, + 2.0, + 3.0, + 4.0, + }; +} + #if defined(ZIG_BACKEND_STAGE2_X86_64) || defined(ZIG_PPC32) || defined(__wasm__) typedef bool Vector2Bool __attribute__((ext_vector_type(2))); diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig @@ -890,6 +890,34 @@ test "big simd vector" { try expect(x[7] == 16); } +const Vector2Float = @Vector(2, f32); +const Vector4Float = @Vector(4, f32); + +extern fn c_vector_2_float(Vector2Float) void; +extern fn c_vector_4_float(Vector4Float) void; + +extern fn c_ret_vector_2_float() Vector2Float; +extern fn c_ret_vector_4_float() Vector4Float; + +test "float simd vectors" { + if (builtin.cpu.arch == .powerpc or builtin.cpu.arch == .powerpc64le) return error.SkipZigTest; + + { + c_vector_2_float(.{ 1.0, 2.0 }); + const vec = c_ret_vector_2_float(); + try expect(vec[0] == 1.0); + try expect(vec[1] == 2.0); + } + { + c_vector_4_float(.{ 1.0, 2.0, 3.0, 4.0 }); + const vec = c_ret_vector_4_float(); + try expect(vec[0] == 1.0); + try expect(vec[1] == 2.0); + try expect(vec[2] == 3.0); + try expect(vec[3] == 4.0); + } +} + const Vector2Bool = @Vector(2, bool); const Vector4Bool = @Vector(4, bool); const Vector8Bool = @Vector(8, bool);