Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update isfinite declaration to be constexpr for c++23 only #3746

Closed
wants to merge 1 commit into from

Conversation

hchataing
Copy link

Fix #3745

@hchataing hchataing force-pushed the constexpr-isfinite branch 2 times, most recently from f009126 to 04338ce Compare December 9, 2023 01:14
@phprus
Copy link
Contributor

phprus commented Dec 9, 2023

Question about the environment:
#3745 (comment)

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

@phprus made a good point and it looks like a bug in is_constant_evaluated and not isfinite.

@hchataing
Copy link
Author

Granted, I might have been too quick in diagnosing the issue

The implementation failed to be constexpr in the situation where:
  __cplusplus == 202002L && !defined(__cpp_lib_is_constant_evaluated)

Fix fmtlib#3745
@@ -2752,7 +2752,7 @@ template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value&&
has_isfinite<T>::value)>
FMT_CONSTEXPR20 bool isfinite(T value) {
constexpr T inf = T(std::numeric_limits<double>::infinity());
if (is_constant_evaluated())
if (is_constant_evaluated(true))
Copy link
Contributor

Choose a reason for hiding this comment

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

One drawback is that it switches to a slightly worse implementation on older systems: https://www.godbolt.org/z/WWzjEsrxj. But I guess we can optimize it later.

Copy link
Author

Choose a reason for hiding this comment

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

I don't see an alternative other than testing FMT_CONSTEXPR20 in the body to replace is_constant_evaluated :(

Copy link
Contributor

Choose a reason for hiding this comment

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

We can use __builtin_is_constant_evaluated instead of std::is_constant_evaluated.

Copy link
Contributor

@phprus phprus Dec 13, 2023

Choose a reason for hiding this comment

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

Or we can improve

fmt/include/fmt/core.h

Lines 108 to 110 in 89860eb

#if ((FMT_CPLUSPLUS >= 202002L) && \
(!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE > 9)) || \
(FMT_CPLUSPLUS >= 201709L && FMT_GCC_VERSION >= 1002)

condition by adding a stdlib version check.

@hchataing, which version of libc++ (_LIBCPP_VERSION) or libstdc++ (_GLIBCXX_RELEASE) you are using?

Copy link
Contributor

Choose a reason for hiding this comment

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

@hchataing check PR #3754 please

Copy link
Author

Choose a reason for hiding this comment

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

Neither macro is defined by the compiler... but maybe I am checking wrong (clang++ -std=c++20 -E -dM foo.cc)

@vitaut
Copy link
Contributor

vitaut commented Dec 13, 2023

Closing in favor of #3754 but thanks for the effort.

@vitaut vitaut closed this Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

isfinite declared constexpr for c++20
3 participants