Line data Source code
1 : //! Utility for checking binary arithmetic flags for ALU operations. 2 : 3 : /// Returns if the sum of an addition operation between two integers generated 4 : /// an arithmetic carry. 5 : /// 6 : /// Since operation is carried out between two u32 integers, if the results 7 : /// is greater than 0xFFFF_FFFF, then bit 31 was carried into bit 32, indicating 8 : /// a carry. 9 0 : pub fn did_add_generate_carry(sum: u64) -> bool { 10 0 : sum > 0xFFFF_FFFF 11 0 : } 12 : 13 : /// Returns if the difference of an subtraction operation between two 14 : /// integers generated an arithmetic borrow. 15 : /// 16 : /// Since the operation is carried out between two u32 integers, if the 17 : /// difference is less than 0x1_0000_0000, then bit 32 wasn't borrowed. 18 0 : pub fn did_sub_generate_borrow(difference: u64) -> bool { 19 0 : difference < 0x1_0000_0000 20 0 : } 21 : 22 : /// Returns if two addends and their sum generated an arithmetic overflow. 23 : /// 24 : /// An addition overflow is identified if the sign of both addends are the same 25 : /// and the sign of the sum is different from both addends. 26 0 : pub fn did_add_generate_overflow(addend_a: u32, addend_b: u32, sum: u32) -> bool { 27 0 : !(addend_a ^ addend_b) & (addend_a ^ sum) & 0x8000_0000 != 0 28 0 : } 29 : 30 : /// Returns if the subtraction operation between the minuend and subtrahend 31 : /// integers and their difference generated an arithmetic overflow. 32 : /// 33 : /// An subtraction overflow is identified if the sign of the minuend and 34 : /// subtrahend are different and the difference has the same sign as the 35 : /// subtrahend. 36 0 : pub fn did_sub_generate_overflow(minuend: u32, subtrahend: u32, difference: u32) -> bool { 37 0 : (minuend ^ subtrahend) & (minuend ^ difference) & 0x8000_0000 > 0 38 0 : }