xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/rusticl/util/math.rs (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 use std::ops::Rem;
2 
gcd<T>(mut a: T, mut b: T) -> T where T: Copy + Default + PartialEq, T: Rem<Output = T>,3 pub fn gcd<T>(mut a: T, mut b: T) -> T
4 where
5     T: Copy + Default + PartialEq,
6     T: Rem<Output = T>,
7 {
8     let mut c = a % b;
9     while c != T::default() {
10         a = b;
11         b = c;
12         c = a % b;
13     }
14 
15     b
16 }
17 
18 pub struct SetBitIndices<T> {
19     val: T,
20 }
21 
22 impl<T> SetBitIndices<T> {
from_msb(val: T) -> Self23     pub fn from_msb(val: T) -> Self {
24         Self { val: val }
25     }
26 }
27 
28 impl Iterator for SetBitIndices<u32> {
29     type Item = u32;
30 
next(&mut self) -> Option<Self::Item>31     fn next(&mut self) -> Option<Self::Item> {
32         if self.val == 0 {
33             None
34         } else {
35             let pos = u32::BITS - self.val.leading_zeros() - 1;
36             self.val ^= 1 << pos;
37             Some(pos)
38         }
39     }
40 }
41