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 33 TEST_F(HalfTest, Basics) { 34 35 EXPECT_EQ(2UL, sizeof(half)); 36 37 // test +/- zero 38 EXPECT_EQ(0x0000, half( 0.0f).getBits()); 39 EXPECT_EQ(0x8000, half(-0.0f).getBits()); 40 41 // test nan 42 EXPECT_EQ(0x7e00, half(NAN).getBits()); 43 44 // test +/- infinity 45 EXPECT_EQ(0x7C00, half( std::numeric_limits<float>::infinity()).getBits()); 46 EXPECT_EQ(0xFC00, half(-std::numeric_limits<float>::infinity()).getBits()); 47 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()); 54 55 // numeric limits 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()); 60 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 66 67 // test all exactly representable integers 68 for (int i=-2048 ; i<= 2048 ; ++i) { 69 half h = i; 70 EXPECT_EQ(i, float(h)); 71 } 72 } 73 74 TEST_F(HalfTest, Literals) { 75 half one = 1.0_hf; 76 half pi = 3.1415926_hf; 77 half minusTwo = -2.0_hf; 78 79 EXPECT_EQ(half(1.0f), one); 80 EXPECT_EQ(half(3.1415926), pi); 81 EXPECT_EQ(half(-2.0f), minusTwo); 82 } 83 84 85 TEST_F(HalfTest, Vec) { 86 float4 f4(1,2,3,4); 87 half4 h4(f4); 88 half3 h3(f4.xyz); 89 half2 h2(f4.xy); 90 91 EXPECT_EQ(f4, h4); 92 EXPECT_EQ(f4.xyz, h3); 93 EXPECT_EQ(f4.xy, h2); 94 } 95 96 }; // namespace android 97