flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
Furs
Compile with -Ofast and it will be just a sqrtss. Looks like a bug to me but GCC is full of crap code generation without -ffast-math.
|
|||
![]() |
|
revolution
My guess would be for exception generation/handling.
|
|||
![]() |
|
fpissarra
Sorry, here's a follow up:
Yes, GCC has a tiny problem with this built in function. It should check if the argument is less than 0 before executing sqrtss (or sqrtsd for sqrt() function). The call to libm sqrtf() needs to be made if x < 0 because errno and math exceptions must be set acordingly. I've reported this bug to GCC Bugzilla and it was accepted as such. Future versions (or patches) will correct the problem (which doesn't affect the correctness of the built in function, but makes it very slow only - I've measured 70000 clock cycles in my machine)... |
|||
![]() |
|
revolution
Exceptions are expensive.
Glad you were able to fix the C silliness. ![]() |
|||
![]() |
|
fpissarra
The solutions, until GCC gets patched, are:
1- Use -ffast-math. This way, the compiler will not touch errno (won't call libm sqrt()) and still returns -NAN when x < 0; 2- Use inline assembly: Code: #include <math.h> inline double sqrt_(double x) { if ( x < 0 ) { errno = EDOM; return -NAN; } __asm__ __volatile__ ( "sqrtsd %0,%0" : "+x" (x) ); return x; } |
|||
![]() |
|
revolution
fpissarra wrote: The solutions, until GCC gets patched, are: But what about option 3? 3- Use pure assembly ![]() |
|||
![]() |
|
fpissarra
revolution wrote: But what about option 3? Yep... but, in this case, every call to sqrtf_() won't be 'inline'. PS: I do prefer #1, since is portable (if you are using GCC on multiple platforms). |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.