均采用魔法数字(Magic Number)实现,一个是经典求平方根函数所使用的魔法数字:0x5f375a86、0x5f3759df。
float Sqrt(float x) noexcept { /* 0x5f3759df */float xhalf = 0.5f * x;int32_t i = *(int32_t*)&x;i = 0x5f375a86 - (i >> 1);x = *(float*)&i;x = x * (1.5f - xhalf * x * x);return x;
}unsigned int Div3(unsigned int i) noexcept {// AT&T:// movl $2863311531, %edx// imulq %rcx, %rdx// shrq $33, %rdx// INTEL:// mov edx, 2863311531// imul rdx, rcx// shr rdx, 33unsigned long long n = static_cast<long long>(i) * 2863311531;unsigned int r = static_cast<unsigned int>(n >> 33);return r;
}unsigned long long Div3(unsigned long long i) noexcept {// INTEL:// mov rax, -6148914691236517205; // mul rcx// shr rdx, 1int64_t rax = -6148914691236517205;int64_t rdx = (rax * i) >> 1;return rdx;
}