summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlntue <35648136+lntue@users.noreply.github.com>2024-02-14 14:44:09 -0500
committerGitHub <noreply@github.com>2024-02-14 14:44:09 -0500
commit1301bc46aea14297478bd13bcacff429e2a18c04 (patch)
treea4d88d3a51975e01aafa7acb3f5f40b9611df6ba
parentd592c8ec8f7138dcbde6f0890d048e59cba95041 (diff)
[libc] Add is_fixed_point type trait. (#81263)
-rw-r--r--libc/include/llvm-libc-macros/stdfix-macros.h2
-rw-r--r--libc/src/__support/CPP/CMakeLists.txt2
-rw-r--r--libc/src/__support/CPP/type_traits.h1
-rw-r--r--libc/src/__support/CPP/type_traits/is_fixed_point.h46
-rw-r--r--utils/bazel/llvm-project-overlay/libc/BUILD.bazel1
5 files changed, 51 insertions, 1 deletions
diff --git a/libc/include/llvm-libc-macros/stdfix-macros.h b/libc/include/llvm-libc-macros/stdfix-macros.h
index 7cb74adc3999..9c83dbc8ef54 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/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index f10bb936047e..d747412791bd 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 1eb2f34ebee3..697cf79d6ccc 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 000000000000..317ba39748b7
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/is_fixed_point.h
@@ -0,0 +1,46 @@
+//===-- 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 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, 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 {};
+#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
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index fde2bac746f4..64e788eae34e 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",