本文主要是介绍C++ Div3、Sqrt 函数高性能实现(带汇编指令集),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
均采用魔法数字(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;
}
这篇关于C++ Div3、Sqrt 函数高性能实现(带汇编指令集)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!