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 typedef
29     __type_list<unsigned char,
30     __type_list<unsigned short,
31     __type_list<unsigned int,
32     __type_list<unsigned long,
33     __type_list<unsigned long long,
34 #ifndef _LIBCPP_HAS_NO_INT128
35     __type_list<__uint128_t,
36 #endif
37     __nat
38 #ifndef _LIBCPP_HAS_NO_INT128
39     >
40 #endif
41     > > > > > __unsigned_types;
42 
43 template <class _Tp, bool = is_integral<_Tp>::value || is_enum<_Tp>::value>
44 struct __make_unsigned {};
45 
46 template <class _Tp>
47 struct __make_unsigned<_Tp, true>
48 {
49     typedef typename __find_first<__unsigned_types, sizeof(_Tp)>::type type;
50 };
51 
52 template <> struct __make_unsigned<bool,               true> {};
53 template <> struct __make_unsigned<  signed short,     true> {typedef unsigned short     type;};
54 template <> struct __make_unsigned<unsigned short,     true> {typedef unsigned short     type;};
55 template <> struct __make_unsigned<  signed int,       true> {typedef unsigned int       type;};
56 template <> struct __make_unsigned<unsigned int,       true> {typedef unsigned int       type;};
57 template <> struct __make_unsigned<  signed long,      true> {typedef unsigned long      type;};
58 template <> struct __make_unsigned<unsigned long,      true> {typedef unsigned long      type;};
59 template <> struct __make_unsigned<  signed long long, true> {typedef unsigned long long type;};
60 template <> struct __make_unsigned<unsigned long long, true> {typedef unsigned long long type;};
61 #ifndef _LIBCPP_HAS_NO_INT128
62 template <> struct __make_unsigned<__int128_t,         true> {typedef __uint128_t        type;};
63 template <> struct __make_unsigned<__uint128_t,        true> {typedef __uint128_t        type;};
64 #endif
65 
66 template <class _Tp>
67 struct _LIBCPP_TEMPLATE_VIS make_unsigned
68 {
69     typedef typename __apply_cv<_Tp, typename __make_unsigned<typename remove_cv<_Tp>::type>::type>::type type;
70 };
71 
72 #if _LIBCPP_STD_VER > 11
73 template <class _Tp> using make_unsigned_t = typename make_unsigned<_Tp>::type;
74 #endif
75 
76 #ifndef _LIBCPP_CXX03_LANG
77 template <class _Tp>
78 _LIBCPP_HIDE_FROM_ABI constexpr
79 typename make_unsigned<_Tp>::type __to_unsigned_like(_Tp __x) noexcept {
80     return static_cast<typename make_unsigned<_Tp>::type>(__x);
81 }
82 #endif
83 
84 template <class _Tp, class _Up>
85 using __copy_unsigned_t = __conditional_t<is_unsigned<_Tp>::value, typename make_unsigned<_Up>::type, _Up>;
86 
87 _LIBCPP_END_NAMESPACE_STD
88 
89 #endif // _LIBCPP___TYPE_TRAITS_MAKE_UNSIGNED_H
90