1# ============================================================================
2#  https://www.gnu.org/software/autoconf-archive/ax_gcc_x86_cpu_supports.html
3# ============================================================================
4#
5# SYNOPSIS
6#
7#   AX_GCC_X86_CPU_SUPPORTS(X86-INSTRUCTION-SET,
8#     [ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
9#
10# DESCRIPTION
11#
12#   Checks if the host cpu supports X86-INSTRUCTION-SET. The instruction set
13#   that can be tested are "mmx, popcnt, sse, sse2, sse3, sse4.1, sse4.2,
14#   sse4a, avx, avx2, avx512f, fma, fma4, bmi, bmi2". If the instruction set
15#   is supported by the host cpu, the C preprocessor macro
16#   HAVE_XXX_INSTRUCTIONS is set to 1. The XXX is up-cased instruction case
17#   with dot replaced by underscore. For example, the test for "sse4.2"
18#   would export HAVE_SSE4_2_INSTRUCTIONS=1. This macro requires gcc
19#   extended builtin function "__builtin_cpu_init" and
20#   "__builtin_cpu_supports" to detect the cpu features. It will error out
21#   if the compiler doesn't has these builtins.
22#
23#   If the test for the instruction set succeeded, the hook ACTION-IF-FOUND
24#   would run. Otherwise the hook ACTION-IF-NOT-FOUND would run if
25#   specified.
26#
27#   See also AX_CHECK_X86_FEATURES, which checks all the possible
28#   instruction set and export the corresponding CFLAGS.
29#
30# LICENSE
31#
32#   Copyright (c) 2016 Felix Chern <idryman@gmail.com>
33#   Copyright (c) 2017 Joseph Benden <joe@benden.us>
34#
35#   This program is free software; you can redistribute it and/or modify it
36#   under the terms of the GNU General Public License as published by the
37#   Free Software Foundation; either version 2 of the License, or (at your
38#   option) any later version.
39#
40#   This program is distributed in the hope that it will be useful, but
41#   WITHOUT ANY WARRANTY; without even the implied warranty of
42#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
43#   Public License for more details.
44#
45#   You should have received a copy of the GNU General Public License along
46#   with this program. If not, see <https://www.gnu.org/licenses/>.
47#
48#   As a special exception, the respective Autoconf Macro's copyright owner
49#   gives unlimited permission to copy, distribute and modify the configure
50#   scripts that are the output of Autoconf when processing the Macro. You
51#   need not follow the terms of the GNU General Public License when using
52#   or distributing such scripts, even though portions of the text of the
53#   Macro appear in them. The GNU General Public License (GPL) does govern
54#   all other use of the material that constitutes the Autoconf Macro.
55#
56#   This special exception to the GPL applies to versions of the Autoconf
57#   Macro released by the Autoconf Archive. When you make and distribute a
58#   modified version of the Autoconf Macro, you may extend this special
59#   exception to the GPL to apply to your modified version as well.
60
61#serial 4
62
63AC_DEFUN_ONCE([_AX_GCC_X86_CPU_INIT],
64 [AC_LANG_PUSH([C])
65  AC_CACHE_CHECK([for gcc __builtin_cpu_init function],
66    [ax_cv_gcc_check_x86_cpu_init],
67    [AC_RUN_IFELSE(
68      [AC_LANG_PROGRAM([#include <stdlib.h>],
69        [__builtin_cpu_init ();])
70      ],
71      [ax_cv_gcc_check_x86_cpu_init=yes],
72      [ax_cv_gcc_check_x86_cpu_init=no],[ax_cv_gcc_check_x86_cpu_init=no])])
73])
74
75AC_DEFUN([AX_GCC_X86_CPU_SUPPORTS],
76  [AC_REQUIRE([AC_PROG_CC])
77   AC_REQUIRE([_AX_GCC_X86_CPU_INIT])
78   AC_LANG_PUSH([C])
79   AS_VAR_PUSHDEF([gcc_x86_feature], [AS_TR_SH([ax_cv_gcc_x86_cpu_supports_$1])])
80   AC_CACHE_CHECK([for x86 $1 instruction support],
81     [gcc_x86_feature],
82     [AC_RUN_IFELSE(
83       [AC_LANG_PROGRAM([
84         #include <stdlib.h>
85         #if defined(__INTEL_COMPILER)
86         #include <immintrin.h>
87         #endif
88       ],[
89         #if defined(__INTEL_COMPILER)
90         int result = 0;
91
92         if (!strcmp("$1", "avx2"))
93           result = _may_i_use_cpu_feature(_FEATURE_AVX2);
94         else if (!strcmp("$1", "avx"))
95           result = _may_i_use_cpu_feature(_FEATURE_AVX);
96         else if (!strcmp("$1", "sse2"))
97           result = _may_i_use_cpu_feature(_FEATURE_SSE2);
98         else if (!strcmp("$1", "mmx"))
99           result = _may_i_use_cpu_feature(_FEATURE_MMX);
100
101         return result ? 0 : 1;
102         #else
103         #if defined(__GNUC__) && !defined(__clang__)
104         __builtin_cpu_init ();
105         #endif
106         if (__builtin_cpu_supports("$1"))
107           return 0;
108         return 1;
109         #endif
110        ])],
111        [gcc_x86_feature=yes],
112        [gcc_x86_feature=no],
113        [gcc_x86_feature=no]
114     )]
115   )
116   AC_LANG_POP([C])
117   AS_VAR_IF([gcc_x86_feature],[yes],
118         [AC_DEFINE(
119           AS_TR_CPP([HAVE_$1_INSTRUCTIONS]),
120           [1],
121           [Define if $1 instructions are supported])
122          $2],
123          [$3]
124         )
125   AS_VAR_POPDEF([gcc_x86_feature])
126])
127