summaryrefslogtreecommitdiff
path: root/libs/math/tests/half_test.cpp
blob: a514d986e4f543a74008b71f20544ecf5061c242 (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
/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "HalfTest"

#include <math.h>
#include <stdlib.h>

#include <math/half.h>
#include <math/vec4.h>

#include <gtest/gtest.h>

namespace android {

class HalfTest : public testing::Test {
protected:
};

TEST_F(HalfTest, Basics) {

    EXPECT_EQ(2UL, sizeof(half));

    // test +/- zero
    EXPECT_EQ(0x0000, half().getBits());
    EXPECT_EQ(0x0000, half( 0.0f).getBits());
    EXPECT_EQ(0x8000, half(-0.0f).getBits());

    // test nan
    EXPECT_EQ(0x7e00, half(NAN).getBits());

    // test +/- infinity
    EXPECT_EQ(0x7C00, half( std::numeric_limits<float>::infinity()).getBits());
    EXPECT_EQ(0xFC00, half(-std::numeric_limits<float>::infinity()).getBits());

    // test a few known values
    EXPECT_EQ(0x3C01, half(1.0009765625).getBits());
    EXPECT_EQ(0xC000, half(-2).getBits());
    EXPECT_EQ(0x0400, half(6.10352e-5).getBits());
    EXPECT_EQ(0x7BFF, half(65504).getBits());
    EXPECT_EQ(0x3555, half(1.0f/3).getBits());

    // numeric limits
    EXPECT_EQ(0x7C00, std::numeric_limits<half>::infinity().getBits());
    EXPECT_EQ(0x0400, std::numeric_limits<half>::min().getBits());
    EXPECT_EQ(0x7BFF, std::numeric_limits<half>::max().getBits());
    EXPECT_EQ(0xFBFF, std::numeric_limits<half>::lowest().getBits());

    // denormals (flushed to zero)
    EXPECT_EQ(0x0000, half( 6.09756e-5).getBits());      // if handled, should be: 0x03FF
    EXPECT_EQ(0x0000, half( 5.96046e-8).getBits());      // if handled, should be: 0x0001
    EXPECT_EQ(0x8000, half(-6.09756e-5).getBits());      // if handled, should be: 0x83FF
    EXPECT_EQ(0x8000, half(-5.96046e-8).getBits());      // if handled, should be: 0x8001

    // test all exactly representable integers
    for (int i=-2048 ; i<= 2048 ; ++i) {
        half h = i;
        EXPECT_EQ(i, float(h));
    }
}

TEST_F(HalfTest, Literals) {
    half one = 1.0_hf;
    half pi = 3.1415926_hf;
    half minusTwo = -2.0_hf;

    EXPECT_EQ(half(1.0f), one);
    EXPECT_EQ(half(3.1415926), pi);
    EXPECT_EQ(half(-2.0f), minusTwo);
}


TEST_F(HalfTest, Vec) {
    float4 f4(1,2,3,4);
    half4 h4(f4);
    half3 h3(f4.xyz);
    half2 h2(f4.xy);

    EXPECT_EQ(f4, h4);
    EXPECT_EQ(f4.xyz, h3);
    EXPECT_EQ(f4.xy, h2);
}


TEST_F(HalfTest, Hash) {
    float4 f4a(1,2,3,4);
    float4 f4b(2,2,3,4);
    half4 h4a(f4a), h4b(f4b);

    EXPECT_NE(std::hash<half4>{}(h4a), std::hash<half4>{}(h4b));
}

}; // namespace android