Skip to content

Commit 68f129f

Browse files
philnik777tomtor
authored andcommitted
[libc++] Refactor signed/unsigned integer traits (llvm#142750)
This patch does a few things: - `__libcpp_is_signed_integer` and `__libcpp_is_unsigned_integer` are refactored to be variable templates instead of class templates. - the two traits are merged into a single header `<__type_traits/integer_traits.h>`. - `__libcpp_signed_integer`, `__libcpp_unsigned_integer` and `__libcpp_integer` are moved into the same header. - The above mentioned concepts are renamed to `__signed_integer`, `__unsigned_integer` and `__signed_or_unsigned_integer` respectively.
1 parent 19ff168 commit 68f129f

22 files changed

+223
-245
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ set(files
800800
__type_traits/extent.h
801801
__type_traits/has_unique_object_representation.h
802802
__type_traits/has_virtual_destructor.h
803+
__type_traits/integer_traits.h
803804
__type_traits/integral_constant.h
804805
__type_traits/invoke.h
805806
__type_traits/is_abstract.h
@@ -850,7 +851,6 @@ set(files
850851
__type_traits/is_same.h
851852
__type_traits/is_scalar.h
852853
__type_traits/is_signed.h
853-
__type_traits/is_signed_integer.h
854854
__type_traits/is_specialization.h
855855
__type_traits/is_standard_layout.h
856856
__type_traits/is_swappable.h
@@ -864,7 +864,6 @@ set(files
864864
__type_traits/is_unbounded_array.h
865865
__type_traits/is_union.h
866866
__type_traits/is_unsigned.h
867-
__type_traits/is_unsigned_integer.h
868867
__type_traits/is_valid_expansion.h
869868
__type_traits/is_void.h
870869
__type_traits/is_volatile.h

libcxx/include/__bit/bit_ceil.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include <__assert>
1313
#include <__bit/countl.h>
14-
#include <__concepts/arithmetic.h>
1514
#include <__config>
15+
#include <__type_traits/integer_traits.h>
1616
#include <limits>
1717

1818
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -41,7 +41,7 @@ template <class _Tp>
4141

4242
# if _LIBCPP_STD_VER >= 20
4343

44-
template <__libcpp_unsigned_integer _Tp>
44+
template <__unsigned_integer _Tp>
4545
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
4646
return std::__bit_ceil(__t);
4747
}

libcxx/include/__bit/bit_floor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#define _LIBCPP___BIT_BIT_FLOOR_H
1111

1212
#include <__bit/bit_log2.h>
13-
#include <__concepts/arithmetic.h>
1413
#include <__config>
14+
#include <__type_traits/integer_traits.h>
1515

1616
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1717
# pragma GCC system_header
@@ -21,7 +21,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2121

2222
#if _LIBCPP_STD_VER >= 20
2323

24-
template <__libcpp_unsigned_integer _Tp>
24+
template <__unsigned_integer _Tp>
2525
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_floor(_Tp __t) noexcept {
2626
return __t == 0 ? 0 : _Tp{1} << std::__bit_log2(__t);
2727
}

libcxx/include/__bit/bit_log2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <__bit/countl.h>
1313
#include <__config>
14-
#include <__type_traits/is_unsigned_integer.h>
14+
#include <__type_traits/integer_traits.h>
1515
#include <limits>
1616

1717
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -22,7 +22,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2222

2323
template <class _Tp>
2424
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __bit_log2(_Tp __t) _NOEXCEPT {
25-
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
25+
static_assert(__is_unsigned_integer_v<_Tp>, "__bit_log2 requires an unsigned integer type");
2626
return numeric_limits<_Tp>::digits - 1 - std::__countl_zero(__t);
2727
}
2828

libcxx/include/__bit/bit_width.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#define _LIBCPP___BIT_BIT_WIDTH_H
1111

1212
#include <__bit/bit_log2.h>
13-
#include <__concepts/arithmetic.h>
1413
#include <__config>
14+
#include <__type_traits/integer_traits.h>
1515

1616
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1717
# pragma GCC system_header
@@ -21,7 +21,7 @@
2121

2222
_LIBCPP_BEGIN_NAMESPACE_STD
2323

24-
template <__libcpp_unsigned_integer _Tp>
24+
template <__unsigned_integer _Tp>
2525
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int bit_width(_Tp __t) noexcept {
2626
return __t == 0 ? 0 : std::__bit_log2(__t) + 1;
2727
}

libcxx/include/__bit/countl.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifndef _LIBCPP___BIT_COUNTL_H
1010
#define _LIBCPP___BIT_COUNTL_H
1111

12-
#include <__concepts/arithmetic.h>
1312
#include <__config>
14-
#include <__type_traits/is_unsigned_integer.h>
13+
#include <__type_traits/integer_traits.h>
1514
#include <limits>
1615

1716
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -25,18 +24,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2524

2625
template <class _Tp>
2726
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _NOEXCEPT {
28-
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
27+
static_assert(__is_unsigned_integer_v<_Tp>, "__countl_zero requires an unsigned integer type");
2928
return __builtin_clzg(__t, numeric_limits<_Tp>::digits);
3029
}
3130

3231
#if _LIBCPP_STD_VER >= 20
3332

34-
template <__libcpp_unsigned_integer _Tp>
33+
template <__unsigned_integer _Tp>
3534
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
3635
return std::__countl_zero(__t);
3736
}
3837

39-
template <__libcpp_unsigned_integer _Tp>
38+
template <__unsigned_integer _Tp>
4039
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
4140
return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
4241
}

libcxx/include/__bit/countr.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifndef _LIBCPP___BIT_COUNTR_H
1010
#define _LIBCPP___BIT_COUNTR_H
1111

12-
#include <__concepts/arithmetic.h>
1312
#include <__config>
14-
#include <__type_traits/is_unsigned_integer.h>
13+
#include <__type_traits/integer_traits.h>
1514
#include <limits>
1615

1716
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -25,18 +24,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2524

2625
template <class _Tp>
2726
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero(_Tp __t) _NOEXCEPT {
28-
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero only works with unsigned types");
27+
static_assert(__is_unsigned_integer_v<_Tp>, "__countr_zero only works with unsigned types");
2928
return __builtin_ctzg(__t, numeric_limits<_Tp>::digits);
3029
}
3130

3231
#if _LIBCPP_STD_VER >= 20
3332

34-
template <__libcpp_unsigned_integer _Tp>
33+
template <__unsigned_integer _Tp>
3534
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
3635
return std::__countr_zero(__t);
3736
}
3837

39-
template <__libcpp_unsigned_integer _Tp>
38+
template <__unsigned_integer _Tp>
4039
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
4140
return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
4241
}

libcxx/include/__bit/has_single_bit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifndef _LIBCPP___BIT_HAS_SINGLE_BIT_H
1010
#define _LIBCPP___BIT_HAS_SINGLE_BIT_H
1111

12-
#include <__concepts/arithmetic.h>
1312
#include <__config>
13+
#include <__type_traits/integer_traits.h>
1414

1515
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1616
# pragma GCC system_header
@@ -23,7 +23,7 @@ _LIBCPP_PUSH_MACROS
2323

2424
_LIBCPP_BEGIN_NAMESPACE_STD
2525

26-
template <__libcpp_unsigned_integer _Tp>
26+
template <__unsigned_integer _Tp>
2727
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
2828
return __t != 0 && (((__t & (__t - 1)) == 0));
2929
}

libcxx/include/__bit/popcount.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifndef _LIBCPP___BIT_POPCOUNT_H
1010
#define _LIBCPP___BIT_POPCOUNT_H
1111

12-
#include <__concepts/arithmetic.h>
1312
#include <__config>
14-
#include <__type_traits/is_unsigned_integer.h>
13+
#include <__type_traits/integer_traits.h>
1514

1615
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1716
# pragma GCC system_header
@@ -24,13 +23,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2423

2524
template <class _Tp>
2625
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT {
27-
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount only works with unsigned types");
26+
static_assert(__is_unsigned_integer_v<_Tp>, "__popcount only works with unsigned types");
2827
return __builtin_popcountg(__t);
2928
}
3029

3130
#if _LIBCPP_STD_VER >= 20
3231

33-
template <__libcpp_unsigned_integer _Tp>
32+
template <__unsigned_integer _Tp>
3433
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
3534
return std::__popcount(__t);
3635
}

libcxx/include/__bit/rotate.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifndef _LIBCPP___BIT_ROTATE_H
1010
#define _LIBCPP___BIT_ROTATE_H
1111

12-
#include <__concepts/arithmetic.h>
1312
#include <__config>
14-
#include <__type_traits/is_unsigned_integer.h>
13+
#include <__type_traits/integer_traits.h>
1514
#include <limits>
1615

1716
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -25,7 +24,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2524
// the rotr function becomes the ROR instruction.
2625
template <class _Tp>
2726
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s) _NOEXCEPT {
28-
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
27+
static_assert(__is_unsigned_integer_v<_Tp>, "__rotl requires an unsigned integer type");
2928
const int __n = numeric_limits<_Tp>::digits;
3029
int __r = __s % __n;
3130

@@ -40,7 +39,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __x, int __s)
4039

4140
template <class _Tp>
4241
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotr(_Tp __x, int __s) _NOEXCEPT {
43-
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
42+
static_assert(__is_unsigned_integer_v<_Tp>, "__rotr requires an unsigned integer type");
4443
const int __n = numeric_limits<_Tp>::digits;
4544
int __r = __s % __n;
4645

@@ -55,12 +54,12 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotr(_Tp __x, int __s)
5554

5655
#if _LIBCPP_STD_VER >= 20
5756

58-
template <__libcpp_unsigned_integer _Tp>
57+
template <__unsigned_integer _Tp>
5958
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
6059
return std::__rotl(__t, __cnt);
6160
}
6261

63-
template <__libcpp_unsigned_integer _Tp>
62+
template <__unsigned_integer _Tp>
6463
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, int __cnt) noexcept {
6564
return std::__rotr(__t, __cnt);
6665
}

libcxx/include/__concepts/arithmetic.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include <__type_traits/is_floating_point.h>
1414
#include <__type_traits/is_integral.h>
1515
#include <__type_traits/is_signed.h>
16-
#include <__type_traits/is_signed_integer.h>
17-
#include <__type_traits/is_unsigned_integer.h>
1816

1917
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2018
# pragma GCC system_header
@@ -38,17 +36,6 @@ concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>;
3836
template <class _Tp>
3937
concept floating_point = is_floating_point_v<_Tp>;
4038

41-
// Concept helpers for the internal type traits for the fundamental types.
42-
43-
template <class _Tp>
44-
concept __libcpp_unsigned_integer = __libcpp_is_unsigned_integer<_Tp>::value;
45-
46-
template <class _Tp>
47-
concept __libcpp_signed_integer = __libcpp_is_signed_integer<_Tp>::value;
48-
49-
template <class _Tp>
50-
concept __libcpp_integer = __libcpp_unsigned_integer<_Tp> || __libcpp_signed_integer<_Tp>;
51-
5239
#endif // _LIBCPP_STD_VER >= 20
5340

5441
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__format/format_arg_store.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
# pragma GCC system_header
1515
#endif
1616

17-
#include <__concepts/arithmetic.h>
1817
#include <__concepts/same_as.h>
1918
#include <__config>
2019
#include <__cstddef/size_t.h>
2120
#include <__format/concepts.h>
2221
#include <__format/format_arg.h>
2322
#include <__type_traits/conditional.h>
2423
#include <__type_traits/extent.h>
24+
#include <__type_traits/integer_traits.h>
2525
#include <__type_traits/remove_const.h>
2626
#include <cstdint>
2727
#include <string>
@@ -65,7 +65,7 @@ consteval __arg_t __determine_arg_t() {
6565
# endif
6666

6767
// Signed integers
68-
template <class, __libcpp_signed_integer _Tp>
68+
template <class, __signed_integer _Tp>
6969
consteval __arg_t __determine_arg_t() {
7070
if constexpr (sizeof(_Tp) <= sizeof(int))
7171
return __arg_t::__int;
@@ -80,7 +80,7 @@ consteval __arg_t __determine_arg_t() {
8080
}
8181

8282
// Unsigned integers
83-
template <class, __libcpp_unsigned_integer _Tp>
83+
template <class, __unsigned_integer _Tp>
8484
consteval __arg_t __determine_arg_t() {
8585
if constexpr (sizeof(_Tp) <= sizeof(unsigned))
8686
return __arg_t::__unsigned;

libcxx/include/__mdspan/extents.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
#include <__config>
2222

2323
#include <__concepts/arithmetic.h>
24-
#include <__cstddef/byte.h>
2524
#include <__type_traits/common_type.h>
25+
#include <__type_traits/integer_traits.h>
2626
#include <__type_traits/is_convertible.h>
2727
#include <__type_traits/is_nothrow_constructible.h>
28-
#include <__type_traits/is_same.h>
2928
#include <__type_traits/make_unsigned.h>
3029
#include <__utility/integer_sequence.h>
3130
#include <__utility/unreachable.h>
@@ -283,7 +282,8 @@ class extents {
283282
using size_type = make_unsigned_t<index_type>;
284283
using rank_type = size_t;
285284

286-
static_assert(__libcpp_integer<index_type>, "extents::index_type must be a signed or unsigned integer type");
285+
static_assert(__signed_or_unsigned_integer<index_type>,
286+
"extents::index_type must be a signed or unsigned integer type");
287287
static_assert(((__mdspan_detail::__is_representable_as<index_type>(_Extents) || (_Extents == dynamic_extent)) && ...),
288288
"extents ctor: arguments must be representable as index_type and nonnegative");
289289

0 commit comments

Comments
 (0)