C++ Using Intrinsics in a Cross-platform Manner
<p>I have a cross platform math library that I am working on and I want to make sure that some common operations are performed in an optimized manner, so I wish to use some intrinsic functions wrapped in inline functions to compute things such as sqrt(float/double). I should mention that at this point I only plan on targeting a select few platforms which are mentioned in the source below.</p>

<p>I have started a single prototype of the a sqrt() function but before I proceed any further I want to know if I am doing anything blatantly wrong.</p>

<p>I am following the documentation at:
<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ff471376%28v=vs.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/ff471376%28v=vs.85%29.aspx</a><br>
<a href="http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html" rel="nofollow">http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html</a></p>

<p>Also, you will note I have a blank line under the pound define for macintosh. I know the newer versions are using clang/llvm. How do you get access to intrinsic functions under the newer versions of mac os and its associated compilers?</p>

<pre><code>#ifndef INTRINSICS_H_INCLUDED
#define INTRINSICS_H_INCLUDED

#if defined(_WIN32)
#elif defined(__gnu_linux__) || defined(__linux__)
#elif defined(__ANDROID__)
#elif defined(__CYGWIN__)
#elif defined(macintosh)
#else
#include &lt;cmath&gt;
#endif

#if defined(_WIN32)
#pragma function( sqrt )
#endif
template&lt;typename T&gt;
inline float sqrt(T &amp;val)
{
#if defined(_WIN32)
  return sqrt(val);
#elif defined(__gnu_linux__) || defined(__linux__)
  return __builtin_sqrt(val);
#elif defined(__ANDROID__)
  return __builtin_sqrt(val);
#elif defined(__CYGWIN__)
  return __builtin_sqrt(val);
#elif defined(macintosh)
  // How do I do this
#else
  return std::sqrt(val);
#endif
}
#if defined(_WIN32)
#pragma function( sqrt )
#endif
#endif
</code></pre>
