https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/186092

Gives slightly better codegen.

>From bb733c65f323b1930a4be4a8237876dd0bfc16f9 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <[email protected]>
Date: Thu, 12 Mar 2026 12:13:08 +0100
Subject: [PATCH] libclc: Improve minmag and maxmag

Gives slightly better codegen.
---
 libclc/clc/lib/generic/math/clc_maxmag.cl  | 10 ++--------
 libclc/clc/lib/generic/math/clc_maxmag.inc | 11 ++++-------
 libclc/clc/lib/generic/math/clc_minmag.cl  | 10 ++--------
 libclc/clc/lib/generic/math/clc_minmag.inc | 10 ++++------
 4 files changed, 12 insertions(+), 29 deletions(-)

diff --git a/libclc/clc/lib/generic/math/clc_maxmag.cl 
b/libclc/clc/lib/generic/math/clc_maxmag.cl
index e34fd5613c3f2..fd457db0c5097 100644
--- a/libclc/clc/lib/generic/math/clc_maxmag.cl
+++ b/libclc/clc/lib/generic/math/clc_maxmag.cl
@@ -6,14 +6,8 @@
 //
 
//===----------------------------------------------------------------------===//
 
-#include <clc/clc_convert.h>
-#include <clc/internal/clc.h>
-#include <clc/math/clc_fabs.h>
-#include <clc/math/clc_fmax.h>
-#include <clc/relational/clc_isequal.h>
-#include <clc/relational/clc_isgreater.h>
-#include <clc/relational/clc_isnan.h>
-#include <clc/relational/clc_select.h>
+#include "clc/math/clc_fabs.h"
+#include "clc/math/clc_fmax.h"
 
 #define __CLC_BODY <clc_maxmag.inc>
 #include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/math/clc_maxmag.inc 
b/libclc/clc/lib/generic/math/clc_maxmag.inc
index 0c346eeac8ae2..f8520efbbea0f 100644
--- a/libclc/clc/lib/generic/math/clc_maxmag.inc
+++ b/libclc/clc/lib/generic/math/clc_maxmag.inc
@@ -8,11 +8,8 @@
 
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_maxmag(__CLC_GENTYPE x,
                                                   __CLC_GENTYPE y) {
-  const __CLC_GENTYPE res = __clc_select(
-      y, x,
-      __CLC_CONVERT_BIT_INTN(__clc_isgreater(__clc_fabs(x), __clc_fabs(y))));
-  return __clc_select(
-      res, __clc_fmax(x, y),
-      __CLC_CONVERT_BIT_INTN(__clc_isnan(x) || __clc_isnan(y) ||
-                             __clc_isequal(__clc_fabs(x), __clc_fabs(y))));
+  __CLC_GENTYPE ax = __clc_fabs(x);
+  __CLC_GENTYPE ay = __clc_fabs(y);
+  __CLC_GENTYPE ret = ax > ay ? x : __clc_fmax(x, y);
+  return ay > ax ? y : ret;
 }
diff --git a/libclc/clc/lib/generic/math/clc_minmag.cl 
b/libclc/clc/lib/generic/math/clc_minmag.cl
index 8529beb8d83f0..b0d7a92729299 100644
--- a/libclc/clc/lib/generic/math/clc_minmag.cl
+++ b/libclc/clc/lib/generic/math/clc_minmag.cl
@@ -6,14 +6,8 @@
 //
 
//===----------------------------------------------------------------------===//
 
-#include <clc/clc_convert.h>
-#include <clc/internal/clc.h>
-#include <clc/math/clc_fabs.h>
-#include <clc/math/clc_fmin.h>
-#include <clc/relational/clc_isequal.h>
-#include <clc/relational/clc_isless.h>
-#include <clc/relational/clc_isnan.h>
-#include <clc/relational/clc_select.h>
+#include "clc/math/clc_fabs.h"
+#include "clc/math/clc_fmin.h"
 
 #define __CLC_BODY <clc_minmag.inc>
 #include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/math/clc_minmag.inc 
b/libclc/clc/lib/generic/math/clc_minmag.inc
index 2b9236fedfeb2..9419d5cdd2e51 100644
--- a/libclc/clc/lib/generic/math/clc_minmag.inc
+++ b/libclc/clc/lib/generic/math/clc_minmag.inc
@@ -8,10 +8,8 @@
 
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_minmag(__CLC_GENTYPE x,
                                                   __CLC_GENTYPE y) {
-  const __CLC_GENTYPE res = __clc_select(
-      y, x, __CLC_CONVERT_BIT_INTN(__clc_isless(__clc_fabs(x), 
__clc_fabs(y))));
-  return __clc_select(
-      res, __clc_fmin(x, y),
-      __CLC_CONVERT_BIT_INTN(__clc_isnan(x) || __clc_isnan(y) ||
-                             __clc_isequal(__clc_fabs(x), __clc_fabs(y))));
+  __CLC_GENTYPE ax = __clc_fabs(x);
+  __CLC_GENTYPE ay = __clc_fabs(y);
+  __CLC_GENTYPE ret = ax < ay ? x : __clc_fmin(x, y);
+  return ay < ax ? y : ret;
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to