Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Half.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under MIT License. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2011 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 
24 using System;
25 using System.ComponentModel;
26 using System.Globalization;
27 using System.Runtime.InteropServices;
28 using SiliconStudio.Core.Serialization;
29 
30 namespace SiliconStudio.Core.Mathematics
31 {
32  /// <summary>
33  /// A half precision (16 bit) floating point value.
34  /// </summary>
35  [DataContract]
36  [StructLayout(LayoutKind.Sequential, Pack = 2)]
37  public struct Half
38  {
39  private ushort value;
40 
41  /// <summary>
42  /// Number of decimal digits of precision.
43  /// </summary>
44  public const int PrecisionDigits = 3;
45 
46  /// <summary>
47  /// Number of bits in the mantissa.
48  /// </summary>
49  public const int MantissaBits = 11;
50 
51  /// <summary>
52  /// Maximum decimal exponent.
53  /// </summary>
54  public const int MaximumDecimalExponent = 4;
55 
56  /// <summary>
57  /// Maximum binary exponent.
58  /// </summary>
59  public const int MaximumBinaryExponent = 15;
60 
61  /// <summary>
62  /// Minimum decimal exponent.
63  /// </summary>
64  public const int MinimumDecimalExponent = -4;
65 
66  /// <summary>
67  /// Minimum binary exponent.
68  /// </summary>
69  public const int MinimumBinaryExponent = -14;
70 
71  /// <summary>
72  /// Exponent radix.
73  /// </summary>
74  public const int ExponentRadix = 2;
75 
76  /// <summary>
77  /// Additional rounding.
78  /// </summary>
79  public const int AdditionRounding = 1;
80 
81  /// <summary>
82  /// Smallest such that 1.0 + epsilon != 1.0
83  /// </summary>
84  public static readonly float Epsilon;
85 
86  /// <summary>
87  /// Maximum value of the number.
88  /// </summary>
89  public static readonly float MaxValue;
90 
91  /// <summary>
92  /// Minimum value of the number.
93  /// </summary>
94  public static readonly float MinValue;
95 
96  /// <summary>
97  /// A <see cref="Half"/> whose value is 0.0f.
98  /// </summary>
99  public static readonly Half Zero;
100 
101  /// <summary>
102  /// A <see cref="Half"/> whose value is 1.0f.
103  /// </summary>
104  public static readonly Half One;
105 
106  /// <summary>
107  /// Initializes a new instance of the <see cref = "T:SiliconStudio.Core.Mathematics.Half" /> structure.
108  /// </summary>
109  /// <param name = "value">The floating point value that should be stored in 16 bit format.</param>
110  public Half(float value)
111  {
112  this.value = HalfUtils.Pack(value);
113  }
114 
115  /// <summary>
116  /// Gets or sets the raw 16 bit value used to back this half-float.
117  /// </summary>
118  public ushort RawValue
119  {
120  get { return value; }
121  set { this.value = value; }
122  }
123 
124  /// <summary>
125  /// Converts an array of half precision values into full precision values.
126  /// </summary>
127  /// <param name = "values">The values to be converted.</param>
128  /// <returns>An array of converted values.</returns>
129  public static float[] ConvertToFloat(Half[] values)
130  {
131  float[] results = new float[values.Length];
132  for(int i = 0; i < results.Length; i++)
133  results[i] = HalfUtils.Unpack(values[i].RawValue);
134  return results;
135  }
136 
137  /// <summary>
138  /// Converts an array of full precision values into half precision values.
139  /// </summary>
140  /// <param name = "values">The values to be converted.</param>
141  /// <returns>An array of converted values.</returns>
142  public static Half[] ConvertToHalf(float[] values)
143  {
144  Half[] results = new Half[values.Length];
145  for(int i = 0; i < results.Length; i++)
146  results[i] = new Half(values[i]);
147  return results;
148  }
149 
150  /// <summary>
151  /// Performs an explicit conversion from <see cref = "T:System.Single" /> to <see cref = "T:SiliconStudio.Core.Mathematics.Half" />.
152  /// </summary>
153  /// <param name = "value">The value to be converted.</param>
154  /// <returns>The converted value.</returns>
155  public static explicit operator Half(float value)
156  {
157  return new Half(value);
158  }
159 
160  /// <summary>
161  /// Performs an implicit conversion from <see cref = "T:SiliconStudio.Core.Mathematics.Half" /> to <see cref = "T:System.Single" />.
162  /// </summary>
163  /// <param name = "value">The value to be converted.</param>
164  /// <returns>The converted value.</returns>
165  public static implicit operator float(Half value)
166  {
167  return HalfUtils.Unpack(value.value);
168  }
169 
170  /// <summary>
171  /// Tests for equality between two objects.
172  /// </summary>
173  /// <param name = "left">The first value to compare.</param>
174  /// <param name = "right">The second value to compare.</param>
175  /// <returns>
176  /// <c>true</c> if <paramref name = "left" /> has the same value as <paramref name = "right" />; otherwise, <c>false</c>.</returns>
177  public static bool operator ==(Half left, Half right)
178  {
179  return left.value == right.value;
180  }
181 
182  /// <summary>
183  /// Tests for inequality between two objects.
184  /// </summary>
185  /// <param name = "left">The first value to compare.</param>
186  /// <param name = "right">The second value to compare.</param>
187  /// <returns>
188  /// <c>true</c> if <paramref name = "left" /> has a different value than <paramref name = "right" />; otherwise, <c>false</c>.</returns>
189  public static bool operator !=(Half left, Half right)
190  {
191  return left.value != right.value;
192  }
193 
194  /// <summary>
195  /// Converts the value of the object to its equivalent string representation.
196  /// </summary>
197  /// <returns>The string representation of the value of this instance.</returns>
198  public override string ToString()
199  {
200  float num = this;
201  return num.ToString(CultureInfo.CurrentCulture);
202  }
203 
204  /// <summary>
205  /// Returns the hash code for this instance.
206  /// </summary>
207  /// <returns>A 32-bit signed integer hash code.</returns>
208  public override int GetHashCode()
209  {
210  ushort num = value;
211  return (((num*3)/2) ^ num);
212  }
213 
214  /// <summary>
215  /// Determines whether the specified object instances are considered equal.
216  /// </summary>
217  /// <param name = "value1" />
218  /// <param name = "value2" />
219  /// <returns>
220  /// <c>true</c> if <paramref name = "value1" /> is the same instance as <paramref name = "value2" /> or
221  /// if both are <c>null</c> references or if <c>value1.Equals(value2)</c> returns <c>true</c>; otherwise, <c>false</c>.</returns>
222  public static bool Equals(ref Half value1, ref Half value2)
223  {
224  return value1.value == value2.value;
225  }
226 
227  /// <summary>
228  /// Returns a value that indicates whether the current instance is equal to the specified object.
229  /// </summary>
230  /// <param name = "other">Object to make the comparison with.</param>
231  /// <returns>
232  /// <c>true</c> if the current instance is equal to the specified object; <c>false</c> otherwise.</returns>
233  public bool Equals(Half other)
234  {
235  return other.value == value;
236  }
237 
238  /// <summary>
239  /// Returns a value that indicates whether the current instance is equal to a specified object.
240  /// </summary>
241  /// <param name = "obj">Object to make the comparison with.</param>
242  /// <returns>
243  /// <c>true</c> if the current instance is equal to the specified object; <c>false</c> otherwise.</returns>
244  public override bool Equals(object obj)
245  {
246  if (obj == null)
247  {
248  return false;
249  }
250  if (obj.GetType() != base.GetType())
251  {
252  return false;
253  }
254  Half half = (Half) obj;
255  return half.value == value;
256  }
257 
258  static Half()
259  {
260  Epsilon = 0.0004887581f;
261  MaxValue = 65504f;
262  MinValue = 6.103516E-05f;
263  Zero = (Half)0.0f;
264  One = (Half)1.0f;
265  }
266  }
267 }
static float[] ConvertToFloat(Half[] values)
Converts an array of half precision values into full precision values.
Definition: Half.cs:129
override int GetHashCode()
Returns the hash code for this instance.
Definition: Half.cs:208
bool Equals(Half other)
Returns a value that indicates whether the current instance is equal to the specified object...
Definition: Half.cs:233
static bool Equals(ref Half value1, ref Half value2)
Determines whether the specified object instances are considered equal.
Definition: Half.cs:222
ushort RawValue
Gets or sets the raw 16 bit value used to back this half-float.
Definition: Half.cs:119
static Half[] ConvertToHalf(float[] values)
Converts an array of full precision values into half precision values.
Definition: Half.cs:142
A half precision (16 bit) floating point value.
Definition: Half.cs:37
static readonly Half Zero
A Half whose value is 0.0f.
Definition: Half.cs:99
SiliconStudio.Paradox.Games.Mathematics.Half Half
override string ToString()
Converts the value of the object to its equivalent string representation.
Definition: Half.cs:198
static readonly float Epsilon
Smallest such that 1.0 + epsilon != 1.0
Definition: Half.cs:84
static readonly Half One
A Half whose value is 1.0f.
Definition: Half.cs:104
static readonly float MaxValue
Maximum value of the number.
Definition: Half.cs:89
override bool Equals(object obj)
Returns a value that indicates whether the current instance is equal to a specified object...
Definition: Half.cs:244
Half(float value)
Initializes a new instance of the T:SiliconStudio.Core.Mathematics.Half structure.
Definition: Half.cs:110
static readonly float MinValue
Minimum value of the number.
Definition: Half.cs:94