Skip to content

[libc] Add is_fixed_point type trait. #81263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libc/include/llvm-libc-macros/stdfix-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
2 changes: 2 additions & 0 deletions libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/CPP/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 46 additions & 0 deletions libc/src/__support/CPP/type_traits/is_fixed_point.h
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down