Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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.  
  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.       int e = ebits - 3;
  44.  
  45.       float value = 1.0f;
  46.  
  47.       value += mbits / 16.0f;
  48.  
  49.       value *= exp2f(e);
  50.  
  51.       vf_to_float[vf] = value;
  52.    }
  53. }
  54.  
  55. union fu {
  56.    float f;
  57.    unsigned u;
  58. };
  59.  
  60. static unsigned
  61. f2u(float f)
  62. {
  63.    union fu fu;
  64.    fu.f = f;
  65.    return fu.u;
  66. }
  67.  
  68. TEST_F(vf_float_conversion_test, test_vf_to_float)
  69. {
  70.    for (int vf = 0; vf < 256; vf++) {
  71.       float expected = vf_to_float[vf % 128];
  72.       if (vf > 127)
  73.          expected = -expected;
  74.  
  75.       EXPECT_EQ(f2u(expected), f2u(brw_vf_to_float(vf)));
  76.    }
  77. }
  78.  
  79. TEST_F(vf_float_conversion_test, test_float_to_vf)
  80. {
  81.    for (int vf = 0; vf < 256; vf++) {
  82.       float f = vf_to_float[vf % 128];
  83.       if (vf > 127)
  84.          f = -f;
  85.  
  86.       EXPECT_EQ(vf, brw_float_to_vf(f));
  87.    }
  88. }
  89.  
  90. TEST_F(vf_float_conversion_test, test_special_case_0)
  91. {
  92.    /* ±0.0f are special cased to the VFs that would otherwise correspond
  93.     * to ±0.125f. Make sure we can't convert these values to VF.
  94.     */
  95.    EXPECT_EQ(brw_float_to_vf(+0.125f), -1);
  96.    EXPECT_EQ(brw_float_to_vf(-0.125f), -1);
  97.  
  98.    EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(+0.0f))), f2u(+0.0f));
  99.    EXPECT_EQ(f2u(brw_vf_to_float(brw_float_to_vf(-0.0f))), f2u(-0.0f));
  100. }
  101.