aboutsummaryrefslogtreecommitdiff
path: root/lib/compiler_rt/bcmp.zig
diff options
context:
space:
mode:
authorLuuk de Gram <luuk@degram.dev>2022-05-28 15:42:05 +0200
committerAndrew Kelley <andrew@ziglang.org>2022-10-15 07:02:38 -0700
commitf5edaa96dd7c820ffb7c5b8fc8da849a620d69b1 (patch)
tree92f3bbfab91e1f1d288bc66fc6b5af6d83a16a6b /lib/compiler_rt/bcmp.zig
parent8bb2e96ac3b61a8aa393f250144fb9e1195ca60a (diff)
downloadzig-f5edaa96dd7c820ffb7c5b8fc8da849a620d69b1.tar.gz
zig-f5edaa96dd7c820ffb7c5b8fc8da849a620d69b1.zip
compiler_rt: Move mem implementations from c.zig
This moves functions that LLVM generates calls to, to the compiler_rt implementation itself, rather than c.zig. This is a prerequisite for native backends to link with compiler-rt. This also allows native backends to generate calls to `memcpy` and the like.
Diffstat (limited to 'lib/compiler_rt/bcmp.zig')
-rw-r--r--lib/compiler_rt/bcmp.zig30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/compiler_rt/bcmp.zig b/lib/compiler_rt/bcmp.zig
new file mode 100644
index 0000000000..add6858ffe
--- /dev/null
+++ b/lib/compiler_rt/bcmp.zig
@@ -0,0 +1,30 @@
+const std = @import("std");
+const common = @import("./common.zig");
+
+comptime {
+ @export(bcmp, .{ .name = "bcmp", .linkage = common.linkage });
+}
+
+pub fn bcmp(vl: [*]allowzero const u8, vr: [*]allowzero const u8, n: usize) callconv(.C) c_int {
+ @setRuntimeSafety(false);
+
+ var index: usize = 0;
+ while (index != n) : (index += 1) {
+ if (vl[index] != vr[index]) {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+test "bcmp" {
+ const base_arr = &[_]u8{ 1, 1, 1 };
+ const arr1 = &[_]u8{ 1, 1, 1 };
+ const arr2 = &[_]u8{ 1, 0, 1 };
+ const arr3 = &[_]u8{ 1, 2, 1 };
+
+ try std.testing.expect(bcmp(base_arr[0..], arr1[0..], base_arr.len) == 0);
+ try std.testing.expect(bcmp(base_arr[0..], arr2[0..], base_arr.len) != 0);
+ try std.testing.expect(bcmp(base_arr[0..], arr3[0..], base_arr.len) != 0);
+}