diff options
| author | Luuk de Gram <luuk@degram.dev> | 2022-05-28 15:42:05 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-10-15 07:02:38 -0700 |
| commit | f5edaa96dd7c820ffb7c5b8fc8da849a620d69b1 (patch) | |
| tree | 92f3bbfab91e1f1d288bc66fc6b5af6d83a16a6b /lib/compiler_rt/memcmp.zig | |
| parent | 8bb2e96ac3b61a8aa393f250144fb9e1195ca60a (diff) | |
| download | zig-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/memcmp.zig')
| -rw-r--r-- | lib/compiler_rt/memcmp.zig | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/compiler_rt/memcmp.zig b/lib/compiler_rt/memcmp.zig new file mode 100644 index 0000000000..b0a925438f --- /dev/null +++ b/lib/compiler_rt/memcmp.zig @@ -0,0 +1,31 @@ +const std = @import("std"); +const common = @import("./common.zig"); + +comptime { + @export(memcmp, .{ .name = "memcmp", .linkage = common.linkage }); +} + +pub fn memcmp(vl: ?[*]const u8, vr: ?[*]const u8, n: usize) callconv(.C) c_int { + @setRuntimeSafety(false); + + var index: usize = 0; + while (index != n) : (index += 1) { + const compare_val = @bitCast(i8, vl.?[index] -% vr.?[index]); + if (compare_val != 0) { + return compare_val; + } + } + + return 0; +} + +test "memcmp" { + 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(memcmp(base_arr[0..], arr1[0..], base_arr.len) == 0); + try std.testing.expect(memcmp(base_arr[0..], arr2[0..], base_arr.len) > 0); + try std.testing.expect(memcmp(base_arr[0..], arr3[0..], base_arr.len) < 0); +} |
