lfs_util.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * lfs utility functions
  3. *
  4. * Copyright (c) 2017 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LFS_UTIL_H
  19. #define LFS_UTIL_H
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. // Builtin functions, these may be replaced by more
  24. // efficient implementations in the system
  25. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  26. return (a > b) ? a : b;
  27. }
  28. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  29. return (a < b) ? a : b;
  30. }
  31. static inline uint32_t lfs_ctz(uint32_t a) {
  32. return __builtin_ctz(a);
  33. }
  34. static inline uint32_t lfs_npw2(uint32_t a) {
  35. return 32 - __builtin_clz(a-1);
  36. }
  37. static inline uint32_t lfs_popc(uint32_t a) {
  38. return __builtin_popcount(a);
  39. }
  40. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  41. return (int)(unsigned)(a - b);
  42. }
  43. // CRC-32 with polynomial = 0x04c11db7
  44. void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
  45. // Logging functions, these may be replaced by system-specific
  46. // logging functions
  47. #define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
  48. #define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
  49. #define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
  50. #endif