Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 73 additions & 47 deletions libcxx/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,53 +307,79 @@ long double truncl(long double x);
// back to C++ linkage before including these C++ headers.
extern "C++" {

# ifdef fpclassify
# undef fpclassify
# endif

# ifdef signbit
# undef signbit
# endif

# ifdef isfinite
# undef isfinite
# endif

# ifdef isinf
# undef isinf
# endif

# ifdef isnan
# undef isnan
# endif

# ifdef isnormal
# undef isnormal
# endif

# ifdef isgreater
# undef isgreater
# endif

# ifdef isgreaterequal
# undef isgreaterequal
# endif

# ifdef isless
# undef isless
# endif

# ifdef islessequal
# undef islessequal
# endif

# ifdef islessgreater
# undef islessgreater
# endif

# ifdef isunordered
# undef isunordered
# endif
// According to section 7.1.4 Use of library functions of the C standard, any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it would make more sense to undefine each macro where the corresponding libc++ function is implemented. For example in

#undef cosh
inline _LIBCPP_HIDE_FROM_ABI float cosh(float __x) _NOEXCEPT { return __builtin_coshf(__x); }
// etc...

in libcxx/include/__math/hyperbolic_functions.h.

Copy link
Member Author

@petrhosek petrhosek Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that in 745745c but that lead to build errors, see #94533 (comment). I noticed that the particular <cmath> include that triggered that build error was removed by @philnik777 in 0dd8c0d so it might worth trying it again and see if there are any remaining issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read that comment, but I still don't understand what's happening that would prevent the #undef from being located right before the definition of the libc++ function with the same name. It seems like we need exactly one canonical place where we #undef foo and then define namespace std { int foo(); }, and I don't see how this can lead to problems?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ldionne Consider

#include <__math/traits.h>
#include <math.h>

The __math/traits.h would try to #undef a macro that will only be defined when including math.h. Does that make it clearer?

// function declared in a header may be additionally implemented as a
// function-like macro defined in the header, so if a library function is
// declared explicitly when the C standard library header is included, as is
// the case here, we need to use #undef to remove any macro definition.
# undef acos
# undef acosh
# undef asin
# undef asinh
# undef atan
# undef atan2
# undef atanh
# undef cbrt
# undef ceil
# undef copysign
# undef cos
# undef cosh
# undef erf
# undef erfc
# undef exp
# undef exp2
# undef expm1
# undef fabs
# undef fdim
# undef floor
# undef fma
# undef fmax
# undef fmin
# undef fmod
# undef fpclassify
# undef frexp
# undef hypot
# undef ilogb
# undef isfinite
# undef isgreater
# undef isgreaterequal
# undef isinf
# undef isless
# undef islessequal
# undef islessgreater
# undef isnan
# undef isnormal
# undef isunordered
# undef ldexp
# undef lgamma
# undef llrint
# undef llround
# undef log
# undef log10
# undef log1p
# undef log2
# undef logb
# undef lrint
# undef lround
# undef modf
# undef nearbyint
# undef nextafter
# undef nexttoward
# undef pow
# undef remainder
# undef remquo
# undef rint
# undef round
# undef scalbln
# undef scalbn
# undef signbit
# undef sin
# undef sinh
# undef sqrt
# undef tan
# undef tanh
# undef tgamma
# undef trunc

# include <__math/abs.h>
# include <__math/copysign.h>
Expand Down
24 changes: 6 additions & 18 deletions libcxx/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,9 @@ void *aligned_alloc(size_t alignment, size_t size); // C11
extern "C++" {
// abs

# ifdef abs
# undef abs
# endif
# ifdef labs
# undef labs
# endif
# ifdef llabs
# undef llabs
# endif
# undef abs
# undef labs
# undef llabs

// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
# if !defined(_LIBCPP_MSVCRT)
Expand All @@ -128,15 +122,9 @@ _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI long double abs(long double __lcp

// div

# ifdef div
# undef div
# endif
# ifdef ldiv
# undef ldiv
# endif
# ifdef lldiv
# undef lldiv
# endif
# undef div
# undef ldiv
# undef lldiv

// MSVCRT already has the correct prototype in <stdlib.h> if __cplusplus is defined
# if !defined(_LIBCPP_MSVCRT)
Expand Down