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