BITMAP_FROM_U64(9)
π BITMAP_FROM_U64(9) β the Kernel API Manual, bestie edition π
Okay so this one is from the Kernel API Manual and it's literally about representing a u64 value in a format that's suitable for a bitmap. It's giving "tiny macro, big endian energy" and I'm obsessed. Let's get into it β¨
β¨ NAME β¨
BITMAP_FROM_U64 β Represent u64 value in the format suitable for bitmap. That's it, that's the whole gig π«Ά
πΈ SYNOPSIS πΈ
BITMAP_FROM_U64 (n );
Yes bestie, that's the whole call. One argument and a dream π
π¦ ARGUMENTS π¦
nβ au64value. That's the number you're feeding in, no cap π₯
π DESCRIPTION π
Okay so first things first, you gotta understand what a Linux bitmap actually is on the inside, because it's not what you'd assume π
Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit integers in a 32-bit environment, and 64-bit integers in a 64-bit one. So the size of each little word totally depends on where you're running. Iconic, honestly.
There are four combinations of endianness and length of the word in linux ABIs, and they are:
LE64BE64LE32BE32
Four icons. One vibe check. π
π the 64-bit girlies π
On 64-bit kernels, 64-bit LE and BE numbers are naturally ordered in bitmaps and therefore don't require any special handling. Like, they're just born correct. Main character energy from birth πβ¨
π the 32-bit situation (this is where it gets spicy) π
On 32-bit kernels, the 32-bit LE ABI orders the lo word of a 64-bit number in memory prior to hi, and 32-bit BE orders hi word prior to lo. So the two endians literally cannot agree on word order. Classic π
The bitmap, on the other hand, is represented as an array of 32-bit words, and the position of bit N may therefore be calculated as: word #(N/32) and bit #(N32) in that word. (Yes, that's exactly how it's written, bestie β copying it character for character, don't come for me π
)
For example, bit #42 is located at 10th position of 2nd word. Cute, right? π
That matches the 32-bit LE ABI, and we can simply let the compiler store 64-bit values in memory as it usually does. But for BE we need to swap hi and lo words manually. So it's not free, it's just correct π
π and that's the whole reason this macro exists π
With all that, the macro BITMAP_FROM_U64 does explicit reordering of hi and lo parts of u64.
- For
LE32it does nothing β literally nothing, a whole no-op, and that's slay β¨ - For BE environment it swaps the
hiandlowords, as is expected by bitmap.
So it's a tiny little endianness bouncer at the door of your bitmap. Doorman behavior, but make it kernel π«Άπ₯
Heads up: don't be out here assuming it does something on LE32. It does nothing. That's the whole point. The reordering only happens when the BE environment needs it. π
π§ SEE ALSO π§
Kernel file ./include/linux/bitmap.h plus the whole bestie lineup, in this exact order, and yes I'm listing all of them because we do not skip the boring parts in this house π
π
bitmap overview(9)bitmap bitops(9)declare bitmap(9)bitmap_find_next_zero_area(9)bitmap_or_equal(9)bitmap_weight_from(9)bitmap_scatter(9)bitmap_gather(9)bitmap_release_region(9)bitmap_allocate_region(9)bitmap_find_free_region(9)bitmap_from_u64(9)bitmap_read(9)bitmap_write(9)
And that's the page! πΈ Big sign-off energy: this one lives in ./include/linux, it's dated October 2026, and honestly? She ate. Periodt. π
πβ¨
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms