half.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Unity.Mathematics
  4. {
  5. public struct half : System.IEquatable<half>, IFormattable
  6. {
  7. public ushort value;
  8. /// <summary>half zero value.</summary>
  9. public static readonly half zero = new half();
  10. public static float MaxValue { get { return 65504.0f; } }
  11. public static float MinValue { get { return -65504.0f; } }
  12. /// <summary>Constructs a half value from a half value.</summary>
  13. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14. public half(half x)
  15. {
  16. value = x.value;
  17. }
  18. /// <summary>Constructs a half value from a float value.</summary>
  19. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  20. public half(float v)
  21. {
  22. value = (ushort)math.f32tof16(v);
  23. }
  24. /// <summary>Constructs a half value from a double value.</summary>
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public half(double v)
  27. {
  28. value = (ushort)math.f32tof16((float)v);
  29. }
  30. /// <summary>Explicitly converts a float value to a half value.</summary>
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. public static explicit operator half(float v) { return new half(v); }
  33. /// <summary>Explicitly converts a double value to a half value.</summary>
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public static explicit operator half(double v) { return new half(v); }
  36. /// <summary>Implicitly converts a half value to a float value.</summary>
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public static implicit operator float(half d) { return math.f16tof32(d.value); }
  39. /// <summary>Implicitly converts a half value to a double value.</summary>
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. public static implicit operator double(half d) { return math.f16tof32(d.value); }
  42. /// <summary>Returns whether two half values are equal.</summary>
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public static bool operator ==(half lhs, half rhs) { return lhs.value == rhs.value; }
  45. /// <summary>Returns whether two half values are different.</summary>
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static bool operator !=(half lhs, half rhs) { return lhs.value != rhs.value; }
  48. /// <summary>Returns true if the half is equal to a given half, false otherwise.</summary>
  49. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. public bool Equals(half rhs) { return value == rhs.value; }
  51. /// <summary>Returns true if the half is equal to a given half, false otherwise.</summary>
  52. public override bool Equals(object o) { return Equals((half)o); }
  53. /// <summary>Returns a hash code for the half.</summary>
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public override int GetHashCode() { return (int)value; }
  56. /// <summary>Returns a string representation of the half.</summary>
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public override string ToString()
  59. {
  60. return math.f16tof32(value).ToString();
  61. }
  62. /// <summary>Returns a string representation of the half using a specified format and culture-specific format information.</summary>
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public string ToString(string format, IFormatProvider formatProvider)
  65. {
  66. return math.f16tof32(value).ToString(format, formatProvider);
  67. }
  68. }
  69. public static partial class math
  70. {
  71. /// <summary>Returns a half value constructed from a half values.</summary>
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. public static half half(half x) { return new half(x); }
  74. /// <summary>Returns a half value constructed from a float value.</summary>
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. public static half half(float v) { return new half(v); }
  77. /// <summary>Returns a half value constructed from a double value.</summary>
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public static half half(double v) { return new half(v); }
  80. /// <summary>Returns a uint hash code of a half value.</summary>
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static uint hash(half v)
  83. {
  84. return v.value * 0x745ED837u + 0x816EFB5Du;
  85. }
  86. }
  87. }