Skip to content

Conversation

@madebr
Copy link
Contributor

@madebr madebr commented Apr 3, 2025

I think include/SDL3/SDL_intrin.h should get a similar makeover.

Copy link
Contributor

@sezero sezero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked OK to be after brief eyeballing.

@madebr
Copy link
Contributor Author

madebr commented Apr 3, 2025

This actions run behaves as expected now: https://github.com/libsdl-org/SDL/actions/runs/14251280259/job/39944568381?pr=12731

  • it uses -DCMAKE_OSX_ARCHITECTURES=x86_64;arm64
  • all simd extensions (mmx, sse, avx, neon) are supported, so no #define SDL_DISABLE_xxx's in the generated SDL_build_config.h

@madebr
Copy link
Contributor Author

madebr commented Apr 3, 2025

An Apple person should check whether the SDL_intrin.h behaves as expected.
The header would need this, and perhaps additional changes.

--- a/include/SDL3/SDL_intrin.h
+++ b/include/SDL3/SDL_intrin.h
@@ -356,7 +356,11 @@ _m_prefetch(void *__P)
 # endif
 #endif
 
-#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
+#ifdef __APPLE__
+#include "TargetConditionals.h"
+#endif
+
+#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) || (defined(TARGET_CPU_X86) && TARGET_CPU_X86) || (defined(TARGET_CPU_X86_64) && TARGET_CPU_X86_64)
 # if ((defined(_MSC_VER) && !defined(_M_X64)) || defined(__MMX__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_MMX)
 #  define SDL_MMX_INTRINSICS 1
 #  include <mmintrin.h>

@slouken slouken added this to the 3.4.0 milestone Apr 3, 2025
@madebr madebr marked this pull request as draft April 3, 2025 19:54
@sezero
Copy link
Contributor

sezero commented Apr 3, 2025

An Apple person should check whether the SDL_intrin.h behaves as expected. The header would need this, and perhaps additional changes.

--- a/include/SDL3/SDL_intrin.h
+++ b/include/SDL3/SDL_intrin.h
@@ -356,7 +356,11 @@ _m_prefetch(void *__P)
 # endif
 #endif
 
-#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
+#ifdef __APPLE__
+#include "TargetConditionals.h"
+#endif
+
+#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) || (defined(TARGET_CPU_X86) && TARGET_CPU_X86) || (defined(TARGET_CPU_X86_64) && TARGET_CPU_X86_64)
 # if ((defined(_MSC_VER) && !defined(_M_X64)) || defined(__MMX__) || defined(SDL_HAS_TARGET_ATTRIBS)) && !defined(SDL_DISABLE_MMX)
 #  define SDL_MMX_INTRINSICS 1
 #  include <mmintrin.h>

I don't think this is necessary: TARGET_CPU_X86, for, e.g., is defined to 1 if __i386__ is defined (otherwise to 0). Similarly, TARGET_CPU_X86_64 is defined to 1 if __x86_64__ is defined (otherwise to 0). In short, we are already covering those cases here. Am I missing something?

@madebr madebr force-pushed the cmake-apple-fat-simd branch from be99236 to 7c9a6c9 Compare April 3, 2025 20:01
@madebr
Copy link
Contributor Author

madebr commented Apr 3, 2025

I don't think this is necessary: TARGET_CPU_X86, for, e.g., is defined to 1 if __i386__ is defined (otherwise to 0). Similarly, TARGET_CPU_X86_64 is defined to 1 if __x86_64__ is defined (otherwise to 0). In short, we are already covering those cases here. Am I missing something?

I think we're fine. After thinking about it, CMake was failing because we were including <xmmintrin.h> when targeting arm64.

@madebr madebr marked this pull request as ready for review April 3, 2025 20:15
# if test_enabled
${MAIN_BODY}
# endif
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading this correctly, this x86 test will succeed for aarch64-apple (non-x86.) Similarly, the arm test below will succeed for x86_64-apple (non-arm.) Is this what you really intend?

Copy link
Contributor Author

@madebr madebr Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions will only get called if cmake is targeting x64/x86.
I'll add a fail-safe that returns false when the cpu is not x86 (and vice verse for the arm function)

@sezero sezero self-requested a review April 4, 2025 20:45
@sezero
Copy link
Contributor

sezero commented Aug 31, 2025

This was forgotten??

Patches 1 and 3 are irrelevant here: They should be applied (and cherry-picked to 3.2.x)

As for patch/2 which matters: It needs rebasing. But is the following simpler / more straight-forward for it?

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e7ae6f2..3ce5df9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -679,6 +679,9 @@ if(SDL_ASSEMBLY)
   set(HAVE_ASSEMBLY TRUE)
 
   if(SDL_MMX)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_MMX TRUE)
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -mmmx")
@@ -698,8 +701,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_MMX)
       set(HAVE_MMX TRUE)
     endif()
+   endif()
   endif()
   if(SDL_SSE)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_SSE TRUE)
+    message("MMX enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -msse")
@@ -719,8 +727,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_SSE)
       set(HAVE_SSE TRUE)
     endif()
+   endif()
   endif()
   if(SDL_SSE2)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_SSE2 TRUE)
+    message("SSE2 enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -msse2")
@@ -740,8 +753,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_SSE2)
       set(HAVE_SSE2 TRUE)
     endif()
+   endif()
   endif()
   if(SDL_SSE3)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_SSE3 TRUE)
+    message("SSE3 enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -msse3")
@@ -762,7 +780,12 @@ if(SDL_ASSEMBLY)
       set(HAVE_SSE3 TRUE)
     endif()
   endif()
+  endif()
   if(SDL_SSE4_1)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_SSE4_1 TRUE)
+    message("SSE4.1 enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -msse4.1")
@@ -782,8 +805,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_SSE4_1)
       set(HAVE_SSE4_1 TRUE)
     endif()
+   endif()
   endif()
   if(SDL_SSE4_2)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_SSE4_2 TRUE)
+    message("SSE4.2 enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -msse4.2")
@@ -800,8 +828,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_SSE4_2)
       set(HAVE_SSE4_2 TRUE)
     endif()
+   endif()
   endif()
   if(SDL_AVX)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_AVX TRUE)
+    message("AVX enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -mavx")
@@ -821,8 +854,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_AVX)
       set(HAVE_AVX TRUE)
     endif()
+   endif()
   endif()
   if(SDL_AVX2)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_AVX2 TRUE)
+    message("AVX2 enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -mavx2")
@@ -842,8 +880,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_AVX2)
       set(HAVE_AVX2 TRUE)
     endif()
+   endif()
   endif()
   if(SDL_AVX512F)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_AVX512F TRUE)
+    message("AVX512F enabled for Apple multi-arch config")
+   else()
     cmake_push_check_state()
     if(USE_GCC OR USE_CLANG OR USE_INTELCC)
       string(APPEND CMAKE_REQUIRED_FLAGS " -mavx512f")
@@ -863,9 +906,13 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_AVX512F)
       set(HAVE_AVX512F TRUE)
     endif()
+   endif()
   endif()
 
   if(SDL_ARMNEON)
+   if(APPLE AND SDL_CPU_X64 AND SDL_CPU_ARM64)
+    set(HAVE_ARMNEON TRUE)
+   else()
     check_c_source_compiles("
         #include <arm_neon.h>
         void floats_add(float *dest, float *a, float *b, unsigned size) {
@@ -881,6 +928,7 @@ if(SDL_ASSEMBLY)
     if(COMPILER_SUPPORTS_ARMNEON)
       set(HAVE_ARMNEON TRUE)
     endif()
+   endif()
   endif()
 
   if(USE_GCC OR USE_CLANG)

@sezero
Copy link
Contributor

sezero commented Aug 31, 2025

See #13839 for an alternative to patch/2

sezero added a commit to sezero/SDL that referenced this pull request Aug 31, 2025
@madebr madebr force-pushed the cmake-apple-fat-simd branch from 7c9a6c9 to f9b8955 Compare August 31, 2025 17:40
sezero added a commit to sezero/SDL that referenced this pull request Aug 31, 2025
Copy link
Contributor

@sezero sezero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks in Apple multi-arch configs are pointless IMO.

Besides the NEON check seems to always fail...

 expands to <nothing> on e.g. Windows, which will be interpreted as true by cmake_dependent_option.
@madebr madebr force-pushed the cmake-apple-fat-simd branch from f9b8955 to 731311f Compare August 31, 2025 19:57
@madebr
Copy link
Contributor Author

madebr commented Aug 31, 2025

Checks in Apple multi-arch configs are pointless IMO.

The errors were because of a copy/paste error (using TARGET_CPU_X86 || TARGET_CPU_X86_64 for arm).

Besides the NEON check seems to always fail...

I'd prefer to keep the tests, and let the "fast" things move to cmake/PreseedXXXCache.cmake

@madebr madebr force-pushed the cmake-apple-fat-simd branch from 731311f to bdf2486 Compare August 31, 2025 20:06
@sezero
Copy link
Contributor

sezero commented Aug 31, 2025

Checks in Apple multi-arch configs are pointless IMO.

I'd prefer to keep the tests, and let the "fast" things move to cmake/PreseedXXXCache.cmake

OK, we won't agree :)

I'll defer to Sam's judgement.

@sezero
Copy link
Contributor

sezero commented Sep 2, 2025

Now that Sam approved this one, I'd like to close #13839.
Is the following good on top of your patch?

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9591315..f8bb29d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -80,6 +80,12 @@ include("${SDL3_SOURCE_DIR}/cmake/PreseedNokiaNGageCache.cmake")
 
 SDL_DetectCompiler()
 SDL_DetectTargetCPUArchitectures(SDL_CPUS)
+if(APPLE AND CMAKE_OSX_ARCHITECTURES)
+  list(LENGTH CMAKE_OSX_ARCHITECTURES _num_arches)
+  if(_num_arches GREATER 1)
+    set(APPLE_MULTIARCH TRUE)
+  endif()
+endif()
 
 # Increment this if there is an incompatible change - but if that happens,
 # we should rename the library from SDL3 to SDL4, at which point this would
diff --git a/cmake/sdlcompilers.cmake b/cmake/sdlcompilers.cmake
index f4e43b7..af80a8e 100644
--- a/cmake/sdlcompilers.cmake
+++ b/cmake/sdlcompilers.cmake
@@ -165,20 +165,19 @@ function(check_x86_source_compiles BODY VAR)
   if(ARGN)
     message(FATAL_ERROR "Unknown arguments: ${ARGN}")
   endif()
-  if(APPLE AND DEFINED CMAKE_OSX_ARCHITECTURES)
+  if(APPLE_MULTIARCH AND (SDL_CPU_X86 OR SDL_CPU_X64))
     set(test_conditional 1)
   else()
     set(test_conditional 0)
   endif()
   check_c_source_compiles("
     #if ${test_conditional}
-    # include \"TargetConditionals.h\"
-    # if TARGET_CPU_X86 || TARGET_CPU_X86_64
+    # if defined(__i386__) || defined(__x86_64__)
     #  define test_enabled 1
     # else
-    #  define test_enabled 0
+    #  define test_enabled 0 /* feign success in Apple multi-arch configs */
     # endif
-    #else
+    #else                    /* test normally */
     # define test_enabled 1
     #endif
     #if test_enabled
@@ -196,20 +195,19 @@ function(check_arm_source_compiles BODY VAR)
   if(ARGN)
     message(FATAL_ERROR "Unknown arguments: ${ARGN}")
   endif()
-  if(APPLE AND DEFINED CMAKE_OSX_ARCHITECTURES)
+  if(APPLE_MULTIARCH AND (SDL_CPU_ARM32 OR SDL_CPU_ARM64))
     set(test_conditional 1)
   else()
     set(test_conditional 0)
   endif()
   check_c_source_compiles("
     #if ${test_conditional}
-    # include \"TargetConditionals.h\"
-    # if TARGET_CPU_ARM || TARGET_CPU_ARM64
+    # if defined(__arm__) || defined(__aarch64__)
     #  define test_enabled 1
     # else
-    #  define test_enabled 0
+    #  define test_enabled 0 /* feign success in Apple multi-arch configs */
     # endif
-    #else
+    #else                    /* test normally */
     # define test_enabled 1
     #endif
     #if test_enabled

@madebr madebr merged commit c0fb71f into libsdl-org:main Sep 2, 2025
41 of 42 checks passed
@madebr
Copy link
Contributor Author

madebr commented Sep 2, 2025

Now that Sam approved this one, I'd like to close #13839. Is the following good on top of your patch?

Makes total sense to me. Feel free to apply (and while you're at it, also cherry-pick this one to the 3.2.x branch).

@madebr madebr deleted the cmake-apple-fat-simd branch September 2, 2025 19:30
@sezero
Copy link
Contributor

sezero commented Sep 2, 2025

Makes total sense to me. Feel free to apply

Applied as 83bb0f9

(and while you're at it, also cherry-pick this one to the 3.2.x branch).

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants