convert_to_integral.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H
  9. #define _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H
  10. #include <__config>
  11. #include <__type_traits/enable_if.h>
  12. #include <__type_traits/is_enum.h>
  13. #include <__type_traits/is_floating_point.h>
  14. #include <__type_traits/underlying_type.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  20. int __convert_to_integral(int __val) { return __val; }
  21. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  22. unsigned __convert_to_integral(unsigned __val) { return __val; }
  23. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  24. long __convert_to_integral(long __val) { return __val; }
  25. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  26. unsigned long __convert_to_integral(unsigned long __val) { return __val; }
  27. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  28. long long __convert_to_integral(long long __val) { return __val; }
  29. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  30. unsigned long long __convert_to_integral(unsigned long long __val) {return __val; }
  31. template<typename _Fp>
  32. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  33. typename enable_if<is_floating_point<_Fp>::value, long long>::type
  34. __convert_to_integral(_Fp __val) { return __val; }
  35. #ifndef _LIBCPP_HAS_NO_INT128
  36. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  37. __int128_t __convert_to_integral(__int128_t __val) { return __val; }
  38. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  39. __uint128_t __convert_to_integral(__uint128_t __val) { return __val; }
  40. #endif
  41. template <class _Tp, bool = is_enum<_Tp>::value>
  42. struct __sfinae_underlying_type
  43. {
  44. typedef typename underlying_type<_Tp>::type type;
  45. typedef decltype(((type)1) + 0) __promoted_type;
  46. };
  47. template <class _Tp>
  48. struct __sfinae_underlying_type<_Tp, false> {};
  49. template <class _Tp>
  50. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  51. typename __sfinae_underlying_type<_Tp>::__promoted_type
  52. __convert_to_integral(_Tp __val) { return __val; }
  53. _LIBCPP_END_NAMESPACE_STD
  54. #endif // _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H