Skip to content

Commit 1301bc4

Browse files
authored
[libc] Add is_fixed_point type trait. (#81263)
1 parent d592c8e commit 1301bc4

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

libc/include/llvm-libc-macros/stdfix-macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#ifdef __clang__
1313
#if (!defined(__cplusplus) || (__clang_major__ >= 18))
14-
// _Fract and _Accum types are avaiable
14+
// _Fract and _Accum types are available
1515
#define LIBC_COMPILER_HAS_FIXED_POINT
1616
#endif // __cplusplus
1717
#endif // __clang__

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ add_header_library(
122122
type_traits/is_convertible.h
123123
type_traits/is_destructible.h
124124
type_traits/is_enum.h
125+
type_traits/is_fixed_point.h
125126
type_traits/is_floating_point.h
126127
type_traits/is_function.h
127128
type_traits/is_integral.h
@@ -155,6 +156,7 @@ add_header_library(
155156
libc.src.__support.macros.attributes
156157
libc.src.__support.macros.config
157158
libc.src.__support.macros.properties.float
159+
libc.include.llvm-libc-macros.stdfix_macros
158160
)
159161

160162
add_header_library(

libc/src/__support/CPP/type_traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "src/__support/CPP/type_traits/is_convertible.h"
2929
#include "src/__support/CPP/type_traits/is_destructible.h"
3030
#include "src/__support/CPP/type_traits/is_enum.h"
31+
#include "src/__support/CPP/type_traits/is_fixed_point.h"
3132
#include "src/__support/CPP/type_traits/is_floating_point.h"
3233
#include "src/__support/CPP/type_traits/is_function.h"
3334
#include "src/__support/CPP/type_traits/is_integral.h"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===-- is_fixed_point type_traits ------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_FIXED_POINT_H
9+
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_FIXED_POINT_H
10+
11+
#include "src/__support/CPP/type_traits/is_same.h"
12+
#include "src/__support/CPP/type_traits/remove_cv.h"
13+
#include "src/__support/macros/attributes.h"
14+
15+
#include "include/llvm-libc-macros/stdfix-macros.h"
16+
17+
namespace LIBC_NAMESPACE::cpp {
18+
19+
// is_fixed_point
20+
#ifdef LIBC_COMPILER_HAS_FIXED_POINT
21+
template <typename T> struct is_fixed_point {
22+
private:
23+
template <typename Head, typename... Args>
24+
LIBC_INLINE static constexpr bool __is_unqualified_any_of() {
25+
return (... || is_same_v<remove_cv_t<Head>, Args>);
26+
}
27+
28+
public:
29+
LIBC_INLINE_VAR static constexpr bool value = __is_unqualified_any_of<
30+
T, short fract, fract, long fract, unsigned short fract, unsigned fract,
31+
unsigned long fract, short accum, accum, long accum, unsigned short accum,
32+
unsigned accum, unsigned long accum, short sat fract, sat fract,
33+
long sat fract, unsigned short sat fract, unsigned sat fract,
34+
unsigned long sat fract, short sat accum, sat accum, long sat accum,
35+
unsigned short sat accum, unsigned sat accum, unsigned long sat accum>();
36+
};
37+
#else
38+
template <typename T> struct is_fixed_point : false_type {};
39+
#endif // LIBC_COMPILER_HAS_FIXED_POINT
40+
41+
template <typename T>
42+
LIBC_INLINE_VAR constexpr bool is_fixed_point_v = is_fixed_point<T>::value;
43+
44+
} // namespace LIBC_NAMESPACE::cpp
45+
46+
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_INTEGRAL_H

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ libc_support_library(
296296
"src/__support/CPP/type_traits/is_convertible.h",
297297
"src/__support/CPP/type_traits/is_destructible.h",
298298
"src/__support/CPP/type_traits/is_enum.h",
299+
"src/__support/CPP/type_traits/is_fixed_point.h",
299300
"src/__support/CPP/type_traits/is_floating_point.h",
300301
"src/__support/CPP/type_traits/is_function.h",
301302
"src/__support/CPP/type_traits/is_integral.h",

0 commit comments

Comments
 (0)