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
From a23cd822ebd333e4c4f3155848cc302e155944e9 Mon Sep 17 00:00:00 2001
From: Paul <{ID}+{username}@users.noreply.github.com>
Date: Sun, 11 Aug 2024 14:13:09 +0200
Subject: [PATCH] constexpr std::div; tests

---
 libstdc++-v3/include/c_global/cstdlib         | 94 +++++++++++++------
 .../headers/cstdlib/constexpr_div.cc          | 78 +++++++++++++++
 2 files changed, 141 insertions(+), 31 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/26_numerics/headers/cstdlib/constexpr_div.cc

diff --git a/libstdc++-v3/include/c_global/cstdlib b/libstdc++-v3/include/c_global/cstdlib
index 69e7a39eb44..dd2e2738b06 100644
--- a/libstdc++-v3/include/c_global/cstdlib
+++ b/libstdc++-v3/include/c_global/cstdlib
@@ -96,12 +96,10 @@ namespace std
 #undef atol
 #undef bsearch
 #undef calloc
-#undef div
 #undef exit
 #undef free
 #undef getenv
 #undef labs
-#undef ldiv
 #undef malloc
 #undef mblen
 #undef mbstowcs
@@ -127,10 +125,6 @@ extern "C++"
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
-
-  using ::div_t;
-  using ::ldiv_t;
-
   using ::abort;
 #if __cplusplus >= 201703L && defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)
   using ::aligned_alloc;
@@ -146,12 +140,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   using ::atol;
   using ::bsearch;
   using ::calloc;
-  using ::div;
   using ::exit;
   using ::free;
   using ::getenv;
   using ::labs;
-  using ::ldiv;
   using ::malloc;
 #ifdef _GLIBCXX_HAVE_MBSTATE_T
   using ::mblen;
@@ -175,13 +167,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   using ::wcstombs;
   using ::wctomb;
 #endif // _GLIBCXX_USE_WCHAR_T
-
-#ifndef __CORRECT_ISO_CPP_STDLIB_H_PROTO
-  inline ldiv_t
-  div(long __i, long __j) _GLIBCXX_NOTHROW { return ldiv(__i, __j); }
-#endif
-
-
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
 
@@ -189,7 +174,6 @@ _GLIBCXX_END_NAMESPACE_VERSION
 
 #undef _Exit
 #undef llabs
-#undef lldiv
 #undef atoll
 #undef strtoll
 #undef strtoull
@@ -199,10 +183,6 @@ _GLIBCXX_END_NAMESPACE_VERSION
 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
-
-#if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
-  using ::lldiv_t;
-#endif
 #if _GLIBCXX_USE_C99_CHECK || _GLIBCXX_USE_C99_DYNAMIC
   extern "C" void (_Exit)(int) _GLIBCXX_NOTHROW _GLIBCXX_NORETURN;
 #endif
@@ -212,12 +192,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
   using ::llabs;
-
-  inline lldiv_t
-  div(long long __n, long long __d)
-  { lldiv_t __q; __q.quot = __n / __d; __q.rem = __n % __d; return __q; }
-
-  using ::lldiv;
 #endif
 
 #if _GLIBCXX_USE_C99_LONG_LONG_CHECK || _GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
@@ -240,14 +214,9 @@ _GLIBCXX_END_NAMESPACE_VERSION
 
 namespace std
 {
-#if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
-  using ::__gnu_cxx::lldiv_t;
-#endif
   using ::__gnu_cxx::_Exit;
 #if !_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC
   using ::__gnu_cxx::llabs;
-  using ::__gnu_cxx::div;
-  using ::__gnu_cxx::lldiv;
 #endif
   using ::__gnu_cxx::atoll;
   using ::__gnu_cxx::strtof;
@@ -276,4 +245,67 @@ namespace std { using ::strtold; }
 
 #endif // !_GLIBCXX_HOSTED
 
+#if _GLIBCXX_HOSTED
+
+#undef div
+#undef ldiv
+
+extern "C++"
+{
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+  using ::div_t;
+  using ::ldiv_t;
+#if __cplusplus >= 201103L
+  struct lldiv_t
+  {
+    long long int quot;		/* Quotient.  */
+    long long int rem;		/* Remainder.  */
+  };
+#endif
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
+} // extern "C++"
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  template <class _Div_t, class _Int>
+  _GLIBCXX23_CONSTEXPR _Div_t __div(_Int __n, _Int __d) _GLIBCXX_NOTHROW
+  {
+    _Div_t __q;
+    __q.quot = __n / __d;
+    __q.rem = __n % __d;
+    return __q;
+  }
+
+  inline _GLIBCXX23_CONSTEXPR
+  div_t div(int __n, int __d) _GLIBCXX_NOTHROW
+  { return __div<div_t>(__n, __d); }
+
+  inline _GLIBCXX23_CONSTEXPR
+  ldiv_t div(long int __n, long int __d) _GLIBCXX_NOTHROW
+  { return __div<ldiv_t>(__n, __d); }
+
+  inline _GLIBCXX23_CONSTEXPR
+  ldiv_t ldiv(long int __n, long int __d) _GLIBCXX_NOTHROW
+  { return std::div(__n, __d); }
+
+#if __cplusplus >= 201103L
+  inline _GLIBCXX23_CONSTEXPR
+  lldiv_t div(long long int __n, long long int __d) _GLIBCXX_NOTHROW
+  { return __div<lldiv_t>(__n, __d); }
+
+  inline _GLIBCXX23_CONSTEXPR
+  lldiv_t lldiv(long long int __n, long long int __d) _GLIBCXX_NOTHROW
+  { return std::div(__n, __d); }
 #endif
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace std
+
+#endif // _GLIBCXX_HOSTED
+
+#endif // _GLIBCXX_CSTDLIB
diff --git a/libstdc++-v3/testsuite/26_numerics/headers/cstdlib/constexpr_div.cc b/libstdc++-v3/testsuite/26_numerics/headers/cstdlib/constexpr_div.cc
new file mode 100644
index 00000000000..2bbfc6d40bb
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/headers/cstdlib/constexpr_div.cc
@@ -0,0 +1,78 @@
+// Copyright (C) 2024-2024 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// Math-related cstdlib bits are not freestanding.
+// { dg-require-effective-target hosted }
+
+// 17.2.2 Headers, cstdlib
+
+#include <cstdlib>
+#include <testsuite_hooks.h>
+
+template <class Callable>
+void check()
+{
+  typedef typename Callable::Int Int;
+  typedef typename Callable::Div_t Div_t;
+
+  _GLIBCXX_CONSTEXPR Int n = 42;
+  _GLIBCXX_CONSTEXPR Int d = 5;
+  _GLIBCXX_CONSTEXPR Int quot = 8;
+  _GLIBCXX_CONSTEXPR Int rem = 2;
+
+  const Div_t a = Callable()(n, d);
+  VERIFY(a.quot == quot);
+  VERIFY(a.rem == rem);
+
+#if __cplusplus >= 202302L
+  constexpr Div_t b = std::div(n, d);
+  static_assert(b.quot == quot);
+  static_assert(b.rem == rem);
+#endif
+}
+
+template <class Int_, class Div_t_>
+struct Call_div {
+  typedef Int_ Int;
+  typedef Div_t_ Div_t;
+  _GLIBCXX_CONSTEXPR Div_t operator()(Int n, Int d) const { return std::div(n, d); }
+};
+
+struct Call_ldiv {
+  typedef long Int;
+  typedef std::ldiv_t Div_t;
+  _GLIBCXX_CONSTEXPR Div_t operator()(Int n, Int d) const { return std::ldiv(n, d); }
+};
+
+#if __cplusplus >= 201103L
+struct Call_lldiv {
+  typedef long long Int;
+  typedef std::lldiv_t Div_t;
+  _GLIBCXX_CONSTEXPR Div_t operator()(Int n, Int d) const { return std::lldiv(n, d); }
+};
+#endif
+
+int main()
+{
+  check<Call_div<int, std::div_t> >();
+  check<Call_div<long, std::ldiv_t> >();
+  check<Call_ldiv>();
+#if __cplusplus >= 201103L
+  check<Call_div<long long, std::lldiv_t> >();
+  check<Call_lldiv>();
+#endif
+}
-- 
2.46.0

Reply via email to