-
-
Notifications
You must be signed in to change notification settings - Fork 893
Add Avx2 Vector4 Span Premultiplication and Reverse #1399
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6c4d65b
Add Avx2 Span Premultiplication and Reverse
JimBobSquarePants bee9e9a
Use Tanner's updated code.
JimBobSquarePants c4f849c
Remove hotpath attr
JimBobSquarePants 59fa1fd
Use Avx.Shuffle for lower latency
JimBobSquarePants 4aebc13
Fix base unpremultiply benchmark
JimBobSquarePants f1959f3
Merge branch 'master' into js/avx2-premultiplication
JimBobSquarePants 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
68 changes: 68 additions & 0 deletions
68
tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk | ||
| { | ||
| [Config(typeof(Config.ShortCore31))] | ||
| public class PremultiplyVector4 | ||
| { | ||
| private static readonly Vector4[] Vectors = CreateVectors(); | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public void PremultiplyBaseline() | ||
| { | ||
| ref Vector4 baseRef = ref MemoryMarshal.GetReference<Vector4>(Vectors); | ||
|
|
||
| for (int i = 0; i < Vectors.Length; i++) | ||
| { | ||
| ref Vector4 v = ref Unsafe.Add(ref baseRef, i); | ||
| Premultiply(ref v); | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void Premultiply() | ||
| { | ||
| Vector4Utilities.Premultiply(Vectors); | ||
| } | ||
|
|
||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| private static void Premultiply(ref Vector4 source) | ||
| { | ||
| float w = source.W; | ||
| source *= w; | ||
| source.W = w; | ||
| } | ||
|
|
||
| private static Vector4[] CreateVectors() | ||
| { | ||
| var rnd = new Random(42); | ||
| return GenerateRandomVectorArray(rnd, 2048, 0, 1); | ||
| } | ||
|
|
||
| private static Vector4[] GenerateRandomVectorArray(Random rnd, int length, float minVal, float maxVal) | ||
| { | ||
| var values = new Vector4[length]; | ||
|
|
||
| for (int i = 0; i < length; i++) | ||
| { | ||
| ref Vector4 v = ref values[i]; | ||
| v.X = GetRandomFloat(rnd, minVal, maxVal); | ||
| v.Y = GetRandomFloat(rnd, minVal, maxVal); | ||
| v.Z = GetRandomFloat(rnd, minVal, maxVal); | ||
| v.W = GetRandomFloat(rnd, minVal, maxVal); | ||
| } | ||
|
|
||
| return values; | ||
| } | ||
|
|
||
| private static float GetRandomFloat(Random rnd, float minVal, float maxVal) | ||
| => ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal; | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using BenchmarkDotNet.Attributes; | ||
|
|
||
| namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk | ||
| { | ||
| [Config(typeof(Config.ShortCore31))] | ||
| public class UnPremultiplyVector4 | ||
| { | ||
| private static readonly Vector4[] Vectors = CreateVectors(); | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public void UnPremultiplyBaseline() | ||
| { | ||
| ref Vector4 baseRef = ref MemoryMarshal.GetReference<Vector4>(Vectors); | ||
|
|
||
| for (int i = 0; i < Vectors.Length; i++) | ||
| { | ||
| ref Vector4 v = ref Unsafe.Add(ref baseRef, i); | ||
| UnPremultiply(ref v); | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void UnPremultiply() | ||
| { | ||
| Vector4Utilities.UnPremultiply(Vectors); | ||
| } | ||
|
|
||
| [MethodImpl(InliningOptions.ShortMethod)] | ||
| private static void UnPremultiply(ref Vector4 source) | ||
| { | ||
| float w = source.W; | ||
| source *= w; | ||
| source.W = w; | ||
| } | ||
|
|
||
| private static Vector4[] CreateVectors() | ||
| { | ||
| var rnd = new Random(42); | ||
| return GenerateRandomVectorArray(rnd, 2048, 0, 1); | ||
| } | ||
|
|
||
| private static Vector4[] GenerateRandomVectorArray(Random rnd, int length, float minVal, float maxVal) | ||
| { | ||
| var values = new Vector4[length]; | ||
|
|
||
| for (int i = 0; i < length; i++) | ||
| { | ||
| ref Vector4 v = ref values[i]; | ||
| v.X = GetRandomFloat(rnd, minVal, maxVal); | ||
| v.Y = GetRandomFloat(rnd, minVal, maxVal); | ||
| v.Z = GetRandomFloat(rnd, minVal, maxVal); | ||
| v.W = GetRandomFloat(rnd, minVal, maxVal); | ||
| } | ||
|
|
||
| return values; | ||
| } | ||
|
|
||
| private static float GetRandomFloat(Random rnd, float minVal, float maxVal) | ||
| => ((float)rnd.NextDouble() * (maxVal - minVal)) + minVal; | ||
| } | ||
| } |
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
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.
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.
You actually don't need to permute here since you're not crossing 128-bit lanes.
Avx.Shuffle(source, source, 0b_11_11_11_11)will do the same thing with lower latency while eliminating the need to load the mask register.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.
Thanks @saucecontrol I didn't know Shuffle had that overload. Slight speedup.
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.
Still don't understand the same method with
Divideinstead ofMultiplyresults in a ~30X difference in benchmark result though 😖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.
Ha, I didn't read through all the comments and missed that bit. Your baseline UnPremultiply method in the benchmark is multiplying instead of dividing, but I don't see why the vectorized version is coming out so much faster. Will have a look after sleep if you don't figure it out ;)
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.
I knew it! I knew there was a mistake! Thanks!
I guess my computer just doesn't like multiplying stuff. The baseline is way faster now too.
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.
That's strange the division is showing lower times. Is that just an iteration count difference between the Premultiply and UnPremultiply runs? BDN is too clever sometimes.
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.
Nah... Exactly the same setup. I think it's optimizing something away.