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