[libclc] [libclc] Move logb/ilogb to CLC library; optimize (PR #128028)

Fraser Cormack via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 20 08:36:13 PST 2025


https://github.com/frasercrmck created https://github.com/llvm/llvm-project/pull/128028

This commit moves the logb and ilogb builtins to the CLC library.

It simultaneously optimizes them both for vector types and for half types. Vector types were being scalarized in some cases. Half types were previously promoting to float, whereas this commit provides them a native implementation.

Everything passes the OpenCL-CTS.

I had to intuit some magic numbers used by these implementations in order to generate the half variants. I gave them clearer definitions derived from what I believe are their actual component numbers, but named them 'magic' to convey that they weren't derived from first principles.

>From 5acc4a88d800b5738c194d25fc6c4247dc6c4684 Mon Sep 17 00:00:00 2001
From: Fraser Cormack <fraser at codeplay.com>
Date: Thu, 20 Feb 2025 08:42:36 +0000
Subject: [PATCH] [libclc] Move logb/ilogb to CLC library; optimize

This commit moves the logb and ilogb builtins to the CLC library.

It simultaneously optimizes them both for vector types and for half
types. Half types were previously promoting to float, whereas this
commit provides them a native implementation.

Everything passes the OpenCL-CTS.

I had to intuit some magic numbers used by these implementations in
order to generate the half variants. I gave them clearer definitions
derived from what I believe are their actual component numbers, but
named them 'magic' to convey that they weren't derived from first
principles.
---
 libclc/clc/include/clc/math/clc_ilogb.h   |  9 +++
 libclc/clc/include/clc/math/clc_ilogb.inc |  1 +
 libclc/clc/include/clc/math/clc_logb.h    | 11 +++
 libclc/clc/include/clc/math/math.h        | 22 ++++++
 libclc/clc/lib/generic/SOURCES            |  2 +
 libclc/clc/lib/generic/math/clc_ilogb.cl  | 32 +++++++++
 libclc/clc/lib/generic/math/clc_ilogb.inc | 73 +++++++++++++++++++
 libclc/clc/lib/generic/math/clc_logb.cl   |  9 +++
 libclc/clc/lib/generic/math/clc_logb.inc  | 46 ++++++++++++
 libclc/generic/lib/math/ilogb.cl          | 86 +----------------------
 libclc/generic/lib/math/ilogb.inc         |  3 +
 libclc/generic/lib/math/logb.cl           | 41 ++---------
 12 files changed, 215 insertions(+), 120 deletions(-)
 create mode 100644 libclc/clc/include/clc/math/clc_ilogb.h
 create mode 100644 libclc/clc/include/clc/math/clc_ilogb.inc
 create mode 100644 libclc/clc/include/clc/math/clc_logb.h
 create mode 100644 libclc/clc/lib/generic/math/clc_ilogb.cl
 create mode 100644 libclc/clc/lib/generic/math/clc_ilogb.inc
 create mode 100644 libclc/clc/lib/generic/math/clc_logb.cl
 create mode 100644 libclc/clc/lib/generic/math/clc_logb.inc
 create mode 100644 libclc/generic/lib/math/ilogb.inc

diff --git a/libclc/clc/include/clc/math/clc_ilogb.h b/libclc/clc/include/clc/math/clc_ilogb.h
new file mode 100644
index 0000000000000..0953c2ed1c25e
--- /dev/null
+++ b/libclc/clc/include/clc/math/clc_ilogb.h
@@ -0,0 +1,9 @@
+#ifndef __CLC_MATH_CLC_ILOGB_H__
+#define __CLC_MATH_CLC_ILOGB_H__
+
+#define __CLC_BODY <clc/math/clc_ilogb.inc>
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+
+#endif // __CLC_MATH_CLC_ILOGB_H__
diff --git a/libclc/clc/include/clc/math/clc_ilogb.inc b/libclc/clc/include/clc/math/clc_ilogb.inc
new file mode 100644
index 0000000000000..7d90973cc7e47
--- /dev/null
+++ b/libclc/clc/include/clc/math/clc_ilogb.inc
@@ -0,0 +1 @@
+_CLC_OVERLOAD _CLC_DECL __CLC_INTN __clc_ilogb(__CLC_GENTYPE x);
diff --git a/libclc/clc/include/clc/math/clc_logb.h b/libclc/clc/include/clc/math/clc_logb.h
new file mode 100644
index 0000000000000..d34a0391615ff
--- /dev/null
+++ b/libclc/clc/include/clc/math/clc_logb.h
@@ -0,0 +1,11 @@
+#ifndef __CLC_MATH_CLC_LOGB_H__
+#define __CLC_MATH_CLC_LOGB_H__
+
+#define __CLC_FUNCTION __clc_logb
+#define __CLC_BODY <clc/shared/unary_decl.inc>
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_LOGB_H__
diff --git a/libclc/clc/include/clc/math/math.h b/libclc/clc/include/clc/math/math.h
index ed37af237bf82..43941692807b4 100644
--- a/libclc/clc/include/clc/math/math.h
+++ b/libclc/clc/include/clc/math/math.h
@@ -66,6 +66,7 @@ bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);
 #define INDEFBITPATT_SP32 0xffc00000
 #define PINFBITPATT_SP32 0x7f800000
 #define NINFBITPATT_SP32 0xff800000
+#define NUMEXPBITS_SP32 8
 #define EXPBIAS_SP32 127
 #define EXPSHIFTBITS_SP32 23
 #define BIASEDEMIN_SP32 1
@@ -76,6 +77,8 @@ bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);
 #define MANTLENGTH_SP32 24
 #define BASEDIGITS_SP32 7
 
+#define LOG_MAGIC_NUM_SP32 (1 + NUMEXPBITS_SP32 - EXPBIAS_SP32)
+
 _CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
   int ix = __clc_as_int(x);
   if (!__clc_fp32_subnormals_supported() && ((ix & EXPBITS_SP32) == 0) &&
@@ -100,6 +103,7 @@ _CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
 #define INDEFBITPATT_DP64 0xfff8000000000000L
 #define PINFBITPATT_DP64 0x7ff0000000000000L
 #define NINFBITPATT_DP64 0xfff0000000000000L
+#define NUMEXPBITS_DP64 11
 #define EXPBIAS_DP64 1023
 #define EXPSHIFTBITS_DP64 52
 #define BIASEDEMIN_DP64 1
@@ -110,8 +114,26 @@ _CLC_OVERLOAD _CLC_INLINE float __clc_flush_denormal_if_not_supported(float x) {
 #define MANTLENGTH_DP64 53
 #define BASEDIGITS_DP64 15
 
+#define LOG_MAGIC_NUM_DP64 (1 + NUMEXPBITS_DP64 - EXPBIAS_DP64)
+
 #endif // cl_khr_fp64
 
+#ifdef cl_khr_fp16
+
+#define SIGNBIT_FP16 0x8000
+#define EXSIGNBIT_FP16 0x7fff
+#define EXPBITS_FP16 0x7c00
+#define MANTBITS_FP16 0x03ff
+#define PINFBITPATT_FP16 0x7c00
+#define NINFBITPATT_FP16 0xfc00
+#define NUMEXPBITS_FP16 5
+#define EXPBIAS_FP16 15
+#define EXPSHIFTBITS_FP16 10
+
+#define LOG_MAGIC_NUM_FP16 (1 + NUMEXPBITS_FP16 - EXPBIAS_FP16)
+
+#endif // cl_khr_fp16
+
 #define ALIGNED(x) __attribute__((aligned(x)))
 
 #endif // __CLC_MATH_MATH_H__
diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index f7fdba0a341ed..6b158160799db 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -22,6 +22,8 @@ math/clc_copysign.cl
 math/clc_fabs.cl
 math/clc_floor.cl
 math/clc_frexp.cl
+math/clc_ilogb.cl
+math/clc_logb.cl
 math/clc_mad.cl
 math/clc_modf.cl
 math/clc_nextafter.cl
diff --git a/libclc/clc/lib/generic/math/clc_ilogb.cl b/libclc/clc/lib/generic/math/clc_ilogb.cl
new file mode 100644
index 0000000000000..be112f38a4ee1
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_ilogb.cl
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2015 Advanced Micro Devices, Inc.
+ * Copyright (c) 2016 Aaron Watry
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <clc/clc_convert.h>
+#include <clc/clcmacro.h>
+#include <clc/float/definitions.h>
+#include <clc/integer/clc_clz.h>
+#include <clc/internal/clc.h>
+#include <clc/math/math.h>
+
+#define __CLC_BODY <clc_ilogb.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/math/clc_ilogb.inc b/libclc/clc/lib/generic/math/clc_ilogb.inc
new file mode 100644
index 0000000000000..e4de616fb38b2
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_ilogb.inc
@@ -0,0 +1,73 @@
+#if __CLC_FPSIZE == 32
+
+_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) {
+  __CLC_UINTN ux = __CLC_AS_UINTN(x);
+  __CLC_UINTN ax = ux & EXSIGNBIT_SP32;
+  __CLC_INTN rs = (__CLC_INTN)LOG_MAGIC_NUM_SP32 -
+                  __CLC_AS_INTN(__clc_clz(ux & MANTBITS_SP32));
+  __CLC_INTN r = __CLC_AS_INTN(ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
+  r = ax < 0x00800000U ? rs : r;
+  r = ax == 0 ? FP_ILOGB0 : r;
+
+  // We could merge those 2 tests and have:
+  //
+  //    r = ax >= EXPBITS_SP32 ? 0x7fffffff : r
+  //
+  // since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
+  // FP_ILOGBNAN can change without requiring changes to __clc_ilogb() code.
+  r = ax > EXPBITS_SP32 ? FP_ILOGBNAN : r;
+  r = ax == EXPBITS_SP32 ? 0x7fffffff : r;
+  return r;
+}
+
+#endif
+
+#if __CLC_FPSIZE == 64
+
+_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) {
+  __CLC_ULONGN ux = __CLC_AS_ULONGN(x);
+  __CLC_ULONGN ax = ux & ~SIGNBIT_DP64;
+  __CLC_INTN rs = (__CLC_INTN)LOG_MAGIC_NUM_DP64 -
+                  __CLC_CONVERT_INTN(__clc_clz(ax & MANTBITS_DP64));
+  __CLC_INTN r = __CLC_CONVERT_INTN(ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
+  r = __CLC_CONVERT_INTN(ax < 0x0010000000000000UL) ? rs : r;
+  r = __CLC_CONVERT_INTN(ax == 0UL) ? (__CLC_INTN)FP_ILOGB0 : r;
+
+  // We could merge those 2 tests and have:
+  //
+  //    r = ax >= 0x7ff0000000000000UL ? 0x7fffffff : r
+  //
+  // since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
+  // FP_ILOGBNAN can change without requiring changes to __clc_ilogb() code.
+  r = __CLC_CONVERT_INTN(ax > 0x7ff0000000000000UL) ? FP_ILOGBNAN : r;
+  r = __CLC_CONVERT_INTN(ax == 0x7ff0000000000000UL) ? 0x7fffffff : r;
+  return r;
+}
+
+#endif
+
+#if __CLC_FPSIZE == 16
+
+_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) {
+  __CLC_USHORTN ux = __CLC_AS_USHORTN(x);
+  __CLC_USHORTN ax = ux & (__CLC_USHORTN)EXSIGNBIT_FP16;
+  __CLC_USHORTN mantx = ux & (__CLC_USHORTN)MANTBITS_FP16;
+  __CLC_INTN rs =
+      (__CLC_INTN)LOG_MAGIC_NUM_FP16 - __CLC_CONVERT_INTN(__clc_clz(mantx));
+  __CLC_INTN r =
+      __CLC_CONVERT_INTN(ax >> (__CLC_USHORTN)EXPSHIFTBITS_FP16) - EXPBIAS_FP16;
+  r = __CLC_CONVERT_INTN(ax < (__CLC_USHORTN)0x0400U) ? rs : r;
+  r = __CLC_CONVERT_INTN(ax == (__CLC_USHORTN)0) ? (__CLC_INTN)FP_ILOGB0 : r;
+
+  // We could merge those 2 tests and have:
+  //
+  //    r = ax >= EXPBITS_FP16 ? 0x7fffffff : r
+  //
+  // since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
+  // FP_ILOGBNAN can change without requiring changes to __clc_ilogb() code.
+  r = __CLC_CONVERT_INTN(ax > (__CLC_USHORTN)EXPBITS_FP16) ? FP_ILOGBNAN : r;
+  r = __CLC_CONVERT_INTN(ax == (__CLC_USHORTN)EXPBITS_FP16) ? 0x7fffffff : r;
+  return r;
+}
+
+#endif
diff --git a/libclc/clc/lib/generic/math/clc_logb.cl b/libclc/clc/lib/generic/math/clc_logb.cl
new file mode 100644
index 0000000000000..50c63f5ea2b73
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_logb.cl
@@ -0,0 +1,9 @@
+#include <clc/clc_convert.h>
+#include <clc/clcmacro.h>
+#include <clc/float/definitions.h>
+#include <clc/integer/clc_clz.h>
+#include <clc/internal/clc.h>
+#include <clc/math/math.h>
+
+#define __CLC_BODY <clc_logb.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/math/clc_logb.inc b/libclc/clc/lib/generic/math/clc_logb.inc
new file mode 100644
index 0000000000000..3d76b7ca796ee
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_logb.inc
@@ -0,0 +1,46 @@
+#if __CLC_FPSIZE == 32
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_logb(__CLC_GENTYPE x) {
+  __CLC_INTN ax = __CLC_AS_INTN(x) & EXSIGNBIT_SP32;
+  __CLC_GENTYPE s = __CLC_CONVERT_GENTYPE(LOG_MAGIC_NUM_SP32 - __clc_clz(ax));
+  __CLC_GENTYPE r =
+      __CLC_CONVERT_GENTYPE((ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32);
+  r = ax >= PINFBITPATT_SP32 ? __CLC_AS_GENTYPE(ax) : r;
+  r = ax < 0x00800000 ? s : r;
+  r = ax == 0 ? __CLC_AS_GENTYPE((__CLC_INTN)NINFBITPATT_SP32) : r;
+  return r;
+}
+
+#endif
+
+#if __CLC_FPSIZE == 64
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_logb(__CLC_GENTYPE x) {
+  __CLC_LONGN ax = __CLC_AS_LONGN(x) & EXSIGNBIT_DP64;
+  __CLC_GENTYPE s = __CLC_CONVERT_GENTYPE(LOG_MAGIC_NUM_DP64 - __clc_clz(ax));
+  __CLC_GENTYPE r =
+      __CLC_CONVERT_GENTYPE((ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
+  r = ax >= PINFBITPATT_DP64 ? __CLC_AS_GENTYPE(ax) : r;
+  r = ax < 0x0010000000000000L ? s : r;
+  r = ax == 0L ? __CLC_AS_GENTYPE((__CLC_LONGN)NINFBITPATT_DP64) : r;
+  return r;
+}
+
+#endif
+
+#if __CLC_FPSIZE == 16
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_logb(__CLC_GENTYPE x) {
+  __CLC_SHORTN ax = __CLC_AS_SHORTN(x) & (__CLC_SHORTN)EXSIGNBIT_FP16;
+  __CLC_GENTYPE s = __CLC_CONVERT_GENTYPE((__CLC_SHORTN)LOG_MAGIC_NUM_FP16 -
+                                          (__CLC_SHORTN)__clc_clz(ax));
+  __CLC_GENTYPE r = __CLC_CONVERT_GENTYPE(
+      (ax >> (__CLC_SHORTN)EXPSHIFTBITS_FP16) - (__CLC_SHORTN)EXPBIAS_FP16);
+  r = ax >= (__CLC_SHORTN)PINFBITPATT_FP16 ? __CLC_AS_GENTYPE(ax) : r;
+  r = ax < (__CLC_SHORTN)0x0400 ? s : r;
+  r = ax == (__CLC_SHORTN)0 ? __CLC_AS_GENTYPE((__CLC_SHORTN)NINFBITPATT_FP16)
+                            : r;
+  return r;
+}
+
+#endif
diff --git a/libclc/generic/lib/math/ilogb.cl b/libclc/generic/lib/math/ilogb.cl
index c9a5014a975db..d2f1609c630ca 100644
--- a/libclc/generic/lib/math/ilogb.cl
+++ b/libclc/generic/lib/math/ilogb.cl
@@ -1,85 +1,5 @@
-/*
- * Copyright (c) 2015 Advanced Micro Devices, Inc.
- * Copyright (c) 2016 Aaron Watry
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
 #include <clc/clc.h>
-#include <clc/clcmacro.h>
-#include <clc/math/math.h>
-
-_CLC_OVERLOAD _CLC_DEF int ilogb(float x) {
-    uint ux = as_uint(x);
-    uint ax = ux & EXSIGNBIT_SP32;
-    int rs = -118 - (int) clz(ux & MANTBITS_SP32);
-    int r = (int) (ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
-    r = ax < 0x00800000U ? rs : r;
-    r = ax == 0 ? FP_ILOGB0 : r;
-
-    // We could merge those 2 tests and have:
-    //
-    //    r = ax >= EXPBITS_SP32 ? 0x7fffffff : r
-    //
-    // since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
-    // FP_ILOGBNAN can change without requiring changes to ilogb() code.
-    r = ax > EXPBITS_SP32 ? FP_ILOGBNAN : r;
-    r = ax == EXPBITS_SP32 ? 0x7fffffff : r;
-    return r;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, ilogb, float);
-
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_OVERLOAD _CLC_DEF int ilogb(double x) {
-    ulong ux = as_ulong(x);
-    ulong ax = ux & ~SIGNBIT_DP64;
-    int r = (int) (ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
-    int rs = -1011 - (int) clz(ax & MANTBITS_DP64);
-    r = ax < 0x0010000000000000UL ? rs : r;
-    r = ax == 0UL ? FP_ILOGB0 : r;
-
-    // We could merge those 2 tests and have:
-    //
-    //    r = ax >= 0x7ff0000000000000UL ? 0x7fffffff : r
-    //
-    // since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
-    // FP_ILOGBNAN can change without requiring changes to ilogb() code.
-    r = ax > 0x7ff0000000000000UL ? FP_ILOGBNAN : r;
-    r = ax == 0x7ff0000000000000UL ? 0x7fffffff : r;
-    return r;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, ilogb, double);
-
-#endif // cl_khr_fp64
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_OVERLOAD _CLC_DEF int ilogb(half x) {
-    return ilogb((float)x);
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, ilogb, half);
+#include <clc/math/clc_ilogb.h>
 
-#endif
+#define __CLC_BODY "ilogb.inc"
+#include <clc/math/gentype.inc>
diff --git a/libclc/generic/lib/math/ilogb.inc b/libclc/generic/lib/math/ilogb.inc
new file mode 100644
index 0000000000000..b2a4c32f76a88
--- /dev/null
+++ b/libclc/generic/lib/math/ilogb.inc
@@ -0,0 +1,3 @@
+_CLC_OVERLOAD _CLC_DEF __CLC_INTN ilogb(__CLC_GENTYPE x) {
+  return __clc_ilogb(x);
+}
diff --git a/libclc/generic/lib/math/logb.cl b/libclc/generic/lib/math/logb.cl
index ae3813225265f..338c43d1f53de 100644
--- a/libclc/generic/lib/math/logb.cl
+++ b/libclc/generic/lib/math/logb.cl
@@ -1,39 +1,6 @@
 #include <clc/clc.h>
-#include <clc/clcmacro.h>
-#include <clc/math/math.h>
+#include <clc/math/clc_logb.h>
 
-_CLC_OVERLOAD _CLC_DEF float logb(float x) {
-    int ax = as_int(x) & EXSIGNBIT_SP32;
-    float s = -118 - clz(ax);
-    float r = (ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
-    r = ax >= PINFBITPATT_SP32 ? as_float(ax) : r;
-    r = ax < 0x00800000 ? s : r;
-    r = ax == 0 ? as_float(NINFBITPATT_SP32) : r;
-    return r;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, logb, float);
-
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_OVERLOAD _CLC_DEF double logb(double x) {
-    long ax = as_long(x) & EXSIGNBIT_DP64;
-    double s = -1011L - clz(ax);
-    double r = (int) (ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
-    r = ax >= PINFBITPATT_DP64 ? as_double(ax) : r;
-    r = ax < 0x0010000000000000L ? s : r;
-    r = ax == 0L ? as_double(NINFBITPATT_DP64) : r;
-    return r;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, logb, double)
-#endif
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN_FP16(logb)
-
-#endif
+#define FUNCTION logb
+#define __CLC_BODY <clc/shared/unary_def.inc>
+#include <clc/math/gentype.inc>



More information about the cfe-commits mailing list