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

via libc-commits libc-commits at lists.llvm.org
Tue Feb 13 17:33:26 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/2] [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/2] 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 {};



More information about the libc-commits mailing list