From e0a04cbc3d159a84a1aa845743a0b89981e16224 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 25 Feb 2025 15:25:08 -0700 Subject: libmisc: macro.h: Add LM_FLOORLOG2 --- libmisc/tests/test_macro.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_macro.c b/libmisc/tests/test_macro.c index 7cbf9d3..69655d1 100644 --- a/libmisc/tests/test_macro.c +++ b/libmisc/tests/test_macro.c @@ -1,6 +1,6 @@ /* libmisc/tests/test_macro.c - Tests for * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -9,6 +9,7 @@ #include "test.h" int main() { + printf("== LM_NEXT_POWER_OF_2 =====================================\n"); /* valid down to 0. */ test_assert(LM_NEXT_POWER_OF_2(0) == 1); test_assert(LM_NEXT_POWER_OF_2(1) == 2); @@ -28,5 +29,27 @@ int main() { /* Valid up to 0x8000000000000000-1 = (1<<63)-1 */ test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000) == 0); /* :( */ + printf("== LM_FLOORLOG2 ===========================================\n"); + /* valid down to 1. */ + test_assert(LM_FLOORLOG2(1) == 0); + test_assert(LM_FLOORLOG2(2) == 1); + test_assert(LM_FLOORLOG2(3) == 1); + test_assert(LM_FLOORLOG2(4) == 2); + test_assert(LM_FLOORLOG2(5) == 2); + test_assert(LM_FLOORLOG2(6) == 2); + test_assert(LM_FLOORLOG2(7) == 2); + test_assert(LM_FLOORLOG2(8) == 3); + /* ... */ + test_assert(LM_FLOORLOG2(16) == 4); + /* ... */ + test_assert(LM_FLOORLOG2(0x80000000) == 31); + /* ... */ + test_assert(LM_FLOORLOG2(0xFFFFFFFF) == 31); + test_assert(LM_FLOORLOG2(0x100000000) == 32); + /* ... */ + test_assert(LM_FLOORLOG2(0x8000000000000000) == 63); + /* ... */ + test_assert(LM_FLOORLOG2(0xFFFFFFFFFFFFFFFF) == 63); + return 0; } -- cgit v1.2.3-2-g168b