sub_borrow

Function sub_borrow 

Source
fn sub_borrow(borrow: bool, lhs: u64, rhs: u64, out: &mut u64) -> bool
Examples found in repository?
crates/competitive/src/num/decimal/addsub.rs (line 105)
92fn sub_absolute_parts_gte(lhs: &Decimal, rhs: &mut Decimal) {
93    debug_assert!(matches!(lhs.cmp_absolute_parts(rhs), Ordering::Greater));
94
95    let mut borrow = false;
96
97    // decimal part
98    let rhs_decimal_len = rhs.decimal.len();
99    if lhs.decimal.len() > rhs_decimal_len {
100        for (l, r) in lhs.decimal[..rhs_decimal_len]
101            .iter()
102            .rev()
103            .zip(rhs.decimal.iter_mut().rev())
104        {
105            borrow = sub_borrow(borrow, *l, *r, r);
106        }
107        rhs.decimal
108            .extend_from_slice(&lhs.decimal[rhs_decimal_len..]);
109    } else {
110        for r in rhs.decimal[lhs.decimal.len()..].iter_mut().rev() {
111            borrow = sub_borrow(borrow, 0, *r, r);
112        }
113        for (l, r) in lhs
114            .decimal
115            .iter()
116            .rev()
117            .zip(rhs.decimal[..lhs.decimal.len()].iter_mut().rev())
118        {
119            borrow = sub_borrow(borrow, *l, *r, r);
120        }
121    }
122
123    // integer part
124    let rhs_integer_len = rhs.integer.len();
125    if lhs.integer.len() > rhs_integer_len {
126        for (l, r) in lhs.integer[..rhs_integer_len]
127            .iter()
128            .zip(rhs.integer.iter_mut())
129        {
130            borrow = sub_borrow(borrow, *l, *r, r);
131        }
132        rhs.integer
133            .extend_from_slice(&lhs.integer[rhs_integer_len..]);
134        if borrow {
135            for r in rhs.integer[rhs_integer_len..].iter_mut() {
136                borrow = sub_borrow(borrow, *r, 0, r);
137                if !borrow {
138                    break;
139                }
140            }
141        }
142    } else {
143        debug_assert_eq!(lhs.integer.len(), rhs_integer_len);
144        for (l, r) in lhs.integer.iter().zip(&mut rhs.integer) {
145            borrow = sub_borrow(borrow, *l, *r, r);
146        }
147    }
148
149    assert!(
150        !borrow,
151        "Cannot subtract lhs from rhs because lhs is smaller than rhs"
152    );
153
154    rhs.normalize();
155}