Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. //Fast float multiplication taking advantage of the fact that most multiplications will
  2. //result in overflow.
  3. Fixfloat_t Fixfloat_t::operator*(Fixfloat_t rhs){
  4.     Fixfloat_t lhs(*this);
  5.     uint8_t expected_exponent = lhs.exponent + rhs.exponent;
  6.    
  7.     //Multiplication will result in overflow
  8.     if(expected_exponent & SIGN_BIT_CLEAR_MASK > INT_SIZE){
  9.        
  10.         //Both numbers longer than 8 bit
  11.         //Lower resolution until both decimals have length 8
  12.         bool lhs_overflow = lhs.exponent > 8;
  13.         bool rhs_overflow = rhs.exponent > 8;
  14.         if(lhs_overflow && rhs_overflow){
  15.            
  16.             lhs.decimals >> lhs.exponent - 8;
  17.             rhs.decimals >> rhs.exponent - 8;
  18.         }
  19.        
  20.         //Only one number causes overflow. Not a likely scenario as
  21.         //most numbers will utilize the full resolution
  22.         else{
  23.             //Only lhs causes overflow
  24.             if(lhs_overflow){
  25.                 lhs.decimals >> (8 - rhs.exponent);
  26.             }
  27.            
  28.             //Only rhs causes overflow
  29.             else if(rhs_overflow)
  30.                 rhs.decimals >> (8 - lhs.exponent);
  31.         }
  32.     }
  33.    
  34.     lhs.decimals *= rhs.decimals;
  35.     lhs.exponent = expected_exponent;
  36.    
  37.     return lhs;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement