Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:38

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___RANDOM_CLAMP_TO_INTEGRAL_H
0010 #define _LIBCPP___CXX03___RANDOM_CLAMP_TO_INTEGRAL_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/cmath>
0014 #include <__cxx03/limits>
0015 
0016 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0017 #  pragma GCC system_header
0018 #endif
0019 
0020 _LIBCPP_PUSH_MACROS
0021 #include <__cxx03/__undef_macros>
0022 
0023 _LIBCPP_BEGIN_NAMESPACE_STD
0024 
0025 template <class _IntT,
0026           class _FloatT,
0027           bool _FloatBigger = (numeric_limits<_FloatT>::digits > numeric_limits<_IntT>::digits),
0028           int _Bits         = (numeric_limits<_IntT>::digits - numeric_limits<_FloatT>::digits)>
0029 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _IntT __max_representable_int_for_float() _NOEXCEPT {
0030   static_assert(is_floating_point<_FloatT>::value, "must be a floating point type");
0031   static_assert(is_integral<_IntT>::value, "must be an integral type");
0032   static_assert(numeric_limits<_FloatT>::radix == 2, "FloatT has incorrect radix");
0033   static_assert(
0034       (_IsSame<_FloatT, float>::value || _IsSame<_FloatT, double>::value || _IsSame<_FloatT, long double>::value),
0035       "unsupported floating point type");
0036   return _FloatBigger ? numeric_limits<_IntT>::max() : (numeric_limits<_IntT>::max() >> _Bits << _Bits);
0037 }
0038 
0039 // Convert a floating point number to the specified integral type after
0040 // clamping to the integral type's representable range.
0041 //
0042 // The behavior is undefined if `__r` is NaN.
0043 template <class _IntT, class _RealT>
0044 _LIBCPP_HIDE_FROM_ABI _IntT __clamp_to_integral(_RealT __r) _NOEXCEPT {
0045   using _Lim            = numeric_limits<_IntT>;
0046   const _IntT __max_val = __max_representable_int_for_float<_IntT, _RealT>();
0047   if (__r >= ::nextafter(static_cast<_RealT>(__max_val), INFINITY)) {
0048     return _Lim::max();
0049   } else if (__r <= _Lim::lowest()) {
0050     return _Lim::min();
0051   }
0052   return static_cast<_IntT>(__r);
0053 }
0054 
0055 _LIBCPP_END_NAMESPACE_STD
0056 
0057 _LIBCPP_POP_MACROS
0058 
0059 #endif // _LIBCPP___CXX03___RANDOM_CLAMP_TO_INTEGRAL_H