# i need help with this half float code

This topic is 3350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
Quote:
 Original post by iMalcYou'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

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 10
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632647
• Total Posts
3007640
• ### Who's Online (See full list)

There are no registered users currently online

×