[libc-commits] [libc] 5aa6515 - [libc][math] Extend iscanonical macro to _Float16 and float128 (#204981)
via libc-commits
libc-commits at lists.llvm.org
Fri Aug 28 08:27:56 PDT 2026
Author: jofrn
Date: 2026-08-28T11:27:51-04:00
New Revision: 5aa651511dcf92376af21c7029e6cd4abef194dd
URL: https://github.com/llvm/llvm-project/commit/5aa651511dcf92376af21c7029e6cd4abef194dd
DIFF: https://github.com/llvm/llvm-project/commit/5aa651511dcf92376af21c7029e6cd4abef194dd.diff
LOG: [libc][math] Extend iscanonical macro to _Float16 and float128 (#204981)
iscanonical is a C23 type-generic macro, so the f16/f128 variants are
surfaced through it rather than as functions in the generated math.h.
float128 is only listed when distinct from long double (LDBL_MANT_DIG !=
113) to avoid two _Generic associations with compatible types.
Stacked below: https://github.com/llvm/llvm-project/pull/205016
Co-authored-by: lntue <lntue at google.com>
Added:
Modified:
libc/include/llvm-libc-macros/CMakeLists.txt
libc/include/llvm-libc-macros/math-function-macros.h
libc/test/include/CMakeLists.txt
libc/test/include/iscanonical_test.c
Removed:
################################################################################
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 848aa2683a947..aee4b019f0d5b 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -173,6 +173,7 @@ add_macro_header(
math-function-macros.h
DEPENDS
.math_macros
+ .float16_macros
)
add_macro_header(
diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 21d09f1f5e1a4..1fbcd603d2045 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
#define LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
+#include "float16-macros.h" // LIBC_TYPES_HAS_FLOAT16, LIBC_TYPES_HAS_FLOAT128
#include "math-macros.h"
#ifndef __cplusplus
@@ -17,11 +18,34 @@
float: issignalingf, \
double: issignaling, \
long double: issignalingl)(x)
+
+// 'iscanonical' is a C23 type-generic macro (7.12.3.1). The '_Float16' and
+// 'float128' associations are only added when those types exist, and 'float128'
+// only when it is distinct from 'long double' (otherwise _Generic would have
+// two associations with compatible types). The 'float128' guard mirrors
+// LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE in
+// src/__support/macros/properties/types.h, which public headers cannot include.
+#ifdef LIBC_TYPES_HAS_FLOAT16
+#define __LIBC_MATH_ISCANONICAL_F16 _Float16 : iscanonicalf16,
+#else
+#define __LIBC_MATH_ISCANONICAL_F16
+#endif
+#if defined(LIBC_TYPES_HAS_FLOAT128) && (LDBL_MANT_DIG != 113)
+#define __LIBC_MATH_ISCANONICAL_F128 \
+ float128: \
+ iscanonicalf128,
+#else
+#define __LIBC_MATH_ISCANONICAL_F128
+#endif
+// clang-format off
#define iscanonical(x) \
_Generic((x), \
+ __LIBC_MATH_ISCANONICAL_F16 \
+ __LIBC_MATH_ISCANONICAL_F128 \
float: iscanonicalf, \
double: iscanonical, \
long double: iscanonicall)(x)
+// clang-format on
#endif
#define isfinite(x) __builtin_isfinite(x)
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 68229b70e15bd..c2cfa416972c3 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -441,6 +441,8 @@ add_libc_test(
# libc.src.math.iscanonical
# libc.src.math.iscanonicalf
# libc.src.math.iscanonicall
+# libc.src.math.iscanonicalf16
+# libc.src.math.iscanonicalf128
# )
add_libc_test(
diff --git a/libc/test/include/iscanonical_test.c b/libc/test/include/iscanonical_test.c
index 670b48b0008e3..4fbf0a42e3495 100644
--- a/libc/test/include/iscanonical_test.c
+++ b/libc/test/include/iscanonical_test.c
@@ -5,9 +5,17 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+#include "include/llvm-libc-macros/float16-macros.h"
+
int iscanonical(double);
int iscanonicalf(float);
int iscanonicall(long double);
+#ifdef LIBC_TYPES_HAS_FLOAT16
+int iscanonicalf16(_Float16);
+#endif
+#if defined(LIBC_TYPES_HAS_FLOAT128) && (LDBL_MANT_DIG != 113)
+int iscanonicalf128(float128);
+#endif
#include "include/llvm-libc-macros/math-function-macros.h"
@@ -24,6 +32,14 @@ int main(void) {
assert(iscanonical(1.819f) == 1);
assert(iscanonical(-1.726) == 1);
assert(iscanonical(1.426L) == 1);
+#ifdef LIBC_TYPES_HAS_FLOAT16
+ assert(iscanonical(__builtin_nansf16("")) == 0);
+ assert(iscanonical((_Float16)1.0) == 1);
+#endif
+#if defined(LIBC_TYPES_HAS_FLOAT128) && (LDBL_MANT_DIG != 113)
+ assert(iscanonical(__builtin_nansf128("")) == 0);
+ assert(iscanonical((float128)1.0) == 1);
+#endif
return 0;
}
#endif
More information about the libc-commits
mailing list