-
Notifications
You must be signed in to change notification settings - Fork 39
Implement LFU sketch using arm64 intrinsics #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e40709
basic impl
adc9869
run tests
bitfaster dbe9b79
fix
bitfaster 75481ff
table lookup
bitfaster 87105f6
opt
bitfaster 7d7d023
opt
bitfaster 9475c56
bench
bitfaster e4d397a
Merge branch 'main' into users/alexpeck/arm64
bitfaster 3f5034f
Merge branch 'main' into users/alexpeck/arm64
bitfaster 865156b
Merge branch 'main' into users/alexpeck/arm64
bitfaster 8ca47d2
Merge branch 'main' into users/alexpeck/arm64
bitfaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
|
|
||
| #if !NETSTANDARD2_0 | ||
| using System.Runtime.Intrinsics; | ||
| using System.Runtime.Intrinsics.X86; | ||
| #endif | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| using System.Runtime.Intrinsics.Arm; | ||
| #endif | ||
|
|
||
| namespace BitFaster.Caching.Lfu | ||
| { | ||
| /// <summary> | ||
|
|
@@ -76,6 +81,12 @@ public int EstimateFrequency(T value) | |
| { | ||
| return EstimateFrequencyAvx(value); | ||
| } | ||
| #if NET6_0_OR_GREATER | ||
| else if (isa.IsArm64Supported) | ||
| { | ||
| return EstimateFrequencyArm(value); | ||
| } | ||
| #endif | ||
| else | ||
| { | ||
| return EstimateFrequencyStd(value); | ||
|
|
@@ -99,6 +110,12 @@ public void Increment(T value) | |
| { | ||
| IncrementAvx(value); | ||
| } | ||
| #if NET6_0_OR_GREATER | ||
| else if (isa.IsArm64Supported) | ||
| { | ||
| IncrementArm(value); | ||
| } | ||
| #endif | ||
| else | ||
| { | ||
| IncrementStd(value); | ||
|
|
@@ -329,5 +346,94 @@ private unsafe void IncrementAvx(T value) | |
| } | ||
| } | ||
| #endif | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] | ||
| private unsafe void IncrementArm(T value) | ||
| { | ||
| int blockHash = Spread(comparer.GetHashCode(value)); | ||
| int counterHash = Rehash(blockHash); | ||
| int block = (blockHash & blockMask) << 3; | ||
|
|
||
| Vector128<int> h = AdvSimd.ShiftArithmetic(Vector128.Create(counterHash), Vector128.Create(0, -8, -16, -24)); | ||
| Vector128<int> index = AdvSimd.And(AdvSimd.ShiftRightLogical(h, 1), Vector128.Create(0xf)); | ||
| Vector128<int> blockOffset = AdvSimd.Add(AdvSimd.Add(Vector128.Create(block), AdvSimd.And(h, Vector128.Create(1))), Vector128.Create(0, 2, 4, 6)); | ||
|
|
||
| fixed (long* tablePtr = table) | ||
| { | ||
| int t0 = AdvSimd.Extract(blockOffset, 0); | ||
| int t1 = AdvSimd.Extract(blockOffset, 1); | ||
| int t2 = AdvSimd.Extract(blockOffset, 2); | ||
| int t3 = AdvSimd.Extract(blockOffset, 3); | ||
|
|
||
| Vector128<long> tableVectorA = Vector128.Create(AdvSimd.LoadVector64(tablePtr + t0), AdvSimd.LoadVector64(tablePtr + t1)); | ||
| Vector128<long> tableVectorB = Vector128.Create(AdvSimd.LoadVector64(tablePtr + t2), AdvSimd.LoadVector64(tablePtr + t3)); | ||
|
|
||
| index = AdvSimd.ShiftLeftLogicalSaturate(index, 2); | ||
|
|
||
| Vector128<int> longOffA = AdvSimd.Arm64.InsertSelectedScalar(AdvSimd.Arm64.InsertSelectedScalar(Vector128<int>.Zero, 0, index, 0), 2, index, 1); | ||
| Vector128<int> longOffB = AdvSimd.Arm64.InsertSelectedScalar(AdvSimd.Arm64.InsertSelectedScalar(Vector128<int>.Zero, 0, index, 2), 2, index, 3); | ||
|
|
||
| Vector128<long> fifteen = Vector128.Create(0xfL); | ||
| Vector128<long> maskA = AdvSimd.ShiftArithmetic(fifteen, longOffA.AsInt64()); | ||
| Vector128<long> maskB = AdvSimd.ShiftArithmetic(fifteen, longOffB.AsInt64()); | ||
|
|
||
| Vector128<long> maskedA = AdvSimd.Not(AdvSimd.Arm64.CompareEqual(AdvSimd.And(tableVectorA, maskA), maskA)); | ||
| Vector128<long> maskedB = AdvSimd.Not(AdvSimd.Arm64.CompareEqual(AdvSimd.And(tableVectorB, maskB), maskB)); | ||
|
|
||
| var one = Vector128.Create(1L); | ||
| Vector128<long> incA = AdvSimd.And(maskedA, AdvSimd.ShiftArithmetic(one, longOffA.AsInt64())); | ||
| Vector128<long> incB = AdvSimd.And(maskedB, AdvSimd.ShiftArithmetic(one, longOffB.AsInt64())); | ||
|
|
||
| tablePtr[t0] += AdvSimd.Extract(incA, 0); | ||
| tablePtr[t1] += AdvSimd.Extract(incA, 1); | ||
| tablePtr[t2] += AdvSimd.Extract(incB, 0); | ||
| tablePtr[t3] += AdvSimd.Extract(incB, 1); | ||
|
|
||
| var max = AdvSimd.Arm64.MaxAcross(AdvSimd.Arm64.InsertSelectedScalar(AdvSimd.Arm64.MaxAcross(incA.AsInt32()), 1, AdvSimd.Arm64.MaxAcross(incB.AsInt32()), 0).AsInt16()); | ||
|
|
||
| if (max.ToScalar() != 0 && (++size == sampleSize)) | ||
| { | ||
| Reset(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)] | ||
| private unsafe int EstimateFrequencyArm(T value) | ||
|
||
| { | ||
| int blockHash = Spread(comparer.GetHashCode(value)); | ||
| int counterHash = Rehash(blockHash); | ||
| int block = (blockHash & blockMask) << 3; | ||
|
|
||
| Vector128<int> h = AdvSimd.ShiftArithmetic(Vector128.Create(counterHash), Vector128.Create(0, -8, -16, -24)); | ||
| Vector128<int> index = AdvSimd.And(AdvSimd.ShiftRightLogical(h, 1), Vector128.Create(0xf)); | ||
| Vector128<int> blockOffset = AdvSimd.Add(AdvSimd.Add(Vector128.Create(block), AdvSimd.And(h, Vector128.Create(1))), Vector128.Create(0, 2, 4, 6)); | ||
|
|
||
| fixed (long* tablePtr = table) | ||
| { | ||
| Vector128<long> tableVectorA = Vector128.Create(AdvSimd.LoadVector64(tablePtr + AdvSimd.Extract(blockOffset, 0)), AdvSimd.LoadVector64(tablePtr + AdvSimd.Extract(blockOffset, 1))); | ||
| Vector128<long> tableVectorB = Vector128.Create(AdvSimd.LoadVector64(tablePtr + AdvSimd.Extract(blockOffset, 2)), AdvSimd.LoadVector64(tablePtr + AdvSimd.Extract(blockOffset, 3))); | ||
|
|
||
| index = AdvSimd.ShiftLeftLogicalSaturate(index, 2); | ||
|
|
||
| Vector128<int> indexA = AdvSimd.Negate(AdvSimd.Arm64.InsertSelectedScalar(AdvSimd.Arm64.InsertSelectedScalar(Vector128<int>.Zero, 0, index, 0), 2, index, 1)); | ||
| Vector128<int> indexB = AdvSimd.Negate(AdvSimd.Arm64.InsertSelectedScalar(AdvSimd.Arm64.InsertSelectedScalar(Vector128<int>.Zero, 0, index, 2), 2, index, 3)); | ||
|
|
||
| var fifteen = Vector128.Create(0xfL); | ||
| Vector128<long> a = AdvSimd.And(AdvSimd.ShiftArithmetic(tableVectorA, indexA.AsInt64()), fifteen); | ||
| Vector128<long> b = AdvSimd.And(AdvSimd.ShiftArithmetic(tableVectorB, indexB.AsInt64()), fifteen); | ||
|
|
||
| // Before: < 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F > | ||
| // After: < 0, 1, 2, 3, 8, 9, A, B, 4, 5, 6, 7, C, D, E, F > | ||
| var min = AdvSimd.Arm64.VectorTableLookup(a.AsByte(), Vector128.Create(0x0B0A090803020100, 0xFFFFFFFFFFFFFFFF).AsByte()); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On later versions of .NET VectorTableLookup can also take two or more 128 bit registers as input. See this example: |
||
| min = AdvSimd.Arm64.VectorTableLookupExtension(min, b.AsByte(), Vector128.Create(0xFFFFFFFFFFFFFFFF, 0x0B0A090803020100).AsByte()); | ||
|
|
||
| var min32 = AdvSimd.Arm64.MinAcross(min.AsInt32()); | ||
|
|
||
| return min32.ToScalar(); | ||
| } | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.