Hacker News new | ask | show | jobs
by kabdib 4296 days ago
The floating point version is dangerous and broken on platforms that have 64-bit ints. (Not sure what happens with ceil and negative numbers, it probably works, but I'd have to try it). And while not an issue here, since this looks like it's user-space code, it's bad to use floats in a systems programming environment (floats are often in registers that can't be modified or maybe even referenced in kernel contexts without some save/restore shenanigans).

The loop version works poorly for large queue sizes, and if I saw this in production code I'd replace it immediately.

I'd be okay with a division and re-multiplication (likely going to be optimized by the compiler, through it's foolish to depend on this).

I don't see any problem with the mask/not/and solution. This has been a programming idiom for decades, and should be no more mysterious than (say) a doubly-linked list.

2 comments

I agree, the mask/not/and is standard within systems contexts. It need not be cognitive overhead, not to be elitist but if you don't have a copy of hacker's delight or the ability to understand constructs like this, you have no business writing systems-level code.
Agreed, I had trouble understanding the author's interest/surprise/confusion re. this idiom.

I asked around the room and the consensus is that a decade of C has brain damaged me.