Skip to content

Commit b9aaeab

Browse files
committed
Adopted LFS_FORCEINLINE
May revisit this in the future, but this is the best solution I can think of right now, that doesn't run into duplicate macro-argument side-effect issues... Statement expressions would be another solution, but that's even less portable! At the moment this is only used for functions that implement LFSR_DATA_* and LFSR_RAT_* macros. These _need_ to be inlined to avoid a large code-size explosion, and GCC seems to have issues with this. For most of the other inlinable functions, relying on C99's inline + compiler heuristics seems to be fine. Code changes: before: 36304 2576 640 no-forceinline: 36460 (+0.4%) 2664 (+3.4%) 640 (+0.0%) yes-forceinline: 36300 (-0.0%) 2576 (+0.0%) 640 (+0.0%)
1 parent 1eb1ec3 commit b9aaeab

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

lfs.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,8 +1663,10 @@ static inline lfs_size_t lfsr_data_size(lfsr_data_t data) {
16631663
}
16641664

16651665
// data slicing
1666-
// TODO what to do about this inlining situation?
1667-
__attribute__((always_inline))
1666+
#define LFSR_DATA_SLICE(_data, _off, _size) \
1667+
((struct {lfsr_data_t d;}){lfsr_data_fromslice(_data, _off, _size)}.d)
1668+
1669+
LFS_FORCEINLINE
16681670
static inline lfsr_data_t lfsr_data_fromslice(lfsr_data_t data,
16691671
lfs_ssize_t off, lfs_ssize_t size) {
16701672
// limit our off/size to data range, note the use of unsigned casts
@@ -1690,17 +1692,19 @@ static inline lfsr_data_t lfsr_data_fromslice(lfsr_data_t data,
16901692
return data;
16911693
}
16921694

1693-
#define LFSR_DATA_SLICE(_data, _off, _size) \
1694-
((struct {lfsr_data_t d;}){lfsr_data_fromslice(_data, _off, _size)}.d)
1695+
#define LFSR_DATA_TRUNCATE(_data, _size) \
1696+
((struct {lfsr_data_t d;}){lfsr_data_fromtruncate(_data, _size)}.d)
16951697

1698+
LFS_FORCEINLINE
16961699
static inline lfsr_data_t lfsr_data_fromtruncate(lfsr_data_t data,
16971700
lfs_size_t size) {
16981701
return LFSR_DATA_SLICE(data, -1, size);
16991702
}
17001703

1701-
#define LFSR_DATA_TRUNCATE(_data, _size) \
1702-
((struct {lfsr_data_t d;}){lfsr_data_fromtruncate(_data, _size)}.d)
1704+
#define LFSR_DATA_FRUNCATE(_data, _size) \
1705+
((struct {lfsr_data_t d;}){lfsr_data_fromfruncate(_data, _size)}.d)
17031706

1707+
LFS_FORCEINLINE
17041708
static inline lfsr_data_t lfsr_data_fromfruncate(lfsr_data_t data,
17051709
lfs_size_t size) {
17061710
return LFSR_DATA_SLICE(data,
@@ -1710,9 +1714,6 @@ static inline lfsr_data_t lfsr_data_fromfruncate(lfsr_data_t data,
17101714
-1);
17111715
}
17121716

1713-
#define LFSR_DATA_FRUNCATE(_data, _size) \
1714-
((struct {lfsr_data_t d;}){lfsr_data_fromfruncate(_data, _size)}.d)
1715-
17161717

17171718
// macros for le32/leb128/lleb128 encoding, these are useful for
17181719
// building rats
@@ -2023,6 +2024,7 @@ typedef struct lfsr_rat {
20232024
#define LFSR_RAT(_tag, _weight, _data) \
20242025
((struct {lfsr_rat_t a;}){lfsr_rat(_tag, _weight, _data)}.a)
20252026

2027+
LFS_FORCEINLINE
20262028
static inline lfsr_rat_t lfsr_rat(
20272029
lfsr_tag_t tag, lfsr_srid_t weight, lfsr_data_t data) {
20282030
// only simple data works here

lfs_util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ extern "C"
229229
#endif
230230

231231

232+
// Some function attributes, no way around these
233+
234+
// Force a function to be inlined
235+
#if !defined(LFS_NO_BUILTINS) && defined(__GNUC__)
236+
#define LFS_FORCEINLINE __attribute__((always_inline))
237+
#else
238+
#define LFS_FORCEINLINE
239+
#endif
240+
241+
232242
// Builtin functions, these may be replaced by more efficient
233243
// toolchain-specific implementations. LFS_NO_BUILTINS falls back to a more
234244
// expensive basic C implementation for debugging purposes

0 commit comments

Comments
 (0)