Commit 885f40520e

Paul Berg <paul@plutojl.org>
2024-02-14 10:40:19
c_abi: add vector tests for floats
1 parent 42446e6
Changed files (2)
test
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)));
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);