fork download
  1. #include <stdio.h>
  2.  
  3. unsigned int2hfloat(int x)
  4. {
  5. unsigned sign = x < 0;
  6. unsigned absx = ((unsigned)x ^ -sign) + sign; // safe abs(x)
  7. unsigned tmp = absx, manbits = 0;
  8. int exp = 0, truncated = 0;
  9.  
  10. // calculate the number of bits needed for the mantissa
  11. while (tmp)
  12. {
  13. tmp >>= 1;
  14. manbits++;
  15. }
  16.  
  17. // round the number if necessary before we do the conversion
  18. if (manbits > 13)
  19. absx += (2<<(manbits-13));
  20.  
  21. manbits = 0;
  22. tmp = absx;
  23. while (tmp)
  24. {
  25. tmp >>= 1;
  26. manbits++;
  27. }
  28.  
  29. // half-precision floats have 11 bits in the mantissa.
  30. // truncate the excess or insert the lacking 0s until there are 11.
  31. if (manbits)
  32. {
  33. exp = 10; // exp bias because 1.0 is at bit position 10
  34. while (manbits > 11)
  35. {
  36. truncated |= absx & 1;
  37. absx >>= 1;
  38. manbits--;
  39. exp++;
  40. }
  41. while (manbits < 11)
  42. {
  43. absx <<= 1;
  44. manbits++;
  45. exp--;
  46. }
  47. }
  48. if (exp + truncated > 16)
  49. {
  50. // absx was too big, force it to +/- infinity
  51. exp = 31; // special infinity value
  52. absx = 0;
  53. }
  54. else if (manbits)
  55. {
  56. // normal case, absx > 0
  57. exp += 15; // bias the exponent
  58. }
  59.  
  60. return (sign << 15) | ((unsigned)exp << 10) | (absx & ((1u<<10)-1));
  61. }
  62.  
  63. int main(void)
  64. {
  65. printf("+4095: 0x%04X\n", int2hfloat(+4095));
  66. printf("+4096: 0x%04X\n", int2hfloat(+4096));
  67. printf("+4097: 0x%04X\n", int2hfloat(+4097));
  68. printf("+4098: 0x%04X\n", int2hfloat(+4098));
  69. printf("+4099: 0x%04X\n", int2hfloat(+4099));
  70. printf("+2052: 0x%04X\n", int2hfloat(+2052));
  71. printf("+8190: 0x%04X\n", int2hfloat(+8190));
  72. printf("+8191: 0x%04X\n", int2hfloat(+8191));
  73. printf("+8192: 0x%04X\n", int2hfloat(+8192));
  74. printf("+8193: 0x%04X\n", int2hfloat(+8193));
  75. printf("+8194: 0x%04X\n", int2hfloat(+8194));
  76. printf("+8195: 0x%04X\n", int2hfloat(+8195));
  77. printf("+8196: 0x%04X\n", int2hfloat(+8196));
  78. printf("+8197: 0x%04X\n", int2hfloat(+8197));
  79. printf("+8198: 0x%04X\n", int2hfloat(+8198));
  80. printf("+8199: 0x%04X\n", int2hfloat(+8199));
  81. printf("+8200: 0x%04X\n", int2hfloat(+8200));
  82. printf("+16398: 0x%04X\n", int2hfloat(+16398));
  83. printf("+16400: 0x%04X\n", int2hfloat(+16400));
  84. printf("+32768: 0x%04X\n", int2hfloat(+32768));
  85. printf("+65504: 0x%04X\n", int2hfloat(+65504));
  86. printf("+65505: 0x%04X\n", int2hfloat(+65505));
  87. printf("+65519: 0x%04X\n", int2hfloat(+65519));
  88. printf("+65534: 0x%04X\n", int2hfloat(+65534));
  89. printf("+65535: 0x%04X\n", int2hfloat(+65535));
  90. printf("+65535: 0x%04X\n", int2hfloat(+65536));
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0s 4504KB
stdin
Standard input is empty
stdout
+4095: 0x6BFF
+4096: 0x6C00
+4097: 0x6C00
+4098: 0x6C00
+4099: 0x6C00
+2052: 0x6802
+8190: 0x6FFF
+8191: 0x6FFF
+8192: 0x7000
+8193: 0x7000
+8194: 0x7000
+8195: 0x7000
+8196: 0x7001
+8197: 0x7001
+8198: 0x7001
+8199: 0x7001
+8200: 0x7001
+16398: 0x7401
+16400: 0x7401
+32768: 0x7800
+65504: 0x7BFF
+65505: 0x7BFF
+65519: 0x7BFF
+65534: 0x7C00
+65535: 0x7C00
+65535: 0x7C00