SDL: cmake: detect rdtsc in CMake

From 97c3077678803218a7516ae4e19933375b8c3ca3 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Wed, 22 Mar 2023 01:05:36 +0100
Subject: [PATCH] cmake: detect rdtsc in CMake

---
 CMakeLists.txt                                | 19 +++++++++++++++++++
 include/build_config/SDL_build_config.h.cmake |  1 +
 2 files changed, 20 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d1008efa4040..f43908f187d2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -370,6 +370,7 @@ set_option(SDL_ASSEMBLY            "Enable assembly routines" ${SDL_ASSEMBLY_DEF
 dep_option(SDL_AVX                 "Use AVX assembly routines" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
 dep_option(SDL_AVX2                "Use AVX2 assembly routines" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
 dep_option(SDL_AVX512              "Use AVX512 assembly routines" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
+dep_option(SDL_RDTSC               "Use RDTSC assembly routine(s)" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
 dep_option(SDL_SSE                 "Use SSE assembly routines" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
 dep_option(SDL_SSE2                "Use SSE2 assembly routines" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
 dep_option(SDL_SSE3                "Use SSE3 assembly routines" ON "SDL_ASSEMBLY;SDL_CPU_X86 OR SDL_CPU_X64" OFF)
@@ -714,6 +715,20 @@ endif()
 if(SDL_ASSEMBLY)
   set(HAVE_ASSEMBLY TRUE)
 
+  if(SDL_RDTSC)
+    check_c_source_compiles("
+      #if defined(_MSC_VER)
+      # include <intrin.h>
+      #else
+      # include <x86intrin.h>
+      #endif
+      int main(int argc, char *argv[]) {
+        return (int)__rdtsc();
+      }" COMPILER_SUPPORTS_RDTSC)
+    if(COMPILER_SUPPORTS_RDTSC)
+      set(HAVE_RDTSC TRUE)
+    endif()
+  endif()
   if(SDL_MMX)
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG)
@@ -1028,6 +1043,10 @@ if(SDL_ASSEMBLY)
   endif()
 endif()
 
+if(NOT HAVE_RDTSC)
+  set(SDL_DISABLE_RDTSC 1)
+endif()
+
 if(NOT HAVE_MMX)
   set(SDL_DISABLE_MMX 1)
 endif()
diff --git a/include/build_config/SDL_build_config.h.cmake b/include/build_config/SDL_build_config.h.cmake
index 3b7814ba4074..f10021683b84 100644
--- a/include/build_config/SDL_build_config.h.cmake
+++ b/include/build_config/SDL_build_config.h.cmake
@@ -585,6 +585,7 @@ typedef unsigned int uintptr_t;
 #endif /* !_STDINT_H_ && !HAVE_STDINT_H */
 
 /* Configure use of intrinsics */
+#cmakedefine SDL_DISABLE_RDTSC 1
 #cmakedefine SDL_DISABLE_SSE 1
 #cmakedefine SDL_DISABLE_SSE2 1
 #cmakedefine SDL_DISABLE_SSE3 1