Square root and Visual C++ compiler
Suppose that:
1) you create a new C++ (Empty C++) in Visual Studio 2008 Express.
2) you have a Core 2 Duo 6400@2.13GHz like mine or something
3) we write, compile and execute this code:
Code:
# include <math.h>
int main (void)
{
sqrt (71634.4174f);
return 0;
}
The implementation of sqrt (71634.4174f); going to map a (or a set of) instruction (s) Math (s) established (s) in the processor, or are we going to use an implementation of calculating the square root of this issue found in the library by pointing math.h. And if so, are there ways to ensure that the program should make use of mathematical instructions located in the CPU, without diving into the writing of microcode assembler?
For info, I found this, about the Core 2 Duos:
Quote:
Sqrt is an intrinsic on the SSE-platform, and thus reduces to a single instruction. The implementation for ppu/spu is based on the inverse-square-root estimate intrinsic of these platforms. This instruction is combined with one iteration of the Newton-Raphson algorithm to provide the final result.
I wonder in passing (and admiration) that their implementation is able to obtain a sufficiently accurate with a single iteration of the Newton algo. Their basic approximation must be devilishly good. Reminds me of 0x5f3759df
I ask this because I have tested the execution time of 10 million once a sqrt () and 10 million times a root obtained by the Newton algo precisely, and I was surprised to get a time 2/3 or 3/4 lower with a code made by hand.
So, what is VC++ compiler will do with a code like that?
Re: Square root and Visual C++ compiler
sqrt drew implementation lib by sellers who will be optimized or not. Under VC, it should be used after implementation IEE SUN MATH 754 or approaching.
1 from Newton-Raphson enough time or your proxy is to be near 10-4 lead to 10-8. It's the same trick in Altivec. And in fact, we wrote more assembler to pr ESS kind since 10 years, should see to keep up
Re: Square root and Visual C++ compiler
Under visual there is a pragma to enable intrinsic, including sqrt. You can also use an optional compiler, I know that. It depends on your compiler and its configuration, not your processor.
If in doubt, compile in release + debug info (possible) and look at the assembly.
Re: Square root and Visual C++ compiler
OK, I try to use #pragma intrinsic (sqrt) but strangely it is not recognized by the compiler:
Quote:
1>.\sq.cpp(1) : warning C4164: 'sqrt' : intrinsic function not declared
Re: Square root and Visual C++ compiler
Quote:
Originally Posted by
kelfro
And in fact, we wrote more assembler to pr ESS kind since 10 years, should see to keep up
I do not know or its seen that, but there are people who write the assembler SSE every day and working for large hi-tech companies.
Of course, not necessarily for general public applications, but it exists. VC in the compilation of SSE intrinsics does all the time not the same as the inline assembler.