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))))]
10#[inline]
11pub(crate) const fn leading_zeros_u16(x: u16) -> u32 {
12 x.leading_zeros()
13}
14
15#[cfg(all(
16 target_arch = "spirv",
17 not(all(
18 target_feature = "IntegerFunctions2INTEL",
19 target_feature = "SPV_INTEL_shader_integer_functions2"
20 ))
21))]
22#[inline]
23pub(crate) const fn leading_zeros_u16(x: u16) -> u32 {
24 leading_zeros_u16_fallback(x)
25}
26
27#[cfg(any(
28 test,
29 all(
30 target_arch = "spirv",
31 not(all(
32 target_feature = "IntegerFunctions2INTEL",
33 target_feature = "SPV_INTEL_shader_integer_functions2"
34 ))
35 )
36))]
37#[inline]
38const fn leading_zeros_u16_fallback(mut x: u16) -> u32 {
39 use crunchy::unroll;
40 let mut c = 0;
41 let msb = 1 << 15;
42 unroll! { for i in 0 .. 16 {
43 if x & msb == 0 {
44 c += 1;
45 } else {
46 return c;
47 }
48 #[allow(unused_assignments)]
49 if i < 15 {
50 x <<= 1;
51 }
52 }}
53 c
54}
55
56#[cfg(test)]
57mod test {
58
59 #[test]
60 fn leading_zeros_u16_fallback() {
61 for x in [44, 97, 304, 1179, 23571] {
62 assert_eq!(super::leading_zeros_u16_fallback(x), x.leading_zeros());
63 }
64 }
65}
66