float_equal.cpp 3.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <migraphx/float_equal.hpp>
#include <migraphx/half.hpp>
#include "test.hpp"

#include <limits>

template <class T, class U>
struct float_equal_expression
{
    T lhs;
    U rhs;

    operator bool() const { return migraphx::float_equal(lhs, rhs); }

    bool operator not() const { return not bool(*this); }

    friend std::ostream& operator<<(std::ostream& s, const float_equal_expression& self)
    {
        s << "migraphx::float_equal(" << self.lhs << ", " << self.rhs << ")";
        return s;
    }
};

template <class T, class U>
auto test_float_equal(T x, U y)
{
    return test::make_lhs_expression(float_equal_expression<T, U>{x, y});
}

template <class T, class U>
void test_equality()
{
    auto x1 = T(0.1);
    auto x2 = U(0.0);
    auto x3 = U(1.0);
    EXPECT(test_float_equal(x1, x1));
    EXPECT(test_float_equal(x2, x2));
    EXPECT(test_float_equal(x3, x3));

    EXPECT(not test_float_equal(x1, x2));
    EXPECT(not test_float_equal(x2, x1));
    EXPECT(not test_float_equal(x1, x3));
    EXPECT(not test_float_equal(x3, x1));
    EXPECT(not test_float_equal(x2, x3));
    EXPECT(not test_float_equal(x3, x2));
}

TEST_CASE_REGISTER(test_equality<double, float>);
TEST_CASE_REGISTER(test_equality<double, int>);
TEST_CASE_REGISTER(test_equality<double, migraphx::half>);
TEST_CASE_REGISTER(test_equality<float, int>);
TEST_CASE_REGISTER(test_equality<migraphx::half, int>);

template <class T, class U>
void test_limits()
{
    auto max1 = std::numeric_limits<T>::max();
    auto max2 = std::numeric_limits<U>::max();

    auto min1 = std::numeric_limits<T>::lowest();
    auto min2 = std::numeric_limits<U>::lowest();

    EXPECT(test_float_equal(max1, max1));
    EXPECT(test_float_equal(max2, max2));

    EXPECT(not test_float_equal(max1, max2));
    EXPECT(not test_float_equal(max2, max1));

    EXPECT(test_float_equal(min1, min1));
    EXPECT(test_float_equal(min2, min2));

    EXPECT(not test_float_equal(min1, min2));
    EXPECT(not test_float_equal(min2, min1));

    EXPECT(not test_float_equal(max1, min1));
    EXPECT(not test_float_equal(min1, max1));
    EXPECT(not test_float_equal(max2, min2));
    EXPECT(not test_float_equal(min2, max2));

    EXPECT(not test_float_equal(max1, min2));
    EXPECT(not test_float_equal(min2, max1));

    EXPECT(not test_float_equal(max2, min1));
    EXPECT(not test_float_equal(min1, max2));
}

TEST_CASE_REGISTER(test_limits<double, float>);
TEST_CASE_REGISTER(test_limits<double, int>);
TEST_CASE_REGISTER(test_limits<double, migraphx::half>);
TEST_CASE_REGISTER(test_limits<float, int>);
TEST_CASE_REGISTER(test_limits<int, migraphx::half>);
TEST_CASE_REGISTER(test_limits<long, int>);
TEST_CASE_REGISTER(test_limits<long, char>);

int main(int argc, const char* argv[]) { test::run(argc, argv); }