[vlc-commits] [Git][videolan/vlc][master] 2 commits: cpu: add vlc_CPU_set() to override cached CPU capability flags
Steve Lhomme (@robUx4)
gitlab at videolan.org
Sun Aug 9 07:28:55 UTC 2026
Steve Lhomme pushed to branch master at VideoLAN / VLC
Commits:
c43115ed by Mazen Hassan at 2026-08-09T07:17:25+00:00
cpu: add vlc_CPU_set() to override cached CPU capability flags
Add an exported vlc_CPU_set(mask) that intersects the raw detected
CPU capabilities with the supplied mask and overwrites the cached
flags returned by vlc_CPU(). Bits can only be cleared, never added,
so this can never make vlc_CPU() report a capability the hardware
does not actually have.
This is intended for testing purposes only (e.g. checkasm), to let
tests force code paths for lesser CPU capabilities to be exercised
without changing the public signatures of Probe() or
vlc_CPU_functions_init().
- - - - -
be6dd1ae by Mazen Hassan at 2026-08-09T07:17:25+00:00
cpu: remove compile-time folding of vlc_CPU_XXX() macros
Every vlc_CPU_XXX() macro folded to a compile-time constant 1 when
the corresponding compiler feature macro (e.g. __SSE2__) was already
defined, skipping the runtime vlc_CPU() check entirely in that case.
This defeated vlc_CPU_set(): masking only affects the cached value
read by vlc_CPU(), so a macro that never calls vlc_CPU() at all
cannot be masked. Make every vlc_CPU_XXX() macro always evaluate
((vlc_CPU() & VLC_CPU_XXX) != 0), so tests can force lesser code
paths to be exercised regardless of what the compiler was told to
target.
The VLC_SSE/VLC_AVX/VLC_ALTIVEC target-attribute macros are left
untouched, since they govern codegen rather than flag testing.
No #if vlc_CPU_XXX() usage exists in the tree (confirmed via grep),
so this doesn't break any preprocessor conditionals. Probe() and
vlc_CPU_functions_init()'s public signatures are unaffected.
- - - - -
3 changed files:
- include/vlc_cpu.h
- src/libvlccore.sym
- src/misc/cpu.c
Changes:
=====================================
include/vlc_cpu.h
=====================================
@@ -33,6 +33,24 @@
*/
VLC_API unsigned vlc_CPU(void);
+/**
+ * Masks CPU capability flags.
+ *
+ * This overrides the cached CPU capability flags with the intersection of
+ * the actually-detected CPU capabilities and the supplied mask. Bits cannot be
+ * added, only cleared: masking never reports a capability that hardware
+ * doesn't actually have.
+ *
+ * This is intended for testing purposes only (e.g. checkasm), to force
+ * code paths for lesser CPU capabilities to be exercised.
+ *
+ * \warning This function is not thread-safe with respect to vlc_CPU(): it
+ * must be called before any thread makes its first call to vlc_CPU(),
+ * otherwise the lazy initialisation in vlc_CPU() may race with and
+ * overwrite the masked value.
+ */
+VLC_API void vlc_CPU_set(unsigned mask);
+
/**
* Computes CPU capability flags.
*
@@ -46,17 +64,8 @@ unsigned vlc_CPU_raw(void);
# define VLC_CPU_ARM_NEON 0x1
# define VLC_CPU_ARM_SVE 0x2
-# ifdef __ARM_NEON
-# define vlc_CPU_ARM_NEON() (1)
-# else
-# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
-# endif
-
-# ifdef __ARM_FEATURE_SVE
-# define vlc_CPU_ARM_SVE() (1)
-# else
-# define vlc_CPU_ARM_SVE() ((vlc_CPU() & VLC_CPU_ARM_SVE) != 0)
-# endif
+# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
+# define vlc_CPU_ARM_SVE() ((vlc_CPU() & VLC_CPU_ARM_SVE) != 0)
# elif defined (__arm__)
# if defined (__VFP_FP__) && !defined (__SOFTFP__)
@@ -75,17 +84,9 @@ unsigned vlc_CPU_raw(void);
# define VLC_CPU_ARM_ARCH 4
# endif
-# if (VLC_CPU_ARM_ARCH >= 6)
-# define vlc_CPU_ARMv6() (1)
-# else
-# define vlc_CPU_ARMv6() ((vlc_CPU() & VLC_CPU_ARMv6) != 0)
-# endif
+# define vlc_CPU_ARMv6() ((vlc_CPU() & VLC_CPU_ARMv6) != 0)
+# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
-# ifdef __ARM_NEON__
-# define vlc_CPU_ARM_NEON() (1)
-# else
-# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
-# endif
# elif defined (__loongarch__)
# define HAVE_FPU 1
@@ -101,11 +102,10 @@ unsigned vlc_CPU_raw(void);
# define HAVE_FPU 1
# define VLC_CPU_ALTIVEC 2
+# define vlc_CPU_ALTIVEC() ((vlc_CPU() & VLC_CPU_ALTIVEC) != 0)
# ifdef ALTIVEC
-# define vlc_CPU_ALTIVEC() (1)
# define VLC_ALTIVEC
# else
-# define vlc_CPU_ALTIVEC() ((vlc_CPU() & VLC_CPU_ALTIVEC) != 0)
# define VLC_ALTIVEC __attribute__ ((__target__ ("altivec")))
# endif
@@ -118,18 +118,8 @@ unsigned vlc_CPU_raw(void);
# define VLC_CPU_RV_V 0x1
# define VLC_CPU_RV_B 0x2
-# ifdef __riscv_v
-# define vlc_CPU_RV_V() (1)
-# else
-# define vlc_CPU_RV_V() ((vlc_CPU() & VLC_CPU_RV_V) != 0)
-# endif
-
-# if (defined (__riscv_b) || (defined (__riscv_zba) && defined (__riscv_zbb) \
- && defined (__riscv_zbs)))
-# define vlc_CPU_RV_B() (1)
-# else
-# define vlc_CPU_RV_B() ((vlc_CPU() & VLC_CPU_RV_B) != 0)
-# endif
+# define vlc_CPU_RV_V() ((vlc_CPU() & VLC_CPU_RV_V) != 0)
+# define vlc_CPU_RV_B() ((vlc_CPU() & VLC_CPU_RV_B) != 0)
# elif defined (__sparc__)
# define HAVE_FPU 1
@@ -149,43 +139,19 @@ unsigned vlc_CPU_raw(void);
# define VLC_SSE __attribute__ ((__target__ ("sse")))
# endif
-# ifdef __SSE2__
-# define vlc_CPU_SSE2() (1)
-# else
-# define vlc_CPU_SSE2() ((vlc_CPU() & VLC_CPU_SSE2) != 0)
-# endif
-
-# ifdef __SSE3__
-# define vlc_CPU_SSE3() (1)
-# else
-# define vlc_CPU_SSE3() ((vlc_CPU() & VLC_CPU_SSE3) != 0)
-# endif
-
-# ifdef __SSSE3__
-# define vlc_CPU_SSSE3() (1)
-# else
-# define vlc_CPU_SSSE3() ((vlc_CPU() & VLC_CPU_SSSE3) != 0)
-# endif
-
-# ifdef __SSE4_1__
-# define vlc_CPU_SSE4_1() (1)
-# else
-# define vlc_CPU_SSE4_1() ((vlc_CPU() & VLC_CPU_SSE4_1) != 0)
-# endif
+# define vlc_CPU_SSE2() ((vlc_CPU() & VLC_CPU_SSE2) != 0)
+# define vlc_CPU_SSE3() ((vlc_CPU() & VLC_CPU_SSE3) != 0)
+# define vlc_CPU_SSSE3() ((vlc_CPU() & VLC_CPU_SSSE3) != 0)
+# define vlc_CPU_SSE4_1() ((vlc_CPU() & VLC_CPU_SSE4_1) != 0)
+# define vlc_CPU_AVX() ((vlc_CPU() & VLC_CPU_AVX) != 0)
# ifdef __AVX__
-# define vlc_CPU_AVX() (1)
# define VLC_AVX
# else
-# define vlc_CPU_AVX() ((vlc_CPU() & VLC_CPU_AVX) != 0)
# define VLC_AVX __attribute__ ((__target__ ("avx")))
# endif
-# ifdef __AVX2__
-# define vlc_CPU_AVX2() (1)
-# else
-# define vlc_CPU_AVX2() ((vlc_CPU() & VLC_CPU_AVX2) != 0)
-# endif
+# define vlc_CPU_AVX2() ((vlc_CPU() & VLC_CPU_AVX2) != 0)
# else
/**
=====================================
src/libvlccore.sym
=====================================
@@ -597,6 +597,7 @@ vlc_control_cancel
vlc_GetCPUCount
vlc_CPU
vlc_CPU_functions_init
+vlc_CPU_set
vlc_filenamecmp
vlc_fourcc_GetCodec
vlc_fourcc_GetCodecAudio
=====================================
src/misc/cpu.c
=====================================
@@ -144,9 +144,10 @@ out:
return i_capabilities;
}
+static atomic_uint cpu_flags = -1U;
+
unsigned vlc_CPU(void)
{
- static atomic_uint cpu_flags = -1U;
unsigned flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);
if (unlikely(flags == -1U)) {
@@ -157,6 +158,11 @@ unsigned vlc_CPU(void)
return flags;
}
+void vlc_CPU_set(unsigned mask)
+{
+ atomic_store_explicit(&cpu_flags, vlc_CPU_raw() & mask, memory_order_relaxed);
+}
+
void vlc_CPU_dump (vlc_object_t *obj)
{
struct vlc_memstream stream;
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/ddd289a53dce75a884549713801d86c71c3b2dcd...be6dd1aeffcc6f27db60b7230665825777e42bba
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/ddd289a53dce75a884549713801d86c71c3b2dcd...be6dd1aeffcc6f27db60b7230665825777e42bba
You're receiving this email because of your account on code.videolan.org. Manage all notifications: https://code.videolan.org/-/profile/notifications | Help: https://code.videolan.org/help
More information about the vlc-commits
mailing list