From da331134263fc6ac16d0de15ffb2a2872b1a1da8 Mon Sep 17 00:00:00 2001
From: Marek Vavrusa <marek.vavrusa@nic.cz>
Date: Tue, 9 Aug 2011 15:59:04 +0200
Subject: [PATCH] Support for detecting CPU extensions.

refs #631
---
 configure.ac                  |   1 +
 m4/ax_check_compiler_flags.m4 |  78 ++++++++++++++++++++++
 m4/ax_ext.m4                  | 122 ++++++++++++++++++++++++++++++++++
 m4/ax_gcc_x86_cpuid.m4        |  65 ++++++++++++++++++
 4 files changed, 266 insertions(+)
 create mode 100644 m4/ax_check_compiler_flags.m4
 create mode 100644 m4/ax_ext.m4
 create mode 100644 m4/ax_gcc_x86_cpuid.m4

diff --git a/configure.ac b/configure.ac
index b5298df0dd..d61ebf7faf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,6 +7,7 @@ AC_CONFIG_SRCDIR([src/knot/main.c])
 AC_CONFIG_HEADERS([src/config.h])
 AC_CONFIG_MACRO_DIR([m4])
 AC_USE_SYSTEM_EXTENSIONS([_GNU_SOURCE])
+AX_EXT
 
 # Enable maintainer mode by default for development
 AM_MAINTAINER_MODE([enable])
diff --git a/m4/ax_check_compiler_flags.m4 b/m4/ax_check_compiler_flags.m4
new file mode 100644
index 0000000000..05e5c3b72c
--- /dev/null
+++ b/m4/ax_check_compiler_flags.m4
@@ -0,0 +1,78 @@
+# ===========================================================================
+#        http://autoconf-archive.cryp.to/ax_check_compiler_flags.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_CHECK_COMPILER_FLAGS(FLAGS, [ACTION-SUCCESS], [ACTION-FAILURE])
+#
+# DESCRIPTION
+#
+#   Check whether the given compiler FLAGS work with the current language's
+#   compiler, or whether they give an error. (Warnings, however, are
+#   ignored.)
+#
+#   ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
+#   success/failure.
+#
+# LAST MODIFICATION
+#
+#   2008-04-12
+#
+# COPYLEFT
+#
+#   Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
+#   Copyright (c) 2008 Matteo Frigo
+#
+#   This program is free software: you can redistribute it and/or modify it
+#   under the terms of the GNU General Public License as published by the
+#   Free Software Foundation, either version 3 of the License, or (at your
+#   option) any later version.
+#
+#   This program is distributed in the hope that it will be useful, but
+#   WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+#   Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License along
+#   with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#   As a special exception, the respective Autoconf Macro's copyright owner
+#   gives unlimited permission to copy, distribute and modify the configure
+#   scripts that are the output of Autoconf when processing the Macro. You
+#   need not follow the terms of the GNU General Public License when using
+#   or distributing such scripts, even though portions of the text of the
+#   Macro appear in them. The GNU General Public License (GPL) does govern
+#   all other use of the material that constitutes the Autoconf Macro.
+#
+#   This special exception to the GPL applies to versions of the Autoconf
+#   Macro released by the Autoconf Macro Archive. When you make and
+#   distribute a modified version of the Autoconf Macro, you may extend this
+#   special exception to the GPL to apply to your modified version as well.
+
+AC_DEFUN([AX_CHECK_COMPILER_FLAGS],
+[AC_PREREQ(2.59) dnl for _AC_LANG_PREFIX
+AC_MSG_CHECKING([whether _AC_LANG compiler accepts $1])
+dnl Some hackery here since AC_CACHE_VAL can't handle a non-literal varname:
+AS_LITERAL_IF([$1],
+  [AC_CACHE_VAL(AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1), [
+      ax_save_FLAGS=$[]_AC_LANG_PREFIX[]FLAGS
+      _AC_LANG_PREFIX[]FLAGS="$1"
+      AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
+        AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=yes,
+        AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=no)
+      _AC_LANG_PREFIX[]FLAGS=$ax_save_FLAGS])],
+  [ax_save_FLAGS=$[]_AC_LANG_PREFIX[]FLAGS
+   _AC_LANG_PREFIX[]FLAGS="$1"
+   AC_COMPILE_IFELSE([AC_LANG_PROGRAM()],
+     eval AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=yes,
+     eval AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)=no)
+   _AC_LANG_PREFIX[]FLAGS=$ax_save_FLAGS])
+eval ax_check_compiler_flags=$AS_TR_SH(ax_cv_[]_AC_LANG_ABBREV[]_flags_$1)
+AC_MSG_RESULT($ax_check_compiler_flags)
+if test "x$ax_check_compiler_flags" = xyes; then
+	m4_default([$2], :)
+else
+	m4_default([$3], :)
+fi
+])dnl AX_CHECK_COMPILER_FLAGS
diff --git a/m4/ax_ext.m4 b/m4/ax_ext.m4
new file mode 100644
index 0000000000..41536f7fe1
--- /dev/null
+++ b/m4/ax_ext.m4
@@ -0,0 +1,122 @@
+# ===========================================================================
+#                 http://autoconf-archive.cryp.to/ax_ext.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_EXT
+#
+# DESCRIPTION
+#
+#   Find supported SIMD extensions by requesting cpuid. When an SIMD
+#   extension is found, the -m"simdextensionname" is added to SIMD_FLAGS
+#   (only if compilator support it) (ie : if "sse2" is available "-msse2" is
+#   added to SIMD_FLAGS)
+#
+#   This macro calls:
+#
+#     AC_SUBST(SIMD_FLAGS)
+#
+#   And defines:
+#
+#     HAVE_MMX / HAVE_SSE / HAVE_SSE2 / HAVE_SSE3 / HAVE_SSSE3
+#
+# LAST MODIFICATION
+#
+#   2008-04-12
+#   2009-04-23  Mark Asbach <markasbach@users.sourceforge.net<
+#               Renamed cache variables so they adhere naming convention
+#               Corrected M4 quoting for AX_CHECK_COMPILER_FLAGS
+#
+# COPYLEFT
+#
+#   Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved.
+
+AC_DEFUN([AX_EXT],
+[
+  AC_REQUIRE([AX_GCC_X86_CPUID])
+  
+  AX_GCC_X86_CPUID([0x00000001])
+  if test "$ax_cv_gcc_x86_cpuid_0x00000001" != "unknown"; then
+    ecx=`echo $ax_cv_gcc_x86_cpuid_0x00000001 | cut -d ":" -f 3`
+    edx=`echo $ax_cv_gcc_x86_cpuid_0x00000001 | cut -d ":" -f 4`
+  fi
+  
+  AC_CACHE_CHECK([whether mmx is supported], [ax_cv_have_mmx_ext],
+  [
+    ax_cv_have_mmx_ext=no
+    if test "$ax_cv_gcc_x86_cpuid_0x00000001" != "unknown"; then
+      if test "$((0x$edx>>23&0x01))" = 1; then
+        ax_cv_have_mmx_ext=yes
+      fi
+    fi
+  ])
+
+  AC_CACHE_CHECK([whether sse is supported], [ax_cv_have_sse_ext],
+  [
+    ax_cv_have_sse_ext=no
+    if test "$ax_cv_gcc_x86_cpuid_0x00000001" != "unknown"; then
+      if test "$((0x$edx>>25&0x01))" = 1; then
+        ax_cv_have_sse_ext=yes
+      fi
+    fi
+  ])
+
+  AC_CACHE_CHECK([whether sse2 is supported], [ax_cv_have_sse2_ext],
+  [
+    ax_cv_have_sse2_ext=no
+    if test "$((0x$edx>>26&0x01))" = 1; then
+      ax_cv_have_sse2_ext=yes
+    fi
+  ])
+
+  AC_CACHE_CHECK([whether sse3 is supported], [ax_cv_have_sse3_ext],
+  [
+    ax_cv_have_sse3_ext=no
+    if test "$ax_cv_gcc_x86_cpuid_0x00000001" != "unknown"; then
+      if test "$((0x$ecx&0x01))" = 1; then
+        ax_cv_have_sse3_ext=yes
+      fi
+    fi
+  ])
+
+  AC_CACHE_CHECK([whether ssse3 is supported], [ax_cv_have_ssse3_ext],
+  [
+    ax_cv_have_ssse3_ext=no
+    if test "$ax_cv_gcc_x86_cpuid_0x00000001" != "unknown"; then
+      if test "$((0x$ecx>>9&0x01))" = 1; then
+        ax_cv_have_ssse3_ext=yes
+      fi
+    fi
+  ])
+
+  if test "$ax_cv_have_mmx_ext" = yes; then
+    AC_DEFINE(HAVE_MMX,,[Support mmx instructions])
+    AX_CHECK_COMPILER_FLAGS([-mmmx], [SIMD_FLAGS="$SIMD_FLAGS -mmmx"], [])
+  fi
+
+  if test "$ax_cv_have_sse_ext" = yes; then
+    AC_DEFINE(HAVE_SSE,,[Support SSE (Streaming SIMD Extensions) instructions])
+    AX_CHECK_COMPILER_FLAGS([-msse], [SIMD_FLAGS="$SIMD_FLAGS -msse"], [])
+  fi
+
+  if test "$ax_cv_have_sse2_ext" = yes; then
+    AC_DEFINE(HAVE_SSE2,,[Support SSE2 (Streaming SIMD Extensions 2) instructions])
+    AX_CHECK_COMPILER_FLAGS([-msse2], [SIMD_FLAGS="$SIMD_FLAGS -msse2"], [])
+  fi
+
+  if test "$ax_cv_have_sse3_ext" = yes; then
+    AC_DEFINE(HAVE_SSE3,,[Support SSE3 (Streaming SIMD Extensions 3) instructions])
+    AX_CHECK_COMPILER_FLAGS([-msse3], [SIMD_FLAGS="$SIMD_FLAGS -msse3"], [])
+  fi
+
+  if test "$ax_cv_have_ssse3_ext" = yes; then
+    AC_DEFINE(HAVE_SSSE3,,[Support SSSE3 (Supplemental Streaming SIMD Extensions 3) instructions])
+  fi
+
+  AC_SUBST(SIMD_FLAGS)
+])
diff --git a/m4/ax_gcc_x86_cpuid.m4 b/m4/ax_gcc_x86_cpuid.m4
new file mode 100644
index 0000000000..e9231b8141
--- /dev/null
+++ b/m4/ax_gcc_x86_cpuid.m4
@@ -0,0 +1,65 @@
+dnl @synopsis AX_GCC_X86_CPUID(OP)
+dnl @summary run x86 cpuid instruction OP using gcc inline assembler
+dnl @category Misc
+dnl
+dnl On Pentium and later x86 processors, with gcc or a compiler that
+dnl has a compatible syntax for inline assembly instructions, run
+dnl a small program that executes the cpuid instruction with
+dnl input OP.  This can be used to detect the CPU type.
+dnl
+dnl On output, the values of the eax, ebx, ecx, and edx registers
+dnl are stored as hexadecimal strings as "eax:ebx:ecx:edx" in
+dnl the cache variable ax_cv_gcc_x86_cpuid_OP.
+dnl
+dnl If the cpuid instruction fails (because you are running a cross-compiler,
+dnl or because you are not using gcc, or because you are on a processor
+dnl that doesn't have this instruction), ax_cv_gcc_x86_cpuid_OP is set
+dnl to the string "unknown".
+dnl
+dnl This macro mainly exists to be used in AX_GCC_ARCHFLAG.
+dnl
+dnl @version 2008-12-06
+dnl @license GPLWithACException
+dnl @author Steven G. Johnson <stevenj@alum.mit.edu> and Matteo Frigo.
+AC_DEFUN([AX_GCC_X86_CPUID],
+[AC_REQUIRE([AC_PROG_CC])
+AC_LANG_PUSH([C])
+AC_CACHE_CHECK([for x86 cpuid $1 output], [ax_cv_gcc_x86_cpuid_$1],
+ [AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>], [
+     int op = $1, eax, ebx, ecx, edx;
+     FILE *f;
+#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
+     __asm__("push %%rbx\n\t"
+             "cpuid\n\t"
+             "pop %%rbx"
+             : "=a" (eax), "=c" (ecx), "=d" (edx)
+             : "a" (op));
+     __asm__("push %%rbx\n\t"
+             "cpuid\n\t"
+             "mov %%rbx, %%rax\n\t"
+             "pop %%rbx"
+             : "=a" (ebx), "=c" (ecx), "=d" (edx)
+             : "a" (op));
+#else
+     __asm__("push %%ebx\n\t"
+             "cpuid\n\t"
+             "pop %%ebx"
+             : "=a" (eax), "=c" (ecx), "=d" (edx)
+             : "a" (op));
+     __asm__("push %%ebx\n\t"
+             "cpuid\n\t"
+             "mov %%ebx, %%eax\n\t"
+             "pop %%ebx"
+             : "=a" (ebx), "=c" (ecx), "=d" (edx)
+             : "a" (op));
+#endif
+     f = fopen("conftest_cpuid", "w"); if (!f) return 1;
+     fprintf(f, "%x:%x:%x:%x\n", eax, ebx, ecx, edx);
+     fclose(f);
+     return 0;
+])], 
+     [ax_cv_gcc_x86_cpuid_$1=`cat conftest_cpuid`; rm -f conftest_cpuid],
+     [ax_cv_gcc_x86_cpuid_$1=unknown; rm -f conftest_cpuid],
+     [ax_cv_gcc_x86_cpuid_$1=unknown])])
+AC_LANG_POP([C])
+])
-- 
GitLab