lfs_util.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * lfs utility functions
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the Apache 2.0 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, these may be replaced by more
  13. // efficient implementations in the system
  14. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  15. return (a > b) ? a : b;
  16. }
  17. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  18. return (a < b) ? a : b;
  19. }
  20. static inline uint32_t lfs_ctz(uint32_t a) {
  21. return __builtin_ctz(a);
  22. }
  23. static inline uint32_t lfs_npw2(uint32_t a) {
  24. return 32 - __builtin_clz(a-1);
  25. }
  26. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  27. return (int)(unsigned)(a - b);
  28. }
  29. // CRC-32 with polynomial = 0x04c11db7
  30. void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
  31. // Logging functions, these may be replaced by system-specific
  32. // logging functions
  33. #define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
  34. #define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
  35. #define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
  36. #endif