| ... | ... | @@ -145,6 +145,101 @@ export fn zig_bool(x: bool) void { |
| 145 | 145 | expect(x) catch @panic("test failure: zig_bool"); |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | // TODO: Replace these with the correct types once we resolve |
| 149 | // https://github.com/ziglang/zig/issues/8465 |
| 150 | // |
| 151 | // For now, we have no way of referring to the _Complex C types from Zig, |
| 152 | // so our ABI is unavoidably broken on some platforms (such as i386) |
| 153 | const ComplexFloat = extern struct { |
| 154 | real: f32, |
| 155 | imag: f32, |
| 156 | }; |
| 157 | const ComplexDouble = extern struct { |
| 158 | real: f64, |
| 159 | imag: f64, |
| 160 | }; |
| 161 | |
| 162 | // Note: These two functions match the signature of __mulsc3 and __muldc3 in compiler-rt (and libgcc) |
| 163 | extern fn c_cmultf_comp(a_r: f32, a_i: f32, b_r: f32, b_i: f32) ComplexFloat; |
| 164 | extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble; |
| 165 | |
| 166 | extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat; |
| 167 | extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble; |
| 168 | |
| 169 | test "C ABI complex float" { |
| 170 | if (true) return error.SkipZigTest; // See https://github.com/ziglang/zig/issues/8465 |
| 171 | |
| 172 | const a = ComplexFloat{ .real = 1.25, .imag = 2.6 }; |
| 173 | const b = ComplexFloat{ .real = 11.3, .imag = -1.5 }; |
| 174 | |
| 175 | const z = c_cmultf(a, b); |
| 176 | expect(z.real == 1.5) catch @panic("test failure: zig_complex_float 1"); |
| 177 | expect(z.imag == 13.5) catch @panic("test failure: zig_complex_float 2"); |
| 178 | } |
| 179 | |
| 180 | test "C ABI complex float by component" { |
| 181 | const a = ComplexFloat{ .real = 1.25, .imag = 2.6 }; |
| 182 | const b = ComplexFloat{ .real = 11.3, .imag = -1.5 }; |
| 183 | |
| 184 | const z2 = c_cmultf_comp(a.real, a.imag, b.real, b.imag); |
| 185 | expect(z2.real == 1.5) catch @panic("test failure: zig_complex_float 3"); |
| 186 | expect(z2.imag == 13.5) catch @panic("test failure: zig_complex_float 4"); |
| 187 | } |
| 188 | |
| 189 | test "C ABI complex double" { |
| 190 | const a = ComplexDouble{ .real = 1.25, .imag = 2.6 }; |
| 191 | const b = ComplexDouble{ .real = 11.3, .imag = -1.5 }; |
| 192 | |
| 193 | const z = c_cmultd(a, b); |
| 194 | expect(z.real == 1.5) catch @panic("test failure: zig_complex_double 1"); |
| 195 | expect(z.imag == 13.5) catch @panic("test failure: zig_complex_double 2"); |
| 196 | } |
| 197 | |
| 198 | test "C ABI complex double by component" { |
| 199 | const a = ComplexDouble{ .real = 1.25, .imag = 2.6 }; |
| 200 | const b = ComplexDouble{ .real = 11.3, .imag = -1.5 }; |
| 201 | |
| 202 | const z = c_cmultd_comp(a.real, a.imag, b.real, b.imag); |
| 203 | expect(z.real == 1.5) catch @panic("test failure: zig_complex_double 3"); |
| 204 | expect(z.imag == 13.5) catch @panic("test failure: zig_complex_double 4"); |
| 205 | } |
| 206 | |
| 207 | export fn zig_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat { |
| 208 | expect(a.real == 1.25) catch @panic("test failure: zig_cmultf 1"); |
| 209 | expect(a.imag == 2.6) catch @panic("test failure: zig_cmultf 2"); |
| 210 | expect(b.real == 11.3) catch @panic("test failure: zig_cmultf 3"); |
| 211 | expect(b.imag == -1.5) catch @panic("test failure: zig_cmultf 4"); |
| 212 | |
| 213 | return .{ .real = 1.5, .imag = 13.5 }; |
| 214 | } |
| 215 | |
| 216 | export fn zig_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble { |
| 217 | expect(a.real == 1.25) catch @panic("test failure: zig_cmultd 1"); |
| 218 | expect(a.imag == 2.6) catch @panic("test failure: zig_cmultd 2"); |
| 219 | expect(b.real == 11.3) catch @panic("test failure: zig_cmultd 3"); |
| 220 | expect(b.imag == -1.5) catch @panic("test failure: zig_cmultd 4"); |
| 221 | |
| 222 | return .{ .real = 1.5, .imag = 13.5 }; |
| 223 | } |
| 224 | |
| 225 | export fn zig_cmultf_comp(a_r: f32, a_i: f32, b_r: f32, b_i: f32) ComplexFloat { |
| 226 | expect(a_r == 1.25) catch @panic("test failure: zig_cmultf_comp 1"); |
| 227 | expect(a_i == 2.6) catch @panic("test failure: zig_cmultf_comp 2"); |
| 228 | expect(b_r == 11.3) catch @panic("test failure: zig_cmultf_comp 3"); |
| 229 | expect(b_i == -1.5) catch @panic("test failure: zig_cmultf_comp 4"); |
| 230 | |
| 231 | return .{ .real = 1.5, .imag = 13.5 }; |
| 232 | } |
| 233 | |
| 234 | export fn zig_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble { |
| 235 | expect(a_r == 1.25) catch @panic("test failure: zig_cmultd_comp 1"); |
| 236 | expect(a_i == 2.6) catch @panic("test failure: zig_cmultd_comp 2"); |
| 237 | expect(b_r == 11.3) catch @panic("test failure: zig_cmultd_comp 3"); |
| 238 | expect(b_i == -1.5) catch @panic("test failure: zig_cmultd_comp 4"); |
| 239 | |
| 240 | return .{ .real = 1.5, .imag = 13.5 }; |
| 241 | } |
| 242 | |
| 148 | 243 | const BigStruct = extern struct { |
| 149 | 244 | a: u64, |
| 150 | 245 | b: u64, |