[libcxx-commits] [PATCH] D75960: [libc++] Implement C++20's P0476r2: std::bit_cast
Arthur O'Dwyer via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Sep 9 08:11:44 PDT 2021
Quuxplusone added inline comments.
================
Comment at: libcxx/test/std/numerics/bit/bit.cast/bit_cast.pass.cpp:253
+int main(int, char**) {
+ tests();
+ static_assert(basic_constexpr_test());
----------------
ldionne wrote:
> jfb wrote:
> > Quuxplusone wrote:
> > > I guess I meant to try to constexpr-ify `tests()` but never succeeded. The problems were (I think) with `std::nanl` and `std::memcmp`. I think it would be fine to ship this with only the `basic_constexpr_test`; although you might also try putting the `memcmp`s under `if (!std::is_constant_evaluated())` and splitting out the floating-point `nan` tests into a non-constexpr special test.
> > `nan` will be `constexpr` if you do `bool my_is_nan(double value) { return value != value; }`.
> As Arthur says, the issue here is really to get a NaN value in constexpr context, which I don't know how to do. The other issue, which is more fundamental, is that we can't use `std::memcmp` in a constexpr context. And we can't use `__builtin_memcmp`, because although it *is* constexpr, it can't be used to compare different types, and we can't `reinterpret_cast` in a constexpr context.
Somewhat circular, but you can define a constexpr-evaluable memcmp in terms of `bit_cast`. :)
https://godbolt.org/z/49dhW4jzz
The only trick is that it can't take `void*, void*, size_t`; it must take `T*, T*`, because even `bit_cast` doesn't let you cast `void*` back to `SomeOtherType*` in a constexpr context.
>From my perspective, the lack of memcmp isn't so fundamental, because we can just `if` out those tests when it's constexpr time. The more awkward programming problem is lines 220-225, where we need to generate an array with a couple different NaNs if it's non-constexpr, but somehow //omit// generating those array elements when it's constexpr time.
See my solution at D107036 (search for the phrase "specifically positive or negative NAN").
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D75960/new/
https://reviews.llvm.org/D75960
More information about the libcxx-commits
mailing list