Working on L4, now on swiss. FMA_TILE is re arranged.

This commit is contained in:
Youmu Liu
2026-03-03 18:02:31 +08:00
parent ae1a49d6fb
commit 91e136f81f
12 changed files with 1083 additions and 477 deletions
@@ -1,162 +0,0 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="StringHashMultiplexer"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace StgSharp.Collections
{
public sealed class StringHashMultiplexer : IHashMultiplexer<string>
{
private int _initCount, _remainedCount, _hashCache, _hashLast;
private string _source, _sourceLast;
public int RemainedMultiplexCount
{
get => _remainedCount;
set => _initCount = value;
}
public void CacheHash(string obj, int count)
{
_hashCache = ForceComputeHash(obj);
_initCount = count;
_remainedCount = _initCount;
_source = obj;
}
public bool Equals(string x, string y)
{
return x == y;
}
public void ExtendCache(int count)
{
_initCount += count;
_remainedCount += count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe int ForceComputeHash([NotNull] string obj)
{
int hash;
fixed (char* cPtr = obj.AsSpan()) {
hash = NativeIntrinsic.Context.city_hash_simplify(cPtr, obj.Length);
}
return hash;
}
public int GetHashCode(string obj)
{
if (_remainedCount > 0 && ReferenceEquals(_source, obj))
{
_remainedCount--;
return _hashCache;
}
if (ReferenceEquals(_sourceLast, obj)) {
return _hashLast;
}
_sourceLast = obj;
_hashLast = ForceComputeHash(obj);
return _hashLast;
}
}
public sealed class ConcurrentStringHashMultiplexer : IHashMultiplexer<string>, IDisposable
{
private bool disposedValue;
private readonly ThreadLocal<StringHashMultiplexer> _multiplexers =
new(() => new StringHashMultiplexer());
public int RemainedMultiplexCount
{
get => _multiplexers.Value!.RemainedMultiplexCount;
set => _multiplexers.Value!.RemainedMultiplexCount = value;
}
public void CacheHash(string obj, int count)
{
throw new NotImplementedException();
}
// ~ConcurrentStringHashMultiplexer()
// {
// Dispose(disposing: false);
// }
public void Dispose()
{
Dispose(disposing:true);
GC.SuppressFinalize(this);
}
public bool Equals(string x, string y)
{
return x == y;
}
public void ExtendCache(int count)
{
_multiplexers.Value!.ExtendCache(count);
}
public unsafe int ForceComputeHash([NotNull] string obj)
{
int hash;
fixed (char* cPtr = obj.AsSpan()) {
hash = NativeIntrinsic.Context.city_hash_simplify(cPtr, obj.Length);
}
return hash;
}
public int GetHashCode(string obj)
{
return _multiplexers.Value!.GetHashCode(obj);
}
private void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing) {
_multiplexers.Dispose();
}
disposedValue = true;
}
}
}
}
@@ -0,0 +1,178 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="SwissTable.X86"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
namespace StgSharp.Collections
{
public partial class SwissTable
{
private unsafe class BucketScannerX86 : IBucketScanner
{
public bool TryFind(
nint key,
uint Hash,
Bucket* bucket,
out nint value
)
{
byte ctrl = (byte)(Hash >> 8);
Vector128<byte> ctrlVector = Vector128.Create(ctrl);
for (int i = 0; i < BucketSize; i += 16)
{
Vector128<byte> bucketCtrlVector = Sse2.LoadVector128(&bucket->Control.Control + i);
Vector128<byte> cmpResult = Sse2.CompareEqual(ctrlVector, bucketCtrlVector);
int mask = Sse2.MoveMask(cmpResult);
while (mask != 0)
{
int bitPos = BitOperations.TrailingZeroCount(mask);
int index = i + bitPos;
if (bucket->Keys.value == key)
{
value = bucket->Values.value;
return true;
}
mask &= mask - 1;
}
}
value = default;
return false;
}
public bool TryRemove(
nint key,
uint hash,
Bucket* bucket,
out nint value
)
{
byte ctrl = (byte)(hash >> 8);
Vector128<byte> ctrlVector = Vector128.Create(ctrl);
for (int i = 0; i < BucketSize; i += 16)
{
Vector128<byte> bucketCtrl = Sse2.LoadVector128(
&bucket->Control[i]);
Vector128<byte> matchMask = Sse2.CompareEqual(ctrlVector, bucketCtrl);
int matches = Sse2.MoveMask(matchMask);
while (matches != 0)
{
int bitPos = BitOperations.TrailingZeroCount(matches);
int index = i + bitPos;
if (bucket->Keys[index] == key)
{
value = bucket->Values[index];
bucket->Control[index] = 0xFE; // mark as DELETED
// no need to remove Key/Value
return true;
}
matches &= matches - 1;
}
}
value = default;
return false;
}
public bool TrySet(
nint key,
uint hash,
Bucket* bucket,
nint value,
out bool isNewKey
)
{
byte ctrl = (byte)(hash >> 8); // H2: upper 8 bits (highest bit is naturally 0)
Vector128<byte> ctrlVector = Vector128.Create(ctrl);
int firstAvailableSlot = -1; // Record the first insertable slot
for (int i = 0; i < BucketSize; i += 16)
{
Vector128<byte> bucketCtrl = Sse2.LoadVector128(
&bucket->Control[i]);
// === Phase 1: Look up an existing key ===
Vector128<byte> matchMask = Sse2.CompareEqual(ctrlVector, bucketCtrl);
int matches = Sse2.MoveMask(matchMask);
while (matches != 0)
{
int bitPos = BitOperations.TrailingZeroCount(matches);
int index = i + bitPos;
if (bucket->Keys[index] == key)
{
// Key already exists -> update value
bucket->Values[index] = value;
isNewKey = false;
return true;
}
matches &= matches - 1;
}
// === Phase 2: Track an available insertion slot ===
if (firstAvailableSlot == -1)
{
// Find EMPTY (0xFF) or DELETED (0xFE)
// Trick: bytes with high bit set are insertable
int emptyOrDeleted = Sse2.MoveMask(bucketCtrl);
// MoveMask extracts the top bit of each byte
// EMPTY/DELETED top bit = 1 -> mask bit = 1
// OCCUPIED top bit = 0 -> mask bit = 0
if (emptyOrDeleted != 0)
{
int bitPos = BitOperations.TrailingZeroCount(emptyOrDeleted);
firstAvailableSlot = i + bitPos;
}
}
}
// === Phase 3: Key not found, try to insert ===
if (firstAvailableSlot == -1)
{
// Bucket is full; resize is required
isNewKey = false;
return false;
}
bucket->Control[firstAvailableSlot] = ctrl;
bucket->Keys[firstAvailableSlot] = key;
bucket->Values[firstAvailableSlot] = value;
isNewKey = true;
return true;
}
}
}
}
+148 -12
View File
@@ -26,34 +26,170 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
namespace StgSharp.Collections
{
public unsafe class SwissTable
/// <summary>
///
/// </summary>
public unsafe partial class SwissTable
{
public bool TryGet(nint key, out nint value)
private const int BucketCount = 1 << EntryMaskWidth;
private const int BucketSize = 256;
private const int EntryMaskWidth = 10; // 1024 entries total, 256 per bucket
public const int MaxCapacity = 1024 * BucketSize; // 256 entries per bucket, 1024 buckets
private readonly Entry* _entries;
private SwissTable(
Entry* buffer
)
{
_entries = buffer;
for (int i = 0; i < BucketCount; i++)
{
// Set all control bytes to 0xFF (empty)
Unsafe.InitBlock(&_entries[i].Bucket.Control, 0xFF, BucketSize);
}
}
public bool TryAddOrSet(
nint key,
nint value
)
{
throw new NotImplementedException();
}
public bool TryAddOrSet(
nint key,
ref nint value,
out bool isNewKey
)
{
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
short entry = (short)(hash & ((1 << 11) - 1));
Bucket* bucket = &_entries[entry].Bucket;
return IBucketScanner.Current.TrySet(key, hash, bucket, value, out isNewKey);
}
public bool TryGet(
nint key,
out nint value
)
{
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
short entry = (short)(hash & ((1 << 11) - 1));
Bucket* bucket = &_entries[entry].Bucket;
return IBucketScanner.Current.TryFind(key, hash, bucket, out value);
/*
* hash = CRC32 of key
* entry = low 8 of hash
* ctrl = 8-15 of hash
* index = rest of hash
* find value of hash
*/
*/
}
public bool TryRemove(
nint key,
out nint value
)
{
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
short entry = (short)(hash & ((1 << 11) - 1));
Bucket* bucket = &_entries[entry].Bucket;
return IBucketScanner.Current.TryRemove(key, hash, bucket, out value);
}
[InlineArray(BucketCount)]
private struct Entry
{
public fixed byte entry[256]; //entry is fully occupied, or empty, or deleted
// array of ctrl array
// array of value array
// array of hash array
public Bucket Bucket;
}
private unsafe struct Bucket
{
public BucketControl Control;
public BucketKeyStorage Keys;
public BucketValueStorage Values;
}
private interface IBucketScanner
{
static IBucketScanner Current { get; } = Create();
bool TryFind(
nint key,
uint Hash,
Bucket* bucket,
out nint value
);
unsafe bool TryRemove(
nint key,
uint hash,
Bucket* bucket,
out nint value
);
bool TrySet(
nint key,
uint hash,
Bucket* bucket,
nint value,
out bool isNewKey
);
private static IBucketScanner Create()
{
if (X86Base.IsSupported)
{
return new BucketScannerX86();
} else if (ArmBase.IsSupported)
{
throw new NotSupportedException("ARM architecture is currently not supported for bucket scanning. Please use an x86 platform.");
} else
{
throw new NotSupportedException("No suitable bucket scanner implementation found for the current platform.");
}
}
}
[InlineArray(BucketSize)]
private struct BucketControl
{
public byte Control;
}
[InlineArray(BucketSize)]
private struct BucketKeyStorage
{
public nint value;
}
[InlineArray(BucketSize)]
private struct BucketValueStorage
{
public nint value;
}
}
}
}