interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / examples / deepinfer / callers / caller.zig

54 lines · 2860 bytes Raw Blame History
1// Zig 0.13 caller for examples/deepinfer/flops_pairs.om. Every Omega declaration comes from the generated
2// omega_native.zig; nothing is typed by hand here. Expected values: evidence/deepinfer-flops-pairs/wolfram.md.
3const std = @import("std");
4const om = @import("omega_native.zig");
5
6var fails: u32 = 0;
7fn check(name: []const u8, ok: bool) void {
8 std.debug.print("{s} {s}\n", .{ if (ok) "PASS" else "FAIL", name });
9 if (!ok) fails += 1;
10}
11
12const Fn = enum { floor_prefix, capped_prefix, pairs };
13const V = struct { f: Fn, a: [4]i64, status: u32, want: i64 };
14
15fn call(x: V, r: ?*u64) u32 {
16 var u: [4]u64 = undefined;
17 for (x.a, 0..) |v, i| u[i] = @bitCast(v);
18 return switch (x.f) {
19 .floor_prefix => om.omega2_floor_prefix(r, u[0], u[1]),
20 .capped_prefix => om.omega2_capped_prefix(r, u[0], u[1], u[2]),
21 .pairs => om.omega2_pairs(r, u[0], u[1], u[2], u[3]),
22 };
23}
24
25pub fn main() void {
26 const vs = [_]V{
27 .{ .f = .floor_prefix, .a = .{ 9, 2, 0, 0 }, .status = 0, .want = 20 },
28 .{ .f = .capped_prefix, .a = .{ 9, 2, 3, 0 }, .status = 0, .want = 18 },
29 .{ .f = .pairs, .a = .{ 3, 9, 2, 3 }, .status = 0, .want = 16 },
30 .{ .f = .pairs, .a = .{ 3, 9, 2, 0 }, .status = 0, .want = 18 },
31 .{ .f = .pairs, .a = .{ 0, 4096, 1, 512 }, .status = 0, .want = 1966336 },
32 .{ .f = .floor_prefix, .a = .{ 3037000499, 1, 0, 0 }, .status = 0, .want = 4611686016981624750 },
33 .{ .f = .capped_prefix, .a = .{ 4611686018427387904, 1, 2, 0 }, .status = 0, .want = 9223372036854775807 },
34 .{ .f = .floor_prefix, .a = .{ 3037000500, 1, 0, 0 }, .status = 1, .want = 0 },
35 .{ .f = .capped_prefix, .a = .{ 4611686018427387905, 1, 2, 0 }, .status = 1, .want = 0 },
36 .{ .f = .capped_prefix, .a = .{ 0, 4294967296, 2147483648, 0 }, .status = 1, .want = 0 },
37 .{ .f = .floor_prefix, .a = .{ -1, 1, 0, 0 }, .status = 9, .want = 0 },
38 .{ .f = .floor_prefix, .a = .{ 5, 0, 0, 0 }, .status = 9, .want = 0 },
39 .{ .f = .pairs, .a = .{ 5, 3, 1, 0 }, .status = 9, .want = 0 },
40 .{ .f = .pairs, .a = .{ 0, 3, 1, -1 }, .status = 9, .want = 0 },
41 };
42 const sentinel: u64 = 0x5a5a5a5a5a5a5a5a;
43 check("ident: library == binding", std.mem.eql(u8, std.mem.span(om.omega2_ident_flops_pairs()), om.OMEGA2_IDENT_flops_pairs));
44 var buf: [160]u8 = undefined;
45 for (vs) |x| {
46 var r: u64 = sentinel;
47 const st = call(x, &r);
48 const ok = st == x.status and (if (st == om.OMEGA_OK) @as(i64, @bitCast(r)) == x.want else r == sentinel);
49 const name = std.fmt.bufPrint(&buf, "{s} {any} -> status {d}", .{ @tagName(x.f), x.a, st }) catch "vector";
50 check(name, ok);
51 }
52 check("null result refused 32", om.omega2_pairs(null, 0, 3, 1, 0) == om.OMEGA_BAD_ARG);
53 std.process.exit(if (fails == 0) 0 else 1);
54}