summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/i965/test_vf_float_conversions.cpp
blob: 7af97d0d09737cf7b69bcd5c38aade4378985deb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
/*
 * Copyright © 2014 Intel Corporation
 *
 * 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 (including the next
 * paragraph) 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.
 */

#include <gtest/gtest.h>
#include <math.h>
#include "brw_reg.h"

class vf_float_conversion_test : public ::testing::Test {
   virtual void SetUp();

public:
   float vf_to_float[128];
};

void vf_float_conversion_test::SetUp() {
   /* 0 is special cased. */
   vf_to_float[0] = 0.0;

   for (int vf = 1; vf < 128; vf++) {
      int ebits = (vf >> 4) & 0x7;
      int mbits = vf & 0xf;

      float x = 1.0f + mbits / 16.0f;
      int exp = ebits - 3;

      vf_to_float[vf] = ldexpf(x, exp);
   }
}

union fu {
   float f;
   unsigned u;
};

static unsigned
f2u(float f)
{
   union fu fu;
   fu.f = f;
   return fu.u;
}

TEST_F(vf_float_conversion_test, test_vf_to_float)
{
   for (int vf = 0; vf < 256; vf++) {
      float expected = vf_to_float[vf % 128];
      if (vf > 127)
         expected = -expected;

      EXPECT_EQ(f2u(expected), f2u(brw_vf_to_float(vf)));
   }
}

TEST_F(vf_float_conversion_test, test_float_to_vf)
{
   for (int vf = 0; vf < 256; vf++) {
      float f = vf_to_float[vf % 128];
      if (vf > 127)
         f = -f;

      EXPECT_EQ(vf, brw_float_to_vf(f));
   }
}

TEST_F(vf_float_conversion_test, test_special_case_0)
{
   /* ±0.0f are special cased to the VFs that would otherwise correspond
    * to ±0.125f. Make sure we can't convert these values to VF.
    */
   EXPECT_EQ(brw_float_to_vf(+0.125f), -1);
   EXPECT_EQ(brw_float_to_vf(-0.125f), -1);

   EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(+0.0f))), f2u(+0.0f));
   EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(-0.0f))), f2u(-0.0f));
}

TEST_F(vf_float_conversion_test, test_nonrepresentable_float_input)
{
   EXPECT_EQ(brw_float_to_vf(+32.0f), -1);
   EXPECT_EQ(brw_float_to_vf(-32.0f), -1);

   EXPECT_EQ(brw_float_to_vf(+16.5f), -1);
   EXPECT_EQ(brw_float_to_vf(-16.5f), -1);

   EXPECT_EQ(brw_float_to_vf(+8.25f), -1);
   EXPECT_EQ(brw_float_to_vf(-8.25f), -1);

   EXPECT_EQ(brw_float_to_vf(+4.125f), -1);
   EXPECT_EQ(brw_float_to_vf(-4.125f), -1);
}