Skip to content

refactor(math): consolidate integer-math helpers into include/math.h#270

Merged
daniel-rossier merged 3 commits into
feat-device-optimize-parse_dtb-functionfrom
cleanup/consolidate-arch-log2-into-math
Jul 7, 2026
Merged

refactor(math): consolidate integer-math helpers into include/math.h#270
daniel-rossier merged 3 commits into
feat-device-optimize-parse_dtb-functionfrom
cleanup/consolidate-arch-log2-into-math

Conversation

@daniel-rossier

@daniel-rossier daniel-rossier commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Stacked on #269 — this PR targets the feat-device-optimize-parse_dtb-function branch, which introduces include/math.h. Merge #269 first (or rebase this onto main afterwards).

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 in include/math.h.

Changes

1. De-duplicate the arch log_2_n_* helperslog_2_n_round_up() / log_2_n_round_down() were defined identically in both arch/arm32/include/asm/utils.h and arch/arm64/include/asm/utils.h, and nothing in the tree included either header or called the functions (verified). Moved into include/math.h; the two dead, duplicated arch headers are deleted. Original U-Boot / Texas Instruments attribution preserved.

2. Centralize the common.h integer-math macrosmin/max, min_t/max_t, DIV_ROUND_UP, DIV_ROUND_CLOSEST, __round_mask, round_up/round_down moved from include/common.h into include/math.h. common.h now #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 in common.h.

3. Reuse round_up() in bitmap.cbitmap.c had a local roundup_power2() macro identical to round_up(); its single use now calls round_up() and the local macro is dropped. The unused local nbits_to_hold_value() macro is removed as well.

include/math.h body is wrapped in #ifndef __ASSEMBLY__ so assembly translation units that pull common.h are unaffected.

Verification

  • Full clean build (virt64_defconfig) succeeds, including the .S files that include common.h — no errors, no min/max/round_* redefinition or implicit-declaration warnings.
  • clang-format reports no diff on the touched files.
  • round_up(v, 32) verified equal to the old roundup_power2(v, 32) over v ∈ [0, 1024].
  • Host test of lib/hashmap.c (which includes math.h) still passes, giving a second independent compile of math.h.
  • No include cycle (math.h pulls only types.h); no .c relies on a libc <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.
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.
@daniel-rossier daniel-rossier changed the title refactor(math): consolidate log_2_n helpers into include/math.h refactor(math): consolidate integer-math helpers into include/math.h Jul 7, 2026
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.
@daniel-rossier daniel-rossier merged commit e89efc7 into feat-device-optimize-parse_dtb-function Jul 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant