From 81dd16ae08c67ab0cda9bbb8d064ed329d7eee84 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:01:30 +0100 Subject: [PATCH 1/9] desktop: make spin-waiting more power efficient --- src/desktop/backends/glfw2.c | 2 +- src/desktop/backends/glfw3.c | 2 +- src/desktop/backends/sdl1.c | 2 +- src/desktop/backends/sdl2.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/desktop/backends/glfw2.c b/src/desktop/backends/glfw2.c index 26c706ec..db1a8d20 100644 --- a/src/desktop/backends/glfw2.c +++ b/src/desktop/backends/glfw2.c @@ -266,6 +266,6 @@ void platformSleepUntil(double time) { glfwSleep(remaining - 0.001); while (platformGetTime() < time) { - // Spin-wait for the remaining sub-millisecond + YIELD(); } } diff --git a/src/desktop/backends/glfw3.c b/src/desktop/backends/glfw3.c index 5f978f72..75d1e004 100644 --- a/src/desktop/backends/glfw3.c +++ b/src/desktop/backends/glfw3.c @@ -427,6 +427,6 @@ void platformSleepUntil(double time) { #endif } while (platformGetTime() < time) { - // Spin-wait for the remaining sub-millisecond + YIELD(); } } diff --git a/src/desktop/backends/sdl1.c b/src/desktop/backends/sdl1.c index c5785707..eaccaba0 100644 --- a/src/desktop/backends/sdl1.c +++ b/src/desktop/backends/sdl1.c @@ -257,6 +257,6 @@ void platformSleepUntil(double time) { SDL_Delay((Uint32)((remaining - 0.001) * 1000)); while (platformGetTime() < time) { - // Spin-wait for the remaining sub-millisecond + YIELD(); } } diff --git a/src/desktop/backends/sdl2.c b/src/desktop/backends/sdl2.c index c8f48fc5..92200ea3 100644 --- a/src/desktop/backends/sdl2.c +++ b/src/desktop/backends/sdl2.c @@ -320,7 +320,7 @@ void platformSleepUntil(double time) { SDL_Delay((Uint32)((remaining - 0.001) * 1000)); while (platformGetTime() < time) { - // Spin-wait for the remaining sub-millisecond + YIELD(); } } From 6c25de42dc950c8e670ab4c89b464a18e8b5614d Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:02:12 +0100 Subject: [PATCH 2/9] desktop: restore spin-wait comment --- src/desktop/backends/glfw2.c | 1 + src/desktop/backends/glfw3.c | 1 + src/desktop/backends/sdl1.c | 1 + src/desktop/backends/sdl2.c | 1 + 4 files changed, 4 insertions(+) diff --git a/src/desktop/backends/glfw2.c b/src/desktop/backends/glfw2.c index db1a8d20..79976c9a 100644 --- a/src/desktop/backends/glfw2.c +++ b/src/desktop/backends/glfw2.c @@ -266,6 +266,7 @@ void platformSleepUntil(double time) { glfwSleep(remaining - 0.001); while (platformGetTime() < time) { + // Spin-wait for the remaining sub-millisecond YIELD(); } } diff --git a/src/desktop/backends/glfw3.c b/src/desktop/backends/glfw3.c index 75d1e004..d4c2cdc1 100644 --- a/src/desktop/backends/glfw3.c +++ b/src/desktop/backends/glfw3.c @@ -427,6 +427,7 @@ void platformSleepUntil(double time) { #endif } while (platformGetTime() < time) { + // Spin-wait for the remaining sub-millisecond YIELD(); } } diff --git a/src/desktop/backends/sdl1.c b/src/desktop/backends/sdl1.c index eaccaba0..f46755b6 100644 --- a/src/desktop/backends/sdl1.c +++ b/src/desktop/backends/sdl1.c @@ -257,6 +257,7 @@ void platformSleepUntil(double time) { SDL_Delay((Uint32)((remaining - 0.001) * 1000)); while (platformGetTime() < time) { + // Spin-wait for the remaining sub-millisecond YIELD(); } } diff --git a/src/desktop/backends/sdl2.c b/src/desktop/backends/sdl2.c index 92200ea3..590375db 100644 --- a/src/desktop/backends/sdl2.c +++ b/src/desktop/backends/sdl2.c @@ -320,6 +320,7 @@ void platformSleepUntil(double time) { SDL_Delay((Uint32)((remaining - 0.001) * 1000)); while (platformGetTime() < time) { + // Spin-wait for the remaining sub-millisecond YIELD(); } } From 84dbace7d87a426e590de529195ca79f5baa57f4 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:02:41 +0100 Subject: [PATCH 3/9] common: forgot to add YIELD macro --- src/collision.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/collision.h b/src/collision.h index afd5999a..d6a965b3 100644 --- a/src/collision.h +++ b/src/collision.h @@ -431,3 +431,15 @@ static inline bool Collision_instancesOverlapPrecise(Runner* runner, Instance* a return false; } + +#if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) + #include + #define YIELD() _mm_pause() +#elif defined(_M_ARM64) || defined(_M_ARM) + #include + #define YIELD() __yield() +#elif defined(__aarch64__) || defined(__arm__) && (__ARM_ARCH >= 7) + #define YIELD() __asm__ volatile("yield" ::: "memory") +#else + #define YIELD() ((void)0) +#endif From b58c7407b10afadb5804727f727875470ac2d310 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:05:16 +0100 Subject: [PATCH 4/9] what the fuck am i doing --- src/collision.h | 14 +------------- src/common.h | 12 ++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/collision.h b/src/collision.h index d6a965b3..5caba2c4 100644 --- a/src/collision.h +++ b/src/collision.h @@ -430,16 +430,4 @@ static inline bool Collision_instancesOverlapPrecise(Runner* runner, Instance* a } return false; -} - -#if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) - #include - #define YIELD() _mm_pause() -#elif defined(_M_ARM64) || defined(_M_ARM) - #include - #define YIELD() __yield() -#elif defined(__aarch64__) || defined(__arm__) && (__ARM_ARCH >= 7) - #define YIELD() __asm__ volatile("yield" ::: "memory") -#else - #define YIELD() ((void)0) -#endif +} \ No newline at end of file diff --git a/src/common.h b/src/common.h index 53f82562..ba927784 100644 --- a/src/common.h +++ b/src/common.h @@ -35,3 +35,15 @@ #define MAYBE_UNUSED #endif #endif + +#if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) + #include + #define YIELD() _mm_pause() +#elif defined(_M_ARM64) || defined(_M_ARM) + #include + #define YIELD() __yield() +#elif defined(__aarch64__) || defined(__arm__) && (__ARM_ARCH >= 7) + #define YIELD() __asm__ volatile("yield" ::: "memory") +#else + #define YIELD() ((void)0) +#endif From 30e714286521f09e9e869274b9dac8d7b9286029 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:16:50 +0100 Subject: [PATCH 5/9] try to make it work with older gcc (uniq pls test) --- src/common.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/common.h b/src/common.h index ba927784..294f6433 100644 --- a/src/common.h +++ b/src/common.h @@ -37,13 +37,25 @@ #endif #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) - #include - #define YIELD() _mm_pause() + #if defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 4)) + #define YIELD() __asm__ volatile("rep; nop" ::: "memory") + #else + #include + #define YIELD() _mm_pause() + #endif #elif defined(_M_ARM64) || defined(_M_ARM) #include #define YIELD() __yield() -#elif defined(__aarch64__) || defined(__arm__) && (__ARM_ARCH >= 7) - #define YIELD() __asm__ volatile("yield" ::: "memory") +#elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) + #if defined(__GNUC__) && (__GNUC__ < 5) + #define YIELD() __asm__ volatile("yield" ::: "memory") + #else + #include + #define YIELD() __yield() + #endif +#elif defined(__riscv) + #define YIELD() __asm__ volatile("pause" ::: "memory") #else #define YIELD() ((void)0) #endif + From 1b7cf55e7354e2eeffee8a8e36c4df93ebf619a2 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:46:38 +0100 Subject: [PATCH 6/9] make YIELD a no-op for non-GNUC --- src/common.h | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/common.h b/src/common.h index 294f6433..fb1dff5c 100644 --- a/src/common.h +++ b/src/common.h @@ -36,26 +36,29 @@ #endif #endif -#if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) - #if defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 4)) - #define YIELD() __asm__ volatile("rep; nop" ::: "memory") - #else - #include - #define YIELD() _mm_pause() - #endif -#elif defined(_M_ARM64) || defined(_M_ARM) - #include - #define YIELD() __yield() -#elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) - #if defined(__GNUC__) && (__GNUC__ < 5) - #define YIELD() __asm__ volatile("yield" ::: "memory") - #else - #include +#ifdef __GNUC__ + #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) + #if defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 4)) + #define YIELD() __asm__ volatile("rep; nop" ::: "memory") + #else + #include + #define YIELD() _mm_pause() + #endif + #elif defined(_M_ARM64) || defined(_M_ARM) + #include #define YIELD() __yield() + #elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) + #if defined(__GNUC__) && (__GNUC__ < 5) + #define YIELD() __asm__ volatile("yield" ::: "memory") + #else + #include + #define YIELD() __yield() + #endif + #elif defined(__riscv) + #define YIELD() __asm__ volatile("pause" ::: "memory") + #else + #define YIELD() ((void)0) #endif -#elif defined(__riscv) - #define YIELD() __asm__ volatile("pause" ::: "memory") #else #define YIELD() ((void)0) #endif - From 14e472cc10996633b3264956f6aeffbe6ecdbb13 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Sun, 7 Jun 2026 11:53:48 +0100 Subject: [PATCH 7/9] remove other GNUCs --- src/common.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common.h b/src/common.h index fb1dff5c..769d201b 100644 --- a/src/common.h +++ b/src/common.h @@ -38,7 +38,7 @@ #ifdef __GNUC__ #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) - #if defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 4)) + #if ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 4)) #define YIELD() __asm__ volatile("rep; nop" ::: "memory") #else #include @@ -48,7 +48,7 @@ #include #define YIELD() __yield() #elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) - #if defined(__GNUC__) && (__GNUC__ < 5) + #if (__GNUC__ < 5) #define YIELD() __asm__ volatile("yield" ::: "memory") #else #include From 9acd14d39daa7d4340c867d08618f6ac3327f0c7 Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Wed, 10 Jun 2026 12:05:42 +0100 Subject: [PATCH 8/9] refactor YIELD define --- src/common.h | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/common.h b/src/common.h index 769d201b..47eb6b34 100644 --- a/src/common.h +++ b/src/common.h @@ -36,26 +36,20 @@ #endif #endif -#ifdef __GNUC__ - #if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) - #if ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 4)) - #define YIELD() __asm__ volatile("rep; nop" ::: "memory") - #else - #include - #define YIELD() _mm_pause() - #endif +#if defined(__GNUC__) || defined(__clang__) + #if defined(__x86_64__) || defined(__i386__) || defined(__riscv) + #define YIELD() __asm__ volatile("pause" ::: "memory") + #elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) + #define YIELD() __asm__ volatile("yield" ::: "memory") + #else + #define YIELD() ((void)0) + #endif +#elif defined(_MSC_VER) + #include + #if defined(_M_X64) || defined(_M_IX86) + #define YIELD() _mm_pause() #elif defined(_M_ARM64) || defined(_M_ARM) - #include #define YIELD() __yield() - #elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) - #if (__GNUC__ < 5) - #define YIELD() __asm__ volatile("yield" ::: "memory") - #else - #include - #define YIELD() __yield() - #endif - #elif defined(__riscv) - #define YIELD() __asm__ volatile("pause" ::: "memory") #else #define YIELD() ((void)0) #endif From 680d176b5aab409b2dbaa9fb12b805c5f57c476d Mon Sep 17 00:00:00 2001 From: cobaltgit Date: Wed, 10 Jun 2026 12:13:03 +0100 Subject: [PATCH 9/9] satisfy uniq's taste for compilers unearthed from the pyramids --- src/common.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common.h b/src/common.h index 47eb6b34..130aa77d 100644 --- a/src/common.h +++ b/src/common.h @@ -38,9 +38,9 @@ #if defined(__GNUC__) || defined(__clang__) #if defined(__x86_64__) || defined(__i386__) || defined(__riscv) - #define YIELD() __asm__ volatile("pause" ::: "memory") + #define YIELD() __asm__ volatile("rep; nop" : : : "memory") #elif defined(__aarch64__) || (defined(__arm__) && defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) - #define YIELD() __asm__ volatile("yield" ::: "memory") + #define YIELD() __asm__ volatile("yield" : : : "memory") #else #define YIELD() ((void)0) #endif