Skip to content

Instantly share code, notes, and snippets.

@seanofw
Last active November 6, 2023 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanofw/3f3a1c1b4e4d2a55403c007b41ccc8b4 to your computer and use it in GitHub Desktop.
Save seanofw/3f3a1c1b4e4d2a55403c007b41ccc8b4 to your computer and use it in GitHub Desktop.
Patches to make OpenTK.Mathematics build on .NET Standard 2.0 (and .NET Framework 4.6).
diff --git a/src/OpenTK.Mathematics/OpenTK.Mathematics.csproj b/src/OpenTK.Mathematics/OpenTK.Mathematics.csproj
index b2f0466db..704323b26 100644
--- a/src/OpenTK.Mathematics/OpenTK.Mathematics.csproj
+++ b/src/OpenTK.Mathematics/OpenTK.Mathematics.csproj
@@ -1,13 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
- <TargetFrameworks>netstandard2.1;netcoreapp3.1</TargetFrameworks>
+ <TargetFrameworks>netstandard2.1;netcoreapp3.1;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>true</Deterministic>
<RootNamespace>OpenTK.Mathematics</RootNamespace>
+ <Nullable>disable</Nullable>
</PropertyGroup>
<PropertyGroup>
<Description>Mathematical structures and algorithms, provided and used by OpenTK.</Description>
+ <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
+ <PackageId>OpenTK.Mathematics.Legacy</PackageId>
+ <Title>Custom build of OpenTK.Mathematics to support .NET Standard 2.0.</Title>
</PropertyGroup>
<ItemGroup>
diff --git a/src/OpenTK.Mathematics/MathHelper.cs b/src/OpenTK.Mathematics/MathHelper.cs
index f693fed1d..1d855a985 100644
--- a/src/OpenTK.Mathematics/MathHelper.cs
+++ b/src/OpenTK.Mathematics/MathHelper.cs
@@ -10,6 +10,7 @@ using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
+using System.Runtime.CompilerServices;
namespace OpenTK.Mathematics
{
@@ -997,13 +998,21 @@ namespace OpenTK.Mathematics
public static bool ApproximatelyEqual(float a, float b, int maxDeltaBits)
{
// we use longs here, otherwise we run into a two's complement problem, causing this to fail with -2 and 2.0
+#if NETSTANDARD2_0
+ long k = SingleToInt32Bits(a);
+#else
long k = BitConverter.SingleToInt32Bits(a);
+#endif
if (k < 0)
{
k = int.MinValue - k;
}
+#if NETSTANDARD2_0
+ long l = SingleToInt32Bits(b);
+#else
long l = BitConverter.SingleToInt32Bits(b);
+#endif
if (l < 0)
{
l = int.MinValue - l;
@@ -1013,6 +1022,14 @@ namespace OpenTK.Mathematics
return intDiff <= 1 << maxDeltaBits;
}
+#if NETSTANDARD2_0
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static unsafe int SingleToInt32Bits(float value)
+ {
+ return *((int*)&value);
+ }
+#endif
+
/// <summary>
/// Approximates double-precision floating point equality by an epsilon (maximum error) value.
/// This method is designed as a "fits-all" solution and attempts to handle as many cases as possible.
@@ -1146,7 +1163,7 @@ namespace OpenTK.Mathematics
[Pure]
public static float Lerp(float start, float end, float t)
{
- t = Math.Clamp(t, 0, 1);
+ t = Math.Min(Math.Max(t, 0), 1);
return start + (t * (end - start));
}
@@ -1160,7 +1177,7 @@ namespace OpenTK.Mathematics
[Pure]
public static double Lerp(double start, double end, double t)
{
- t = Math.Clamp(t, 0, 1);
+ t = Math.Min(Math.Max(t, 0), 1);
return start + (t * (end - start));
}
#if NETSTANDARD2_0
// This is copied almost verbatim from the .NET Core source code at
// https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/shared/System/HashCode.cs
// with just a few small tweaks to allow it to compile for .NET Standard 2.0.
//
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*
The xxHash32 implementation is based on the code published by Yann Collet:
https://raw.githubusercontent.com/Cyan4973/xxHash/5c174cfa4e45a42f94082dc0d4539b39696afea1/xxhash.c
xxHash - Fast Hash algorithm
Copyright (C) 2012-2016, Yann Collet
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at :
- xxHash homepage: http://www.xxhash.com
- xxHash source repository : https://github.com/Cyan4973/xxHash
*/
using System.Collections.Generic;
using System.ComponentModel;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace OpenTK.Mathematics
{
// xxHash32 is used for the hash code.
// https://github.com/Cyan4973/xxHash
internal struct HashCode
{
private const uint Prime1 = 2654435761U;
private const uint Prime2 = 2246822519U;
private const uint Prime3 = 3266489917U;
private const uint Prime4 = 668265263U;
private const uint Prime5 = 374761393U;
private static readonly uint _staticSeed = (uint)System.DateTime.Now.Ticks;
private uint _v1;
private uint _v2;
private uint _v3;
private uint _v4;
private uint _queue1;
private uint _queue2;
private uint _queue3;
private uint _length;
public static int Combine<T1>(T1 value1)
{
// Provide a way of diffusing bits from something with a limited
// input hash space. For example, many enums only have a few
// possible hashes, only using the bottom few bits of the code. Some
// collections are built on the assumption that hashes are spread
// over a larger space, so diffusing the bits may help the
// collection work more efficiently.
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 4;
hash = QueueRound(hash, hc1);
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2>(T1 value1, T2 value2)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 8;
hash = QueueRound(hash, hc1);
hash = QueueRound(hash, hc2);
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2, T3>(T1 value1, T2 value2, T3 value3)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hash = MixEmptyState();
hash += 12;
hash = QueueRound(hash, hc1);
hash = QueueRound(hash, hc2);
hash = QueueRound(hash, hc3);
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 16;
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 20;
hash = QueueRound(hash, hc5);
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2, T3, T4, T5, T6>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 24;
hash = QueueRound(hash, hc5);
hash = QueueRound(hash, hc6);
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2, T3, T4, T5, T6, T7>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
uint hash = MixState(v1, v2, v3, v4);
hash += 28;
hash = QueueRound(hash, hc5);
hash = QueueRound(hash, hc6);
hash = QueueRound(hash, hc7);
hash = MixFinal(hash);
return (int)hash;
}
public static int Combine<T1, T2, T3, T4, T5, T6, T7, T8>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc2 = (uint)(value2?.GetHashCode() ?? 0);
uint hc3 = (uint)(value3?.GetHashCode() ?? 0);
uint hc4 = (uint)(value4?.GetHashCode() ?? 0);
uint hc5 = (uint)(value5?.GetHashCode() ?? 0);
uint hc6 = (uint)(value6?.GetHashCode() ?? 0);
uint hc7 = (uint)(value7?.GetHashCode() ?? 0);
uint hc8 = (uint)(value8?.GetHashCode() ?? 0);
Initialize(out uint v1, out uint v2, out uint v3, out uint v4);
v1 = Round(v1, hc1);
v2 = Round(v2, hc2);
v3 = Round(v3, hc3);
v4 = Round(v4, hc4);
v1 = Round(v1, hc5);
v2 = Round(v2, hc6);
v3 = Round(v3, hc7);
v4 = Round(v4, hc8);
uint hash = MixState(v1, v2, v3, v4);
hash += 32;
hash = MixFinal(hash);
return (int)hash;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Initialize(out uint v1, out uint v2, out uint v3, out uint v4)
{
v1 = _staticSeed + Prime1 + Prime2;
v2 = _staticSeed + Prime2;
v3 = _staticSeed;
v4 = _staticSeed - Prime1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint Round(uint hash, uint input)
{
return RotateLeft(hash + (input * Prime2), 13) * Prime1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint QueueRound(uint hash, uint queuedValue)
{
return RotateLeft(hash + (queuedValue * Prime3), 17) * Prime4;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MixState(uint v1, uint v2, uint v3, uint v4)
{
return RotateLeft(v1, 1) + RotateLeft(v2, 7) + RotateLeft(v3, 12) + RotateLeft(v4, 18);
}
private static uint MixEmptyState()
{
return _staticSeed + Prime5;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint MixFinal(uint hash)
{
hash ^= hash >> 15;
hash *= Prime2;
hash ^= hash >> 13;
hash *= Prime3;
hash ^= hash >> 16;
return hash;
}
public void Add<T>(T value)
{
Add(value?.GetHashCode() ?? 0);
}
private void Add(int value)
{
// The original xxHash works as follows:
// 0. Initialize immediately. We can't do this in a struct (no
// default ctor).
// 1. Accumulate blocks of length 16 (4 uints) into 4 accumulators.
// 2. Accumulate remaining blocks of length 4 (1 uint) into the
// hash.
// 3. Accumulate remaining blocks of length 1 into the hash.
// There is no need for #3 as this type only accepts ints. _queue1,
// _queue2 and _queue3 are basically a buffer so that when
// ToHashCode is called we can execute #2 correctly.
// We need to initialize the xxHash32 state (_v1 to _v4) lazily (see
// #0) nd the last place that can be done if you look at the
// original code is just before the first block of 16 bytes is mixed
// in. The xxHash32 state is never used for streams containing fewer
// than 16 bytes.
// To see what's really going on here, have a look at the Combine
// methods.
uint val = (uint)value;
// Storing the value of _length locally shaves of quite a few bytes
// in the resulting machine code.
uint previousLength = _length++;
uint position = previousLength % 4;
// Switch can't be inlined.
if (position == 0)
{
_queue1 = val;
}
else if (position == 1)
{
_queue2 = val;
}
else if (position == 2)
{
_queue3 = val;
}
else
{
if (previousLength == 3)
{
Initialize(out _v1, out _v2, out _v3, out _v4);
}
_v1 = Round(_v1, _queue1);
_v2 = Round(_v2, _queue2);
_v3 = Round(_v3, _queue3);
_v4 = Round(_v4, val);
}
}
public int ToHashCode()
{
// Storing the value of _length locally shaves of quite a few bytes
// in the resulting machine code.
uint length = _length;
// position refers to the *next* queue position in this method, so
// position == 1 means that _queue1 is populated; _queue2 would have
// been populated on the next call to Add.
uint position = length % 4;
// If the length is less than 4, _v1 to _v4 don't contain anything
// yet. xxHash32 treats this differently.
uint hash = length < 4 ? MixEmptyState() : MixState(_v1, _v2, _v3, _v4);
// _length is incremented once per Add(Int32) and is therefore 4
// times too small (xxHash length is in bytes, not ints).
hash += length * 4;
// Mix what remains in the queue
// Switch can't be inlined right now, so use as few branches as
// possible by manually excluding impossible scenarios (position > 1
// is always false if position is not > 0).
if (position > 0)
{
hash = QueueRound(hash, _queue1);
if (position > 1)
{
hash = QueueRound(hash, _queue2);
if (position > 2)
{
hash = QueueRound(hash, _queue3);
}
}
}
hash = MixFinal(hash);
return (int)hash;
}
/// <summary>
/// Rotates the specified value left by the specified number of bits.
/// Similar in behavior to the x86 instruction ROL.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..31] is treated as congruent mod 32.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint RotateLeft(uint value, int offset)
=> (value << offset) | (value >> (32 - offset));
/// <summary>
/// Rotates the specified value left by the specified number of bits.
/// Similar in behavior to the x86 instruction ROL.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..63] is treated as congruent mod 64.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong RotateLeft(ulong value, int offset)
=> (value << offset) | (value >> (64 - offset));
/// <summary>
/// Rotates the specified value right by the specified number of bits.
/// Similar in behavior to the x86 instruction ROR.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..31] is treated as congruent mod 32.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static uint RotateRight(uint value, int offset)
=> (value >> offset) | (value << (32 - offset));
/// <summary>
/// Rotates the specified value right by the specified number of bits.
/// Similar in behavior to the x86 instruction ROR.
/// </summary>
/// <param name="value">The value to rotate.</param>
/// <param name="offset">The number of bits to rotate by.
/// Any value outside the range [0..63] is treated as congruent mod 64.</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong RotateRight(ulong value, int offset)
=> (value >> offset) | (value << (64 - offset));
}
}
#endif
#if NETSTANDARD2_0
// This is a simple wrapper that provides a crude equivalent of MathF on
// flavors of .NET that do not already have it.
//
// This is distributed under the MIT license.
using System;
using System.Runtime.CompilerServices;
#pragma warning disable SA1516
namespace OpenTK.Mathematics
{
internal static class MathF
{
public const float PI = 3.141592653f;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Abs(float x)
=> (float)Math.Abs(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Floor(float x)
=> (float)Math.Floor(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Ceiling(float x)
=> (float)Math.Ceiling(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sqrt(float x)
=> (float)Math.Sqrt(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Log(float x)
=> (float)Math.Log(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Log(float x, float y)
=> (float)Math.Log(x, y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Pow(float x, float y)
=> (float)Math.Pow(x, y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sin(float x)
=> (float)Math.Sin(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Cos(float x)
=> (float)Math.Cos(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Tan(float x)
=> (float)Math.Tan(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Asin(float x)
=> (float)Math.Asin(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Acos(float x)
=> (float)Math.Acos(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Atan(float x)
=> (float)Math.Atan(x);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Atan2(float x, float y)
=> (float)Math.Atan2(x, y);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment