1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <algorithm> 11 12 // __half_positive divides an integer number by 2 as unsigned number for known types. 13 // It can be an important optimization for lower bound, for example. 14 15 #include <algorithm> 16 #include <cassert> 17 #include <limits> 18 #include <type_traits> 19 20 #include "test_macros.h" 21 #include "user_defined_integral.hpp" 22 23 namespace { 24 25 template <class IntType, class UnderlyingType = IntType> test(IntType max_v=IntType (std::numeric_limits<UnderlyingType>::max ()))26TEST_CONSTEXPR bool test(IntType max_v = IntType(std::numeric_limits<UnderlyingType>::max())) { 27 return std::__half_positive(max_v) == max_v / 2; 28 } 29 30 } // namespace 31 main()32int main() 33 { 34 { 35 assert(test<char>()); 36 assert(test<int>()); 37 assert(test<long>()); 38 assert((test<UserDefinedIntegral<int>, int>())); 39 assert(test<size_t>()); 40 #if !defined(_LIBCPP_HAS_NO_INT128) 41 assert(test<__int128_t>()); 42 #endif // !defined(_LIBCPP_HAS_NO_INT128) 43 } 44 45 #if TEST_STD_VER >= 11 46 { 47 static_assert(test<char>(), ""); 48 static_assert(test<int>(), ""); 49 static_assert(test<long>(), ""); 50 static_assert(test<size_t>(), ""); 51 #if !defined(_LIBCPP_HAS_NO_INT128) 52 static_assert(test<__int128_t>(), ""); 53 #endif // !defined(_LIBCPP_HAS_NO_INT128) 54 } 55 #endif // TEST_STD_VER >= 11 56 } 57