Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

half.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Unity.IL2CPP.CompilerServices;
  4. namespace Unity.Mathematics
  5. {
  6. /// <summary>
  7. /// A half precision float that uses 16 bits instead of 32 bits.
  8. /// </summary>
  9. [Il2CppEagerStaticClassConstruction]
  10. [Serializable]
  11. public struct half : System.IEquatable<half>, IFormattable
  12. {
  13. /// <summary>
  14. /// The raw 16 bit value of the half.
  15. /// </summary>
  16. public ushort value;
  17. /// <summary>half zero value.</summary>
  18. public static readonly half zero = new half();
  19. /// <summary>
  20. /// The maximum finite half value as a single precision float.
  21. /// </summary>
  22. public static float MaxValue { get { return 65504.0f; } }
  23. /// <summary>
  24. /// The minimum finite half value as a single precision float.
  25. /// </summary>
  26. public static float MinValue { get { return -65504.0f; } }
  27. /// <summary>
  28. /// The maximum finite half value as a half.
  29. /// </summary>
  30. public static half MaxValueAsHalf => new half(MaxValue);
  31. /// <summary>
  32. /// The minimum finite half value as a half.
  33. /// </summary>
  34. public static half MinValueAsHalf => new half(MinValue);
  35. /// <summary>Constructs a half value from a half value.</summary>
  36. /// <param name="x">The input half value to copy.</param>
  37. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  38. public half(half x)
  39. {
  40. value = x.value;
  41. }
  42. /// <summary>Constructs a half value from a float value.</summary>
  43. /// <param name="v">The single precision float value to convert to half.</param>
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public half(float v)
  46. {
  47. value = (ushort)math.f32tof16(v);
  48. }
  49. /// <summary>Constructs a half value from a double value.</summary>
  50. /// <param name="v">The double precision float value to convert to half.</param>
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. public half(double v)
  53. {
  54. value = (ushort)math.f32tof16((float)v);
  55. }
  56. /// <summary>Explicitly converts a float value to a half value.</summary>
  57. /// <param name="v">The single precision float value to convert to half.</param>
  58. /// <returns>The converted half value.</returns>
  59. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  60. public static explicit operator half(float v) { return new half(v); }
  61. /// <summary>Explicitly converts a double value to a half value.</summary>
  62. /// <param name="v">The double precision float value to convert to half.</param>
  63. /// <returns>The converted half value.</returns>
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public static explicit operator half(double v) { return new half(v); }
  66. /// <summary>Implicitly converts a half value to a float value.</summary>
  67. /// <param name="d">The half value to convert to a single precision float.</param>
  68. /// <returns>The converted single precision float value.</returns>
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public static implicit operator float(half d) { return math.f16tof32(d.value); }
  71. /// <summary>Implicitly converts a half value to a double value.</summary>
  72. /// <param name="d">The half value to convert to double precision float.</param>
  73. /// <returns>The converted double precision float value.</returns>
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public static implicit operator double(half d) { return math.f16tof32(d.value); }
  76. /// <summary>Returns whether two half values are bitwise equivalent.</summary>
  77. /// <param name="lhs">Left hand side half value to use in comparison.</param>
  78. /// <param name="rhs">Right hand side half value to use in comparison.</param>
  79. /// <returns>True if the two half values are bitwise equivalent, false otherwise.</returns>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static bool operator ==(half lhs, half rhs) { return lhs.value == rhs.value; }
  82. /// <summary>Returns whether two half values are not bitwise equivalent.</summary>
  83. /// <param name="lhs">Left hand side half value to use in comparison.</param>
  84. /// <param name="rhs">Right hand side half value to use in comparison.</param>
  85. /// <returns>True if the two half values are not bitwise equivalent, false otherwise.</returns>
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. public static bool operator !=(half lhs, half rhs) { return lhs.value != rhs.value; }
  88. /// <summary>Returns true if the half is bitwise equivalent to a given half, false otherwise.</summary>
  89. /// <param name="rhs">Right hand side half value to use in comparison.</param>
  90. /// <returns>True if the half value is bitwise equivalent to the input, false otherwise.</returns>
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public bool Equals(half rhs) { return value == rhs.value; }
  93. /// <summary>Returns true if the half is equal to a given half, false otherwise.</summary>
  94. /// <param name="o">Right hand side object to use in comparison.</param>
  95. /// <returns>True if the object is of type half and is bitwise equivalent, false otherwise.</returns>
  96. public override bool Equals(object o) { return o is half converted && Equals(converted); }
  97. /// <summary>Returns a hash code for the half.</summary>
  98. /// <returns>The computed hash code of the half.</returns>
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public override int GetHashCode() { return (int)value; }
  101. /// <summary>Returns a string representation of the half.</summary>
  102. /// <returns>The string representation of the half.</returns>
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. public override string ToString()
  105. {
  106. return math.f16tof32(value).ToString();
  107. }
  108. /// <summary>Returns a string representation of the half using a specified format and culture-specific format information.</summary>
  109. /// <param name="format">The format string to use during string formatting.</param>
  110. /// <param name="formatProvider">The format provider to use during string formatting.</param>
  111. /// <returns>The string representation of the half.</returns>
  112. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  113. public string ToString(string format, IFormatProvider formatProvider)
  114. {
  115. return math.f16tof32(value).ToString(format, formatProvider);
  116. }
  117. }
  118. public static partial class math
  119. {
  120. /// <summary>Returns a half value constructed from a half values.</summary>
  121. /// <param name="x">The input half value to copy.</param>
  122. /// <returns>The constructed half value.</returns>
  123. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  124. public static half half(half x) { return new half(x); }
  125. /// <summary>Returns a half value constructed from a float value.</summary>
  126. /// <param name="v">The single precision float value to convert to half.</param>
  127. /// <returns>The constructed half value.</returns>
  128. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  129. public static half half(float v) { return new half(v); }
  130. /// <summary>Returns a half value constructed from a double value.</summary>
  131. /// <param name="v">The double precision float value to convert to half.</param>
  132. /// <returns>The constructed half value.</returns>
  133. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  134. public static half half(double v) { return new half(v); }
  135. /// <summary>Returns a uint hash code of a half value.</summary>
  136. /// <param name="v">The half value to hash.</param>
  137. /// <returns>The computed hash code of the half value.</returns>
  138. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  139. public static uint hash(half v)
  140. {
  141. return v.value * 0x745ED837u + 0x816EFB5Du;
  142. }
  143. }
  144. }