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