|
1 |
| -// Copyright (c) Six Labors. |
| 1 | +// Copyright (c) Six Labors. |
2 | 2 | // Licensed under the Six Labors Split License.
|
3 | 3 |
|
4 | 4 | using System.Diagnostics;
|
5 | 5 | using System.Numerics;
|
6 | 6 | using System.Runtime.CompilerServices;
|
7 |
| -using System.Runtime.InteropServices; |
8 | 7 | using System.Runtime.Intrinsics;
|
9 | 8 | using System.Runtime.Intrinsics.X86;
|
10 | 9 |
|
@@ -36,30 +35,37 @@ internal static Vector4 PseudoRound(this Vector4 v)
|
36 | 35 |
|
37 | 36 | /// <summary>
|
38 | 37 | /// Rounds all values in 'v' to the nearest integer following <see cref="MidpointRounding.ToEven"/> semantics.
|
39 |
| - /// Source: |
40 |
| - /// <see> |
41 |
| - /// <cref>https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L110</cref> |
42 |
| - /// </see> |
43 | 38 | /// </summary>
|
44 | 39 | /// <param name="v">The vector</param>
|
45 | 40 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
46 | 41 | internal static Vector<float> FastRound(this Vector<float> v)
|
47 | 42 | {
|
48 |
| - if (Avx2.IsSupported) |
| 43 | + if (Avx512F.IsSupported && Vector<float>.Count == Vector512<float>.Count) |
| 44 | + { |
| 45 | + ref Vector512<float> v512 = ref Unsafe.As<Vector<float>, Vector512<float>>(ref v); |
| 46 | + |
| 47 | + // imm8 = 0b1000: |
| 48 | + // imm8[7:4] = 0b0000 -> preserve 0 fractional bits (round to whole numbers) |
| 49 | + // imm8[3:0] = 0b1000 -> _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC (round to nearest even, suppress exceptions) |
| 50 | + Vector512<float> vRound = Avx512F.RoundScale(v512, 0b0000_1000); |
| 51 | + return Unsafe.As<Vector512<float>, Vector<float>>(ref vRound); |
| 52 | + } |
| 53 | + |
| 54 | + if (Avx2.IsSupported && Vector<float>.Count == Vector256<float>.Count) |
49 | 55 | {
|
50 | 56 | ref Vector256<float> v256 = ref Unsafe.As<Vector<float>, Vector256<float>>(ref v);
|
51 | 57 | Vector256<float> vRound = Avx.RoundToNearestInteger(v256);
|
52 | 58 | return Unsafe.As<Vector256<float>, Vector<float>>(ref vRound);
|
53 | 59 | }
|
54 |
| - else |
55 |
| - { |
56 |
| - var magic0 = new Vector<int>(int.MinValue); // 0x80000000 |
57 |
| - var sgn0 = Vector.AsVectorSingle(magic0); |
58 |
| - var and0 = Vector.BitwiseAnd(sgn0, v); |
59 |
| - var or0 = Vector.BitwiseOr(and0, new Vector<float>(8388608.0f)); |
60 |
| - var add0 = Vector.Add(v, or0); |
61 |
| - return Vector.Subtract(add0, or0); |
62 |
| - } |
| 60 | + |
| 61 | + // https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L11 |
| 62 | + Vector<int> magic0 = new(int.MinValue); // 0x80000000 |
| 63 | + Vector<float> sgn0 = Vector.AsVectorSingle(magic0); |
| 64 | + Vector<float> and0 = Vector.BitwiseAnd(sgn0, v); |
| 65 | + Vector<float> or0 = Vector.BitwiseOr(and0, new Vector<float>(8388608.0f)); |
| 66 | + Vector<float> add0 = Vector.Add(v, or0); |
| 67 | + |
| 68 | + return Vector.Subtract(add0, or0); |
63 | 69 | }
|
64 | 70 |
|
65 | 71 | [Conditional("DEBUG")]
|
|
0 commit comments