From 8315fa1d3ac3bdfd35f71375a7e993c337d416d9 Mon Sep 17 00:00:00 2001 From: George Steed Date: Mon, 19 Aug 2024 09:53:47 +0100 Subject: [PATCH] Avoid duplication of CPU feature disable macros The same conditions are repeated across all *_row.h headers which makes it harder than necessary to guard enabling new architecture features depending on compiler versions etc. Avoid this duplication by merging the conditions into a new cpu_support.h header. Change-Id: Ibe7dfcef138edca6cc36870f1cfbb1bb108083e3 Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/5802957 Reviewed-by: Frank Barchard Reviewed-by: Justin Green --- include/libyuv/compare_row.h | 31 +------------ include/libyuv/cpu_support.h | 85 ++++++++++++++++++++++++++++++++++++ include/libyuv/rotate_row.h | 24 +--------- include/libyuv/row.h | 54 +---------------------- include/libyuv/scale_row.h | 38 +--------------- 5 files changed, 89 insertions(+), 143 deletions(-) create mode 100644 include/libyuv/cpu_support.h diff --git a/include/libyuv/compare_row.h b/include/libyuv/compare_row.h index 8a8358db3..31113c0e2 100644 --- a/include/libyuv/compare_row.h +++ b/include/libyuv/compare_row.h @@ -12,42 +12,13 @@ #define INCLUDE_LIBYUV_COMPARE_ROW_H_ #include "libyuv/basic_types.h" +#include "libyuv/cpu_support.h" #ifdef __cplusplus namespace libyuv { extern "C" { #endif -#if defined(__pnacl__) || defined(__CLR_VER) || \ - (defined(__native_client__) && defined(__x86_64__)) || \ - (defined(__i386__) && !defined(__SSE__) && !defined(__clang__)) -#define LIBYUV_DISABLE_X86 -#endif -#if defined(__native_client__) -#define LIBYUV_DISABLE_NEON -#endif -// MemorySanitizer does not support assembly code yet. http://crbug.com/344505 -#if defined(__has_feature) -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_NEON) -#define LIBYUV_DISABLE_NEON -#endif -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_X86) -#define LIBYUV_DISABLE_X86 -#endif -#endif -// Visual C 2012 required for AVX2. -#if defined(_M_IX86) && !defined(__clang__) && defined(_MSC_VER) && \ - _MSC_VER >= 1700 -#define VISUALC_HAS_AVX2 1 -#endif // VisualStudio >= 2012 - -// clang >= 3.4.0 required for AVX2. -#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) -#if (__clang_major__ > 3) || (__clang_major__ == 3 && (__clang_minor__ >= 4)) -#define CLANG_HAS_AVX2 1 -#endif // clang >= 3.4 -#endif // __clang__ - // The following are available for Visual C and GCC: #if !defined(LIBYUV_DISABLE_X86) && \ (defined(__x86_64__) || defined(__i386__) || defined(_M_IX86)) diff --git a/include/libyuv/cpu_support.h b/include/libyuv/cpu_support.h new file mode 100644 index 000000000..ac16575a1 --- /dev/null +++ b/include/libyuv/cpu_support.h @@ -0,0 +1,85 @@ +/* + * Copyright 2024 The LibYuv Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef INCLUDE_LIBYUV_CPU_SUPPORT_H_ +#define INCLUDE_LIBYUV_CPU_SUPPORT_H_ + +#ifdef __cplusplus +namespace libyuv { +extern "C" { +#endif + +#if defined(__pnacl__) || defined(__CLR_VER) || \ + (defined(__native_client__) && defined(__x86_64__)) || \ + (defined(__i386__) && !defined(__SSE__) && !defined(__clang__)) +#define LIBYUV_DISABLE_X86 +#endif + +#if defined(__native_client__) +#define LIBYUV_DISABLE_NEON +#endif + +// MemorySanitizer does not support assembly code yet. http://crbug.com/344505 +#if defined(__has_feature) +#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_NEON) +#define LIBYUV_DISABLE_NEON +#endif +#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_X86) +#define LIBYUV_DISABLE_X86 +#endif +#endif + +// clang >= 3.5.0 required for Arm64. +#if defined(__clang__) && defined(__aarch64__) && !defined(LIBYUV_DISABLE_NEON) +#if (__clang_major__ < 3) || (__clang_major__ == 3 && (__clang_minor__ < 5)) +#define LIBYUV_DISABLE_NEON +#endif // clang >= 3.5 +#endif // __clang__ + +// GCC >= 4.7.0 required for AVX2. +#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__)) +#if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 7)) +#define GCC_HAS_AVX2 1 +#endif // GNUC >= 4.7 +#endif // __GNUC__ + +// clang >= 3.4.0 required for AVX2. +#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) +#if (__clang_major__ > 3) || (__clang_major__ == 3 && (__clang_minor__ >= 4)) +#define CLANG_HAS_AVX2 1 +#endif // clang >= 3.4 +#endif // __clang__ + +// clang >= 6.0.0 required for AVX512. +#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) +// clang in xcode follows a different versioning scheme. +// TODO(fbarchard): fix xcode 9 ios b/789. +#if (__clang_major__ >= 7) && !defined(__APPLE__) +#define CLANG_HAS_AVX512 1 +#endif // clang >= 7 +#endif // __clang__ + +// Visual C 2012 required for AVX2. +#if defined(_M_IX86) && !defined(__clang__) && defined(_MSC_VER) && \ + _MSC_VER >= 1700 +#define VISUALC_HAS_AVX2 1 +#endif // VisualStudio >= 2012 + +// Temporary disable SME. +#if !defined(LIBYUV_DISABLE_SME) +#define LIBYUV_DISABLE_SME +#endif + +#ifdef __cplusplus +} // extern "C" +} // namespace libyuv +#endif + +#endif // INCLUDE_LIBYUV_CPU_SUPPORT_H_ diff --git a/include/libyuv/rotate_row.h b/include/libyuv/rotate_row.h index d4a974c54..7672e2c1f 100644 --- a/include/libyuv/rotate_row.h +++ b/include/libyuv/rotate_row.h @@ -12,35 +12,13 @@ #define INCLUDE_LIBYUV_ROTATE_ROW_H_ #include "libyuv/basic_types.h" +#include "libyuv/cpu_support.h" #ifdef __cplusplus namespace libyuv { extern "C" { #endif -#if defined(__pnacl__) || defined(__CLR_VER) || \ - (defined(__native_client__) && defined(__x86_64__)) || \ - (defined(__i386__) && !defined(__SSE__) && !defined(__clang__)) -#define LIBYUV_DISABLE_X86 -#endif -#if defined(__native_client__) -#define LIBYUV_DISABLE_NEON -#endif - -// temporary disable SME -#if !defined(LIBYUV_DISABLE_SME) -#define LIBYUV_DISABLE_SME -#endif - -// MemorySanitizer does not support assembly code yet. http://crbug.com/344505 -#if defined(__has_feature) -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_NEON) -#define LIBYUV_DISABLE_NEON -#endif -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_X86) -#define LIBYUV_DISABLE_X86 -#endif -#endif // The following are available for Visual C 32 bit: #if !defined(LIBYUV_DISABLE_X86) && defined(_M_IX86) && defined(_MSC_VER) && \ !defined(__clang__) diff --git a/include/libyuv/row.h b/include/libyuv/row.h index 9baa5155c..dae94294a 100644 --- a/include/libyuv/row.h +++ b/include/libyuv/row.h @@ -15,65 +15,13 @@ #include // For malloc #include "libyuv/basic_types.h" +#include "libyuv/cpu_support.h" #ifdef __cplusplus namespace libyuv { extern "C" { #endif -#if defined(__pnacl__) || defined(__CLR_VER) || \ - (defined(__native_client__) && defined(__x86_64__)) || \ - (defined(__i386__) && !defined(__SSE__) && !defined(__clang__)) -#define LIBYUV_DISABLE_X86 -#endif -#if defined(__native_client__) -#define LIBYUV_DISABLE_NEON -#endif -// MemorySanitizer does not support assembly code yet. http://crbug.com/344505 -#if defined(__has_feature) -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_NEON) -#define LIBYUV_DISABLE_NEON -#endif -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_X86) -#define LIBYUV_DISABLE_X86 -#endif -#endif -// clang >= 3.5.0 required for Arm64. -#if defined(__clang__) && defined(__aarch64__) && !defined(LIBYUV_DISABLE_NEON) -#if (__clang_major__ < 3) || (__clang_major__ == 3 && (__clang_minor__ < 5)) -#define LIBYUV_DISABLE_NEON -#endif // clang >= 3.5 -#endif // __clang__ - -// GCC >= 4.7.0 required for AVX2. -#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__)) -#if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 7)) -#define GCC_HAS_AVX2 1 -#endif // GNUC >= 4.7 -#endif // __GNUC__ - -// clang >= 3.4.0 required for AVX2. -#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) -#if (__clang_major__ > 3) || (__clang_major__ == 3 && (__clang_minor__ >= 4)) -#define CLANG_HAS_AVX2 1 -#endif // clang >= 3.4 -#endif // __clang__ - -// clang >= 6.0.0 required for AVX512. -#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) -// clang in xcode follows a different versioning scheme. -// TODO(fbarchard): fix xcode 9 ios b/789. -#if (__clang_major__ >= 7) && !defined(__APPLE__) -#define CLANG_HAS_AVX512 1 -#endif // clang >= 7 -#endif // __clang__ - -// Visual C 2012 required for AVX2. -#if defined(_M_IX86) && !defined(__clang__) && defined(_MSC_VER) && \ - _MSC_VER >= 1700 -#define VISUALC_HAS_AVX2 1 -#endif // VisualStudio >= 2012 - // The following are available on all x86 platforms: #if !defined(LIBYUV_DISABLE_X86) && \ (defined(_M_IX86) || defined(__x86_64__) || defined(__i386__)) diff --git a/include/libyuv/scale_row.h b/include/libyuv/scale_row.h index 3d9a11b02..a8ec4776a 100644 --- a/include/libyuv/scale_row.h +++ b/include/libyuv/scale_row.h @@ -12,6 +12,7 @@ #define INCLUDE_LIBYUV_SCALE_ROW_H_ #include "libyuv/basic_types.h" +#include "libyuv/cpu_support.h" #include "libyuv/scale.h" #ifdef __cplusplus @@ -19,43 +20,6 @@ namespace libyuv { extern "C" { #endif -#if defined(__pnacl__) || defined(__CLR_VER) || \ - (defined(__native_client__) && defined(__x86_64__)) || \ - (defined(__i386__) && !defined(__SSE__) && !defined(__clang__)) -#define LIBYUV_DISABLE_X86 -#endif -#if defined(__native_client__) -#define LIBYUV_DISABLE_NEON -#endif -// MemorySanitizer does not support assembly code yet. http://crbug.com/344505 -#if defined(__has_feature) -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_NEON) -#define LIBYUV_DISABLE_NEON -#endif -#if __has_feature(memory_sanitizer) && !defined(LIBYUV_DISABLE_X86) -#define LIBYUV_DISABLE_X86 -#endif -#endif -// GCC >= 4.7.0 required for AVX2. -#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__)) -#if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 7)) -#define GCC_HAS_AVX2 1 -#endif // GNUC >= 4.7 -#endif // __GNUC__ - -// clang >= 3.4.0 required for AVX2. -#if defined(__clang__) && (defined(__x86_64__) || defined(__i386__)) -#if (__clang_major__ > 3) || (__clang_major__ == 3 && (__clang_minor__ >= 4)) -#define CLANG_HAS_AVX2 1 -#endif // clang >= 3.4 -#endif // __clang__ - -// Visual C 2012 required for AVX2. -#if defined(_M_IX86) && !defined(__clang__) && defined(_MSC_VER) && \ - _MSC_VER >= 1700 -#define VISUALC_HAS_AVX2 1 -#endif // VisualStudio >= 2012 - // The following are available on all x86 platforms: #if !defined(LIBYUV_DISABLE_X86) && \ (defined(_M_IX86) || defined(__x86_64__) || defined(__i386__))