From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/half/src/leading_zeros.rs | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 vendor/half/src/leading_zeros.rs (limited to 'vendor/half/src/leading_zeros.rs') diff --git a/vendor/half/src/leading_zeros.rs b/vendor/half/src/leading_zeros.rs new file mode 100644 index 0000000..6c73148 --- /dev/null +++ b/vendor/half/src/leading_zeros.rs @@ -0,0 +1,62 @@ +// https://doc.rust-lang.org/std/primitive.u16.html#method.leading_zeros + +#[cfg(not(any(all( + target_arch = "spirv", + not(all( + target_feature = "IntegerFunctions2INTEL", + target_feature = "SPV_INTEL_shader_integer_functions2" + )) +))))] +pub(crate) const fn leading_zeros_u16(x: u16) -> u32 { + x.leading_zeros() +} + +#[cfg(all( + target_arch = "spirv", + not(all( + target_feature = "IntegerFunctions2INTEL", + target_feature = "SPV_INTEL_shader_integer_functions2" + )) +))] +pub(crate) const fn leading_zeros_u16(x: u16) -> u32 { + leading_zeros_u16_fallback(x) +} + +#[cfg(any( + test, + all( + target_arch = "spirv", + not(all( + target_feature = "IntegerFunctions2INTEL", + target_feature = "SPV_INTEL_shader_integer_functions2" + )) + ) +))] +const fn leading_zeros_u16_fallback(mut x: u16) -> u32 { + use crunchy::unroll; + let mut c = 0; + let msb = 1 << 15; + unroll! { for i in 0 .. 16 { + if x & msb == 0 { + c += 1; + } else { + return c; + } + #[allow(unused_assignments)] + if i < 15 { + x <<= 1; + } + }} + c +} + +#[cfg(test)] +mod test { + + #[test] + fn leading_zeros_u16_fallback() { + for x in [44, 97, 304, 1179, 23571] { + assert_eq!(super::leading_zeros_u16_fallback(x), x.leading_zeros()); + } + } +} -- cgit v1.2.3