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

[raymath] Make every normalize function similar #3847

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

planetis-m
Copy link
Contributor

@planetis-m planetis-m commented Feb 29, 2024

Follow up to #2982 Makes normalize functions consistent, moved sqrtf call after the if statement which causes its assembly to be slightly better in both clang 17 and gcc 13. It now uses one rsqrtss instruction instead of sqrtss + divss combo https://godbolt.org/z/5K475o88n plus in clang it's branchless.

@raysan5
Copy link
Owner

raysan5 commented Feb 29, 2024

@planetis-m This change modifies multiple functions and it's quite sensible, did you implement some test example to verify everything works exactly the same way? Some test cases would be really useful in this situation.

@planetis-m
Copy link
Contributor Author

planetis-m commented Feb 29, 2024

I don't have tests cases for all the functions I changed, they're quite a lot, however my justification is that the number with square root 0 can only be 0. So delaying the call to the function sqrtf after the "if value is 0" statement doesn't make any difference in the result. In fact it was already done like that in the ClampValue functions.

@planetis-m
Copy link
Contributor Author

Relevant SO question: https://stackoverflow.com/questions/73988574/given-x0-is-it-possible-for-sqrtx-0-to-give-a-floating-point-error Maybe it's preferable to remove the check altogether?

@raysan5 raysan5 changed the title Make every normalize function similar [raymath] Make every normalize function similar Mar 31, 2024
@planetis-m
Copy link
Contributor Author

planetis-m commented May 6, 2024

I have tried the example in the SO answer and the condition xxyy == 0 was hit with both clang and gcc. I've also asked LLMs to try to break it with their own values for x and y but couldn't. I think it's ok to merge.

#include <stdio.h>
#include <math.h>

int main() {
    double x = 0x1p-1021;
    double y = 0x2p-1020; // or y = 0
    double xxyy = x * x + y * y;
    if (xxyy == 0) printf("caught\n");
    printf("%a %a\n", x, y);
    printf("%a %a\n", xxyy, sqrt(xxyy));

    return 0;
}

But I can switch the checks to abs(xxyy) <= EPSILON, if it's any better.

@raysan5
Copy link
Owner

raysan5 commented May 7, 2024

@planetis-m This PR changes many functions and it should be carefully tested to verify every new implementation behaves exactly like previous one. Also note that comparing float values with == could be really dangerous due to rounding issues. Afaik, it's a not recomended practize.

@raysan5 raysan5 added the help needed - please! I need help with this issue label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help needed - please! I need help with this issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants