Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 11 commits into from
Jan 28, 2025

Conversation

AlexRadch
Copy link
Contributor

Close #28731

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.
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 10, 2025
AlexRadch and others added 4 commits January 10, 2025 15:46
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.
@AlexRadch AlexRadch requested a review from huoyaoyuan January 10, 2025 10:10
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.
Comment on lines +371 to +372
Vector128<float> v1 = vector1.AsVector128Unsafe();
Vector128<float> v2 = vector2.AsVector128Unsafe();
Copy link
Member

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".

Comment on lines 341 to 345
//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();
Copy link
Member

@tannergooding tannergooding Jan 27, 2025

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.
@tannergooding tannergooding merged commit f1901a0 into dotnet:main Jan 28, 2025
142 checks passed
@AlexRadch AlexRadch deleted the Vector2_Vector4_CrossProduct branch January 28, 2025 19:58
grendello added a commit to grendello/runtime that referenced this pull request Jan 28, 2025
* 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)
  ...
@github-actions github-actions bot locked and limited conversation to collaborators Feb 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Numerics community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(System.Numerics) Cross Product for Vector2 and Vector4
5 participants