TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Unorm and SNORM to float, hardware edition

31 pointsby luu5 months ago

2 comments

mtklein5 months ago
It can be fun to explore the interactions of unorm and float bit representations even when you have float instructions. E.g. if you bit-or a unorm8 into 0x47000000 (32768.0f) then subtract 32768.0f, you&#x27;ll get a number very close to right, just a float multiply of (256&#x2F;255.0f) away. Reordering the math so that the subtraction and multiply can become a single FMA is a fun homework exercise.<p><pre><code> union { int bits; float f; } pun = {x}, scale = {0x47000000}; pun.bits |= scale.bits; pun.f -= scale.f; pun.f *= (256&#x2F;255.0f); </code></pre> This basically amounts to a software implementation of int-&gt;float conversion instructions; sadly I have never found a spot where it&#x27;s actually worth doing when you have those int-&gt;float instructions available already, even with the FMA as a single instruction.<p>It&#x27;s also worth considering whether your application can handle approximate conversion. If you have a [0,255] unorm in x, x + (x&gt;&gt;7) or equivalently x + (x&gt;0x7f) will round it to a [0,256] fixed-point value. Crucially, this rounding does handle 0x00 and 0xff inputs correctly. Once in fixed-point with a nice power-of-two divisor, you can play all sorts of tricks, either again making use of the bit representation of floats, using ARM fixed-point instructions, etc. If you&#x27;ve ever looked longingly at the pmulhrsw family of instructions, this is a ripe area to explore.
mgaunard5 months ago
For those who like me had no ideas what UNORM&#x2F;SNORM are, it&#x27;s a base-2 fixed-point representation apparently common in the latest GPU APIs (DirectX 10&#x2F;11, Vulkan).
评论 #42515927 未加载
评论 #42515905 未加载
评论 #42516605 未加载