-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.zig
91 lines (76 loc) · 2.08 KB
/
test.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// zig build-lib test.zig -target wasm32-freestanding -mcpu=mvp+bulk_memory -O ReleaseSafe && wasm2wat -f libtest.a.o
// zig build-lib test.zig -target wasm32-freestanding -mcpu=mvp+bulk_memory -O Debug && wasm2wat -f libtest.a.o
// zig build-lib test.zig -target wasm32-freestanding -mcpu=mvp+bulk_memory -O Debug -fno-llvm -fno-lld -fno-libllvm -rdynamic && wasm2wat -f libtest.a
const std = @import("std");
export fn use(p: **void) void {
p.* = @constCast(@ptrCast(&set));
//p.* = @constCast(@ptrCast(&initA));
//p.* = @constCast(@ptrCast(&initB));
//p.* = @constCast(@ptrCast(&initC));
//p.* = @constCast(@ptrCast(&pass));
}
pub fn add(a: i32, b: i32) i32 {
@setRuntimeSafety(false);
return a + b;
}
pub fn midpoint(a: i32, b: i32) i32 {
@setRuntimeSafety(false);
return @divTrunc(a + b, 2);
}
pub fn power(a: i32, b: i32) i32 {
@setRuntimeSafety(false);
var total: i32 = 1;
for (0..@intCast(b)) |_| {
total *= a;
}
return total;
}
const Pair = struct {
a: i32,
b: i32,
};
pub fn flipped(p: Pair) Pair {
return .{ .a = p.b, .b = p.a };
}
pub fn flip(p: *Pair) void {
p.* = .{ .a = p.b, .b = p.a };
}
const n = 2;
pub fn pass(p: [n]i32) [n]i32 {
return p;
}
pub fn initA(c: i32) [1]i32 {
return .{c};
}
pub fn initB(c: i32) [1]i32 {
const tmp: [1]i32 = .{c};
return tmp;
}
pub fn initC(c: i32) [1]i32 {
const tmp = [1]i32{c};
return tmp;
}
pub fn set(p: *[1]i32, c: [1]i32) void {
p.* = c;
}
//pub fn main() void {
// var p = Pair{ .a = 1, .b = 2 };
// flip(&p);
// std.debug.print("{any}\n", .{p});
//}
//pub fn squareMatrixRepeatedly(m: *[2][2]i32, n: usize) void {
// @setRuntimeSafety(false);
// for (0..n) |_| {
// const squared = .{
// .{
// m[0][0] * m[0][0] + m[0][1] * m[1][0],
// m[0][0] * m[0][1] + m[0][1] * m[1][1],
// },
// .{
// m[1][0] * m[0][0] + m[1][1] * m[1][0],
// m[1][0] * m[0][1] + m[1][1] * m[1][1],
// },
// };
// m.* = squared;
// }
//}