Skip to content

[ESIMD] Revise log/exp implementation to be consistent with std/sycl #5211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions sycl/include/sycl/ext/intel/experimental/esimd/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ ESIMD_NODEBUG
////////////////////////////////////////////////////////////////////////////////
// ESIMD arithmetic intrinsics:
//
// inv, log, exp, sqrt, rsqrt, sin, cos
// inv, log2, exp2, sqrt, rsqrt, sin, cos
//
// share the same requirements.
//
Expand Down Expand Up @@ -1427,8 +1427,8 @@ ESIMD_NODEBUG
}

ESIMD_INTRINSIC_DEF(float, inv, inv)
ESIMD_INTRINSIC_DEF(float, log, log)
ESIMD_INTRINSIC_DEF(float, exp, exp)
ESIMD_INTRINSIC_DEF(float, log2, log)
ESIMD_INTRINSIC_DEF(float, exp2, exp)
ESIMD_INTRINSIC_DEF(float, sqrt, sqrt)
ESIMD_INTRINSIC_DEF(float, ieee_sqrt, sqrt_ieee)
ESIMD_INTRINSIC_DEF(float, rsqrt, rsqrt)
Expand Down Expand Up @@ -1592,6 +1592,41 @@ asin(T src0, int flag = saturation_off) {
return Result[0];
}

/// Computes the natural logarithm of the given argument. This is an
/// emulated version based on the H/W supported log2.
/// @param the source operand to compute base-e logarithm of.
/// @return the base-e logarithm of \p src0.
template <int SZ>
ESIMD_NODEBUG ESIMD_INLINE simd<float, SZ> log(simd<float, SZ> src0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a doc comment that this is s/w-emulated version based on h/w supported log2

int flag = saturation_off) {
constexpr float ln2 = 0.69314718f; // std::numbers::ln2_v<float> in c++20
simd<float, SZ> Result = esimd::log2<SZ>(src0) * ln2;

if (flag != saturation_on)
return Result;

return esimd::saturate<float>(Result);
}

ESIMD_NODEBUG ESIMD_INLINE float log(float src0, int flag = saturation_off) {
return esimd::log<1>(src0, flag)[0];
}

/// Computes e raised to the power of the given argument. This is an
/// emulated version based on the H/W supported exp2.
/// @param the source operand to compute base-e exponential of.
/// @return e raised to the power of \p src0.
template <int SZ>
ESIMD_NODEBUG ESIMD_INLINE simd<float, SZ> exp(simd<float, SZ> src0,
int flag = saturation_off) {
constexpr float log2e = 1.442695f; // std::numbers::log2e_v<float> in c++20
return esimd::exp2<SZ>(src0 * log2e, flag);
}

ESIMD_NODEBUG ESIMD_INLINE float exp(float src0, int flag = saturation_off) {
return esimd::exp<1>(src0, flag)[0];
}

////////////////////////////////////////////////////////////////////////////////
// Rounding intrinsics.
////////////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 14 additions & 2 deletions sycl/test/esimd/math_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SYCL_ESIMD_FUNCTION SYCL_EXTERNAL simd<float, 16> sycl_math(simd<float, 16> x) {
return v;
}

// Math sin,cos,log,exp functions from esimd namespace are translated
// Math sin,cos,log2,exp2 functions from esimd namespace are translated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add E2E test link

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// into vector __esimd_ calls, which later translate into GenX intrinsics.
SYCL_ESIMD_FUNCTION SYCL_EXTERNAL simd<float, 16>
esimd_math(simd<float, 16> x) {
Expand All @@ -35,7 +35,19 @@ esimd_math(simd<float, 16> x) {
//CHECK: call spir_func <16 x float> @_Z11__esimd_sin
v = esimd::sin(v);
//CHECK: call spir_func <16 x float> @_Z11__esimd_log
v = esimd::log(v);
v = esimd::log2(v);
//CHECK: call spir_func <16 x float> @_Z11__esimd_exp
v = esimd::exp2(v);
return v;
}

// Math log,exp functions from esimd namespace are emulated with
// __esimd_ log/exp calls, which later translate into GenX intrinsics.
SYCL_ESIMD_FUNCTION SYCL_EXTERNAL simd<float, 16>
esimd_math_emu(simd<float, 16> x) {
simd<float, 16> v = 0;
//CHECK: call spir_func <16 x float> @_Z11__esimd_log
v = esimd::log(x);
//CHECK: call spir_func <16 x float> @_Z11__esimd_exp
v = esimd::exp(v);
return v;
Expand Down