util.h 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef TRAY_UTIL_H_
  2. #define TRAY_UTIL_H_
  3. #include <stdint.h>
  4. #define __stringify(x) #x
  5. #define stringify(x) __stringify(x)
  6. #define min(a, b) ({ \
  7. __typeof__(a) __a = (a); \
  8. __typeof__(b) __b = (b); \
  9. __a < __b ? __a : __b; \
  10. })
  11. #define max(a, b) ({ \
  12. __typeof__(a) __a = (a); \
  13. __typeof__(b) __b = (b); \
  14. __a > __b ? __a : __b; \
  15. })
  16. #define clamp(v, mi, ma) max(min(v, ma), mi)
  17. #define round_up(n, s) ((((n) + (s) - 1) / (s)) * (s))
  18. #define div_round_up(x, d) ({ \
  19. __typeof__(x) __x = (x); \
  20. __typeof__(d) __d = (d); \
  21. (__x + __d - 1) / __d; \
  22. })
  23. #define div_round(x, d) ({ \
  24. __typeof__(x) __x = (x); \
  25. __typeof__(d) __d = (d); \
  26. (__x + (__d / 2)) / __d; \
  27. })
  28. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  29. #define compiler_barrier() __asm__ __volatile__("" : : : "memory")
  30. #define ALIGN(x) __attribute__((__aligned__(x)))
  31. #define ALIGN_MAX ALIGN(__BIGGEST_ALIGNMENT__)
  32. void msleep(unsigned int msecs);
  33. #endif /* TRAY_UTIL_H_ */