summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJianjun Wang <jianjun.wang@mediatek.com>2021-11-30 10:51:53 +0800
committerHung-Te Lin <hungte@chromium.org>2021-12-13 02:57:07 +0000
commit8bb59ca2faee83ba50850900c8b4edf9331ae931 (patch)
treefab7bde1be5ff24af106a41c8f47b7d4fac75762
parent5588f34a35209f34d9061e6a83856289450d8131 (diff)
downloadcoreboot-8bb59ca2faee83ba50850900c8b4edf9331ae931.tar.gz
coreboot-8bb59ca2faee83ba50850900c8b4edf9331ae931.tar.bz2
coreboot-8bb59ca2faee83ba50850900c8b4edf9331ae931.zip
lib: Add __fls() (Find Last Set)
Implement __fls() as an alias for log2(), and remove the duplicate definitions in commonlib/storage/sdhci.c. Signed-off-by: Jianjun Wang <jianjun.wang@mediatek.com> Change-Id: Ib458abfec7e03b2979569a8440a6e69b0285ac32 Reviewed-on: https://review.coreboot.org/c/coreboot/+/59738 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-by: Yu-Ping Wu <yupingso@google.com>
-rw-r--r--payloads/libpayload/include/libpayload.h3
-rw-r--r--src/commonlib/storage/sdhci.c35
-rw-r--r--src/include/lib.h3
-rw-r--r--tests/lib/lib-test.c10
4 files changed, 19 insertions, 32 deletions
diff --git a/payloads/libpayload/include/libpayload.h b/payloads/libpayload/include/libpayload.h
index e08d21198361..389571071a50 100644
--- a/payloads/libpayload/include/libpayload.h
+++ b/payloads/libpayload/include/libpayload.h
@@ -457,6 +457,8 @@ static inline int clz(u32 x)
static inline int log2(u32 x) { return (int)sizeof(x) * 8 - clz(x) - 1; }
/* Find First Set: __ffs(0xf) == 0, __ffs(0) == -1, __ffs(1 << 31) == 31 */
static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
+/* Find Last Set: __fls(1) == 0, __fls(5) == 2, __fls(1 << 31) == 31 */
+static inline int __fls(u32 x) { return log2(x); }
static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
static inline int clz64(u64 x)
@@ -466,6 +468,7 @@ static inline int clz64(u64 x)
static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
+static inline int __fls64(u64 x) { return log2_64(x); }
/** @} */
/**
diff --git a/src/commonlib/storage/sdhci.c b/src/commonlib/storage/sdhci.c
index 6d39a45f5e52..16420d99fa9d 100644
--- a/src/commonlib/storage/sdhci.c
+++ b/src/commonlib/storage/sdhci.c
@@ -6,14 +6,15 @@
#include "bouncebuf.h"
#include <commonlib/sd_mmc_ctrlr.h>
#include <commonlib/sdhci.h>
+#include <commonlib/stdlib.h>
#include <commonlib/storage.h>
#include <delay.h>
#include <endian.h>
+#include <lib.h>
#include "sdhci.h"
#include "sd_mmc.h"
#include "storage.h"
#include <timer.h>
-#include <commonlib/stdlib.h>
#define DMA_AVAILABLE ((CONFIG(SDHCI_ADMA_IN_BOOTBLOCK) && ENV_BOOTBLOCK) \
|| (CONFIG(SDHCI_ADMA_IN_VERSTAGE) && ENV_SEPARATE_VERSTAGE) \
@@ -411,36 +412,6 @@ static int sdhci_set_clock(struct sdhci_ctrlr *sdhci_ctrlr, unsigned int clock)
return 0;
}
-/* Find leftmost set bit in a 32 bit integer */
-static int fls(u32 x)
-{
- int r = 32;
-
- if (!x)
- return 0;
- if (!(x & 0xffff0000u)) {
- x <<= 16;
- r -= 16;
- }
- if (!(x & 0xff000000u)) {
- x <<= 8;
- r -= 8;
- }
- if (!(x & 0xf0000000u)) {
- x <<= 4;
- r -= 4;
- }
- if (!(x & 0xc0000000u)) {
- x <<= 2;
- r -= 2;
- }
- if (!(x & 0x80000000u)) {
- x <<= 1;
- r -= 1;
- }
- return r;
-}
-
static void sdhci_set_power(struct sdhci_ctrlr *sdhci_ctrlr,
unsigned short power)
{
@@ -718,7 +689,7 @@ static int sdhci_init(struct sdhci_ctrlr *sdhci_ctrlr)
if (rv)
return rv; /* The error has been already reported */
- sdhci_set_power(sdhci_ctrlr, fls(ctrlr->voltages) - 1);
+ sdhci_set_power(sdhci_ctrlr, __fls(ctrlr->voltages));
if (ctrlr->caps & DRVR_CAP_NO_CD) {
unsigned int status;
diff --git a/src/include/lib.h b/src/include/lib.h
index b3cedb571c78..863888e16ba3 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -53,6 +53,8 @@ static inline int clz(u32 x) { return x ? __builtin_clz(x) : sizeof(x) * 8; }
static inline int log2(u32 x) { return sizeof(x) * 8 - clz(x) - 1; }
/* Find First Set: __ffs(1) == 0, __ffs(0) == -1, __ffs(1<<31) == 31 */
static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
+/* Find Last Set: __fls(1) == 0, __fls(5) == 2, __fls(1 << 31) == 31 */
+static inline int __fls(u32 x) { return log2(x); }
/* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2_ceil(5) == 3 */
static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x - 1) + 1; }
@@ -61,5 +63,6 @@ static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
static inline int clz64(u64 x) { return x ? __builtin_clzll(x) : sizeof(x) * 8; }
static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
+static inline int __fls64(u64 x) { return log2_64(x); }
#endif /* __LIB_H__ */
diff --git a/tests/lib/lib-test.c b/tests/lib/lib-test.c
index adaa2f9500ef..826c6c563a94 100644
--- a/tests/lib/lib-test.c
+++ b/tests/lib/lib-test.c
@@ -37,6 +37,15 @@ void test_ffs(void **state)
assert_int_equal(__ffs(0xffffffff), 0);
}
+void test_fls(void **state)
+{
+ assert_int_equal(__fls(0x0), -1);
+ assert_int_equal(__fls(0x1), 0);
+ assert_int_equal(__fls(0x5), 2);
+ assert_int_equal(__fls(0x80000000), 31);
+ assert_int_equal(__fls(0xffffffff), 31);
+}
+
void test_log2_ceil(void **state)
{
assert_int_equal(log2_ceil(0x0), -1);
@@ -53,6 +62,7 @@ int main(void)
cmocka_unit_test(test_clz),
cmocka_unit_test(test_log2),
cmocka_unit_test(test_ffs),
+ cmocka_unit_test(test_fls),
cmocka_unit_test(test_log2_ceil),
};