fork(17) 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. // half-precision floats have 11 bits in the mantissa.
  18. // truncate the excess or insert the lacking 0s until there are 11.
  19. if (manbits)
  20. {
  21. exp = 10; // exp bias because 1.0 is at bit position 10
  22. while (manbits > 11)
  23. {
  24. truncated |= absx & 1;
  25. absx >>= 1;
  26. manbits--;
  27. exp++;
  28. }
  29. while (manbits < 11)
  30. {
  31. absx <<= 1;
  32. manbits++;
  33. exp--;
  34. }
  35. }
  36.  
  37. if (exp + truncated > 15)
  38. {
  39. // absx was too big, force it to +/- infinity
  40. exp = 31; // special infinity value
  41. absx = 0;
  42. }
  43. else if (manbits)
  44. {
  45. // normal case, absx > 0
  46. exp += 15; // bias the exponent
  47. }
  48.  
  49. return (sign << 15) | ((unsigned)exp << 10) | (absx & ((1u<<10)-1));
  50. }
  51.  
  52. int main(void)
  53. {
  54. printf(" 0: 0x%04X\n", int2hfloat(0));
  55. printf("-1: 0x%04X\n", int2hfloat(-1));
  56. printf("+1: 0x%04X\n", int2hfloat(+1));
  57. printf("-2: 0x%04X\n", int2hfloat(-2));
  58. printf("+2: 0x%04X\n", int2hfloat(+2));
  59. printf("-3: 0x%04X\n", int2hfloat(-3));
  60. printf("+3: 0x%04X\n", int2hfloat(+3));
  61. printf("-2047: 0x%04X\n", int2hfloat(-2047));
  62. printf("+2047: 0x%04X\n", int2hfloat(+2047));
  63. printf("-2048: 0x%04X\n", int2hfloat(-2048));
  64. printf("+2048: 0x%04X\n", int2hfloat(+2048));
  65. printf("-2049: 0x%04X\n", int2hfloat(-2049)); // first inexact integer
  66. printf("+2049: 0x%04X\n", int2hfloat(+2049));
  67. printf("-2050: 0x%04X\n", int2hfloat(-2050));
  68. printf("+2050: 0x%04X\n", int2hfloat(+2050));
  69. printf("-32752: 0x%04X\n", int2hfloat(-32752));
  70. printf("+32752: 0x%04X\n", int2hfloat(+32752));
  71. printf("-32768: 0x%04X\n", int2hfloat(-32768));
  72. printf("+32768: 0x%04X\n", int2hfloat(+32768));
  73. printf("-65504: 0x%04X\n", int2hfloat(-65504)); // legal maximum
  74. printf("+65504: 0x%04X\n", int2hfloat(+65504));
  75. printf("-65505: 0x%04X\n", int2hfloat(-65505)); // infinity from here on
  76. printf("+65505: 0x%04X\n", int2hfloat(+65505));
  77. printf("-65535: 0x%04X\n", int2hfloat(-65535));
  78. printf("+65535: 0x%04X\n", int2hfloat(+65535));
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.02s 1676KB
stdin
Standard input is empty
stdout
 0: 0x0000
-1: 0xBC00
+1: 0x3C00
-2: 0xC000
+2: 0x4000
-3: 0xC200
+3: 0x4200
-2047: 0xE7FF
+2047: 0x67FF
-2048: 0xE800
+2048: 0x6800
-2049: 0xE800
+2049: 0x6800
-2050: 0xE801
+2050: 0x6801
-32752: 0xF7FF
+32752: 0x77FF
-32768: 0xF800
+32768: 0x7800
-65504: 0xFBFF
+65504: 0x7BFF
-65505: 0xFC00
+65505: 0x7C00
-65535: 0xFC00
+65535: 0x7C00