StitchATon2/Domain/Utils.cs

76 lines
2.7 KiB
C#
Raw Normal View History

2025-07-30 07:30:00 +07:00
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using StitchATon2.Infra.Buffers;
namespace StitchATon2.Domain;
public static class Utils
{
[Pure]
public static string GetSbsNotationRow(int row)
2025-07-30 07:30:00 +07:00
=> row <= 26
? new string([(char)(row + 'A' - 1)])
: new string(['A', (char)(row + 'A' - 27)]);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int DivCeil(int a, int b)
{
return (a + b - 1) / b;
}
2025-07-31 06:19:32 +07:00
[Pure]
public static (int Column, int Row) GetSbsNotationCoordinate(string coordinate)
2025-07-30 07:30:00 +07:00
{
var column = coordinate[^1] - '0';
if(char.IsDigit(coordinate[^2]))
column += 10 * (coordinate[^2] - '0');
var row = char.IsLetter(coordinate[1])
? 26 + coordinate[1] - 'A' + 1
: coordinate[0] - 'A' + 1;
return (column, row);
}
2025-07-31 06:19:32 +07:00
/// <summary>
/// Performs a SIMD-accelerated calculation that generates a buffer of bounded, scaled indices.
/// </summary>
/// <param name="scaleFactor">The amount by which to scale the sequence values.</param>
/// <param name="length">The total number of scalar values to generate.</param>
/// <param name="max">Upper limit (exclusive) for clamping values.</param>
/// <param name="offset">The offset to apply before clamping.</param>
2025-07-30 07:30:00 +07:00
public static IBuffer<int> BoundsMatrix(float scaleFactor, int length, int max, int offset)
{
var vectorSize = DivCeil(length + 1, Vector<float>.Count);
2025-07-30 07:30:00 +07:00
using var buffer = MemoryAllocator.Allocate<Vector<float>>(vectorSize);
var span = buffer.Span;
var vectorMin = Vector<int>.Zero;
var vectorOffset = new Vector<int>(offset);
2025-07-30 07:30:00 +07:00
var vectorMax = new Vector<int>(max - 1);
var vectorScale = new Vector<float>(scaleFactor);
2025-07-31 06:19:32 +07:00
var vectorSequence = Vector.CreateSequence(0f, 1f);
2025-07-30 07:30:00 +07:00
var seq = -1f;
2025-07-30 07:30:00 +07:00
for (var i = 0; i < vectorSize; i++, seq += Vector<float>.Count)
{
var sequence = new Vector<float>(seq) + vectorSequence;
2025-07-31 06:19:32 +07:00
span[i] = Vector.FusedMultiplyAdd(sequence, vectorScale, vectorScale);
2025-07-30 07:30:00 +07:00
span[i] = Vector.Ceiling(span[i]);
}
var result = MemoryAllocator.Allocate<int>(vectorSize * Vector<int>.Count);
var resultSpan = MemoryMarshal.Cast<int, Vector<int>>(result.Span);
for (var i = 0; i < vectorSize; i++)
{
resultSpan[i] = Vector.ConvertToInt32(span[i]);
resultSpan[i] = Vector.Add(resultSpan[i], vectorOffset);
2025-07-31 06:19:32 +07:00
resultSpan[i] = Vector.ClampNative(resultSpan[i], vectorMin, vectorMax);
2025-07-30 07:30:00 +07:00
}
return result;
}
}