Eigen  5.0.1
 
Loading...
Searching...
No Matches
MathFunctionsFP16.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2025 The Eigen Authors.
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_MATH_FUNCTIONS_FP16_AVX512_H
12#define EIGEN_MATH_FUNCTIONS_FP16_AVX512_H
13
14// IWYU pragma: private
15#include "../../InternalHeaderCheck.h"
16
17namespace Eigen {
18namespace internal {
19
20EIGEN_STRONG_INLINE Packet32h combine2Packet16h(const Packet16h& a, const Packet16h& b) {
21 __m512i result = _mm512_castsi256_si512(_mm256_castph_si256(a));
22 result = _mm512_inserti64x4(result, _mm256_castph_si256(b), 1);
23 return _mm512_castsi512_ph(result);
24}
25
26EIGEN_STRONG_INLINE void extract2Packet16h(const Packet32h& x, Packet16h& a, Packet16h& b) {
27 a = _mm256_castsi256_ph(_mm512_castsi512_si256(_mm512_castph_si512(x)));
28 b = _mm256_castsi256_ph(_mm512_extracti64x4_epi64(_mm512_castph_si512(x), 1));
29}
30
31#define _EIGEN_GENERATE_FP16_MATH_FUNCTION(func) \
32 template <> \
33 EIGEN_STRONG_INLINE Packet8h func<Packet8h>(const Packet8h& a) { \
34 return float2half(func(half2float(a))); \
35 } \
36 \
37 template <> \
38 EIGEN_STRONG_INLINE Packet16h func<Packet16h>(const Packet16h& a) { \
39 return float2half(func(half2float(a))); \
40 } \
41 \
42 template <> \
43 EIGEN_STRONG_INLINE Packet32h func<Packet32h>(const Packet32h& a) { \
44 Packet16h low; \
45 Packet16h high; \
46 extract2Packet16h(a, low, high); \
47 return combine2Packet16h(func(low), func(high)); \
48 }
49
50_EIGEN_GENERATE_FP16_MATH_FUNCTION(psin)
51_EIGEN_GENERATE_FP16_MATH_FUNCTION(pcos)
52_EIGEN_GENERATE_FP16_MATH_FUNCTION(plog)
53_EIGEN_GENERATE_FP16_MATH_FUNCTION(plog2)
54_EIGEN_GENERATE_FP16_MATH_FUNCTION(plog1p)
55_EIGEN_GENERATE_FP16_MATH_FUNCTION(pexp)
56_EIGEN_GENERATE_FP16_MATH_FUNCTION(pexpm1)
57_EIGEN_GENERATE_FP16_MATH_FUNCTION(pexp2)
58_EIGEN_GENERATE_FP16_MATH_FUNCTION(ptanh)
59#undef _EIGEN_GENERATE_FP16_MATH_FUNCTION
60
61// pfrexp
62template <>
63EIGEN_STRONG_INLINE Packet32h pfrexp<Packet32h>(const Packet32h& a, Packet32h& exponent) {
64 return pfrexp_generic(a, exponent);
65}
66
67// pldexp
68template <>
69EIGEN_STRONG_INLINE Packet32h pldexp<Packet32h>(const Packet32h& a, const Packet32h& exponent) {
70 // Clamped for the reasons at pldexp<Packet16f>; an int exponent beyond the range of half arrives as +-inf.
71 const Packet32h max_exponent = pset1<Packet32h>(Eigen::half(41.0f));
72 const Packet32h e = pmin(pmax(exponent, pnegate(max_exponent)), max_exponent);
73 return _mm512_scalef_ph(a, _mm512_roundscale_ph(e, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
74}
75
76} // end namespace internal
77} // end namespace Eigen
78
79#endif // EIGEN_MATH_FUNCTIONS_FP16_AVX512_H