├── Assets ├── FloatUint.cs └── Half.cs ├── LICENSE └── README.md /Assets/FloatUint.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System.Runtime.InteropServices; 4 | 5 | namespace Chaos { 6 | 7 | [StructLayout(LayoutKind.Explicit)] 8 | public struct FloatUint 9 | { 10 | [FieldOffset(0)] 11 | uint _uint; 12 | [FieldOffset(0)] 13 | float _float; 14 | 15 | public static implicit operator FloatUint(float f) 16 | { 17 | FloatUint fu = new FloatUint() {_float = f}; 18 | return fu; 19 | } 20 | 21 | public static implicit operator FloatUint(uint u) 22 | { 23 | FloatUint fu = new FloatUint() {_uint = u}; 24 | return fu; 25 | } 26 | 27 | public static implicit operator float(FloatUint fu) 28 | { 29 | return fu._float; 30 | } 31 | 32 | public static implicit operator uint(FloatUint fu) 33 | { 34 | return fu._uint; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Assets/Half.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Chaos; 3 | 4 | public struct Half : IFormattable, IComparable, IComparable, IEquatable 5 | { 6 | ushort _value; 7 | 8 | public static explicit operator Half(float f) 9 | { 10 | uint u = (FloatUint)f; 11 | if (u == 0) 12 | { 13 | return new Half() {_value = 0}; 14 | } 15 | if (u == 0x80000000) 16 | { 17 | return new Half() {_value = 0x8000}; 18 | } 19 | // TODO: subnormal numbers 20 | return new Half() {_value = (ushort)(((u >> 16) & 0x8000) | ((((u & 0x7f800000) - 0x38000000) >> 13) & 0x7c00) | ((u >> 13) & 0x03ff))}; 21 | } 22 | 23 | public static implicit operator float(Half h) 24 | { 25 | ushort s = h._value; 26 | if (s == 0) 27 | { 28 | return 0; 29 | } 30 | if (s == 0x8000) 31 | { 32 | return (FloatUint)0x80000000; 33 | } 34 | // TODO: subnormal numbers 35 | return (FloatUint)(uint)(((s & 0x8000) << 16) | (((s & 0x7c00) + 0x1C000) << 13) | ((s & 0x03FF) << 13)); 36 | } 37 | 38 | 39 | public static Half operator +(Half a, Half b) 40 | { 41 | return (Half)((float) a + (float) b); 42 | } 43 | 44 | 45 | public static Half operator -(Half a, Half b) 46 | { 47 | return (Half)((float)a - (float)b); 48 | } 49 | 50 | public static Half operator *(Half a, Half b) 51 | { 52 | return (Half)((float)a * (float)b); 53 | } 54 | 55 | 56 | public static Half operator /(Half a, Half b) 57 | { 58 | return (Half)((float)a / (float)b); 59 | } 60 | 61 | 62 | public static bool operator >(Half a, Half b) 63 | { 64 | return (float)a > (float)b; 65 | } 66 | 67 | public static bool operator <(Half a, Half b) 68 | { 69 | return (float)a < (float)b; 70 | } 71 | 72 | public static bool operator >=(Half a, Half b) 73 | { 74 | return (float)a >= (float)b; 75 | } 76 | 77 | public static bool operator <=(Half a, Half b) 78 | { 79 | return (float)a <= (float)b; 80 | } 81 | 82 | public static bool operator ==(Half a, Half b) 83 | { 84 | return a._value == b._value; 85 | } 86 | public static bool operator !=(Half a, Half b) 87 | { 88 | return a._value != b._value; 89 | } 90 | 91 | public override string ToString() 92 | { 93 | return ((float) this).ToString(); 94 | } 95 | 96 | public string ToString(string format, IFormatProvider formatProvider) 97 | { 98 | return ((float) this).ToString(format, formatProvider); 99 | } 100 | 101 | public int CompareTo(object obj) 102 | { 103 | return CompareTo((Half)obj); 104 | } 105 | 106 | public int CompareTo(Half h) 107 | { 108 | return this == h ? 0 : (this > h ? 1 : -1); 109 | } 110 | 111 | public bool Equals(Half other) 112 | { 113 | return this == other; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Khaos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HalfFloat 2 | Reference: 3 | 4 | [wiki:Half-precision_floating-point_format](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) 5 | 6 | 7 | ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf 8 | 9 | 10 | 11 | --------------------------------------------------------------------------------