mppp icon indicating copy to clipboard operation
mppp copied to clipboard

[BUG] Literal `_rq` performs conversion of string literal in runtime instead of compile time

Open benruijl opened this issue 3 years ago • 2 comments

I expect the performance of the quad prec literal NUM_rq to have the same performance of doing real128(NUM.q), however it is much slower. Running perf on some sample code shows that the conversion from string to __float128 is not happening at compile time as seems to be the case with .q, but occurs at runtime: strtoflt128

When I use real128(NUM.q), there is no function call to strtofloat: nostringconv

Changing from _rq to .q made my code about 1.5 times faster.

benruijl avatar Oct 20 '21 20:10 benruijl

Hi @benruijl . The _rq literal is implemented via a C function from the GCC quadmath library:

https://gcc.gnu.org/onlinedocs/libquadmath/strtoflt128.html#strtoflt128

In order to have the conversion done at compile time, one would need to re-implement the strtoflt128 function (or a subset of it at least) as a constexpr function.

An alternative is to use the floating-point literal for __float128 provided by GCC, which however requires the use of specific compiler flags:

https://gcc.gnu.org/onlinedocs/gcc/Floating-Types.html

bluescarni avatar Oct 21 '21 08:10 bluescarni

At the moment, I am using this second option. Basically, I have turned all the literals into real128(NUM.q), which requires the -fext-numeric-literals flag.

benruijl avatar Oct 21 '21 08:10 benruijl