Path Tracer
Half.h
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 either from CUDA's or HIP'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_H
37 #define EIGEN_HALF_H
38 
39 #include <sstream>
40 
41 #if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
42 // When compiling with GPU support, the "__half_raw" base class as well as
43 // some other routines are defined in the GPU compiler header files
44 // (cuda_fp16.h, hip_fp16.h), and they are not tagged constexpr
45 // As a consequence, we get compile failures when compiling Eigen with
46 // GPU support. Hence the need to disable EIGEN_CONSTEXPR when building
47 // Eigen with GPU support
48  #pragma push_macro("EIGEN_CONSTEXPR")
49  #undef EIGEN_CONSTEXPR
50  #define EIGEN_CONSTEXPR
51 #endif
52 
53 #define F16_PACKET_FUNCTION(PACKET_F, PACKET_F16, METHOD) \
54  template <> \
55  EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_UNUSED \
56  PACKET_F16 METHOD<PACKET_F16>(const PACKET_F16& _x) { \
57  return float2half(METHOD<PACKET_F>(half2float(_x))); \
58  }
59 
60 namespace Eigen {
61 
62 struct half;
63 
64 namespace half_impl {
65 
66 #if !defined(EIGEN_HAS_GPU_FP16)
67 // Make our own __half_raw definition that is similar to CUDA's.
68 struct __half_raw {
69  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw() : x(0) {}
70 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
71  explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(numext::bit_cast<__fp16>(raw)) {
72  }
73  __fp16 x;
74 #else
75  explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(raw) {}
76  numext::uint16_t x;
77 #endif
78 };
79 
80 #elif defined(EIGEN_HAS_HIP_FP16)
81  // Nothing to do here
82  // HIP fp16 header file has a definition for __half_raw
83 #elif defined(EIGEN_HAS_CUDA_FP16)
84  #if EIGEN_CUDA_SDK_VER < 90000
85  // In CUDA < 9.0, __half is the equivalent of CUDA 9's __half_raw
86  typedef __half __half_raw;
87  #endif // defined(EIGEN_HAS_CUDA_FP16)
88 #elif defined(SYCL_DEVICE_ONLY)
89  typedef cl::sycl::half __half_raw;
90 #endif
91 
92 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x);
93 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff);
94 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h);
95 
96 struct half_base : public __half_raw {
97  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base() {}
98  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half_raw& h) : __half_raw(h) {}
99 
100 #if defined(EIGEN_HAS_GPU_FP16)
101  #if defined(EIGEN_HAS_HIP_FP16)
102  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) { x = __half_as_ushort(h); }
103  #elif defined(EIGEN_HAS_CUDA_FP16)
104  #if EIGEN_CUDA_SDK_VER >= 90000
105  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) : __half_raw(*(__half_raw*)&h) {}
106  #endif
107  #endif
108 #endif
109 };
110 
111 } // namespace half_impl
112 
113 // Class definition.
114 struct half : public half_impl::half_base {
115 
116  // Writing this out as separate #if-else blocks to make the code easier to follow
117  // The same applies to most #if-else blocks in this file
118 #if !defined(EIGEN_HAS_GPU_FP16)
120 #elif defined(EIGEN_HAS_HIP_FP16)
121  // Nothing to do here
122  // HIP fp16 header file has a definition for __half_raw
123 #elif defined(EIGEN_HAS_CUDA_FP16)
124  // Note that EIGEN_CUDA_SDK_VER is set to 0 even when compiling with HIP, so
125  // (EIGEN_CUDA_SDK_VER < 90000) is true even for HIP! So keeping this within
126  // #if defined(EIGEN_HAS_CUDA_FP16) is needed
127  #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
129  #endif
130 #endif
131 
132  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half() {}
133 
134  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half_raw& h) : half_impl::half_base(h) {}
135 
136 #if defined(EIGEN_HAS_GPU_FP16)
137  #if defined(EIGEN_HAS_HIP_FP16)
138  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
139  #elif defined(EIGEN_HAS_CUDA_FP16)
140  #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
141  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
142  #endif
143  #endif
144 #endif
145 
146 
147  explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(bool b)
148  : half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
149  template<class T>
150  explicit EIGEN_DEVICE_FUNC half(const T& val)
151  : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
152  explicit EIGEN_DEVICE_FUNC half(float f)
153  : half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
154 
155  // Following the convention of numpy, converting between complex and
156  // float will lead to loss of imag value.
157  template<typename RealScalar>
158  explicit EIGEN_DEVICE_FUNC half(std::complex<RealScalar> c)
159  : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(c.real()))) {}
160 
161  EIGEN_DEVICE_FUNC operator float() const { // NOLINT: Allow implicit conversion to float, because it is lossless.
162  return half_impl::half_to_float(*this);
163  }
164 };
165 
166 } // end namespace Eigen
167 
168 namespace std {
169 template<>
170 struct numeric_limits<Eigen::half> {
171  static const bool is_specialized = true;
172  static const bool is_signed = true;
173  static const bool is_integer = false;
174  static const bool is_exact = false;
175  static const bool has_infinity = true;
176  static const bool has_quiet_NaN = true;
177  static const bool has_signaling_NaN = true;
178  static const float_denorm_style has_denorm = denorm_present;
179  static const bool has_denorm_loss = false;
180  static const std::float_round_style round_style = std::round_to_nearest;
181  static const bool is_iec559 = false;
182  static const bool is_bounded = false;
183  static const bool is_modulo = false;
184  static const int digits = 11;
185  static const int digits10 = 3; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
186  static const int max_digits10 = 5; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
187  static const int radix = 2;
188  static const int min_exponent = -13;
189  static const int min_exponent10 = -4;
190  static const int max_exponent = 16;
191  static const int max_exponent10 = 4;
192  static const bool traps = true;
193  static const bool tinyness_before = false;
194 
195  static Eigen::half (min)() { return Eigen::half_impl::raw_uint16_to_half(0x400); }
196  static Eigen::half lowest() { return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
197  static Eigen::half (max)() { return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
198  static Eigen::half epsilon() { return Eigen::half_impl::raw_uint16_to_half(0x0800); }
199  static Eigen::half round_error() { return Eigen::half(0.5); }
200  static Eigen::half infinity() { return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
201  static Eigen::half quiet_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
202  static Eigen::half signaling_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7d00); }
203  static Eigen::half denorm_min() { return Eigen::half_impl::raw_uint16_to_half(0x1); }
204 };
205 
206 // If std::numeric_limits<T> is specialized, should also specialize
207 // std::numeric_limits<const T>, std::numeric_limits<volatile T>, and
208 // std::numeric_limits<const volatile T>
209 // https://stackoverflow.com/a/16519653/
210 template<>
211 struct numeric_limits<const Eigen::half> : numeric_limits<Eigen::half> {};
212 template<>
213 struct numeric_limits<volatile Eigen::half> : numeric_limits<Eigen::half> {};
214 template<>
215 struct numeric_limits<const volatile Eigen::half> : numeric_limits<Eigen::half> {};
216 } // end namespace std
217 
218 namespace Eigen {
219 
220 namespace half_impl {
221 
222 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && \
223  EIGEN_CUDA_ARCH >= 530) || \
224  (defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
225 // Note: We deliberatly do *not* define this to 1 even if we have Arm's native
226 // fp16 type since GPU halfs are rather different from native CPU halfs.
227 // TODO: Rename to something like EIGEN_HAS_NATIVE_GPU_FP16
228 #define EIGEN_HAS_NATIVE_FP16
229 #endif
230 
231 // Intrinsics for native fp16 support. Note that on current hardware,
232 // these are no faster than fp32 arithmetic (you need to use the half2
233 // versions to get the ALU speed increased), but you do save the
234 // conversion steps back and forth.
235 
236 #if defined(EIGEN_HAS_NATIVE_FP16)
237 EIGEN_STRONG_INLINE __device__ half operator + (const half& a, const half& b) {
238 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
239  return __hadd(::__half(a), ::__half(b));
240 #else
241  return __hadd(a, b);
242 #endif
243 }
244 EIGEN_STRONG_INLINE __device__ half operator * (const half& a, const half& b) {
245  return __hmul(a, b);
246 }
247 EIGEN_STRONG_INLINE __device__ half operator - (const half& a, const half& b) {
248  return __hsub(a, b);
249 }
250 EIGEN_STRONG_INLINE __device__ half operator / (const half& a, const half& b) {
251 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
252  return __hdiv(a, b);
253 #else
254  float num = __half2float(a);
255  float denom = __half2float(b);
256  return __float2half(num / denom);
257 #endif
258 }
259 EIGEN_STRONG_INLINE __device__ half operator - (const half& a) {
260  return __hneg(a);
261 }
262 EIGEN_STRONG_INLINE __device__ half& operator += (half& a, const half& b) {
263  a = a + b;
264  return a;
265 }
266 EIGEN_STRONG_INLINE __device__ half& operator *= (half& a, const half& b) {
267  a = a * b;
268  return a;
269 }
270 EIGEN_STRONG_INLINE __device__ half& operator -= (half& a, const half& b) {
271  a = a - b;
272  return a;
273 }
274 EIGEN_STRONG_INLINE __device__ half& operator /= (half& a, const half& b) {
275  a = a / b;
276  return a;
277 }
278 EIGEN_STRONG_INLINE __device__ bool operator == (const half& a, const half& b) {
279  return __heq(a, b);
280 }
281 EIGEN_STRONG_INLINE __device__ bool operator != (const half& a, const half& b) {
282  return __hne(a, b);
283 }
284 EIGEN_STRONG_INLINE __device__ bool operator < (const half& a, const half& b) {
285  return __hlt(a, b);
286 }
287 EIGEN_STRONG_INLINE __device__ bool operator <= (const half& a, const half& b) {
288  return __hle(a, b);
289 }
290 EIGEN_STRONG_INLINE __device__ bool operator > (const half& a, const half& b) {
291  return __hgt(a, b);
292 }
293 EIGEN_STRONG_INLINE __device__ bool operator >= (const half& a, const half& b) {
294  return __hge(a, b);
295 }
296 #endif
297 
298 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
299 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
300  return half(vaddh_f16(a.x, b.x));
301 }
302 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
303  return half(vmulh_f16(a.x, b.x));
304 }
305 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
306  return half(vsubh_f16(a.x, b.x));
307 }
308 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
309  return half(vdivh_f16(a.x, b.x));
310 }
311 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
312  return half(vnegh_f16(a.x));
313 }
314 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
315  a = half(vaddh_f16(a.x, b.x));
316  return a;
317 }
318 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
319  a = half(vmulh_f16(a.x, b.x));
320  return a;
321 }
322 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
323  a = half(vsubh_f16(a.x, b.x));
324  return a;
325 }
326 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
327  a = half(vdivh_f16(a.x, b.x));
328  return a;
329 }
330 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
331  return vceqh_f16(a.x, b.x);
332 }
333 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
334  return !vceqh_f16(a.x, b.x);
335 }
336 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
337  return vclth_f16(a.x, b.x);
338 }
339 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
340  return vcleh_f16(a.x, b.x);
341 }
342 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
343  return vcgth_f16(a.x, b.x);
344 }
345 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
346  return vcgeh_f16(a.x, b.x);
347 }
348 // We need to distinguish ‘clang as the CUDA compiler’ from ‘clang as the host compiler,
349 // invoked by NVCC’ (e.g. on MacOS). The former needs to see both host and device implementation
350 // of the functions, while the latter can only deal with one of them.
351 #elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC) // Emulate support for half floats
352 
353 #if EIGEN_COMP_CLANG && defined(EIGEN_CUDACC)
354 // We need to provide emulated *host-side* FP16 operators for clang.
355 #pragma push_macro("EIGEN_DEVICE_FUNC")
356 #undef EIGEN_DEVICE_FUNC
357 #if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
358 #define EIGEN_DEVICE_FUNC __host__
359 #else // both host and device need emulated ops.
360 #define EIGEN_DEVICE_FUNC __host__ __device__
361 #endif
362 #endif
363 
364 // Definitions for CPUs and older HIP+CUDA, mostly working through conversion
365 // to/from fp32.
366 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
367  return half(float(a) + float(b));
368 }
369 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
370  return half(float(a) * float(b));
371 }
372 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
373  return half(float(a) - float(b));
374 }
375 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
376  return half(float(a) / float(b));
377 }
378 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
379  half result;
380  result.x = a.x ^ 0x8000;
381  return result;
382 }
383 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
384  a = half(float(a) + float(b));
385  return a;
386 }
387 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
388  a = half(float(a) * float(b));
389  return a;
390 }
391 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
392  a = half(float(a) - float(b));
393  return a;
394 }
395 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
396  a = half(float(a) / float(b));
397  return a;
398 }
399 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
400  return numext::equal_strict(float(a),float(b));
401 }
402 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
403  return numext::not_equal_strict(float(a), float(b));
404 }
405 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
406  return float(a) < float(b);
407 }
408 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
409  return float(a) <= float(b);
410 }
411 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
412  return float(a) > float(b);
413 }
414 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
415  return float(a) >= float(b);
416 }
417 
418 #if defined(__clang__) && defined(__CUDA__)
419 #pragma pop_macro("EIGEN_DEVICE_FUNC")
420 #endif
421 #endif // Emulate support for half floats
422 
423 // Division by an index. Do it in full float precision to avoid accuracy
424 // issues in converting the denominator to half.
425 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, Index b) {
426  return half(static_cast<float>(a) / static_cast<float>(b));
427 }
428 
429 // Conversion routines, including fallbacks for the host or older CUDA.
430 // Note that newer Intel CPUs (Haswell or newer) have vectorized versions of
431 // these in hardware. If we need more performance on older/other CPUs, they are
432 // also possible to vectorize directly.
433 
434 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
435  // We cannot simply do a "return __half_raw(x)" here, because __half_raw is union type
436  // in the hip_fp16 header file, and that will trigger a compile error
437  // On the other hand, having anything but a return statement also triggers a compile error
438  // because this is constexpr function.
439  // Fortunately, since we need to disable EIGEN_CONSTEXPR for GPU anyway, we can get out
440  // of this catch22 by having separate bodies for GPU / non GPU
441 #if defined(EIGEN_HAS_GPU_FP16)
442  __half_raw h;
443  h.x = x;
444  return h;
445 #else
446  return __half_raw(x);
447 #endif
448 }
449 
450 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(const __half_raw& h) {
451  // HIP/CUDA/Default have a member 'x' of type uint16_t.
452  // For ARM64 native half, the member 'x' is of type __fp16, so we need to bit-cast.
453  // For SYCL, cl::sycl::half is _Float16, so cast directly.
454 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
455  return numext::bit_cast<numext::uint16_t>(h.x);
456 #elif defined(SYCL_DEVICE_ONLY)
457  return numext::bit_cast<numext::uint16_t>(h);
458 #else
459  return h.x;
460 #endif
461 }
462 
464  unsigned int u;
465  float f;
466 };
467 
468 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff) {
469 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
470  (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
471  __half tmp_ff = __float2half(ff);
472  return *(__half_raw*)&tmp_ff;
473 
474 #elif defined(EIGEN_HAS_FP16_C)
475  __half_raw h;
476  h.x = _cvtss_sh(ff, 0);
477  return h;
478 
479 #elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
480  __half_raw h;
481  h.x = static_cast<__fp16>(ff);
482  return h;
483 
484 #else
485  float32_bits f; f.f = ff;
486 
487  const float32_bits f32infty = { 255 << 23 };
488  const float32_bits f16max = { (127 + 16) << 23 };
489  const float32_bits denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 };
490  unsigned int sign_mask = 0x80000000u;
491  __half_raw o;
492  o.x = static_cast<numext::uint16_t>(0x0u);
493 
494  unsigned int sign = f.u & sign_mask;
495  f.u ^= sign;
496 
497  // NOTE all the integer compares in this function can be safely
498  // compiled into signed compares since all operands are below
499  // 0x80000000. Important if you want fast straight SSE2 code
500  // (since there's no unsigned PCMPGTD).
501 
502  if (f.u >= f16max.u) { // result is Inf or NaN (all exponent bits set)
503  o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
504  } else { // (De)normalized number or zero
505  if (f.u < (113 << 23)) { // resulting FP16 is subnormal or zero
506  // use a magic value to align our 10 mantissa bits at the bottom of
507  // the float. as long as FP addition is round-to-nearest-even this
508  // just works.
509  f.f += denorm_magic.f;
510 
511  // and one integer subtract of the bias later, we have our final float!
512  o.x = static_cast<numext::uint16_t>(f.u - denorm_magic.u);
513  } else {
514  unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd
515 
516  // update exponent, rounding bias part 1
517  // Equivalent to `f.u += ((unsigned int)(15 - 127) << 23) + 0xfff`, but
518  // without arithmetic overflow.
519  f.u += 0xc8000fffU;
520  // rounding bias part 2
521  f.u += mant_odd;
522  // take the bits!
523  o.x = static_cast<numext::uint16_t>(f.u >> 13);
524  }
525  }
526 
527  o.x |= static_cast<numext::uint16_t>(sign >> 16);
528  return o;
529 #endif
530 }
531 
532 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h) {
533 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
534  (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
535  return __half2float(h);
536 #elif defined(EIGEN_HAS_FP16_C)
537  return _cvtsh_ss(h.x);
538 #elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
539  return static_cast<float>(h.x);
540 #else
541  const float32_bits magic = { 113 << 23 };
542  const unsigned int shifted_exp = 0x7c00 << 13; // exponent mask after shift
543  float32_bits o;
544 
545  o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
546  unsigned int exp = shifted_exp & o.u; // just the exponent
547  o.u += (127 - 15) << 23; // exponent adjust
548 
549  // handle exponent special cases
550  if (exp == shifted_exp) { // Inf/NaN?
551  o.u += (128 - 16) << 23; // extra exp adjust
552  } else if (exp == 0) { // Zero/Denormal?
553  o.u += 1 << 23; // extra exp adjust
554  o.f -= magic.f; // renormalize
555  }
556 
557  o.u |= (h.x & 0x8000) << 16; // sign bit
558  return o.f;
559 #endif
560 }
561 
562 // --- standard functions ---
563 
564 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isinf)(const half& a) {
565 #ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
566  return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
567 #else
568  return (a.x & 0x7fff) == 0x7c00;
569 #endif
570 }
571 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isnan)(const half& a) {
572 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
573  (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
574  return __hisnan(a);
575 #elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
576  return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
577 #else
578  return (a.x & 0x7fff) > 0x7c00;
579 #endif
580 }
581 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isfinite)(const half& a) {
582  return !(isinf EIGEN_NOT_A_MACRO (a)) && !(isnan EIGEN_NOT_A_MACRO (a));
583 }
584 
585 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(const half& a) {
586 #if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
587  return half(vabsh_f16(a.x));
588 #else
589  half result;
590  result.x = a.x & 0x7FFF;
591  return result;
592 #endif
593 }
594 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(const half& a) {
595 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
596  defined(EIGEN_HIP_DEVICE_COMPILE)
597  return half(hexp(a));
598 #else
599  return half(::expf(float(a)));
600 #endif
601 }
602 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half expm1(const half& a) {
603  return half(numext::expm1(float(a)));
604 }
605 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(const half& a) {
606 #if (defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 80000 && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
607  (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
608  return half(::hlog(a));
609 #else
610  return half(::logf(float(a)));
611 #endif
612 }
613 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(const half& a) {
614  return half(numext::log1p(float(a)));
615 }
616 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(const half& a) {
617  return half(::log10f(float(a)));
618 }
619 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log2(const half& a) {
620  return half(static_cast<float>(EIGEN_LOG2E) * ::logf(float(a)));
621 }
622 
623 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(const half& a) {
624 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
625  defined(EIGEN_HIP_DEVICE_COMPILE)
626  return half(hsqrt(a));
627 #else
628  return half(::sqrtf(float(a)));
629 #endif
630 }
631 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half& a, const half& b) {
632  return half(::powf(float(a), float(b)));
633 }
634 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(const half& a) {
635  return half(::sinf(float(a)));
636 }
637 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(const half& a) {
638  return half(::cosf(float(a)));
639 }
640 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(const half& a) {
641  return half(::tanf(float(a)));
642 }
643 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(const half& a) {
644  return half(::tanhf(float(a)));
645 }
646 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half asin(const half& a) {
647  return half(::asinf(float(a)));
648 }
649 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half acos(const half& a) {
650  return half(::acosf(float(a)));
651 }
652 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half floor(const half& a) {
653 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
654  defined(EIGEN_HIP_DEVICE_COMPILE)
655  return half(hfloor(a));
656 #else
657  return half(::floorf(float(a)));
658 #endif
659 }
660 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half rint(const half& a) {
661  return half(::rintf(float(a)));
662 }
663 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half ceil(const half& a) {
664 #if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
665  defined(EIGEN_HIP_DEVICE_COMPILE)
666  return half(hceil(a));
667 #else
668  return half(::ceilf(float(a)));
669 #endif
670 }
671 
672 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (min)(const half& a, const half& b) {
673 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
674  (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
675  return __hlt(b, a) ? b : a;
676 #else
677  const float f1 = static_cast<float>(a);
678  const float f2 = static_cast<float>(b);
679  return f2 < f1 ? b : a;
680 #endif
681 }
682 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (max)(const half& a, const half& b) {
683 #if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
684  (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
685  return __hlt(a, b) ? b : a;
686 #else
687  const float f1 = static_cast<float>(a);
688  const float f2 = static_cast<float>(b);
689  return f1 < f2 ? b : a;
690 #endif
691 }
692 
693 #ifndef EIGEN_NO_IO
694 EIGEN_ALWAYS_INLINE std::ostream& operator << (std::ostream& os, const half& v) {
695  os << static_cast<float>(v);
696  return os;
697 }
698 #endif
699 
700 } // end namespace half_impl
701 
702 // import Eigen::half_impl::half into Eigen namespace
703 // using half_impl::half;
704 
705 namespace internal {
706 
707 template<>
708 struct random_default_impl<half, false, false>
709 {
710  static inline half run(const half& x, const half& y)
711  {
712  return x + (y-x) * half(float(std::rand()) / float(RAND_MAX));
713  }
714  static inline half run()
715  {
716  return run(half(-1.f), half(1.f));
717  }
718 };
719 
720 template<> struct is_arithmetic<half> { enum { value = true }; };
721 
722 } // end namespace internal
723 
724 template<> struct NumTraits<Eigen::half>
725  : GenericNumTraits<Eigen::half>
726 {
727  enum {
728  IsSigned = true,
729  IsInteger = false,
730  IsComplex = false,
731  RequireInitialization = false
732  };
733 
734  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half epsilon() {
735  return half_impl::raw_uint16_to_half(0x0800);
736  }
737  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half dummy_precision() {
738  return half_impl::raw_uint16_to_half(0x211f); // Eigen::half(1e-2f);
739  }
740  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half highest() {
741  return half_impl::raw_uint16_to_half(0x7bff);
742  }
743  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half lowest() {
744  return half_impl::raw_uint16_to_half(0xfbff);
745  }
746  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half infinity() {
747  return half_impl::raw_uint16_to_half(0x7c00);
748  }
749  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half quiet_NaN() {
750  return half_impl::raw_uint16_to_half(0x7e00);
751  }
752 };
753 
754 } // end namespace Eigen
755 
756 #if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
757  #pragma pop_macro("EIGEN_CONSTEXPR")
758 #endif
759 
760 namespace std {
761 
762 #if __cplusplus > 199711L
763 template <>
764 struct hash<Eigen::half> {
765  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(const Eigen::half& a) const {
766  return static_cast<std::size_t>(a.x);
767  }
768 };
769 #endif
770 
771 } // end namespace std
772 
773 namespace Eigen {
774 namespace numext {
775 
776 #if defined(EIGEN_GPU_COMPILE_PHASE)
777 
778 template <>
779 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isnan)(const Eigen::half& h) {
780  return (half_impl::isnan)(h);
781 }
782 
783 template <>
784 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isinf)(const Eigen::half& h) {
785  return (half_impl::isinf)(h);
786 }
787 
788 template <>
789 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isfinite)(const Eigen::half& h) {
790  return (half_impl::isfinite)(h);
791 }
792 
793 #endif
794 
795 template <>
796 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half bit_cast<Eigen::half, uint16_t>(const uint16_t& src) {
797  return Eigen::half(Eigen::half_impl::raw_uint16_to_half(src));
798 }
799 
800 template <>
801 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint16_t bit_cast<uint16_t, Eigen::half>(const Eigen::half& src) {
802  return Eigen::half_impl::raw_half_as_uint16(src);
803 }
804 
805 } // namespace numext
806 } // namespace Eigen
807 
808 // Add the missing shfl* intrinsics.
809 // The __shfl* functions are only valid on HIP or _CUDA_ARCH_ >= 300.
810 // CUDA defines them for (__CUDA_ARCH__ >= 300 || !defined(__CUDA_ARCH__))
811 //
812 // HIP and CUDA prior to SDK 9.0 define
813 // __shfl, __shfl_up, __shfl_down, __shfl_xor for int and float
814 // CUDA since 9.0 deprecates those and instead defines
815 // __shfl_sync, __shfl_up_sync, __shfl_down_sync, __shfl_xor_sync,
816 // with native support for __half and __nv_bfloat16
817 //
818 // Note that the following are __device__ - only functions.
819 #if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 300)) \
820  || defined(EIGEN_HIPCC)
821 
822 #if defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 90000
823 
824 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_sync(unsigned mask, Eigen::half var, int srcLane, int width=warpSize) {
825  return static_cast<Eigen::half>(__shfl_sync(mask, static_cast<__half>(var), srcLane, width));
826 }
827 
828 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up_sync(unsigned mask, Eigen::half var, unsigned int delta, int width=warpSize) {
829  return static_cast<Eigen::half>(__shfl_up_sync(mask, static_cast<__half>(var), delta, width));
830 }
831 
832 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down_sync(unsigned mask, Eigen::half var, unsigned int delta, int width=warpSize) {
833  return static_cast<Eigen::half>(__shfl_down_sync(mask, static_cast<__half>(var), delta, width));
834 }
835 
836 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor_sync(unsigned mask, Eigen::half var, int laneMask, int width=warpSize) {
837  return static_cast<Eigen::half>(__shfl_xor_sync(mask, static_cast<__half>(var), laneMask, width));
838 }
839 
840 #else // HIP or CUDA SDK < 9.0
841 
842 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl(Eigen::half var, int srcLane, int width=warpSize) {
843  const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
844  return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl(ivar, srcLane, width)));
845 }
846 
847 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up(Eigen::half var, unsigned int delta, int width=warpSize) {
848  const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
849  return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_up(ivar, delta, width)));
850 }
851 
852 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down(Eigen::half var, unsigned int delta, int width=warpSize) {
853  const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
854  return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_down(ivar, delta, width)));
855 }
856 
857 __device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor(Eigen::half var, int laneMask, int width=warpSize) {
858  const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
859  return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_xor(ivar, laneMask, width)));
860 }
861 
862 #endif // HIP vs CUDA
863 #endif // __shfl*
864 
865 // ldg() has an overload for __half_raw, but we also need one for Eigen::half.
866 #if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 350)) \
867  || defined(EIGEN_HIPCC)
868 EIGEN_STRONG_INLINE __device__ Eigen::half __ldg(const Eigen::half* ptr) {
869  return Eigen::half_impl::raw_uint16_to_half(__ldg(reinterpret_cast<const Eigen::numext::uint16_t*>(ptr)));
870 }
871 #endif // __ldg
872 
873 #endif // EIGEN_HALF_H
Eigen
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
Eigen::half_impl::half_base
Definition: Half.h:96
Eigen::half_impl::__half_raw
Definition: Half.h:68
Eigen::operator*
EIGEN_DEVICE_FUNC const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:515
Eigen::GenericNumTraits
Definition: NumTraits.h:144
Eigen::internal::random_default_impl
Definition: thirdparty/Eigen/src/Core/MathFunctions.h:718
Eigen::half_impl::float32_bits
Definition: Half.h:463
Eigen::half
Definition: Half.h:114
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:213
Eigen::internal::is_arithmetic
Definition: Meta.h:100
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:42