2 * Copyright 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "HalfTest"
25 #include <gtest/gtest.h>
29 class HalfTest : public testing::Test {
33 TEST_F(HalfTest, Basics) {
35 EXPECT_EQ(2, sizeof(half));
38 EXPECT_EQ(0x0000, half( 0.0f).getBits());
39 EXPECT_EQ(0x8000, half(-0.0f).getBits());
42 EXPECT_EQ(0x7e00, half(NAN).getBits());
45 EXPECT_EQ(0x7C00, half( std::numeric_limits<float>::infinity()).getBits());
46 EXPECT_EQ(0xFC00, half(-std::numeric_limits<float>::infinity()).getBits());
48 // test a few known values
49 EXPECT_EQ(0x3C01, half(1.0009765625).getBits());
50 EXPECT_EQ(0xC000, half(-2).getBits());
51 EXPECT_EQ(0x0400, half(6.10352e-5).getBits());
52 EXPECT_EQ(0x7BFF, half(65504).getBits());
53 EXPECT_EQ(0x3555, half(1.0f/3).getBits());
56 EXPECT_EQ(0x7C00, std::numeric_limits<half>::infinity().getBits());
57 EXPECT_EQ(0x0400, std::numeric_limits<half>::min().getBits());
58 EXPECT_EQ(0x7BFF, std::numeric_limits<half>::max().getBits());
59 EXPECT_EQ(0xFBFF, std::numeric_limits<half>::lowest().getBits());
61 // denormals (flushed to zero)
62 EXPECT_EQ(0x0000, half( 6.09756e-5).getBits()); // if handled, should be: 0x03FF
63 EXPECT_EQ(0x0000, half( 5.96046e-8).getBits()); // if handled, should be: 0x0001
64 EXPECT_EQ(0x8000, half(-6.09756e-5).getBits()); // if handled, should be: 0x83FF
65 EXPECT_EQ(0x8000, half(-5.96046e-8).getBits()); // if handled, should be: 0x8001
67 // test all exactly representable integers
68 for (int i=-2048 ; i<= 2048 ; ++i) {
70 EXPECT_EQ(i, float(h));
74 TEST_F(HalfTest, Literals) {
76 half pi = 3.1415926_hf;
77 half minusTwo = -2.0_hf;
79 EXPECT_EQ(half(1.0f), one);
80 EXPECT_EQ(half(3.1415926), pi);
81 EXPECT_EQ(half(-2.0f), minusTwo);
85 TEST_F(HalfTest, Vec) {
92 EXPECT_EQ(f4.xyz, h3);
96 }; // namespace android