[PATCH] Add configure option to force enable 'soft' HW feature bits

Jussi Kivilinna jussi.kivilinna at iki.fi
Thu Jan 21 21:30:14 CET 2021


* configure.ac (force_soft_hwfeatures)
(ENABLE_FORCE_SOFT_HWFEATURES): New.
* src/hwf-x86.c (detect_x86_gnuc): Enable HWF_INTEL_FAST_SHLD
and HWF_INTEL_FAST_VPGATHER if ENABLE_FORCE_SOFT_HWFEATURES enabled.
--

Patch allows enabling HW features, that are fast only select CPU models,
on all CPUs. For example, SHLD instruction is fast on only select Intel
processors and should not be used on others. This configuration option
allows enabling these 'soft' HW features for testing purposes on all
CPUs.

Current 'soft' HW features are:
 - "intel-fast-shld": supported by all x86 (but very slow on most)
 - "intel-fast-vpgather": supported by all x86 with AVX2 (but slow on
   most)

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 configure.ac  | 14 ++++++++++++++
 src/hwf-x86.c | 23 +++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/configure.ac b/configure.ac
index ce17d9f4..8aba8ece 100644
--- a/configure.ac
+++ b/configure.ac
@@ -566,6 +566,15 @@ AC_ARG_ENABLE(large-data-tests,
 AC_MSG_RESULT($large_data_tests)
 AC_SUBST(RUN_LARGE_DATA_TESTS, $large_data_tests)
 
+# Implementation of --enable-force-soft-hwfeatures
+AC_MSG_CHECKING([whether 'soft' HW feature bits are forced on])
+AC_ARG_ENABLE([force-soft-hwfeatures],
+              AS_HELP_STRING([--enable-force-soft-hwfeatures],
+                             [Enable forcing 'soft' HW feature bits on]),
+              [force_soft_hwfeatures=$enableval],
+              [force_soft_hwfeatures=no])
+AC_MSG_RESULT($force_soft_hwfeatures)
+
 
 # Implementation of the --with-capabilities switch.
 # Check whether we want to use Linux capabilities
@@ -2434,6 +2443,11 @@ if test x"$drngsupport" = xyes ; then
 fi
 
 
+if test x"$force_soft_hwfeatures" = xyes ; then
+  AC_DEFINE(ENABLE_FORCE_SOFT_HWFEATURES, 1,
+            [Enable forcing 'soft' HW feature bits on (for testing).])
+fi
+
 # Define conditional sources and config.h symbols depending on the
 # selected ciphers, pubkey-ciphers, digests, kdfs, and random modules.
 
diff --git a/src/hwf-x86.c b/src/hwf-x86.c
index 796e874f..9a9ed6d3 100644
--- a/src/hwf-x86.c
+++ b/src/hwf-x86.c
@@ -301,6 +301,29 @@ detect_x86_gnuc (void)
       avoid_vpgather |= 1;
     }
 
+#ifdef ENABLE_FORCE_SOFT_HWFEATURES
+  /* Soft HW features mark functionality that is available on all systems
+   * but not feasible to use because of slow HW implementation. */
+
+  /* SHLD is faster at rotating register than actual ROR/ROL instructions
+   * on older Intel systems (~sandy-bridge era). However, SHLD is very
+   * slow on almost anything else and later Intel processors have faster
+   * ROR/ROL. Therefore in regular build HWF_INTEL_FAST_SHLD is enabled
+   * only for those Intel processors that benefit from the SHLD
+   * instruction. Enabled here unconditionally as requested. */
+  result |= HWF_INTEL_FAST_SHLD;
+
+  /* VPGATHER instructions are used for look-up table based
+   * implementations which require VPGATHER to be fast enough to beat
+   * regular parallelized look-up table implementations (see Twofish).
+   * So far, only Intel processors beginning with skylake have had
+   * VPGATHER fast enough to be enabled. AMD Zen3 comes close to
+   * being feasible, but not quite (where twofish-avx2 is few percent
+   * slower than twofish-3way). Enable VPGATHER here unconditionally
+   * as requested. */
+  avoid_vpgather = 0;
+#endif
+
 #ifdef ENABLE_PCLMUL_SUPPORT
   /* Test bit 1 for PCLMUL.  */
   if (features & 0x00000002)
-- 
2.27.0




More information about the Gcrypt-devel mailing list