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/apply_cv.h>
14 #include <__type_traits/conditional.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 typedef
35     __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 
49 template <class _Tp, bool = is_integral<_Tp>::value || is_enum<_Tp>::value>
50 struct __make_unsigned {};
51 
52 template <class _Tp>
53 struct __make_unsigned<_Tp, true>
54 {
55     typedef typename __find_first<__unsigned_types, sizeof(_Tp)>::type type;
56 };
57 
58 template <> struct __make_unsigned<bool,               true> {};
59 template <> struct __make_unsigned<  signed short,     true> {typedef unsigned short     type;};
60 template <> struct __make_unsigned<unsigned short,     true> {typedef unsigned short     type;};
61 template <> struct __make_unsigned<  signed int,       true> {typedef unsigned int       type;};
62 template <> struct __make_unsigned<unsigned int,       true> {typedef unsigned int       type;};
63 template <> struct __make_unsigned<  signed long,      true> {typedef unsigned long      type;};
64 template <> struct __make_unsigned<unsigned long,      true> {typedef unsigned long      type;};
65 template <> struct __make_unsigned<  signed long long, true> {typedef unsigned long long type;};
66 template <> struct __make_unsigned<unsigned long long, true> {typedef unsigned long long type;};
67 #  ifndef _LIBCPP_HAS_NO_INT128
68 template <> struct __make_unsigned<__int128_t,         true> {typedef __uint128_t        type;};
69 template <> struct __make_unsigned<__uint128_t,        true> {typedef __uint128_t        type;};
70 #  endif
71 
72 template <class _Tp>
73 using __make_unsigned_t = typename __apply_cv<_Tp, typename __make_unsigned<__remove_cv_t<_Tp> >::type>::type;
74 
75 #endif // __has_builtin(__make_unsigned)
76 
77 template <class _Tp>
78 struct make_unsigned {
79   using type _LIBCPP_NODEBUG = __make_unsigned_t<_Tp>;
80 };
81 
82 #if _LIBCPP_STD_VER > 11
83 template <class _Tp> using make_unsigned_t = __make_unsigned_t<_Tp>;
84 #endif
85 
86 #ifndef _LIBCPP_CXX03_LANG
87 template <class _Tp>
88 _LIBCPP_HIDE_FROM_ABI constexpr
89 __make_unsigned_t<_Tp> __to_unsigned_like(_Tp __x) noexcept {
90     return static_cast<__make_unsigned_t<_Tp> >(__x);
91 }
92 #endif
93 
94 template <class _Tp, class _Up>
95 using __copy_unsigned_t = __conditional_t<is_unsigned<_Tp>::value, __make_unsigned_t<_Up>, _Up>;
96 
97 _LIBCPP_END_NAMESPACE_STD
98 
99 #endif // _LIBCPP___TYPE_TRAITS_MAKE_UNSIGNED_H
100