Skip to content

Instantly share code, notes, and snippets.

@Validark
Created February 22, 2023 23:00
Show Gist options
  • Save Validark/c82aabd3b3efb13cae7d631e6b69864e to your computer and use it in GitHub Desktop.
Save Validark/c82aabd3b3efb13cae7d631e6b69864e to your computer and use it in GitHub Desktop.
pdep in Zig with select implementation that find the nth set bit of an integer
fn pdep(src: u64, mask: u64) u64 {
return asm ("pdep %[mask], %[src], %[ret]"
: [ret] "=r" (-> u64),
: [src] "r" (src),
[mask] "r" (mask),
);
}
fn select(x: u64, k: u6) u6 {
@setRuntimeSafety(false);
return @intCast(u6, @ctz(pdep(@as(u64, 1) << k, x)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment