16 bit floats

Get answers to all your basic programming questions. No Ogre questions, please!
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

16 bit floats

Post by PhilipLB »

Hi,

I'm currently writing a serializer for the volume component to save voxeldata to disc. And actually, there's no need to be super-exact with the density values so to save some space, I'd like to save the data as 16 bit floats. But... I don't see a direct datatype for this like float16 or so. How would you do that? An easy one would be to use fixed point numbers and just save shorts.
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: 16 bit floats

Post by Kojack »

C++ and the cpu itself have no support for half floats (the gpu does). You'll need to get a library (or at least a class) that supports them.
Fixed point is probably going to be easier.
What's the range of values you are expecting for density? It may be easier to just use the short as 0-65535 representing the scaled range of density values (such as 0-1 or whatever it is). If the density range can change, then store the min and max as well.
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

Re: 16 bit floats

Post by PhilipLB »

That's what I supposed, thanks. :) Yep, going for shorts now. Else, everything could be really slow due to the lack of CPU support.
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: 16 bit floats

Post by bstone »

Ogre::Bitwise has floatToHalf() and halfToFloat(). You might try them and see how big the performance hit would be compared to the 16bit fixed point.
PhilipLB
Google Summer of Code Student
Google Summer of Code Student
Posts: 550
Joined: Thu Jun 04, 2009 5:07 pm
Location: Berlin
x 108

Re: 16 bit floats

Post by PhilipLB »

Ah, good to know, thx. :)
Google Summer of Code 2012 Student
Topic: "Volume Rendering with LOD aimed at terrain"
Project links: Project thread, WIKI page, Code fork for the project
Mentor: Mattan Furst


Volume GFX, accepting donations.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: 16 bit floats

Post by Kojack »

I didn't know Bitwise had half conversions.
Then again, I've never heard of Ogre::Bitwise.
(Hey, it's a big library)
:)
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: 16 bit floats

Post by bstone »

Kojack wrote:(Hey, it's a big library)
And evolving! :)
User avatar
Zonder
Ogre Magi
Posts: 1172
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Re: 16 bit floats

Post by Zonder »

reading this made me read the 80286 architecture docs geek but I actually though the 386 had 16bit float registers.... never mine lol. What surprised me most was the 386 stopped manufacture in 2006!!!
There are 10 types of people in the world: Those who understand binary, and those who don't...
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: 16 bit floats

Post by mkultra333 »

Be nice if float16 was available on the CPU. They're handy for so many things. I'm just working on some metaballs at the moment based on some BSD license code I found. It's SSE optimized, and I'm just thinking how great it would be to process 8 float16 at a time instead of 4 floats.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.