[2/5] iee754: prvoide gcc builtins based generic sqrt functions

Message ID 20200530020047.5490-3-vgupta@synopsys.com
State Superseded
Headers
Series use gcc builtins for sqrt and fma functions |

Commit Message

Vineet Gupta May 30, 2020, 2 a.m. UTC
  ---
 sysdeps/generic/math-use-builtins.h | 3 +++
 sysdeps/ieee754/dbl-64/e_sqrt.c     | 6 ++++++
 sysdeps/ieee754/flt-32/e_sqrtf.c    | 6 ++++++
 3 files changed, 15 insertions(+)
  

Comments

Adhemerval Zanella June 1, 2020, 2:13 p.m. UTC | #1
On 29/05/2020 23:00, Vineet Gupta wrote:

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  sysdeps/generic/math-use-builtins.h | 3 +++
>  sysdeps/ieee754/dbl-64/e_sqrt.c     | 6 ++++++
>  sysdeps/ieee754/flt-32/e_sqrtf.c    | 6 ++++++
>  3 files changed, 15 insertions(+)
> 
> diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
> index 8a39ef58bc95..fc724c824a17 100644
> --- a/sysdeps/generic/math-use-builtins.h
> +++ b/sysdeps/generic/math-use-builtins.h
> @@ -60,4 +60,7 @@
>  # define USE_COPYSIGNF128_BUILTIN 0
>  #endif
>  
> +#define USE_SQRT_BUILTIN 0
> +#define USE_SQRTF_BUILTIN 0
> +
>  #endif /* math-use-builtins.h */

Ok.

> diff --git a/sysdeps/ieee754/dbl-64/e_sqrt.c b/sysdeps/ieee754/dbl-64/e_sqrt.c
> index d42a1a4eb6e9..518a8ae5cdaf 100644
> --- a/sysdeps/ieee754/dbl-64/e_sqrt.c
> +++ b/sysdeps/ieee754/dbl-64/e_sqrt.c
> @@ -41,6 +41,7 @@
>  #include <math_private.h>
>  #include <fenv_private.h>
>  #include <libm-alias-finite.h>
> +#include <math-use-builtins.h>
>  
>  /*********************************************************************/
>  /* An ultimate sqrt routine. Given an IEEE double machine number x   */

Ok.

> @@ -50,6 +51,10 @@
>  double
>  __ieee754_sqrt (double x)
>  {
> +#if USE_SQRT_BUILTIN
> +  return __builtin_sqrt (x);
> +#else
> +  /* Use generic implementation.  */
>    static const double
>      rt0 = 9.99999999859990725855365213134618E-01,
>      rt1 = 4.99999999495955425917856814202739E-01,
> @@ -138,6 +143,7 @@ __ieee754_sqrt (double x)
>  	return (x - x) / (x - x); /* sqrt(-ve)=sNaN */
>        return 0x1p-256 * __ieee754_sqrt (x * 0x1p512);
>      }
> +#endif /* ! USE_SQRT_BUILTIN  */
>  }
>  #ifndef __ieee754_sqrt
>  libm_alias_finite (__ieee754_sqrt, __sqrt)

Ok.

> diff --git a/sysdeps/ieee754/flt-32/e_sqrtf.c b/sysdeps/ieee754/flt-32/e_sqrtf.c
> index b339444301aa..68fc80e1e1ee 100644
> --- a/sysdeps/ieee754/flt-32/e_sqrtf.c
> +++ b/sysdeps/ieee754/flt-32/e_sqrtf.c
> @@ -16,12 +16,17 @@
>  #include <math.h>
>  #include <math_private.h>
>  #include <libm-alias-finite.h>
> +#include <math-use-builtins.h>
>  
>  static	const float	one	= 1.0, tiny=1.0e-30;
>  
>  float
>  __ieee754_sqrtf(float x)
>  {
> +#if USE_SQRTF_BUILTIN
> +	return __builtin_sqrtf (x);
> +#else
> +	/* Use generic implementation.  */
>  	float z;
>  	int32_t sign = (int)0x80000000;
>  	int32_t ix,s,q,m,t,i;
> @@ -83,6 +88,7 @@ __ieee754_sqrtf(float x)
>  	ix += (m <<23);
>  	SET_FLOAT_WORD(z,ix);
>  	return z;
> +#endif /* ! USE_SQRTF_BUILTIN  */
>  }
>  #ifndef __ieee754_sqrtf
>  libm_alias_finite (__ieee754_sqrtf, __sqrtf)
> 

Ok.
  
Florian Weimer June 1, 2020, 2:18 p.m. UTC | #2
There's a typo in commit subject: “prvoide”.
  
Adhemerval Zanella June 1, 2020, 2:42 p.m. UTC | #3
On 01/06/2020 11:13, Adhemerval Zanella wrote:
> 
> 
> On 29/05/2020 23:00, Vineet Gupta wrote:
> 
> LGTM, thanks.
> 
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
> 
>> ---
>>  sysdeps/generic/math-use-builtins.h | 3 +++
>>  sysdeps/ieee754/dbl-64/e_sqrt.c     | 6 ++++++
>>  sysdeps/ieee754/flt-32/e_sqrtf.c    | 6 ++++++
>>  3 files changed, 15 insertions(+)
>>
>> diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
>> index 8a39ef58bc95..fc724c824a17 100644
>> --- a/sysdeps/generic/math-use-builtins.h
>> +++ b/sysdeps/generic/math-use-builtins.h
>> @@ -60,4 +60,7 @@
>>  # define USE_COPYSIGNF128_BUILTIN 0
>>  #endif
>>  
>> +#define USE_SQRT_BUILTIN 0
>> +#define USE_SQRTF_BUILTIN 0
>> +
>>  #endif /* math-use-builtins.h */
> 
> Ok.
> 
>> diff --git a/sysdeps/ieee754/dbl-64/e_sqrt.c b/sysdeps/ieee754/dbl-64/e_sqrt.c
>> index d42a1a4eb6e9..518a8ae5cdaf 100644
>> --- a/sysdeps/ieee754/dbl-64/e_sqrt.c
>> +++ b/sysdeps/ieee754/dbl-64/e_sqrt.c
>> @@ -41,6 +41,7 @@
>>  #include <math_private.h>
>>  #include <fenv_private.h>
>>  #include <libm-alias-finite.h>
>> +#include <math-use-builtins.h>
>>  
>>  /*********************************************************************/
>>  /* An ultimate sqrt routine. Given an IEEE double machine number x   */
> 
> Ok.
> 
>> @@ -50,6 +51,10 @@
>>  double
>>  __ieee754_sqrt (double x)
>>  {
>> +#if USE_SQRT_BUILTIN
>> +  return __builtin_sqrt (x);
>> +#else
>> +  /* Use generic implementation.  */
>>    static const double
>>      rt0 = 9.99999999859990725855365213134618E-01,
>>      rt1 = 4.99999999495955425917856814202739E-01,
>> @@ -138,6 +143,7 @@ __ieee754_sqrt (double x)
>>  	return (x - x) / (x - x); /* sqrt(-ve)=sNaN */
>>        return 0x1p-256 * __ieee754_sqrt (x * 0x1p512);
>>      }
>> +#endif /* ! USE_SQRT_BUILTIN  */
>>  }
>>  #ifndef __ieee754_sqrt
>>  libm_alias_finite (__ieee754_sqrt, __sqrt)
> 
> Ok.
> 
>> diff --git a/sysdeps/ieee754/flt-32/e_sqrtf.c b/sysdeps/ieee754/flt-32/e_sqrtf.c
>> index b339444301aa..68fc80e1e1ee 100644
>> --- a/sysdeps/ieee754/flt-32/e_sqrtf.c
>> +++ b/sysdeps/ieee754/flt-32/e_sqrtf.c
>> @@ -16,12 +16,17 @@
>>  #include <math.h>
>>  #include <math_private.h>
>>  #include <libm-alias-finite.h>
>> +#include <math-use-builtins.h>
>>  
>>  static	const float	one	= 1.0, tiny=1.0e-30;

You will need to move this definitions inside the !USE_SQRTF_BUILTIN
to avoid defined by not used warnings.  Current practice is to just
open code the constants and let compiler optimize the constant pool:

diff --git a/sysdeps/ieee754/flt-32/e_sqrtf.c b/sysdeps/ieee754/flt-32/e_sqrtf.c
index 68fc80e..d85a041 100644
--- a/sysdeps/ieee754/flt-32/e_sqrtf.c
+++ b/sysdeps/ieee754/flt-32/e_sqrtf.c
@@ -18,8 +18,6 @@
 #include <libm-alias-finite.h>
 #include <math-use-builtins.h>
 
-static const float     one     = 1.0, tiny=1.0e-30;
-
 float
 __ieee754_sqrtf(float x)
 {
@@ -75,10 +73,10 @@ __ieee754_sqrtf(float x)
 
     /* use floating add to find out rounding direction */
        if(ix!=0) {
-           z = one-tiny; /* trigger inexact flag */
-           if (z>=one) {
-               z = one+tiny;
-               if (z>one)
+           z = 0x1p0 - 0x1.4484cp-100; /* trigger inexact flag */
+           if (z >= 0x1p0) {
+               z = 0x1p0 + 0x1.4484cp-100;
+               if (z > 0x1p0)
                    q += 2;
                else
                    q += (q&1);

>>  
>>  float
>>  __ieee754_sqrtf(float x)
>>  {
>> +#if USE_SQRTF_BUILTIN
>> +	return __builtin_sqrtf (x);
>> +#else
>> +	/* Use generic implementation.  */
>>  	float z;
>>  	int32_t sign = (int)0x80000000;
>>  	int32_t ix,s,q,m,t,i;
>> @@ -83,6 +88,7 @@ __ieee754_sqrtf(float x)
>>  	ix += (m <<23);
>>  	SET_FLOAT_WORD(z,ix);
>>  	return z;
>> +#endif /* ! USE_SQRTF_BUILTIN  */
>>  }
>>  #ifndef __ieee754_sqrtf
>>  libm_alias_finite (__ieee754_sqrtf, __sqrtf)
>>
> 
> Ok.
>
  
Vineet Gupta June 1, 2020, 6:12 p.m. UTC | #4
On 6/1/20 7:42 AM, Adhemerval Zanella via Libc-alpha wrote:
> You will need to move this definitions inside the !USE_SQRTF_BUILTIN
> to avoid defined by not used warnings.  Current practice is to just
> open code the constants and let compiler optimize the constant pool:

Won't it be better to keep the const variable and trust the compiler to subsume it
instead of open coding in multiple places. Makes it more readable ?

-Vineet
  
Vineet Gupta June 1, 2020, 6:12 p.m. UTC | #5
On 6/1/20 7:18 AM, Florian Weimer wrote:
> There's a typo in commit subject: “prvoide”.

oops sorry, fixed now.
  
Adhemerval Zanella June 1, 2020, 6:32 p.m. UTC | #6
On 01/06/2020 15:12, Vineet Gupta wrote:
> On 6/1/20 7:42 AM, Adhemerval Zanella via Libc-alpha wrote:
>> You will need to move this definitions inside the !USE_SQRTF_BUILTIN
>> to avoid defined by not used warnings.  Current practice is to just
>> open code the constants and let compiler optimize the constant pool:
> 
> Won't it be better to keep the const variable and trust the compiler to subsume it
> instead of open coding in multiple places. Makes it more readable ?

I don't have a strong preference, it is just the recent math optimizations
use the constant directly in the hex format instead of trying to factoring
them out.
  

Patch

diff --git a/sysdeps/generic/math-use-builtins.h b/sysdeps/generic/math-use-builtins.h
index 8a39ef58bc95..fc724c824a17 100644
--- a/sysdeps/generic/math-use-builtins.h
+++ b/sysdeps/generic/math-use-builtins.h
@@ -60,4 +60,7 @@ 
 # define USE_COPYSIGNF128_BUILTIN 0
 #endif
 
+#define USE_SQRT_BUILTIN 0
+#define USE_SQRTF_BUILTIN 0
+
 #endif /* math-use-builtins.h */
diff --git a/sysdeps/ieee754/dbl-64/e_sqrt.c b/sysdeps/ieee754/dbl-64/e_sqrt.c
index d42a1a4eb6e9..518a8ae5cdaf 100644
--- a/sysdeps/ieee754/dbl-64/e_sqrt.c
+++ b/sysdeps/ieee754/dbl-64/e_sqrt.c
@@ -41,6 +41,7 @@ 
 #include <math_private.h>
 #include <fenv_private.h>
 #include <libm-alias-finite.h>
+#include <math-use-builtins.h>
 
 /*********************************************************************/
 /* An ultimate sqrt routine. Given an IEEE double machine number x   */
@@ -50,6 +51,10 @@ 
 double
 __ieee754_sqrt (double x)
 {
+#if USE_SQRT_BUILTIN
+  return __builtin_sqrt (x);
+#else
+  /* Use generic implementation.  */
   static const double
     rt0 = 9.99999999859990725855365213134618E-01,
     rt1 = 4.99999999495955425917856814202739E-01,
@@ -138,6 +143,7 @@  __ieee754_sqrt (double x)
 	return (x - x) / (x - x); /* sqrt(-ve)=sNaN */
       return 0x1p-256 * __ieee754_sqrt (x * 0x1p512);
     }
+#endif /* ! USE_SQRT_BUILTIN  */
 }
 #ifndef __ieee754_sqrt
 libm_alias_finite (__ieee754_sqrt, __sqrt)
diff --git a/sysdeps/ieee754/flt-32/e_sqrtf.c b/sysdeps/ieee754/flt-32/e_sqrtf.c
index b339444301aa..68fc80e1e1ee 100644
--- a/sysdeps/ieee754/flt-32/e_sqrtf.c
+++ b/sysdeps/ieee754/flt-32/e_sqrtf.c
@@ -16,12 +16,17 @@ 
 #include <math.h>
 #include <math_private.h>
 #include <libm-alias-finite.h>
+#include <math-use-builtins.h>
 
 static	const float	one	= 1.0, tiny=1.0e-30;
 
 float
 __ieee754_sqrtf(float x)
 {
+#if USE_SQRTF_BUILTIN
+	return __builtin_sqrtf (x);
+#else
+	/* Use generic implementation.  */
 	float z;
 	int32_t sign = (int)0x80000000;
 	int32_t ix,s,q,m,t,i;
@@ -83,6 +88,7 @@  __ieee754_sqrtf(float x)
 	ix += (m <<23);
 	SET_FLOAT_WORD(z,ix);
 	return z;
+#endif /* ! USE_SQRTF_BUILTIN  */
 }
 #ifndef __ieee754_sqrtf
 libm_alias_finite (__ieee754_sqrtf, __sqrtf)