1 // https://doc.rust-lang.org/std/primitive.u16.html#method.leading_zeros
2
3 #[cfg(not(any(all(
4 target_arch = "spirv",
5 not(all(
6 target_feature = "IntegerFunctions2INTEL",
7 target_feature = "SPV_INTEL_shader_integer_functions2"
8 ))
9 ))))]
leading_zeros_u16(x: u16) -> u3210 pub(crate) const fn leading_zeros_u16(x: u16) -> u32 {
11 x.leading_zeros()
12 }
13
14 #[cfg(all(
15 target_arch = "spirv",
16 not(all(
17 target_feature = "IntegerFunctions2INTEL",
18 target_feature = "SPV_INTEL_shader_integer_functions2"
19 ))
20 ))]
leading_zeros_u16(x: u16) -> u3221 pub(crate) const fn leading_zeros_u16(x: u16) -> u32 {
22 leading_zeros_u16_fallback(x)
23 }
24
25 #[cfg(any(
26 test,
27 all(
28 target_arch = "spirv",
29 not(all(
30 target_feature = "IntegerFunctions2INTEL",
31 target_feature = "SPV_INTEL_shader_integer_functions2"
32 ))
33 )
34 ))]
leading_zeros_u16_fallback(mut x: u16) -> u3235 const fn leading_zeros_u16_fallback(mut x: u16) -> u32 {
36 use crunchy::unroll;
37 let mut c = 0;
38 let msb = 1 << 15;
39 unroll! { for i in 0 .. 16 {
40 if x & msb == 0 {
41 c += 1;
42 } else {
43 return c;
44 }
45 #[allow(unused_assignments)]
46 if i < 15 {
47 x <<= 1;
48 }
49 }}
50 c
51 }
52
53 #[cfg(test)]
54 mod test {
55
56 #[test]
leading_zeros_u16_fallback()57 fn leading_zeros_u16_fallback() {
58 for x in [44, 97, 304, 1179, 23571] {
59 assert_eq!(super::leading_zeros_u16_fallback(x), x.leading_zeros());
60 }
61 }
62 }
63