This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Rewrite <cmath>
AbandonedPublicDraft

Authored by philnik on Sep 24 2022, 5:14 AM.

Details

Reviewers
ldionne
Mordante
var-const
huixie90
Group Reviewers
Restricted Project
Summary

The <cmath> implementation has a few things that I wanted to change. I originally planned to do them one at a time, but noticed that the changes essentially amount to a rewrite of the header, so I did that instead.

Specifically the changes are:

  • Replacing _LIBCPP_INLINE_VISIBILITY with _LIBCPP_HIDE_FROM_ABI
  • Moving the enable_ifs from the return type to a defaulted template parameter
  • Removing the libcpp prefix on the argument names
  • Granularizing the header
  • Moving the functions into the versioned namespace
  • Using builtins for the math functions

The libcpp prefix has been added for compatibility with newlib in D5080, but I think we can remove the workaround. We use __x elsewhere and newlib doesn't use __x in it's implementation since 2015 for GCC 2.97 and later. I think even older newlibs shouldn't be affected, since we don't rely on the macros from the libc with this patch.

The main functional change here is switching to builtins for all functions. This has multiple benefits:

  • We can provide the full overload set of all functions without having to include the libc math.h
  • We can support <cmath> during constant evaluation, even on platforms which don't have the functions implemented (once P0533R9 is implemented)
  • Platforms with an incomplete implementation of math.h can use libc++ (there will be a link-time error if the functions are used instead of a compile-time error when trying to build libc++)

The main disadvantage of this patch is that it adds a double overload for all functions, but I think this is outweighed by the benefits, especially since the overloads are all just forwarding to __builtin_<function name>.

Diff Detail

Event Timeline

philnik created this revision.Sep 24 2022, 5:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 24 2022, 5:14 AM
philnik updated this revision to Diff 462661.Sep 24 2022, 5:26 AM
  • Generate files
philnik updated this revision to Diff 462665.Sep 24 2022, 6:16 AM
  • try to fix CI
philnik retitled this revision from [libc++] Assume that builtins are available for cmake functions to [libc++] Assume that builtins are available for cmath functions.Sep 27 2022, 1:34 AM
philnik updated this revision to Diff 464947.Oct 4 2022, 4:07 AM

Rewrite the rest of math.h

philnik updated this revision to Diff 464974.Oct 4 2022, 5:25 AM
  • Try to fix CI
philnik retitled this revision from [libc++] Assume that builtins are available for cmath functions to [libc++] Rewrite <cmath>.Oct 4 2022, 6:30 AM
philnik edited the summary of this revision. (Show Details)
philnik updated this revision to Diff 465022.Oct 4 2022, 8:27 AM
  • Try to fix CI
philnik updated this revision to Diff 465065.Oct 4 2022, 10:09 AM
  • Fix tests
philnik updated this revision to Diff 465723.Oct 6 2022, 7:22 AM
  • Try to fix CI
ldionne added inline comments.Oct 6 2022, 8:51 AM
libcxx/include/__cmath/angular_functions.h
2

This change needs to be split into multiple patches that can be reviewed and validated on their own, especially since cmath and math.h interact with the C library, which makes them even more tricky. You already outlined in your commit message all the different things that this patch does, and I think each of them can be a separate patch. I know this is more work, but I think it's the only way to land this.

philnik abandoned this revision.Nov 29 2022, 5:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 29 2022, 5:37 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript