Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Export a C library (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Mar 1, 2024
1 parent 65d47c6 commit cf89d63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ of this library.
Another possible advantage to the FastLanes loop ordering is that we can avoid unrolling the outer SIMD word loop,
resulting in potentially much smaller code size for minimal impact on performance.


## C Library

TODO: this library will be made available as a C library.
Running `zig build lib` will generate a static C library in `zig-out/lib` and a header file in `zig-out/include`.
The header file requires `zig.h` which is located in the `lib_dir` output by running `zig env`.

This process should improve as https://github.com/ziglang/zig/issues/13528 is resolved.

## Python Library

Expand Down
18 changes: 18 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ pub fn build(b: *std.Build) void {
},
});

// Static Library
const lib = b.addStaticLibrary(.{
.name = "fastlanez",
.target = target,
.optimize = optimize,
.root_source_file = .{ .path = "src/lib.zig" },
});
const lib_install = b.addInstallArtifact(lib, .{});

// Ideally we would use dlib.getEmittedH(), but https://github.com/ziglang/zig/issues/18497
_ = lib.getEmittedH(); // Needed to trigger header generation
const lib_header = b.addInstallFile(.{ .path = "zig-cache/fastlanez.h" }, "include/fastlanez.h");
lib_header.step.dependOn(&lib_install.step);

const lib_step = b.step("lib", "Build static C library");
lib_step.dependOn(&lib_header.step);
lib_step.dependOn(&lib_install.step);

// Unit Tests
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/fastlanez.zig" },
Expand Down
23 changes: 23 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! The C library for FastLanez.
const fl = @import("./fastlanez.zig");
const std = @import("std");

// BitPacking
comptime {
const BitPacking = @import("./bitpacking.zig").BitPacking;
for (.{ u8, u16, u32, u64 }) |E| {
const FL = fl.FastLanez(E);

for (1..FL.T) |W| {
const Wrapper = struct {
fn encode(in: *const FL.Vector, out: *FL.PackedBytes(W)) callconv(.C) void {
@call(.always_inline, BitPacking(FL).encode, .{ W, in, out });
}
};

@export(Wrapper.encode, .{
.name = "fl_bitpack_" ++ @typeName(E) ++ "_" ++ @typeName(std.meta.Int(.unsigned, W)),
});
}
}
}

0 comments on commit cf89d63

Please sign in to comment.