esp_compiler.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "sdkconfig.h"
  8. /*
  9. * The likely and unlikely macro pairs:
  10. * These macros are useful to place when application
  11. * knows the majority ocurrence of a decision paths,
  12. * placing one of these macros can hint the compiler
  13. * to reorder instructions producing more optimized
  14. * code.
  15. */
  16. #if (CONFIG_COMPILER_OPTIMIZATION_PERF)
  17. #ifndef likely
  18. #define likely(x) __builtin_expect(!!(x), 1)
  19. #endif
  20. #ifndef unlikely
  21. #define unlikely(x) __builtin_expect(!!(x), 0)
  22. #endif
  23. #else
  24. #ifndef likely
  25. #define likely(x) (x)
  26. #endif
  27. #ifndef unlikely
  28. #define unlikely(x) (x)
  29. #endif
  30. #endif
  31. /*
  32. * Utility macros used for designated initializers, which work differently
  33. * in C99 and C++ standards mainly for aggregate types.
  34. * The member separator, comma, is already part of the macro, please omit the trailing comma.
  35. * Usage example:
  36. * struct config_t { char* pchr; char arr[SIZE]; } config = {
  37. * ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(pchr)
  38. * ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(arr, "Value")
  39. * };
  40. */
  41. #if defined(__cplusplus) && __cplusplus >= 202002L
  42. #define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) .member = value,
  43. #define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member) .member = { },
  44. #elif defined(__cplusplus) && __cplusplus < 202002L
  45. #define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) { .member = value },
  46. #define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member) .member = { },
  47. #else
  48. #define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) .member = value,
  49. #define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member)
  50. #endif