From cf89d6367bc68de1b01ae0e364580b8b44e84d4d Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Fri, 1 Mar 2024 15:48:43 +0000 Subject: [PATCH] Export a C library (#4) --- README.md | 6 ++++-- build.zig | 18 ++++++++++++++++++ src/lib.zig | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/lib.zig diff --git a/README.md b/README.md index 7762ba4..4d8452a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.zig b/build.zig index 2f1ffcb..3565f0a 100644 --- a/build.zig +++ b/build.zig @@ -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" }, diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000..37631c8 --- /dev/null +++ b/src/lib.zig @@ -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)), + }); + } + } +}