Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><code>MethodImplOptions.InternalCall</code></p> </blockquote> <p>That means that the method is actually implemented in the CLR, written in C++. The just-in-time compiler consults a table with internally implemented methods and compiles the call to the C++ function directly.</p> <p>Having a look at the code requires the source code for the CLR. You can get that from the <a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=4917" rel="noreferrer">SSCLI20 distribution</a>. It was written around the .NET 2.0 time frame, I've found the low-level implementations, like <code>Math.Pow()</code> to be still largely accurate for later versions of the CLR.</p> <p>The lookup table is located in clr/src/vm/ecall.cpp. The section that's relevant to <code>Math.Pow()</code> looks like this:</p> <pre><code>FCFuncStart(gMathFuncs) FCIntrinsic("Sin", COMDouble::Sin, CORINFO_INTRINSIC_Sin) FCIntrinsic("Cos", COMDouble::Cos, CORINFO_INTRINSIC_Cos) FCIntrinsic("Sqrt", COMDouble::Sqrt, CORINFO_INTRINSIC_Sqrt) FCIntrinsic("Round", COMDouble::Round, CORINFO_INTRINSIC_Round) FCIntrinsicSig("Abs", &amp;gsig_SM_Flt_RetFlt, COMDouble::AbsFlt, CORINFO_INTRINSIC_Abs) FCIntrinsicSig("Abs", &amp;gsig_SM_Dbl_RetDbl, COMDouble::AbsDbl, CORINFO_INTRINSIC_Abs) FCFuncElement("Exp", COMDouble::Exp) FCFuncElement("Pow", COMDouble::Pow) // etc.. FCFuncEnd() </code></pre> <p>Searching for "COMDouble" takes you to clr/src/classlibnative/float/comfloat.cpp. I'll spare you the code, just have a look for yourself. It basically checks for corner cases, then calls the CRT's version of <code>pow()</code>.</p> <p>The only other implementation detail that's interesting is the FCIntrinsic macro in the table. That's a hint that the jitter may implement the function as an intrinsic. In other words, substitute the function call with a floating point machine code instruction. Which is not the case for <code>Pow()</code>, there is no FPU instruction for it. But certainly for the other simple operations. Notable is that this can make floating point math in C# substantially faster than the same code in C++, check <a href="https://stackoverflow.com/questions/686483/c-sharp-vs-c-big-performance-difference/687741#687741">this answer</a> for the reason why.</p> <p>By the way, the source code for the CRT is also available if you have the full version of Visual Studio vc/crt/src directory. You'll hit the wall on <code>pow()</code> though, Microsoft purchased that code from Intel. Doing a better job than the Intel engineers is unlikely. Although my high-school book's identity was twice as fast when I tried it:</p> <pre><code>public static double FasterPow(double x, double y) { return Math.Exp(y * Math.Log(x)); } </code></pre> <p>But not a true substitute because it accumulates error from 3 floating point operations and doesn't deal with the weirdo domain problems that Pow() has. Like 0^0 and -Infinity raised to any power.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload