1 /*
2 * Copyright © 2014 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 <gtest/gtest.h>
25 #include <math.h>
26 #include "brw_reg.h"
27
28 class vf_float_conversion_test : public ::testing::Test {
29 virtual void SetUp();
30
31 public:
32 float vf_to_float[128];
33 };
34
SetUp()35 void vf_float_conversion_test::SetUp() {
36 /* 0 is special cased. */
37 vf_to_float[0] = 0.0;
38
39 for (int vf = 1; vf < 128; vf++) {
40 int ebits = (vf >> 4) & 0x7;
41 int mbits = vf & 0xf;
42
43 float x = 1.0f + mbits / 16.0f;
44 int exp = ebits - 3;
45
46 vf_to_float[vf] = ldexpf(x, exp);
47 }
48 }
49
50 union fu {
51 float f;
52 unsigned u;
53 };
54
55 static unsigned
f2u(float f)56 f2u(float f)
57 {
58 union fu fu;
59 fu.f = f;
60 return fu.u;
61 }
62
TEST_F(vf_float_conversion_test,test_vf_to_float)63 TEST_F(vf_float_conversion_test, test_vf_to_float)
64 {
65 for (int vf = 0; vf < 256; vf++) {
66 float expected = vf_to_float[vf % 128];
67 if (vf > 127)
68 expected = -expected;
69
70 EXPECT_EQ(f2u(expected), f2u(brw_vf_to_float(vf)));
71 }
72 }
73
TEST_F(vf_float_conversion_test,test_float_to_vf)74 TEST_F(vf_float_conversion_test, test_float_to_vf)
75 {
76 for (int vf = 0; vf < 256; vf++) {
77 float f = vf_to_float[vf % 128];
78 if (vf > 127)
79 f = -f;
80
81 EXPECT_EQ(vf, brw_float_to_vf(f));
82 }
83 }
84
TEST_F(vf_float_conversion_test,test_special_case_0)85 TEST_F(vf_float_conversion_test, test_special_case_0)
86 {
87 /* ±0.0f are special cased to the VFs that would otherwise correspond
88 * to ±0.125f. Make sure we can't convert these values to VF.
89 */
90 EXPECT_EQ(brw_float_to_vf(+0.125f), -1);
91 EXPECT_EQ(brw_float_to_vf(-0.125f), -1);
92
93 EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(+0.0f))), f2u(+0.0f));
94 EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(-0.0f))), f2u(-0.0f));
95 }
96
TEST_F(vf_float_conversion_test,test_nonrepresentable_float_input)97 TEST_F(vf_float_conversion_test, test_nonrepresentable_float_input)
98 {
99 EXPECT_EQ(brw_float_to_vf(+32.0f), -1);
100 EXPECT_EQ(brw_float_to_vf(-32.0f), -1);
101
102 EXPECT_EQ(brw_float_to_vf(+16.5f), -1);
103 EXPECT_EQ(brw_float_to_vf(-16.5f), -1);
104
105 EXPECT_EQ(brw_float_to_vf(+8.25f), -1);
106 EXPECT_EQ(brw_float_to_vf(-8.25f), -1);
107
108 EXPECT_EQ(brw_float_to_vf(+4.125f), -1);
109 EXPECT_EQ(brw_float_to_vf(-4.125f), -1);
110 }
111