Skip to content

Commit b34d0a8

Browse files
1.2.0 Release
# 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
1 parent 250bb55 commit b34d0a8

33 files changed

+5721
-309
lines changed

Runtime/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
// Build Number
3232
// Revision
3333
//
34-
[assembly: AssemblyVersion("1.1.0")]
35-
[assembly: AssemblyFileVersion("1.1.0")]
36-
[assembly: AssemblyInformationalVersion("1.1 Release")]
34+
[assembly: AssemblyVersion("1.2.0")]
35+
[assembly: AssemblyFileVersion("1.2.0")]
36+
[assembly: AssemblyInformationalVersion("1.2 Release")]
3737

3838
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Unity.Mathematics API consistency")]

Runtime/Functions/Arithmetic/Absolute.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.CompilerServices;
22
using Unity.Burst.Intrinsics;
3+
using Unity.Mathematics;
34

45
using static Unity.Burst.Intrinsics.X86;
56

@@ -122,6 +123,78 @@ public static long4 abs(long4 x)
122123
}
123124

124125

126+
/// <summary> Returns the absolute value of a quarter value. </summary>
127+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128+
public static quarter abs(quarter x)
129+
{
130+
return asquarter((byte)(asbyte(x) & 0b0111_1111));
131+
}
132+
133+
/// <summary> Returns the componentwise absolute value of a quarter2 vector. </summary>
134+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
135+
public static quarter2 abs(quarter2 x)
136+
{
137+
return asquarter(asbyte(x) & 0b0111_1111);
138+
}
139+
140+
/// <summary> Returns the componentwise absolute value of a quarter3 vector. </summary>
141+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
142+
public static quarter3 abs(quarter3 x)
143+
{
144+
return asquarter(asbyte(x) & 0b0111_1111);
145+
}
146+
147+
/// <summary> Returns the componentwise absolute value of a quarter4 vector. </summary>
148+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149+
public static quarter4 abs(quarter4 x)
150+
{
151+
return asquarter(asbyte(x) & 0b0111_1111);
152+
}
153+
154+
/// <summary> Returns the componentwise absolute value of a quarter8 vector. </summary>
155+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
156+
public static quarter8 abs(quarter8 x)
157+
{
158+
return asquarter(asbyte(x) & 0b0111_1111);
159+
}
160+
161+
162+
/// <summary> Returns the absolute value of a half value. </summary>
163+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
164+
public static half abs(half x)
165+
{
166+
return new half { value = ((ushort)(x.value & 0x7FFF)) };
167+
}
168+
169+
/// <summary> Returns the componentwise absolute value of a half2 vector. </summary>
170+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
171+
public static half2 abs(half2 x)
172+
{
173+
return ashalf(asushort(x) & 0x7FFF);
174+
}
175+
176+
/// <summary> Returns the componentwise absolute value of a half3 vector. </summary>
177+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178+
public static half3 abs(half3 x)
179+
{
180+
return ashalf(asushort(x) & 0x7FFF);
181+
}
182+
183+
/// <summary> Returns the componentwise absolute value of a half4 vector. </summary>
184+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
185+
public static half4 abs(half4 x)
186+
{
187+
return ashalf(asushort(x) & 0x7FFF);
188+
}
189+
190+
/// <summary> Returns the componentwise absolute value of a half8 vector. </summary>
191+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
192+
public static half8 abs(half8 x)
193+
{
194+
return ashalf(asushort(x) & 0x7FFF);
195+
}
196+
197+
125198
/// <summary> Returns the componentwise absolute value of a float8 vector. </summary>
126199
[MethodImpl(MethodImplOptions.AggressiveInlining)]
127200
public static float8 abs(float8 x)

Runtime/Functions/Bitwise/Bit Pattern.cs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,150 @@ namespace MaxMath
88
{
99
unsafe public static partial class maxmath
1010
{
11+
/// <summary> Returns the bit pattern of an sbyte as a quarter. </summary>
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13+
public static quarter asquarter(sbyte x)
14+
{
15+
return new quarter { value = (byte)x };
16+
}
17+
18+
/// <summary> Returns the bit pattern of an sbyte2 as a quarter2. </summary>
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
public static quarter2 asquarter(sbyte2 x)
21+
{
22+
return (v128)x;
23+
}
24+
25+
/// <summary> Returns the bit pattern of an sbyte3 as a quarter3. </summary>
26+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27+
public static quarter3 asquarter(sbyte3 x)
28+
{
29+
return (v128)x;
30+
}
31+
32+
/// <summary> Returns the bit pattern of an sbyte4 as a quarter4. </summary>
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public static quarter4 asquarter(sbyte4 x)
35+
{
36+
return (v128)x;
37+
}
38+
39+
/// <summary> Returns the bit pattern of an sbyte8 as a quarter8. </summary>
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public static quarter8 asquarter(sbyte8 x)
42+
{
43+
return (v128)x;
44+
}
45+
46+
47+
/// <summary> Returns the bit pattern of a byte as a quarter. </summary>
48+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
49+
public static quarter asquarter(byte x)
50+
{
51+
return new quarter { value = x };
52+
}
53+
54+
/// <summary> Returns the bit pattern of a byte2 as a quarter2. </summary>
55+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
56+
public static quarter2 asquarter(byte2 x)
57+
{
58+
return (v128)x;
59+
}
60+
61+
/// <summary> Returns the bit pattern of a byte3 as a quarter3. </summary>
62+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63+
public static quarter3 asquarter(byte3 x)
64+
{
65+
return (v128)x;
66+
}
67+
68+
/// <summary> Returns the bit pattern of a byte4 as a quarter4. </summary>
69+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70+
public static quarter4 asquarter(byte4 x)
71+
{
72+
return (v128)x;
73+
}
74+
75+
/// <summary> Returns the bit pattern of a byte8 as a quarter8. </summary>
76+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
77+
public static quarter8 asquarter(byte8 x)
78+
{
79+
return (v128)x;
80+
}
81+
82+
83+
/// <summary> Returns the bit pattern of a quarter as an sbyte. </summary>
84+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
85+
public static sbyte assbyte(quarter x)
86+
{
87+
return (sbyte)x.value;
88+
}
89+
90+
/// <summary> Returns the bit pattern of a quarter2 as an sbyte2. </summary>
91+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92+
public static sbyte2 assbyte(quarter2 x)
93+
{
94+
return (v128)x;
95+
}
96+
97+
/// <summary> Returns the bit pattern of a quarter3 as an sbyte3. </summary>
98+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
99+
public static sbyte3 assbyte(quarter3 x)
100+
{
101+
return (v128)x;
102+
}
103+
104+
/// <summary> Returns the bit pattern of a quarter4 as an sbyte4. </summary>
105+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106+
public static sbyte4 assbyte(quarter4 x)
107+
{
108+
return (v128)x;
109+
}
110+
111+
/// <summary> Returns the bit pattern of a quarter8 as an sbyte8. </summary>
112+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
113+
public static sbyte8 assbyte(quarter8 x)
114+
{
115+
return (v128)x;
116+
}
117+
118+
119+
/// <summary> Returns the bit pattern of a quarter as a byte. </summary>
120+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121+
public static byte asbyte(quarter x)
122+
{
123+
return x.value;
124+
}
125+
126+
/// <summary> Returns the bit pattern of a quarter2 as a byte2. </summary>
127+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128+
public static byte2 asbyte(quarter2 x)
129+
{
130+
return (v128)x;
131+
}
132+
133+
/// <summary> Returns the bit pattern of a quarter3 as a byte3. </summary>
134+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
135+
public static byte3 asbyte(quarter3 x)
136+
{
137+
return (v128)x;
138+
}
139+
140+
/// <summary> Returns the bit pattern of a quarter4 as a byte4. </summary>
141+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
142+
public static byte4 asbyte(quarter4 x)
143+
{
144+
return (v128)x;
145+
}
146+
147+
/// <summary> Returns the bit pattern of a quarter8 as a byte8. </summary>
148+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149+
public static byte8 asbyte(quarter8 x)
150+
{
151+
return (v128)x;
152+
}
153+
154+
11155
/// <summary> Returns the bit pattern of a short as a half. </summary>
12156
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13157
public static half ashalf(short x)

Runtime/Functions/Bitwise/Mask to Boolean Vector.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,47 @@ unsafe public static partial class maxmath
1111
{
1212
/// <summary> Returns a bool2 vector from the first two bits of an int value. </summary>
1313
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14-
public static bool2 tobool2(int imm8)
14+
public static bool2 tobool2(int mask)
1515
{
16-
Assert.IsBetween(imm8, 0, 3);
16+
Assert.IsBetween(mask, 0, 3);
1717

18-
int result = 0x0101 & (imm8 | (imm8 << 7));
18+
int result = 0x0101 & (mask | (mask << 7));
1919

2020
return *(bool2*)&result;
2121
}
2222

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

2929
return *(bool3*)&temp;
3030
}
3131

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

3838
return *(bool4*)&temp;
3939
}
4040

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

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

54-
int8 broadcast = imm8;
54+
int8 broadcast = mask;
5555

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

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

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

0 commit comments

Comments
 (0)