Skip to content

Commit b448b38

Browse files
author
kr-2003
committed
changed the design pattern
1 parent 5d0229b commit b448b38

File tree

2 files changed

+40
-152
lines changed

2 files changed

+40
-152
lines changed

libc/src/__support/CPP/type_traits/is_signed.h

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,92 +11,37 @@
1111
#include "include/llvm-libc-macros/stdfix-macros.h"
1212
#include "src/__support/CPP/type_traits/bool_constant.h"
1313
#include "src/__support/CPP/type_traits/is_arithmetic.h"
14-
#include "src/__support/CPP/type_traits/is_fixed_point.h"
14+
#include "src/__support/CPP/type_traits/is_same.h"
15+
#include "src/__support/CPP/type_traits/remove_cv.h"
1516
#include "src/__support/macros/attributes.h"
1617
#include "src/__support/macros/config.h"
1718

1819
namespace LIBC_NAMESPACE_DECL {
1920
namespace cpp {
2021

21-
// Primary template: handles arithmetic and signed fixed-point types
22+
#ifndef LIBC_COMPILER_HAS_FIXED_POINT
2223
template <typename T>
2324
struct is_signed : bool_constant<(is_arithmetic_v<T> && (T(-1) < T(0)))> {
2425
LIBC_INLINE constexpr operator bool() const { return is_signed::value; }
2526
LIBC_INLINE constexpr bool operator()() const { return is_signed::value; }
2627
};
27-
28-
#ifdef LIBC_COMPILER_HAS_FIXED_POINT
29-
// Specializations for unsigned fixed-point types
30-
template <typename T, bool IsSigned>
31-
struct fixed_point_is_signed : bool_constant<IsSigned> {
32-
LIBC_INLINE constexpr operator bool() const {
33-
return fixed_point_is_signed::value;
34-
}
35-
LIBC_INLINE constexpr bool operator()() const {
36-
return fixed_point_is_signed::value;
28+
#else
29+
template <typename T> struct is_signed {
30+
private:
31+
template <typename Head, typename... Args>
32+
LIBC_INLINE static constexpr bool __is_unqualified_any_of() {
33+
return (... || is_same_v<remove_cv_t<Head>, Args>);
3734
}
38-
};
39-
template <> struct is_signed<fract> : fixed_point_is_signed<fract, true> {};
40-
template <>
41-
struct is_signed<unsigned short fract>
42-
: fixed_point_is_signed<unsigned short fract, false> {};
43-
template <>
44-
struct is_signed<unsigned fract>
45-
: fixed_point_is_signed<unsigned fract, false> {};
46-
template <>
47-
struct is_signed<unsigned long fract>
48-
: fixed_point_is_signed<unsigned long fract, false> {};
49-
template <>
50-
struct is_signed<short fract> : fixed_point_is_signed<short fract, true> {};
51-
template <>
52-
struct is_signed<long fract> : fixed_point_is_signed<long fract, true> {};
53-
template <> struct is_signed<accum> : fixed_point_is_signed<accum, true> {};
54-
template <>
55-
struct is_signed<unsigned short accum>
56-
: fixed_point_is_signed<unsigned short accum, false> {};
57-
template <>
58-
struct is_signed<unsigned accum>
59-
: fixed_point_is_signed<unsigned accum, false> {};
60-
template <>
61-
struct is_signed<unsigned long accum>
62-
: fixed_point_is_signed<unsigned long accum, false> {};
63-
template <>
64-
struct is_signed<short accum> : fixed_point_is_signed<short accum, true> {};
65-
template <>
66-
struct is_signed<long accum> : fixed_point_is_signed<long accum, true> {};
67-
template <>
68-
struct is_signed<sat fract> : fixed_point_is_signed<sat fract, true> {};
69-
template <>
70-
struct is_signed<unsigned short sat fract>
71-
: fixed_point_is_signed<unsigned short sat fract, false> {};
72-
template <>
73-
struct is_signed<unsigned sat fract>
74-
: fixed_point_is_signed<unsigned sat fract, false> {};
75-
template <>
76-
struct is_signed<unsigned long sat fract>
77-
: fixed_point_is_signed<unsigned long sat fract, false> {};
78-
template <>
79-
struct is_signed<short sat fract>
80-
: fixed_point_is_signed<short sat fract, true> {};
81-
template <>
82-
struct is_signed<long sat fract> : fixed_point_is_signed<long sat fract, true> {
83-
};
84-
template <>
85-
struct is_signed<sat accum> : fixed_point_is_signed<sat accum, true> {};
86-
template <>
87-
struct is_signed<unsigned short sat accum>
88-
: fixed_point_is_signed<unsigned short sat accum, false> {};
89-
template <>
90-
struct is_signed<unsigned sat accum>
91-
: fixed_point_is_signed<unsigned sat accum, false> {};
92-
template <>
93-
struct is_signed<unsigned long sat accum>
94-
: fixed_point_is_signed<unsigned long sat accum, false> {};
95-
template <>
96-
struct is_signed<short sat accum>
97-
: fixed_point_is_signed<short sat accum, true> {};
98-
template <>
99-
struct is_signed<long sat accum> : fixed_point_is_signed<long sat accum, true> {
35+
36+
public:
37+
LIBC_INLINE_VAR static constexpr bool value =
38+
(is_arithmetic_v<T> && (T(-1) < T(0))) ||
39+
__is_unqualified_any_of<T, short fract, fract, long fract, short accum,
40+
accum, long accum, short sat fract, sat fract,
41+
long sat fract, short sat accum, sat accum,
42+
long sat accum>();
43+
LIBC_INLINE constexpr operator bool() const { return is_signed::value; }
44+
LIBC_INLINE constexpr bool operator()() const { return is_signed::value; }
10045
};
10146
#endif // LIBC_COMPILER_HAS_FIXED_POINT
10247

libc/src/__support/CPP/type_traits/is_unsigned.h

Lines changed: 21 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,40 @@
1111
#include "include/llvm-libc-macros/stdfix-macros.h"
1212
#include "src/__support/CPP/type_traits/bool_constant.h"
1313
#include "src/__support/CPP/type_traits/is_arithmetic.h"
14-
#include "src/__support/CPP/type_traits/is_fixed_point.h"
14+
#include "src/__support/CPP/type_traits/is_same.h"
15+
#include "src/__support/CPP/type_traits/remove_cv.h"
1516
#include "src/__support/macros/attributes.h"
1617
#include "src/__support/macros/config.h"
1718

1819
namespace LIBC_NAMESPACE_DECL {
1920
namespace cpp {
2021

21-
// Primary template: handles arithmetic and signed fixed-point types
22+
#ifndef LIBC_COMPILER_HAS_FIXED_POINT
2223
template <typename T>
2324
struct is_unsigned : bool_constant<(is_arithmetic_v<T> && (T(-1) > T(0)))> {
2425
LIBC_INLINE constexpr operator bool() const { return is_unsigned::value; }
2526
LIBC_INLINE constexpr bool operator()() const { return is_unsigned::value; }
2627
};
27-
28-
#ifdef LIBC_COMPILER_HAS_FIXED_POINT
29-
// Specializations for unsigned fixed-point types
30-
template <typename T, bool IsUnsigned>
31-
struct fixed_point_is_unsigned : bool_constant<IsUnsigned> {
32-
LIBC_INLINE constexpr operator bool() const {
33-
return fixed_point_is_unsigned::value;
34-
}
35-
LIBC_INLINE constexpr bool operator()() const {
36-
return fixed_point_is_unsigned::value;
28+
#else
29+
template <typename T> struct is_unsigned {
30+
private:
31+
template <typename Head, typename... Args>
32+
LIBC_INLINE static constexpr bool __is_unqualified_any_of() {
33+
return (... || is_same_v<remove_cv_t<Head>, Args>);
3734
}
35+
36+
public:
37+
LIBC_INLINE_VAR static constexpr bool value =
38+
(is_arithmetic_v<T> && (T(-1) > T(0))) ||
39+
__is_unqualified_any_of<T, unsigned short fract, unsigned fract,
40+
unsigned long fract, unsigned short accum,
41+
unsigned accum, unsigned long accum,
42+
unsigned short sat fract, unsigned sat fract,
43+
unsigned long sat fract, unsigned short sat accum,
44+
unsigned sat accum, unsigned long sat accum>();
45+
LIBC_INLINE constexpr operator bool() const { return is_unsigned::value; }
46+
LIBC_INLINE constexpr bool operator()() const { return is_unsigned::value; }
3847
};
39-
template <>
40-
struct is_unsigned<fract> : fixed_point_is_unsigned<fract, false> {};
41-
template <>
42-
struct is_unsigned<unsigned short fract>
43-
: fixed_point_is_unsigned<unsigned short fract, true> {};
44-
template <>
45-
struct is_unsigned<unsigned fract>
46-
: fixed_point_is_unsigned<unsigned fract, true> {};
47-
template <>
48-
struct is_unsigned<unsigned long fract>
49-
: fixed_point_is_unsigned<unsigned long fract, true> {};
50-
template <>
51-
struct is_unsigned<short fract> : fixed_point_is_unsigned<short fract, false> {
52-
};
53-
template <>
54-
struct is_unsigned<long fract> : fixed_point_is_unsigned<long fract, false> {};
55-
template <>
56-
struct is_unsigned<accum> : fixed_point_is_unsigned<accum, false> {};
57-
template <>
58-
struct is_unsigned<unsigned short accum>
59-
: fixed_point_is_unsigned<unsigned short accum, true> {};
60-
template <>
61-
struct is_unsigned<unsigned accum>
62-
: fixed_point_is_unsigned<unsigned accum, true> {};
63-
template <>
64-
struct is_unsigned<unsigned long accum>
65-
: fixed_point_is_unsigned<unsigned long accum, true> {};
66-
template <>
67-
struct is_unsigned<short accum> : fixed_point_is_unsigned<short accum, false> {
68-
};
69-
template <>
70-
struct is_unsigned<long accum> : fixed_point_is_unsigned<long accum, false> {};
71-
template <>
72-
struct is_unsigned<sat fract> : fixed_point_is_unsigned<sat fract, false> {};
73-
template <>
74-
struct is_unsigned<unsigned short sat fract>
75-
: fixed_point_is_unsigned<unsigned short sat fract, true> {};
76-
template <>
77-
struct is_unsigned<unsigned sat fract>
78-
: fixed_point_is_unsigned<unsigned sat fract, true> {};
79-
template <>
80-
struct is_unsigned<unsigned long sat fract>
81-
: fixed_point_is_unsigned<unsigned long sat fract, true> {};
82-
template <>
83-
struct is_unsigned<short sat fract>
84-
: fixed_point_is_unsigned<short sat fract, false> {};
85-
template <>
86-
struct is_unsigned<long sat fract>
87-
: fixed_point_is_unsigned<long sat fract, false> {};
88-
template <>
89-
struct is_unsigned<sat accum> : fixed_point_is_unsigned<sat accum, false> {};
90-
template <>
91-
struct is_unsigned<unsigned short sat accum>
92-
: fixed_point_is_unsigned<unsigned short sat accum, true> {};
93-
template <>
94-
struct is_unsigned<unsigned sat accum>
95-
: fixed_point_is_unsigned<unsigned sat accum, true> {};
96-
template <>
97-
struct is_unsigned<unsigned long sat accum>
98-
: fixed_point_is_unsigned<unsigned long sat accum, true> {};
99-
template <>
100-
struct is_unsigned<short sat accum>
101-
: fixed_point_is_unsigned<short sat accum, false> {};
102-
template <>
103-
struct is_unsigned<long sat accum>
104-
: fixed_point_is_unsigned<long sat accum, false> {};
10548
#endif // LIBC_COMPILER_HAS_FIXED_POINT
10649

10750
template <typename T>

0 commit comments

Comments
 (0)