You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. // Copyright 2022 Stefan Kerkmann (KarlK90)
  2. // Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #pragma once
  5. #include "bitwise.h"
  6. // convert to string
  7. #define STR(s) XSTR(s)
  8. #define XSTR(s) #s
  9. #if !defined(MIN)
  10. # define MIN(x, y) (((x) < (y)) ? (x) : (y))
  11. #endif
  12. #if !defined(MAX)
  13. # define MAX(x, y) (((x) > (y)) ? (x) : (y))
  14. #endif
  15. #if !defined(CEILING)
  16. /**
  17. * @brief Computes the rounded up result of a division of two integers at
  18. * compile time.
  19. */
  20. # define CEILING(dividend, divisor) (((dividend) + (divisor)-1) / (divisor))
  21. #endif
  22. #if !defined(IS_ARRAY)
  23. /**
  24. * @brief Returns true if the value is an array, false if it's a pointer.
  25. *
  26. * This macro is ill-formed for scalars, which is OK for its intended use in
  27. * ARRAY_SIZE.
  28. */
  29. # define IS_ARRAY(value) (!__builtin_types_compatible_p(typeof((value)), typeof(&(value)[0])))
  30. #endif
  31. #if !defined(ARRAY_SIZE)
  32. /**
  33. * @brief Computes the number of elements of the given array at compile time.
  34. *
  35. * This Macro can only be used for statically allocated arrays that have not
  36. * been decayed into a pointer. This is detected at compile time, though the
  37. * error message for scalar values is poor.
  38. */
  39. # define ARRAY_SIZE(array) (__builtin_choose_expr(IS_ARRAY((array)), sizeof((array)) / sizeof((array)[0]), (void)0))
  40. #endif
  41. #if !defined(PACKED)
  42. # define PACKED __attribute__((__packed__))
  43. #endif
  44. #if __has_include("_util.h")
  45. # include "_util.h" /* Include the platform's _util.h */
  46. #endif