Skip to content

[libclc] Move __clc_ldexp to CLC library #126078

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 1 commit into from
Feb 26, 2025
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
1 change: 1 addition & 0 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
spirv/lib/SOURCES;
# CLC internal libraries
clc/lib/generic/SOURCES;
clc/lib/amdgcn/SOURCES;
clc/lib/clspv/SOURCES;
clc/lib/spirv/SOURCES;
)
Expand Down
1 change: 0 additions & 1 deletion libclc/amdgcn/lib/SOURCES
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cl_khr_int64_extended_atomics/minmax_helpers.ll
math/fmax.cl
math/fmin.cl
math/ldexp.cl
mem_fence/fence.cl
synchronization/barrier.cl
workitem/get_global_offset.cl
Expand Down
7 changes: 7 additions & 0 deletions libclc/clc/include/clc/math/clc_ldexp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __CLC_MATH_CLC_LDEXP_H__
#define __CLC_MATH_CLC_LDEXP_H__

#define __CLC_BODY <clc/math/clc_ldexp.inc>
#include <clc/math/gentype.inc>

#endif // __CLC_MATH_CLC_LDEXP_H__
1 change: 1 addition & 0 deletions libclc/clc/include/clc/math/clc_ldexp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_CLC_DECL _CLC_OVERLOAD __CLC_GENTYPE __clc_ldexp(__CLC_GENTYPE, __CLC_INTN);
1 change: 1 addition & 0 deletions libclc/clc/lib/amdgcn/SOURCES
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
math/clc_ldexp_override.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,18 @@
* THE SOFTWARE.
*/

#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_ldexp.h>

#ifdef __HAS_LDEXPF__
#define BUILTINF __builtin_amdgcn_ldexpf
#else
#include "math/clc_ldexp.h"
#define BUILTINF __clc_ldexp
#endif

// This defines all the ldexp(floatN, intN) variants.
_CLC_DEFINE_BINARY_BUILTIN(float, ldexp, BUILTINF, float, int);
_CLC_DEFINE_BINARY_BUILTIN(float, __clc_ldexp, __builtin_amdgcn_ldexpf, float, int);
#endif

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
// This defines all the ldexp(doubleN, intN) variants.
_CLC_DEFINE_BINARY_BUILTIN(double, ldexp, __builtin_amdgcn_ldexp, double, int);
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
// This defines all the ldexp(doubleN, intN) variants.
_CLC_DEFINE_BINARY_BUILTIN(double, __clc_ldexp, __builtin_amdgcn_ldexp, double,
int);
#endif

// This defines all the ldexp(GENTYPE, int);
#define __CLC_BODY <../../../generic/lib/math/ldexp.inc>
#include <clc/math/gentype.inc>

#undef BUILTINF
1 change: 1 addition & 0 deletions libclc/clc/lib/generic/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ math/clc_fabs.cl
math/clc_fma.cl
math/clc_floor.cl
math/clc_frexp.cl
math/clc_ldexp.cl
math/clc_log.cl
math/clc_log10.cl
math/clc_log2.cl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
* THE SOFTWARE.
*/

#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/integer/clc_add_sat.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/relational/clc_isinf.h>
#include <clc/relational/clc_isnan.h>
#include <clc/shared/clc_clamp.h>

_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
#define _CLC_DEF_ldexp _CLC_DEF __attribute__((weak))

if (!__clc_fp32_subnormals_supported()) {
_CLC_DEF_ldexp _CLC_OVERLOAD float __clc_ldexp(float x, int n) {

if (!__clc_fp32_subnormals_supported()) {
// This treats subnormals as zeros
int i = as_int(x);
int i = __clc_as_int(x);
int e = (i >> 23) & 0xff;
int m = i & 0x007fffff;
int s = i & 0x80000000;
Expand All @@ -45,7 +46,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
mr = c ? m : mr;
int er = c ? e : v;
er = e ? er : e;
return as_float(s | (er << 23) | mr);
return __clc_as_float(s | (er << 23) | mr);
}

/* supports denormal values */
Expand All @@ -54,7 +55,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
uint val_ui;
uint sign;
int exponent;
val_ui = as_uint(x);
val_ui = __clc_as_uint(x);
sign = val_ui & 0x80000000;
val_ui = val_ui & 0x7fffffff; /* remove the sign bit */
int val_x = val_ui;
Expand All @@ -64,7 +65,9 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {

/* denormal support */
int fbh =
127 - (as_uint((float)(as_float(val_ui | 0x3f800000) - 1.0f)) >> 23);
127 -
(__clc_as_uint((float)(__clc_as_float(val_ui | 0x3f800000) - 1.0f)) >>
23);
int dexponent = 25 - fbh;
uint dval_ui = (((val_ui << fbh) & 0x007fffff) | (dexponent << 23));
int ex = dexponent + n - multiplier;
Expand All @@ -77,7 +80,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
dval_ui = dexponent > 254 ? 0x7f800000 : dval_ui; /*overflow*/
dval_ui = dexponent < -multiplier ? 0 : dval_ui; /*underflow*/
dval_ui = dval_ui | sign;
val_f = as_float(dval_ui);
val_f = __clc_as_float(dval_ui);

exponent += n;

Expand All @@ -91,22 +94,24 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
val_ui = val_ui | sign;

val_ui = dexp == 0 ? dval_ui : val_ui;
val_f = as_float(val_ui);
val_f = __clc_as_float(val_ui);

val_f = __clc_isnan(x) | __clc_isinf(x) | val_x == 0 ? x : val_f;
return val_f;
}

_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF_ldexp, float, __clc_ldexp, float, int);

#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
long l = as_ulong(x);
_CLC_DEF_ldexp _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
long l = __clc_as_ulong(x);
int e = (l >> 52) & 0x7ff;
long s = l & 0x8000000000000000;

ulong ux = as_ulong(x * 0x1.0p+53);
ulong ux = __clc_as_ulong(x * 0x1.0p+53);
int de = ((int)(ux >> 52) & 0x7ff) - 53;
int c = e == 0;
e = c ? de : e;
Expand All @@ -118,28 +123,30 @@ _CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {

ux &= ~EXPBITS_DP64;

double mr = as_double(ux | ((ulong)(v + 53) << 52));
double mr = __clc_as_double(ux | ((ulong)(v + 53) << 52));
mr = mr * 0x1.0p-53;

mr = v > 0 ? as_double(ux | ((ulong)v << 52)) : mr;
mr = v > 0 ? __clc_as_double(ux | ((ulong)v << 52)) : mr;

mr = v == 0x7ff ? as_double(s | PINFBITPATT_DP64) : mr;
mr = v < -53 ? as_double(s) : mr;
mr = v == 0x7ff ? __clc_as_double(s | PINFBITPATT_DP64) : mr;
mr = v < -53 ? __clc_as_double(s) : mr;

mr = ((n == 0) | __clc_isinf(x) | (x == 0)) ? x : mr;
return mr;
}

_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF_ldexp, double, __clc_ldexp, double, int);

#endif

#ifdef cl_khr_fp16

#pragma OPENCL EXTENSION cl_khr_fp16 : enable

_CLC_OVERLOAD _CLC_DEF half __clc_ldexp(half x, int n) {
_CLC_OVERLOAD _CLC_DEF_ldexp half __clc_ldexp(half x, int n) {
return (half)__clc_ldexp((float)x, n);
}

_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_ldexp, half, int);
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF_ldexp, half, __clc_ldexp, half, int);

#endif
1 change: 0 additions & 1 deletion libclc/clspv/lib/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ subnormal_config.cl
../../generic/lib/math/clc_exp10.cl
../../generic/lib/math/clc_fmod.cl
../../generic/lib/math/clc_hypot.cl
../../generic/lib/math/clc_ldexp.cl
../../generic/lib/math/clc_pow.cl
../../generic/lib/math/clc_pown.cl
../../generic/lib/math/clc_powr.cl
Expand Down
13 changes: 0 additions & 13 deletions libclc/generic/include/math/clc_ldexp.h

This file was deleted.

1 change: 0 additions & 1 deletion libclc/generic/lib/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ math/half_tan.cl
math/clc_hypot.cl
math/hypot.cl
math/ilogb.cl
math/clc_ldexp.cl
math/ldexp.cl
math/lgamma.cl
math/lgamma_r.cl
Expand Down
12 changes: 6 additions & 6 deletions libclc/generic/lib/math/ldexp.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
* THE SOFTWARE.
*/

#include "math/clc_ldexp.h"
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/math.h>
#include <clc/math/clc_ldexp.h>

_CLC_DEFINE_BINARY_BUILTIN(float, ldexp, __clc_ldexp, float, int)
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(float, ldexp, __clc_ldexp, float, int)

#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEFINE_BINARY_BUILTIN(double, ldexp, __clc_ldexp, double, int)
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(double, ldexp, __clc_ldexp, double, int)

#endif

#ifdef cl_khr_fp16

#pragma OPENCL EXTENSION cl_khr_fp16 : enable

_CLC_DEFINE_BINARY_BUILTIN(half, ldexp, __clc_ldexp, half, int)
_CLC_DEFINE_BINARY_BUILTIN_NO_SCALARIZE(half, ldexp, __clc_ldexp, half, int)

#endif

// This defines all the ldexp(GENTYPE, int) variants
Expand Down
7 changes: 1 addition & 6 deletions libclc/generic/lib/math/ldexp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@
* THE SOFTWARE.
*/

// TODO: Enable half precision when ldexp is implemented.
#if __CLC_FPSIZE > 16

#ifndef __CLC_SCALAR

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE ldexp(__CLC_GENTYPE x, int n) {
return ldexp(x, (__CLC_INTN)n);
return __clc_ldexp(x, (__CLC_INTN)n);
}

#endif

#endif
1 change: 0 additions & 1 deletion libclc/spirv/lib/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ math/fma.cl
../../generic/lib/math/clc_hypot.cl
../../generic/lib/math/hypot.cl
../../generic/lib/math/ilogb.cl
../../generic/lib/math/clc_ldexp.cl
../../generic/lib/math/ldexp.cl
../../generic/lib/math/lgamma.cl
../../generic/lib/math/lgamma_r.cl
Expand Down