lfs_util.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 int lfs_scmp(uint32_t a, uint32_t b) {
  38. return (int)(unsigned)(a - b);
  39. }
  40. // CRC-32 with polynomial = 0x04c11db7
  41. void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
  42. // Logging functions, these may be replaced by system-specific
  43. // logging functions
  44. #define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
  45. #define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
  46. #define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
  47. #endif