1 /*
2  * lfs1 utility functions
3  *
4  * Copyright (c) 2017, Arm Limited. All rights reserved.
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef LFS1_UTIL_H
8 #define LFS1_UTIL_H
9 
10 // Users can override lfs1_util.h with their own configuration by defining
11 // LFS1_CONFIG as a header file to include (-DLFS1_CONFIG=lfs1_config.h).
12 //
13 // If LFS1_CONFIG is used, none of the default utils will be emitted and must be
14 // provided by the config file. To start I would suggest copying lfs1_util.h and
15 // modifying as needed.
16 #ifdef LFS1_CONFIG
17 #define LFS1_STRINGIZE(x) LFS1_STRINGIZE2(x)
18 #define LFS1_STRINGIZE2(x) #x
19 #include LFS1_STRINGIZE(LFS1_CONFIG)
20 #else
21 
22 // System includes
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include <string.h>
26 
27 #ifndef LFS1_NO_MALLOC
28 #include <stdlib.h>
29 #endif
30 #ifndef LFS1_NO_ASSERT
31 #include <assert.h>
32 #endif
33 #if !defined(LFS1_NO_DEBUG) || !defined(LFS1_NO_WARN) || !defined(LFS1_NO_ERROR)
34 #include <stdio.h>
35 #endif
36 
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41 
42 
43 // Macros, may be replaced by system specific wrappers. Arguments to these
44 // macros must not have side-effects as the macros can be removed for a smaller
45 // code footprint
46 
47 // Logging functions
48 #ifndef LFS1_NO_DEBUG
49 #define LFS1_DEBUG(fmt, ...) \
50     printf("lfs1 debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
51 #else
52 #define LFS1_DEBUG(fmt, ...)
53 #endif
54 
55 #ifndef LFS1_NO_WARN
56 #define LFS1_WARN(fmt, ...) \
57     printf("lfs1 warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
58 #else
59 #define LFS1_WARN(fmt, ...)
60 #endif
61 
62 #ifndef LFS1_NO_ERROR
63 #define LFS1_ERROR(fmt, ...) \
64     printf("lfs1 error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
65 #else
66 #define LFS1_ERROR(fmt, ...)
67 #endif
68 
69 // Runtime assertions
70 #ifndef LFS1_NO_ASSERT
71 #define LFS1_ASSERT(test) assert(test)
72 #else
73 #define LFS1_ASSERT(test)
74 #endif
75 
76 
77 // Builtin functions, these may be replaced by more efficient
78 // toolchain-specific implementations. LFS1_NO_INTRINSICS falls back to a more
79 // expensive basic C implementation for debugging purposes
80 
81 // Min/max functions for unsigned 32-bit numbers
lfs1_max(uint32_t a,uint32_t b)82 static inline uint32_t lfs1_max(uint32_t a, uint32_t b) {
83     return (a > b) ? a : b;
84 }
85 
lfs1_min(uint32_t a,uint32_t b)86 static inline uint32_t lfs1_min(uint32_t a, uint32_t b) {
87     return (a < b) ? a : b;
88 }
89 
90 // Find the next smallest power of 2 less than or equal to a
lfs1_npw2(uint32_t a)91 static inline uint32_t lfs1_npw2(uint32_t a) {
92 #if !defined(LFS1_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
93     return 32 - __builtin_clz(a-1);
94 #else
95     uint32_t r = 0;
96     uint32_t s;
97     a -= 1;
98     s = (a > 0xffff) << 4; a >>= s; r |= s;
99     s = (a > 0xff  ) << 3; a >>= s; r |= s;
100     s = (a > 0xf   ) << 2; a >>= s; r |= s;
101     s = (a > 0x3   ) << 1; a >>= s; r |= s;
102     return (r | (a >> 1)) + 1;
103 #endif
104 }
105 
106 // Count the number of trailing binary zeros in a
107 // lfs1_ctz(0) may be undefined
lfs1_ctz(uint32_t a)108 static inline uint32_t lfs1_ctz(uint32_t a) {
109 #if !defined(LFS1_NO_INTRINSICS) && defined(__GNUC__)
110     return __builtin_ctz(a);
111 #else
112     return lfs1_npw2((a & -a) + 1) - 1;
113 #endif
114 }
115 
116 // Count the number of binary ones in a
lfs1_popc(uint32_t a)117 static inline uint32_t lfs1_popc(uint32_t a) {
118 #if !defined(LFS1_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 
127 // Find the sequence comparison of a and b, this is the distance
128 // between a and b ignoring overflow
lfs1_scmp(uint32_t a,uint32_t b)129 static inline int lfs1_scmp(uint32_t a, uint32_t b) {
130     return (int)(unsigned)(a - b);
131 }
132 
133 // Convert from 32-bit little-endian to native order
lfs1_fromle32(uint32_t a)134 static inline uint32_t lfs1_fromle32(uint32_t a) {
135 #if !defined(LFS1_NO_INTRINSICS) && ( \
136     (defined(  BYTE_ORDER  ) &&   BYTE_ORDER   ==   ORDER_LITTLE_ENDIAN  ) || \
137     (defined(__BYTE_ORDER  ) && __BYTE_ORDER   == __ORDER_LITTLE_ENDIAN  ) || \
138     (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
139     return a;
140 #elif !defined(LFS1_NO_INTRINSICS) && ( \
141     (defined(  BYTE_ORDER  ) &&   BYTE_ORDER   ==   ORDER_BIG_ENDIAN  ) || \
142     (defined(__BYTE_ORDER  ) && __BYTE_ORDER   == __ORDER_BIG_ENDIAN  ) || \
143     (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
144     return __builtin_bswap32(a);
145 #else
146     return (((uint8_t*)&a)[0] <<  0) |
147            (((uint8_t*)&a)[1] <<  8) |
148            (((uint8_t*)&a)[2] << 16) |
149            (((uint8_t*)&a)[3] << 24);
150 #endif
151 }
152 
153 // Convert to 32-bit little-endian from native order
lfs1_tole32(uint32_t a)154 static inline uint32_t lfs1_tole32(uint32_t a) {
155     return lfs1_fromle32(a);
156 }
157 
158 // Calculate CRC-32 with polynomial = 0x04c11db7
159 void lfs1_crc(uint32_t *crc, const void *buffer, size_t size);
160 
161 // Allocate memory, only used if buffers are not provided to littlefs
lfs1_malloc(size_t size)162 static inline void *lfs1_malloc(size_t size) {
163 #ifndef LFS1_NO_MALLOC
164     return malloc(size);
165 #else
166     (void)size;
167     return NULL;
168 #endif
169 }
170 
171 // Deallocate memory, only used if buffers are not provided to littlefs
lfs1_free(void * p)172 static inline void lfs1_free(void *p) {
173 #ifndef LFS1_NO_MALLOC
174     free(p);
175 #else
176     (void)p;
177 #endif
178 }
179 
180 
181 #ifdef __cplusplus
182 } /* extern "C" */
183 #endif
184 
185 #endif
186 #endif
187