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 // The conversion routines are Copyright (c) Fabian Giesen, 2016.
9 // The original license follows:
10 //
11 // Copyright (c) Fabian Giesen, 2016
12 // All rights reserved.
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27
28 // Standard 16-bit float type, mostly useful for GPUs. Defines a new
29 // type Eigen::half (inheriting from CUDA's __half struct) with
30 // operator overloads such that it behaves basically as an arithmetic
31 // type. It will be quite slow on CPUs (so it is recommended to stay
32 // in fp32 for CPUs, except for simple parameter conversions, I/O
33 // to disk and the likes), but fast on GPUs.
34
35
36 #ifndef EIGEN_HALF_CUDA_H
37 #define EIGEN_HALF_CUDA_H
38
39 #if __cplusplus > 199711L
40 #define EIGEN_EXPLICIT_CAST(tgt_type) explicit operator tgt_type()
41 #else
42 #define EIGEN_EXPLICIT_CAST(tgt_type) operator tgt_type()
43 #endif
44
45 #include <sstream>
46
47 namespace Eigen {
48
49 struct half;
50
51 namespace half_impl {
52
53 #if !defined(EIGEN_HAS_CUDA_FP16)
54
55 // Make our own __half definition that is similar to CUDA's.
56 struct __half {
__half__half57 EIGEN_DEVICE_FUNC __half() {}
__half__half58 explicit EIGEN_DEVICE_FUNC __half(unsigned short raw) : x(raw) {}
59 unsigned short x;
60 };
61
62 #endif
63
64 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half raw_uint16_to_half(unsigned short x);
65 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half float_to_half_rtne(float ff);
66 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half h);
67
68 struct half_base : public __half {
half_basehalf_base69 EIGEN_DEVICE_FUNC half_base() {}
half_basehalf_base70 EIGEN_DEVICE_FUNC half_base(const half_base& h) : __half(h) {}
half_basehalf_base71 EIGEN_DEVICE_FUNC half_base(const __half& h) : __half(h) {}
72 };
73
74 } // namespace half_impl
75
76 // Class definition.
77 struct half : public half_impl::half_base {
78 #if !defined(EIGEN_HAS_CUDA_FP16)
79 typedef half_impl::__half __half;
80 #endif
81
halfhalf82 EIGEN_DEVICE_FUNC half() {}
83
halfhalf84 EIGEN_DEVICE_FUNC half(const __half& h) : half_impl::half_base(h) {}
halfhalf85 EIGEN_DEVICE_FUNC half(const half& h) : half_impl::half_base(h) {}
86
halfhalf87 explicit EIGEN_DEVICE_FUNC half(bool b)
88 : half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
89 template<class T>
halfhalf90 explicit EIGEN_DEVICE_FUNC half(const T& val)
91 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
halfhalf92 explicit EIGEN_DEVICE_FUNC half(float f)
93 : half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
94
EIGEN_EXPLICIT_CASThalf95 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(bool) const {
96 // +0.0 and -0.0 become false, everything else becomes true.
97 return (x & 0x7fff) != 0;
98 }
EIGEN_EXPLICIT_CASThalf99 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(signed char) const {
100 return static_cast<signed char>(half_impl::half_to_float(*this));
101 }
EIGEN_EXPLICIT_CASThalf102 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned char) const {
103 return static_cast<unsigned char>(half_impl::half_to_float(*this));
104 }
EIGEN_EXPLICIT_CASThalf105 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(short) const {
106 return static_cast<short>(half_impl::half_to_float(*this));
107 }
EIGEN_EXPLICIT_CASThalf108 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned short) const {
109 return static_cast<unsigned short>(half_impl::half_to_float(*this));
110 }
EIGEN_EXPLICIT_CASThalf111 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(int) const {
112 return static_cast<int>(half_impl::half_to_float(*this));
113 }
EIGEN_EXPLICIT_CASThalf114 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned int) const {
115 return static_cast<unsigned int>(half_impl::half_to_float(*this));
116 }
EIGEN_EXPLICIT_CASThalf117 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(long) const {
118 return static_cast<long>(half_impl::half_to_float(*this));
119 }
EIGEN_EXPLICIT_CASThalf120 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned long) const {
121 return static_cast<unsigned long>(half_impl::half_to_float(*this));
122 }
EIGEN_EXPLICIT_CASThalf123 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(long long) const {
124 return static_cast<long long>(half_impl::half_to_float(*this));
125 }
EIGEN_EXPLICIT_CASThalf126 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned long long) const {
127 return static_cast<unsigned long long>(half_to_float(*this));
128 }
EIGEN_EXPLICIT_CASThalf129 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(float) const {
130 return half_impl::half_to_float(*this);
131 }
EIGEN_EXPLICIT_CASThalf132 EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(double) const {
133 return static_cast<double>(half_impl::half_to_float(*this));
134 }
135
136 EIGEN_DEVICE_FUNC half& operator=(const half& other) {
137 x = other.x;
138 return *this;
139 }
140 };
141
142 namespace half_impl {
143
144 #if defined(EIGEN_HAS_CUDA_FP16) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
145
146 // Intrinsics for native fp16 support. Note that on current hardware,
147 // these are no faster than fp32 arithmetic (you need to use the half2
148 // versions to get the ALU speed increased), but you do save the
149 // conversion steps back and forth.
150
151 __device__ half operator + (const half& a, const half& b) {
152 return __hadd(a, b);
153 }
154 __device__ half operator * (const half& a, const half& b) {
155 return __hmul(a, b);
156 }
157 __device__ half operator - (const half& a, const half& b) {
158 return __hsub(a, b);
159 }
160 __device__ half operator / (const half& a, const half& b) {
161 float num = __half2float(a);
162 float denom = __half2float(b);
163 return __float2half(num / denom);
164 }
165 __device__ half operator - (const half& a) {
166 return __hneg(a);
167 }
168 __device__ half& operator += (half& a, const half& b) {
169 a = a + b;
170 return a;
171 }
172 __device__ half& operator *= (half& a, const half& b) {
173 a = a * b;
174 return a;
175 }
176 __device__ half& operator -= (half& a, const half& b) {
177 a = a - b;
178 return a;
179 }
180 __device__ half& operator /= (half& a, const half& b) {
181 a = a / b;
182 return a;
183 }
184 __device__ bool operator == (const half& a, const half& b) {
185 return __heq(a, b);
186 }
187 __device__ bool operator != (const half& a, const half& b) {
188 return __hne(a, b);
189 }
190 __device__ bool operator < (const half& a, const half& b) {
191 return __hlt(a, b);
192 }
193 __device__ bool operator <= (const half& a, const half& b) {
194 return __hle(a, b);
195 }
196 __device__ bool operator > (const half& a, const half& b) {
197 return __hgt(a, b);
198 }
199 __device__ bool operator >= (const half& a, const half& b) {
200 return __hge(a, b);
201 }
202
203 #else // Emulate support for half floats
204
205 // Definitions for CPUs and older CUDA, mostly working through conversion
206 // to/from fp32.
207
208 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
209 return half(float(a) + float(b));
210 }
211 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
212 return half(float(a) * float(b));
213 }
214 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
215 return half(float(a) - float(b));
216 }
217 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
218 return half(float(a) / float(b));
219 }
220 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
221 half result;
222 result.x = a.x ^ 0x8000;
223 return result;
224 }
225 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
226 a = half(float(a) + float(b));
227 return a;
228 }
229 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
230 a = half(float(a) * float(b));
231 return a;
232 }
233 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
234 a = half(float(a) - float(b));
235 return a;
236 }
237 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
238 a = half(float(a) / float(b));
239 return a;
240 }
241 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
242 return float(a) == float(b);
243 }
244 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
245 return float(a) != float(b);
246 }
247 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
248 return float(a) < float(b);
249 }
250 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
251 return float(a) <= float(b);
252 }
253 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
254 return float(a) > float(b);
255 }
256 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
257 return float(a) >= float(b);
258 }
259
260 #endif // Emulate support for half floats
261
262 // Division by an index. Do it in full float precision to avoid accuracy
263 // issues in converting the denominator to half.
264 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, Index b) {
265 return half(static_cast<float>(a) / static_cast<float>(b));
266 }
267
268 // Conversion routines, including fallbacks for the host or older CUDA.
269 // Note that newer Intel CPUs (Haswell or newer) have vectorized versions of
270 // these in hardware. If we need more performance on older/other CPUs, they are
271 // also possible to vectorize directly.
272
raw_uint16_to_half(unsigned short x)273 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half raw_uint16_to_half(unsigned short x) {
274 __half h;
275 h.x = x;
276 return h;
277 }
278
279 union FP32 {
280 unsigned int u;
281 float f;
282 };
283
float_to_half_rtne(float ff)284 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half float_to_half_rtne(float ff) {
285 #if defined(EIGEN_HAS_CUDA_FP16) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 300
286 return __float2half(ff);
287
288 #elif defined(EIGEN_HAS_FP16_C)
289 __half h;
290 h.x = _cvtss_sh(ff, 0);
291 return h;
292
293 #else
294 FP32 f; f.f = ff;
295
296 const FP32 f32infty = { 255 << 23 };
297 const FP32 f16max = { (127 + 16) << 23 };
298 const FP32 denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 };
299 unsigned int sign_mask = 0x80000000u;
300 __half o;
301 o.x = static_cast<unsigned short>(0x0u);
302
303 unsigned int sign = f.u & sign_mask;
304 f.u ^= sign;
305
306 // NOTE all the integer compares in this function can be safely
307 // compiled into signed compares since all operands are below
308 // 0x80000000. Important if you want fast straight SSE2 code
309 // (since there's no unsigned PCMPGTD).
310
311 if (f.u >= f16max.u) { // result is Inf or NaN (all exponent bits set)
312 o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
313 } else { // (De)normalized number or zero
314 if (f.u < (113 << 23)) { // resulting FP16 is subnormal or zero
315 // use a magic value to align our 10 mantissa bits at the bottom of
316 // the float. as long as FP addition is round-to-nearest-even this
317 // just works.
318 f.f += denorm_magic.f;
319
320 // and one integer subtract of the bias later, we have our final float!
321 o.x = static_cast<unsigned short>(f.u - denorm_magic.u);
322 } else {
323 unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd
324
325 // update exponent, rounding bias part 1
326 f.u += ((unsigned int)(15 - 127) << 23) + 0xfff;
327 // rounding bias part 2
328 f.u += mant_odd;
329 // take the bits!
330 o.x = static_cast<unsigned short>(f.u >> 13);
331 }
332 }
333
334 o.x |= static_cast<unsigned short>(sign >> 16);
335 return o;
336 #endif
337 }
338
half_to_float(__half h)339 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half h) {
340 #if defined(EIGEN_HAS_CUDA_FP16) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 300
341 return __half2float(h);
342
343 #elif defined(EIGEN_HAS_FP16_C)
344 return _cvtsh_ss(h.x);
345
346 #else
347 const FP32 magic = { 113 << 23 };
348 const unsigned int shifted_exp = 0x7c00 << 13; // exponent mask after shift
349 FP32 o;
350
351 o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
352 unsigned int exp = shifted_exp & o.u; // just the exponent
353 o.u += (127 - 15) << 23; // exponent adjust
354
355 // handle exponent special cases
356 if (exp == shifted_exp) { // Inf/NaN?
357 o.u += (128 - 16) << 23; // extra exp adjust
358 } else if (exp == 0) { // Zero/Denormal?
359 o.u += 1 << 23; // extra exp adjust
360 o.f -= magic.f; // renormalize
361 }
362
363 o.u |= (h.x & 0x8000) << 16; // sign bit
364 return o.f;
365 #endif
366 }
367
368 // --- standard functions ---
369
370 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isinf)(const half& a) {
371 return (a.x & 0x7fff) == 0x7c00;
372 }
373 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isnan)(const half& a) {
374 #if defined(EIGEN_HAS_CUDA_FP16) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
375 return __hisnan(a);
376 #else
377 return (a.x & 0x7fff) > 0x7c00;
378 #endif
379 }
380 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isfinite)(const half& a) {
381 return !(isinf EIGEN_NOT_A_MACRO (a)) && !(isnan EIGEN_NOT_A_MACRO (a));
382 }
383
abs(const half & a)384 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(const half& a) {
385 half result;
386 result.x = a.x & 0x7FFF;
387 return result;
388 }
exp(const half & a)389 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(const half& a) {
390 return half(::expf(float(a)));
391 }
log(const half & a)392 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(const half& a) {
393 #if defined(EIGEN_HAS_CUDA_FP16) && defined __CUDACC_VER__ && __CUDACC_VER__ >= 80000 && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
394 return Eigen::half(::hlog(a));
395 #else
396 return half(::logf(float(a)));
397 #endif
398 }
log1p(const half & a)399 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(const half& a) {
400 return half(numext::log1p(float(a)));
401 }
log10(const half & a)402 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(const half& a) {
403 return half(::log10f(float(a)));
404 }
sqrt(const half & a)405 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(const half& a) {
406 return half(::sqrtf(float(a)));
407 }
pow(const half & a,const half & b)408 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half& a, const half& b) {
409 return half(::powf(float(a), float(b)));
410 }
sin(const half & a)411 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(const half& a) {
412 return half(::sinf(float(a)));
413 }
cos(const half & a)414 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(const half& a) {
415 return half(::cosf(float(a)));
416 }
tan(const half & a)417 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(const half& a) {
418 return half(::tanf(float(a)));
419 }
tanh(const half & a)420 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(const half& a) {
421 return half(::tanhf(float(a)));
422 }
floor(const half & a)423 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half floor(const half& a) {
424 return half(::floorf(float(a)));
425 }
ceil(const half & a)426 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half ceil(const half& a) {
427 return half(::ceilf(float(a)));
428 }
429
half(min)430 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (min)(const half& a, const half& b) {
431 #if defined(EIGEN_HAS_CUDA_FP16) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
432 return __hlt(b, a) ? b : a;
433 #else
434 const float f1 = static_cast<float>(a);
435 const float f2 = static_cast<float>(b);
436 return f2 < f1 ? b : a;
437 #endif
438 }
half(max)439 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (max)(const half& a, const half& b) {
440 #if defined(EIGEN_HAS_CUDA_FP16) && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
441 return __hlt(a, b) ? b : a;
442 #else
443 const float f1 = static_cast<float>(a);
444 const float f2 = static_cast<float>(b);
445 return f1 < f2 ? b : a;
446 #endif
447 }
448
449 EIGEN_ALWAYS_INLINE std::ostream& operator << (std::ostream& os, const half& v) {
450 os << static_cast<float>(v);
451 return os;
452 }
453
454 } // end namespace half_impl
455
456 // import Eigen::half_impl::half into Eigen namespace
457 // using half_impl::half;
458
459 namespace internal {
460
461 template<>
462 struct random_default_impl<half, false, false>
463 {
464 static inline half run(const half& x, const half& y)
465 {
466 return x + (y-x) * half(float(std::rand()) / float(RAND_MAX));
467 }
468 static inline half run()
469 {
470 return run(half(-1.f), half(1.f));
471 }
472 };
473
474 template<> struct is_arithmetic<half> { enum { value = true }; };
475
476 } // end namespace internal
477
478 } // end namespace Eigen
479
480 namespace std {
481 template<>
482 struct numeric_limits<Eigen::half> {
483 static const bool is_specialized = true;
484 static const bool is_signed = true;
485 static const bool is_integer = false;
486 static const bool is_exact = false;
487 static const bool has_infinity = true;
488 static const bool has_quiet_NaN = true;
489 static const bool has_signaling_NaN = true;
490 static const float_denorm_style has_denorm = denorm_present;
491 static const bool has_denorm_loss = false;
492 static const std::float_round_style round_style = std::round_to_nearest;
493 static const bool is_iec559 = false;
494 static const bool is_bounded = false;
495 static const bool is_modulo = false;
496 static const int digits = 11;
497 static const int digits10 = 2;
498 //static const int max_digits10 = ;
499 static const int radix = 2;
500 static const int min_exponent = -13;
501 static const int min_exponent10 = -4;
502 static const int max_exponent = 16;
503 static const int max_exponent10 = 4;
504 static const bool traps = true;
505 static const bool tinyness_before = false;
506
507 static Eigen::half (min)() { return Eigen::half_impl::raw_uint16_to_half(0x400); }
508 static Eigen::half lowest() { return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
509 static Eigen::half (max)() { return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
510 static Eigen::half epsilon() { return Eigen::half_impl::raw_uint16_to_half(0x0800); }
511 static Eigen::half round_error() { return Eigen::half(0.5); }
512 static Eigen::half infinity() { return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
513 static Eigen::half quiet_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
514 static Eigen::half signaling_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
515 static Eigen::half denorm_min() { return Eigen::half_impl::raw_uint16_to_half(0x1); }
516 };
517 }
518
519 namespace Eigen {
520
521 template<> struct NumTraits<Eigen::half>
522 : GenericNumTraits<Eigen::half>
523 {
524 enum {
525 IsSigned = true,
526 IsInteger = false,
527 IsComplex = false,
528 RequireInitialization = false
529 };
530
531 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Eigen::half epsilon() {
532 return half_impl::raw_uint16_to_half(0x0800);
533 }
534 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Eigen::half dummy_precision() { return Eigen::half(1e-2f); }
535 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Eigen::half highest() {
536 return half_impl::raw_uint16_to_half(0x7bff);
537 }
538 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Eigen::half lowest() {
539 return half_impl::raw_uint16_to_half(0xfbff);
540 }
541 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Eigen::half infinity() {
542 return half_impl::raw_uint16_to_half(0x7c00);
543 }
544 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Eigen::half quiet_NaN() {
545 return half_impl::raw_uint16_to_half(0x7c01);
546 }
547 };
548
549 } // end namespace Eigen
550
551 // C-like standard mathematical functions and trancendentals.
552 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half fabsh(const Eigen::half& a) {
553 Eigen::half result;
554 result.x = a.x & 0x7FFF;
555 return result;
556 }
557 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half exph(const Eigen::half& a) {
558 return Eigen::half(::expf(float(a)));
559 }
560 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half logh(const Eigen::half& a) {
561 #if defined __CUDACC_VER__ && __CUDACC_VER__ >= 80000 && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
562 return Eigen::half(::hlog(a));
563 #else
564 return Eigen::half(::logf(float(a)));
565 #endif
566 }
567 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half sqrth(const Eigen::half& a) {
568 return Eigen::half(::sqrtf(float(a)));
569 }
570 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half powh(const Eigen::half& a, const Eigen::half& b) {
571 return Eigen::half(::powf(float(a), float(b)));
572 }
573 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half floorh(const Eigen::half& a) {
574 return Eigen::half(::floorf(float(a)));
575 }
576 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half ceilh(const Eigen::half& a) {
577 return Eigen::half(::ceilf(float(a)));
578 }
579
580 namespace std {
581
582 #if __cplusplus > 199711L
583 template <>
584 struct hash<Eigen::half> {
585 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(const Eigen::half& a) const {
586 return static_cast<std::size_t>(a.x);
587 }
588 };
589 #endif
590
591 } // end namespace std
592
593
594 // Add the missing shfl_xor intrinsic
595 #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 300
596 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor(Eigen::half var, int laneMask, int width=warpSize) {
597 return static_cast<Eigen::half>(__shfl_xor(static_cast<float>(var), laneMask, width));
598 }
599 #endif
600
601 // ldg() has an overload for __half, but we also need one for Eigen::half.
602 #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 350
603 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half __ldg(const Eigen::half* ptr) {
604 return Eigen::half_impl::raw_uint16_to_half(
605 __ldg(reinterpret_cast<const unsigned short*>(ptr)));
606 }
607 #endif
608
609
610 #if defined(__CUDA_ARCH__)
611 namespace Eigen {
612 namespace numext {
613
614 template<>
615 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
616 bool (isnan)(const Eigen::half& h) {
617 return (half_impl::isnan)(h);
618 }
619
620 template<>
621 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
622 bool (isinf)(const Eigen::half& h) {
623 return (half_impl::isinf)(h);
624 }
625
626 template<>
627 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
628 bool (isfinite)(const Eigen::half& h) {
629 return (half_impl::isfinite)(h);
630 }
631
632 } // namespace Eigen
633 } // namespace numext
634 #endif
635
636 #endif // EIGEN_HALF_CUDA_H
637