Skip to content

[libclc] Move ceil/fabs/floor/rint/trunc to CLC library #114774

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
Nov 4, 2024
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
19 changes: 19 additions & 0 deletions libclc/clc/include/clc/math/clc_ceil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __CLC_MATH_CLC_CEIL_H__
#define __CLC_MATH_CLC_CEIL_H__

#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible ceil
#define __clc_ceil ceil
#else

// Map the function to an LLVM intrinsic
#define __CLC_FUNCTION __clc_ceil
#define __CLC_INTRINSIC "llvm.ceil"
#include <clc/math/unary_intrin.inc>

#undef __CLC_INTRINSIC
#undef __CLC_FUNCTION

#endif

#endif // __CLC_MATH_CLC_CEIL_H__
19 changes: 19 additions & 0 deletions libclc/clc/include/clc/math/clc_fabs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __CLC_MATH_CLC_FABS_H__
#define __CLC_MATH_CLC_FABS_H__

#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible fabs
#define __clc_fabs fabs
#else

// Map the function to an LLVM intrinsic
#define __CLC_FUNCTION __clc_fabs
#define __CLC_INTRINSIC "llvm.fabs"
#include <clc/math/unary_intrin.inc>

#undef __CLC_INTRINSIC
#undef __CLC_FUNCTION

#endif

#endif // __CLC_MATH_CLC_FABS_H__
19 changes: 19 additions & 0 deletions libclc/clc/include/clc/math/clc_floor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __CLC_MATH_CLC_FLOOR_H__
#define __CLC_MATH_CLC_FLOOR_H__

#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible floor
#define __clc_floor floor
#else

// Map the function to an LLVM intrinsic
#define __CLC_FUNCTION __clc_floor
#define __CLC_INTRINSIC "llvm.floor"
#include <clc/math/unary_intrin.inc>

#undef __CLC_INTRINSIC
#undef __CLC_FUNCTION

#endif

#endif // __CLC_MATH_CLC_FLOOR_H__
19 changes: 19 additions & 0 deletions libclc/clc/include/clc/math/clc_rint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __CLC_MATH_CLC_RINT_H__
#define __CLC_MATH_CLC_RINT_H__

#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible rint
#define __clc_rint rint
#else

// Map the function to an LLVM intrinsic
#define __CLC_FUNCTION __clc_rint
#define __CLC_INTRINSIC "llvm.rint"
#include <clc/math/unary_intrin.inc>

#undef __CLC_INTRINSIC
#undef __CLC_FUNCTION

#endif

#endif // __CLC_MATH_CLC_RINT_H__
19 changes: 19 additions & 0 deletions libclc/clc/include/clc/math/clc_trunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __CLC_MATH_CLC_TRUNC_H__
#define __CLC_MATH_CLC_TRUNC_H__

#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible trunc
#define __clc_trunc trunc
#else

// Map the function to an LLVM intrinsic
#define __CLC_FUNCTION __clc_trunc
#define __CLC_INTRINSIC "llvm.trunc"
#include <clc/math/unary_intrin.inc>

#undef __CLC_INTRINSIC
#undef __CLC_FUNCTION

#endif

#endif // __CLC_MATH_CLC_TRUNC_H__
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ _CLC_OVERLOAD float2 __CLC_FUNCTION(float2 f) __asm(__CLC_INTRINSIC ".v2f32");
_CLC_OVERLOAD float3 __CLC_FUNCTION(float3 f) __asm(__CLC_INTRINSIC ".v3f32");
_CLC_OVERLOAD float4 __CLC_FUNCTION(float4 f) __asm(__CLC_INTRINSIC ".v4f32");
_CLC_OVERLOAD float8 __CLC_FUNCTION(float8 f) __asm(__CLC_INTRINSIC ".v8f32");
_CLC_OVERLOAD float16 __CLC_FUNCTION(float16 f) __asm(__CLC_INTRINSIC ".v16f32");
_CLC_OVERLOAD float16 __CLC_FUNCTION(float16 f) __asm(__CLC_INTRINSIC
".v16f32");

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
Expand All @@ -12,11 +13,12 @@ _CLC_OVERLOAD double2 __CLC_FUNCTION(double2 d) __asm(__CLC_INTRINSIC ".v2f64");
_CLC_OVERLOAD double3 __CLC_FUNCTION(double3 d) __asm(__CLC_INTRINSIC ".v3f64");
_CLC_OVERLOAD double4 __CLC_FUNCTION(double4 d) __asm(__CLC_INTRINSIC ".v4f64");
_CLC_OVERLOAD double8 __CLC_FUNCTION(double8 d) __asm(__CLC_INTRINSIC ".v8f64");
_CLC_OVERLOAD double16 __CLC_FUNCTION(double16 d) __asm(__CLC_INTRINSIC ".v16f64");
_CLC_OVERLOAD double16 __CLC_FUNCTION(double16 d) __asm(__CLC_INTRINSIC
".v16f64");
#endif

#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16: enable
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
_CLC_OVERLOAD half __CLC_FUNCTION(half d) __asm(__CLC_INTRINSIC ".f16");
_CLC_OVERLOAD half2 __CLC_FUNCTION(half2 d) __asm(__CLC_INTRINSIC ".v2f16");
_CLC_OVERLOAD half3 __CLC_FUNCTION(half3 d) __asm(__CLC_INTRINSIC ".v3f16");
Expand Down
10 changes: 10 additions & 0 deletions libclc/clc/include/clc/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __CLC_UTILS_H__
#define __CLC_UTILS_H__

#define __CLC_CONCAT(x, y) x##y
#define __CLC_XCONCAT(x, y) __CLC_CONCAT(x, y)

#define __CLC_STR(x) #x
#define __CLC_XSTR(x) __CLC_STR(x)

#endif // __CLC_UTILS_H__
1 change: 1 addition & 0 deletions libclc/generic/lib/clcmacro.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <clc/clc.h>
#include <utils.h>

#define _CLC_UNARY_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE) \
Expand Down
8 changes: 2 additions & 6 deletions libclc/generic/lib/math/ceil.cl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <clc/clc.h>
#include "../clcmacro.h"

// Map the llvm intrinsic to an OpenCL function.
#define __CLC_FUNCTION __clc_ceil
#define __CLC_INTRINSIC "llvm.ceil"
#include "math/unary_intrin.inc"
#include <clc/clc.h>
#include <clc/math/clc_ceil.h>

#undef __CLC_FUNCTION
#define __CLC_FUNCTION ceil
Expand Down
6 changes: 4 additions & 2 deletions libclc/generic/lib/math/clc_fmod.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_floor.h>
#include <clc/math/clc_trunc.h>

#include <math/clc_remainder.h>
#include "../clcmacro.h"
Expand Down Expand Up @@ -119,7 +121,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y)

for (i = 0; i < ntimes; i++) {
// Compute integral multiplier
t = trunc(dx / w);
t = __clc_trunc(dx / w);

// Compute w * t in quad precision
p = w * t;
Expand All @@ -138,7 +140,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y)

// One more time
// Variable todd says whether the integer t is odd or not
t = floor(dx / w);
t = __clc_floor(dx / w);
long lt = (long)t;
int todd = lt & 1;

Expand Down
3 changes: 2 additions & 1 deletion libclc/generic/lib/math/clc_pow.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_fabs.h>

#include "config.h"
#include "math.h"
Expand Down Expand Up @@ -80,7 +81,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_pow(float x, float y)
* First handle case that x is close to 1
*/
float r = 1.0f - as_float(ax);
int near1 = fabs(r) < 0x1.0p-4f;
int near1 = __clc_fabs(r) < 0x1.0p-4f;
float r2 = r*r;

/* Coefficients are just 1/3, 1/4, 1/5 and 1/6 */
Expand Down
3 changes: 2 additions & 1 deletion libclc/generic/lib/math/clc_pown.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_fabs.h>

#include "config.h"
#include "math.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_pown(float x, int ny)
// Extra precise log calculation
// First handle case that x is close to 1
float r = 1.0f - as_float(ax);
int near1 = fabs(r) < 0x1.0p-4f;
int near1 = __clc_fabs(r) < 0x1.0p-4f;
float r2 = r*r;

// Coefficients are just 1/3, 1/4, 1/5 and 1/6
Expand Down
3 changes: 2 additions & 1 deletion libclc/generic/lib/math/clc_powr.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_fabs.h>

#include "config.h"
#include "math.h"
Expand Down Expand Up @@ -76,7 +77,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_powr(float x, float y)
// Extra precise log calculation
// First handle case that x is close to 1
float r = 1.0f - as_float(ax);
int near1 = fabs(r) < 0x1.0p-4f;
int near1 = __clc_fabs(r) < 0x1.0p-4f;
float r2 = r*r;

// Coefficients are just 1/3, 1/4, 1/5 and 1/6
Expand Down
6 changes: 4 additions & 2 deletions libclc/generic/lib/math/clc_remainder.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_floor.h>
#include <clc/math/clc_trunc.h>

#include <math/clc_remainder.h>
#include "../clcmacro.h"
Expand Down Expand Up @@ -129,7 +131,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y)

for (i = 0; i < ntimes; i++) {
// Compute integral multiplier
t = trunc(dx / w);
t = __clc_trunc(dx / w);

// Compute w * t in quad precision
p = w * t;
Expand All @@ -148,7 +150,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y)

// One more time
// Variable todd says whether the integer t is odd or not
t = floor(dx / w);
t = __clc_floor(dx / w);
long lt = (long)t;
int todd = lt & 1;

Expand Down
6 changes: 4 additions & 2 deletions libclc/generic/lib/math/clc_remquo.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_floor.h>
#include <clc/math/clc_trunc.h>

#include <math/clc_remainder.h>
#include "../clcmacro.h"
Expand Down Expand Up @@ -154,7 +156,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y, __private int *pq

for (i = 0; i < ntimes; i++) {
// Compute integral multiplier
t = trunc(dx / w);
t = __clc_trunc(dx / w);

// Compute w * t in quad precision
p = w * t;
Expand All @@ -173,7 +175,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y, __private int *pq

// One more time
// Variable todd says whether the integer t is odd or not
t = floor(dx / w);
t = __clc_floor(dx / w);
long lt = (long)t;
int todd = lt & 1;

Expand Down
3 changes: 2 additions & 1 deletion libclc/generic/lib/math/clc_rootn.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include <clc/clc.h>
#include <clc/math/clc_fabs.h>

#include "config.h"
#include "math.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_rootn(float x, int ny)
// Extra precise log calculation
// First handle case that x is close to 1
float r = 1.0f - as_float(ax);
int near1 = fabs(r) < 0x1.0p-4f;
int near1 = __clc_fabs(r) < 0x1.0p-4f;
float r2 = r*r;

// Coefficients are just 1/3, 1/4, 1/5 and 1/6
Expand Down
2 changes: 1 addition & 1 deletion libclc/generic/lib/math/clc_sqrt.cl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// Map the llvm sqrt intrinsic to an OpenCL function.
#define __CLC_FUNCTION __clc_llvm_intr_sqrt
#define __CLC_INTRINSIC "llvm.sqrt"
#include <math/unary_intrin.inc>
#include <clc/math/unary_intrin.inc>
#undef __CLC_FUNCTION
#undef __CLC_INTRINSIC

Expand Down
4 changes: 3 additions & 1 deletion libclc/generic/lib/math/clc_tan.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* THE SOFTWARE.
*/
#include <clc/clc.h>
#include <clc/math/clc_fabs.h>

#include "math.h"
#include "sincos_helpers.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ _CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_tan, float);
#include "sincosD_piby4.h"

_CLC_DEF _CLC_OVERLOAD double __clc_tan(double x) {
double y = fabs(x);
double y = __clc_fabs(x);

double r, rr;
int regn;
Expand All @@ -66,4 +67,5 @@ _CLC_DEF _CLC_OVERLOAD double __clc_tan(double x) {
return isnan(x) || isinf(x) ? as_double(QNANBITPATT_DP64) : as_double(t);
}
_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_tan, double);

#endif
8 changes: 2 additions & 6 deletions libclc/generic/lib/math/fabs.cl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <clc/clc.h>
#include "../clcmacro.h"

// Map the llvm intrinsic to an OpenCL function.
#define __CLC_FUNCTION __clc_fabs
#define __CLC_INTRINSIC "llvm.fabs"
#include "math/unary_intrin.inc"
#include <clc/clc.h>
#include <clc/math/clc_fabs.h>

#undef __CLC_FUNCTION
#define __CLC_FUNCTION fabs
Expand Down
8 changes: 2 additions & 6 deletions libclc/generic/lib/math/floor.cl
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <clc/clc.h>
#include "../clcmacro.h"

// Map the llvm intrinsic to an OpenCL function.
#define __CLC_FUNCTION __clc_floor
#define __CLC_INTRINSIC "llvm.floor"
#include "math/unary_intrin.inc"
#include <clc/clc.h>
#include <clc/math/clc_floor.h>

#undef __CLC_FUNCTION
#define __CLC_FUNCTION floor
Expand Down
2 changes: 1 addition & 1 deletion libclc/generic/lib/math/native_unary_intrinsic.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define __CLC_INTRINSIC "llvm." __CLC_XSTR(__CLC_NATIVE_INTRINSIC)

#undef cl_khr_fp64
#include <math/unary_intrin.inc>
#include <clc/math/unary_intrin.inc>

#endif

Expand Down
6 changes: 1 addition & 5 deletions libclc/generic/lib/math/rint.cl
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include <clc/clc.h>

// Map the llvm intrinsic to an OpenCL function.
#define __CLC_FUNCTION __clc_rint
#define __CLC_INTRINSIC "llvm.rint"
#include "math/unary_intrin.inc"
#include <clc/math/clc_rint.h>

#undef __CLC_FUNCTION
#define __CLC_FUNCTION rint
Expand Down
2 changes: 1 addition & 1 deletion libclc/generic/lib/math/round.cl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Map the llvm intrinsic to an OpenCL function.
#define __CLC_FUNCTION __clc_round
#define __CLC_INTRINSIC "llvm.round"
#include "math/unary_intrin.inc"
#include <clc/math/unary_intrin.inc>

#undef __CLC_FUNCTION
#define __CLC_FUNCTION round
Expand Down
Loading
Loading