xref: /freebsd/contrib/llvm-project/libcxx/include/__utility/convert_to_integral.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric 
9bdd1243dSDimitry Andric #ifndef _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H
10bdd1243dSDimitry Andric #define _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H
11bdd1243dSDimitry Andric 
12bdd1243dSDimitry Andric #include <__config>
13bdd1243dSDimitry Andric #include <__type_traits/enable_if.h>
14bdd1243dSDimitry Andric #include <__type_traits/is_enum.h>
15bdd1243dSDimitry Andric #include <__type_traits/is_floating_point.h>
16bdd1243dSDimitry Andric #include <__type_traits/underlying_type.h>
17bdd1243dSDimitry Andric 
18bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19bdd1243dSDimitry Andric #  pragma GCC system_header
20bdd1243dSDimitry Andric #endif
21bdd1243dSDimitry Andric 
22bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
23bdd1243dSDimitry Andric 
24*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __convert_to_integral(int __val) { return __val; }
25bdd1243dSDimitry Andric 
26*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unsigned __convert_to_integral(unsigned __val) { return __val; }
27bdd1243dSDimitry Andric 
28*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long __convert_to_integral(long __val) { return __val; }
29bdd1243dSDimitry Andric 
30*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unsigned long __convert_to_integral(unsigned long __val) {
31*cb14a3feSDimitry Andric   return __val;
32*cb14a3feSDimitry Andric }
33bdd1243dSDimitry Andric 
34*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long long __convert_to_integral(long long __val) { return __val; }
35bdd1243dSDimitry Andric 
36*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unsigned long long __convert_to_integral(unsigned long long __val) {
37*cb14a3feSDimitry Andric   return __val;
38*cb14a3feSDimitry Andric }
39bdd1243dSDimitry Andric 
405f757f3fSDimitry Andric template <typename _Fp, __enable_if_t<is_floating_point<_Fp>::value, int> = 0>
41*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long long __convert_to_integral(_Fp __val) {
42*cb14a3feSDimitry Andric   return __val;
43*cb14a3feSDimitry Andric }
44bdd1243dSDimitry Andric 
45bdd1243dSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
46*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __int128_t __convert_to_integral(__int128_t __val) { return __val; }
47bdd1243dSDimitry Andric 
48*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __uint128_t __convert_to_integral(__uint128_t __val) { return __val; }
49bdd1243dSDimitry Andric #endif
50bdd1243dSDimitry Andric 
51bdd1243dSDimitry Andric template <class _Tp, bool = is_enum<_Tp>::value>
52*cb14a3feSDimitry Andric struct __sfinae_underlying_type {
53bdd1243dSDimitry Andric   typedef typename underlying_type<_Tp>::type type;
54bdd1243dSDimitry Andric   typedef decltype(((type)1) + 0) __promoted_type;
55bdd1243dSDimitry Andric };
56bdd1243dSDimitry Andric 
57bdd1243dSDimitry Andric template <class _Tp>
58bdd1243dSDimitry Andric struct __sfinae_underlying_type<_Tp, false> {};
59bdd1243dSDimitry Andric 
60bdd1243dSDimitry Andric template <class _Tp>
61*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename __sfinae_underlying_type<_Tp>::__promoted_type
62*cb14a3feSDimitry Andric __convert_to_integral(_Tp __val) {
63*cb14a3feSDimitry Andric   return __val;
64*cb14a3feSDimitry Andric }
65bdd1243dSDimitry Andric 
66bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
67bdd1243dSDimitry Andric 
68bdd1243dSDimitry Andric #endif // _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H
69