Skip to content

Commit

Permalink
1.2.0 Release
Browse files Browse the repository at this point in the history
# Known Issues

- half8 "equals" and "not equals" operators don't conform to the IEEE 754 standard - Unity has not yet reacted to my bug-report in regards to their "half" implementation.

# Fixes

- Added preliminary safety cast to a float of the half value in toboolsafe() until Unity fixes their half '==' and '!=' operators according to IEEE 754

# Additions

### "quarter" precision floats and vectors

- "quarter" is an 8-bit IEEE 754 1.3.4.-3 floating point value, often called a "minifloat"
- It has a very limited range of [-15.5, 15.5] with an epsilon of 0.015625. All integers, aswell as i + 0.5, within that range can be represented as a quarter
- Type conversion from - and to quarters also conforms to the IEEE 754 standard. In detail, casting to a quarter performs rounding according to a) its' precision and b) whether or not the more precise value is closer to 0 or to quarter.Epsilon. NaN and +/- zero preservation, aswell as preservation/clamping to +/- infintiy was also implemented
- "==" and "!=" operators for vectors conforming to the IEEE 754 standard were implemented (unlike, currently, Unity's "half" type). All the other boolean- and arithmetic operators were implemented for the base type only, which will return single precision results (for arithmetic operations). For vectors, quarter vectors are to be (implicitly) cast to single precision vectors first, until/if Unity chnages their "half" implementation.
- Type conversions from - and to all other single value and vector types were implemented
- Full function implementation within the library was added, including: abs(), isnan(), isinf(), isfinite(), select(), as[s]byte/asquarter(), vrol/r(), vshl/r(), tobool[safe]() and toquarter[safe]()

### Fixed Oversights

- Added missing type conversions from - and to half8 for (s)byte8, (u)short8 and (u)int8 vectors
- Added missing type conversions from - and to half8 for booleans and boolean vectors

- Added half "select" functions

- Improved the performance of unsafe boolean-to-half/float/double functions

- added (preliminary?) "abs", "isnan", "isinf" and "isfinite" for half and half vectors, eliminating unnecessary casting
  • Loading branch information
MrUnbelievable92 committed Jan 7, 2021
1 parent 250bb55 commit b34d0a8
Show file tree
Hide file tree
Showing 33 changed files with 5,721 additions and 309 deletions.
6 changes: 3 additions & 3 deletions Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.1.0")]
[assembly: AssemblyFileVersion("1.1.0")]
[assembly: AssemblyInformationalVersion("1.1 Release")]
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly: AssemblyInformationalVersion("1.2 Release")]

[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Unity.Mathematics API consistency")]
73 changes: 73 additions & 0 deletions Runtime/Functions/Arithmetic/Absolute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using Unity.Burst.Intrinsics;
using Unity.Mathematics;

using static Unity.Burst.Intrinsics.X86;

Expand Down Expand Up @@ -122,6 +123,78 @@ public static long4 abs(long4 x)
}


/// <summary> Returns the absolute value of a quarter value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter abs(quarter x)
{
return asquarter((byte)(asbyte(x) & 0b0111_1111));
}

/// <summary> Returns the componentwise absolute value of a quarter2 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter2 abs(quarter2 x)
{
return asquarter(asbyte(x) & 0b0111_1111);
}

/// <summary> Returns the componentwise absolute value of a quarter3 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter3 abs(quarter3 x)
{
return asquarter(asbyte(x) & 0b0111_1111);
}

/// <summary> Returns the componentwise absolute value of a quarter4 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter4 abs(quarter4 x)
{
return asquarter(asbyte(x) & 0b0111_1111);
}

/// <summary> Returns the componentwise absolute value of a quarter8 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter8 abs(quarter8 x)
{
return asquarter(asbyte(x) & 0b0111_1111);
}


/// <summary> Returns the absolute value of a half value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static half abs(half x)
{
return new half { value = ((ushort)(x.value & 0x7FFF)) };
}

/// <summary> Returns the componentwise absolute value of a half2 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static half2 abs(half2 x)
{
return ashalf(asushort(x) & 0x7FFF);
}

/// <summary> Returns the componentwise absolute value of a half3 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static half3 abs(half3 x)
{
return ashalf(asushort(x) & 0x7FFF);
}

/// <summary> Returns the componentwise absolute value of a half4 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static half4 abs(half4 x)
{
return ashalf(asushort(x) & 0x7FFF);
}

/// <summary> Returns the componentwise absolute value of a half8 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static half8 abs(half8 x)
{
return ashalf(asushort(x) & 0x7FFF);
}


/// <summary> Returns the componentwise absolute value of a float8 vector. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float8 abs(float8 x)
Expand Down
144 changes: 144 additions & 0 deletions Runtime/Functions/Bitwise/Bit Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,150 @@ namespace MaxMath
{
unsafe public static partial class maxmath
{
/// <summary> Returns the bit pattern of an sbyte as a quarter. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter asquarter(sbyte x)
{
return new quarter { value = (byte)x };
}

/// <summary> Returns the bit pattern of an sbyte2 as a quarter2. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter2 asquarter(sbyte2 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of an sbyte3 as a quarter3. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter3 asquarter(sbyte3 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of an sbyte4 as a quarter4. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter4 asquarter(sbyte4 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of an sbyte8 as a quarter8. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter8 asquarter(sbyte8 x)
{
return (v128)x;
}


/// <summary> Returns the bit pattern of a byte as a quarter. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter asquarter(byte x)
{
return new quarter { value = x };
}

/// <summary> Returns the bit pattern of a byte2 as a quarter2. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter2 asquarter(byte2 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a byte3 as a quarter3. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter3 asquarter(byte3 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a byte4 as a quarter4. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter4 asquarter(byte4 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a byte8 as a quarter8. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static quarter8 asquarter(byte8 x)
{
return (v128)x;
}


/// <summary> Returns the bit pattern of a quarter as an sbyte. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte assbyte(quarter x)
{
return (sbyte)x.value;
}

/// <summary> Returns the bit pattern of a quarter2 as an sbyte2. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte2 assbyte(quarter2 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a quarter3 as an sbyte3. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte3 assbyte(quarter3 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a quarter4 as an sbyte4. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte4 assbyte(quarter4 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a quarter8 as an sbyte8. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static sbyte8 assbyte(quarter8 x)
{
return (v128)x;
}


/// <summary> Returns the bit pattern of a quarter as a byte. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte asbyte(quarter x)
{
return x.value;
}

/// <summary> Returns the bit pattern of a quarter2 as a byte2. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte2 asbyte(quarter2 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a quarter3 as a byte3. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte3 asbyte(quarter3 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a quarter4 as a byte4. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte4 asbyte(quarter4 x)
{
return (v128)x;
}

/// <summary> Returns the bit pattern of a quarter8 as a byte8. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte8 asbyte(quarter8 x)
{
return (v128)x;
}


/// <summary> Returns the bit pattern of a short as a half. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static half ashalf(short x)
Expand Down
28 changes: 14 additions & 14 deletions Runtime/Functions/Bitwise/Mask to Boolean Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,47 @@ unsafe public static partial class maxmath
{
/// <summary> Returns a bool2 vector from the first two bits of an int value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool2 tobool2(int imm8)
public static bool2 tobool2(int mask)
{
Assert.IsBetween(imm8, 0, 3);
Assert.IsBetween(mask, 0, 3);

int result = 0x0101 & (imm8 | (imm8 << 7));
int result = 0x0101 & (mask | (mask << 7));

return *(bool2*)&result;
}

/// <summary> Returns a bool3 vector from the first 3 bits of an int value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool3 tobool3(int imm8)
public static bool3 tobool3(int mask)
{
byte3 temp = (byte3)(1 & shrl(imm8, new int3(0, 1, 2)));
byte3 temp = (byte3)(1 & shrl(mask, new int3(0, 1, 2)));

return *(bool3*)&temp;
}

/// <summary> Returns a bool4 vector from the first 4 bits of an int value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool4 tobool4(int imm8)
public static bool4 tobool4(int mask)
{
byte4 temp = (byte4)(1 & shrl(imm8, new int4(0, 1, 2, 3)));
byte4 temp = (byte4)(1 & shrl(mask, new int4(0, 1, 2, 3)));

return *(bool4*)&temp;
}

/// <summary> Returns a bool8 vector from the first 8 bits of an int value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool8 tobool8(int imm8)
public static bool8 tobool8(int mask)
{
return (v128)((byte8)(1 & shrl(imm8, new int8(0, 1, 2, 3, 4, 5, 6, 7))));
return (v128)((byte8)(1 & shrl(mask, new int8(0, 1, 2, 3, 4, 5, 6, 7))));
}

/// <summary> Returns a bool16 vector from the first 16 bits of an int value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool16 tobool16(int imm8)
public static bool16 tobool16(int mask)
{
Assert.IsBetween(imm8, 0, ushort.MaxValue);
Assert.IsBetween(mask, 0, ushort.MaxValue);

int8 broadcast = imm8;
int8 broadcast = mask;

ushort16 shufCast = Avx2.mm256_packus_epi32(shrl(broadcast, new int8(0, 1, 2, 3, 4, 5, 6, 7)),
shrl(broadcast, new int8(8, 9, 10, 11, 12, 13, 14, 15)));
Expand All @@ -63,9 +63,9 @@ public static bool16 tobool16(int imm8)

/// <summary> Returns a bool32 vector from the bits of an int value. </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool32 tobool32(int imm8)
public static bool32 tobool32(int mask)
{
int8 broadcast = imm8;
int8 broadcast = mask;

ushort16 hi = Avx2.mm256_packus_epi32(1 & shrl(broadcast, new int8(0, 1, 2, 3, 4, 5, 6, 7)),
1 & shrl(broadcast, new int8(8, 9, 10, 11, 12, 13, 14, 15)));
Expand Down
Loading

0 comments on commit b34d0a8

Please sign in to comment.