Are the old lookup table, approx functions tricks for creating faster sqrt () implementations still useful or are the default implementations as fast as they are for modern compilers and hardware?
Before you put any effort into the belief that you can defeat the optimizer, you should comment on everything and find out where the bottleneck actually lies. In general, it is unlikely that sqrt()this is your bottleneck.
sqrt()
sqrt() , - , ( , - ), sqrt() .
C CRT , , sqrt(), .
, MinGW gcc v3.4.5 sqrt() , FPU FSQRT. , C IEEE 754, FSQRT , sqrt() , .
FSQRT
sqrt() double, .
double
( ) , , .
, , . , (-b + sqrt(b*b - 4.*a*c)) / (2*a) ?
(-b + sqrt(b*b - 4.*a*c)) / (2*a)
, , , .
.
, , , , C , .
: , , . , .
Edit2: , kmm .
Sqrt . , , "".
() , , . ( ), - .
, , " ".
, sqrt() , , . .
, . .
, , -, ? ( , , ), - , ?
? , , .
, ? , CPU sqrt, , . , , CPU, , sqrt().
, ?
" sqrt".
" ".
, , :
float fastsqrt(float val) { union { int tmp; float val; } u; u.val = val; u.tmp -= 1<<23; /* Remove last bit so 1.0 gives 1.0 */ /* tmp is now an approximation to logbase2(val) */ u.tmp >>= 1; /* divide by 2 */ u.tmp += 1<<29; /* add 64 to exponent: (e+127)/2 =(e/2)+63, */ /* that represents (e/2)-64 but we want e/2 */ return u.val; }
, , . 0,00175228 .
float InvSqrt (float x) { float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; return x*(1.5f - xhalf*x*x); }
( ) 4 , (float)(1.0/sqrt(x))
(float)(1.0/sqrt(x))
? , , !
, sqrt - , . , - , , , (, L1 L2), .
- , .
, , , SIMD: rsqrtps. - , . rsqrtps , , ( , , ).
rsqrtps
sqrt, , , . , , . , C sqrt, .
sqrt
(, ), , , , , malloc. malloc , , . , , .
malloc
Source: https://habr.com/ru/post/1709324/More articles:Improve speed BufferedReader - javaProgrammatically remove etag suffix (change number) from IIS6 metabase - wmiКак я могу предоставить отзыв моей команде об изменениях, включенных в сборку, и их влиянии на риск? - version-controlOnclick prototype button - prototypejsUnicode issues in PyObjC - python.NET binary serialization between 32-bit and 64-bit OS - .netProblems getting Oracle TimeStamp fields in .Net - oracleNameError using execfile in python - scopeWhat is the right way to tell when NSArrayController finishes loading its content from persistent storage? - objective-cРазрешение SQL Server 2008 CONTROL SERVER - sql-serverAll Articles