Skip to content

Commit

Permalink
add memmove to builtin.o
Browse files Browse the repository at this point in the history
related: #514
  • Loading branch information
andrewrk committed Apr 11, 2018
1 parent 27e881c commit f6c7774
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions std/special/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,43 @@ pub fn panic(msg: []const u8, error_return_trace: ?&builtin.StackTrace) noreturn
}
}

// Note that memset does not return `dest`, like the libc API.
// The semantics of memset is dictated by the corresponding
// LLVM intrinsics, not by the libc API.
export fn memset(dest: ?&u8, c: u8, n: usize) void {
export fn memset(dest: ?&u8, c: u8, n: usize) ?&u8 {
@setRuntimeSafety(false);

var index: usize = 0;
while (index != n) : (index += 1)
(??dest)[index] = c;

return dest;
}

// Note that memcpy does not return `dest`, like the libc API.
// The semantics of memcpy is dictated by the corresponding
// LLVM intrinsics, not by the libc API.
export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) void {
export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) ?&u8 {
@setRuntimeSafety(false);

var index: usize = 0;
while (index != n) : (index += 1)
(??dest)[index] = (??src)[index];

return dest;
}

export fn memmove(dest: ?&u8, src: ?&const u8, n: usize) ?&u8 {
@setRuntimeSafety(false);

if (@ptrToInt(dest) < @ptrToInt(src)) {
var index: usize = 0;
while (index != n) : (index += 1) {
(??dest)[index] = (??src)[index];
}
} else {
var index = n;
while (index != 0) {
index -= 1;
(??dest)[index] = (??src)[index];
}
}

return dest;
}

comptime {
Expand Down

0 comments on commit f6c7774

Please sign in to comment.