Skip to content

[libclc] Move fmod, remainder & remquo to the CLC library #132054

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 3 commits into from
Mar 27, 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
13 changes: 13 additions & 0 deletions libclc/clc/include/clc/math/binary_def_via_fp32.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
__CLC_GENTYPE y) {
return __CLC_CONVERT_GENTYPE(
__CLC_FUNCTION(__CLC_CONVERT_FLOATN(x), __CLC_CONVERT_FLOATN(y)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
//
//===----------------------------------------------------------------------===//

#ifndef __CLC_MATH_CLC_FMOD_H__
#define __CLC_MATH_CLC_FMOD_H__

#define __CLC_FUNCTION __clc_fmod
#define __CLC_BODY <clc/math/binary_decl_tt.inc>
#define __CLC_BODY <clc/shared/binary_decl.inc>

#include <clc/math/gentype.inc>

#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_FMOD_H__
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
//
//===----------------------------------------------------------------------===//

#ifndef __CLC_MATH_CLC_REMAINDER_H__
#define __CLC_MATH_CLC_REMAINDER_H__

#define __CLC_FUNCTION __clc_remainder
#define __CLC_BODY <clc/math/binary_decl_tt.inc>
#define __CLC_BODY <clc/shared/binary_decl.inc>

#include <clc/math/gentype.inc>

#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_REMAINDER_H__
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
//
//===----------------------------------------------------------------------===//

#ifndef __CLC_MATH_CLC_REMQUO_H__
#define __CLC_MATH_CLC_REMQUO_H__

#define __CLC_FUNCTION __clc_remquo

#define __CLC_BODY <clc/math/remquo.inc>
#define __CLC_BODY <clc/math/remquo_decl.inc>
#define __CLC_ADDRESS_SPACE private
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#undef __CLC_ADDRESS_SPACE
#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_REMQUO_H__
7 changes: 5 additions & 2 deletions libclc/clc/include/clc/math/gentype.inc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#define __CLC_CONVERT_S_GENTYPE __CLC_XCONCAT(__clc_convert_, __CLC_S_GENTYPE)
#define __CLC_CONVERT_U_GENTYPE __CLC_XCONCAT(__clc_convert_, __CLC_U_GENTYPE)

#if (!defined(__HALF_ONLY) && !defined(__DOUBLE_ONLY))
#define __CLC_SCALAR_GENTYPE float
#define __CLC_FPSIZE 32
#define __CLC_FP_LIT(x) x##F
Expand Down Expand Up @@ -133,7 +134,9 @@
#undef __CLC_FPSIZE
#undef __CLC_SCALAR_GENTYPE

#ifndef __FLOAT_ONLY
#endif

#if (!defined(__HALF_ONLY) && !defined(__FLOAT_ONLY))
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable

Expand Down Expand Up @@ -204,7 +207,7 @@
#endif
#endif

#ifndef __FLOAT_ONLY
#if (!defined(__FLOAT_ONLY) && !defined(__DOUBLE_ONLY))
#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
//
//===----------------------------------------------------------------------===//

_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q);
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(
__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q);
3 changes: 3 additions & 0 deletions libclc/clc/lib/generic/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ math/clc_copysign.cl
math/clc_ep_log.cl
math/clc_fabs.cl
math/clc_fma.cl
math/clc_fmod.cl
math/clc_floor.cl
math/clc_frexp.cl
math/clc_hypot.cl
Expand All @@ -45,6 +46,8 @@ math/clc_mad.cl
math/clc_modf.cl
math/clc_nan.cl
math/clc_nextafter.cl
math/clc_remainder.cl
math/clc_remquo.cl
math/clc_rint.cl
math/clc_round.cl
math/clc_rsqrt.cl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clc.h>
#include <clc/clc_convert.h>
#include <clc/clcmacro.h>
#include <clc/integer/clc_clz.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_floor.h>
#include <clc/math/clc_fma.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_trunc.h>
#include <clc/math/math.h>
#include <clc/shared/clc_max.h>
#include <math/clc_remainder.h>

_CLC_DEF _CLC_OVERLOAD float __clc_fmod(float x, float y) {
int ux = __clc_as_int(x);
Expand Down Expand Up @@ -67,6 +66,9 @@ _CLC_DEF _CLC_OVERLOAD float __clc_fmod(float x, float y) {
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_fmod, float, float);

#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) {
ulong ux = __clc_as_ulong(x);
ulong ax = ux & ~SIGNBIT_DP64;
Expand All @@ -92,7 +94,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) {
// but it doesn't matter - it just means that we'll go round
// the loop below one extra time.
int ntimes = __clc_max(0, (xexp1 - yexp1) / 53);
double w = ldexp(dy, ntimes * 53);
double w = __clc_ldexp(dy, ntimes * 53);
w = ntimes == 0 ? dy : w;
double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;

Expand Down Expand Up @@ -171,3 +173,15 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) {
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_fmod, double,
double);
#endif

#ifdef cl_khr_fp16

#pragma OPENCL EXTENSION cl_khr_fp16 : enable

// Forward the half version of this builtin onto the float one
#define __HALF_ONLY
#define __CLC_FUNCTION __clc_fmod
#define __CLC_BODY <clc/math/binary_def_via_fp32.inc>
#include <clc/math/gentype.inc>

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clc.h>
#include <clc/clc_convert.h>
#include <clc/clcmacro.h>
#include <clc/integer/clc_clz.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_floor.h>
#include <clc/math/clc_fma.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_remainder.h>
#include <clc/math/clc_trunc.h>
#include <clc/math/math.h>
#include <clc/shared/clc_max.h>
#include <math/clc_remainder.h>

_CLC_DEF _CLC_OVERLOAD float __clc_remainder(float x, float y) {
int ux = __clc_as_int(x);
Expand Down Expand Up @@ -77,6 +77,9 @@ _CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_remainder, float,
float);

#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) {
ulong ux = __clc_as_ulong(x);
ulong ax = ux & ~SIGNBIT_DP64;
Expand Down Expand Up @@ -104,7 +107,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) {
// but it doesn't matter - it just means that we'll go round
// the loop below one extra time.
int ntimes = __clc_max(0, (xexp1 - yexp1) / 53);
double w = ldexp(dy, ntimes * 53);
double w = __clc_ldexp(dy, ntimes * 53);
w = ntimes == 0 ? dy : w;
double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;

Expand Down Expand Up @@ -207,3 +210,15 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) {
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_remainder, double,
double);
#endif

#ifdef cl_khr_fp16

#pragma OPENCL EXTENSION cl_khr_fp16 : enable

// Forward the half version of this builtin onto the float one
#define __HALF_ONLY
#define __CLC_FUNCTION __clc_remainder
#define __CLC_BODY <clc/math/binary_def_via_fp32.inc>
#include <clc/math/gentype.inc>

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clc.h>
#include <clc/clc_convert.h>
#include <clc/clcmacro.h>
#include <clc/integer/clc_clz.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_floor.h>
#include <clc/math/clc_fma.h>
#include <clc/math/clc_ldexp.h>
#include <clc/math/clc_subnormal_config.h>
#include <clc/math/clc_trunc.h>
#include <clc/math/math.h>
#include <clc/shared/clc_max.h>
#include <math/clc_remainder.h>

_CLC_DEF _CLC_OVERLOAD float __clc_remquo(float x, float y,
__private int *quo) {
Expand Down Expand Up @@ -116,6 +116,9 @@ __VEC_REMQUO(float, 8, 4)
__VEC_REMQUO(float, 16, 8)

#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y,
__private int *pquo) {
ulong ux = __clc_as_ulong(x);
Expand Down Expand Up @@ -144,7 +147,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y,
// but it doesn't matter - it just means that we'll go round
// the loop below one extra time.
int ntimes = __clc_max(0, (xexp1 - yexp1) / 53);
double w = ldexp(dy, ntimes * 53);
double w = __clc_ldexp(dy, ntimes * 53);
w = ntimes == 0 ? dy : w;
double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;

Expand Down
3 changes: 0 additions & 3 deletions libclc/clspv/lib/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ subnormal_config.cl
../../generic/lib/math/atanpi.cl
../../generic/lib/math/cbrt.cl
../../generic/lib/math/clc_exp10.cl
../../generic/lib/math/clc_fmod.cl
../../generic/lib/math/clc_pow.cl
../../generic/lib/math/clc_pown.cl
../../generic/lib/math/clc_powr.cl
../../generic/lib/math/clc_remainder.cl
../../generic/lib/math/clc_remquo.cl
../../generic/lib/math/clc_rootn.cl
../../generic/lib/math/clc_tan.cl
../../generic/lib/math/clc_tanpi.cl
Expand Down
6 changes: 3 additions & 3 deletions libclc/generic/include/clc/math/remquo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

#define __CLC_FUNCTION remquo

#define __CLC_BODY <clc/math/remquo.inc>
#define __CLC_BODY <clc/math/remquo_decl.inc>
#define __CLC_ADDRESS_SPACE global
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#define __CLC_BODY <clc/math/remquo.inc>
#define __CLC_BODY <clc/math/remquo_decl.inc>
#define __CLC_ADDRESS_SPACE local
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#define __CLC_BODY <clc/math/remquo.inc>
#define __CLC_BODY <clc/math/remquo_decl.inc>
#define __CLC_ADDRESS_SPACE private
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE
Expand Down
3 changes: 0 additions & 3 deletions libclc/generic/lib/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ math/floor.cl
math/fma.cl
math/fmax.cl
math/fmin.cl
math/clc_fmod.cl
math/fmod.cl
math/fract.cl
math/frexp.cl
Expand Down Expand Up @@ -163,9 +162,7 @@ math/clc_pown.cl
math/pown.cl
math/clc_powr.cl
math/powr.cl
math/clc_remainder.cl
math/remainder.cl
math/clc_remquo.cl
math/remquo.cl
math/rint.cl
math/clc_rootn.cl
Expand Down
6 changes: 3 additions & 3 deletions libclc/generic/lib/math/fmod.cl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//===----------------------------------------------------------------------===//

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

#define __CLC_FUNC fmod
#define __CLC_BODY <clc_sw_binary.inc>
#define FUNCTION fmod
#define __CLC_BODY <clc/shared/binary_def.inc>
#include <clc/math/gentype.inc>
6 changes: 3 additions & 3 deletions libclc/generic/lib/math/remainder.cl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//===----------------------------------------------------------------------===//

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

#define __CLC_FUNC remainder
#define __CLC_BODY <clc_sw_binary.inc>
#define FUNCTION remainder
#define __CLC_BODY <clc/shared/binary_def.inc>
#include <clc/math/gentype.inc>
2 changes: 1 addition & 1 deletion libclc/generic/lib/math/remquo.cl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

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

#define __CLC_BODY <remquo.inc>
#define __CLC_ADDRESS_SPACE global
Expand Down
3 changes: 2 additions & 1 deletion libclc/generic/lib/math/remquo.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
//
//===----------------------------------------------------------------------===//

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q) {
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y,
__CLC_ADDRESS_SPACE __CLC_INTN *q) {
__CLC_INTN local_q;
__CLC_GENTYPE ret = __clc_remquo(x, y, &local_q);
*q = local_q;
Expand Down
3 changes: 0 additions & 3 deletions libclc/spirv/lib/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ subnormal_config.cl
../../generic/lib/math/clc_exp10.cl
../../generic/lib/math/exp10.cl
math/fma.cl
../../generic/lib/math/clc_fmod.cl
../../generic/lib/math/fmod.cl
../../generic/lib/math/fract.cl
../../generic/lib/math/frexp.cl
Expand All @@ -65,9 +64,7 @@ math/fma.cl
../../generic/lib/math/pown.cl
../../generic/lib/math/clc_powr.cl
../../generic/lib/math/powr.cl
../../generic/lib/math/clc_remainder.cl
../../generic/lib/math/remainder.cl
../../generic/lib/math/clc_remquo.cl
../../generic/lib/math/remquo.cl
../../generic/lib/math/clc_rootn.cl
../../generic/lib/math/rootn.cl
Expand Down