From 7778779489be80c730c98ca53b3f2386d4537504 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 12 Feb 2021 15:14:03 +1100 Subject: [PATCH 1/2] newlib: Avoid introducing ESP-IDF macros from newlib platform headers Also, rewrite the assert.h header to be clearer Closes https://github.com/espressif/esp-idf/issues/6445 --- components/newlib/platform_include/assert.h | 37 ++++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/components/newlib/platform_include/assert.h b/components/newlib/platform_include/assert.h index 77d4cf08f5..c5f43e3aff 100644 --- a/components/newlib/platform_include/assert.h +++ b/components/newlib/platform_include/assert.h @@ -19,23 +19,28 @@ #pragma once #include #include -#include "esp_compiler.h" #include_next -#if defined(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT) && !defined(NDEBUG) - #undef assert - #define assert(__e) (likely(__e)) ? (void)0 : abort() -#else - /* moved part of toolchain provided assert to there then - * we can tweak the original assert macro to perform likely - * before deliver it to original toolchain implementation - */ - #undef assert - #ifdef NDEBUG - # define assert(__e) ((void)0) - #else - # define assert(__e) (likely(__e) ? (void)0 : __assert_func (__FILE__, __LINE__, \ - __ASSERT_FUNC, #__e)) - #endif +/* moved part of libc provided assert to here allows + * tweaking the assert macro to use __builtin_expect() + * and reduce jumps in the "asserts OK" code path + * + * Note: using __builtin_expect() not likely() to avoid defining the likely + * macro in namespace of non-IDF code that may include this standard header. + */ +#undef assert + +#if defined(NDEBUG) + +# define assert(__e) ((void)0) + +#elif CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT + +#define assert(__e) __builtin_expect(!!(__e), 1) ? (void)0 : abort() + +#else // !CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT + +#define assert(__e) (__builtin_expect(!!(__e), 1) ? (void)0 : __assert_func (__FILE__, __LINE__, \ + __ASSERT_FUNC, #__e)) #endif From e3b7337f03ed3138e4cb7b277013e64f22a25602 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 12 Feb 2021 15:23:36 +1100 Subject: [PATCH 2/2] esp_common: Don't redefine likely/unlikely if already defined As these macros aren't namespaced, they may have been defined by another header. Also reported in https://github.com/espressif/esp-idf/issues/6445 --- components/esp_common/include/esp_compiler.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/esp_common/include/esp_compiler.h b/components/esp_common/include/esp_compiler.h index 7ab8cb9af5..917c660253 100644 --- a/components/esp_common/include/esp_compiler.h +++ b/components/esp_common/include/esp_compiler.h @@ -23,12 +23,20 @@ * code. */ #if (CONFIG_COMPILER_OPTIMIZATION_PERF) +#ifndef likely #define likely(x) __builtin_expect(!!(x), 1) +#endif +#ifndef unlikely #define unlikely(x) __builtin_expect(!!(x), 0) +#endif #else +#ifndef likely #define likely(x) (x) +#endif +#ifndef unlikely #define unlikely(x) (x) #endif +#endif /* * Utility macros used for designated initializers, which work differently