ScriptCore: Use engine implementation of half to no longer rely on Half-Package

This commit is contained in:
Simon Lübeß
2023-12-17 18:47:07 +01:00
parent 64f7896c15
commit 6a87d1ac09
5 changed files with 321 additions and 6 deletions
+11 -1
View File
@@ -24,7 +24,7 @@ struct half : IFloating, ISigned, IFormattable, IHashable, IEquatable<half>, ICa
public bool IsNegative => (_data & Half_Sign_Mask) > 0; public bool IsNegative => (_data & Half_Sign_Mask) > 0;
public bool IsFinity => (_data & ~Half_Sign_Mask) < Half_Exponent_Mask; public bool IsFinite => (_data & ~Half_Sign_Mask) < Half_Exponent_Mask;
public bool IsInfinity => (_data & ~Half_Sign_Mask) == Half_Exponent_Mask; public bool IsInfinity => (_data & ~Half_Sign_Mask) == Half_Exponent_Mask;
public bool IsPositiveInfinity => _data == PositiveInfinity._data; public bool IsPositiveInfinity => _data == PositiveInfinity._data;
@@ -299,6 +299,16 @@ struct half : IFloating, ISigned, IFormattable, IHashable, IEquatable<half>, ICa
return (half)((float)lhs % (float)rhs); return (half)((float)lhs % (float)rhs);
} }
public static half operator ++(half value)
{
return (half)((float)value + 1.0f);
}
public static half operator --(half value)
{
return (half)((float)value - 1.0f);
}
public static bool operator==(half value1, half value2) => value1._data == value2._data; public static bool operator==(half value1, half value2) => value1._data == value2._data;
public static bool operator!=(half value1, half value2) => value1._data != value2._data; public static bool operator!=(half value1, half value2) => value1._data != value2._data;
+28
View File
@@ -465,6 +465,34 @@ static class ScriptGlue
RegisterCall<function float2(float2, out float2)>("ScriptGlue::modf_float2", => GlitchyEngine.Math.modf); RegisterCall<function float2(float2, out float2)>("ScriptGlue::modf_float2", => GlitchyEngine.Math.modf);
RegisterCall<function float3(float3, out float3)>("ScriptGlue::modf_float3", => GlitchyEngine.Math.modf); RegisterCall<function float3(float3, out float3)>("ScriptGlue::modf_float3", => GlitchyEngine.Math.modf);
RegisterCall<function float4(float4, out float4)>("ScriptGlue::modf_float4", => GlitchyEngine.Math.modf); RegisterCall<function float4(float4, out float4)>("ScriptGlue::modf_float4", => GlitchyEngine.Math.modf);
RegisterHalfFunctions();
}
private static void RegisterHalfFunctions()
{
RegisterCall<function void(float, out half)>("Math.MyHalf::FromFloat32", (value, halfValue) => halfValue = GlitchyEngine.Math.half.FromFloat32(value));
RegisterCall<function void(half, out float)>("Math.MyHalf::ToFloat32", (value, floatValue) => floatValue = GlitchyEngine.Math.half.ToFloat32(value));
RegisterCall<function void(half, half, out bool)>("Math.MyHalf::LessThan_Impl", (left, right, result) => result = left < right);
RegisterCall<function void(half, half, out bool)>("Math.MyHalf::LessThanOrEqual_Impl", (left, right, result) => result = left <= right);
RegisterCall<function void(half, half, out bool)>("Math.MyHalf::GreaterThan_Impl", (left, right, result) => result = left > right);
RegisterCall<function void(half, half, out bool)>("Math.MyHalf::GreaterThanOrEqual_Impl", (left, right, result) => result = left >= right);
RegisterCall<function void(half, half, out half)>("Math.MyHalf::Add_Impl", (left, right, result) => result = left + right);
RegisterCall<function void(half, half, out half)>("Math.MyHalf::Subtract_Impl", (left, right, result) => result = left - right);
RegisterCall<function void(half, half, out half)>("Math.MyHalf::Multiply_Impl", (left, right, result) => result = left * right);
RegisterCall<function void(half, half, out half)>("Math.MyHalf::Divide_Impl", (left, right, result) => result = left / right);
RegisterCall<function void(half, half, out half)>("Math.MyHalf::Modulo_Impl", (left, right, result) => result = left % right);
RegisterCall<function void(half, out half)>("Math.MyHalf::Negate_Impl", (value, result) => result = -value);
RegisterCall<function void(half, out half)>("Math.MyHalf::Increment_Impl", (value, result) => result = ++value);
RegisterCall<function void(half, out half)>("Math.MyHalf::Decrement_Impl", (value, result) => result = --value);
RegisterCall<function bool(half)>("Math.MyHalf::IsNegative_Impl", (value) => value.IsNegative);
RegisterCall<function bool(half)>("Math.MyHalf::IsFinite_Impl", (value) => value.IsFinite);
RegisterCall<function bool(half)>("Math.MyHalf::IsInfinity_Impl", (value) => value.IsInfinity);
RegisterCall<function bool(half)>("Math.MyHalf::IsNan_Impl", (value) => value.IsNaN);
RegisterCall<function bool(half)>("Math.MyHalf::IsSubnormal_Impl", (value) => value.IsSubnormal);
} }
#endregion #endregion
+282
View File
@@ -0,0 +1,282 @@
using System;
using System.Runtime.CompilerServices;
namespace GlitchyEngine.Math;
/// <summary>
/// Represents a 16-bit floating point number. (IEEE 754 half-precision binary floating-point (binary16))
/// </summary>
/// <remarks>
/// Even though it is possible, it's not recommended to perform calculations using this type directly, because most operators will simply cast the operands to <see cref="float"/> and cast the result back to <see cref="Half"/>.
/// </remarks>
public struct Half : IComparable , IComparable<Half>, IConvertible, IEquatable<Half>, IFormattable
{
public static readonly Half MinValue = new(-65504); // Should be 0xFBFF
public static readonly Half MaxValue = new(65504); // Should be 0x7BFF
// The numbers for Inifnity, NaN and Zero need to be hardcoded as binaries,
// because the conversion intself relies on them.
public static readonly Half PositiveInfinity = new(0x7C00);
public static readonly Half NegativeInfinity = new(0xFC00);
public static readonly Half NaN = new(0x7CFF);
public static readonly Half Zero = new(0x0000);
public static readonly Half NegativeZero = new(0x8000);
private ushort _data;
public bool IsNegative => IsNegative_Impl(this);
public bool IsFinite => IsFinite_Impl(this);
public bool IsInfinity => IsInfinity_Impl(this);
public bool IsPositiveInfinity => _data == PositiveInfinity._data;
public bool IsNegativeInfinity => _data == NegativeInfinity._data;
public bool IsNaN => IsNan_Impl(this);
public bool IsSubnormal => IsSubnormal_Impl(this);
public Half(float value)
{
FromFloat32(value, out this);
}
private Half(ushort data)
{
_data = data;
}
public static explicit operator float(Half value)
{
ToFloat32(value, out float floatValue);
return floatValue;
}
public static explicit operator Half(float value)
{
FromFloat32(value, out Half halfValue);
return halfValue;
}
/// <summary>Converts the numeric value of this instance to its equivalent string representation.</summary>
/// <returns>The string representation of the value of this instance.</returns>
public override string ToString() => ((float)this).ToString();
public int CompareTo(object obj)
{
if (obj == null)
return 1;
if (obj is not Half value)
throw new ArgumentException($"Object must be of type {typeof(Half)}");
return CompareTo(value);
}
public int CompareTo(Half other)
{
return ((float)this).CompareTo((float)other);
}
public TypeCode GetTypeCode() => TypeCode.Object;
public bool ToBoolean(IFormatProvider provider) => Convert.ToBoolean((float)this, provider);
public byte ToByte(IFormatProvider provider) => Convert.ToByte((float)this, provider);
public char ToChar(IFormatProvider provider) => Convert.ToChar((float)this, provider);
public DateTime ToDateTime(IFormatProvider provider) => Convert.ToDateTime((float)this, provider);
public decimal ToDecimal(IFormatProvider provider) => Convert.ToDecimal((float)this, provider);
public double ToDouble(IFormatProvider provider) => Convert.ToDouble((float)this, provider);
public short ToInt16(IFormatProvider provider) => Convert.ToInt16((float)this, provider);
public int ToInt32(IFormatProvider provider) => Convert.ToInt32((float)this, provider);
public long ToInt64(IFormatProvider provider) => Convert.ToInt64((float)this, provider);
public sbyte ToSByte(IFormatProvider provider) => Convert.ToSByte((float)this, provider);
public float ToSingle(IFormatProvider provider) => Convert.ToSingle((float)this, provider);
/// <summary>Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.</summary>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>The string representation of the value of this instance as specified by <paramref name="provider">provider</paramref>.</returns>
public string ToString(IFormatProvider provider) => ((float)this).ToString(provider);
/// <summary>Converts the numeric value of this instance to its equivalent string representation, using the specified format.</summary>
/// <param name="format">A numeric format string.</param>
/// <returns>The string representation of the value of this instance as specified by <paramref name="format">format</paramref>.</returns>
/// <exception cref="T:System.FormatException"><paramref name="format">format</paramref> is invalid.</exception>
public string ToString(string format) => ((float)this).ToString(format);
/// <summary>Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.</summary>
/// <param name="format">A numeric format string.</param>
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>The string representation of the value of this instance as specified by <paramref name="format">format</paramref> and <paramref name="provider">provider</paramref>.</returns>
public string ToString(string format, IFormatProvider provider) => ((float)this).ToString(format, provider);
public object ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)(float)this).ToType(conversionType, provider);
public ushort ToUInt16(IFormatProvider provider) => Convert.ToUInt16((float)this, provider);
public uint ToUInt32(IFormatProvider provider) => Convert.ToUInt32((float)this, provider);
public ulong ToUInt64(IFormatProvider provider) => Convert.ToUInt64((float)this, provider);
public bool Equals(Half other)
{
return _data == other._data;
}
public override bool Equals(object obj)
{
return obj is Half other && Equals(other);
}
public override int GetHashCode()
{
return _data.GetHashCode();
}
#region Operators
public static bool operator ==(Half left, Half right) => left._data == right._data;
public static bool operator !=(Half left, Half right) => left._data != right._data;
public static bool operator <(Half left, Half right)
{
LessThan_Impl(left, right, out bool result);
return result;
}
public static bool operator <=(Half left, Half right)
{
LessThanOrEqual_Impl(left, right, out bool result);
return result;
}
public static bool operator >(Half left, Half right)
{
GreaterThan_Impl(left, right, out bool result);
return result;
}
public static bool operator >=(Half left, Half right)
{
GreaterThanOrEqual_Impl(left, right, out bool result);
return result;
}
public static Half operator +(Half value) => value;
public static Half operator -(Half value)
{
Negate_Impl(value, out Half result);
return result;
}
public static Half operator +(Half left, Half right)
{
Add_Impl(left, right, out Half result);
return result;
}
public static Half operator -(Half left, Half right)
{
Subtract_Impl(left, right, out Half result);
return result;
}
public static Half operator *(Half left, Half right)
{
Multiply_Impl(left, right, out Half result);
return result;
}
public static Half operator /(Half left, Half right)
{
Divide_Impl(left, right, out Half result);
return result;
}
public static Half operator %(Half left, Half right)
{
Modulo_Impl(left, right, out Half result);
return result;
}
public static Half operator ++(Half value)
{
Increment_Impl(value, out Half result);
return result;
}
public static Half operator --(Half value)
{
Decrement_Impl(value, out Half result);
return result;
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void LessThan_Impl(Half left, Half right, out bool result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void LessThanOrEqual_Impl(Half left, Half right, out bool result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void GreaterThan_Impl(Half left, Half right, out bool result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void GreaterThanOrEqual_Impl(Half left, Half right, out bool result);
// Externe Methoden hier als Platzhalter
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Add_Impl(Half left, Half right, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Subtract_Impl(Half left, Half right, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Multiply_Impl(Half left, Half right, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Divide_Impl(Half left, Half right, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Modulo_Impl(Half left, Half right, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Negate_Impl(Half value, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Increment_Impl(Half value, out Half result);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Decrement_Impl(Half value, out Half result);
#endregion
#region Bindings
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void FromFloat32(float value, out Half halfValue);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void ToFloat32(Half value, out float floatValue);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsNegative_Impl(Half self);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsFinite_Impl(Half self);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsInfinity_Impl(Half self);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsNan_Impl(Half self);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsSubnormal_Impl(Half self);
#endregion
}
-1
View File
@@ -1,5 +1,4 @@
using System; using System;
using GlitchyEngine.Math;
using GlitchyEngine.Math.Attributes; using GlitchyEngine.Math.Attributes;
namespace GlitchyEngine.Math; namespace GlitchyEngine.Math;
-4
View File
@@ -14,10 +14,6 @@
<Exec Command="PowerShell ./postbuild.ps1 -sourceDir $(OutDir) -destinationDir &quot;..\GlitchyEditor\resources\scripts&quot;" /> <Exec Command="PowerShell ./postbuild.ps1 -sourceDir $(OutDir) -destinationDir &quot;..\GlitchyEditor\resources\scripts&quot;" />
</Target> </Target>
<ItemGroup>
<PackageReference Include="Half" Version="1.0.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> <ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\vendor\ImGui.NET\src\ImGui.NET\ImGui.NET.csproj" /> <ProjectReference Include="..\vendor\ImGui.NET\src\ImGui.NET\ImGui.NET.csproj" />