From 7c0614a0d97783baff2d3fb98bfc67f6e608d20f Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Tue, 7 Jul 2026 18:30:34 +0200 Subject: [PATCH 1/3] refactor(math): consolidate log_2_n helpers into include/math.h log_2_n_round_up() and log_2_n_round_down() were defined identically in both arch/arm32/include/asm/utils.h and arch/arm64/include/asm/utils.h, yet nothing in the tree included either header or called the functions. Move the two integer-log2 helpers into the generic include/math.h, next to round_up_pow2(), and delete the two duplicated and unused arch headers. The functions keep their original U-Boot / Texas Instruments attribution. --- so3/so3/arch/arm32/include/asm/utils.h | 56 -------------------------- so3/so3/arch/arm64/include/asm/utils.h | 56 -------------------------- so3/so3/include/math.h | 34 ++++++++++++++++ 3 files changed, 34 insertions(+), 112 deletions(-) delete mode 100644 so3/so3/arch/arm32/include/asm/utils.h delete mode 100644 so3/so3/arch/arm64/include/asm/utils.h diff --git a/so3/so3/arch/arm32/include/asm/utils.h b/so3/so3/arch/arm32/include/asm/utils.h deleted file mode 100644 index 828b86cb36..0000000000 --- a/so3/so3/arch/arm32/include/asm/utils.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * (C) Copyright 2010 - * Texas Instruments, - * Aneesh V - * - * See file CREDITS for list of people who contributed to this - * project. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA - */ -#ifndef _UTILS_H_ -#define _UTILS_H_ - -static inline s32 log_2_n_round_up(u32 n) -{ - s32 log2n = -1; - u32 temp = n; - - while (temp) { - log2n++; - temp >>= 1; - } - - if (n & (n - 1)) - return log2n + 1; /* not power of 2 - round up */ - else - return log2n; /* power of 2 */ -} - -static inline s32 log_2_n_round_down(u32 n) -{ - s32 log2n = -1; - u32 temp = n; - - while (temp) { - log2n++; - temp >>= 1; - } - - return log2n; -} - -#endif diff --git a/so3/so3/arch/arm64/include/asm/utils.h b/so3/so3/arch/arm64/include/asm/utils.h deleted file mode 100644 index 828b86cb36..0000000000 --- a/so3/so3/arch/arm64/include/asm/utils.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * (C) Copyright 2010 - * Texas Instruments, - * Aneesh V - * - * See file CREDITS for list of people who contributed to this - * project. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of - * the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, - * MA 02111-1307 USA - */ -#ifndef _UTILS_H_ -#define _UTILS_H_ - -static inline s32 log_2_n_round_up(u32 n) -{ - s32 log2n = -1; - u32 temp = n; - - while (temp) { - log2n++; - temp >>= 1; - } - - if (n & (n - 1)) - return log2n + 1; /* not power of 2 - round up */ - else - return log2n; /* power of 2 */ -} - -static inline s32 log_2_n_round_down(u32 n) -{ - s32 log2n = -1; - u32 temp = n; - - while (temp) { - log2n++; - temp >>= 1; - } - - return log2n; -} - -#endif diff --git a/so3/so3/include/math.h b/so3/so3/include/math.h index 7844149ddd..325763b6f6 100644 --- a/so3/so3/include/math.h +++ b/so3/so3/include/math.h @@ -36,4 +36,38 @@ static inline size_t round_up_pow2(size_t n) return p; } +/* + * Integer base-2 logarithm of @n, rounded up to the next integer. Originally + * from U-Boot -- Copyright (C) 2010 Texas Instruments, Aneesh V . + */ +static inline s32 log_2_n_round_up(u32 n) +{ + s32 log2n = -1; + u32 temp = n; + + while (temp) { + log2n++; + temp >>= 1; + } + + if (n & (n - 1)) + return log2n + 1; /* not power of 2 - round up */ + else + return log2n; /* power of 2 */ +} + +/* Integer base-2 logarithm of @n, rounded down (i.e. the index of the MSB). */ +static inline s32 log_2_n_round_down(u32 n) +{ + s32 log2n = -1; + u32 temp = n; + + while (temp) { + log2n++; + temp >>= 1; + } + + return log2n; +} + #endif /* MATH_H */ From b52b970e54ba80f791326a63129a7538a3951160 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Tue, 7 Jul 2026 18:35:25 +0200 Subject: [PATCH 2/3] refactor(math): move common.h integer-math macros into include/math.h min/max, min_t/max_t, DIV_ROUND_UP, DIV_ROUND_CLOSEST, __round_mask and round_up/round_down were general-purpose integer-math macros living in include/common.h. Move them next to the other helpers in include/math.h and have common.h include (within its non-assembly section) so every existing user keeps seeing them unchanged. Non-math helpers (container_of, typecheck, BUG/ASSERT, panic, ...) stay in common.h. --- so3/so3/include/common.h | 69 +------------------------------------ so3/so3/include/math.h | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 68 deletions(-) diff --git a/so3/so3/include/common.h b/so3/so3/include/common.h index bfc2f53ced..877df438e6 100644 --- a/so3/so3/include/common.h +++ b/so3/so3/include/common.h @@ -27,6 +27,7 @@ #include #include #include +#include #endif /* __ASSEMBLY__ */ @@ -49,30 +50,6 @@ extern addr_t __end[]; extern addr_t __stack_bottom[]; -#define DIV_ROUND_CLOSEST(x, divisor) \ - ({ \ - typeof(x) __x = x; \ - typeof(divisor) __d = divisor; \ - (((typeof(x)) -1) > 0 || ((typeof(divisor)) -1) > 0 || (__x) > 0) ? (((__x) + ((__d) / 2)) / (__d)) : \ - (((__x) - ((__d) / 2)) / (__d)); \ - }) - -#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) - -#define max(a, b) \ - ({ \ - typeof(a) _a = a; \ - typeof(b) _b = b; \ - _a > _b ? _a : _b; \ - }) - -#define min(a, b) \ - ({ \ - typeof(a) _a = a; \ - typeof(b) _b = b; \ - _a < _b ? _a : _b; \ - }) - /** * container_of - cast a member of a structure out to the containing structure * @@ -138,24 +115,6 @@ extern boot_stage_t boot_stage; extern uint32_t origin_cpu; extern void __backtrace(void); -/* - * ..and if you can't take the strict - * types, you can specify one yourself. - * - * Or not use min/max at all, of course. - */ -#define min_t(type, x, y) \ - ({ \ - type __x = (x); \ - type __y = (y); \ - __x < __y ? __x : __y; \ - }) -#define max_t(type, x, y) \ - ({ \ - type __x = (x); \ - type __y = (y); \ - __x > __y ? __x : __y; \ - }) /* * Check at compile time that something is of a particular type. @@ -169,32 +128,6 @@ extern void __backtrace(void); 1; \ }) -/* - * This looks more complex than it should be. But we need to - * get the type for the ~ right in round_down (it needs to be - * as wide as the result!), and we want to evaluate the macro - * arguments just once each. - */ -#define __round_mask(x, y) ((__typeof__(x)) ((y) - 1)) -/** - * round_up - round up to next specified power of 2 - * @x: the value to round - * @y: multiple to round up to (must be a power of 2) - * - * Rounds @x up to next multiple of @y (which must be a power of 2). - * To perform arbitrary rounding up, use roundup() below. - */ -#define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1) -/** - * round_down - round down to next specified power of 2 - * @x: the value to round - * @y: multiple to round down to (must be a power of 2) - * - * Rounds @x down to next multiple of @y (which must be a power of 2). - * To perform arbitrary rounding down, use rounddown() below. - */ -#define round_down(x, y) ((x) & ~__round_mask(x, y)) - #endif /* __ASSEMBLY__ */ #endif /* COMMON_H */ diff --git a/so3/so3/include/math.h b/so3/so3/include/math.h index 325763b6f6..5d0f22a7aa 100644 --- a/so3/so3/include/math.h +++ b/so3/so3/include/math.h @@ -22,6 +22,77 @@ #include +#ifndef __ASSEMBLY__ + +#define max(a, b) \ + ({ \ + typeof(a) _a = a; \ + typeof(b) _b = b; \ + _a > _b ? _a : _b; \ + }) + +#define min(a, b) \ + ({ \ + typeof(a) _a = a; \ + typeof(b) _b = b; \ + _a < _b ? _a : _b; \ + }) + +/* + * ..and if you can't take the strict + * types, you can specify one yourself. + * + * Or not use min/max at all, of course. + */ +#define min_t(type, x, y) \ + ({ \ + type __x = (x); \ + type __y = (y); \ + __x < __y ? __x : __y; \ + }) +#define max_t(type, x, y) \ + ({ \ + type __x = (x); \ + type __y = (y); \ + __x > __y ? __x : __y; \ + }) + +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) + +#define DIV_ROUND_CLOSEST(x, divisor) \ + ({ \ + typeof(x) __x = x; \ + typeof(divisor) __d = divisor; \ + (((typeof(x)) -1) > 0 || ((typeof(divisor)) -1) > 0 || (__x) > 0) ? (((__x) + ((__d) / 2)) / (__d)) : \ + (((__x) - ((__d) / 2)) / (__d)); \ + }) + +/* + * This looks more complex than it should be. But we need to + * get the type for the ~ right in round_down (it needs to be + * as wide as the result!), and we want to evaluate the macro + * arguments just once each. + */ +#define __round_mask(x, y) ((__typeof__(x)) ((y) - 1)) +/** + * round_up - round up to next specified power of 2 + * @x: the value to round + * @y: multiple to round up to (must be a power of 2) + * + * Rounds @x up to next multiple of @y (which must be a power of 2). + * To perform arbitrary rounding up, use roundup() below. + */ +#define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1) +/** + * round_down - round down to next specified power of 2 + * @x: the value to round + * @y: multiple to round down to (must be a power of 2) + * + * Rounds @x down to next multiple of @y (which must be a power of 2). + * To perform arbitrary rounding down, use rounddown() below. + */ +#define round_down(x, y) ((x) & ~__round_mask(x, y)) + /* * Round @n up to the next power of two. Returns @n unchanged when it already is * a power of two, and 1 for @n == 0. @@ -70,4 +141,6 @@ static inline s32 log_2_n_round_down(u32 n) return log2n; } +#endif /* __ASSEMBLY__ */ + #endif /* MATH_H */ From c90cc1e1e11ce64bd83297df3be0d6d448a7afa2 Mon Sep 17 00:00:00 2001 From: Daniel Rossier Date: Tue, 7 Jul 2026 18:37:42 +0200 Subject: [PATCH 3/3] refactor(bitmap): reuse round_up() from math.h bitmap.c defined a local roundup_power2() macro that is exactly the round_up() helper now living in include/math.h. Replace its single use with round_up() and drop the local macro. Also remove nbits_to_hold_value(), a local macro that was defined but never used. --- so3/so3/kernel/bitmap.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/so3/so3/kernel/bitmap.c b/so3/so3/kernel/bitmap.c index 582b368409..67483cf581 100644 --- a/so3/so3/kernel/bitmap.c +++ b/so3/so3/kernel/bitmap.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -242,8 +243,6 @@ int __bitmap_subset(const unsigned long *bitmap1, const unsigned long *bitmap2, */ #define CHUNKSZ 32 -#define nbits_to_hold_value(val) fls(val) -#define roundup_power2(val, modulus) (((val) + (modulus) - 1) & ~((modulus) - 1)) #define unhex(c) (isdigit(c) ? (c - '0') : (toupper(c) - 'A' + 10)) #define BASEDEC 10 /* fancier cpuset lists input in decimal */ @@ -269,7 +268,7 @@ int bitmap_scnprintf(char *buf, unsigned int buflen, const unsigned long *maskp, if (chunksz == 0) chunksz = CHUNKSZ; - i = roundup_power2(nmaskbits, CHUNKSZ) - CHUNKSZ; + i = round_up(nmaskbits, CHUNKSZ) - CHUNKSZ; for (; i >= 0; i -= CHUNKSZ) { chunkmask = ((1ULL << chunksz) - 1); word = i / BITS_PER_LONG;