xref: /freebsd/contrib/llvm-project/libcxx/include/__type_traits/make_unsigned.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___TYPE_TRAITS_MAKE_UNSIGNED_H
10 #define _LIBCPP___TYPE_TRAITS_MAKE_UNSIGNED_H
11 
12 #include <__config>
13 #include <__type_traits/conditional.h>
14 #include <__type_traits/copy_cv.h>
15 #include <__type_traits/is_enum.h>
16 #include <__type_traits/is_integral.h>
17 #include <__type_traits/is_unsigned.h>
18 #include <__type_traits/nat.h>
19 #include <__type_traits/remove_cv.h>
20 #include <__type_traits/type_list.h>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_BEGIN_NAMESPACE_STD
27 
28 #if __has_builtin(__make_unsigned)
29 
30 template <class _Tp>
31 using __make_unsigned_t = __make_unsigned(_Tp);
32 
33 #else
34 // clang-format off
35 typedef __type_list<unsigned char,
36         __type_list<unsigned short,
37         __type_list<unsigned int,
38         __type_list<unsigned long,
39         __type_list<unsigned long long,
40 #  ifndef _LIBCPP_HAS_NO_INT128
41         __type_list<__uint128_t,
42 #  endif
43         __nat
44 #  ifndef _LIBCPP_HAS_NO_INT128
45         >
46 #  endif
47         > > > > > __unsigned_types;
48 // clang-format on
49 
50 template <class _Tp, bool = is_integral<_Tp>::value || is_enum<_Tp>::value>
51 struct __make_unsigned{};
52 
53 template <class _Tp>
54 struct __make_unsigned<_Tp, true> {
55   typedef typename __find_first<__unsigned_types, sizeof(_Tp)>::type type;
56 };
57 
58 // clang-format off
59 template <> struct __make_unsigned<bool,               true> {};
60 template <> struct __make_unsigned<  signed short,     true> {typedef unsigned short     type;};
61 template <> struct __make_unsigned<unsigned short,     true> {typedef unsigned short     type;};
62 template <> struct __make_unsigned<  signed int,       true> {typedef unsigned int       type;};
63 template <> struct __make_unsigned<unsigned int,       true> {typedef unsigned int       type;};
64 template <> struct __make_unsigned<  signed long,      true> {typedef unsigned long      type;};
65 template <> struct __make_unsigned<unsigned long,      true> {typedef unsigned long      type;};
66 template <> struct __make_unsigned<  signed long long, true> {typedef unsigned long long type;};
67 template <> struct __make_unsigned<unsigned long long, true> {typedef unsigned long long type;};
68 #  ifndef _LIBCPP_HAS_NO_INT128
69 template <> struct __make_unsigned<__int128_t,         true> {typedef __uint128_t        type;};
70 template <> struct __make_unsigned<__uint128_t,        true> {typedef __uint128_t        type;};
71 #  endif
72 // clang-format on
73 
74 template <class _Tp>
75 using __make_unsigned_t = __copy_cv_t<_Tp, typename __make_unsigned<__remove_cv_t<_Tp> >::type>;
76 
77 #endif // __has_builtin(__make_unsigned)
78 
79 template <class _Tp>
80 struct make_unsigned {
81   using type _LIBCPP_NODEBUG = __make_unsigned_t<_Tp>;
82 };
83 
84 #if _LIBCPP_STD_VER >= 14
85 template <class _Tp>
86 using make_unsigned_t = __make_unsigned_t<_Tp>;
87 #endif
88 
89 #ifndef _LIBCPP_CXX03_LANG
90 template <class _Tp>
__to_unsigned_like(_Tp __x)91 _LIBCPP_HIDE_FROM_ABI constexpr __make_unsigned_t<_Tp> __to_unsigned_like(_Tp __x) noexcept {
92   return static_cast<__make_unsigned_t<_Tp> >(__x);
93 }
94 #endif
95 
96 template <class _Tp, class _Up>
97 using __copy_unsigned_t = __conditional_t<is_unsigned<_Tp>::value, __make_unsigned_t<_Up>, _Up>;
98 
99 _LIBCPP_END_NAMESPACE_STD
100 
101 #endif // _LIBCPP___TYPE_TRAITS_MAKE_UNSIGNED_H
102