Skip to content

Conversation

@0xilis
Copy link
Contributor

@0xilis 0xilis commented Dec 8, 2023

No description provided.

@0xilis 0xilis changed the title Optim: Don't need to 0 out these bytes due to right shift Optim: Misc math optimizations Dec 8, 2023
@0xilis
Copy link
Contributor Author

0xilis commented Dec 8, 2023

Commit #1: Don't need to 0 out these bytes due to right shift

For the R value, since we're only getting the top level bits of RGB, the bits afterward are irrelevant, and we don't need to 0 them out.

Commit #2: calcPos() faster

Numerous optimizations have been made to calcPos().

For one, width/2 has been moved into a variable. The line w = fabs(height * s * 0.5 / sinHDPi); has also been changed to use the precalculated value of height/2 (y2). The initial value of w also goes unused since it's going to immediately be overridden in the following if statement, so I removed it.

I also improved the logic so cosine is no longer calculated twice in some scenarios.

The most complicated change (and what I expect to be the most controversial) is the new cosine approximation. Rather than using libc's built in cos() function, I changed it to a different approximation formula that was thought of in the 7th Century by Bhaskara the First. This was done as Bhaskara's formula, at least to my testing on my x86_64 mac, seems consistently faster than libc's cos() function. I know this is slightly less accurate, but I believe for our use case it's accurate enough.

I also made calcPos() no longer need to calculate the sin() and instead just get it from the cosine.

Also, not part of calcPos(), but doublePi has been moved down in calcAngle() :P.

Commit #3: Faster sin approximation

I also implemented Bhaskara's sin approximation formula to be used over libc's. This speeds up wave().

Commit #4 (and 5 and 6): Remove unused arg in wave()

Remove the height arg in wave as it is unused.

@0xilis
Copy link
Contributor Author

0xilis commented Dec 8, 2023

Commit #7 (and 8 and 9): Change width arg to unsigned int in wave().

Commit #10: Remove unreachable code path in ScrollRecognizer

Commit #11: Not tested but hopefully caching those as local variables should boost speed...

@0xilis
Copy link
Contributor Author

0xilis commented Dec 9, 2023

Commit #12: Implemented my custom atan approx which is faster than libc

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.

1 participant