• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <math.h>
28 
29 #include "macros.h"
30 #include "rounding.h"
31 
32 #include <gtest/gtest.h>
33 
TEST(Rounding,RoundevenFloat)34 TEST(Rounding, RoundevenFloat)
35 {
36    const struct {
37       float input, expected;
38    } float_data[] = {
39       { 0.0,                  0.0 },
40       { nextafterf(0.5, 0.0), 0.0 },
41       { 0.5,                  0.0 },
42       { nextafterf(0.5, 1.0), 1.0 },
43       { 1.0,                  1.0 },
44       { nextafterf(1.5, 1.0), 1.0 },
45       { 1.5,                  2.0 },
46       { nextafterf(1.5, 2.0), 2.0 },
47       { 2.0,                  2.0 },
48       { nextafterf(2.5, 2.0), 2.0 },
49       { 2.5,                  2.0 },
50       { nextafterf(2.5, 3.0), 3.0 },
51    };
52 
53    for (unsigned i = 0; i < ARRAY_SIZE(float_data); i++) {
54       float output = _mesa_roundevenf(float_data[i].input);
55       EXPECT_TRUE(memcmp(&float_data[i].expected, &output, sizeof(float)) == 0)
56          << "Subtest " << i << " float value: expected " << float_data[i].expected << " from "
57          << "_mesa_roundevenf(" << float_data[i].input << " but got " << output << "\n";
58    }
59 
60    // Test negated values.
61    for (unsigned i = 0; i < ARRAY_SIZE(float_data); i++) {
62       float output = _mesa_roundevenf(-float_data[i].input);
63       float negated_expected = -float_data[i].expected;
64       EXPECT_TRUE(memcmp(&negated_expected, &output, sizeof(float)) == 0)
65          << "Subtest " << i << " float value: expected " << negated_expected << " from "
66          << "_mesa_roundevenf(" << -float_data[i].input << " but got " << output << "\n";
67    }
68 }
69 
TEST(Rounding,RoundevenDouble)70 TEST(Rounding, RoundevenDouble)
71 {
72    const struct {
73       double input, expected;
74    } double_data[] = {
75       { 0.0,                 0.0 },
76       { nextafter(0.5, 0.0), 0.0 },
77       { 0.5,                 0.0 },
78       { nextafter(0.5, 1.0), 1.0 },
79       { 1.0,                 1.0 },
80       { nextafter(1.5, 1.0), 1.0 },
81       { 1.5,                 2.0 },
82       { nextafter(1.5, 2.0), 2.0 },
83       { 2.0,                 2.0 },
84       { nextafter(2.5, 2.0), 2.0 },
85       { 2.5,                 2.0 },
86       { nextafter(2.5, 3.0), 3.0 },
87    };
88 
89    for (unsigned i = 0; i < ARRAY_SIZE(double_data); i++) {
90       double output = _mesa_roundeven(double_data[i].input);
91       EXPECT_TRUE(memcmp(&double_data[i].expected, &output, sizeof(double)) == 0)
92          << "Subtest " << i << " double value: expected " << double_data[i].expected << " from "
93          << "_mesa_roundeven(" << double_data[i].input << " but got " << output << "\n";
94    }
95 
96    // Test negated values.
97    for (unsigned i = 0; i < ARRAY_SIZE(double_data); i++) {
98       double output = _mesa_roundeven(-double_data[i].input);
99       double negated_expected = -double_data[i].expected;
100       EXPECT_TRUE(memcmp(&negated_expected, &output, sizeof(double)) == 0)
101          << "Subtest " << i << " double value: expected " << negated_expected << " from "
102          << "_mesa_roundeven(" << -double_data[i].input << " but got " << output << "\n";
103    }
104 }
105