This is an archive of the discontinued LLVM Phabricator instance.

[libc][math] Implement scalbn, scalbnf, scalbnl.
ClosedPublic

Authored by renyichen on Feb 1 2023, 3:43 PM.

Details

Summary

Implement scalbn via fptuil::ldexp for FLT_RADIX==2 case.
"unimplemented" otherwise.

Diff Detail

Event Timeline

renyichen created this revision.Feb 1 2023, 3:43 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 1 2023, 3:44 PM
renyichen requested review of this revision.Feb 1 2023, 3:44 PM
renyichen updated this revision to Diff 494095.Feb 1 2023, 3:47 PM

include other commits.

renyichen updated this revision to Diff 494100.Feb 1 2023, 3:59 PM

Add darwin as well.

renyichen updated this revision to Diff 494101.Feb 1 2023, 4:00 PM

Add scalbn.

renyichen retitled this revision from edit the error message to Add scalbn, scalbnf, scalbnl..Feb 1 2023, 4:53 PM
renyichen edited the summary of this revision. (Show Details)
renyichen edited the summary of this revision. (Show Details)
renyichen updated this revision to Diff 494123.Feb 1 2023, 5:30 PM

Add newline at end of file.

renyichen updated this revision to Diff 494129.Feb 1 2023, 5:41 PM
  • small fix
renyichen edited the summary of this revision. (Show Details)Feb 2 2023, 12:23 PM
renyichen updated this revision to Diff 494447.Feb 2 2023, 3:22 PM

Add scalbn, scalbnf, scalbnl.

scalbn is supposed to compute more efficiently than directly performing x*FLT_RADIX^n as described in C99 Standard (in existing implementations, it doesn't necessary holds).
A rough compare has been performed locally by fixing n and test over the range defined in the existing infra for scalbnf(x, n):

llvmlibcglibcdirectly calc x*exp2f(n)
n = 10 denormal range
avg runtime (ns/op)13.784957.570358.7697
n = 10 normal range
avg runtime (ns/op)9.7110310.740613.6085

llvmlibc outperforms both glibc and directly calc.

Current optimization mechanisms in fputil::ldexp(): 1. early return if n is outside [-MAX_EXPONENT-MantissaWidth-1, MAX_EXPONENT+MantissaWidth+1]. 2. bit manipulation.

renyichen retitled this revision from Add scalbn, scalbnf, scalbnl. to [libc][math] Implement scalbn, scalbnf, scalbnl..Feb 2 2023, 3:39 PM
renyichen edited the summary of this revision. (Show Details)
renyichen added reviewers: lntue, sivachandra.
sivachandra accepted this revision.Feb 2 2023, 3:54 PM

Thanks for the patch. Please wait for @lntue also to approve this.

This revision is now accepted and ready to land.Feb 2 2023, 3:54 PM
lntue accepted this revision.Feb 2 2023, 4:40 PM
lntue added inline comments.
libc/src/__support/FPUtil/ManipulationFunctions.h
119–122

These are exceptional cases, adding unlikely to these 2 conditions could potentially speed up the main cases.

lntue added a comment.Feb 2 2023, 4:42 PM

Can you also update the status table in math.rst?

renyichen updated this revision to Diff 494727.Feb 3 2023, 2:02 PM

Implement scalbn.

renyichen marked an inline comment as done.Feb 3 2023, 2:03 PM
lntue accepted this revision.Feb 3 2023, 4:21 PM
This revision was automatically updated to reflect the committed changes.