logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

util.h (1530B)


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