Skip to content
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

Reorder operations in *, abs2 #82

Merged
merged 4 commits into from
Apr 5, 2022
Merged

Conversation

mikmoore
Copy link
Contributor

@mikmoore mikmoore commented Apr 3, 2022

The purpose of this PR is to re-order the operations in *. The primary motivator was to ensure that q' * q == q * q' == abs2(q) exactly, even for float operations. The previous arithmetic was dependent on lucky rounding so these equalities were only approximate. I made sure that Octonion matches Quaternion for these operations when the back half is zero, so that promotion doesn't risk changing arithmetic. I also added methods for scalar multiplication (the compiler wasn't able to compile away the zeros on its own) and some basic tests to ensure that * doesn't get broken by accident if somebody tries to improve it in the future.

There is also some performance improvement due to spending some time fiddling with the ordering of operations. *(::QuaternionF64,::QuaternionF64) goes from 4ns down to 3ns on my machine (and with #75 it looks like it might drop further to the 2.5ns ballpark). In the process I tried to get abs2 to be as fast as possible, since #75 might make use of it. I changed abs to track these changes.

There are alternative orderings that could permit some fma instructions without breaking the target equalities and presumably be more accurate (and slightly faster on machines with native fma), but when I experimented with fma versions they weren't generating faster code. Also, it's not easy to add "fma only if native" operations right now (muladd can re-associate adjacent operations, not just the arguments). Maybe somebody revisits this in the future, but the gains won't be huge.

@codecov
Copy link

codecov bot commented Apr 3, 2022

Codecov Report

Merging #82 (a80e259) into master (3d2468a) will increase coverage by 0.12%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #82      +/-   ##
==========================================
+ Coverage   97.12%   97.25%   +0.12%     
==========================================
  Files           3        3              
  Lines         383      401      +18     
==========================================
+ Hits          372      390      +18     
  Misses         11       11              
Impacted Files Coverage Δ
src/Octonion.jl 96.62% <100.00%> (+0.47%) ⬆️
src/Quaternion.jl 98.55% <100.00%> (+0.05%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

Co-authored-by: Yuto Horikawa <hyrodium@gmail.com>
Copy link
Collaborator

@hyrodium hyrodium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! (I'll merge after the abs2 testset for Octonion is updated.)

Co-authored-by: Yuto Horikawa <hyrodium@gmail.com>
@hyrodium hyrodium merged commit c7ae0f1 into JuliaGeometry:master Apr 5, 2022
@hyrodium hyrodium mentioned this pull request Dec 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants