What's the fastest way to divide an integer by 3?

后端 未结 12 2128
天涯浪人
天涯浪人 2020-11-29 03:40
int x = n / 3;  // <-- make this faster

// for instance

int a = n * 3; // <-- normal integer multiplication

int b = (n << 1) + n; // <-- potentiall         


        
12条回答
  •  再見小時候
    2020-11-29 03:54

    A lookup table approach would also be faster in some architectures.

    uint8_t DivBy3LU(uint8_t u8Operand)
    {
       uint8_t ai8Div3 = [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, ....];
    
       return ai8Div3[u8Operand];
    }
    

提交回复
热议问题