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