-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Change bound checking in SSE/AVX intrinsics to avoid pointer overflow #821
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
Changes from 1 commit
2e0033e
a9558f4
f26f112
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -26,6 +26,9 @@ internal static class SseIntrinsics | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sse.StaticCast<int, float>(Sse2.SetAllVector128(0x7FFFFFFF)) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sse.SetAllVector128(BitConverter.Int32BitsToSingle(0x7FFFFFFF)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// The count of 32-bit floats in Vector128<T> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
internal const int SseAlignment = 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// The count of bytes in Vector128<T>, corresponding to _cbAlign in AlignedArray | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private const int Vector128Alignment = 16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -412,21 +415,20 @@ public static unsafe void AddScalarU(float scalar, Span<float> dst) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fixed (float* pdst = dst) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
float* pDstEnd = pdst + dst.Length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
float* pDstCurrent = pdst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Vector128<float> scalarVector = Sse.SetAllVector128(scalar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
int count = Math.DivRem(dst.Length, SseAlignment, out int remainder); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
float* pDstCurrent = pdst; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (pDstCurrent + 4 <= pDstEnd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = 0; i < count; i++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Vector128<float> dstVector = Sse.LoadVector128(pDstCurrent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dstVector = Sse.Add(dstVector, scalarVector); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sse.Store(pDstCurrent, dstVector); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pDstCurrent += 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pDstCurrent += SseAlignment; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (pDstCurrent < pDstEnd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (int i = 0; i < remainder; i++) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Vector128<float> dstVector = Sse.LoadScalarVector128(pDstCurrent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. This would be way more readable as The various 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. Yes, perf test results show that this change improves the runtime significantly: After all changes, including changing scalar intrinsics to indexed code:BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.228 (1803/April2018Update/Redstone4)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-alpha1-20180720-2
[Host] : .NET Core 3.0.0-preview1-26710-03 (CoreCLR 4.6.26710.05, CoreFX 4.6.26708.04), 64bit RyuJIT
Toolchain=InProcessToolchain
After partial changes (removing the 2nd time of Math.DivRem):BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.228 (1803/April2018Update/Redstone4)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-alpha1-20180720-2
[Host] : .NET Core 3.0.0-preview1-26710-03 (CoreCLR 4.6.26710.05, CoreFX 4.6.26708.04), 64bit RyuJIT
Toolchain=InProcessToolchain
Before the change:BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.228 (1803/April2018Update/Redstone4)
Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.0.100-alpha1-20180720-2
[Host] : .NET Core 3.0.0-preview1-26710-03 (CoreCLR 4.6.26710.05, CoreFX 4.6.26708.04), 64bit RyuJIT
Toolchain=InProcessToolchain
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dstVector = Sse.AddScalar(dstVector, scalarVector); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't alignment, but rather the number of 32-bit elements that Vector256 can hold...
Maybe
Vector256SingleElementCount
or something similar...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
Vector256FloatCount
? Is there any preference forSingleElement
?