[PATCH] PPC faster sqrt functions

Steve Munroe sjmunroe@us.ibm.com
Tue May 25 16:49:00 GMT 2004


On 64-bit powerpc processors the optional instructions (including fsqrt 
and fsqrts) are always implemented. So for PPC64 and PPC32 running on 
PPC64 hardware we should take advantage of the hardware sqrt.

The trick is to use the AT_HWCAP via _dl_hwcap to check for 
PPC_FEATURE_64. So I redefined the current __ieee754_sqrt[f] 
implementations as __slow_ieee754_sqrt[f]. Then updated mathinline.h to 
defined __MATH_INLINE versions of __ieee754_sqrt() and  __ieee754_sqrt() 
which use the following form:


+__MATH_INLINE double
+__ieee754_sqrt (double __x)
+{
+  double __z;
+
+  /* If the CPU is 64-bit we can use the optional FP instructions we.  */
+  if ((GLRO(dl_hwcap) & PPC_FEATURE_64) != 0)
+  {
+    /* Volatile is required to prevent the compiler from moving the
+       fsqrt instruction above the branch.  */
+     __asm __volatile (
+	"	fsqrt	%0,%1\n"
+		: "=f" (__z)
+		: "f" (__x));
+  }
+  else
+     __z = __slow_ieee754_sqrt(__x);
+
+  return __z;
+}

This inline is then used in the wapper functions __sqrt[f]() and 
internally to other libm functions that use __ieee_sqrt[f](). This the 
software implementation for PPC32 hardware but leverages the fsqrt 
instruction on PPC64 hardware. The performance gain is 2.5-3 times.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ppc-sqrt-20040520.txt
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20040525/9f204957/attachment.txt>


More information about the Libc-alpha mailing list