[libclc] [libclc] Move min/max/clamp into the CLC builtins library (PR #114386)

Fraser Cormack via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 31 04:25:27 PDT 2024


https://github.com/frasercrmck updated https://github.com/llvm/llvm-project/pull/114386

>From 0465b89a7b624b8015e27efdbaf948c4aadd0e94 Mon Sep 17 00:00:00 2001
From: Fraser Cormack <fraser at codeplay.com>
Date: Thu, 26 Sep 2024 08:57:15 +0100
Subject: [PATCH] [libclc] Move min/max/clamp into the CLC builtins library

These functions are "shared" between integer and floating-point types,
hence the directory name. They are used in several CLC internal
functions such as __clc_ldexp.

Note that clspv and spirv targets don't want to define these functions,
so pre-processor macros replace calls to __clc_min with regular min, for
example. This means they can use as much of the generic CLC source files
as possible, but where CLC functions would usually call out to an
external __clc_min symbol, they call out to an external min symbol. Then
they opt out of defining __clc_min itself in their CLC builtins
library.

Preprocessor definitions for these targets have also been
changed somewhat: what used to be CLC_SPIRV (the 32-bit target) is now
CLC_SPIRV32, and CLC_SPIRV now represents either CLC_SPIRV32 or
CLC_SPIRV64. Same goes for CLC_CLSPV.

There are no differences (measured with llvm-diff) in any of the final
builtins libraries for nvptx, amdgpu, or clspv. Neither are there
differences in the SPIR-V targets' LLVM IR before it's actually lowered
to SPIR-V.
---
 libclc/CMakeLists.txt                             | 15 ++++++++++++---
 libclc/clc/include/clc/clcfunc.h                  |  4 ++--
 .../include/clc/integer/gentype.inc               |  4 ++--
 .../{generic => clc}/include/clc/math/gentype.inc |  0
 libclc/clc/include/clc/shared/clc_clamp.h         | 15 +++++++++++++++
 libclc/clc/include/clc/shared/clc_clamp.inc       |  9 +++++++++
 libclc/clc/include/clc/shared/clc_max.h           | 12 ++++++++++++
 libclc/clc/include/clc/shared/clc_max.inc         |  7 +++++++
 libclc/clc/include/clc/shared/clc_min.h           | 12 ++++++++++++
 libclc/clc/include/clc/shared/clc_min.inc         |  7 +++++++
 libclc/clc/lib/generic/SOURCES                    |  3 +++
 libclc/clc/lib/generic/shared/clc_clamp.cl        |  7 +++++++
 libclc/clc/lib/generic/shared/clc_clamp.inc       | 14 ++++++++++++++
 libclc/clc/lib/generic/shared/clc_max.cl          |  7 +++++++
 libclc/clc/lib/generic/shared/clc_max.inc         | 11 +++++++++++
 libclc/clc/lib/generic/shared/clc_min.cl          |  7 +++++++
 libclc/clc/lib/generic/shared/clc_min.inc         | 11 +++++++++++
 libclc/generic/include/config.h                   |  2 ++
 libclc/generic/lib/common/smoothstep.cl           |  2 +-
 libclc/generic/lib/common/step.cl                 |  2 +-
 libclc/generic/lib/math/clc_hypot.cl              |  4 +++-
 libclc/generic/lib/math/clc_ldexp.cl              |  9 +++++----
 libclc/generic/lib/math/math.h                    |  2 +-
 libclc/generic/lib/shared/clamp.cl                |  1 +
 libclc/generic/lib/shared/clamp.inc               |  4 ++--
 libclc/generic/lib/shared/max.cl                  |  1 +
 libclc/generic/lib/shared/max.inc                 |  7 ++++---
 libclc/generic/lib/shared/min.cl                  |  1 +
 libclc/generic/lib/shared/min.inc                 |  7 ++++---
 29 files changed, 164 insertions(+), 23 deletions(-)
 rename libclc/{generic => clc}/include/clc/integer/gentype.inc (99%)
 rename libclc/{generic => clc}/include/clc/math/gentype.inc (100%)
 create mode 100644 libclc/clc/include/clc/shared/clc_clamp.h
 create mode 100644 libclc/clc/include/clc/shared/clc_clamp.inc
 create mode 100644 libclc/clc/include/clc/shared/clc_max.h
 create mode 100644 libclc/clc/include/clc/shared/clc_max.inc
 create mode 100644 libclc/clc/include/clc/shared/clc_min.h
 create mode 100644 libclc/clc/include/clc/shared/clc_min.inc
 create mode 100644 libclc/clc/lib/generic/shared/clc_clamp.cl
 create mode 100644 libclc/clc/lib/generic/shared/clc_clamp.inc
 create mode 100644 libclc/clc/lib/generic/shared/clc_max.cl
 create mode 100644 libclc/clc/lib/generic/shared/clc_max.inc
 create mode 100644 libclc/clc/lib/generic/shared/clc_min.cl
 create mode 100644 libclc/clc/lib/generic/shared/clc_min.inc

diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index 16d74e53295cc1..2c2c7f16e29442 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -321,21 +321,30 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
     message( STATUS "  device: ${d} ( ${${d}_aliases} )" )
 
     if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
-      set( build_flags -O0 -finline-hint-functions )
+      set( build_flags -O0 -finline-hint-functions -DCLC_SPIRV )
       set( opt_flags )
       set( spvflags --spirv-max-version=1.1 )
+      set( MACRO_ARCH SPIRV32 )
+      if( ARCH STREQUAL spirv64 )
+        set( MACRO_ARCH SPIRV64 )
+      endif()
     elseif( ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
-      set( build_flags "-Wno-unknown-assumption")
+      set( build_flags "-Wno-unknown-assumption" -DCLC_CLSPV )
       set( opt_flags -O3 )
+      set( MACRO_ARCH CLSPV32 )
+      if( ARCH STREQUAL clspv64 )
+        set( MACRO_ARCH CLSPV64 )
+      endif()
     else()
       set( build_flags )
       set( opt_flags -O3 )
+      set( MACRO_ARCH ${ARCH} )
     endif()
 
     set( LIBCLC_ARCH_OBJFILE_DIR "${LIBCLC_OBJFILE_DIR}/${arch_suffix}" )
     file( MAKE_DIRECTORY ${LIBCLC_ARCH_OBJFILE_DIR} )
 
-    string( TOUPPER "CLC_${ARCH}" CLC_TARGET_DEFINE )
+    string( TOUPPER "CLC_${MACRO_ARCH}" CLC_TARGET_DEFINE )
 
     list( APPEND build_flags
       -D__CLC_INTERNAL
diff --git a/libclc/clc/include/clc/clcfunc.h b/libclc/clc/include/clc/clcfunc.h
index fe3406f64fecb8..4698f0950d8a8e 100644
--- a/libclc/clc/include/clc/clcfunc.h
+++ b/libclc/clc/include/clc/clcfunc.h
@@ -7,9 +7,9 @@
 
 // avoid inlines for SPIR-V related targets since we'll optimise later in the
 // chain
-#if defined(CLC_SPIRV) || defined(CLC_SPIRV64)
+#if defined(CLC_SPIRV)
 #define _CLC_DEF
-#elif defined(CLC_CLSPV) || defined(CLC_CLSPV64)
+#elif defined(CLC_CLSPV)
 #define _CLC_DEF __attribute__((noinline)) __attribute__((clspv_libclc_builtin))
 #else
 #define _CLC_DEF __attribute__((always_inline))
diff --git a/libclc/generic/include/clc/integer/gentype.inc b/libclc/clc/include/clc/integer/gentype.inc
similarity index 99%
rename from libclc/generic/include/clc/integer/gentype.inc
rename to libclc/clc/include/clc/integer/gentype.inc
index cefed9c5e51eef..2c8dd143db8798 100644
--- a/libclc/generic/include/clc/integer/gentype.inc
+++ b/libclc/clc/include/clc/integer/gentype.inc
@@ -1,5 +1,5 @@
-//These 2 defines only change when switching between data sizes or base types to
-//keep this file manageable.
+// These 2 defines only change when switching between data sizes or base types
+// to keep this file manageable.
 #define __CLC_GENSIZE 8
 #define __CLC_SCALAR_GENTYPE char
 
diff --git a/libclc/generic/include/clc/math/gentype.inc b/libclc/clc/include/clc/math/gentype.inc
similarity index 100%
rename from libclc/generic/include/clc/math/gentype.inc
rename to libclc/clc/include/clc/math/gentype.inc
diff --git a/libclc/clc/include/clc/shared/clc_clamp.h b/libclc/clc/include/clc/shared/clc_clamp.h
new file mode 100644
index 00000000000000..5c044c9a1a510d
--- /dev/null
+++ b/libclc/clc/include/clc/shared/clc_clamp.h
@@ -0,0 +1,15 @@
+#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
+// clspv and spir-v targets provide their own OpenCL-compatible clamp
+#define __clc_clamp clamp
+#else
+
+#include <clc/clcfunc.h>
+#include <clc/clctypes.h>
+
+#define __CLC_BODY <clc/shared/clc_clamp.inc>
+#include <clc/integer/gentype.inc>
+
+#define __CLC_BODY <clc/shared/clc_clamp.inc>
+#include <clc/math/gentype.inc>
+
+#endif
diff --git a/libclc/clc/include/clc/shared/clc_clamp.inc b/libclc/clc/include/clc/shared/clc_clamp.inc
new file mode 100644
index 00000000000000..cf6b0b2789bc54
--- /dev/null
+++ b/libclc/clc/include/clc/shared/clc_clamp.inc
@@ -0,0 +1,9 @@
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
+                                                  __CLC_GENTYPE y,
+                                                  __CLC_GENTYPE z);
+
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
+                                                  __CLC_SCALAR_GENTYPE y,
+                                                  __CLC_SCALAR_GENTYPE z);
+#endif
diff --git a/libclc/clc/include/clc/shared/clc_max.h b/libclc/clc/include/clc/shared/clc_max.h
new file mode 100644
index 00000000000000..2825640f6c2913
--- /dev/null
+++ b/libclc/clc/include/clc/shared/clc_max.h
@@ -0,0 +1,12 @@
+#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
+// clspv and spir-v targets provide their own OpenCL-compatible max
+#define __clc_max max
+#else
+
+#define __CLC_BODY <clc/shared/clc_max.inc>
+#include <clc/integer/gentype.inc>
+
+#define __CLC_BODY <clc/shared/clc_max.inc>
+#include <clc/math/gentype.inc>
+
+#endif
diff --git a/libclc/clc/include/clc/shared/clc_max.inc b/libclc/clc/include/clc/shared/clc_max.inc
new file mode 100644
index 00000000000000..bddb3fa3d920ca
--- /dev/null
+++ b/libclc/clc/include/clc/shared/clc_max.inc
@@ -0,0 +1,7 @@
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
+                                                __CLC_GENTYPE b);
+
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
+                                                __CLC_SCALAR_GENTYPE b);
+#endif
diff --git a/libclc/clc/include/clc/shared/clc_min.h b/libclc/clc/include/clc/shared/clc_min.h
new file mode 100644
index 00000000000000..0b7ee140b8f452
--- /dev/null
+++ b/libclc/clc/include/clc/shared/clc_min.h
@@ -0,0 +1,12 @@
+#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
+// clspv and spir-v targets provide their own OpenCL-compatible min
+#define __clc_min min
+#else
+
+#define __CLC_BODY <clc/shared/clc_min.inc>
+#include <clc/integer/gentype.inc>
+
+#define __CLC_BODY <clc/shared/clc_min.inc>
+#include <clc/math/gentype.inc>
+
+#endif
diff --git a/libclc/clc/include/clc/shared/clc_min.inc b/libclc/clc/include/clc/shared/clc_min.inc
new file mode 100644
index 00000000000000..3e1da96df43dd1
--- /dev/null
+++ b/libclc/clc/include/clc/shared/clc_min.inc
@@ -0,0 +1,7 @@
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
+                                                __CLC_GENTYPE b);
+
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
+                                                __CLC_SCALAR_GENTYPE b);
+#endif
diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index fa2e4f50b99cd7..db523adb638363 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -1 +1,4 @@
 geometric/clc_dot.cl
+shared/clc_clamp.cl
+shared/clc_max.cl
+shared/clc_min.cl
diff --git a/libclc/clc/lib/generic/shared/clc_clamp.cl b/libclc/clc/lib/generic/shared/clc_clamp.cl
new file mode 100644
index 00000000000000..1d40da3cf22961
--- /dev/null
+++ b/libclc/clc/lib/generic/shared/clc_clamp.cl
@@ -0,0 +1,7 @@
+#include <clc/internal/clc.h>
+
+#define __CLC_BODY <clc_clamp.inc>
+#include <clc/integer/gentype.inc>
+
+#define __CLC_BODY <clc_clamp.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/shared/clc_clamp.inc b/libclc/clc/lib/generic/shared/clc_clamp.inc
new file mode 100644
index 00000000000000..da67cd2ad69dba
--- /dev/null
+++ b/libclc/clc/lib/generic/shared/clc_clamp.inc
@@ -0,0 +1,14 @@
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
+                                                 __CLC_GENTYPE y,
+                                                 __CLC_GENTYPE z) {
+  return (x > z ? z : (x < y ? y : x));
+}
+
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
+                                                 __CLC_SCALAR_GENTYPE y,
+                                                 __CLC_SCALAR_GENTYPE z) {
+  return (x > (__CLC_GENTYPE)z ? (__CLC_GENTYPE)z
+                               : (x < (__CLC_GENTYPE)y ? (__CLC_GENTYPE)y : x));
+}
+#endif
diff --git a/libclc/clc/lib/generic/shared/clc_max.cl b/libclc/clc/lib/generic/shared/clc_max.cl
new file mode 100644
index 00000000000000..e1050ed0007eec
--- /dev/null
+++ b/libclc/clc/lib/generic/shared/clc_max.cl
@@ -0,0 +1,7 @@
+#include <clc/internal/clc.h>
+
+#define __CLC_BODY <clc_max.inc>
+#include <clc/integer/gentype.inc>
+
+#define __CLC_BODY <clc_max.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/shared/clc_max.inc b/libclc/clc/lib/generic/shared/clc_max.inc
new file mode 100644
index 00000000000000..f4234cb359d86e
--- /dev/null
+++ b/libclc/clc/lib/generic/shared/clc_max.inc
@@ -0,0 +1,11 @@
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
+                                               __CLC_GENTYPE b) {
+  return (a > b ? a : b);
+}
+
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
+                                               __CLC_SCALAR_GENTYPE b) {
+  return (a > (__CLC_GENTYPE)b ? a : (__CLC_GENTYPE)b);
+}
+#endif
diff --git a/libclc/clc/lib/generic/shared/clc_min.cl b/libclc/clc/lib/generic/shared/clc_min.cl
new file mode 100644
index 00000000000000..12a26f53524077
--- /dev/null
+++ b/libclc/clc/lib/generic/shared/clc_min.cl
@@ -0,0 +1,7 @@
+#include <clc/internal/clc.h>
+
+#define __CLC_BODY <clc_min.inc>
+#include <clc/integer/gentype.inc>
+
+#define __CLC_BODY <clc_min.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/shared/clc_min.inc b/libclc/clc/lib/generic/shared/clc_min.inc
new file mode 100644
index 00000000000000..e9c85ddd3affaa
--- /dev/null
+++ b/libclc/clc/lib/generic/shared/clc_min.inc
@@ -0,0 +1,11 @@
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
+                                               __CLC_GENTYPE b) {
+  return (b < a ? b : a);
+}
+
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
+                                               __CLC_SCALAR_GENTYPE b) {
+  return (b < (__CLC_GENTYPE)a ? (__CLC_GENTYPE)b : a);
+}
+#endif
diff --git a/libclc/generic/include/config.h b/libclc/generic/include/config.h
index 2994199b02c5b8..7aa5967f4eb688 100644
--- a/libclc/generic/include/config.h
+++ b/libclc/generic/include/config.h
@@ -20,6 +20,8 @@
  * THE SOFTWARE.
  */
 
+#include <clc/clcfunc.h>
+
 _CLC_DECL bool __clc_subnormals_disabled();
 _CLC_DECL bool __clc_fp16_subnormals_supported();
 _CLC_DECL bool __clc_fp32_subnormals_supported();
diff --git a/libclc/generic/lib/common/smoothstep.cl b/libclc/generic/lib/common/smoothstep.cl
index 9f513eb379e195..1b6a74b89d2c21 100644
--- a/libclc/generic/lib/common/smoothstep.cl
+++ b/libclc/generic/lib/common/smoothstep.cl
@@ -46,7 +46,7 @@ SMOOTH_STEP_DEF(double, double, SMOOTH_STEP_IMPL_D);
 
 _CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, smoothstep, double, double, double);
 
-#if !defined(CLC_SPIRV) && !defined(CLC_SPIRV64)
+#if !defined(CLC_SPIRV)
 SMOOTH_STEP_DEF(float, double, SMOOTH_STEP_IMPL_D);
 SMOOTH_STEP_DEF(double, float, SMOOTH_STEP_IMPL_D);
 
diff --git a/libclc/generic/lib/common/step.cl b/libclc/generic/lib/common/step.cl
index 5d7c48780d4fad..8155b469fb210f 100644
--- a/libclc/generic/lib/common/step.cl
+++ b/libclc/generic/lib/common/step.cl
@@ -45,7 +45,7 @@ STEP_DEF(double, double);
 _CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, double, double);
 _CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, double, double);
 
-#if !defined(CLC_SPIRV) && !defined(CLC_SPIRV64)
+#if !defined(CLC_SPIRV)
 STEP_DEF(float, double);
 STEP_DEF(double, float);
 
diff --git a/libclc/generic/lib/math/clc_hypot.cl b/libclc/generic/lib/math/clc_hypot.cl
index 118dd9f454ccf2..ce88f276cf2f20 100644
--- a/libclc/generic/lib/math/clc_hypot.cl
+++ b/libclc/generic/lib/math/clc_hypot.cl
@@ -21,6 +21,7 @@
  */
 
 #include <clc/clc.h>
+#include <clc/shared/clc_clamp.h>
 #include <math/clc_hypot.h>
 
 #include "config.h"
@@ -39,7 +40,8 @@ _CLC_DEF _CLC_OVERLOAD float __clc_hypot(float x, float y) {
   ux = c ? aux : auy;
   uy = c ? auy : aux;
 
-  int xexp = clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
+  int xexp =
+      __clc_clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
   float fx_exp = as_float((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
   float fi_exp = as_float((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
   float fx = as_float(ux) * fi_exp;
diff --git a/libclc/generic/lib/math/clc_ldexp.cl b/libclc/generic/lib/math/clc_ldexp.cl
index 961e28f011f571..438c31835a3648 100644
--- a/libclc/generic/lib/math/clc_ldexp.cl
+++ b/libclc/generic/lib/math/clc_ldexp.cl
@@ -20,10 +20,11 @@
  * THE SOFTWARE.
  */
 
-#include <clc/clc.h>
-#include "config.h"
 #include "../clcmacro.h"
+#include "config.h"
 #include "math.h"
+#include <clc/clc.h>
+#include <clc/shared/clc_clamp.h>
 
 _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
 
@@ -35,7 +36,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
     int m = i & 0x007fffff;
     int s = i & 0x80000000;
     int v = add_sat(e, n);
-    v = clamp(v, 0, 0xff);
+    v = __clc_clamp(v, 0, 0xff);
     int mr = e == 0 | v == 0 | v == 0xff ? 0 : m;
     int c = e == 0xff;
     mr = c ? m : mr;
@@ -110,7 +111,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
   ux = c ? ux : l;
 
   int v = e + n;
-  v = clamp(v, -0x7ff, 0x7ff);
+  v = __clc_clamp(v, -0x7ff, 0x7ff);
 
   ux &= ~EXPBITS_DP64;
 
diff --git a/libclc/generic/lib/math/math.h b/libclc/generic/lib/math/math.h
index 351e37dc3f12ca..d5ef0871e52019 100644
--- a/libclc/generic/lib/math/math.h
+++ b/libclc/generic/lib/math/math.h
@@ -40,7 +40,7 @@
 
 #if (defined __AMDGCN__ || defined __R600__) && !defined __HAS_FMAF__
 #define HAVE_HW_FMA32() (0)
-#elif defined CLC_SPIRV || defined CLC_SPIRV64
+#elif defined(CLC_SPIRV)
 bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);
 #define HAVE_HW_FMA32() __clc_runtime_has_hw_fma32()
 #else
diff --git a/libclc/generic/lib/shared/clamp.cl b/libclc/generic/lib/shared/clamp.cl
index b946220485bea6..f470fc822f7561 100644
--- a/libclc/generic/lib/shared/clamp.cl
+++ b/libclc/generic/lib/shared/clamp.cl
@@ -1,4 +1,5 @@
 #include <clc/clc.h>
+#include <clc/shared/clc_clamp.h>
 
 #define __CLC_BODY <clamp.inc>
 #include <clc/integer/gentype.inc>
diff --git a/libclc/generic/lib/shared/clamp.inc b/libclc/generic/lib/shared/clamp.inc
index c918f9c499e70f..7e02cb2e1c47e9 100644
--- a/libclc/generic/lib/shared/clamp.inc
+++ b/libclc/generic/lib/shared/clamp.inc
@@ -1,9 +1,9 @@
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE clamp(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_GENTYPE z) {
-  return (x > z ? z : (x < y ? y : x));
+  return __clc_clamp(x, y, z);
 }
 
 #ifndef __CLC_SCALAR
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE clamp(__CLC_GENTYPE x, __CLC_SCALAR_GENTYPE y, __CLC_SCALAR_GENTYPE z) {
-  return (x > (__CLC_GENTYPE)z ? (__CLC_GENTYPE)z : (x < (__CLC_GENTYPE)y ? (__CLC_GENTYPE)y : x));
+  return __clc_clamp(x, y, z);
 }
 #endif
diff --git a/libclc/generic/lib/shared/max.cl b/libclc/generic/lib/shared/max.cl
index eb573cdbca86b8..2266d5905afd6d 100644
--- a/libclc/generic/lib/shared/max.cl
+++ b/libclc/generic/lib/shared/max.cl
@@ -1,4 +1,5 @@
 #include <clc/clc.h>
+#include <clc/shared/clc_max.h>
 
 #define __CLC_BODY <max.inc>
 #include <clc/integer/gentype.inc>
diff --git a/libclc/generic/lib/shared/max.inc b/libclc/generic/lib/shared/max.inc
index 75a24c077d1ab5..ec433a89c6e920 100644
--- a/libclc/generic/lib/shared/max.inc
+++ b/libclc/generic/lib/shared/max.inc
@@ -1,9 +1,10 @@
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE max(__CLC_GENTYPE a, __CLC_GENTYPE b) {
-  return (a > b ? a : b);
+  return __clc_max(a, b);
 }
 
 #ifndef __CLC_SCALAR
-_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE max(__CLC_GENTYPE a, __CLC_SCALAR_GENTYPE b) {
-  return (a > (__CLC_GENTYPE)b ? a : (__CLC_GENTYPE)b);
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE max(__CLC_GENTYPE a,
+                                         __CLC_SCALAR_GENTYPE b) {
+  return __clc_max(a, b);
 }
 #endif
diff --git a/libclc/generic/lib/shared/min.cl b/libclc/generic/lib/shared/min.cl
index 19a7d796c7b99c..f5c4d57f4b8d8e 100644
--- a/libclc/generic/lib/shared/min.cl
+++ b/libclc/generic/lib/shared/min.cl
@@ -1,4 +1,5 @@
 #include <clc/clc.h>
+#include <clc/shared/clc_min.h>
 
 #define __CLC_BODY <min.inc>
 #include <clc/integer/gentype.inc>
diff --git a/libclc/generic/lib/shared/min.inc b/libclc/generic/lib/shared/min.inc
index e15e05591342e0..6a00944cbe35ee 100644
--- a/libclc/generic/lib/shared/min.inc
+++ b/libclc/generic/lib/shared/min.inc
@@ -1,9 +1,10 @@
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE min(__CLC_GENTYPE a, __CLC_GENTYPE b) {
-  return (b < a ? b : a);
+  return __clc_min(a, b);
 }
 
 #ifndef __CLC_SCALAR
-_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE min(__CLC_GENTYPE a, __CLC_SCALAR_GENTYPE b) {
-  return (b < (__CLC_GENTYPE)a ? (__CLC_GENTYPE)b : a);
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE min(__CLC_GENTYPE a,
+                                         __CLC_SCALAR_GENTYPE b) {
+  return __clc_min(a, b);
 }
 #endif



More information about the cfe-commits mailing list