-
Notifications
You must be signed in to change notification settings - Fork 5k
Cross Product for Vector2 and Vector4 #111265
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
Cross Product for Vector2 and Vector4 #111265
Conversation
…tor4_CrossProduct
Refactor test methods in `Vector2Tests.cs` and `Vector4Tests.cs` for clarity and correctness. Enhance the `Cross` methods in `Vector2.cs` and `Vector4.cs` using SIMD techniques for improved performance.
Note regarding the
|
1 similar comment
Note regarding the
|
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs
Outdated
Show resolved
Hide resolved
Reverted the Cross method to a simpler implementation by removing the use of Vector64 for vector math. The method now directly computes the cross product using the formula `value1.X * value2.Y - value1.Y * value2.X`, prioritizing clarity and performance on xarch architectures.
…r4.cs Co-authored-by: Huo Yaoyuan <[email protected]>
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs
Outdated
Show resolved
Hide resolved
Updated the `Cross` method in `Vector2.cs` to utilize SIMD operations with `Vector128`, enhancing performance through hardware acceleration. The `Cross` method in `Vector3.cs` has been modified to use `AsVector128Unsafe()`, aligning with the performance improvements.
Refactor the `Cross` method to enhance performance by utilizing `Vector128.MultiplyAddEstimate`.
Removed unnecessary assignment in the Cross method and streamlined the return statement. This change enhances code clarity and efficiency by eliminating redundancy.
Vector128<float> v1 = vector1.AsVector128Unsafe(); | ||
Vector128<float> v2 = vector2.AsVector128Unsafe(); |
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.
If you're going to do this, you should modify the Shuffle
below to use -1
, rather than 3
, for selecting W
It avoids a potential pessimization on some hardware if W
is NaN
or similar and is "free".
//return value1.X * value2.Y - value1.Y * value2.X; | ||
|
||
Vector128<float> mul = value1.AsVector128Unsafe() * | ||
Vector128.Shuffle(value2.AsVector128Unsafe(), Vector128.Create(1, 0, 1, 0)); | ||
return (mul - Vector128.Shuffle(mul, Vector128.Create(1, 0, 1, 0))).ToScalar(); |
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.
Similar here. It is preferable to use AsVector128
or to guarantee the zeroing of the upper elements by using -1
as the index in the shuffle. It helps avoid pessimization and should be naturally part of the underlying codegen without inserting additional instructions (in some cases at least, you'll need to compare and see which is better in most cases)
Refactored the `Cross` method in `Vector2.cs` to enhance performance by using vector shuffling and subtraction with SIMD operations. Updated the `Cross` method in `Vector3.cs` to correct shuffling indices for accurate component multiplication, also leveraging SIMD for improved efficiency.
* main: (31 commits) Fix linux-x86 build (dotnet#111861) Add FrozenDictionary specialization for integers / enums (dotnet#111886) [SRM] Refactor reading from streams. (dotnet#111323) Sign the DAC and DBI during the build process instead of in separate steps (dotnet#111416) Removing Entry2MethodDesc as it is unnecessary (dotnet#111756) Cross Product for Vector2 and Vector4 (dotnet#111265) Handle unicode in absolute URI path for combine. (dotnet#111710) Drop RequiresProcessIsolation on mcc tests (dotnet#111887) [main] Update dependencies from dotnet/roslyn (dotnet#111691) new trimmer feature System.TimeZoneInfo.Invariant (dotnet#111215) [browser] reduce msbuild memory footprint (dotnet#111751) Add debugging checks for stack overflow tests failure (dotnet#111867) Localized file check-in by OneLocBuild Task: Build definition ID 679: Build ID 2629821 (dotnet#111884) Bump main to preview2 (dotnet#111882) Avoid generic virtual dispatch for frozen collections alternate lookup (dotnet#108732) Bump main versioning to preview1 (dotnet#111880) Switch OneLoc to main (dotnet#111872) Improve docs on building ILVerify (dotnet#111851) Update Debian version to 13 (dotnet#111768) win32: add fallback to environment vars for system folder (dotnet#109673) ...
Close #28731