[libc-commits] [libc] [llvm] [libc] Add is_fixed_point type trait. (PR #81263)

via libc-commits libc-commits at lists.llvm.org
Tue Feb 13 17:37:44 PST 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/81263

>From 22d552952a0430908f0f28268cbd322684883577 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Fri, 9 Feb 2024 16:14:16 +0000
Subject: [PATCH 1/3] [libc] Add `is_fixed_point` type trait.

---
 libc/src/__support/CPP/CMakeLists.txt         |  2 +
 libc/src/__support/CPP/type_traits.h          |  1 +
 .../CPP/type_traits/is_fixed_point.h          | 43 +++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 libc/src/__support/CPP/type_traits/is_fixed_point.h

diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index f10bb936047eb9..d747412791bd8e 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -122,6 +122,7 @@ add_header_library(
     type_traits/is_convertible.h
     type_traits/is_destructible.h
     type_traits/is_enum.h
+    type_traits/is_fixed_point.h
     type_traits/is_floating_point.h
     type_traits/is_function.h
     type_traits/is_integral.h
@@ -155,6 +156,7 @@ add_header_library(
     libc.src.__support.macros.attributes
     libc.src.__support.macros.config
     libc.src.__support.macros.properties.float
+    libc.include.llvm-libc-macros.stdfix_macros
 )
 
 add_header_library(
diff --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index 1eb2f34ebee37f..697cf79d6ccc59 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -28,6 +28,7 @@
 #include "src/__support/CPP/type_traits/is_convertible.h"
 #include "src/__support/CPP/type_traits/is_destructible.h"
 #include "src/__support/CPP/type_traits/is_enum.h"
+#include "src/__support/CPP/type_traits/is_fixed_point.h"
 #include "src/__support/CPP/type_traits/is_floating_point.h"
 #include "src/__support/CPP/type_traits/is_function.h"
 #include "src/__support/CPP/type_traits/is_integral.h"
diff --git a/libc/src/__support/CPP/type_traits/is_fixed_point.h b/libc/src/__support/CPP/type_traits/is_fixed_point.h
new file mode 100644
index 00000000000000..01be65792ae213
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_fixed_point.h
@@ -0,0 +1,43 @@
+//===-- is_fixed_point type_traits ------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_FIXED_POINT_H
+#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_FIXED_POINT_H
+
+#include "src/__support/CPP/type_traits/is_same.h"
+#include "src/__support/CPP/type_traits/remove_cv.h"
+#include "src/__support/macros/attributes.h"
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+
+namespace LIBC_NAMESPACE::cpp {
+
+// is_fixed_point
+#ifdef LIBC_COMPILER_HAS_FIXED_POINT
+template <typename T> struct is_fixed_point {
+private:
+  template <typename Head, typename... Args>
+  LIBC_INLINE_VAR static constexpr bool __is_unqualified_any_of() {
+    return (... || is_same_v<remove_cv_t<Head>, Args>);
+  }
+
+public:
+  LIBC_INLINE_VAR static constexpr bool value = __is_unqualified_any_of<
+      T, short fract, fract, long fract, unsigned short fract, unsigned fract,
+      unsigned long fract, short accum, accum, long accum, unsigned short accum,
+      unsigned accum, unsigned long accum>();
+};
+#else
+template <typename T> struct is_fixed_point : false_type {};
+#endif // LIBC_COMPILER_HAS_FIXED_POINT
+
+template <typename T>
+LIBC_INLINE_VAR constexpr bool is_fixed_point_v = is_fixed_point<T>::value;
+
+} // namespace LIBC_NAMESPACE::cpp
+
+#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_INTEGRAL_H

>From 985d4581b4b05528130376d3645dc6e0b6424556 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Fri, 9 Feb 2024 19:15:41 +0000
Subject: [PATCH 2/3] Add saturate variants to the list.

---
 libc/src/__support/CPP/type_traits/is_fixed_point.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/type_traits/is_fixed_point.h b/libc/src/__support/CPP/type_traits/is_fixed_point.h
index 01be65792ae213..9114e52173467d 100644
--- a/libc/src/__support/CPP/type_traits/is_fixed_point.h
+++ b/libc/src/__support/CPP/type_traits/is_fixed_point.h
@@ -29,7 +29,10 @@ template <typename T> struct is_fixed_point {
   LIBC_INLINE_VAR static constexpr bool value = __is_unqualified_any_of<
       T, short fract, fract, long fract, unsigned short fract, unsigned fract,
       unsigned long fract, short accum, accum, long accum, unsigned short accum,
-      unsigned accum, unsigned long accum>();
+      unsigned accum, unsigned long accum, short sat fract, sat fract,
+      long sat fract, unsigned short sat fract, unsigned sat fract,
+      unsigned long sat fract, short sat accum, sat accum, long sat accum,
+      unsigned short sat accum, unsigned sat accum, unsigned long sat accum>();
 };
 #else
 template <typename T> struct is_fixed_point : false_type {};

>From af316fb51db6ab561f9567c4a7ccf595de5c9c98 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 14 Feb 2024 01:37:28 +0000
Subject: [PATCH 3/3] Address comments.

---
 libc/include/llvm-libc-macros/stdfix-macros.h       | 2 +-
 libc/src/__support/CPP/type_traits/is_fixed_point.h | 2 +-
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel   | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/include/llvm-libc-macros/stdfix-macros.h b/libc/include/llvm-libc-macros/stdfix-macros.h
index 7cb74adc3999fe..9c83dbc8ef5463 100644
--- a/libc/include/llvm-libc-macros/stdfix-macros.h
+++ b/libc/include/llvm-libc-macros/stdfix-macros.h
@@ -11,7 +11,7 @@
 
 #ifdef __clang__
 #if (!defined(__cplusplus) || (__clang_major__ >= 18))
-// _Fract and _Accum types are avaiable
+// _Fract and _Accum types are available
 #define LIBC_COMPILER_HAS_FIXED_POINT
 #endif // __cplusplus
 #endif // __clang__
diff --git a/libc/src/__support/CPP/type_traits/is_fixed_point.h b/libc/src/__support/CPP/type_traits/is_fixed_point.h
index 9114e52173467d..317ba39748b7de 100644
--- a/libc/src/__support/CPP/type_traits/is_fixed_point.h
+++ b/libc/src/__support/CPP/type_traits/is_fixed_point.h
@@ -21,7 +21,7 @@ namespace LIBC_NAMESPACE::cpp {
 template <typename T> struct is_fixed_point {
 private:
   template <typename Head, typename... Args>
-  LIBC_INLINE_VAR static constexpr bool __is_unqualified_any_of() {
+  LIBC_INLINE static constexpr bool __is_unqualified_any_of() {
     return (... || is_same_v<remove_cv_t<Head>, Args>);
   }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 30c180b5a8b473..2cdd5f0ca58039 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -296,6 +296,7 @@ libc_support_library(
         "src/__support/CPP/type_traits/is_convertible.h",
         "src/__support/CPP/type_traits/is_destructible.h",
         "src/__support/CPP/type_traits/is_enum.h",
+        "src/__support/CPP/type_traits/is_fixed_point.h",
         "src/__support/CPP/type_traits/is_floating_point.h",
         "src/__support/CPP/type_traits/is_function.h",
         "src/__support/CPP/type_traits/is_integral.h",



More information about the libc-commits mailing list