This is an archive of the discontinued LLVM Phabricator instance.

[libc][math] Improved FBits performance and readablity.
ClosedPublic

Authored by orex on Jun 6 2022, 3:37 AM.

Details

Summary

Some function added in preparation to fmod commit.

Diff Detail

Event Timeline

orex created this revision.Jun 6 2022, 3:37 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 6 2022, 3:37 AM
orex updated this revision to Diff 434424.Jun 6 2022, 4:13 AM

Removed common.h

orex published this revision for review.Jun 6 2022, 4:13 AM
lntue added inline comments.Jun 6 2022, 6:56 AM
libc/src/__support/FPUtil/FPBits.h
129

I forget whether for 80-bit long double, bits 80-127 might contain garbage or not. Would you mind double checking that? If that's the case, you might simply add EXPONENT_MANTISSA_MASK (or abbreviated to EXP_MANT_MASK is up to you) and use that instead of ~SIGN_MASK.

orex added inline comments.Jun 6 2022, 7:21 AM
libc/src/__support/FPUtil/FPBits.h
129

Probably, I did not get your idea fully. long double representation is significantly different from float, double or quad therefore it is covered by separate FBits class (see LongDoubleBits.h). Do you want me to improve that class also? If yes, I don't think that it should be done in the review. There are a lot of checks which should be done and I prefer it to be in a separate review.
As for the mask, it looks like that it is a good idea. I'll implement this.

orex updated this revision to Diff 434466.Jun 6 2022, 7:21 AM

Added EXP_MANT_MASK.

sivachandra accepted this revision.Jun 6 2022, 11:24 PM

As with the other patch, while this is accepted, please hold-off on submitting until further notice here.

libc/src/__support/FPUtil/FloatProperties.h
36

We should use a more formal tone. So, may be: "Exponent and mantissa masks are not as expected."

50

Why is multiplication by 8 preferred over shift by 3?

This revision is now accepted and ready to land.Jun 6 2022, 11:24 PM
orex updated this revision to Diff 434731.Jun 7 2022, 1:39 AM

Update static_assert message.

orex marked an inline comment as done.Jun 7 2022, 1:44 AM
orex added inline comments.
libc/src/__support/FPUtil/FloatProperties.h
50

Just to improve readability. Everybody knows, that byte has 8 bits. But in previous representation it should:

  1. Understand that << refers to explicit multiplication, but not bit/masks operations as everywhere else in the code.
  2. Remember that 2^3=8.

Of course, it is not a big problem, but the code is still complex and need attention. And we should not add extra complications for nothing.

sivachandra accepted this revision.Jun 7 2022, 2:01 AM
sivachandra added inline comments.
libc/src/__support/FPUtil/FloatProperties.h
50

To be absolutely clear, I am OK with using * 8 in this patch. At the same time, I would have found the reasoning more appealing if it was like, "we should let the compilers choose the best instructions for the semantic of multiplying by 8."

orex added inline comments.Jun 7 2022, 2:10 AM
libc/src/__support/FPUtil/FloatProperties.h
50

To be absolutely clear, I'll use your suggestion in the future. It is absolutely reasonable. But in this particular case, this is a` constexpr`, therefore the "best instruction" never appears in the code. It will be done during the compile time, so will not affect runtime at all.

Feel free to submit now.

This revision was automatically updated to reflect the committed changes.