17 #define LFS2_STRINGIZE(x) LFS2_STRINGIZE2(x) 18 #define LFS2_STRINGIZE2(x) #x 19 #include LFS2_STRINGIZE(LFS2_CONFIG) 28 #ifndef LFS2_NO_MALLOC 31 #ifndef LFS2_NO_ASSERT 34 #if !defined(LFS2_NO_DEBUG) || \ 35 !defined(LFS2_NO_WARN) || \ 36 !defined(LFS2_NO_ERROR) || \ 37 defined(LFS2_YES_TRACE) 52 #include "mbed_debug.h" 53 #include "mbed_assert.h" 54 #include "cmsis_compiler.h" 56 #define MBED_LFS2_ENABLE_INFO false 57 #define MBED_LFS2_ENABLE_DEBUG true 58 #define MBED_LFS2_ENABLE_WARN true 59 #define MBED_LFS2_ENABLE_ERROR true 60 #define MBED_LFS2_ENABLE_ASSERT true 61 #define MBED_LFS2_INTRINSICS true 65 #if defined(LFS2_YES_TRACE) && MBED_LFS2_ENABLE_TRACE 66 #define LFS2_TRACE_(fmt, ...) \ 67 printf("%s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 68 #define LFS2_TRACE(...) LFS2_TRACE_(__VA_ARGS__, "") 69 #elif defined(LFS2_YES_TRACE) && !defined(MBED_LFS2_ENABLE_TRACE) 70 #define LFS2_TRACE_(fmt, ...) \ 71 debug("%s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 72 #define LFS2_TRACE(...) LFS2_TRACE_(__VA_ARGS__, "") 74 #define LFS2_TRACE(...) 77 #if !defined(LFS2_NO_DEBUG) && MBED_LFS2_ENABLE_DEBUG 78 #define LFS2_DEBUG_(fmt, ...) \ 79 printf("%s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 80 #define LFS2_DEBUG(...) LFS2_DEBUG_(__VA_ARGS__, "") 81 #elif !defined(LFS2_NO_DEBUG) && !defined(MBED_LFS2_ENABLE_DEBUG) 82 #define LFS2_DEBUG_(fmt, ...) \ 83 debug("%s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 84 #define LFS2_DEBUG(...) LFS2_DEBUG_(__VA_ARGS__, "") 86 #define LFS2_DEBUG(...) 89 #if !defined(LFS2_NO_WARN) && MBED_LFS2_ENABLE_WARN 90 #define LFS2_WARN_(fmt, ...) \ 91 printf("%s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 92 #define LFS2_WARN(...) LFS2_WARN_(__VA_ARGS__, "") 93 #elif !defined(LFS2_NO_WARN) && !defined(MBED_LFS2_ENABLE_WARN) 94 #define LFS2_WARN_(fmt, ...) \ 95 debug("%s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 96 #define LFS2_WARN(...) LFS2_WARN_(__VA_ARGS__, "") 98 #define LFS2_WARN(...) 101 #if !defined(LFS2_NO_ERROR) && MBED_LFS2_ENABLE_ERROR 102 #define LFS2_ERROR_(fmt, ...) \ 103 printf("%s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 104 #define LFS2_ERROR(...) LFS2_ERROR_(__VA_ARGS__, "") 105 #elif !defined(LFS2_NO_ERROR) && !defined(MBED_LFS2_ENABLE_ERROR) 106 #define LFS2_ERROR_(fmt, ...) \ 107 debug("%s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__) 108 #define LFS2_ERROR(...) LFS2_ERROR_(__VA_ARGS__, "") 110 #define LFS2_ERROR(...) 114 #if !defined(LFS2_NO_ASSERT) && MBED_LFS2_ENABLE_ASSERT 115 #define LFS2_ASSERT(test) assert(test) 116 #elif !defined(LFS2_NO_ASSERT) && !defined(MBED_LFS2_ENABLE_ASSERT) 117 #define LFS2_ASSERT(test) MBED_ASSERT(test) 119 #define LFS2_ASSERT(test) 128 static inline uint32_t lfs2_max(uint32_t a, uint32_t b) {
129 return (a > b) ? a : b;
132 static inline uint32_t lfs2_min(uint32_t a, uint32_t b) {
133 return (a < b) ? a : b;
137 static inline uint32_t lfs2_aligndown(uint32_t a, uint32_t alignment) {
138 return a - (a % alignment);
141 static inline uint32_t lfs2_alignup(uint32_t a, uint32_t alignment) {
142 return lfs2_aligndown(a + alignment-1, alignment);
146 static inline uint32_t lfs2_npw2(uint32_t a) {
147 #if !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && \ 148 (defined(__GNUC__) || defined(__CC_ARM)) 149 return 32 - __builtin_clz(a-1);
154 s = (a > 0xffff) << 4; a >>= s; r |= s;
155 s = (a > 0xff ) << 3; a >>= s; r |= s;
156 s = (a > 0xf ) << 2; a >>= s; r |= s;
157 s = (a > 0x3 ) << 1; a >>= s; r |= s;
158 return (r | (a >> 1)) + 1;
164 static inline uint32_t lfs2_ctz(uint32_t a) {
165 #if !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && \ 167 return __builtin_ctz(a);
169 return lfs2_npw2((a & -a) + 1) - 1;
174 static inline uint32_t lfs2_popc(uint32_t a) {
175 #if !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && \ 176 (defined(__GNUC__) || defined(__CC_ARM)) 177 return __builtin_popcount(a);
179 a = a - ((a >> 1) & 0x55555555);
180 a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
181 return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
187 static inline int lfs2_scmp(uint32_t a, uint32_t b) {
188 return (
int)(unsigned)(a - b);
192 static inline uint32_t lfs2_fromle32(uint32_t a) {
193 #if !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && ( \ 194 (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \ 195 (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \ 196 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) 198 #elif !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && ( \ 199 (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \ 200 (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \ 201 (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) 202 return __builtin_bswap32(a);
204 return (((uint8_t*)&a)[0] << 0) |
205 (((uint8_t*)&a)[1] << 8) |
206 (((uint8_t*)&a)[2] << 16) |
207 (((uint8_t*)&a)[3] << 24);
211 static inline uint32_t lfs2_tole32(uint32_t a) {
212 return lfs2_fromle32(a);
216 static inline uint32_t lfs2_rbit(uint32_t a) {
217 #if !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && \ 221 a = ((a & 0xaaaaaaaa) >> 1) | ((a & 0x55555555) << 1);
222 a = ((a & 0xcccccccc) >> 2) | ((a & 0x33333333) << 2);
223 a = ((a & 0xf0f0f0f0) >> 4) | ((a & 0x0f0f0f0f) << 4);
224 a = ((a & 0xff00ff00) >> 8) | ((a & 0x00ff00ff) << 8);
225 a = (a >> 16) | (a << 16);
231 static inline uint32_t lfs2_frombe32(uint32_t a) {
232 #if !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && ( \ 233 (defined( BYTE_ORDER ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \ 234 (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \ 235 (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) 236 return __builtin_bswap32(a);
237 #elif !defined(LFS2_NO_INTRINSICS) && MBED_LFS2_INTRINSICS && ( \ 238 (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \ 239 (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \ 240 (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) 243 return (((uint8_t*)&a)[0] << 24) |
244 (((uint8_t*)&a)[1] << 16) |
245 (((uint8_t*)&a)[2] << 8) |
246 (((uint8_t*)&a)[3] << 0);
250 static inline uint32_t lfs2_tobe32(uint32_t a) {
251 return lfs2_frombe32(a);
255 uint32_t lfs2_crc(uint32_t crc,
const void *buffer,
size_t size);
259 static inline void *lfs2_malloc(
size_t size) {
260 #ifndef LFS2_NO_MALLOC 269 static inline void lfs2_free(
void *p) {
270 #ifndef LFS2_NO_MALLOC