Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Created August 22, 2020 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedisct1/426511511dbe2bca01d1eecd02a88e42 to your computer and use it in GitHub Desktop.
Save jedisct1/426511511dbe2bca01d1eecd02a88e42 to your computer and use it in GitHub Desktop.
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