Skip to content

Commit ecfdaa1

Browse files
authored
[ESIMD] Revise log/exp implementation to be consistent with std/sycl (#5211)
- Rename esimd::log/exp to esimd::log2/exp2 since they operate with base 2 - Add esimd::log/exp functions emulated via esimd::log2/exp2 Signed-off-by: Sergey Dmitriev <[email protected]>
1 parent 7b7e044 commit ecfdaa1

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

sycl/include/sycl/ext/intel/experimental/esimd/math.hpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ ESIMD_NODEBUG
13791379
////////////////////////////////////////////////////////////////////////////////
13801380
// ESIMD arithmetic intrinsics:
13811381
//
1382-
// inv, log, exp, sqrt, rsqrt, sin, cos
1382+
// inv, log2, exp2, sqrt, rsqrt, sin, cos
13831383
//
13841384
// share the same requirements.
13851385
//
@@ -1427,8 +1427,8 @@ ESIMD_NODEBUG
14271427
}
14281428

14291429
ESIMD_INTRINSIC_DEF(float, inv, inv)
1430-
ESIMD_INTRINSIC_DEF(float, log, log)
1431-
ESIMD_INTRINSIC_DEF(float, exp, exp)
1430+
ESIMD_INTRINSIC_DEF(float, log2, log)
1431+
ESIMD_INTRINSIC_DEF(float, exp2, exp)
14321432
ESIMD_INTRINSIC_DEF(float, sqrt, sqrt)
14331433
ESIMD_INTRINSIC_DEF(float, ieee_sqrt, sqrt_ieee)
14341434
ESIMD_INTRINSIC_DEF(float, rsqrt, rsqrt)
@@ -1592,6 +1592,41 @@ asin(T src0, int flag = saturation_off) {
15921592
return Result[0];
15931593
}
15941594

1595+
/// Computes the natural logarithm of the given argument. This is an
1596+
/// emulated version based on the H/W supported log2.
1597+
/// @param the source operand to compute base-e logarithm of.
1598+
/// @return the base-e logarithm of \p src0.
1599+
template <int SZ>
1600+
ESIMD_NODEBUG ESIMD_INLINE simd<float, SZ> log(simd<float, SZ> src0,
1601+
int flag = saturation_off) {
1602+
constexpr float ln2 = 0.69314718f; // std::numbers::ln2_v<float> in c++20
1603+
simd<float, SZ> Result = esimd::log2<SZ>(src0) * ln2;
1604+
1605+
if (flag != saturation_on)
1606+
return Result;
1607+
1608+
return esimd::saturate<float>(Result);
1609+
}
1610+
1611+
ESIMD_NODEBUG ESIMD_INLINE float log(float src0, int flag = saturation_off) {
1612+
return esimd::log<1>(src0, flag)[0];
1613+
}
1614+
1615+
/// Computes e raised to the power of the given argument. This is an
1616+
/// emulated version based on the H/W supported exp2.
1617+
/// @param the source operand to compute base-e exponential of.
1618+
/// @return e raised to the power of \p src0.
1619+
template <int SZ>
1620+
ESIMD_NODEBUG ESIMD_INLINE simd<float, SZ> exp(simd<float, SZ> src0,
1621+
int flag = saturation_off) {
1622+
constexpr float log2e = 1.442695f; // std::numbers::log2e_v<float> in c++20
1623+
return esimd::exp2<SZ>(src0 * log2e, flag);
1624+
}
1625+
1626+
ESIMD_NODEBUG ESIMD_INLINE float exp(float src0, int flag = saturation_off) {
1627+
return esimd::exp<1>(src0, flag)[0];
1628+
}
1629+
15951630
////////////////////////////////////////////////////////////////////////////////
15961631
// Rounding intrinsics.
15971632
////////////////////////////////////////////////////////////////////////////////

sycl/test/esimd/math_impl.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SYCL_ESIMD_FUNCTION SYCL_EXTERNAL simd<float, 16> sycl_math(simd<float, 16> x) {
2525
return v;
2626
}
2727

28-
// Math sin,cos,log,exp functions from esimd namespace are translated
28+
// Math sin,cos,log2,exp2 functions from esimd namespace are translated
2929
// into vector __esimd_ calls, which later translate into GenX intrinsics.
3030
SYCL_ESIMD_FUNCTION SYCL_EXTERNAL simd<float, 16>
3131
esimd_math(simd<float, 16> x) {
@@ -35,7 +35,19 @@ esimd_math(simd<float, 16> x) {
3535
//CHECK: call spir_func <16 x float> @_Z11__esimd_sin
3636
v = esimd::sin(v);
3737
//CHECK: call spir_func <16 x float> @_Z11__esimd_log
38-
v = esimd::log(v);
38+
v = esimd::log2(v);
39+
//CHECK: call spir_func <16 x float> @_Z11__esimd_exp
40+
v = esimd::exp2(v);
41+
return v;
42+
}
43+
44+
// Math log,exp functions from esimd namespace are emulated with
45+
// __esimd_ log/exp calls, which later translate into GenX intrinsics.
46+
SYCL_ESIMD_FUNCTION SYCL_EXTERNAL simd<float, 16>
47+
esimd_math_emu(simd<float, 16> x) {
48+
simd<float, 16> v = 0;
49+
//CHECK: call spir_func <16 x float> @_Z11__esimd_log
50+
v = esimd::log(x);
3951
//CHECK: call spir_func <16 x float> @_Z11__esimd_exp
4052
v = esimd::exp(v);
4153
return v;

0 commit comments

Comments
 (0)