HalfConversion.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  3. {
  4. static class HalfConversion
  5. {
  6. public static float HalfToSingle(int value)
  7. {
  8. int mantissa = (value >> 0) & 0x3ff;
  9. int exponent = (value >> 10) & 0x1f;
  10. int sign = (value >> 15) & 0x1;
  11. if (exponent == 0x1f)
  12. {
  13. // NaN or Infinity.
  14. mantissa <<= 13;
  15. exponent = 0xff;
  16. }
  17. else if (exponent != 0 || mantissa != 0 )
  18. {
  19. if (exponent == 0)
  20. {
  21. // Denormal.
  22. int e = -1;
  23. int m = mantissa;
  24. do
  25. {
  26. e++;
  27. m <<= 1;
  28. }
  29. while ((m & 0x400) == 0);
  30. mantissa = m & 0x3ff;
  31. exponent = e;
  32. }
  33. mantissa <<= 13;
  34. exponent = 127 - 15 + exponent;
  35. }
  36. int output = (sign << 31) | (exponent << 23) | mantissa;
  37. return BitConverter.Int32BitsToSingle(output);
  38. }
  39. }
  40. }