Skip to content

Commit 96309bf

Browse files
aratajewigcbot
authored andcommitted
Fix edge case behavior of lgamma_r
`lgamma_r` function computes the natural logarithm of the absolute value of the gamma function. The sign of the gamma function is returned in the signp argument of `lgamma_r`. According to the C99 specification, the gamma function should behave as follows: - If the argument is a negative integer, NaN is returned - If the argument is ±0, ±∞ is returned The current implementation in IGC adheres to these specifications but incorrectly calculates the `signp` argument when the input is ±0. OpenCL C specification introduces "Additional Requirements Beyond C99": - lgamma_r(x, signp) returns 0 in signp if x is zero or a negative integer. Without this change, IGC incorrectly calls the `__spirv_ocl_sign` built-in function with the result of `gamma(±0)`, which is ±∞. This results in a `signp` value of ±1, which does not conform to the OpenCL C specification requirement.
1 parent 476925c commit 96309bf

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

IGC/BiFModule/Implementation/Math/lgamma_r.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ INLINE float SPIRV_OVERLOADABLE SPIRV_OCL_BUILTIN(lgamma_r, _f32_p0i32, )( float
131131
{
132132
float g = SPIRV_OCL_BUILTIN(tgamma, _f32, )(x);
133133
r = __intel_relaxed_isnan(g) ? INFINITY : SPIRV_OCL_BUILTIN(native_log, _f32, )(SPIRV_OCL_BUILTIN(fabs, _f32, )(g));
134-
s = SPIRV_OCL_BUILTIN(sign, _f32, )(g);
134+
s = (x == 0) ? 0 : SPIRV_OCL_BUILTIN(sign, _f32, )(g);
135135
}
136136
signp[0] = s;
137137
return r;

0 commit comments

Comments
 (0)