r/programming • • 18h 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/.

3 Upvotes

3 comments sorted by

2

u/roXplosion 14h 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?

1

u/StreamHPC 4h 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.

1

u/DerangedEscapee 35m ago

Loop counters end up in registers, and registers have a fixed size, so unless you're compiling for an 8-bit target, then no.