ce426f
commit e6d90d675d4cae810be76a5ff41b8ae8bd6bc914
ce426f
Author: Wilco Dijkstra <wdijkstr@arm.com>
ce426f
Date:   Mon Jun 23 17:15:41 2014 +0100
ce426f
ce426f
    Add generic HAVE_RM_CTX implementation
ce426f
    
ce426f
    This patch adds a generic implementation of HAVE_RM_CTX using standard
ce426f
    fenv calls. As a result math functions using SET_RESTORE_ROUND* macros
ce426f
    do not suffer from a large slowdown on targets which do not implement
ce426f
    optimized libc_fe*_ctx inline functions. Most of the libc_fe* inline
ce426f
    functions are now unused and could be removed in the future (there are
ce426f
    a few math functions left which use a mixture of standard fenv calls
ce426f
    and libc_fe* inline functions - they could be updated to use
ce426f
    SET_RESTORE_ROUND or improved to avoid expensive fenv manipulations
ce426f
    across just a few FP instructions).
ce426f
    
ce426f
    libc_feholdsetround*_noex_ctx is added to enable better optimization of
ce426f
    SET_RESTORE_ROUND_NOEX* implementations.
ce426f
    
ce426f
    Performance measurements on ARM and x86 of sin() show significant gains
ce426f
    over the current default, fairly close to a highly optimized fenv_private:
ce426f
    
ce426f
                            ARM   x86
ce426f
    no fenv_private      : 100%  100%
ce426f
    generic HAVE_RM_CTX  : 250%  350%
ce426f
    fenv_private (CTX)   : 250%  450%
ce426f
    
ce426f
    2014-06-23  Will Newton  <will.newton@linaro.org>
ce426f
    	    Wilco  <wdijkstr@arm.com>
ce426f
    
ce426f
    	* sysdeps/generic/math_private.h: Add generic HAVE_RM_CTX
ce426f
    	implementation.  Include get-rounding-mode.h.
ce426f
    	[!HAVE_RM_CTX]: Define HAVE_RM_CTX to zero.
ce426f
    	[!libc_feholdsetround_noex_ctx]: Define
ce426f
    	libc_feholdsetround_noex_ctx.
ce426f
    	[!libc_feholdsetround_noexf_ctx]: Define
ce426f
    	libc_feholdsetround_noexf_ctx.
ce426f
    	[!libc_feholdsetround_noexl_ctx]: Define
ce426f
    	libc_feholdsetround_noexl_ctx.
ce426f
    	(libc_feholdsetround_ctx): New function.
ce426f
    	(libc_feresetround_ctx): New function.
ce426f
    	(libc_feholdsetround_noex_ctx): New function.
ce426f
    	(libc_feresetround_noex_ctx): New function.
ce426f
ce426f
diff --git a/sysdeps/generic/math_private.h b/sysdeps/generic/math_private.h
ce426f
index 9b881a3..94c1e4a 100644
ce426f
--- a/sysdeps/generic/math_private.h
ce426f
+++ b/sysdeps/generic/math_private.h
ce426f
@@ -20,6 +20,7 @@
ce426f
 #include <stdint.h>
ce426f
 #include <sys/types.h>
ce426f
 #include <fenv.h>
ce426f
+#include <get-rounding-mode.h>
ce426f
 
ce426f
 /* The original fdlibm code used statements like:
ce426f
 	n0 = ((*(int*)&one)>>29)^1;		* index of high word *
ce426f
@@ -551,12 +552,26 @@ default_libc_feupdateenv_test (fenv_t *e, int ex)
ce426f
 # define libc_feresetround_noexl libc_fesetenvl
ce426f
 #endif
ce426f
 
ce426f
+#ifndef HAVE_RM_CTX
ce426f
+# define HAVE_RM_CTX 0
ce426f
+#endif
ce426f
+
ce426f
 #if HAVE_RM_CTX
ce426f
 /* Set/Restore Rounding Modes only when necessary.  If defined, these functions
ce426f
    set/restore floating point state only if the state needed within the lexical
ce426f
    block is different from the current state.  This saves a lot of time when
ce426f
    the floating point unit is much slower than the fixed point units.  */
ce426f
 
ce426f
+# ifndef libc_feholdsetround_noex_ctx
ce426f
+#   define libc_feholdsetround_noex_ctx  libc_feholdsetround_ctx
ce426f
+# endif
ce426f
+# ifndef libc_feholdsetround_noexf_ctx
ce426f
+#   define libc_feholdsetround_noexf_ctx libc_feholdsetroundf_ctx
ce426f
+# endif
ce426f
+# ifndef libc_feholdsetround_noexl_ctx
ce426f
+#   define libc_feholdsetround_noexl_ctx libc_feholdsetroundl_ctx
ce426f
+# endif
ce426f
+
ce426f
 # ifndef libc_feresetround_noex_ctx
ce426f
 #   define libc_feresetround_noex_ctx  libc_fesetenv_ctx
ce426f
 # endif
ce426f
@@ -567,24 +582,80 @@ default_libc_feupdateenv_test (fenv_t *e, int ex)
ce426f
 #   define libc_feresetround_noexl_ctx libc_fesetenvl_ctx
ce426f
 # endif
ce426f
 
ce426f
-# ifndef libc_feholdsetround_53bit_ctx
ce426f
-#   define libc_feholdsetround_53bit_ctx libc_feholdsetround_ctx
ce426f
-# endif
ce426f
+#else
ce426f
 
ce426f
-# ifndef libc_feresetround_53bit_ctx
ce426f
-#   define libc_feresetround_53bit_ctx libc_feresetround_ctx
ce426f
-# endif
ce426f
+/* Default implementation using standard fenv functions.
ce426f
+   Avoid unnecessary rounding mode changes by first checking the
ce426f
+   current rounding mode.  Note the use of __glibc_unlikely is
ce426f
+   important for performance.  */
ce426f
 
ce426f
-# define SET_RESTORE_ROUND_GENERIC(RM,ROUNDFUNC,CLEANUPFUNC) \
ce426f
-  struct rm_ctx ctx __attribute__((cleanup(CLEANUPFUNC ## _ctx)));	      \
ce426f
-  ROUNDFUNC ## _ctx (&ctx, (RM))
ce426f
-#else
ce426f
-# define SET_RESTORE_ROUND_GENERIC(RM, ROUNDFUNC, CLEANUPFUNC) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(CLEANUPFUNC)));	\
ce426f
-  ROUNDFUNC (&__libc_save_rm, (RM))
ce426f
+static __always_inline void
ce426f
+libc_feholdsetround_ctx (struct rm_ctx *ctx, int round)
ce426f
+{
ce426f
+  ctx->updated_status = false;
ce426f
+
ce426f
+  /* Update rounding mode only if different.  */
ce426f
+  if (__glibc_unlikely (round != get_rounding_mode ()))
ce426f
+    {
ce426f
+      ctx->updated_status = true;
ce426f
+      fegetenv (&ctx->env);
ce426f
+      fesetround (round);
ce426f
+    }
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feresetround_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  /* Restore the rounding mode if updated.  */
ce426f
+  if (__glibc_unlikely (ctx->updated_status))
ce426f
+    feupdateenv (&ctx->env);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdsetround_noex_ctx (struct rm_ctx *ctx, int round)
ce426f
+{
ce426f
+  /* Save exception flags and rounding mode.  */
ce426f
+  fegetenv (&ctx->env);
ce426f
+
ce426f
+  /* Update rounding mode only if different.  */
ce426f
+  if (__glibc_unlikely (round != get_rounding_mode ()))
ce426f
+    fesetround (round);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feresetround_noex_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  /* Restore exception flags and rounding mode.  */
ce426f
+  fesetenv (&ctx->env);
ce426f
+}
ce426f
+
ce426f
+# define libc_feholdsetroundf_ctx libc_feholdsetround_ctx
ce426f
+# define libc_feholdsetroundl_ctx libc_feholdsetround_ctx
ce426f
+# define libc_feresetroundf_ctx   libc_feresetround_ctx
ce426f
+# define libc_feresetroundl_ctx   libc_feresetround_ctx
ce426f
+
ce426f
+# define libc_feholdsetround_noexf_ctx libc_feholdsetround_noex_ctx
ce426f
+# define libc_feholdsetround_noexl_ctx libc_feholdsetround_noex_ctx
ce426f
+# define libc_feresetround_noexf_ctx   libc_feresetround_noex_ctx
ce426f
+# define libc_feresetround_noexl_ctx   libc_feresetround_noex_ctx
ce426f
+
ce426f
+#endif
ce426f
+
ce426f
+#ifndef libc_feholdsetround_53bit_ctx
ce426f
+#  define libc_feholdsetround_53bit_ctx libc_feholdsetround_ctx
ce426f
+#endif
ce426f
+#ifndef libc_feresetround_53bit_ctx
ce426f
+#  define libc_feresetround_53bit_ctx libc_feresetround_ctx
ce426f
 #endif
ce426f
 
ce426f
-/* Save and restore the rounding mode within a lexical block.  */
ce426f
+#define SET_RESTORE_ROUND_GENERIC(RM,ROUNDFUNC,CLEANUPFUNC) \
ce426f
+  struct rm_ctx ctx __attribute__((cleanup (CLEANUPFUNC ## _ctx))); \
ce426f
+  ROUNDFUNC ## _ctx (&ctx, (RM))
ce426f
+
ce426f
+/* Set the rounding mode within a lexical block.  Restore the rounding mode to
ce426f
+   the value at the start of the block.  The exception mode must be preserved.
ce426f
+   Exceptions raised within the block must be set in the exception flags.
ce426f
+   Non-stop mode may be enabled inside the block.  */
ce426f
 
ce426f
 #define SET_RESTORE_ROUND(RM) \
ce426f
   SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround, libc_feresetround)
ce426f
@@ -593,15 +664,21 @@ default_libc_feupdateenv_test (fenv_t *e, int ex)
ce426f
 #define SET_RESTORE_ROUNDL(RM) \
ce426f
   SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetroundl, libc_feresetroundl)
ce426f
 
ce426f
-/* Save and restore the rounding mode within a lexical block, and also
ce426f
-   the set of exceptions raised within the block may be discarded.  */
ce426f
+/* Set the rounding mode within a lexical block.  Restore the rounding mode to
ce426f
+   the value at the start of the block.  The exception mode must be preserved.
ce426f
+   Exceptions raised within the block must be discarded, and exception flags
ce426f
+   are restored to the value at the start of the block.
ce426f
+   Non-stop mode may be enabled inside the block.  */
ce426f
 
ce426f
 #define SET_RESTORE_ROUND_NOEX(RM) \
ce426f
-  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround, libc_feresetround_noex)
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround_noex, \
ce426f
+			     libc_feresetround_noex)
ce426f
 #define SET_RESTORE_ROUND_NOEXF(RM) \
ce426f
-  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetroundf, libc_feresetround_noexf)
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround_noexf, \
ce426f
+			     libc_feresetround_noexf)
ce426f
 #define SET_RESTORE_ROUND_NOEXL(RM) \
ce426f
-  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetroundl, libc_feresetround_noexl)
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround_noexl, \
ce426f
+			     libc_feresetround_noexl)
ce426f
 
ce426f
 /* Like SET_RESTORE_ROUND, but also set rounding precision to 53 bits.  */
ce426f
 #define SET_RESTORE_ROUND_53BIT(RM) \