c++, libstdc++: (Partially) Implement C++23 P0533: constexpr std::div()

PaulXiCao paulxicao7@gmail.com
Sun Aug 11 18:50:17 GMT 2024


Hi!

This patch makes std::div() constexpr. It is part of C++23, paper P0533:
https://wg21.link/p0533 .

The previous implementation dispatched to the libc's implementation (which
is not constexpr). I reimplemented it to be uniform across all standards
(e.g. fewer ifdefs). The code has also been formatted s.t. all
implementation details for
std::div() are closer together in code (i.e. appended to the file in one
block).

Development branch can be viewed here:
https://github.com/PaulXiCao/gcc/tree/constexprCmath_div .

# Description
P0533 was included in C++23 and mandates multiple math functions to be
constexpr. This patch changes std::div(), std::ldiv(), and std::lldiv() to
be constexpr.

# Changelog
libstdc++-v3/ChangeLog:

* include/c_global/cstdlib: Implements std::div() as constexpr.
* testsuite/26_numerics/headers/cstdlib/constexpr_div.cc: Tests run- and
compile-time results of std::div().

# Testcases
A test file checking the basic functionality as well as constexpr for C++23
has been added (constexpr_div.cc).

# Patch
(Appended to mail.)

# Mail thread history
The initial mail was:
https://gcc.gnu.org/pipermail/libstdc++/2024-August/059367.html .

It contained a question about the interoperability with C through stdlib.h
but was answered through irc. There was also a typo in the paper number
(P0553 instead of P0533) which is why I started a new mail thread.

The implementation changed in the following ways:

* Include _GLIBCXX_NOTHROW function attribute.
* Hide long long int variants behind the C++11 standard macro.

Cheers
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20240811/84f95b5e/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-constexpr-std-div-tests.patch
Type: text/x-patch
Size: 7175 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20240811/84f95b5e/attachment-0001.bin>


More information about the Libstdc++ mailing list