lfs_util.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * lfs utility functions
  3. *
  4. * Copyright (c) 2017, Arm Limited. All rights reserved.
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef LFS_UTIL_H
  8. #define LFS_UTIL_H
  9. // Users can override lfs_util.h with their own configuration by defining
  10. // LFS_CONFIG as a header file to include (-DLFS_CONFIG=lfs_config.h).
  11. //
  12. // If LFS_CONFIG is used, none of the default utils will be emitted and must be
  13. // provided by the config file. To start, I would suggest copying lfs_util.h
  14. // and modifying as needed.
  15. #ifdef LFS_CONFIG
  16. #define LFS_STRINGIZE(x) LFS_STRINGIZE2(x)
  17. #define LFS_STRINGIZE2(x) #x
  18. #include LFS_STRINGIZE(LFS_CONFIG)
  19. #else
  20. // System includes
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <string.h>
  24. #include <inttypes.h>
  25. #ifndef LFS_NO_MALLOC
  26. #include <stdlib.h>
  27. #endif
  28. #ifndef LFS_NO_ASSERT
  29. #include <assert.h>
  30. #endif
  31. #if !defined(LFS_NO_DEBUG) || \
  32. !defined(LFS_NO_WARN) || \
  33. !defined(LFS_NO_ERROR) || \
  34. defined(LFS_YES_TRACE)
  35. #include <stdio.h>
  36. #endif
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. // Macros, may be replaced by system specific wrappers. Arguments to these
  42. // macros must not have side-effects as the macros can be removed for a smaller
  43. // code footprint
  44. // Logging functions
  45. #ifdef LFS_YES_TRACE
  46. #define LFS_TRACE(fmt, ...) \
  47. printf("lfs_trace:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  48. #else
  49. #define LFS_TRACE(fmt, ...)
  50. #endif
  51. #ifndef LFS_NO_DEBUG
  52. #define LFS_DEBUG(fmt, ...) \
  53. printf("lfs_debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  54. #else
  55. #define LFS_DEBUG(fmt, ...)
  56. #endif
  57. #ifndef LFS_NO_WARN
  58. #define LFS_WARN(fmt, ...) \
  59. printf("lfs_warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  60. #else
  61. #define LFS_WARN(fmt, ...)
  62. #endif
  63. #ifndef LFS_NO_ERROR
  64. #define LFS_ERROR(fmt, ...) \
  65. printf("lfs_error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  66. #else
  67. #define LFS_ERROR(fmt, ...)
  68. #endif
  69. // Runtime assertions
  70. #ifndef LFS_NO_ASSERT
  71. #define LFS_ASSERT(test) assert(test)
  72. #else
  73. #define LFS_ASSERT(test)
  74. #endif
  75. // Builtin functions, these may be replaced by more efficient
  76. // toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
  77. // expensive basic C implementation for debugging purposes
  78. // Min/max functions for unsigned 32-bit numbers
  79. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  80. return (a > b) ? a : b;
  81. }
  82. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  83. return (a < b) ? a : b;
  84. }
  85. // Align to nearest multiple of a size
  86. static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
  87. return a - (a % alignment);
  88. }
  89. static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
  90. return lfs_aligndown(a + alignment-1, alignment);
  91. }
  92. // Find the smallest power of 2 greater than or equal to a
  93. static inline uint32_t lfs_npw2(uint32_t a) {
  94. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  95. return 32 - __builtin_clz(a-1);
  96. #else
  97. uint32_t r = 0;
  98. uint32_t s;
  99. a -= 1;
  100. s = (a > 0xffff) << 4; a >>= s; r |= s;
  101. s = (a > 0xff ) << 3; a >>= s; r |= s;
  102. s = (a > 0xf ) << 2; a >>= s; r |= s;
  103. s = (a > 0x3 ) << 1; a >>= s; r |= s;
  104. return (r | (a >> 1)) + 1;
  105. #endif
  106. }
  107. // Count the number of trailing binary zeros in a
  108. // lfs_ctz(0) may be undefined
  109. static inline uint32_t lfs_ctz(uint32_t a) {
  110. #if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
  111. return __builtin_ctz(a);
  112. #else
  113. return lfs_npw2((a & -a) + 1) - 1;
  114. #endif
  115. }
  116. // Count the number of binary ones in a
  117. static inline uint32_t lfs_popc(uint32_t a) {
  118. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  119. return __builtin_popcount(a);
  120. #else
  121. a = a - ((a >> 1) & 0x55555555);
  122. a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
  123. return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
  124. #endif
  125. }
  126. // Find the sequence comparison of a and b, this is the distance
  127. // between a and b ignoring overflow
  128. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  129. return (int)(unsigned)(a - b);
  130. }
  131. // Convert between 32-bit little-endian and native order
  132. static inline uint32_t lfs_fromle32(uint32_t a) {
  133. #if !defined(LFS_NO_INTRINSICS) && ( \
  134. (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  135. (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  136. (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  137. return a;
  138. #elif !defined(LFS_NO_INTRINSICS) && ( \
  139. (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  140. (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  141. (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  142. return __builtin_bswap32(a);
  143. #else
  144. return (((uint8_t*)&a)[0] << 0) |
  145. (((uint8_t*)&a)[1] << 8) |
  146. (((uint8_t*)&a)[2] << 16) |
  147. (((uint8_t*)&a)[3] << 24);
  148. #endif
  149. }
  150. static inline uint32_t lfs_tole32(uint32_t a) {
  151. return lfs_fromle32(a);
  152. }
  153. // Convert between 32-bit big-endian and native order
  154. static inline uint32_t lfs_frombe32(uint32_t a) {
  155. #if !defined(LFS_NO_INTRINSICS) && ( \
  156. (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  157. (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  158. (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  159. return __builtin_bswap32(a);
  160. #elif !defined(LFS_NO_INTRINSICS) && ( \
  161. (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  162. (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  163. (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  164. return a;
  165. #else
  166. return (((uint8_t*)&a)[0] << 24) |
  167. (((uint8_t*)&a)[1] << 16) |
  168. (((uint8_t*)&a)[2] << 8) |
  169. (((uint8_t*)&a)[3] << 0);
  170. #endif
  171. }
  172. static inline uint32_t lfs_tobe32(uint32_t a) {
  173. return lfs_frombe32(a);
  174. }
  175. // Calculate CRC-32 with polynomial = 0x04c11db7
  176. uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
  177. // Allocate memory, only used if buffers are not provided to littlefs
  178. // Note, memory must be 64-bit aligned
  179. static inline void *lfs_malloc(size_t size) {
  180. #ifndef LFS_NO_MALLOC
  181. return malloc(size);
  182. #else
  183. (void)size;
  184. return NULL;
  185. #endif
  186. }
  187. // Deallocate memory, only used if buffers are not provided to littlefs
  188. static inline void lfs_free(void *p) {
  189. #ifndef LFS_NO_MALLOC
  190. free(p);
  191. #else
  192. (void)p;
  193. #endif
  194. }
  195. #ifdef __cplusplus
  196. } /* extern "C" */
  197. #endif
  198. #endif
  199. #endif