| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 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 |  | 
| Mathias Agopian | 1d77b71 | 2017-02-17 15:46:13 -0800 | [diff] [blame] | 22 | #include <math/half.h> | 
|  | 23 | #include <math/vec4.h> | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 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 |  | 
| Romain Guy | caf2ca4 | 2016-11-10 11:45:58 -0800 | [diff] [blame] | 35 | EXPECT_EQ(2UL, sizeof(half)); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 36 |  | 
|  | 37 | // test +/- zero | 
| Krzysztof KosiĆski | 91a4df1 | 2020-05-21 00:18:54 -0700 | [diff] [blame] | 38 | EXPECT_EQ(0x0000, half().getBits()); | 
| Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 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 |  | 
|  | 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 |  | 
|  | 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 | }; // namespace android |