Sign in to follow this  
jor1980

i need help with this half float code

Recommended Posts

Hi i have a code to convert a float into a halffloat, but it has errors when the number is negative or another special cases, i have doun a document with a code to do this convertion but i don´t know how to adapt it to my code. Anyone could adapt that code to my code? here is my code: public float inttohalf(short h) { int s, e, f; int hh; if (h == 0) return 0; s = (int)((h >> 15) & 0x00000001); e = (int)((h >> 10) & 0x0000001f); f = (int)(h & 0x000003ff); if (e == 0) { if (f == 0) { hh = (int)(s << 31); } else { hh = f & 0x00000400; while (hh == 0x00000000) { f <<= 1; e -= 1; e += 1; f &= ~0x00000400; } } } else if (e == 31) { if (f == 0) hh = (int)((s << 31) | 0x7f800000); else hh = (int)((s << 31) | 0x7f800000 | (f << 13)); } e = e + (127 - 15); f = f << 13; hh = (int)((s << 31) | (e << 23) | f); return BitConverter.ToSingle(BitConverter.GetBytes(hh), 0); } } And here the code that i have found: void generatetables(){ unsigned int i; int e; for(i=0; i<256; ++i){ e=i-127; if(e<-24){ // Very small numbers map to zero basetable[i|0x000]=0x0000; basetable[i|0x100]=0x8000; shifttable[i|0x000]=24; shifttable[i|0x100]=24; } else if(e<-14){ // Small numbers map to denorms basetable[i|0x000]=(0x0400>>(18-e)); basetable[i|0x100]=(0x0400>>(18-e)) | 0x8000; shifttable[i|0x000]=-e-1; shifttable[i|0x100]=-e-1; } else if(e<=15){ // Normal numbers just lose precision basetable[i|0x000]=((e+15)<<10); basetable[i|0x100]=((e+15)<<10) | 0x8000; shifttable[i|0x000]=13; shifttable[i|0x100]=13; } else if(e<128){ // Large numbers map to Infinity basetable[i|0x000]=0x7C00; basetable[i|0x100]=0xFC00; shifttable[i|0x000]=24; shifttable[i|0x100]=24; } else{ // Infinity and NaN's stay Infinity and NaN's basetable[i|0x000]=0x7C00; basetable[i|0x100]=0xFC00; shifttable[i|0x000]=13; shifttable[i|0x100]=13; } } }

Share this post


Link to post
Share on other sites
You've got it mixed up. The code at the bottom is Jeroen van der Zijp's code for generating the tables required to convert from the float to a short. To go from short back to float you need the mantissatable offsettable and exponenttable.
He gives all the code necessary including the one line required for converting each way, in the paper.

I wrapped the code in a nice little C++ class myself (cpp h). Maybe you could port that to C#?

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
You've got it mixed up. The code at the bottom is Jeroen van der Zijp's code for generating the tables required to convert from the float to a short. To go from short back to float you need the mantissatable offsettable and exponenttable.
He gives all the code necessary including the one line required for converting each way, in the paper.

I wrapped the code in a nice little C++ class myself (cpp h). Maybe you could port that to C#?


Thank´s what i need is to transform a float into a short(two byte) that represents a halffloat i don´t know if i am explaining myself well, because i need to write that two bytes into a file.COuld your code do this transformation?

thank´s again

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this