Skip to content

Commit

Permalink
Replace inline with static inline
Browse files Browse the repository at this point in the history
Some header files were defining functions as `inline` rather than
`static inline`, which causes the compile/link process to fail at
certain optimization levels (-O0).
In some cases, an extern definition is added to a .c file which fixes
the issue, but this is not an ideal solution, and in many cases new
inline functions have been added to the header file and not to the
.c file, causing compilation with -O0 to fail.

This commit solves that issue by replacing all `inline` declarations
with `static inline`, and removing the `extern` declaration if it
existed (sometimes removing an entire C file which only contained these
definitions).
Note that all the functions declared this way were small functions,
so this change won't cause a noticeable increase in the program size.

This fixes issue #481.
  • Loading branch information
Javier Mora committed Sep 9, 2024
1 parent a3db63b commit 0477b3c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 112 deletions.
16 changes: 8 additions & 8 deletions sw/device/lib/base/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ typedef enum dif_toggle {
// * @param val A potential dif_toggle_t value.
// * @return Bool indicating validity of toggle value.
// */
// inline bool dif_is_valid_toggle(dif_toggle_t val) {
// static inline bool dif_is_valid_toggle(dif_toggle_t val) {
// switch (val) {
// case kDifToggleEnabled:
// return true;
Expand All @@ -123,7 +123,7 @@ typedef enum dif_toggle {
// * @param val A dif_toggle_t value.
// * @return Corresponding bool value.
// */
// inline bool dif_toggle_to_bool(dif_toggle_t val) {
// static inline bool dif_toggle_to_bool(dif_toggle_t val) {
// switch (val) {
// case kDifToggleEnabled:
// return true;
Expand All @@ -140,7 +140,7 @@ typedef enum dif_toggle {
// * @param val A bool value.
// * @return Corresponding dif_toggle_t value.
// */
// inline dif_toggle_t dif_bool_to_toggle(bool val) {
// static inline dif_toggle_t dif_bool_to_toggle(bool val) {
// return val ? kDifToggleEnabled : kDifToggleDisabled;
// }

Expand All @@ -150,7 +150,7 @@ typedef enum dif_toggle {
// * @param val A multi-bit bool value.
// * @return Corresponding dif_toggle_t value.
// */
// inline dif_toggle_t dif_multi_bit_bool_to_toggle(multi_bit_bool_t val) {
// static inline dif_toggle_t dif_multi_bit_bool_to_toggle(multi_bit_bool_t val) {
// switch (val) {
// case kMultiBitBool4True:
// case kMultiBitBool8True:
Expand All @@ -169,7 +169,7 @@ typedef enum dif_toggle {
// * @return Corresponding `multi_bit_bool_t` value. Invalid values resolve to
// * "false".
// */
// inline multi_bit_bool_t dif_toggle_to_multi_bit_bool4(dif_toggle_t val) {
// static inline multi_bit_bool_t dif_toggle_to_multi_bit_bool4(dif_toggle_t val) {
// if (val == kDifToggleEnabled) {
// return kMultiBitBool4True;
// } else {
Expand All @@ -184,7 +184,7 @@ typedef enum dif_toggle {
// * @return Corresponding `multi_bit_bool_t` value. Invalid values resolve to
// * "false".
// */
// inline multi_bit_bool_t dif_toggle_to_multi_bit_bool8(dif_toggle_t val) {
// static inline multi_bit_bool_t dif_toggle_to_multi_bit_bool8(dif_toggle_t val) {
// if (val == kDifToggleEnabled) {
// return kMultiBitBool8True;
// } else {
Expand All @@ -199,7 +199,7 @@ typedef enum dif_toggle {
// * @return Corresponding `multi_bit_bool_t` value. Invalid values resolve to
// * "false".
// */
// inline multi_bit_bool_t dif_toggle_to_multi_bit_bool12(dif_toggle_t val) {
// static inline multi_bit_bool_t dif_toggle_to_multi_bit_bool12(dif_toggle_t val) {
// if (val == kDifToggleEnabled) {
// return kMultiBitBool12True;
// } else {
Expand All @@ -214,7 +214,7 @@ typedef enum dif_toggle {
// * @return Corresponding `multi_bit_bool_t` value. Invalid values resolve to
// * "false".
// */
// inline multi_bit_bool_t dif_toggle_to_multi_bit_bool16(dif_toggle_t val) {
// static inline multi_bit_bool_t dif_toggle_to_multi_bit_bool16(dif_toggle_t val) {
// if (val == kDifToggleEnabled) {
// return kMultiBitBool16True;
// } else {
Expand Down
30 changes: 0 additions & 30 deletions sw/device/lib/base/bitfield.c

This file was deleted.

26 changes: 13 additions & 13 deletions sw/device/lib/base/bitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ typedef struct bitfield_field32 {
* @return Zero-extended `field` from `bitfield`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline uint32_t bitfield_field32_read(uint32_t bitfield,
static inline uint32_t bitfield_field32_read(uint32_t bitfield,
bitfield_field32_t field) {
return (bitfield >> field.index) & field.mask;
}
Expand All @@ -92,7 +92,7 @@ inline uint32_t bitfield_field32_read(uint32_t bitfield,
* @return `bitfield` with `field` set to `value`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline uint32_t bitfield_field32_write(uint32_t bitfield,
static inline uint32_t bitfield_field32_write(uint32_t bitfield,
bitfield_field32_t field,
uint32_t value) {
bitfield &= ~(field.mask << field.index);
Expand Down Expand Up @@ -123,7 +123,7 @@ typedef uint32_t bitfield_bit32_index_t;
* @return A 1-bit field that corresponds to `bit_index`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline bitfield_field32_t bitfield_bit32_to_field32(
static inline bitfield_field32_t bitfield_bit32_to_field32(
bitfield_bit32_index_t bit_index) {
return (bitfield_field32_t){
.mask = 0x1, .index = bit_index,
Expand All @@ -138,7 +138,7 @@ inline bitfield_field32_t bitfield_bit32_to_field32(
* @return `true` if the bit was one, `false` otherwise.
*/
BITFIELD_WARN_UNUSED_RESULT
inline bool bitfield_bit32_read(uint32_t bitfield,
static inline bool bitfield_bit32_read(uint32_t bitfield,
bitfield_bit32_index_t bit_index) {
return bitfield_field32_read(bitfield,
bitfield_bit32_to_field32(bit_index)) == 0x1u;
Expand All @@ -153,7 +153,7 @@ inline bool bitfield_bit32_read(uint32_t bitfield,
* @return `bitfield` with the `bit_index`th bit set to `value`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline uint32_t bitfield_bit32_write(uint32_t bitfield,
static inline uint32_t bitfield_bit32_write(uint32_t bitfield,
bitfield_bit32_index_t bit_index,
bool value) {
return bitfield_field32_write(bitfield, bitfield_bit32_to_field32(bit_index),
Expand All @@ -174,7 +174,7 @@ inline uint32_t bitfield_bit32_write(uint32_t bitfield,
* @return Zero-extended `field` from `bitfield`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline uint32_t bitfield_read(uint32_t bitfield,
static inline uint32_t bitfield_read(uint32_t bitfield,
uint32_t mask,
uint32_t index)
{
Expand All @@ -195,7 +195,7 @@ inline uint32_t bitfield_read(uint32_t bitfield,
* @return `bitfield` with `field` set to `value`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline uint32_t bitfield_write(uint32_t bitfield,
static inline uint32_t bitfield_write(uint32_t bitfield,
uint32_t mask,
uint32_t index,
uint32_t value)
Expand Down Expand Up @@ -223,7 +223,7 @@ inline uint32_t bitfield_write(uint32_t bitfield,
* @return One plus the index of the least-significant 1-bit of `bitfield`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline int32_t bitfield_find_first_set32(int32_t bitfield) {
static inline int32_t bitfield_find_first_set32(int32_t bitfield) {
return __builtin_ffs(bitfield);
}

Expand All @@ -247,7 +247,7 @@ inline int32_t bitfield_find_first_set32(int32_t bitfield) {
* @return The number of leading 0-bits in `bitfield`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline int32_t bitfield_count_leading_zeroes32(uint32_t bitfield) {
static inline int32_t bitfield_count_leading_zeroes32(uint32_t bitfield) {
return (bitfield != 0) ? __builtin_clz(bitfield) : 32;
}

Expand All @@ -271,7 +271,7 @@ inline int32_t bitfield_count_leading_zeroes32(uint32_t bitfield) {
* @return The number of trailing 0-bits in `bitfield`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline int32_t bitfield_count_trailing_zeroes32(uint32_t bitfield) {
static inline int32_t bitfield_count_trailing_zeroes32(uint32_t bitfield) {
return (bitfield != 0) ? __builtin_ctz(bitfield) : 32;
}

Expand All @@ -293,7 +293,7 @@ inline int32_t bitfield_count_trailing_zeroes32(uint32_t bitfield) {
* @return The number of 1-bits in `bitfield`.
*/
BITFIELD_WARN_UNUSED_RESULT
inline int32_t bitfield_popcount32(uint32_t bitfield) {
static inline int32_t bitfield_popcount32(uint32_t bitfield) {
return __builtin_popcount(bitfield);
}

Expand All @@ -315,7 +315,7 @@ inline int32_t bitfield_popcount32(uint32_t bitfield) {
* @return The number of 1-bits in `bitfield`, modulo 2.
*/
BITFIELD_WARN_UNUSED_RESULT
inline int32_t bitfield_parity32(uint32_t bitfield) {
static inline int32_t bitfield_parity32(uint32_t bitfield) {
return __builtin_parity(bitfield);
}

Expand All @@ -338,7 +338,7 @@ inline int32_t bitfield_parity32(uint32_t bitfield) {
* @return `bitfield` with the order of bytes reversed.
*/
BITFIELD_WARN_UNUSED_RESULT
inline uint32_t bitfield_byteswap32(uint32_t bitfield) {
static inline uint32_t bitfield_byteswap32(uint32_t bitfield) {
return __builtin_bswap32(bitfield);
}

Expand Down
3 changes: 0 additions & 3 deletions sw/device/lib/base/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ extern "C" {

#include "memory.h"

extern uint32_t read_32(const void *);
extern void write_32(uint32_t, void *);

// Some symbols below are only defined for device builds. For host builds, we
// their implementations will be provided by the host's libc implementation.
//
Expand Down
4 changes: 2 additions & 2 deletions sw/device/lib/base/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ extern "C" {
* @param ptr a word-aligned pointer pointed to at least four bytes of memory.
* @return the word `ptr` points to.
*/
inline uint32_t read_32(const void *ptr) {
static inline uint32_t read_32(const void *ptr) {
// Both GCC and Clang optimize the code below into a single word-load on most
// platforms. It is necessary and sufficient to indicate to the compiler that
// the pointer points to four bytes of four-byte-aligned memory.
Expand Down Expand Up @@ -86,7 +86,7 @@ inline uint32_t read_32(const void *ptr) {
* @param value the value to store.
* @param ptr a word-aligned pointer pointed to at least four bytes of memory.
*/
inline void write_32(uint32_t value, void *ptr) {
static inline void write_32(uint32_t value, void *ptr) {
// Both GCC and Clang optimize the code below into a single word-store on most
// platforms. See the comment in `read_32()` for more implementation-private
// information.
Expand Down
39 changes: 0 additions & 39 deletions sw/device/lib/base/mmio.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,45 +125,6 @@ void mmio_region_memcpy_to_mmio32(mmio_region_t base, uint32_t offset,
mmio_region_memcpy32(base, offset, (void *)src, len, false);
}

// `extern` declarations to give the inline functions in the
// corresponding header a link location.
extern uint8_t mmio_region_read8(mmio_region_t base, ptrdiff_t offset);
extern uint32_t mmio_region_read32(mmio_region_t base, ptrdiff_t offset);
extern void mmio_region_write8(mmio_region_t base, ptrdiff_t offset,
uint8_t value);
extern void mmio_region_write32(mmio_region_t base, ptrdiff_t offset,
uint32_t value);
extern uint32_t mmio_region_read_mask32(mmio_region_t base, ptrdiff_t offset,
uint32_t mask, uint32_t mask_index);
extern bool mmio_region_get_bit32(mmio_region_t base, ptrdiff_t offset,
uint32_t bit_index);
extern void mmio_region_nonatomic_clear_mask32(mmio_region_t base,
ptrdiff_t offset, uint32_t mask,
uint32_t mask_index);
extern void mmio_region_nonatomic_set_mask32(mmio_region_t base,
ptrdiff_t offset, uint32_t mask,
uint32_t mask_index);
extern void mmio_region_write_only_set_mask32(mmio_region_t base,
ptrdiff_t offset, uint32_t mask,
uint32_t mask_index);
extern void mmio_region_nonatomic_set_field32(mmio_region_t base,
ptrdiff_t offset,
bitfield_field32_t field,
uint32_t value);
extern void mmio_region_write_only_set_field32(mmio_region_t base,
ptrdiff_t offset,
bitfield_field32_t field,
uint32_t value);
extern void mmio_region_nonatomic_clear_bit32(mmio_region_t base,
ptrdiff_t offset,
uint32_t bit_index);
extern void mmio_region_nonatomic_set_bit32(mmio_region_t base,
ptrdiff_t offset,
uint32_t bit_index);
extern void mmio_region_write_only_set_bit32(mmio_region_t base,
ptrdiff_t offset,
uint32_t bit_index);


#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 0477b3c

Please sign in to comment.