using System; using System.Runtime.CompilerServices; namespace Unity.Mathematics { public struct half : System.IEquatable, IFormattable { public ushort value; /// half zero value. public static readonly half zero = new half(); public static float MaxValue { get { return 65504.0f; } } public static float MinValue { get { return -65504.0f; } } /// Constructs a half value from a half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public half(half x) { value = x.value; } /// Constructs a half value from a float value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public half(float v) { value = (ushort)math.f32tof16(v); } /// Constructs a half value from a double value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public half(double v) { value = (ushort)math.f32tof16((float)v); } /// Explicitly converts a float value to a 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. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator half(double v) { return new half(v); } /// Implicitly converts a half value to a 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. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator double(half d) { return math.f16tof32(d.value); } /// Returns whether two half values are equal. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(half lhs, half rhs) { return lhs.value == rhs.value; } /// Returns whether two half values are different. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(half lhs, half rhs) { return lhs.value != rhs.value; } /// Returns true if the half is equal to a given half, 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. public override bool Equals(object o) { return Equals((half)o); } /// Returns a hash code for the half. [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() { return (int)value; } /// Returns a 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. [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. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static half half(half x) { return new half(x); } /// Returns a half value constructed from a float value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static half half(float v) { return new half(v); } /// Returns a half value constructed from a double value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static half half(double v) { return new half(v); } /// Returns a uint hash code of a half value. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static uint hash(half v) { return v.value * 0x745ED837u + 0x816EFB5Du; } } }