using System; using System.Runtime.CompilerServices; using Unity.IL2CPP.CompilerServices; namespace Unity.Mathematics { /// /// A half precision float that uses 16 bits instead of 32 bits. /// [Il2CppEagerStaticClassConstruction] [Serializable] public struct half : System.IEquatable, IFormattable { /// /// The raw 16 bit value of the half. /// public ushort value; /// half zero value. public static readonly half zero = new half(); /// /// The maximum finite half value as a single precision float. /// public static float MaxValue { get { return 65504.0f; } } /// /// The minimum finite half value as a single precision float. /// public static float MinValue { get { return -65504.0f; } } /// /// The maximum finite half value as a half. /// public static half MaxValueAsHalf => new half(MaxValue); /// /// The minimum finite half value as a half. /// public static half MinValueAsHalf => new half(MinValue); /// Constructs a half value from a half value. /// The input half value to copy. [MethodImpl(MethodImplOptions.AggressiveInlining)] public half(half x) { value = x.value; } /// Constructs a half value from a float value. /// The single precision float value to convert to half. [MethodImpl(MethodImplOptions.AggressiveInlining)] public half(float v) { value = (ushort)math.f32tof16(v); } /// Constructs a half value from a double value. /// The double precision float value to convert to half. [MethodImpl(MethodImplOptions.AggressiveInlining)] public half(double v) { value = (ushort)math.f32tof16((float)v); } /// Explicitly converts a float value to a half value. /// The single precision float value to convert to half. /// The converted half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator half(float v) { return new half(v); } /// Explicitly converts a double value to a half value. /// The double precision float value to convert to half. /// The converted half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator half(double v) { return new half(v); } /// Implicitly converts a half value to a float value. /// The half value to convert to a single precision float. /// The converted single precision float value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator float(half d) { return math.f16tof32(d.value); } /// Implicitly converts a half value to a double value. /// The half value to convert to double precision float. /// The converted double precision float value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator double(half d) { return math.f16tof32(d.value); } /// Returns whether two half values are bitwise equivalent. /// Left hand side half value to use in comparison. /// Right hand side half value to use in comparison. /// True if the two half values are bitwise equivalent, false otherwise. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(half lhs, half rhs) { return lhs.value == rhs.value; } /// Returns whether two half values are not bitwise equivalent. /// Left hand side half value to use in comparison. /// Right hand side half value to use in comparison. /// True if the two half values are not bitwise equivalent, false otherwise. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(half lhs, half rhs) { return lhs.value != rhs.value; } /// Returns true if the half is bitwise equivalent to a given half, false otherwise. /// Right hand side half value to use in comparison. /// True if the half value is bitwise equivalent to the input, false otherwise. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(half rhs) { return value == rhs.value; } /// Returns true if the half is equal to a given half, false otherwise. /// Right hand side object to use in comparison. /// True if the object is of type half and is bitwise equivalent, false otherwise. public override bool Equals(object o) { return o is half converted && Equals(converted); } /// Returns a hash code for the half. /// The computed hash code of the half. [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() { return (int)value; } /// Returns a string representation of the half. /// The string representation of the half. [MethodImpl(MethodImplOptions.AggressiveInlining)] public override string ToString() { return math.f16tof32(value).ToString(); } /// Returns a string representation of the half using a specified format and culture-specific format information. /// The format string to use during string formatting. /// The format provider to use during string formatting. /// The string representation of the half. [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ToString(string format, IFormatProvider formatProvider) { return math.f16tof32(value).ToString(format, formatProvider); } } public static partial class math { /// Returns a half value constructed from a half values. /// The input half value to copy. /// The constructed half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static half half(half x) { return new half(x); } /// Returns a half value constructed from a float value. /// The single precision float value to convert to half. /// The constructed half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static half half(float v) { return new half(v); } /// Returns a half value constructed from a double value. /// The double precision float value to convert to half. /// The constructed half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static half half(double v) { return new half(v); } /// Returns a uint hash code of a half value. /// The half value to hash. /// The computed hash code of the half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint hash(half v) { return v.value * 0x745ED837u + 0x816EFB5Du; } } }