From b64a7610670a71dcf234dc93908eb08c25c06126 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 9 Jul 2024 10:25:42 +1000 Subject: [PATCH] base: enable ETC__CPU_ARCH__X86_64_V2 on MSVC If I understand the https://github.com/google/wuffs/issues/148 discussion correctly, a user overrode WUFFS_BASE__CPU_ARCH__X86_FAMILY (without also providing /arch:AVX) to enable SSE4.2 code (even though, technically, x86_64 doesn't guarantee SSE4.2, only SSE2). This seemed to work fine in practice, but was unsupported by Wuffs and 'broke' by fixing #145, now looking for WUFFS_BASE__CPU_ARCH__X86_64 instead of WUFFS_BASE__CPU_ARCH__X86_FAMILY. WUFFS_BASE__CPU_ARCH__X86_FAMILY has since been renamed to WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_FAMILY and then removed entirely. As SSE4.2 (roughly equivalent to x86-64-v2) seems to work fine at compile time (with the existing cpuid detection at runtime), enable it by default for MSVC, without need /arch. Enabling x86-64-v3 too, by default, is held back for now, pending further confirmation from MSVC users. The #148 user was exercising Wuffs' PNG codec, which uses SSE4.2 but not AVX or AVX2. Currently, only Wuffs' JPEG codec (and YCbCr conversion) uses AVX or AVX2. GCC and Clang don't need this fiddliness, because they support "__attribute__((target(arg)))". Updates #148 --- internal/cgen/base/fundamental-public.h | 10 +++++----- release/c/wuffs-unsupported-snapshot.c | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/cgen/base/fundamental-public.h b/internal/cgen/base/fundamental-public.h index 0daada75..e03f6ed8 100644 --- a/internal/cgen/base/fundamental-public.h +++ b/internal/cgen/base/fundamental-public.h @@ -119,11 +119,13 @@ #elif defined(_MSC_VER) // (#if-chain ref AVOID_CPU_ARCH_1) #if defined(_M_X64) -#if defined(__AVX2__) || defined(__clang__) - // We need for the __cpuid function. #include -// That's not enough for X64 SIMD, with clang-cl, if we want to use +#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64 +#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V2 +#if defined(__AVX2__) || defined(__clang__) + +// intrin.h isn't enough for X64 SIMD, with clang-cl, if we want to use // "__attribute__((target(arg)))" without e.g. "/arch:AVX". // // Some web pages suggest that is all you need, as it pulls in @@ -132,8 +134,6 @@ #include // AVX, AVX2, FMA, POPCNT #include // SSE4.2 #include // AES, PCLMUL -#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64 -#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V2 #define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V3 #else // defined(__AVX2__) || defined(__clang__) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index fdd1d4c3..715c3d51 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -175,11 +175,13 @@ extern "C" { #elif defined(_MSC_VER) // (#if-chain ref AVOID_CPU_ARCH_1) #if defined(_M_X64) -#if defined(__AVX2__) || defined(__clang__) - // We need for the __cpuid function. #include -// That's not enough for X64 SIMD, with clang-cl, if we want to use +#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64 +#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V2 +#if defined(__AVX2__) || defined(__clang__) + +// intrin.h isn't enough for X64 SIMD, with clang-cl, if we want to use // "__attribute__((target(arg)))" without e.g. "/arch:AVX". // // Some web pages suggest that is all you need, as it pulls in @@ -188,8 +190,6 @@ extern "C" { #include // AVX, AVX2, FMA, POPCNT #include // SSE4.2 #include // AES, PCLMUL -#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64 -#define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V2 #define WUFFS_PRIVATE_IMPL__CPU_ARCH__X86_64_V3 #else // defined(__AVX2__) || defined(__clang__)