ce426f
commit 2506109403de69bd454de27835d42e6eb6ec3abc
ce426f
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
ce426f
Date:   Wed Jun 12 10:36:48 2013 +0530
ce426f
ce426f
    Set/restore rounding mode only when needed
ce426f
    
ce426f
    The most common use case of math functions is with default rounding
ce426f
    mode, i.e. rounding to nearest.  Setting and restoring rounding mode
ce426f
    is an unnecessary overhead for this, so I've added support for a
ce426f
    context, which does the set/restore only if the FP status needs a
ce426f
    change.  The code is written such that only x86 uses these.  Other
ce426f
    architectures should be unaffected by it, but would definitely benefit
ce426f
    if the set/restore has as much overhead relative to the rest of the
ce426f
    code, as the x86 bits do.
ce426f
    
ce426f
    Here's a summary of the performance improvement due to these
ce426f
    improvements; I've only mentioned functions that use the set/restore
ce426f
    and have benchmark inputs for x86_64:
ce426f
    
ce426f
    Before:
ce426f
    
ce426f
    cos(): ITERS:4.69335e+08: TOTAL:28884.6Mcy, MAX:4080.28cy, MIN:57.562cy, 16248.6 calls/Mcy
ce426f
    exp(): ITERS:4.47604e+08: TOTAL:28796.2Mcy, MAX:207.721cy, MIN:62.385cy, 15543.9 calls/Mcy
ce426f
    pow(): ITERS:1.63485e+08: TOTAL:28879.9Mcy, MAX:362.255cy, MIN:172.469cy, 5660.86 calls/Mcy
ce426f
    sin(): ITERS:3.89578e+08: TOTAL:28900Mcy, MAX:704.859cy, MIN:47.583cy, 13480.2 calls/Mcy
ce426f
    tan(): ITERS:7.0971e+07: TOTAL:28902.2Mcy, MAX:1357.79cy, MIN:388.58cy, 2455.55 calls/Mcy
ce426f
    
ce426f
    After:
ce426f
    
ce426f
    cos(): ITERS:6.0014e+08: TOTAL:28875.9Mcy, MAX:364.283cy, MIN:45.716cy, 20783.4 calls/Mcy
ce426f
    exp(): ITERS:5.48578e+08: TOTAL:28764.9Mcy, MAX:191.617cy, MIN:51.011cy, 19071.1 calls/Mcy
ce426f
    pow(): ITERS:1.70013e+08: TOTAL:28873.6Mcy, MAX:689.522cy, MIN:163.989cy, 5888.18 calls/Mcy
ce426f
    sin(): ITERS:4.64079e+08: TOTAL:28891.5Mcy, MAX:6959.3cy, MIN:36.189cy, 16062.8 calls/Mcy
ce426f
    tan(): ITERS:7.2354e+07: TOTAL:28898.9Mcy, MAX:1295.57cy, MIN:380.698cy, 2503.7 calls/Mcy
ce426f
    
ce426f
    So the improvements are:
ce426f
    
ce426f
    cos: 27.9089%
ce426f
    exp: 22.6919%
ce426f
    pow: 4.01564%
ce426f
    sin: 19.1585%
ce426f
    tan: 1.96086%
ce426f
    
ce426f
    The downside of the change is that it will have an adverse performance
ce426f
    impact on non-default rounding modes, but I think the tradeoff is
ce426f
    justified.
ce426f
ce426f
diff --git glibc-2.17-c758a686/include/fenv.h glibc-2.17-c758a686/include/fenv.h
ce426f
index ed6d139..9f90d17 100644
ce426f
--- glibc-2.17-c758a686/include/fenv.h
ce426f
+++ glibc-2.17-c758a686/include/fenv.h
ce426f
@@ -1,5 +1,6 @@
ce426f
 #ifndef _FENV_H
ce426f
 #include <math/fenv.h>
ce426f
+#include <stdbool.h>
ce426f
 
ce426f
 #ifndef _ISOMAC
ce426f
 /* Now define the internal interfaces.  */
ce426f
@@ -23,4 +24,13 @@ libm_hidden_proto (fetestexcept)
ce426f
 libm_hidden_proto (feclearexcept)
ce426f
 #endif
ce426f
 
ce426f
+/* Rounding mode context.  This allows functions to set/restore rounding mode
ce426f
+   only when the desired rounding mode is different from the current rounding
ce426f
+   mode.  */
ce426f
+struct rm_ctx
ce426f
+{
ce426f
+  fenv_t env;
ce426f
+  bool updated_status;
ce426f
+};
ce426f
+
ce426f
 #endif
ce426f
diff --git glibc-2.17-c758a686/sysdeps/generic/math_private.h glibc-2.17-c758a686/sysdeps/generic/math_private.h
ce426f
index e98360d..c0fc03d 100644
ce426f
--- glibc-2.17-c758a686/sysdeps/generic/math_private.h
ce426f
+++ glibc-2.17-c758a686/sysdeps/generic/math_private.h
ce426f
@@ -553,35 +553,62 @@ default_libc_feupdateenv_test (fenv_t *e, int ex)
ce426f
 # define libc_feresetround_noexl libc_fesetenvl
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_feresetround_noex_ctx
ce426f
+#   define libc_feresetround_noex_ctx  libc_fesetenv_ctx
ce426f
+# endif
ce426f
+# ifndef libc_feresetround_noexf_ctx
ce426f
+#   define libc_feresetround_noexf_ctx libc_fesetenvf_ctx
ce426f
+# endif
ce426f
+# ifndef libc_feresetround_noexl_ctx
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
+
ce426f
+# ifndef libc_feresetround_53bit_ctx
ce426f
+#   define libc_feresetround_53bit_ctx libc_feresetround_ctx
ce426f
+# endif
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
+#endif
ce426f
+
ce426f
 /* Save and restore the rounding mode within a lexical block.  */
ce426f
 
ce426f
 #define SET_RESTORE_ROUND(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetround)));	\
ce426f
-  libc_feholdsetround (&__libc_save_rm, (RM))
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround, libc_feresetround)
ce426f
 #define SET_RESTORE_ROUNDF(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetroundf)));	\
ce426f
-  libc_feholdsetroundf (&__libc_save_rm, (RM))
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetroundf, libc_feresetroundf)
ce426f
 #define SET_RESTORE_ROUNDL(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetroundl)));	\
ce426f
-  libc_feholdsetroundl (&__libc_save_rm, (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
 
ce426f
 #define SET_RESTORE_ROUND_NOEX(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetround_noex))); \
ce426f
-  libc_feholdsetround (&__libc_save_rm, (RM))
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround, libc_feresetround_noex)
ce426f
 #define SET_RESTORE_ROUND_NOEXF(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetround_noexf))); \
ce426f
-  libc_feholdsetroundf (&__libc_save_rm, (RM))
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetroundf, libc_feresetround_noexf)
ce426f
 #define SET_RESTORE_ROUND_NOEXL(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetround_noexl))); \
ce426f
-  libc_feholdsetroundl (&__libc_save_rm, (RM))
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetroundl, libc_feresetround_noexl)
ce426f
 
ce426f
 /* Like SET_RESTORE_ROUND, but also set rounding precision to 53 bits.  */
ce426f
 #define SET_RESTORE_ROUND_53BIT(RM) \
ce426f
-  fenv_t __libc_save_rm __attribute__((cleanup(libc_feresetround_53bit))); \
ce426f
-  libc_feholdsetround_53bit (&__libc_save_rm, (RM))
ce426f
+  SET_RESTORE_ROUND_GENERIC (RM, libc_feholdsetround_53bit,	      \
ce426f
+			     libc_feresetround_53bit)
ce426f
 
ce426f
 #define __nan(str) \
ce426f
   (__builtin_constant_p (str) && str[0] == '\0' ? NAN : __nan (str))
ce426f
diff --git glibc-2.17-c758a686/sysdeps/i386/fpu/fenv_private.h glibc-2.17-c758a686/sysdeps/i386/fpu/fenv_private.h
ce426f
index 1f8336c..3998387 100644
ce426f
--- glibc-2.17-c758a686/sysdeps/i386/fpu/fenv_private.h
ce426f
+++ glibc-2.17-c758a686/sysdeps/i386/fpu/fenv_private.h
ce426f
@@ -322,6 +322,179 @@ libc_feresetround_387 (fenv_t *e)
ce426f
 # define libc_feholdsetround_53bit	libc_feholdsetround_387_53bit
ce426f
 #endif
ce426f
 
ce426f
+/* We have support for rounding mode context.  */
ce426f
+#define HAVE_RM_CTX 1
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdexcept_setround_sse_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  unsigned int mxcsr, new_mxcsr;
ce426f
+  asm (STMXCSR " %0" : "=m" (*&mxcsr));
ce426f
+  new_mxcsr = ((mxcsr | 0x1f80) & ~0x603f) | (r << 3);
ce426f
+
ce426f
+  ctx->env.__mxcsr = mxcsr;
ce426f
+  if (__glibc_unlikely (mxcsr != new_mxcsr))
ce426f
+    {
ce426f
+      asm volatile (LDMXCSR " %0" : : "m" (*&new_mxcsr));
ce426f
+      ctx->updated_status = true;
ce426f
+    }
ce426f
+  else
ce426f
+    ctx->updated_status = false;
ce426f
+}
ce426f
+
ce426f
+/* Unconditional since we want to overwrite any exceptions that occurred in the
ce426f
+   context.  This is also why all fehold* functions unconditionally write into
ce426f
+   ctx->env.  */
ce426f
+static __always_inline void
ce426f
+libc_fesetenv_sse_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  libc_fesetenv_sse (&ctx->env);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feupdateenv_sse_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  if (__glibc_unlikely (ctx->updated_status))
ce426f
+    libc_feupdateenv_test_sse (&ctx->env, 0);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdexcept_setround_387_prec_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  libc_feholdexcept_387 (&ctx->env);
ce426f
+
ce426f
+  fpu_control_t cw = ctx->env.__control_word;
ce426f
+  fpu_control_t old_cw = cw;
ce426f
+  cw &= ~(_FPU_RC_ZERO | _FPU_EXTENDED);
ce426f
+  cw |= r | 0x3f;
ce426f
+
ce426f
+  if (__glibc_unlikely (old_cw != cw))
ce426f
+    {
ce426f
+      _FPU_SETCW (cw);
ce426f
+      ctx->updated_status = true;
ce426f
+    }
ce426f
+  else
ce426f
+    ctx->updated_status = false;
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdexcept_setround_387_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  libc_feholdexcept_setround_387_prec_ctx (ctx, r | _FPU_EXTENDED);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdexcept_setround_387_53bit_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  libc_feholdexcept_setround_387_prec_ctx (ctx, r | _FPU_DOUBLE);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdsetround_387_prec_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  fpu_control_t cw, new_cw;
ce426f
+
ce426f
+  _FPU_GETCW (cw);
ce426f
+  new_cw = cw;
ce426f
+  new_cw &= ~(_FPU_RC_ZERO | _FPU_EXTENDED);
ce426f
+  new_cw |= r;
ce426f
+
ce426f
+  ctx->env.__control_word = cw;
ce426f
+  if (__glibc_unlikely (new_cw != cw))
ce426f
+    {
ce426f
+      _FPU_SETCW (new_cw);
ce426f
+      ctx->updated_status = true;
ce426f
+    }
ce426f
+  else
ce426f
+    ctx->updated_status = false;
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdsetround_387_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  libc_feholdsetround_387_prec_ctx (ctx, r | _FPU_EXTENDED);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdsetround_387_53bit_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  libc_feholdsetround_387_prec_ctx (ctx, r | _FPU_DOUBLE);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feholdsetround_sse_ctx (struct rm_ctx *ctx, int r)
ce426f
+{
ce426f
+  unsigned int mxcsr, new_mxcsr;
ce426f
+
ce426f
+  asm (STMXCSR " %0" : "=m" (*&mxcsr));
ce426f
+  new_mxcsr = (mxcsr & ~0x6000) | (r << 3);
ce426f
+
ce426f
+  ctx->env.__mxcsr = mxcsr;
ce426f
+  if (__glibc_unlikely (new_mxcsr != mxcsr))
ce426f
+    {
ce426f
+      asm volatile (LDMXCSR " %0" : : "m" (*&new_mxcsr));
ce426f
+      ctx->updated_status = true;
ce426f
+    }
ce426f
+  else
ce426f
+    ctx->updated_status = false;
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feresetround_sse_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  if (__glibc_unlikely (ctx->updated_status))
ce426f
+    libc_feresetround_sse (&ctx->env);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feresetround_387_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  if (__glibc_unlikely (ctx->updated_status))
ce426f
+    _FPU_SETCW (ctx->env.__control_word);
ce426f
+}
ce426f
+
ce426f
+static __always_inline void
ce426f
+libc_feupdateenv_387_ctx (struct rm_ctx *ctx)
ce426f
+{
ce426f
+  if (__glibc_unlikely (ctx->updated_status))
ce426f
+    libc_feupdateenv_test_387 (&ctx->env, 0);
ce426f
+}
ce426f
+
ce426f
+#ifdef __SSE_MATH__
ce426f
+# define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_sse_ctx
ce426f
+# define libc_fesetenvf_ctx		libc_fesetenv_sse_ctx
ce426f
+# define libc_feupdateenvf_ctx		libc_feupdateenv_sse_ctx
ce426f
+# define libc_feholdsetroundf_ctx	libc_feholdsetround_sse_ctx
ce426f
+# define libc_feresetroundf_ctx		libc_feresetround_sse_ctx
ce426f
+#else
ce426f
+# define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_387_ctx
ce426f
+# define libc_feupdateenvf_ctx		libc_feupdateenv_387_ctx
ce426f
+# define libc_feholdsetroundf_ctx	libc_feholdsetround_387_ctx
ce426f
+# define libc_feresetroundf_ctx		libc_feresetround_387_ctx
ce426f
+#endif /* __SSE_MATH__ */
ce426f
+
ce426f
+#ifdef __SSE2_MATH__
ce426f
+# define libc_feholdexcept_setround_ctx	libc_feholdexcept_setround_sse_ctx
ce426f
+# define libc_fesetenv_ctx		libc_fesetenv_sse_ctx
ce426f
+# define libc_feupdateenv_ctx		libc_feupdateenv_sse_ctx
ce426f
+# define libc_feholdsetround_ctx	libc_feholdsetround_sse_ctx
ce426f
+# define libc_feresetround_ctx		libc_feresetround_sse_ctx
ce426f
+#else
ce426f
+# define libc_feholdexcept_setround_ctx	libc_feholdexcept_setround_387_ctx
ce426f
+# define libc_feupdateenv_ctx		libc_feupdateenv_387_ctx
ce426f
+# define libc_feresetround_ctx		libc_feresetround_387_ctx
ce426f
+#endif /* __SSE2_MATH__ */
ce426f
+
ce426f
+#define libc_feholdexcept_setroundl_ctx	libc_feholdexcept_setround_387_ctx
ce426f
+#define libc_feupdateenvl_ctx		libc_feupdateenv_387_ctx
ce426f
+#define libc_feholdsetroundl_ctx	libc_feholdsetround_387_ctx
ce426f
+#define libc_feresetroundl_ctx		libc_feresetround_387_ctx
ce426f
+
ce426f
+#ifndef __SSE2_MATH__
ce426f
+# define libc_feholdsetround_53bit_ctx	libc_feholdsetround_387_53bit_ctx
ce426f
+# define libc_feresetround_53bit_ctx	libc_feresetround_387_ctx
ce426f
+#endif
ce426f
+
ce426f
 #undef __mxcsr
ce426f
 
ce426f
 #endif /* FENV_PRIVATE_H */