• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "HalfTest"
18 
19 #include <math.h>
20 #include <stdlib.h>
21 
22 #include <math/half.h>
23 #include <math/vec4.h>
24 
25 #include <gtest/gtest.h>
26 
27 namespace android {
28 
29 class HalfTest : public testing::Test {
30 protected:
31 };
32 
TEST_F(HalfTest,Basics)33 TEST_F(HalfTest, Basics) {
34 
35     EXPECT_EQ(2UL, sizeof(half));
36 
37     // test +/- zero
38     EXPECT_EQ(0x0000, half().getBits());
39     EXPECT_EQ(0x0000, half( 0.0f).getBits());
40     EXPECT_EQ(0x8000, half(-0.0f).getBits());
41 
42     // test nan
43     EXPECT_EQ(0x7e00, half(NAN).getBits());
44 
45     // test +/- infinity
46     EXPECT_EQ(0x7C00, half( std::numeric_limits<float>::infinity()).getBits());
47     EXPECT_EQ(0xFC00, half(-std::numeric_limits<float>::infinity()).getBits());
48 
49     // test a few known values
50     EXPECT_EQ(0x3C01, half(1.0009765625).getBits());
51     EXPECT_EQ(0xC000, half(-2).getBits());
52     EXPECT_EQ(0x0400, half(6.10352e-5).getBits());
53     EXPECT_EQ(0x7BFF, half(65504).getBits());
54     EXPECT_EQ(0x3555, half(1.0f/3).getBits());
55 
56     // numeric limits
57     EXPECT_EQ(0x7C00, std::numeric_limits<half>::infinity().getBits());
58     EXPECT_EQ(0x0400, std::numeric_limits<half>::min().getBits());
59     EXPECT_EQ(0x7BFF, std::numeric_limits<half>::max().getBits());
60     EXPECT_EQ(0xFBFF, std::numeric_limits<half>::lowest().getBits());
61 
62     // denormals (flushed to zero)
63     EXPECT_EQ(0x0000, half( 6.09756e-5).getBits());      // if handled, should be: 0x03FF
64     EXPECT_EQ(0x0000, half( 5.96046e-8).getBits());      // if handled, should be: 0x0001
65     EXPECT_EQ(0x8000, half(-6.09756e-5).getBits());      // if handled, should be: 0x83FF
66     EXPECT_EQ(0x8000, half(-5.96046e-8).getBits());      // if handled, should be: 0x8001
67 
68     // test all exactly representable integers
69     for (int i=-2048 ; i<= 2048 ; ++i) {
70         half h = i;
71         EXPECT_EQ(i, float(h));
72     }
73 }
74 
TEST_F(HalfTest,Literals)75 TEST_F(HalfTest, Literals) {
76     half one = 1.0_hf;
77     half pi = 3.1415926_hf;
78     half minusTwo = -2.0_hf;
79 
80     EXPECT_EQ(half(1.0f), one);
81     EXPECT_EQ(half(3.1415926), pi);
82     EXPECT_EQ(half(-2.0f), minusTwo);
83 }
84 
85 
TEST_F(HalfTest,Vec)86 TEST_F(HalfTest, Vec) {
87     float4 f4(1,2,3,4);
88     half4 h4(f4);
89     half3 h3(f4.xyz);
90     half2 h2(f4.xy);
91 
92     EXPECT_EQ(f4, h4);
93     EXPECT_EQ(f4.xyz, h3);
94     EXPECT_EQ(f4.xy, h2);
95 }
96 
97 
TEST_F(HalfTest,Hash)98 TEST_F(HalfTest, Hash) {
99     float4 f4a(1,2,3,4);
100     float4 f4b(2,2,3,4);
101     half4 h4a(f4a), h4b(f4b);
102 
103     EXPECT_NE(std::hash<half4>{}(h4a), std::hash<half4>{}(h4b));
104 }
105 
106 }; // namespace android
107