sample-code: Expand the capabilities of the sample code

This commit is contained in:
2024-07-11 04:02:44 -05:00
parent a646a9e521
commit fffc370380
2 changed files with 65 additions and 10 deletions

36
sample-code/math.cl Normal file
View File

@@ -0,0 +1,36 @@
//! Useful math functions
pub fn max(a: T, b: T) -> T {
(if a < b { b } else { a })
}
pub fn min(a: T, b: T) -> T {
(if a > b { b } else { a })
}
pub fn count_leading_zeroes(n: u64) -> u64 {
let mut xd = 64;
if n < 0 {
return 0;
}
while n != 0 {
xd -= 1;
n >>= 1;
}
xd
}
pub fn count_trailing_zeroes(n: u64) -> u64 {
let mut xd = 0;
if n == 0 {
64
} else if n < 0 {
0
} else {
while n & 1 == 0 {
xd += 1;
n >>= 1;
}
xd
}
}