1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // This Source Code Form is subject to the terms of the Mozilla 5 // Public License v. 2.0. If a copy of the MPL was not distributed 6 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 8 #include <sstream> 9 10 #include "main.h" 11 12 #include <Eigen/src/Core/arch/CUDA/Half.h> 13 14 // Make sure it's possible to forward declare Eigen::half 15 namespace Eigen { 16 struct half; 17 } 18 19 using Eigen::half; 20 21 void test_conversion() 22 { 23 using Eigen::half_impl::__half; 24 25 // Conversion from float. 26 VERIFY_IS_EQUAL(half(1.0f).x, 0x3c00); 27 VERIFY_IS_EQUAL(half(0.5f).x, 0x3800); 28 VERIFY_IS_EQUAL(half(0.33333f).x, 0x3555); 29 VERIFY_IS_EQUAL(half(0.0f).x, 0x0000); 30 VERIFY_IS_EQUAL(half(-0.0f).x, 0x8000); 31 VERIFY_IS_EQUAL(half(65504.0f).x, 0x7bff); 32 VERIFY_IS_EQUAL(half(65536.0f).x, 0x7c00); // Becomes infinity. 33 34 // Denormals. 35 VERIFY_IS_EQUAL(half(-5.96046e-08f).x, 0x8001); 36 VERIFY_IS_EQUAL(half(5.96046e-08f).x, 0x0001); 37 VERIFY_IS_EQUAL(half(1.19209e-07f).x, 0x0002); 38 39 // Verify round-to-nearest-even behavior. 40 float val1 = float(half(__half(0x3c00))); 41 float val2 = float(half(__half(0x3c01))); 42 float val3 = float(half(__half(0x3c02))); 43 VERIFY_IS_EQUAL(half(0.5f * (val1 + val2)).x, 0x3c00); 44 VERIFY_IS_EQUAL(half(0.5f * (val2 + val3)).x, 0x3c02); 45 46 // Conversion from int. 47 VERIFY_IS_EQUAL(half(-1).x, 0xbc00); 48 VERIFY_IS_EQUAL(half(0).x, 0x0000); 49 VERIFY_IS_EQUAL(half(1).x, 0x3c00); 50 VERIFY_IS_EQUAL(half(2).x, 0x4000); 51 VERIFY_IS_EQUAL(half(3).x, 0x4200); 52 53 // Conversion from bool. 54 VERIFY_IS_EQUAL(half(false).x, 0x0000); 55 VERIFY_IS_EQUAL(half(true).x, 0x3c00); 56 57 // Conversion to float. 58 VERIFY_IS_EQUAL(float(half(__half(0x0000))), 0.0f); 59 VERIFY_IS_EQUAL(float(half(__half(0x3c00))), 1.0f); 60 61 // Denormals. 62 VERIFY_IS_APPROX(float(half(__half(0x8001))), -5.96046e-08f); 63 VERIFY_IS_APPROX(float(half(__half(0x0001))), 5.96046e-08f); 64 VERIFY_IS_APPROX(float(half(__half(0x0002))), 1.19209e-07f); 65 66 // NaNs and infinities. 67 VERIFY(!(numext::isinf)(float(half(65504.0f)))); // Largest finite number. 68 VERIFY(!(numext::isnan)(float(half(0.0f)))); 69 VERIFY((numext::isinf)(float(half(__half(0xfc00))))); 70 VERIFY((numext::isnan)(float(half(__half(0xfc01))))); 71 VERIFY((numext::isinf)(float(half(__half(0x7c00))))); 72 VERIFY((numext::isnan)(float(half(__half(0x7c01))))); 73 74 #if !EIGEN_COMP_MSVC 75 // Visual Studio errors out on divisions by 0 76 VERIFY((numext::isnan)(float(half(0.0 / 0.0)))); 77 VERIFY((numext::isinf)(float(half(1.0 / 0.0)))); 78 VERIFY((numext::isinf)(float(half(-1.0 / 0.0)))); 79 #endif 80 81 // Exactly same checks as above, just directly on the half representation. 82 VERIFY(!(numext::isinf)(half(__half(0x7bff)))); 83 VERIFY(!(numext::isnan)(half(__half(0x0000)))); 84 VERIFY((numext::isinf)(half(__half(0xfc00)))); 85 VERIFY((numext::isnan)(half(__half(0xfc01)))); 86 VERIFY((numext::isinf)(half(__half(0x7c00)))); 87 VERIFY((numext::isnan)(half(__half(0x7c01)))); 88 89 #if !EIGEN_COMP_MSVC 90 // Visual Studio errors out on divisions by 0 91 VERIFY((numext::isnan)(half(0.0 / 0.0))); 92 VERIFY((numext::isinf)(half(1.0 / 0.0))); 93 VERIFY((numext::isinf)(half(-1.0 / 0.0))); 94 #endif 95 } 96 97 void test_numtraits() 98 { 99 std::cout << "epsilon = " << NumTraits<half>::epsilon() << " (0x" << std::hex << NumTraits<half>::epsilon().x << ")" << std::endl; 100 std::cout << "highest = " << NumTraits<half>::highest() << " (0x" << std::hex << NumTraits<half>::highest().x << ")" << std::endl; 101 std::cout << "lowest = " << NumTraits<half>::lowest() << " (0x" << std::hex << NumTraits<half>::lowest().x << ")" << std::endl; 102 std::cout << "min = " << (std::numeric_limits<half>::min)() << " (0x" << std::hex << half((std::numeric_limits<half>::min)()).x << ")" << std::endl; 103 std::cout << "denorm min = " << (std::numeric_limits<half>::denorm_min)() << " (0x" << std::hex << half((std::numeric_limits<half>::denorm_min)()).x << ")" << std::endl; 104 std::cout << "infinity = " << NumTraits<half>::infinity() << " (0x" << std::hex << NumTraits<half>::infinity().x << ")" << std::endl; 105 std::cout << "quiet nan = " << NumTraits<half>::quiet_NaN() << " (0x" << std::hex << NumTraits<half>::quiet_NaN().x << ")" << std::endl; 106 std::cout << "signaling nan = " << std::numeric_limits<half>::signaling_NaN() << " (0x" << std::hex << std::numeric_limits<half>::signaling_NaN().x << ")" << std::endl; 107 108 VERIFY(NumTraits<half>::IsSigned); 109 110 VERIFY_IS_EQUAL( std::numeric_limits<half>::infinity().x, half(std::numeric_limits<float>::infinity()).x ); 111 VERIFY_IS_EQUAL( std::numeric_limits<half>::quiet_NaN().x, half(std::numeric_limits<float>::quiet_NaN()).x ); 112 VERIFY_IS_EQUAL( std::numeric_limits<half>::signaling_NaN().x, half(std::numeric_limits<float>::signaling_NaN()).x ); 113 VERIFY( (std::numeric_limits<half>::min)() > half(0.f) ); 114 VERIFY( (std::numeric_limits<half>::denorm_min)() > half(0.f) ); 115 VERIFY( (std::numeric_limits<half>::min)()/half(2) > half(0.f) ); 116 VERIFY_IS_EQUAL( (std::numeric_limits<half>::denorm_min)()/half(2), half(0.f) ); 117 } 118 119 void test_arithmetic() 120 { 121 VERIFY_IS_EQUAL(float(half(2) + half(2)), 4); 122 VERIFY_IS_EQUAL(float(half(2) + half(-2)), 0); 123 VERIFY_IS_APPROX(float(half(0.33333f) + half(0.66667f)), 1.0f); 124 VERIFY_IS_EQUAL(float(half(2.0f) * half(-5.5f)), -11.0f); 125 VERIFY_IS_APPROX(float(half(1.0f) / half(3.0f)), 0.33333f); 126 VERIFY_IS_EQUAL(float(-half(4096.0f)), -4096.0f); 127 VERIFY_IS_EQUAL(float(-half(-4096.0f)), 4096.0f); 128 } 129 130 void test_comparison() 131 { 132 VERIFY(half(1.0f) > half(0.5f)); 133 VERIFY(half(0.5f) < half(1.0f)); 134 VERIFY(!(half(1.0f) < half(0.5f))); 135 VERIFY(!(half(0.5f) > half(1.0f))); 136 137 VERIFY(!(half(4.0f) > half(4.0f))); 138 VERIFY(!(half(4.0f) < half(4.0f))); 139 140 VERIFY(!(half(0.0f) < half(-0.0f))); 141 VERIFY(!(half(-0.0f) < half(0.0f))); 142 VERIFY(!(half(0.0f) > half(-0.0f))); 143 VERIFY(!(half(-0.0f) > half(0.0f))); 144 145 VERIFY(half(0.2f) > half(-1.0f)); 146 VERIFY(half(-1.0f) < half(0.2f)); 147 VERIFY(half(-16.0f) < half(-15.0f)); 148 149 VERIFY(half(1.0f) == half(1.0f)); 150 VERIFY(half(1.0f) != half(2.0f)); 151 152 // Comparisons with NaNs and infinities. 153 #if !EIGEN_COMP_MSVC 154 // Visual Studio errors out on divisions by 0 155 VERIFY(!(half(0.0 / 0.0) == half(0.0 / 0.0))); 156 VERIFY(half(0.0 / 0.0) != half(0.0 / 0.0)); 157 158 VERIFY(!(half(1.0) == half(0.0 / 0.0))); 159 VERIFY(!(half(1.0) < half(0.0 / 0.0))); 160 VERIFY(!(half(1.0) > half(0.0 / 0.0))); 161 VERIFY(half(1.0) != half(0.0 / 0.0)); 162 163 VERIFY(half(1.0) < half(1.0 / 0.0)); 164 VERIFY(half(1.0) > half(-1.0 / 0.0)); 165 #endif 166 } 167 168 void test_basic_functions() 169 { 170 VERIFY_IS_EQUAL(float(numext::abs(half(3.5f))), 3.5f); 171 VERIFY_IS_EQUAL(float(abs(half(3.5f))), 3.5f); 172 VERIFY_IS_EQUAL(float(numext::abs(half(-3.5f))), 3.5f); 173 VERIFY_IS_EQUAL(float(abs(half(-3.5f))), 3.5f); 174 175 VERIFY_IS_EQUAL(float(numext::floor(half(3.5f))), 3.0f); 176 VERIFY_IS_EQUAL(float(floor(half(3.5f))), 3.0f); 177 VERIFY_IS_EQUAL(float(numext::floor(half(-3.5f))), -4.0f); 178 VERIFY_IS_EQUAL(float(floor(half(-3.5f))), -4.0f); 179 180 VERIFY_IS_EQUAL(float(numext::ceil(half(3.5f))), 4.0f); 181 VERIFY_IS_EQUAL(float(ceil(half(3.5f))), 4.0f); 182 VERIFY_IS_EQUAL(float(numext::ceil(half(-3.5f))), -3.0f); 183 VERIFY_IS_EQUAL(float(ceil(half(-3.5f))), -3.0f); 184 185 VERIFY_IS_APPROX(float(numext::sqrt(half(0.0f))), 0.0f); 186 VERIFY_IS_APPROX(float(sqrt(half(0.0f))), 0.0f); 187 VERIFY_IS_APPROX(float(numext::sqrt(half(4.0f))), 2.0f); 188 VERIFY_IS_APPROX(float(sqrt(half(4.0f))), 2.0f); 189 190 VERIFY_IS_APPROX(float(numext::pow(half(0.0f), half(1.0f))), 0.0f); 191 VERIFY_IS_APPROX(float(pow(half(0.0f), half(1.0f))), 0.0f); 192 VERIFY_IS_APPROX(float(numext::pow(half(2.0f), half(2.0f))), 4.0f); 193 VERIFY_IS_APPROX(float(pow(half(2.0f), half(2.0f))), 4.0f); 194 195 VERIFY_IS_EQUAL(float(numext::exp(half(0.0f))), 1.0f); 196 VERIFY_IS_EQUAL(float(exp(half(0.0f))), 1.0f); 197 VERIFY_IS_APPROX(float(numext::exp(half(EIGEN_PI))), 20.f + float(EIGEN_PI)); 198 VERIFY_IS_APPROX(float(exp(half(EIGEN_PI))), 20.f + float(EIGEN_PI)); 199 200 VERIFY_IS_EQUAL(float(numext::log(half(1.0f))), 0.0f); 201 VERIFY_IS_EQUAL(float(log(half(1.0f))), 0.0f); 202 VERIFY_IS_APPROX(float(numext::log(half(10.0f))), 2.30273f); 203 VERIFY_IS_APPROX(float(log(half(10.0f))), 2.30273f); 204 205 VERIFY_IS_EQUAL(float(numext::log1p(half(0.0f))), 0.0f); 206 VERIFY_IS_EQUAL(float(log1p(half(0.0f))), 0.0f); 207 VERIFY_IS_APPROX(float(numext::log1p(half(10.0f))), 2.3978953f); 208 VERIFY_IS_APPROX(float(log1p(half(10.0f))), 2.3978953f); 209 } 210 211 void test_trigonometric_functions() 212 { 213 VERIFY_IS_APPROX(numext::cos(half(0.0f)), half(cosf(0.0f))); 214 VERIFY_IS_APPROX(cos(half(0.0f)), half(cosf(0.0f))); 215 VERIFY_IS_APPROX(numext::cos(half(EIGEN_PI)), half(cosf(EIGEN_PI))); 216 //VERIFY_IS_APPROX(numext::cos(half(EIGEN_PI/2)), half(cosf(EIGEN_PI/2))); 217 //VERIFY_IS_APPROX(numext::cos(half(3*EIGEN_PI/2)), half(cosf(3*EIGEN_PI/2))); 218 VERIFY_IS_APPROX(numext::cos(half(3.5f)), half(cosf(3.5f))); 219 220 VERIFY_IS_APPROX(numext::sin(half(0.0f)), half(sinf(0.0f))); 221 VERIFY_IS_APPROX(sin(half(0.0f)), half(sinf(0.0f))); 222 // VERIFY_IS_APPROX(numext::sin(half(EIGEN_PI)), half(sinf(EIGEN_PI))); 223 VERIFY_IS_APPROX(numext::sin(half(EIGEN_PI/2)), half(sinf(EIGEN_PI/2))); 224 VERIFY_IS_APPROX(numext::sin(half(3*EIGEN_PI/2)), half(sinf(3*EIGEN_PI/2))); 225 VERIFY_IS_APPROX(numext::sin(half(3.5f)), half(sinf(3.5f))); 226 227 VERIFY_IS_APPROX(numext::tan(half(0.0f)), half(tanf(0.0f))); 228 VERIFY_IS_APPROX(tan(half(0.0f)), half(tanf(0.0f))); 229 // VERIFY_IS_APPROX(numext::tan(half(EIGEN_PI)), half(tanf(EIGEN_PI))); 230 // VERIFY_IS_APPROX(numext::tan(half(EIGEN_PI/2)), half(tanf(EIGEN_PI/2))); 231 //VERIFY_IS_APPROX(numext::tan(half(3*EIGEN_PI/2)), half(tanf(3*EIGEN_PI/2))); 232 VERIFY_IS_APPROX(numext::tan(half(3.5f)), half(tanf(3.5f))); 233 } 234 235 void test_array() 236 { 237 typedef Array<half,1,Dynamic> ArrayXh; 238 Index size = internal::random<Index>(1,10); 239 Index i = internal::random<Index>(0,size-1); 240 ArrayXh a1 = ArrayXh::Random(size), a2 = ArrayXh::Random(size); 241 VERIFY_IS_APPROX( a1+a1, half(2)*a1 ); 242 VERIFY( (a1.abs() >= half(0)).all() ); 243 VERIFY_IS_APPROX( (a1*a1).sqrt(), a1.abs() ); 244 245 VERIFY( ((a1.min)(a2) <= (a1.max)(a2)).all() ); 246 a1(i) = half(-10.); 247 VERIFY_IS_EQUAL( a1.minCoeff(), half(-10.) ); 248 a1(i) = half(10.); 249 VERIFY_IS_EQUAL( a1.maxCoeff(), half(10.) ); 250 251 std::stringstream ss; 252 ss << a1; 253 } 254 255 void test_half_float() 256 { 257 CALL_SUBTEST(test_conversion()); 258 CALL_SUBTEST(test_numtraits()); 259 CALL_SUBTEST(test_arithmetic()); 260 CALL_SUBTEST(test_comparison()); 261 CALL_SUBTEST(test_basic_functions()); 262 CALL_SUBTEST(test_trigonometric_functions()); 263 CALL_SUBTEST(test_array()); 264 } 265