Created
August 22, 2020 20:36
-
-
Save jedisct1/426511511dbe2bca01d1eecd02a88e42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
fn eq(comptime T: type, comptime len: usize, a: []const T, b: []const T) bool { | |
if (len != a.len or len != b.len) return false; | |
if (a.ptr == b.ptr) return true; | |
const x16code = comptime blk: { | |
var i: usize = 0; | |
var buf = [_]u8{0} ** 4096; | |
var code = std.io.fixedBufferStream(&buf); | |
while (i + 16 <= len) : (i += 16) { | |
std.fmt.format(code.outStream(), | |
\\ vmovups {}(%[a]), %[s]; | |
\\ vmovups {}(%[b]), %[t]; | |
\\ vpxor %[s], %[t], %[t]; | |
\\ vpor %[t], %[u], %[u]; | |
, .{ i, i }) catch unreachable; | |
} | |
break :blk code.buffer; | |
}; | |
var r: u128 = 0; | |
var s: u128 = 0; | |
var t: u128 = 0; | |
var u: u128 = 0; | |
comptime const code = | |
\\ vpxor %[u], %[u], %[u]; | |
++ x16code ++ | |
\\ vpxor %[s], %[s], %[s]; | |
\\ vpcmpeqd %[s], %[u], %[s]; | |
\\ vpmovmskb %[s], %[ret]; | |
; | |
const ret = asm volatile (code | |
: [ret] "=r" (-> u64) | |
: [a] "r" (&a), | |
[b] "r" (&b), | |
[s] "x" (&s), | |
[t] "x" (&t), | |
[u] "x" (&u) | |
); | |
std.debug.print("{X}\n", .{ret}); | |
return true; | |
} | |
pub fn main() anyerror!void { | |
var a = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; | |
var b = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; | |
_ = eq(u8, 16, &a, &b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment