// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. // // Copyright (c) 2010-2011 SharpDX - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.ComponentModel; using System.Globalization; using System.Runtime.InteropServices; using Xenko.Core.Serialization; using System.Runtime.Serialization; namespace math { /// /// A half precision (16 bit) floating point value. /// [DataContract] [StructLayout(LayoutKind.Sequential, Pack = 2)] public struct Half { private ushort value; /// /// Number of decimal digits of precision. /// public const int PrecisionDigits = 3; /// /// Number of bits in the mantissa. /// public const int MantissaBits = 11; /// /// Maximum decimal exponent. /// public const int MaximumDecimalExponent = 4; /// /// Maximum binary exponent. /// public const int MaximumBinaryExponent = 15; /// /// Minimum decimal exponent. /// public const int MinimumDecimalExponent = -4; /// /// Minimum binary exponent. /// public const int MinimumBinaryExponent = -14; /// /// Exponent radix. /// public const int ExponentRadix = 2; /// /// Additional rounding. /// public const int AdditionRounding = 1; /// /// Smallest such that 1.0 + epsilon != 1.0 /// public static readonly float Epsilon; /// /// Maximum value of the number. /// public static readonly float MaxValue; /// /// Minimum value of the number. /// public static readonly float MinValue; /// /// A whose value is 0.0f. /// public static readonly Half Zero; /// /// A whose value is 1.0f. /// public static readonly Half One; /// /// Initializes a new instance of the structure. /// /// The floating point value that should be stored in 16 bit format. public Half(float value) { this.value = HalfUtils.Pack(value); } /// /// Gets or sets the raw 16 bit value used to back this half-float. /// public ushort RawValue { get { return value; } set { this.value = value; } } /// /// Converts an array of half precision values into full precision values. /// /// The values to be converted. /// An array of converted values. public static float[] ConvertToFloat(Half[] values) { float[] results = new float[values.Length]; for (int i = 0; i < results.Length; i++) results[i] = HalfUtils.Unpack(values[i].RawValue); return results; } /// /// Converts an array of full precision values into half precision values. /// /// The values to be converted. /// An array of converted values. public static Half[] ConvertToHalf(float[] values) { Half[] results = new Half[values.Length]; for (int i = 0; i < results.Length; i++) results[i] = new Half(values[i]); return results; } /// /// Performs an explicit conversion from to . /// /// The value to be converted. /// The converted value. public static explicit operator Half(float value) { return new Half(value); } /// /// Performs an implicit conversion from to . /// /// The value to be converted. /// The converted value. public static implicit operator float(Half value) { return HalfUtils.Unpack(value.value); } /// /// Tests for equality between two objects. /// /// The first value to compare. /// The second value to compare. /// /// true if has the same value as ; otherwise, false. public static bool operator ==(Half left, Half right) { return left.value == right.value; } /// /// Tests for inequality between two objects. /// /// The first value to compare. /// The second value to compare. /// /// true if has a different value than ; otherwise, false. public static bool operator !=(Half left, Half right) { return left.value != right.value; } /// /// Converts the value of the object to its equivalent string representation. /// /// The string representation of the value of this instance. public override string ToString() { float num = this; return num.ToString(CultureInfo.CurrentCulture); } /// /// Returns the hash code for this instance. /// /// A 32-bit signed integer hash code. public override int GetHashCode() { ushort num = value; return ((num * 3) / 2) ^ num; } /// /// Determines whether the specified object instances are considered equal. /// /// The first value. /// The second value. /// /// true if is the same instance as or /// if both are null references or if value1.Equals(value2) returns true; otherwise, false. public static bool Equals(ref Half value1, ref Half value2) { return value1.value == value2.value; } /// /// Returns a value that indicates whether the current instance is equal to the specified object. /// /// Object to make the comparison with. /// /// true if the current instance is equal to the specified object; false otherwise. public bool Equals(Half other) { return other.value == value; } /// /// Returns a value that indicates whether the current instance is equal to a specified object. /// /// Object to make the comparison with. /// /// true if the current instance is equal to the specified object; false otherwise. public override bool Equals(object obj) { if (obj == null) { return false; } if (obj.GetType() != GetType()) { return false; } Half half = (Half)obj; return half.value == value; } static Half() { Epsilon = 0.0004887581f; MaxValue = 65504f; MinValue = 6.103516E-05f; Zero = (Half)0.0f; One = (Half)1.0f; } } }