1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <gtest/gtest.h>
25bf215546Sopenharmony_ci#include <math.h>
26bf215546Sopenharmony_ci#include "brw_reg.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ciclass vf_float_conversion_test : public ::testing::Test {
29bf215546Sopenharmony_ci   virtual void SetUp();
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cipublic:
32bf215546Sopenharmony_ci   float vf_to_float[128];
33bf215546Sopenharmony_ci};
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_civoid vf_float_conversion_test::SetUp() {
36bf215546Sopenharmony_ci   /* 0 is special cased. */
37bf215546Sopenharmony_ci   vf_to_float[0] = 0.0;
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci   for (int vf = 1; vf < 128; vf++) {
40bf215546Sopenharmony_ci      int ebits = (vf >> 4) & 0x7;
41bf215546Sopenharmony_ci      int mbits = vf & 0xf;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci      float x = 1.0f + mbits / 16.0f;
44bf215546Sopenharmony_ci      int exp = ebits - 3;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci      vf_to_float[vf] = ldexpf(x, exp);
47bf215546Sopenharmony_ci   }
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ciunion fu {
51bf215546Sopenharmony_ci   float f;
52bf215546Sopenharmony_ci   unsigned u;
53bf215546Sopenharmony_ci};
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cistatic unsigned
56bf215546Sopenharmony_cif2u(float f)
57bf215546Sopenharmony_ci{
58bf215546Sopenharmony_ci   union fu fu;
59bf215546Sopenharmony_ci   fu.f = f;
60bf215546Sopenharmony_ci   return fu.u;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ciTEST_F(vf_float_conversion_test, test_vf_to_float)
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci   for (int vf = 0; vf < 256; vf++) {
66bf215546Sopenharmony_ci      float expected = vf_to_float[vf % 128];
67bf215546Sopenharmony_ci      if (vf > 127)
68bf215546Sopenharmony_ci         expected = -expected;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci      EXPECT_EQ(f2u(expected), f2u(brw_vf_to_float(vf)));
71bf215546Sopenharmony_ci   }
72bf215546Sopenharmony_ci}
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ciTEST_F(vf_float_conversion_test, test_float_to_vf)
75bf215546Sopenharmony_ci{
76bf215546Sopenharmony_ci   for (int vf = 0; vf < 256; vf++) {
77bf215546Sopenharmony_ci      float f = vf_to_float[vf % 128];
78bf215546Sopenharmony_ci      if (vf > 127)
79bf215546Sopenharmony_ci         f = -f;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci      EXPECT_EQ(vf, brw_float_to_vf(f));
82bf215546Sopenharmony_ci   }
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ciTEST_F(vf_float_conversion_test, test_special_case_0)
86bf215546Sopenharmony_ci{
87bf215546Sopenharmony_ci   /* ±0.0f are special cased to the VFs that would otherwise correspond
88bf215546Sopenharmony_ci    * to ±0.125f. Make sure we can't convert these values to VF.
89bf215546Sopenharmony_ci    */
90bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(+0.125f), -1);
91bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(-0.125f), -1);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(+0.0f))), f2u(+0.0f));
94bf215546Sopenharmony_ci   EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(-0.0f))), f2u(-0.0f));
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ciTEST_F(vf_float_conversion_test, test_nonrepresentable_float_input)
98bf215546Sopenharmony_ci{
99bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(+32.0f), -1);
100bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(-32.0f), -1);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(+16.5f), -1);
103bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(-16.5f), -1);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(+8.25f), -1);
106bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(-8.25f), -1);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(+4.125f), -1);
109bf215546Sopenharmony_ci   EXPECT_EQ(brw_float_to_vf(-4.125f), -1);
110bf215546Sopenharmony_ci}
111