-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from orca-app/rjd/bytebox-integration
Bytebox wasm backend
- Loading branch information
Showing
36 changed files
with
20,755 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Reuben Dunnington | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const std = @import("std"); | ||
const bytebox = @import("bytebox"); | ||
const Val = bytebox.Val; | ||
const Timer = std.time.Timer; | ||
|
||
const Benchmark = struct { | ||
name: []const u8, | ||
filename: []const u8, | ||
param: i32, | ||
}; | ||
|
||
fn elapsedMilliseconds(timer: *std.time.Timer) f64 { | ||
var ns_elapsed: f64 = @as(f64, @floatFromInt(timer.read())); | ||
const ms_elapsed = ns_elapsed / 1000000.0; | ||
return ms_elapsed; | ||
} | ||
|
||
fn run(allocator: std.mem.Allocator, benchmark: Benchmark) !void { | ||
var cwd = std.fs.cwd(); | ||
var wasm_data: []u8 = try cwd.readFileAlloc(allocator, benchmark.filename, 1024 * 64); // Our wasm programs aren't very large | ||
|
||
var timer = try Timer.start(); | ||
|
||
var module_def = try bytebox.createModuleDefinition(allocator, .{}); | ||
defer module_def.destroy(); | ||
try module_def.decode(wasm_data); | ||
|
||
var module_instance = try bytebox.createModuleInstance(.Stack, module_def, allocator); | ||
defer module_instance.destroy(); | ||
try module_instance.instantiate(.{}); | ||
|
||
const handle = try module_instance.getFunctionHandle("run"); | ||
var input = [1]Val{.{ .I32 = benchmark.param }}; | ||
var output = [1]Val{.{ .I32 = 0 }}; | ||
try module_instance.invoke(handle, &input, &output, .{}); | ||
|
||
const ms_elapsed: f64 = elapsedMilliseconds(&timer); | ||
std.log.info("{s} decode+instantiate+run took {d}ms\n", .{ benchmark.name, ms_elapsed }); | ||
} | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
var allocator: std.mem.Allocator = gpa.allocator(); | ||
|
||
const benchmarks = [_]Benchmark{ .{ | ||
.name = "add-one", | ||
.filename = "zig-out/lib/add-one.wasm", | ||
.param = 123456789, | ||
}, .{ | ||
.name = "fibonacci", | ||
.filename = "zig-out/lib/fibonacci.wasm", | ||
.param = 20, | ||
}, .{ | ||
.name = "mandelbrot", | ||
.filename = "zig-out/lib/mandelbrot.wasm", | ||
.param = 20, | ||
} }; | ||
|
||
for (benchmarks) |benchmark| { | ||
run(allocator, benchmark) catch |e| { | ||
std.log.err("{s} 'run' invocation failed with error: {}\n", .{ benchmark.name, e }); | ||
return e; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export fn run(n: i32) i32 { | ||
return n + 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export fn run(n: i32) i32 { | ||
if (n < 2) { | ||
return 1; | ||
} else { | ||
var a = run(n - 1); | ||
var b = run(n - 2); | ||
return a + b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const std = @import("std"); | ||
const complex = std.math.complex; | ||
const Complex = complex.Complex(f32); | ||
|
||
const Color = struct { | ||
R: u8, | ||
G: u8, | ||
B: u8, | ||
}; | ||
|
||
const COLOR_BLACK = Color{ .R = 0, .G = 0, .B = 0 }; | ||
const COLOR_WHITE = Color{ .R = 255, .G = 255, .B = 255 }; | ||
|
||
const WIDTH = 256; | ||
const HEIGHT = 256; | ||
|
||
var pixels = [_]Color{.{ .R = 0, .G = 0, .B = 0 }} ** (WIDTH * HEIGHT); | ||
|
||
fn mandelbrot(c: Complex, max_counter: i32) Color { | ||
var counter: u32 = 0; | ||
var z = Complex.init(0, 0); | ||
while (counter < max_counter) : (counter += 1) { | ||
z = z.mul(z).add(c); | ||
if (2.0 <= complex.abs(z)) { | ||
return COLOR_WHITE; | ||
} | ||
} | ||
|
||
return COLOR_BLACK; | ||
} | ||
|
||
export fn run(max_counter: i32) i32 { | ||
var y: u32 = 0; | ||
while (y < HEIGHT) : (y += 1) { | ||
var x: u32 = 0; | ||
while (x < WIDTH) : (x += 1) { | ||
const c = Complex.init(@as(f32, @floatFromInt(x)), @as(f32, @floatFromInt(y))); | ||
const color: Color = mandelbrot(c, max_counter); | ||
pixels[y * HEIGHT + x] = color; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.