Skip to content

Commit 1e05d93

Browse files
alexeyvoronov-intelbader
authored andcommitted
[SYCL] Update lgamma_r host implementation to independent of a system.
::lgamma_r is not standard C++ math functions and may not be supported in some compilers/OS so it was replaced. Signed-off-by: Alexey Voronov <[email protected]>
1 parent 610143c commit 1e05d93

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

sycl/source/detail/builtins.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ template <typename T> T inline __fract(T x, T *iptr) {
409409
return std::fmin(x - f, nextafter(T(1.0), T(0.0)));
410410
}
411411

412+
template <typename T> inline T __lgamma_r(T x, s::cl_int *signp) {
413+
T g = std::tgamma(x);
414+
*signp = std::signbit(g) ? -1 : 1;
415+
return std::log(std::abs(g));
416+
}
417+
412418
template <typename T> inline T __mad(T a, T b, T c) { return (a * b) + c; }
413419

414420
template <typename T> inline T __maxmag(T x, T y) {
@@ -1167,13 +1173,13 @@ MAKE_1V(lgamma, s::cl_half, s::cl_half)
11671173

11681174
// lgamma_r
11691175
cl_float lgamma_r(s::cl_float x, s::cl_int *signp) __NOEXC {
1170-
return ::lgamma_r(x, signp);
1176+
return __lgamma_r(x, signp);
11711177
}
11721178
cl_double lgamma_r(s::cl_double x, s::cl_int *signp) __NOEXC {
1173-
return ::lgamma_r(x, signp);
1179+
return __lgamma_r(x, signp);
11741180
}
11751181
cl_half lgamma_r(s::cl_half x, s::cl_int *signp) __NOEXC {
1176-
return ::lgamma_r(x, signp);
1182+
return __lgamma_r(x, signp);
11771183
}
11781184
MAKE_1V_2P(lgamma_r, s::cl_float, s::cl_float, s::cl_int)
11791185
MAKE_1V_2P(lgamma_r, s::cl_double, s::cl_double, s::cl_int)

sycl/test/built-ins/scalar_math.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,86 @@ int main() {
540540
assert(i == 1.0f);
541541
}
542542

543+
// lgamma with private memory
544+
{
545+
s::cl_float r{ 0 };
546+
{
547+
s::buffer<s::cl_float, 1> BufR(&r, s::range<1>(1));
548+
s::queue myQueue;
549+
myQueue.submit([&](s::handler &cgh) {
550+
auto AccR = BufR.get_access<s::access::mode::read_write>(cgh);
551+
cgh.single_task<class lgammaF1>([=]() {
552+
AccR[0] = s::lgamma(s::cl_float{ 10.f });
553+
});
554+
});
555+
}
556+
assert(r > 12.8017f && r < 12.8019f); // ~12.8018
557+
}
558+
559+
// lgamma with private memory
560+
{
561+
s::cl_float r{ 0 };
562+
{
563+
s::buffer<s::cl_float, 1> BufR(&r, s::range<1>(1));
564+
s::queue myQueue;
565+
myQueue.submit([&](s::handler &cgh) {
566+
auto AccR = BufR.get_access<s::access::mode::read_write>(cgh);
567+
cgh.single_task<class lgammaF1_neg>([=]() {
568+
AccR[0] = s::lgamma(s::cl_float{ -2.4f });
569+
});
570+
});
571+
}
572+
assert(r > 0.1024f && r < 0.1026f); // ~0.102583
573+
}
574+
575+
// lgamma_r with private memory
576+
{
577+
s::cl_float r{ 0 };
578+
s::cl_int i{ 999 };
579+
{
580+
s::buffer<s::cl_float, 1> BufR(&r, s::range<1>(1));
581+
s::buffer<s::cl_int, 1> BufI(&i, s::range<1>(1),
582+
{ s::property::buffer::use_host_ptr() });
583+
s::queue myQueue;
584+
myQueue.submit([&](s::handler &cgh) {
585+
auto AccR = BufR.get_access<s::access::mode::read_write>(cgh);
586+
auto AccI = BufI.get_access<s::access::mode::read_write>(cgh);
587+
cgh.single_task<class lgamma_rF1PI1>([=]() {
588+
s::cl_int temp(0.0);
589+
s::private_ptr<s::cl_int> Iptr(&temp);
590+
AccR[0] = s::lgamma_r(s::cl_float{ 10.f }, Iptr);
591+
AccI[0] = *Iptr;
592+
});
593+
});
594+
}
595+
assert(r > 12.8017f && r < 12.8019f); // ~12.8018
596+
assert(i == 1); // tgamma of 10 is ~362880.0
597+
}
598+
599+
// lgamma_r with private memory
600+
{
601+
s::cl_float r{ 0 };
602+
s::cl_int i{ 999 };
603+
{
604+
s::buffer<s::cl_float, 1> BufR(&r, s::range<1>(1));
605+
s::buffer<s::cl_int, 1> BufI(&i, s::range<1>(1),
606+
{ s::property::buffer::use_host_ptr() });
607+
s::queue myQueue;
608+
myQueue.submit([&](s::handler &cgh) {
609+
auto AccR = BufR.get_access<s::access::mode::read_write>(cgh);
610+
auto AccI = BufI.get_access<s::access::mode::read_write>(cgh);
611+
cgh.single_task<class lgamma_rF1PI1_neg>([=]() {
612+
s::cl_int temp(0.0);
613+
s::private_ptr<s::cl_int> Iptr(&temp);
614+
AccR[0] = s::lgamma_r(s::cl_float{ -2.4f }, Iptr);
615+
AccI[0] = *Iptr;
616+
});
617+
});
618+
}
619+
assert(r > 0.1024f && r < 0.1026f); // ~0.102583
620+
assert(i == -1); // tgamma of -2.4 is ~-1.1080299470333461
621+
}
622+
543623
// nan
544624
{
545625
s::cl_double r{ 0 };

sycl/test/built-ins/vector_math.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,57 @@ int main() {
150150
assert(i2 == 2.0f);
151151
}
152152

153+
// lgamma with private memory
154+
{
155+
s::cl_float2 r{ 0, 0 };
156+
{
157+
s::buffer<s::cl_float2, 1> BufR(&r, s::range<1>(1));
158+
s::queue myQueue;
159+
myQueue.submit([&](s::handler &cgh) {
160+
auto AccR = BufR.get_access<s::access::mode::read_write>(cgh);
161+
cgh.single_task<class lgamma_rF2>([=]() {
162+
AccR[0] = s::lgamma(s::cl_float2{ 10.f, -2.4f });
163+
});
164+
});
165+
}
166+
167+
s::cl_float r1 = r.x();
168+
s::cl_float r2 = r.y();
169+
170+
assert(r1 > 12.8017f && r1 < 12.8019f); // ~12.8018
171+
assert(r2 > 0.1024f && r2 < 0.1026f); // ~0.102583
172+
}
173+
174+
// lgamma_r with private memory
175+
{
176+
s::cl_float2 r{ 0, 0 };
177+
s::cl_int2 i{ 0, 0 };
178+
{
179+
s::buffer<s::cl_float2, 1> BufR(&r, s::range<1>(1));
180+
s::buffer<s::cl_int2, 1> BufI(&i, s::range<1>(1));
181+
s::queue myQueue;
182+
myQueue.submit([&](s::handler &cgh) {
183+
auto AccR = BufR.get_access<s::access::mode::read_write>(cgh);
184+
auto AccI = BufI.get_access<s::access::mode::read_write>(cgh);
185+
cgh.single_task<class lgamma_rF2PF2>([=]() {
186+
s::cl_int2 temp(0.0);
187+
s::private_ptr<s::cl_int2> Iptr(&temp);
188+
AccR[0] = s::lgamma_r(s::cl_float2{ 10.f, -2.4f }, Iptr);
189+
AccI[0] = *Iptr;
190+
});
191+
});
192+
}
193+
194+
s::cl_float r1 = r.x();
195+
s::cl_float r2 = r.y();
196+
s::cl_int i1 = i.x();
197+
s::cl_int i2 = i.y();
198+
199+
assert(r1 > 12.8017f && r1 < 12.8019f); // ~12.8018
200+
assert(r2 > 0.1024f && r2 < 0.1026f); // ~0.102583
201+
assert(i1 == 1); // tgamma of 10 is ~362880.0
202+
assert(i2 == -1); // tgamma of -2.4 is ~-1.1080299470333461
203+
}
204+
153205
return 0;
154206
}

0 commit comments

Comments
 (0)