-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
target/riscv: Replace [sg]et_field macros with functions.
Compilers are good at optimizing, and with functions it's abundantly clear what all the types involved are. This change means we don't have to be super careful about the type of values because of what the macro might do to them that might cause overflow. The only place where the return type matters is in printf-style functions, and I made get_value32() for those cases where a change was needed. This should set the stage for simply copying the latest debug_defines.h from the debug spec build again. Change-Id: I5fb19d0cfc1e20137832a7b344b05db215ce00e1 Signed-off-by: Tim Newsome <[email protected]>
- Loading branch information
Showing
5 changed files
with
62 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-or-later */ | ||
|
||
#ifndef FIELD_HELPERS_H | ||
#define FIELD_HELPERS_H | ||
|
||
#include <stdint.h> | ||
#include <assert.h> | ||
|
||
static inline uint64_t get_field(uint64_t reg, uint64_t mask) | ||
{ | ||
return (reg & mask) / (mask & ~(mask << 1)); | ||
} | ||
|
||
static inline uint32_t get_field32(uint64_t reg, uint64_t mask) | ||
{ | ||
uint64_t value = get_field(reg, mask); | ||
assert(value <= UINT32_MAX); | ||
return value; | ||
} | ||
static inline uint64_t set_field(uint64_t reg, uint64_t mask, uint64_t val) | ||
{ | ||
/* Clear current value from field. */ | ||
reg &= ~mask; | ||
uint64_t low_field_bit = mask & ~(mask << 1); | ||
reg |= (val * low_field_bit) & mask; | ||
return reg; | ||
} | ||
|
||
static inline uint64_t field_value(uint64_t mask, uint64_t val) | ||
{ | ||
return set_field(0, mask, val); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters