Created
February 22, 2023 23:00
-
-
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
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
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