Working on L4, now on swiss. FMA_TILE is re arranged.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user