|
| 1 | +// SPDX-FileCopyrightText: 2025 The Ginkgo authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +#include <complex> |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +#include <ginkgo/core/base/math.hpp> |
| 10 | + |
| 11 | +#include "core/test/utils.hpp" |
| 12 | + |
| 13 | + |
| 14 | +TEST(HighestPrecision, SamePrecisionsGivesSame) |
| 15 | +{ |
| 16 | + ::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, gko::half>, |
| 17 | + gko::half>(); |
| 18 | + ::testing::StaticAssertTypeEq< |
| 19 | + gko::highest_precision<gko::bfloat16, gko::bfloat16>, gko::bfloat16>(); |
| 20 | + ::testing::StaticAssertTypeEq<gko::highest_precision<float, float>, |
| 21 | + float>(); |
| 22 | + ::testing::StaticAssertTypeEq<gko::highest_precision<double, double>, |
| 23 | + double>(); |
| 24 | +} |
| 25 | + |
| 26 | +TEST(HighestPrecision, Different16bitGivesFloat) |
| 27 | +{ |
| 28 | + ::testing::StaticAssertTypeEq< |
| 29 | + gko::highest_precision<gko::half, gko::bfloat16>, float>(); |
| 30 | + ::testing::StaticAssertTypeEq< |
| 31 | + gko::highest_precision<gko::bfloat16, gko::half>, float>(); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(HighestPrecision, HalfCombinations) |
| 35 | +{ |
| 36 | + // two same precisions give the same precision |
| 37 | + ::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, gko::half>, |
| 38 | + gko::half>(); |
| 39 | + // different 16 bit precisions give float |
| 40 | + ::testing::StaticAssertTypeEq< |
| 41 | + gko::highest_precision<gko::half, gko::bfloat16>, float>(); |
| 42 | + ::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, float>, |
| 43 | + float>(); |
| 44 | + ::testing::StaticAssertTypeEq<gko::highest_precision<gko::half, double>, |
| 45 | + double>(); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +TEST(HighestPrecision, Bfloat16Combinations) |
| 50 | +{ |
| 51 | + // different 16 bit precisions give float |
| 52 | + ::testing::StaticAssertTypeEq< |
| 53 | + gko::highest_precision<gko::bfloat16, gko::half>, float>(); |
| 54 | + // two same precisions give the same precision |
| 55 | + ::testing::StaticAssertTypeEq< |
| 56 | + gko::highest_precision<gko::bfloat16, gko::bfloat16>, gko::bfloat16>(); |
| 57 | + ::testing::StaticAssertTypeEq<gko::highest_precision<gko::bfloat16, float>, |
| 58 | + float>(); |
| 59 | + ::testing::StaticAssertTypeEq<gko::highest_precision<gko::bfloat16, double>, |
| 60 | + double>(); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +TEST(HighestPrecision, FloatCombinations) |
| 65 | +{ |
| 66 | + ::testing::StaticAssertTypeEq<gko::highest_precision<float, gko::half>, |
| 67 | + float>(); |
| 68 | + ::testing::StaticAssertTypeEq<gko::highest_precision<float, gko::bfloat16>, |
| 69 | + float>(); |
| 70 | + // two same precisions give the same precision |
| 71 | + ::testing::StaticAssertTypeEq<gko::highest_precision<float, float>, |
| 72 | + float>(); |
| 73 | + ::testing::StaticAssertTypeEq<gko::highest_precision<float, double>, |
| 74 | + double>(); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +TEST(HighestPrecision, DoubleCombinations) |
| 79 | +{ |
| 80 | + ::testing::StaticAssertTypeEq<gko::highest_precision<double, gko::half>, |
| 81 | + double>(); |
| 82 | + ::testing::StaticAssertTypeEq<gko::highest_precision<double, gko::bfloat16>, |
| 83 | + double>(); |
| 84 | + ::testing::StaticAssertTypeEq<gko::highest_precision<double, float>, |
| 85 | + double>(); |
| 86 | + // two same precisions give the same precision |
| 87 | + ::testing::StaticAssertTypeEq<gko::highest_precision<double, double>, |
| 88 | + double>(); |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +template <typename TwoRealType> |
| 93 | +class ComplexHigestPrecision : public ::testing::Test { |
| 94 | +public: |
| 95 | + using first_type = |
| 96 | + typename std::tuple_element<0, decltype(TwoRealType())>::type; |
| 97 | + using second_type = |
| 98 | + typename std::tuple_element<0, decltype(TwoRealType())>::type; |
| 99 | +}; |
| 100 | + |
| 101 | +using TwoRealType = |
| 102 | + gko::test::cartesian_type_product_t<gko::test::RealValueTypes, |
| 103 | + gko::test::RealValueTypes>; |
| 104 | + |
| 105 | +TYPED_TEST_SUITE(ComplexHigestPrecision, TwoRealType, |
| 106 | + PairTypenameNameGenerator); |
| 107 | + |
| 108 | + |
| 109 | +TYPED_TEST(ComplexHigestPrecision, ComplexBasedOnReal) |
| 110 | +{ |
| 111 | + using first_type = typename TestFixture::first_type; |
| 112 | + using second_type = typename TestFixture::second_type; |
| 113 | + ::testing::StaticAssertTypeEq< |
| 114 | + gko::highest_precision<std::complex<first_type>, |
| 115 | + std::complex<second_type>>, |
| 116 | + std::complex<gko::highest_precision<first_type, second_type>>>(); |
| 117 | +} |
0 commit comments