Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Fast float multiplication taking advantage of the fact that most multiplications will
- //result in overflow.
- Fixfloat_t Fixfloat_t::operator*(Fixfloat_t rhs){
- Fixfloat_t lhs(*this);
- uint8_t expected_exponent = lhs.exponent + rhs.exponent;
- //Multiplication will result in overflow
- if(expected_exponent & SIGN_BIT_CLEAR_MASK > INT_SIZE){
- //Both numbers longer than 8 bit
- //Lower resolution until both decimals have length 8
- bool lhs_overflow = lhs.exponent > 8;
- bool rhs_overflow = rhs.exponent > 8;
- if(lhs_overflow && rhs_overflow){
- lhs.decimals >> lhs.exponent - 8;
- rhs.decimals >> rhs.exponent - 8;
- }
- //Only one number causes overflow. Not a likely scenario as
- //most numbers will utilize the full resolution
- else{
- //Only lhs causes overflow
- if(lhs_overflow){
- lhs.decimals >> (8 - rhs.exponent);
- }
- //Only rhs causes overflow
- else if(rhs_overflow)
- rhs.decimals >> (8 - lhs.exponent);
- }
- }
- lhs.decimals *= rhs.decimals;
- lhs.exponent = expected_exponent;
- return lhs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement