Re:Should you normalize RGB values by 255 or 256?
by lacedeconstruct on Monday June 01, 2026 @04:13PM
(#48362006)
The difference between 20 cycles and 1 clock cycle in a hot loop is very noticeable
[ Parent
| on HN ]
Re:Should you normalize RGB values by 255 or 256?
by Sesse__ on Monday June 01, 2026 @04:35PM
(#48362298)
Useful, then, that you can start several vectorized floating-point muls each cycle. (E.g., most modern x86 are 3/0.5 cycles for vmulps. No 20 cycles in sight.)
Re:Should you normalize RGB values by 255 or 256?
by Tuna-Fish on Monday June 01, 2026 @04:46PM
(#48362420)
FP Division by constant is optimized by a compiler into a multiply. Graphics processing typically happens on the GPU these days, and on all recent GPUs FPMUL belongs to the class of lowest-latency operations. That is, there are no other instructions that complete faster.
Re:Should you normalize RGB values by 255 or 256?
by pixelesque on Monday June 01, 2026 @05:17PM
(#48362777)
Only with things like -ffast-math enabled will compilers do the reciprocal.
It can make a fair difference in some cases, but it's often better to selectively use it in code locations you know are acceptable by doing it manually in the code.
Re:Should you normalize RGB values by 255 or 256?
by mgaunard on Monday June 01, 2026 @05:03PM
(#48362601)
That's only valid to do if the reciprocal is representable exactly.
Re:Should you normalize RGB values by 255 or 256?
by hansvm on Monday June 01, 2026 @06:14PM
(#48363345)
That's not totally true. It's sufficient to be exactly representable, but you only need the reciprocal rounding error to be small enough to guarantee the multiplication rounding step fixes it across the entire range of numerators. For IEEE754 f16 values, there are 28 such extra values, the positive and negative sides of 1705/x where x is a power of 2 at least as great as 2048.
Re:Should you normalize RGB values by 255 or 256?
by mgaunard on Tuesday June 02, 2026 @04:05PM
(#48375495)
Interesting, but pretty limited corner case. Would compilers even identify those 28 values and do the transformation?
Re:Should you normalize RGB values by 255 or 256?
by hansvm on Wednesday June 03, 2026 @10:53AM
(#48384941)
Maybe for f16. The compiler's implementation could just be checking all numerators to see if the transformation is safe. The corner cases are messy and not quickly brute-forceable for f32 or brute-forceable at all for f64 though, so I doubt they'd bother, especially when I bet those constants have showed up literally zero times across all programs.
Re:Should you normalize RGB values by 255 or 256?
by exyi on Monday June 01, 2026 @04:49PM
(#48362452)
It's 3 cycles for float multiplication (and 1 for shift right):
https://uops.info/table.html?search=mulss&cb_lat=on&cb_tp=on...
https://uops.info/table.html?search=shr&cb_lat=on&cb_tp=on&c...
In throughput it's even less of a difference: 2 per cycle vs 3 per cycle.
Re:Should you normalize RGB values by 255 or 256?
by userbinator on Monday June 01, 2026 @11:27PM
(#48365632)
It's 3 cycles for float multiplication (and 1 for shift right):
3x faster
In throughput it's even less of a difference: 2 per cycle vs 3 per cycle.
50% faster