Skip to content

[libclc] Improve nextafter behaviour around zero #127469

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 2 commits into from
Feb 19, 2025
Merged
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
36 changes: 23 additions & 13 deletions libclc/clc/lib/generic/math/clc_nextafter.cl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <clc/clcmacro.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_fabs.h>
#include <clc/relational/clc_isnan.h>

// This file provides OpenCL C implementations of __clc_nextafter for
Expand All @@ -12,21 +13,30 @@
FLOAT_TYPE y) { \
const UINT_TYPE sign_bit = (UINT_TYPE)1 \
<< (sizeof(INT_TYPE_SCALAR) * 8 - 1); \
const UINT_TYPE sign_bit_mask = sign_bit - (UINT_TYPE)1; \
INT_TYPE ix = CLC_AS_TYPE(INT_TYPE)(x); \
UINT_TYPE ax = CLC_AS_TYPE(UINT_TYPE)(ix) & sign_bit_mask; \
INT_TYPE mx = CLC_AS_TYPE(INT_TYPE)(sign_bit) - ix; \
mx = CLC_AS_TYPE(INT_TYPE)(ix) < (INT_TYPE)0 ? mx : ix; \
INT_TYPE iy = CLC_AS_TYPE(INT_TYPE)(y); \
UINT_TYPE ay = CLC_AS_TYPE(UINT_TYPE)(iy) & sign_bit_mask; \
INT_TYPE my = CLC_AS_TYPE(INT_TYPE)(sign_bit) - iy; \
my = iy < (INT_TYPE)0 ? my : iy; \
UINT_TYPE ix = CLC_AS_TYPE(UINT_TYPE)(x); \
FLOAT_TYPE absx = __clc_fabs(x); \
UINT_TYPE mxu = sign_bit - ix; \
INT_TYPE mx = CLC_AS_TYPE(INT_TYPE)(mxu); \
mx = CLC_AS_TYPE(INT_TYPE)(ix) < (INT_TYPE)0 ? mx \
: CLC_AS_TYPE(INT_TYPE)(ix); \
UINT_TYPE iy = CLC_AS_TYPE(UINT_TYPE)(y); \
FLOAT_TYPE absy = __clc_fabs(y); \
UINT_TYPE myu = sign_bit - iy; \
INT_TYPE my = CLC_AS_TYPE(INT_TYPE)(myu); \
my = CLC_AS_TYPE(INT_TYPE)(iy) < (INT_TYPE)0 ? my \
: CLC_AS_TYPE(INT_TYPE)(iy); \
INT_TYPE t = mx + (mx < my ? (INT_TYPE)1 : (INT_TYPE)-1); \
INT_TYPE r = CLC_AS_TYPE(INT_TYPE)(sign_bit) - t; \
r = t < (INT_TYPE)0 ? r : t; \
UINT_TYPE r = sign_bit - CLC_AS_TYPE(UINT_TYPE)(t); \
r = (t < (INT_TYPE)0 || (t == (INT_TYPE)0 && mx < my)) \
? r \
: CLC_AS_TYPE(UINT_TYPE)(t); \
r = __clc_isnan(x) ? ix : r; \
r = __clc_isnan(y) ? CLC_AS_TYPE(INT_TYPE)(iy) : r; \
r = ((ax | ay) == (UINT_TYPE)0 || ix == iy) ? iy : r; \
r = __clc_isnan(y) ? iy : r; \
r = ((CLC_AS_TYPE(UINT_TYPE)(absx) | CLC_AS_TYPE(UINT_TYPE)(absy)) == \
(UINT_TYPE)0 || \
ix == iy) \
? iy \
: r; \
return CLC_AS_TYPE(FLOAT_TYPE)(r); \
}

Expand Down