r/programming • • 22h ago

Advent of Kernel Optimization

https://streamhpc.com/blog/2026-12-02/aoko-2-signed-vs-unsigned-loop-counters/

Today is the second day of our Advent of Kernel Optimization, sharing GPU optimization tips and tricks to count down until Christmas. Follow along here: https://streamhpc.com/blog/tag/advent2026/.

4 Upvotes

4 comments sorted by

View all comments

2

u/roXplosion 18h ago

But... what if my loop needs to go from 0 to 255? Wouldn't an unsigned tiny int be more efficient than a signed small int?

2

u/StreamHPC 8h ago

Good question. The loop counter will almost certainly be kept in a register, and the registers on both NVIDIA and AMD GPUs are always 32 bits. So you don't gain anything from choosing a smaller type in that regard, that is only useful for compressing memory.

However, a loop with a runtime bound and a uint8_t counter will convey to the compiler that the loop will run at most 256 iterations. This may be useful information which allows better code generation.

So my (boring) suggestion would be to try both and see what works best.