refactor(math): consolidate integer-math helpers into include/math.h#270
Merged
daniel-rossier merged 3 commits intoJul 7, 2026
Conversation
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.
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 <math.h> (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.
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.
e89efc7
into
feat-device-optimize-parse_dtb-function
2 checks passed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
While deciding where a generic
round_up_pow2()should live (#269), I found other integer-math helpers scattered and duplicated across the tree. This PR gives them a single home ininclude/math.h.Changes
1. De-duplicate the arch
log_2_n_*helpers —log_2_n_round_up()/log_2_n_round_down()were defined identically in botharch/arm32/include/asm/utils.handarch/arm64/include/asm/utils.h, and nothing in the tree included either header or called the functions (verified). Moved intoinclude/math.h; the two dead, duplicated arch headers are deleted. Original U-Boot / Texas Instruments attribution preserved.2. Centralize the
common.hinteger-math macros —min/max,min_t/max_t,DIV_ROUND_UP,DIV_ROUND_CLOSEST,__round_mask,round_up/round_downmoved frominclude/common.hintoinclude/math.h.common.hnow#include <math.h>inside its non-assembly section, so every existing user keeps seeing these macros unchanged. Non-math helpers (container_of,typecheck,BUG/ASSERT,panic, …) stay incommon.h.3. Reuse
round_up()inbitmap.c—bitmap.chad a localroundup_power2()macro identical toround_up(); its single use now callsround_up()and the local macro is dropped. The unused localnbits_to_hold_value()macro is removed as well.include/math.hbody is wrapped in#ifndef __ASSEMBLY__so assembly translation units that pullcommon.hare unaffected.Verification
virt64_defconfig) succeeds, including the.Sfiles that includecommon.h— no errors, nomin/max/round_*redefinition or implicit-declaration warnings.clang-formatreports no diff on the touched files.round_up(v, 32)verified equal to the oldroundup_power2(v, 32)over v ∈ [0, 1024].lib/hashmap.c(which includesmath.h) still passes, giving a second independent compile ofmath.h.math.hpulls onlytypes.h); no.crelies on a libc<math.h>.