From 0507bdb3b56083c8e8b011fc3d4c16fed2700340 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 29 Jan 2023 18:37:16 +0100
Subject: Retrieve CPU capabilities.

---
 src/common/Makefile.am |   3 ++
 src/common/cpu.c       | 100 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/common/cpu.h       |  50 +++++++++++++++++++++++++
 3 files changed, 153 insertions(+)
 create mode 100644 src/common/cpu.c
 create mode 100644 src/common/cpu.h

diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 638eee0..2f6fd6d 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -9,6 +9,7 @@ libcommon_la_SOURCES =					\
 	bits.h bits.c						\
 	compression.h compression.c			\
 	cpp.h								\
+	cpu.h cpu.c							\
 	dllist.h dllist.c					\
 	endianness.h endianness.c			\
 	environment.h environment.c			\
@@ -37,6 +38,8 @@ libcommon_la_SOURCES +=					\
 
 endif
 
+cpu.lo: CFLAGS += -mavx512f
+
 libcommon_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS)
 
 if BUILD_CURL_SUPPORT
diff --git a/src/common/cpu.c b/src/common/cpu.c
new file mode 100644
index 0000000..968def5
--- /dev/null
+++ b/src/common/cpu.c
@@ -0,0 +1,100 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * cpu.c - obtention d'indications de fonctionnalités liées au CPU
+ *
+ * Copyright (C) 2022 Cyrille Bagard
+ *
+ *  This file is part of Chrysalide.
+ *
+ *  Chrysalide 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.
+ *
+ *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "cpu.h"
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Indique les capacités de calculs parallèles anticipées.      *
+*                                                                             *
+*  Retour      : Fonctionnalités disponibles lors de la compilation.          *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+CPUSMIDFeature get_supported_cpu_smid_feature(void)
+{
+    CPUSMIDFeature result;                  /* Indications à retourner     */
+
+    result = CSF_NONE;
+
+    /**
+     * $ gcc -mavx512f -dM -E - < /dev/null | grep AVX
+     * #define __AVX512F__ 1
+     * #define __AVX__ 1
+     * #define __AVX2__ 1
+     */
+
+#ifdef __AVX2__
+    result |= CSF_AVX2;
+#endif
+
+#ifdef __AVX512F__
+    result |= CSF_AVX512;
+#endif
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Indique les capacités de calculs parallèles sollicitables.   *
+*                                                                             *
+*  Retour      : Fonctionnalités disponibles dans l'environnement.            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+CPUSMIDFeature get_avalaible_cpu_smid_feature(void)
+{
+    CPUSMIDFeature result;                  /* Indications à retourner     */
+
+    result = CSF_NONE;
+
+    /**
+     * Cf. Documentations suivantes :
+     *   - https://www.intel.com/content/dam/develop/external/us/en/documents/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family.pdf
+     *   - https://gcc.gnu.org/onlinedocs/gcc/x86-Built-in-Functions.html
+     */
+
+  __builtin_cpu_init();
+
+  if (__builtin_cpu_supports("ssse3"))
+      result |= CSF_AVX2;
+
+  if (__builtin_cpu_supports("avx512f"))
+      result |= CSF_AVX512;
+
+    return result;
+
+}
diff --git a/src/common/cpu.h b/src/common/cpu.h
new file mode 100644
index 0000000..58e53bd
--- /dev/null
+++ b/src/common/cpu.h
@@ -0,0 +1,50 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * cpu.h - prototypes pour l'obtention d'indications de fonctionnalités liées au CPU
+ *
+ * Copyright (C) 2022 Cyrille Bagard
+ *
+ *  This file is part of Chrysalide.
+ *
+ *  Chrysalide 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.
+ *
+ *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _COMMON_CPU_H
+#define _COMMON_CPU_H
+
+
+
+/* Indication de capacité de calculs parallèles */
+typedef enum _CPUSMIDFeature
+{
+    CSF_NONE   = (0 << 0),                  /* Absence d'indication        */
+
+    CSF_AVX2   = (1 << 0),                  /* Advanced Vector Extensions  */
+    CSF_AVX512 = (1 << 1),                  /* Advanced Vector Extensions  */
+
+    CSF_ALL    = ((1 << 2) - 1),
+
+} CPUSMIDFeature;
+
+
+/* Indique les capacités de calculs parallèles anticipées. */
+CPUSMIDFeature get_supported_cpu_smid_feature(void);
+
+/* Indique les capacités de calculs parallèles sollicitables. */
+CPUSMIDFeature get_avalaible_cpu_smid_feature(void);
+
+
+
+#endif  /* _COMMON_CPU_H */
-- 
cgit v0.11.2-87-g4458