lfs_util.h 975 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * lfs utility functions
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the MIT license
  6. */
  7. #ifndef LFS_UTIL_H
  8. #define LFS_UTIL_H
  9. #include <stdlib.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. // Builtin functions
  13. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  14. return (a > b) ? a : b;
  15. }
  16. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  17. return (a < b) ? a : b;
  18. }
  19. static inline uint32_t lfs_ctz(uint32_t a) {
  20. return __builtin_ctz(a);
  21. }
  22. static inline uint32_t lfs_npw2(uint32_t a) {
  23. return 32 - __builtin_clz(a-1);
  24. }
  25. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  26. return (int)(unsigned)(a - b);
  27. }
  28. uint32_t lfs_crc(uint32_t crc, size_t size, const void *buffer);
  29. // Logging functions
  30. #define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
  31. #define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
  32. #define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
  33. #endif