Commit 7e946bc790

Veikka Tuominen <git@vexu.eu>
2022-10-20 09:52:26
make C ABI tests compile on i386
1 parent 5149118
Changed files (2)
test
test/c_abi/cfuncs.c
@@ -12,6 +12,15 @@ static void assert_or_panic(bool ok) {
     }
 }
 
+#ifdef __i386__
+#  define ZIG_NO_I128
+#endif
+
+#ifdef __i386__
+#  define ZIG_NO_COMPLEX
+#endif
+
+#ifndef ZIG_NO_I128
 struct i128 {
     __int128 value;
 };
@@ -19,17 +28,22 @@ struct i128 {
 struct u128 {
     unsigned __int128 value;
 };
+#endif
 
 void zig_u8(uint8_t);
 void zig_u16(uint16_t);
 void zig_u32(uint32_t);
 void zig_u64(uint64_t);
+#ifndef ZIG_NO_I128
 void zig_struct_u128(struct u128);
+#endif
 void zig_i8(int8_t);
 void zig_i16(int16_t);
 void zig_i32(int32_t);
 void zig_i64(int64_t);
+#ifndef ZIG_NO_I128
 void zig_struct_i128(struct i128);
+#endif
 void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);
 
 void zig_f32(float);
@@ -95,7 +109,9 @@ void zig_med_struct_mixed(struct MedStructMixed);
 struct MedStructMixed zig_ret_med_struct_mixed();
 
 void zig_small_packed_struct(uint8_t);
+#ifndef ZIG_NO_I128
 void zig_big_packed_struct(__int128);
+#endif
 
 struct SplitStructInts {
     uint64_t a;
@@ -151,19 +167,26 @@ void run_c_tests(void) {
     zig_u16(0xfffe);
     zig_u32(0xfffffffd);
     zig_u64(0xfffffffffffffffc);
+
+#ifndef ZIG_NO_I128
     {
         struct u128 s = {0xfffffffffffffffc};
         zig_struct_u128(s);
     }
+#endif
 
     zig_i8(-1);
     zig_i16(-2);
     zig_i32(-3);
     zig_i64(-4);
+
+#ifndef ZIG_NO_I128
     {
         struct i128 s = {-6};
         zig_struct_i128(s);
     }
+#endif
+
     zig_five_integers(12, 34, 56, 78, 90);
 
     zig_f32(12.34f);
@@ -175,6 +198,7 @@ void run_c_tests(void) {
 
     zig_bool(true);
 
+#ifndef ZIG_NO_COMPLEX
     // TODO: Resolve https://github.com/ziglang/zig/issues/8465
     //{
     //    float complex a = 1.25f + I * 2.6f;
@@ -211,23 +235,28 @@ void run_c_tests(void) {
         assert_or_panic(creal(z) == 1.5);
         assert_or_panic(cimag(z) == 13.5);
     }
+#endif
 
     {
         struct BigStruct s = {1, 2, 3, 4, 5};
         zig_big_struct(s);
     }
 
+#ifndef __i386__
     {
         struct SmallStructInts s = {1, 2, 3, 4};
         zig_small_struct_ints(s);
     }
+#endif
 
+#ifndef ZIG_NO_I128
     {
         __int128 s = 0;
         s |= 1 << 0;
         s |= (__int128)2 << 64;
         zig_big_packed_struct(s);
     }
+#endif
 
     {
         uint8_t s = 0;
@@ -238,20 +267,24 @@ void run_c_tests(void) {
         zig_small_packed_struct(s);
     }
 
+#ifndef __i386__
     {
         struct SplitStructInts s = {1234, 100, 1337};
         zig_split_struct_ints(s);
     }
+#endif
 
     {
         struct MedStructMixed s = {1234, 100.0f, 1337.0f};
         zig_med_struct_mixed(s);
     }
 
+#ifndef __i386__
     {
         struct SplitStructMixed s = {1234, 100, 1337.0f};
         zig_split_struct_mixed(s);
     }
+#endif
 
     {
         struct BigStruct s = {30, 31, 32, 33, 34};
@@ -306,9 +339,11 @@ void c_u64(uint64_t x) {
     assert_or_panic(x == 0xfffffffffffffffcULL);
 }
 
+#ifndef ZIG_NO_I128
 void c_struct_u128(struct u128 x) {
     assert_or_panic(x.value == 0xfffffffffffffffcULL);
 }
+#endif
 
 void c_i8(int8_t x) {
     assert_or_panic(x == -1);
@@ -326,9 +361,11 @@ void c_i64(int64_t x) {
     assert_or_panic(x == -4);
 }
 
+#ifndef ZIG_NO_I128
 void c_struct_i128(struct i128 x) {
     assert_or_panic(x.value == -6);
 }
+#endif
 
 void c_f32(float x) {
     assert_or_panic(x == 12.34f);
@@ -495,6 +532,7 @@ void c_small_packed_struct(uint8_t x) {
     assert_or_panic(((x >> 6) & 0x3) == 3);
 }
 
+#ifndef ZIG_NO_I128
 __int128 c_ret_big_packed_struct() {
     __int128 s = 0;
     s |= 1 << 0;
@@ -506,6 +544,7 @@ void c_big_packed_struct(__int128 x) {
     assert_or_panic(((x >> 0) & 0xFFFFFFFFFFFFFFFF) == 1);
     assert_or_panic(((x >> 64) & 0xFFFFFFFFFFFFFFFF) == 2);
 }
+#endif
 
 struct SplitStructMixed c_ret_split_struct_mixed() {
     struct SplitStructMixed s = {
test/c_abi/main.zig
@@ -2,6 +2,7 @@ const std = @import("std");
 const builtin = @import("builtin");
 const print = std.debug.print;
 const expect = std.testing.expect;
+const has_i128 = builtin.cpu.arch != .i386;
 
 extern fn run_c_tests() void;
 
@@ -40,13 +41,13 @@ test "C ABI integers" {
     c_u16(0xfffe);
     c_u32(0xfffffffd);
     c_u64(0xfffffffffffffffc);
-    c_struct_u128(.{ .value = 0xfffffffffffffffc });
+    if (has_i128) c_struct_u128(.{ .value = 0xfffffffffffffffc });
 
     c_i8(-1);
     c_i16(-2);
     c_i32(-3);
     c_i64(-4);
-    c_struct_i128(.{ .value = -6 });
+    if (has_i128) c_struct_i128(.{ .value = -6 });
     c_five_integers(12, 34, 56, 78, 90);
 }
 
@@ -178,6 +179,8 @@ test "C ABI complex float" {
 }
 
 test "C ABI complex float by component" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     const a = ComplexFloat{ .real = 1.25, .imag = 2.6 };
     const b = ComplexFloat{ .real = 11.3, .imag = -1.5 };
 
@@ -187,6 +190,8 @@ test "C ABI complex float by component" {
 }
 
 test "C ABI complex double" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
     const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
 
@@ -196,6 +201,8 @@ test "C ABI complex double" {
 }
 
 test "C ABI complex double by component" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     const a = ComplexDouble{ .real = 1.25, .imag = 2.6 };
     const b = ComplexDouble{ .real = 11.3, .imag = -1.5 };
 
@@ -304,6 +311,8 @@ extern fn c_med_struct_mixed(MedStructMixed) void;
 extern fn c_ret_med_struct_mixed() MedStructMixed;
 
 test "C ABI medium struct of ints and floats" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     var s = MedStructMixed{
         .a = 1234,
         .b = 100.0,
@@ -332,6 +341,8 @@ extern fn c_small_struct_ints(SmallStructInts) void;
 extern fn c_ret_small_struct_ints() SmallStructInts;
 
 test "C ABI small struct of ints" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     var s = SmallStructInts{
         .a = 1,
         .b = 2,
@@ -392,6 +403,8 @@ export fn zig_big_packed_struct(x: BigPackedStruct) void {
 }
 
 test "C ABI big packed struct" {
+    if (!has_i128) return error.SkipZigTest;
+
     var s = BigPackedStruct{ .a = 1, .b = 2 };
     c_big_packed_struct(s);
     var s2 = c_ret_big_packed_struct();
@@ -407,6 +420,8 @@ const SplitStructInt = extern struct {
 extern fn c_split_struct_ints(SplitStructInt) void;
 
 test "C ABI split struct of ints" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     var s = SplitStructInt{
         .a = 1234,
         .b = 100,
@@ -430,6 +445,8 @@ extern fn c_split_struct_mixed(SplitStructMixed) void;
 extern fn c_ret_split_struct_mixed() SplitStructMixed;
 
 test "C ABI split struct of ints and floats" {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     var s = SplitStructMixed{
         .a = 1234,
         .b = 100,
@@ -675,6 +692,8 @@ extern fn c_struct_with_array(StructWithArray) void;
 extern fn c_ret_struct_with_array() StructWithArray;
 
 test "Struct with array as padding." {
+    if (builtin.cpu.arch == .i386) return error.SkipZigTest;
+
     c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 });
 
     var x = c_ret_struct_with_array();