xref: /aosp_15_r20/external/libffi/m4/ax_gcc_x86_cpuid.m4 (revision 1fd5a2e1d639cd1ddf29dd0c484c123bbd850c21)
1*1fd5a2e1SPrashanth Swaminathan# ===========================================================================
2*1fd5a2e1SPrashanth Swaminathan#     https://www.gnu.org/software/autoconf-archive/ax_gcc_x86_cpuid.html
3*1fd5a2e1SPrashanth Swaminathan# ===========================================================================
4*1fd5a2e1SPrashanth Swaminathan#
5*1fd5a2e1SPrashanth Swaminathan# SYNOPSIS
6*1fd5a2e1SPrashanth Swaminathan#
7*1fd5a2e1SPrashanth Swaminathan#   AX_GCC_X86_CPUID(OP)
8*1fd5a2e1SPrashanth Swaminathan#   AX_GCC_X86_CPUID_COUNT(OP, COUNT)
9*1fd5a2e1SPrashanth Swaminathan#
10*1fd5a2e1SPrashanth Swaminathan# DESCRIPTION
11*1fd5a2e1SPrashanth Swaminathan#
12*1fd5a2e1SPrashanth Swaminathan#   On Pentium and later x86 processors, with gcc or a compiler that has a
13*1fd5a2e1SPrashanth Swaminathan#   compatible syntax for inline assembly instructions, run a small program
14*1fd5a2e1SPrashanth Swaminathan#   that executes the cpuid instruction with input OP. This can be used to
15*1fd5a2e1SPrashanth Swaminathan#   detect the CPU type. AX_GCC_X86_CPUID_COUNT takes an additional COUNT
16*1fd5a2e1SPrashanth Swaminathan#   parameter that gets passed into register ECX before calling cpuid.
17*1fd5a2e1SPrashanth Swaminathan#
18*1fd5a2e1SPrashanth Swaminathan#   On output, the values of the eax, ebx, ecx, and edx registers are stored
19*1fd5a2e1SPrashanth Swaminathan#   as hexadecimal strings as "eax:ebx:ecx:edx" in the cache variable
20*1fd5a2e1SPrashanth Swaminathan#   ax_cv_gcc_x86_cpuid_OP.
21*1fd5a2e1SPrashanth Swaminathan#
22*1fd5a2e1SPrashanth Swaminathan#   If the cpuid instruction fails (because you are running a
23*1fd5a2e1SPrashanth Swaminathan#   cross-compiler, or because you are not using gcc, or because you are on
24*1fd5a2e1SPrashanth Swaminathan#   a processor that doesn't have this instruction), ax_cv_gcc_x86_cpuid_OP
25*1fd5a2e1SPrashanth Swaminathan#   is set to the string "unknown".
26*1fd5a2e1SPrashanth Swaminathan#
27*1fd5a2e1SPrashanth Swaminathan#   This macro mainly exists to be used in AX_GCC_ARCHFLAG.
28*1fd5a2e1SPrashanth Swaminathan#
29*1fd5a2e1SPrashanth Swaminathan# LICENSE
30*1fd5a2e1SPrashanth Swaminathan#
31*1fd5a2e1SPrashanth Swaminathan#   Copyright (c) 2008 Steven G. Johnson <[email protected]>
32*1fd5a2e1SPrashanth Swaminathan#   Copyright (c) 2008 Matteo Frigo
33*1fd5a2e1SPrashanth Swaminathan#   Copyright (c) 2015 Michael Petch <[email protected]>
34*1fd5a2e1SPrashanth Swaminathan#
35*1fd5a2e1SPrashanth Swaminathan#   This program is free software: you can redistribute it and/or modify it
36*1fd5a2e1SPrashanth Swaminathan#   under the terms of the GNU General Public License as published by the
37*1fd5a2e1SPrashanth Swaminathan#   Free Software Foundation, either version 3 of the License, or (at your
38*1fd5a2e1SPrashanth Swaminathan#   option) any later version.
39*1fd5a2e1SPrashanth Swaminathan#
40*1fd5a2e1SPrashanth Swaminathan#   This program is distributed in the hope that it will be useful, but
41*1fd5a2e1SPrashanth Swaminathan#   WITHOUT ANY WARRANTY; without even the implied warranty of
42*1fd5a2e1SPrashanth Swaminathan#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
43*1fd5a2e1SPrashanth Swaminathan#   Public License for more details.
44*1fd5a2e1SPrashanth Swaminathan#
45*1fd5a2e1SPrashanth Swaminathan#   You should have received a copy of the GNU General Public License along
46*1fd5a2e1SPrashanth Swaminathan#   with this program. If not, see <https://www.gnu.org/licenses/>.
47*1fd5a2e1SPrashanth Swaminathan#
48*1fd5a2e1SPrashanth Swaminathan#   As a special exception, the respective Autoconf Macro's copyright owner
49*1fd5a2e1SPrashanth Swaminathan#   gives unlimited permission to copy, distribute and modify the configure
50*1fd5a2e1SPrashanth Swaminathan#   scripts that are the output of Autoconf when processing the Macro. You
51*1fd5a2e1SPrashanth Swaminathan#   need not follow the terms of the GNU General Public License when using
52*1fd5a2e1SPrashanth Swaminathan#   or distributing such scripts, even though portions of the text of the
53*1fd5a2e1SPrashanth Swaminathan#   Macro appear in them. The GNU General Public License (GPL) does govern
54*1fd5a2e1SPrashanth Swaminathan#   all other use of the material that constitutes the Autoconf Macro.
55*1fd5a2e1SPrashanth Swaminathan#
56*1fd5a2e1SPrashanth Swaminathan#   This special exception to the GPL applies to versions of the Autoconf
57*1fd5a2e1SPrashanth Swaminathan#   Macro released by the Autoconf Archive. When you make and distribute a
58*1fd5a2e1SPrashanth Swaminathan#   modified version of the Autoconf Macro, you may extend this special
59*1fd5a2e1SPrashanth Swaminathan#   exception to the GPL to apply to your modified version as well.
60*1fd5a2e1SPrashanth Swaminathan
61*1fd5a2e1SPrashanth Swaminathan#serial 10
62*1fd5a2e1SPrashanth Swaminathan
63*1fd5a2e1SPrashanth SwaminathanAC_DEFUN([AX_GCC_X86_CPUID],
64*1fd5a2e1SPrashanth Swaminathan[AX_GCC_X86_CPUID_COUNT($1, 0)
65*1fd5a2e1SPrashanth Swaminathan])
66*1fd5a2e1SPrashanth Swaminathan
67*1fd5a2e1SPrashanth SwaminathanAC_DEFUN([AX_GCC_X86_CPUID_COUNT],
68*1fd5a2e1SPrashanth Swaminathan[AC_REQUIRE([AC_PROG_CC])
69*1fd5a2e1SPrashanth SwaminathanAC_LANG_PUSH([C])
70*1fd5a2e1SPrashanth SwaminathanAC_CACHE_CHECK(for x86 cpuid $1 output, ax_cv_gcc_x86_cpuid_$1,
71*1fd5a2e1SPrashanth Swaminathan [AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <stdio.h>], [
72*1fd5a2e1SPrashanth Swaminathan     int op = $1, level = $2, eax, ebx, ecx, edx;
73*1fd5a2e1SPrashanth Swaminathan     FILE *f;
74*1fd5a2e1SPrashanth Swaminathan      __asm__ __volatile__ ("xchg %%ebx, %1\n"
75*1fd5a2e1SPrashanth Swaminathan        "cpuid\n"
76*1fd5a2e1SPrashanth Swaminathan        "xchg %%ebx, %1\n"
77*1fd5a2e1SPrashanth Swaminathan        : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
78*1fd5a2e1SPrashanth Swaminathan        : "a" (op), "2" (level));
79*1fd5a2e1SPrashanth Swaminathan
80*1fd5a2e1SPrashanth Swaminathan     f = fopen("conftest_cpuid", "w"); if (!f) return 1;
81*1fd5a2e1SPrashanth Swaminathan     fprintf(f, "%x:%x:%x:%x\n", eax, ebx, ecx, edx);
82*1fd5a2e1SPrashanth Swaminathan     fclose(f);
83*1fd5a2e1SPrashanth Swaminathan     return 0;
84*1fd5a2e1SPrashanth Swaminathan])],
85*1fd5a2e1SPrashanth Swaminathan     [ax_cv_gcc_x86_cpuid_$1=`cat conftest_cpuid`; rm -f conftest_cpuid],
86*1fd5a2e1SPrashanth Swaminathan     [ax_cv_gcc_x86_cpuid_$1=unknown; rm -f conftest_cpuid],
87*1fd5a2e1SPrashanth Swaminathan     [ax_cv_gcc_x86_cpuid_$1=unknown])])
88*1fd5a2e1SPrashanth SwaminathanAC_LANG_POP([C])
89*1fd5a2e1SPrashanth Swaminathan])
90