min_max.cpp (1945B)
1 /** 2 * KFR (https://www.kfrlib.com) 3 * Copyright (C) 2016-2023 Dan Cazarin 4 * See LICENSE.txt for details 5 */ 6 7 #include <kfr/simd/min_max.hpp> 8 9 #include <kfr/io.hpp> 10 11 CMT_PRAGMA_MSVC(warning(push)) 12 CMT_PRAGMA_MSVC(warning(disable : 4146)) 13 14 namespace kfr 15 { 16 inline namespace CMT_ARCH_NAME 17 { 18 TEST(min) 19 { 20 test_function2( 21 test_catogories::all, [](auto x, auto y) { return kfr::min(x, y); }, 22 [](auto x, auto y) -> std::common_type_t<decltype(x), decltype(y)> { return x <= y ? x : y; }); 23 } 24 25 TEST(max) 26 { 27 test_function2( 28 test_catogories::all, [](auto x, auto y) { return kfr::max(x, y); }, 29 [](auto x, auto y) -> std::common_type_t<decltype(x), decltype(y)> { return x >= y ? x : y; }); 30 } 31 32 struct IsNotMinInt 33 { 34 template <typename T> 35 bool operator()(ctype_t<T>, identity<T> x, identity<T> y) const 36 { 37 return std::is_floating_point_v<T> || std::is_unsigned_v<T> || 38 (x != std::numeric_limits<T>::min() && y != std::numeric_limits<T>::min()); 39 } 40 template <typename T, size_t N> 41 bool operator()(ctype_t<vec<T, N>>, identity<T> x, identity<T> y) const 42 { 43 return operator()(cometa::ctype<T>, x, y); 44 } 45 }; 46 47 TEST(absmin) 48 { 49 test_function2( 50 test_catogories::all, [](auto x, auto y) { return kfr::absmin(x, y); }, 51 [](auto x, auto y) -> std::common_type_t<decltype(x), decltype(y)> 52 { 53 x = x >= 0 ? x : -x; 54 y = y >= 0 ? y : -y; 55 return x <= y ? x : y; 56 }, 57 IsNotMinInt{}); 58 } 59 60 TEST(absmax) 61 { 62 test_function2( 63 test_catogories::all, [](auto x, auto y) { return kfr::absmax(x, y); }, 64 [](auto x, auto y) -> std::common_type_t<decltype(x), decltype(y)> 65 { 66 x = x >= 0 ? x : -x; 67 y = y >= 0 ? y : -y; 68 return x >= y ? x : y; 69 }, 70 IsNotMinInt{}); 71 } 72 } // namespace CMT_ARCH_NAME 73 } // namespace kfr 74 75 CMT_PRAGMA_MSVC(warning(pop))