* [PATCH v11 00/40] Optimize type traits performance
@ 2023-09-14 6:42 Ken Matsui
2023-09-14 6:42 ` [PATCH v11 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
` (40 more replies)
0 siblings, 41 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch series optimizes type traits performance by implementing
built-in type traits and using them in libstdc++. This patch version
is bumped from the highest version among my separated patches.
Changes in v11:
* Merge all patches into one patch series
* Rebase on top of trunk
* Unify commit message style
* Use _GLIBCXX_USE_BUILTIN_TRAIT
Ken Matsui (40):
c++: Sort built-in identifiers alphabetically
c++: Implement __is_const built-in trait
libstdc++: Optimize is_const trait performance
c++: Implement __is_volatile built-in trait
libstdc++: Optimize is_volatile trait performance
c++: Implement __is_array built-in trait
libstdc++: Optimize is_array trait performance
c++: Implement __is_unbounded_array built-in trait
libstdc++: Optimize is_unbounded_array trait performance
c++: Implement __is_bounded_array built-in trait
libstdc++: Optimize is_bounded_array trait performance
c++: Implement __is_scoped_enum built-in trait
libstdc++: Optimize is_scoped_enum trait performance
c++: Implement __is_member_pointer built-in trait
libstdc++: Optimize is_member_pointer trait performance
c, c++: Use 16 bits for all use of enum rid for more keyword space
c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly
c++: Implement __is_member_function_pointer built-in trait
libstdc++: Optimize is_member_function_pointer trait performance
c++: Implement __is_member_object_pointer built-in trait
libstdc++: Optimize is_member_object_pointer trait performance
c++: Implement __is_reference built-in trait
libstdc++: Optimize is_reference trait performance
c++: Implement __is_function built-in trait
libstdc++: Optimize is_function trait performance
libstdc++: Optimize is_object trait performance
c++: Implement __remove_pointer built-in trait
libstdc++: Optimize remove_pointer trait performance
c++, libstdc++: Implement __is_pointer built-in trait
libstdc++: Optimize is_pointer trait performance
c++, libstdc++: Implement __is_arithmetic built-in trait
libstdc++: Optimize is_arithmetic trait performance
libstdc++: Optimize is_fundamental trait performance
libstdc++: Optimize is_compound trait performance
c++: Implement __is_unsigned built-in trait
libstdc++: Optimize is_unsigned trait performance
c++, libstdc++: Implement __is_signed built-in trait
libstdc++: Optimize is_signed trait performance
c++, libstdc++: Implement __is_scalar built-in trait
libstdc++: Optimize is_scalar trait performance
gcc/c-family/c-common.h | 2 +-
gcc/c-family/c-indentation.h | 2 +-
gcc/c/c-parser.cc | 6 +-
gcc/c/c-parser.h | 6 +-
gcc/cp/constraint.cc | 112 +++++--
gcc/cp/cp-trait.def | 27 +-
gcc/cp/parser.h | 8 +-
gcc/cp/semantics.cc | 149 ++++++---
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 117 ++++++--
gcc/testsuite/g++.dg/ext/is_arithmetic.C | 33 ++
gcc/testsuite/g++.dg/ext/is_array.C | 28 ++
gcc/testsuite/g++.dg/ext/is_bounded_array.C | 38 +++
gcc/testsuite/g++.dg/ext/is_const.C | 19 ++
gcc/testsuite/g++.dg/ext/is_function.C | 58 ++++
.../g++.dg/ext/is_member_function_pointer.C | 31 ++
.../g++.dg/ext/is_member_object_pointer.C | 30 ++
gcc/testsuite/g++.dg/ext/is_member_pointer.C | 30 ++
gcc/testsuite/g++.dg/ext/is_pointer.C | 51 ++++
gcc/testsuite/g++.dg/ext/is_reference.C | 34 +++
gcc/testsuite/g++.dg/ext/is_scalar.C | 31 ++
gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++
gcc/testsuite/g++.dg/ext/is_signed.C | 47 +++
gcc/testsuite/g++.dg/ext/is_unbounded_array.C | 37 +++
gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 +++
gcc/testsuite/g++.dg/ext/is_volatile.C | 19 ++
gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 ++++
gcc/testsuite/g++.dg/tm/pr46567.C | 48 +--
gcc/testsuite/g++.dg/torture/20070621-1.C | 4 +-
gcc/testsuite/g++.dg/torture/pr57107.C | 8 +-
libcpp/include/cpplib.h | 2 +-
libstdc++-v3/include/bits/charconv.h | 2 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 18 +-
libstdc++-v3/include/bits/deque.tcc | 6 +-
libstdc++-v3/include/bits/locale_facets.tcc | 6 +-
libstdc++-v3/include/bits/stl_algobase.h | 14 +-
libstdc++-v3/include/bits/uniform_int_dist.h | 4 +-
libstdc++-v3/include/bits/valarray_array.h | 2 +-
libstdc++-v3/include/c_global/cmath | 48 +--
libstdc++-v3/include/c_std/cmath | 24 +-
libstdc++-v3/include/ext/numeric_traits.h | 18 +-
libstdc++-v3/include/std/type_traits | 284 ++++++++++++++++--
libstdc++-v3/include/tr1/cmath | 24 +-
42 files changed, 1343 insertions(+), 249 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_bounded_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_const.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_function.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_reference.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_unbounded_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_volatile.C
create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 01/40] c++: Sort built-in identifiers alphabetically
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 02/40] c++: Implement __is_const built-in trait Ken Matsui
` (39 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch sorts built-in identifiers alphabetically for better code
readability.
gcc/cp/ChangeLog:
* constraint.cc (diagnose_trait_expr): Sort built-in identifiers
alphabetically.
* cp-trait.def: Likewise.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
(finish_trait_type): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Sort built-in identifiers
alphabetically.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 68 +++++++++----------
gcc/cp/cp-trait.def | 10 +--
gcc/cp/semantics.cc | 86 ++++++++++++------------
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 70 +++++++++----------
4 files changed, 117 insertions(+), 117 deletions(-)
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9e4e7043cd..722fc334e6f 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3702,18 +3702,36 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_HAS_TRIVIAL_DESTRUCTOR:
inform (loc, " %qT is not trivially destructible", t1);
break;
+ case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
+ inform (loc, " %qT does not have unique object representations", t1);
+ break;
case CPTK_HAS_VIRTUAL_DESTRUCTOR:
inform (loc, " %qT does not have a virtual destructor", t1);
break;
case CPTK_IS_ABSTRACT:
inform (loc, " %qT is not an abstract class", t1);
break;
+ case CPTK_IS_AGGREGATE:
+ inform (loc, " %qT is not an aggregate", t1);
+ break;
+ case CPTK_IS_ASSIGNABLE:
+ inform (loc, " %qT is not assignable from %qT", t1, t2);
+ break;
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
+ case CPTK_IS_CONSTRUCTIBLE:
+ if (!t2)
+ inform (loc, " %qT is not default constructible", t1);
+ else
+ inform (loc, " %qT is not constructible from %qE", t1, t2);
+ break;
+ case CPTK_IS_CONVERTIBLE:
+ inform (loc, " %qT is not convertible from %qE", t2, t1);
+ break;
case CPTK_IS_EMPTY:
inform (loc, " %qT is not an empty class", t1);
break;
@@ -3729,6 +3747,18 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_NOTHROW_ASSIGNABLE:
+ inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
+ break;
+ case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
+ if (!t2)
+ inform (loc, " %qT is not nothrow default constructible", t1);
+ else
+ inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
+ break;
+ case CPTK_IS_NOTHROW_CONVERTIBLE:
+ inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
+ break;
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
inform (loc, " %qT is not pointer-interconvertible base of %qT",
t1, t2);
@@ -3748,50 +3778,20 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_TRIVIAL:
inform (loc, " %qT is not a trivial type", t1);
break;
- case CPTK_IS_UNION:
- inform (loc, " %qT is not a union", t1);
- break;
- case CPTK_IS_AGGREGATE:
- inform (loc, " %qT is not an aggregate", t1);
- break;
- case CPTK_IS_TRIVIALLY_COPYABLE:
- inform (loc, " %qT is not trivially copyable", t1);
- break;
- case CPTK_IS_ASSIGNABLE:
- inform (loc, " %qT is not assignable from %qT", t1, t2);
- break;
case CPTK_IS_TRIVIALLY_ASSIGNABLE:
inform (loc, " %qT is not trivially assignable from %qT", t1, t2);
break;
- case CPTK_IS_NOTHROW_ASSIGNABLE:
- inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
- break;
- case CPTK_IS_CONSTRUCTIBLE:
- if (!t2)
- inform (loc, " %qT is not default constructible", t1);
- else
- inform (loc, " %qT is not constructible from %qE", t1, t2);
- break;
case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
if (!t2)
inform (loc, " %qT is not trivially default constructible", t1);
else
inform (loc, " %qT is not trivially constructible from %qE", t1, t2);
break;
- case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- if (!t2)
- inform (loc, " %qT is not nothrow default constructible", t1);
- else
- inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
- break;
- case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
- inform (loc, " %qT does not have unique object representations", t1);
- break;
- case CPTK_IS_CONVERTIBLE:
- inform (loc, " %qT is not convertible from %qE", t2, t1);
+ case CPTK_IS_TRIVIALLY_COPYABLE:
+ inform (loc, " %qT is not trivially copyable", t1);
break;
- case CPTK_IS_NOTHROW_CONVERTIBLE:
- inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
+ case CPTK_IS_UNION:
+ inform (loc, " %qT is not a union", t1);
break;
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
inform (loc, " %qT is not a reference that binds to a temporary "
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 8b7fece0cc8..ce3733df641 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -84,14 +84,14 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
-/* FIXME Added space to avoid direct usage in GCC 13. */
-DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible ", 2)
-
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
-DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
-DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
+DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+
+/* FIXME Added space to avoid direct usage in GCC 13. */
+DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible ", 2)
/* These traits yield a type pack, not a type, and are represented by
cp_parser_trait as a special BASES tree instead of a TRAIT_TYPE tree. */
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 0f7f4e87ae4..7d0240edcc0 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12075,15 +12075,6 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& classtype_has_nothrow_assign_or_copy_p (type1,
true))));
- case CPTK_HAS_TRIVIAL_ASSIGN:
- /* ??? The standard seems to be missing the "or array of such a class
- type" wording for this trait. */
- type1 = strip_array_types (type1);
- return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
- && (trivial_type_p (type1)
- || (CLASS_TYPE_P (type1)
- && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
-
case CPTK_HAS_NOTHROW_CONSTRUCTOR:
type1 = strip_array_types (type1);
return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
@@ -12092,17 +12083,26 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& maybe_instantiate_noexcept (t)
&& TYPE_NOTHROW_P (TREE_TYPE (t))));
- case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
- type1 = strip_array_types (type1);
- return (trivial_type_p (type1)
- || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
-
case CPTK_HAS_NOTHROW_COPY:
type1 = strip_array_types (type1);
return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
|| (CLASS_TYPE_P (type1)
&& classtype_has_nothrow_assign_or_copy_p (type1, false)));
+ case CPTK_HAS_TRIVIAL_ASSIGN:
+ /* ??? The standard seems to be missing the "or array of such a class
+ type" wording for this trait. */
+ type1 = strip_array_types (type1);
+ return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
+ && (trivial_type_p (type1)
+ || (CLASS_TYPE_P (type1)
+ && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
+
+ case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
+ type1 = strip_array_types (type1);
+ return (trivial_type_p (type1)
+ || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
+
case CPTK_HAS_TRIVIAL_COPY:
/* ??? The standard seems to be missing the "or array of such a class
type" wording for this trait. */
@@ -12116,18 +12116,21 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
|| (CLASS_TYPE_P (type1)
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
- case CPTK_HAS_VIRTUAL_DESTRUCTOR:
- return type_has_virtual_destructor (type1);
-
case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
return type_has_unique_obj_representations (type1);
+ case CPTK_HAS_VIRTUAL_DESTRUCTOR:
+ return type_has_virtual_destructor (type1);
+
case CPTK_IS_ABSTRACT:
return ABSTRACT_CLASS_TYPE_P (type1);
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ASSIGNABLE:
+ return is_xible (MODIFY_EXPR, type1, type2);
+
case CPTK_IS_BASE_OF:
return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
@@ -12136,6 +12139,12 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
+ case CPTK_IS_CONSTRUCTIBLE:
+ return is_xible (INIT_EXPR, type1, type2);
+
+ case CPTK_IS_CONVERTIBLE:
+ return is_convertible (type1, type2);
+
case CPTK_IS_EMPTY:
return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
@@ -12151,6 +12160,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_NOTHROW_ASSIGNABLE:
+ return is_nothrow_xible (MODIFY_EXPR, type1, type2);
+
+ case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
+ return is_nothrow_xible (INIT_EXPR, type1, type2);
+
+ case CPTK_IS_NOTHROW_CONVERTIBLE:
+ return is_nothrow_convertible (type1, type2);
+
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
return pointer_interconvertible_base_of_p (type1, type2);
@@ -12181,24 +12199,6 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
- case CPTK_IS_ASSIGNABLE:
- return is_xible (MODIFY_EXPR, type1, type2);
-
- case CPTK_IS_CONSTRUCTIBLE:
- return is_xible (INIT_EXPR, type1, type2);
-
- case CPTK_IS_NOTHROW_ASSIGNABLE:
- return is_nothrow_xible (MODIFY_EXPR, type1, type2);
-
- case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- return is_nothrow_xible (INIT_EXPR, type1, type2);
-
- case CPTK_IS_CONVERTIBLE:
- return is_convertible (type1, type2);
-
- case CPTK_IS_NOTHROW_CONVERTIBLE:
- return is_nothrow_convertible (type1, type2);
-
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
@@ -12357,8 +12357,8 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_ENUM:
- case CPTK_IS_UNION:
case CPTK_IS_SAME:
+ case CPTK_IS_UNION:
break;
case CPTK_IS_LAYOUT_COMPATIBLE:
@@ -12421,25 +12421,25 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
switch (kind)
{
- case CPTK_UNDERLYING_TYPE:
- return finish_underlying_type (type1);
-
case CPTK_REMOVE_CV:
return cv_unqualified (type1);
- case CPTK_REMOVE_REFERENCE:
+ case CPTK_REMOVE_CVREF:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
- return type1;
+ return cv_unqualified (type1);
- case CPTK_REMOVE_CVREF:
+ case CPTK_REMOVE_REFERENCE:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
- return cv_unqualified (type1);
+ return type1;
case CPTK_TYPE_PACK_ELEMENT:
return finish_type_pack_element (type1, type2, complain);
+ case CPTK_UNDERLYING_TYPE:
+ return finish_underlying_type (type1);
+
#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
case CPTK_##CODE:
#include "cp-trait.def"
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index f343e153e56..2223f08a628 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -8,9 +8,21 @@
#if !__has_builtin (__builtin_bit_cast)
# error "__has_builtin (__builtin_bit_cast) failed"
#endif
+#if !__has_builtin (__builtin_is_constant_evaluated)
+# error "__has_builtin (__builtin_is_constant_evaluated) failed"
+#endif
+#if !__has_builtin (__builtin_is_corresponding_member)
+# error "__has_builtin (__builtin_is_corresponding_member) failed"
+#endif
+#if !__has_builtin (__builtin_is_pointer_interconvertible_with_class)
+# error "__has_builtin (__builtin_is_pointer_interconvertible_with_class) failed"
+#endif
#if !__has_builtin (__builtin_launder)
# error "__has_builtin (__builtin_launder) failed"
#endif
+#if !__has_builtin (__builtin_source_location)
+# error "__has_builtin (__builtin_source_location) failed"
+#endif
#if !__has_builtin (__has_nothrow_assign)
# error "__has_builtin (__has_nothrow_assign) failed"
#endif
@@ -44,12 +56,21 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_assignable)
+# error "__has_builtin (__is_assignable) failed"
+#endif
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
+#if !__has_builtin (__is_constructible)
+# error "__has_builtin (__is_constructible) failed"
+#endif
+#if !__has_builtin (__is_convertible)
+# error "__has_builtin (__is_convertible) failed"
+#endif
#if !__has_builtin (__is_empty)
# error "__has_builtin (__is_empty) failed"
#endif
@@ -65,6 +86,15 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_nothrow_assignable)
+# error "__has_builtin (__is_nothrow_assignable) failed"
+#endif
+#if !__has_builtin (__is_nothrow_constructible)
+# error "__has_builtin (__is_nothrow_constructible) failed"
+#endif
+#if !__has_builtin (__is_nothrow_convertible)
+# error "__has_builtin (__is_nothrow_convertible) failed"
+#endif
#if !__has_builtin (__is_pointer_interconvertible_base_of)
# error "__has_builtin (__is_pointer_interconvertible_base_of) failed"
#endif
@@ -98,51 +128,21 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
-#if !__has_builtin (__underlying_type)
-# error "__has_builtin (__underlying_type) failed"
-#endif
-#if !__has_builtin (__is_assignable)
-# error "__has_builtin (__is_assignable) failed"
-#endif
-#if !__has_builtin (__is_constructible)
-# error "__has_builtin (__is_constructible) failed"
-#endif
-#if !__has_builtin (__is_nothrow_assignable)
-# error "__has_builtin (__is_nothrow_assignable) failed"
-#endif
-#if !__has_builtin (__is_nothrow_constructible)
-# error "__has_builtin (__is_nothrow_constructible) failed"
-#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) failed"
#endif
#if !__has_builtin (__reference_converts_from_temporary)
# error "__has_builtin (__reference_converts_from_temporary) failed"
#endif
-#if !__has_builtin (__builtin_is_constant_evaluated)
-# error "__has_builtin (__builtin_is_constant_evaluated) failed"
-#endif
-#if !__has_builtin (__builtin_source_location)
-# error "__has_builtin (__builtin_source_location) failed"
-#endif
-#if !__has_builtin (__builtin_is_corresponding_member)
-# error "__has_builtin (__builtin_is_corresponding_member) failed"
-#endif
-#if !__has_builtin (__builtin_is_pointer_interconvertible_with_class)
-# error "__has_builtin (__builtin_is_pointer_interconvertible_with_class) failed"
-#endif
-#if !__has_builtin (__is_convertible)
-# error "__has_builtin (__is_convertible) failed"
-#endif
-#if !__has_builtin (__is_nothrow_convertible)
-# error "__has_builtin (__is_nothrow_convertible) failed"
-#endif
#if !__has_builtin (__remove_cv)
# error "__has_builtin (__remove_cv) failed"
#endif
+#if !__has_builtin (__remove_cvref)
+# error "__has_builtin (__remove_cvref) failed"
+#endif
#if !__has_builtin (__remove_reference)
# error "__has_builtin (__remove_reference) failed"
#endif
-#if !__has_builtin (__remove_cvref)
-# error "__has_builtin (__remove_cvref) failed"
+#if !__has_builtin (__underlying_type)
+# error "__has_builtin (__underlying_type) failed"
#endif
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 02/40] c++: Implement __is_const built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
2023-09-14 6:42 ` [PATCH v11 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 03/40] libstdc++: Optimize is_const trait performance Ken Matsui
` (38 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_const.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_const.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_CONST.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_const.
* g++.dg/ext/is_const.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_const.C | 19 +++++++++++++++++++
5 files changed, 30 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_const.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 722fc334e6f..567dd35fe0a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3723,6 +3723,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
+ case CPTK_IS_CONST:
+ inform (loc, " %qT is not a const type", t1);
+ break;
case CPTK_IS_CONSTRUCTIBLE:
if (!t2)
inform (loc, " %qT is not default constructible", t1);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index ce3733df641..a4ebfd9f319 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
+DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7d0240edcc0..baeda9f03e9 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12139,6 +12139,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
+ case CPTK_IS_CONST:
+ return CP_TYPE_CONST_P (type1);
+
case CPTK_IS_CONSTRUCTIBLE:
return is_xible (INIT_EXPR, type1, type2);
@@ -12356,6 +12359,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;
case CPTK_IS_CLASS:
+ case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 2223f08a628..e6e481b13c5 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -65,6 +65,9 @@
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
+#if !__has_builtin (__is_const)
+# error "__has_builtin (__is_const) failed"
+#endif
#if !__has_builtin (__is_constructible)
# error "__has_builtin (__is_constructible) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_const.C b/gcc/testsuite/g++.dg/ext/is_const.C
new file mode 100644
index 00000000000..8f2d7c2fce9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_const.C
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+SA(__is_const(const int));
+SA(__is_const(const volatile int));
+SA(__is_const(cClassType));
+SA(__is_const(cvClassType));
+
+// Negative tests.
+SA(!__is_const(int));
+SA(!__is_const(volatile int));
+SA(!__is_const(ClassType));
+SA(!__is_const(vClassType));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 03/40] libstdc++: Optimize is_const trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
2023-09-14 6:42 ` [PATCH v11 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
2023-09-14 6:42 ` [PATCH v11 02/40] c++: Implement __is_const built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 04/40] c++: Implement __is_volatile built-in trait Ken Matsui
` (37 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_const trait by dispatching to
the new __is_const built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_const): Use __is_const built-in trait.
(is_const_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 677cd934b94..686e38e47c3 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -784,6 +784,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Type properties.
/// is_const
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
+ template<typename _Tp>
+ struct is_const
+ : public __bool_constant<__is_const(_Tp)>
+ { };
+#else
template<typename>
struct is_const
: public false_type { };
@@ -791,6 +797,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_const<_Tp const>
: public true_type { };
+#endif
/// is_volatile
template<typename>
@@ -3218,10 +3225,17 @@ template <typename _Tp>
inline constexpr bool is_compound_v = is_compound<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_member_pointer_v = is_member_pointer<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
+template <typename _Tp>
+ inline constexpr bool is_const_v = __is_const(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_const_v = false;
template <typename _Tp>
inline constexpr bool is_const_v<const _Tp> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_volatile_v = false;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 04/40] c++: Implement __is_volatile built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (2 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 03/40] libstdc++: Optimize is_const trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 05/40] libstdc++: Optimize is_volatile trait performance Ken Matsui
` (36 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_volatile.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_volatile.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_VOLATILE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_volatile.
* g++.dg/ext/is_volatile.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_volatile.C | 19 +++++++++++++++++++
5 files changed, 30 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_volatile.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 567dd35fe0a..f031e022541 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3796,6 +3796,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
+ case CPTK_IS_VOLATILE:
+ inform (loc, " %qT is not a volatile type", t1);
+ break;
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
inform (loc, " %qT is not a reference that binds to a temporary "
"object of type %qT (direct-initialization)", t1, t2);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index a4ebfd9f319..60462cd9874 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -83,6 +83,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index baeda9f03e9..18390f530ee 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12202,6 +12202,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
+ case CPTK_IS_VOLATILE:
+ return CP_TYPE_VOLATILE_P (type1);
+
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
@@ -12363,6 +12366,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
case CPTK_IS_UNION:
+ case CPTK_IS_VOLATILE:
break;
case CPTK_IS_LAYOUT_COMPATIBLE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index e6e481b13c5..fb03dd20e84 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -131,6 +131,9 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
+#if !__has_builtin (__is_volatile)
+# error "__has_builtin (__is_volatile) failed"
+#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_volatile.C b/gcc/testsuite/g++.dg/ext/is_volatile.C
new file mode 100644
index 00000000000..004e397e5e7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_volatile.C
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+SA(__is_volatile(volatile int));
+SA(__is_volatile(const volatile int));
+SA(__is_volatile(vClassType));
+SA(__is_volatile(cvClassType));
+
+// Negative tests.
+SA(!__is_volatile(int));
+SA(!__is_volatile(const int));
+SA(!__is_volatile(ClassType));
+SA(!__is_volatile(cClassType));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 05/40] libstdc++: Optimize is_volatile trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (3 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 04/40] c++: Implement __is_volatile built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 06/40] c++: Implement __is_array built-in trait Ken Matsui
` (35 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_volatile trait by dispatching
to the new __is_volatile built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_volatile): Use __is_volatile built-in
trait.
(is_volatile_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 686e38e47c3..c01f65df22b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -800,6 +800,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_volatile
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_volatile)
+ template<typename _Tp>
+ struct is_volatile
+ : public __bool_constant<__is_volatile(_Tp)>
+ { };
+#else
template<typename>
struct is_volatile
: public false_type { };
@@ -807,6 +813,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_volatile<_Tp volatile>
: public true_type { };
+#endif
/// is_trivial
template<typename _Tp>
@@ -3236,10 +3243,15 @@ template <typename _Tp>
inline constexpr bool is_const_v<const _Tp> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_volatile)
+template <typename _Tp>
+ inline constexpr bool is_volatile_v = __is_volatile(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_volatile_v = false;
template <typename _Tp>
inline constexpr bool is_volatile_v<volatile _Tp> = true;
+#endif
template <typename _Tp>
inline constexpr bool is_trivial_v = __is_trivial(_Tp);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 06/40] c++: Implement __is_array built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (4 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 05/40] libstdc++: Optimize is_volatile trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 07/40] libstdc++: Optimize is_array trait performance Ken Matsui
` (34 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_array.
* g++.dg/ext/is_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_array.C | 28 ++++++++++++++++++++++++
5 files changed, 39 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index f031e022541..5e30a4a907a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_AGGREGATE:
inform (loc, " %qT is not an aggregate", t1);
break;
+ case CPTK_IS_ARRAY:
+ inform (loc, " %qT is not an array", t1);
+ break;
case CPTK_IS_ASSIGNABLE:
inform (loc, " %qT is not assignable from %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 60462cd9874..c9106242bc8 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 18390f530ee..562b0bb8438 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12128,6 +12128,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ARRAY:
+ return type_code1 == ARRAY_TYPE;
+
case CPTK_IS_ASSIGNABLE:
return is_xible (MODIFY_EXPR, type1, type2);
@@ -12361,6 +12364,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index fb03dd20e84..645cabe088e 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_array)
+# error "__has_builtin (__is_array) failed"
+#endif
#if !__has_builtin (__is_assignable)
# error "__has_builtin (__is_assignable) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_array.C b/gcc/testsuite/g++.dg/ext/is_array.C
new file mode 100644
index 00000000000..facfed5c7cb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_array.C
@@ -0,0 +1,28 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_array, int[2], true);
+SA_TEST_CATEGORY(__is_array, int[], true);
+SA_TEST_CATEGORY(__is_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_array, int[][3], true);
+SA_TEST_CATEGORY(__is_array, float*[2], true);
+SA_TEST_CATEGORY(__is_array, float*[], true);
+SA_TEST_CATEGORY(__is_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[][3], true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_array, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 07/40] libstdc++: Optimize is_array trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (5 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 06/40] c++: Implement __is_array built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 08/40] c++: Implement __is_unbounded_array built-in trait Ken Matsui
` (33 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_array trait by dispatching to
the new __is_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_array): Use __is_array built-in trait.
(is_array_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index c01f65df22b..4e8165e5af5 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -523,6 +523,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_array
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_array)
+ template<typename _Tp>
+ struct is_array
+ : public __bool_constant<__is_array(_Tp)>
+ { };
+#else
template<typename>
struct is_array
: public false_type { };
@@ -534,6 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_array<_Tp[]>
: public true_type { };
+#endif
template<typename>
struct __is_pointer_helper
@@ -3183,12 +3190,17 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_floating_point_v = is_floating_point<_Tp>::value;
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_array)
+template <typename _Tp>
+ inline constexpr bool is_array_v = __is_array(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_array_v = false;
template <typename _Tp>
inline constexpr bool is_array_v<_Tp[]> = true;
template <typename _Tp, size_t _Num>
inline constexpr bool is_array_v<_Tp[_Num]> = true;
+#endif
template <typename _Tp>
inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 08/40] c++: Implement __is_unbounded_array built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (6 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 07/40] libstdc++: Optimize is_array trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 09/40] libstdc++: Optimize is_unbounded_array trait performance Ken Matsui
` (32 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_unbounded_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_unbounded_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNBOUNDED_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_unbounded_array.
* g++.dg/ext/is_unbounded_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_unbounded_array.C | 37 +++++++++++++++++++
5 files changed, 48 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_unbounded_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 5e30a4a907a..751ac61b25a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3796,6 +3796,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_TRIVIALLY_COPYABLE:
inform (loc, " %qT is not trivially copyable", t1);
break;
+ case CPTK_IS_UNBOUNDED_ARRAY:
+ inform (loc, " %qT is not an unbounded array", t1);
+ break;
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index c9106242bc8..1e67a3d2089 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -83,6 +83,7 @@ DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
+DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 562b0bb8438..4fdec0c30c1 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12202,6 +12202,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_TRIVIALLY_COPYABLE:
return trivially_copyable_p (type1);
+ case CPTK_IS_UNBOUNDED_ARRAY:
+ return array_of_unknown_bound_p (type1);
+
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
@@ -12369,6 +12372,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
+ case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
case CPTK_IS_VOLATILE:
break;
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 645cabe088e..90997210c12 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -131,6 +131,9 @@
#if !__has_builtin (__is_trivially_copyable)
# error "__has_builtin (__is_trivially_copyable) failed"
#endif
+#if !__has_builtin (__is_unbounded_array)
+# error "__has_builtin (__is_unbounded_array) failed"
+#endif
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_unbounded_array.C b/gcc/testsuite/g++.dg/ext/is_unbounded_array.C
new file mode 100644
index 00000000000..1307d24f5a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_unbounded_array.C
@@ -0,0 +1,37 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_unbounded_array, int[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, int[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, int(*)[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(*)[], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(&)[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(&)[], false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType, false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass, false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteUnion, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 09/40] libstdc++: Optimize is_unbounded_array trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (7 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 08/40] c++: Implement __is_unbounded_array built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 10/40] c++: Implement __is_bounded_array built-in trait Ken Matsui
` (31 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_unbounded_array trait by
dispatching to the new __is_unbounded_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_unbounded_array_v): Use
__is_unbounded_array built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 4e8165e5af5..cb3d9e238fa 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3541,11 +3541,16 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True for a type that is an array of unknown bound.
/// @ingroup variable_templates
/// @since C++20
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unbounded_array)
+ template<typename _Tp>
+ inline constexpr bool is_unbounded_array_v = __is_unbounded_array(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_unbounded_array_v = false;
template<typename _Tp>
inline constexpr bool is_unbounded_array_v<_Tp[]> = true;
+# endif
/// True for a type that is an array of known bound.
/// @since C++20
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 10/40] c++: Implement __is_bounded_array built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (8 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 09/40] libstdc++: Optimize is_unbounded_array trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 11/40] libstdc++: Optimize is_bounded_array trait performance Ken Matsui
` (30 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_bounded_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_bounded_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_BOUNDED_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_bounded_array.
* g++.dg/ext/is_bounded_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_bounded_array.C | 38 +++++++++++++++++++++
5 files changed, 49 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_bounded_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 751ac61b25a..d09252a56b6 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3723,6 +3723,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
+ case CPTK_IS_BOUNDED_ARRAY:
+ inform (loc, " %qT is not a bounded array", t1);
+ break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 1e67a3d2089..b6146c010f6 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
+DEFTRAIT_EXPR (IS_BOUNDED_ARRAY, "__is_bounded_array", 1)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 4fdec0c30c1..bf6438fcfc5 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12139,6 +12139,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
|| DERIVED_FROM_P (type1, type2)));
+ case CPTK_IS_BOUNDED_ARRAY:
+ return type_code1 == ARRAY_TYPE && TYPE_DOMAIN (type1);
+
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
@@ -12368,6 +12371,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;
case CPTK_IS_ARRAY:
+ case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 90997210c12..4142da518b1 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -65,6 +65,9 @@
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
+#if !__has_builtin (__is_bounded_array)
+# error "__has_builtin (__is_bounded_array) failed"
+#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_bounded_array.C b/gcc/testsuite/g++.dg/ext/is_bounded_array.C
new file mode 100644
index 00000000000..346790eba12
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_bounded_array.C
@@ -0,0 +1,38 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CONST(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_bounded_array, int[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, int[], false);
+SA_TEST_CATEGORY(__is_bounded_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, int[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, float*[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, float*[], false);
+SA_TEST_CATEGORY(__is_bounded_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, float*[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[], false);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(*)[2], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(*)[], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(&)[2], false);
+SA_TEST_CONST(__is_bounded_array, int(&)[], false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_bounded_array, ClassType, false);
+SA_TEST_CONST(__is_bounded_array, void(), false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 11/40] libstdc++: Optimize is_bounded_array trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (9 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 10/40] c++: Implement __is_bounded_array built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 12/40] c++: Implement __is_scoped_enum built-in trait Ken Matsui
` (29 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_bounded_array trait by
dispatching to the new __is_bounded_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_bounded_array_v): Use __is_bounded_array
built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index cb3d9e238fa..d306073a797 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3532,11 +3532,16 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True for a type that is an array of known bound.
/// @ingroup variable_templates
/// @since C++20
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_bounded_array)
+ template<typename _Tp>
+ inline constexpr bool is_bounded_array_v = __is_bounded_array(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_bounded_array_v = false;
template<typename _Tp, size_t _Size>
inline constexpr bool is_bounded_array_v<_Tp[_Size]> = true;
+# endif
/// True for a type that is an array of unknown bound.
/// @ingroup variable_templates
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 12/40] c++: Implement __is_scoped_enum built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (10 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 11/40] libstdc++: Optimize is_bounded_array trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 13/40] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui
` (28 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_scoped_enum.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_scoped_enum.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCOPED_ENUM.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_scoped_enum.
* g++.dg/ext/is_scoped_enum.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +
gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++++++++++++++++++++
5 files changed, 78 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d09252a56b6..1c0b2e0f178 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3781,6 +3781,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SCOPED_ENUM:
+ inform (loc, " %qT is not a scoped enum", t1);
+ break;
case CPTK_IS_STD_LAYOUT:
inform (loc, " %qT is not an standard layout type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index b6146c010f6..047307c95ce 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -79,6 +79,7 @@ DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertib
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index bf6438fcfc5..7f89616aa03 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12190,6 +12190,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SCOPED_ENUM:
+ return SCOPED_ENUM_P (type1);
+
case CPTK_IS_STD_LAYOUT:
return std_layout_type_p (type1);
@@ -12376,6 +12379,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
+ case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
case CPTK_IS_VOLATILE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4142da518b1..ba97beea3c3 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -119,6 +119,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_scoped_enum)
+# error "__has_builtin (__is_scoped_enum) failed"
+#endif
#if !__has_builtin (__is_standard_layout)
# error "__has_builtin (__is_standard_layout) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_scoped_enum.C b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C
new file mode 100644
index 00000000000..a563b6ee67d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C
@@ -0,0 +1,67 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT);
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+enum class E { e1, e2 };
+SA_TEST_CATEGORY(__is_scoped_enum, E, true);
+enum class Ec : char { e1, e2 };
+SA_TEST_CATEGORY(__is_scoped_enum, Ec, true);
+
+// negative tests
+enum U { u1, u2 };
+SA_TEST_CATEGORY(__is_scoped_enum, U, false);
+enum F : int { f1, f2 };
+SA_TEST_CATEGORY(__is_scoped_enum, F, false);
+struct S;
+SA_TEST_CATEGORY(__is_scoped_enum, S, false);
+struct S { };
+SA_TEST_CATEGORY(__is_scoped_enum, S, false);
+
+SA_TEST_CATEGORY(__is_scoped_enum, int, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[2], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[][2], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[2][3], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int*, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int&, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int*&, false);
+SA_TEST_FN(__is_scoped_enum, int(), false);
+SA_TEST_FN(__is_scoped_enum, int(*)(), false);
+SA_TEST_FN(__is_scoped_enum, int(&)(), false);
+
+enum opaque_unscoped : short;
+enum class opaque_scoped;
+enum class opaque_scoped_with_base : long;
+
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_unscoped, false);
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped, true);
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped_with_base, true);
+
+enum unscoped {
+ u_is_scoped = __is_scoped_enum(unscoped),
+};
+SA( ! unscoped::u_is_scoped );
+
+enum unscoped_fixed : char {
+ uf_is_scoped = __is_scoped_enum(unscoped_fixed),
+};
+SA( ! unscoped_fixed::uf_is_scoped );
+
+enum class scoped {
+ is_scoped = __is_scoped_enum(scoped),
+};
+SA( (bool) scoped::is_scoped );
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 13/40] libstdc++: Optimize is_scoped_enum trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (11 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 12/40] c++: Implement __is_scoped_enum built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 14/40] c++: Implement __is_member_pointer built-in trait Ken Matsui
` (27 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_scoped_enum trait
by dispatching to the new __is_scoped_enum built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_scoped_enum): Use
__is_scoped_enum built-in trait.
(is_scoped_enum_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index d306073a797..7fd29d8d9f2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3633,6 +3633,12 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True if the type is a scoped enumeration type.
/// @since C++23
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum)
+ template<typename _Tp>
+ struct is_scoped_enum
+ : bool_constant<__is_scoped_enum(_Tp)>
+ { };
+# else
template<typename _Tp>
struct is_scoped_enum
: false_type
@@ -3644,11 +3650,17 @@ template<typename _Ret, typename _Fn, typename... _Args>
struct is_scoped_enum<_Tp>
: bool_constant<!requires(_Tp __t, void(*__f)(int)) { __f(__t); }>
{ };
+# endif
/// @ingroup variable_templates
/// @since C++23
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum)
+ template<typename _Tp>
+ inline constexpr bool is_scoped_enum_v = __is_scoped_enum(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<_Tp>::value;
+# endif
#endif
#ifdef __cpp_lib_reference_from_temporary // C++ >= 23 && ref_{converts,constructs}_from_temp
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 14/40] c++: Implement __is_member_pointer built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (12 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 13/40] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 15/40] libstdc++: Optimize is_member_pointer trait performance Ken Matsui
` (26 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_pointer.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_MEMBER_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_member_pointer.
* g++.dg/ext/is_member_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_member_pointer.C | 30 ++++++++++++++++++++
5 files changed, 41 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 1c0b2e0f178..f0d3f89464c 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3756,6 +3756,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_POINTER:
+ inform (loc, " %qT is not a member pointer", t1);
+ break;
case CPTK_IS_NOTHROW_ASSIGNABLE:
inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 047307c95ce..7fed3483221 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -72,6 +72,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7f89616aa03..84291d660ce 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12169,6 +12169,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_POINTER:
+ return TYPE_PTRMEM_P (type1);
+
case CPTK_IS_NOTHROW_ASSIGNABLE:
return is_nothrow_xible (MODIFY_EXPR, type1, type2);
@@ -12378,6 +12381,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index ba97beea3c3..994873f14e9 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -95,6 +95,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_pointer)
+# error "__has_builtin (__is_member_pointer) failed"
+#endif
#if !__has_builtin (__is_nothrow_assignable)
# error "__has_builtin (__is_nothrow_assignable) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_pointer.C
new file mode 100644
index 00000000000..7ee2e3ab90c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_pointer.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_NON_VOLATILE(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_member_pointer, int (ClassType::*), true);
+SA_TEST_CATEGORY(__is_member_pointer, ClassType (ClassType::*), true);
+
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(int), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(int) const, true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(float, ...), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, ClassType (ClassType::*)(ClassType), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer,
+ float (ClassType::*)(int, float, int[], int&), true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 15/40] libstdc++: Optimize is_member_pointer trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (13 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 14/40] c++: Implement __is_member_pointer built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
` (25 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_pointer trait
by dispatching to the new __is_member_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_pointer): Use __is_member_pointer
built-in trait.
(is_member_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 7fd29d8d9f2..d7f89cf7c06 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -716,6 +716,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_compound
: public __not_<is_fundamental<_Tp>>::type { };
+ /// is_member_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
+ template<typename _Tp>
+ struct is_member_pointer
+ : public __bool_constant<__is_member_pointer(_Tp)>
+ { };
+#else
/// @cond undocumented
template<typename _Tp>
struct __is_member_pointer_helper
@@ -726,11 +733,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public true_type { };
/// @endcond
- /// is_member_pointer
template<typename _Tp>
struct is_member_pointer
: public __is_member_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
template<typename, typename>
struct is_same;
@@ -3242,8 +3249,14 @@ template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_compound_v = is_compound<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_pointer_v = __is_member_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_pointer_v = is_member_pointer<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (14 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 15/40] libstdc++: Optimize is_member_pointer trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 17:53 ` Joseph Myers
2023-09-14 6:42 ` [PATCH v11 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly Ken Matsui
` (24 subsequent siblings)
40 siblings, 1 reply; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
Now that RID_MAX has reached 255, we need to update the bit sizes of every
use of the enum rid from 8 to 16 to support more keywords.
gcc/c-family/ChangeLog:
* c-indentation.h (struct token_indent_info): Make keyword 16 bits
and move this upward to minimize memory fragmentation.
gcc/c/ChangeLog:
* c-parser.cc (c_parse_init): Handle RID_MAX not to exceed the max
value of 16 bits.
* c-parser.h (struct c_token): Make keyword 16 bits and move this
upward to minimize memory fragmentation.
gcc/cp/ChangeLog:
* parser.h (struct cp_token): Make keyword 16 bits and move this
upward to minimize memory fragmentation.
(struct cp_lexer): Likewise, for saved_keyword.
libcpp/ChangeLog:
* include/cpplib.h (struct cpp_hashnode): Make rid_code 16 bits.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/c-family/c-indentation.h | 2 +-
gcc/c/c-parser.cc | 6 +++---
gcc/c/c-parser.h | 6 +++---
gcc/cp/parser.h | 8 ++++----
libcpp/include/cpplib.h | 2 +-
5 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/gcc/c-family/c-indentation.h b/gcc/c-family/c-indentation.h
index c0e07bf49f1..1ce8753813a 100644
--- a/gcc/c-family/c-indentation.h
+++ b/gcc/c-family/c-indentation.h
@@ -25,8 +25,8 @@ along with GCC; see the file COPYING3. If not see
struct token_indent_info
{
location_t location;
+ ENUM_BITFIELD (rid) keyword : 16;
ENUM_BITFIELD (cpp_ttype) type : 8;
- ENUM_BITFIELD (rid) keyword : 8;
};
/* Extract token information from TOKEN, which ought to either be a
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index b9a1b75ca43..2086f253923 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -115,9 +115,9 @@ c_parse_init (void)
tree id;
int mask = 0;
- /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
- the c_token structure. */
- gcc_assert (RID_MAX <= 255);
+ /* Make sure RID_MAX hasn't grown past the 16 bits used to hold the keyword
+ in the c_token structure. */
+ gcc_assert (RID_MAX <= 65535);
mask |= D_CXXONLY;
if (!flag_isoc99)
diff --git a/gcc/c/c-parser.h b/gcc/c/c-parser.h
index 545f0f4d9eb..eed6deaf0f8 100644
--- a/gcc/c/c-parser.h
+++ b/gcc/c/c-parser.h
@@ -51,14 +51,14 @@ enum c_id_kind {
/* A single C token after string literal concatenation and conversion
of preprocessing tokens to tokens. */
struct GTY (()) c_token {
+ /* If this token is a keyword, this value indicates which keyword.
+ Otherwise, this value is RID_MAX. */
+ ENUM_BITFIELD (rid) keyword : 16;
/* The kind of token. */
ENUM_BITFIELD (cpp_ttype) type : 8;
/* If this token is a CPP_NAME, this value indicates whether also
declared as some kind of type. Otherwise, it is C_ID_NONE. */
ENUM_BITFIELD (c_id_kind) id_kind : 8;
- /* If this token is a keyword, this value indicates which keyword.
- Otherwise, this value is RID_MAX. */
- ENUM_BITFIELD (rid) keyword : 8;
/* If this token is a CPP_PRAGMA, this indicates the pragma that
was seen. Otherwise it is PRAGMA_NONE. */
ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
index 6cbb9a8e031..3c3c482c6ce 100644
--- a/gcc/cp/parser.h
+++ b/gcc/cp/parser.h
@@ -40,11 +40,11 @@ struct GTY(()) tree_check {
/* A C++ token. */
struct GTY (()) cp_token {
- /* The kind of token. */
- enum cpp_ttype type : 8;
/* If this token is a keyword, this value indicates which keyword.
Otherwise, this value is RID_MAX. */
- enum rid keyword : 8;
+ enum rid keyword : 16;
+ /* The kind of token. */
+ enum cpp_ttype type : 8;
/* Token flags. */
unsigned char flags;
/* True if this token is from a context where it is implicitly extern "C" */
@@ -101,8 +101,8 @@ struct GTY (()) cp_lexer {
vec<cp_token_position> GTY ((skip)) saved_tokens;
/* Saved pieces of end token we replaced with the eof token. */
+ enum rid saved_keyword : 16;
enum cpp_ttype saved_type : 8;
- enum rid saved_keyword : 8;
/* The next lexer in a linked list of lexers. */
struct cp_lexer *next;
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index fcdaf082b09..b93899cd364 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -988,7 +988,7 @@ struct GTY(()) cpp_hashnode {
unsigned int directive_index : 7; /* If is_directive,
then index into directive table.
Otherwise, a NODE_OPERATOR. */
- unsigned int rid_code : 8; /* Rid code - for front ends. */
+ unsigned int rid_code : 16; /* Rid code - for front ends. */
unsigned int flags : 9; /* CPP flags. */
ENUM_BITFIELD(node_type) type : 2; /* CPP node type. */
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (15 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 18/40] c++: Implement __is_member_function_pointer built-in trait Ken Matsui
` (23 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui, Andrew Pinski
This patch fixes incorrect handling for the new 16-bit rid code. Unsigned
char was previously used for the 8-bit rid code, but unsigned short is now
required.
gcc/c-family/ChangeLog:
* c-common.h (C_SET_RID_CODE): Use unsigned short instead of
unsigned char.
Ref: Initial discussion: https://gcc.gnu.org/pipermail/gcc/2023-September/242460.html
Code provided by Andrew: https://gcc.gnu.org/pipermail/gcc/2023-September/242461.html
Co-authored-by: Andrew Pinski <pinskia@gmail.com>
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/c-family/c-common.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 1fdba7ef3ea..73bc23fa49f 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -382,7 +382,7 @@ enum c_tree_index
#define C_RID_CODE(id) \
((enum rid) (((struct c_common_identifier *) (id))->node.rid_code))
#define C_SET_RID_CODE(id, code) \
- (((struct c_common_identifier *) (id))->node.rid_code = (unsigned char) code)
+ (((struct c_common_identifier *) (id))->node.rid_code = (unsigned short) code)
/* Identifier part common to the C front ends. Inherits from
tree_identifier, despite appearances. */
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 18/40] c++: Implement __is_member_function_pointer built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (16 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 19/40] libstdc++: Optimize is_member_function_pointer trait performance Ken Matsui
` (22 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_function_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_function_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_FUNCTION_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of
__is_member_function_pointer.
* g++.dg/ext/is_member_function_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
.../g++.dg/ext/is_member_function_pointer.C | 31 +++++++++++++++++++
5 files changed, 42 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index f0d3f89464c..d0464dd4f6a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3756,6 +3756,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ inform (loc, " %qT is not a member function pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 7fed3483221..6ebe3984d17 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -72,6 +72,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 84291d660ce..294bcf3dcca 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12169,6 +12169,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ return TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12381,6 +12384,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 994873f14e9..0dfe957474b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -95,6 +95,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_function_pointer)
+# error "__has_builtin (__is_member_function_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
new file mode 100644
index 00000000000..555123e8f07
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT);
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int), true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int) const, true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (float, ...), true);
+SA_TEST_FN(__is_member_function_pointer, ClassType (ClassType::*) (ClassType), true);
+SA_TEST_FN(__is_member_function_pointer, float (ClassType::*) (int, float, int[], int&), true);
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_member_function_pointer, int (ClassType::*), false);
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType (ClassType::*), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 19/40] libstdc++: Optimize is_member_function_pointer trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (17 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 18/40] c++: Implement __is_member_function_pointer built-in trait Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:42 ` [PATCH v11 20/40] c++: Implement __is_member_object_pointer built-in trait Ken Matsui
` (21 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_function_pointer trait
by dispatching to the new __is_member_function_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_function_pointer): Use
__is_member_function_pointer built-in trait.
(is_member_function_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index d7f89cf7c06..e1b10240dc2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -588,6 +588,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __is_member_object_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
+ /// is_member_function_pointer
+ template<typename _Tp>
+ struct is_member_function_pointer
+ : public __bool_constant<__is_member_function_pointer(_Tp)>
+ { };
+#else
template<typename>
struct __is_member_function_pointer_helper
: public false_type { };
@@ -601,6 +608,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_member_function_pointer
: public __is_member_function_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
/// is_enum
template<typename _Tp>
@@ -3222,9 +3230,17 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_member_object_pointer_v =
is_member_object_pointer<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_function_pointer_v =
+ __is_member_function_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_function_pointer_v =
is_member_function_pointer<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_enum_v = __is_enum(_Tp);
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 20/40] c++: Implement __is_member_object_pointer built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (18 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 19/40] libstdc++: Optimize is_member_function_pointer trait performance Ken Matsui
@ 2023-09-14 6:42 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 21/40] libstdc++: Optimize is_member_object_pointer trait performance Ken Matsui
` (20 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:42 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_object_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_object_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_OBJECT_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of
__is_member_object_pointer.
* g++.dg/ext/is_member_object_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
.../g++.dg/ext/is_member_object_pointer.C | 30 +++++++++++++++++++
5 files changed, 41 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d0464dd4f6a..98b1f004a68 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3759,6 +3759,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
inform (loc, " %qT is not a member function pointer", t1);
break;
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
+ inform (loc, " %qT is not a member object pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 6ebe3984d17..47649150ab5 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -73,6 +73,7 @@ DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
+DEFTRAIT_EXPR (IS_MEMBER_OBJECT_POINTER, "__is_member_object_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 294bcf3dcca..534f24a1f9c 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12172,6 +12172,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
return TYPE_PTRMEMFUNC_P (type1);
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
+ return TYPE_PTRMEM_P (type1) && !TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12385,6 +12388,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 0dfe957474b..8d9cdc528cd 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -98,6 +98,9 @@
#if !__has_builtin (__is_member_function_pointer)
# error "__has_builtin (__is_member_function_pointer) failed"
#endif
+#if !__has_builtin (__is_member_object_pointer)
+# error "__has_builtin (__is_member_object_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
new file mode 100644
index 00000000000..835e48c8f8e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_NON_VOLATILE(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_CATEGORY(__is_member_object_pointer, int (ClassType::*), true);
+SA_TEST_CATEGORY(__is_member_object_pointer, ClassType (ClassType::*), true);
+
+// Negative tests.
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, int (ClassType::*) (int), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, int (ClassType::*) (float, ...), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, ClassType (ClassType::*) (ClassType), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, float (ClassType::*) (int, float, int[], int&), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_object_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 21/40] libstdc++: Optimize is_member_object_pointer trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (19 preceding siblings ...)
2023-09-14 6:42 ` [PATCH v11 20/40] c++: Implement __is_member_object_pointer built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 22/40] c++: Implement __is_reference built-in trait Ken Matsui
` (19 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_object_pointer trait
by dispatching to the new __is_member_object_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_object_pointer): Use
__is_member_object_pointer built-in trait.
(is_member_object_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index e1b10240dc2..792213ebfe8 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -574,6 +574,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_rvalue_reference<_Tp&&>
: public true_type { };
+ /// is_member_object_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_object_pointer)
+ template<typename _Tp>
+ struct is_member_object_pointer
+ : public __bool_constant<__is_member_object_pointer(_Tp)>
+ { };
+#else
template<typename>
struct __is_member_object_pointer_helper
: public false_type { };
@@ -582,11 +589,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __is_member_object_pointer_helper<_Tp _Cp::*>
: public __not_<is_function<_Tp>>::type { };
- /// is_member_object_pointer
+
template<typename _Tp>
struct is_member_object_pointer
: public __is_member_object_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
/// is_member_function_pointer
@@ -3227,9 +3235,16 @@ template <typename _Tp>
inline constexpr bool is_rvalue_reference_v = false;
template <typename _Tp>
inline constexpr bool is_rvalue_reference_v<_Tp&&> = true;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_object_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_object_pointer_v =
+ __is_member_object_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_object_pointer_v =
is_member_object_pointer<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 22/40] c++: Implement __is_reference built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (20 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 21/40] libstdc++: Optimize is_member_object_pointer trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 23/40] libstdc++: Optimize is_reference trait performance Ken Matsui
` (18 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_reference.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_reference.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_REFERENCE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_reference.
* g++.dg/ext/is_reference.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_reference.C | 34 ++++++++++++++++++++++++
5 files changed, 45 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_reference.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 98b1f004a68..5cdb59d174e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3787,6 +3787,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
+ case CPTK_IS_REFERENCE:
+ inform (loc, " %qT is not a reference", t1);
+ break;
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 47649150ab5..ac9fa026b4e 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -81,6 +81,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
+DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 534f24a1f9c..97ec53a7606 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12196,6 +12196,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
+ case CPTK_IS_REFERENCE:
+ return type_code1 == REFERENCE_TYPE;
+
case CPTK_IS_SAME:
return same_type_p (type1, type2);
@@ -12390,6 +12393,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
+ case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 8d9cdc528cd..e112d317657 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -122,6 +122,9 @@
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif
+#if !__has_builtin (__is_reference)
+# error "__has_builtin (__is_reference) failed"
+#endif
#if !__has_builtin (__is_same)
# error "__has_builtin (__is_same) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_reference.C b/gcc/testsuite/g++.dg/ext/is_reference.C
new file mode 100644
index 00000000000..b5ce4db7afd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_reference.C
@@ -0,0 +1,34 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_CATEGORY(__is_reference, int&, true);
+SA_TEST_CATEGORY(__is_reference, ClassType&, true);
+SA(__is_reference(int(&)(int)));
+SA_TEST_CATEGORY(__is_reference, int&&, true);
+SA_TEST_CATEGORY(__is_reference, ClassType&&, true);
+SA(__is_reference(int(&&)(int)));
+SA_TEST_CATEGORY(__is_reference, IncompleteClass&, true);
+
+// Negative tests
+SA_TEST_CATEGORY(__is_reference, void, false);
+SA_TEST_CATEGORY(__is_reference, int*, false);
+SA_TEST_CATEGORY(__is_reference, int[3], false);
+SA(!__is_reference(int(int)));
+SA(!__is_reference(int(*const)(int)));
+SA(!__is_reference(int(*volatile)(int)));
+SA(!__is_reference(int(*const volatile)(int)));
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_reference, ClassType, false);
+SA_TEST_CATEGORY(__is_reference, IncompleteClass, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 23/40] libstdc++: Optimize is_reference trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (21 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 22/40] c++: Implement __is_reference built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 24/40] c++: Implement __is_function built-in trait Ken Matsui
` (17 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_reference trait by dispatching
to the new __is_reference built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_reference): Use __is_reference built-in
trait.
(is_reference_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 792213ebfe8..36ad9814047 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -682,6 +682,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Composite type categories.
/// is_reference
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+ template<typename _Tp>
+ struct is_reference
+ : public __bool_constant<__is_reference(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_reference
: public false_type
@@ -696,6 +702,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_reference<_Tp&&>
: public true_type
{ };
+#endif
/// is_arithmetic
template<typename _Tp>
@@ -3264,12 +3271,19 @@ template <typename _Tp>
inline constexpr bool is_class_v = __is_class(_Tp);
template <typename _Tp>
inline constexpr bool is_function_v = is_function<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+template <typename _Tp>
+ inline constexpr bool is_reference_v = __is_reference(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_reference_v = false;
template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&> = true;
template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&&> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 24/40] c++: Implement __is_function built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (22 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 23/40] libstdc++: Optimize is_reference trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 25/40] libstdc++: Optimize is_function trait performance Ken Matsui
` (16 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_function.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_function.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_FUNCTION.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_function.
* g++.dg/ext/is_function.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_function.C | 58 ++++++++++++++++++++++++
5 files changed, 69 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_function.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 5cdb59d174e..99a7e7247ce 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3750,6 +3750,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_FINAL:
inform (loc, " %qT is not a final class", t1);
break;
+ case CPTK_IS_FUNCTION:
+ inform (loc, " %qT is not a function", t1);
+ break;
case CPTK_IS_LAYOUT_COMPATIBLE:
inform (loc, " %qT is not layout compatible with %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index ac9fa026b4e..3bb33a3d5c0 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -70,6 +70,7 @@ DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
+DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 97ec53a7606..7457475f646 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12163,6 +12163,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_FINAL:
return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
+ case CPTK_IS_FUNCTION:
+ return type_code1 == FUNCTION_TYPE;
+
case CPTK_IS_LAYOUT_COMPATIBLE:
return layout_compatible_type_p (type1, type2);
@@ -12390,6 +12393,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_FUNCTION:
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index e112d317657..4d3947572a4 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -89,6 +89,9 @@
#if !__has_builtin (__is_final)
# error "__has_builtin (__is_final) failed"
#endif
+#if !__has_builtin (__is_function)
+# error "__has_builtin (__is_function) failed"
+#endif
#if !__has_builtin (__is_layout_compatible)
# error "__has_builtin (__is_layout_compatible) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_function.C b/gcc/testsuite/g++.dg/ext/is_function.C
new file mode 100644
index 00000000000..2e1594b12ad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_function.C
@@ -0,0 +1,58 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+struct A
+{ void fn(); };
+
+template<typename>
+struct AHolder { };
+
+template<class T, class U>
+struct AHolder<U T::*>
+{ using type = U; };
+
+// Positive tests.
+SA(__is_function(int (int)));
+SA(__is_function(ClassType (ClassType)));
+SA(__is_function(float (int, float, int[], int&)));
+SA(__is_function(int (int, ...)));
+SA(__is_function(bool (ClassType) const));
+SA(__is_function(AHolder<decltype(&A::fn)>::type));
+
+void fn();
+SA(__is_function(decltype(fn)));
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_function, int, false);
+SA_TEST_CATEGORY(__is_function, int*, false);
+SA_TEST_CATEGORY(__is_function, int&, false);
+SA_TEST_CATEGORY(__is_function, void, false);
+SA_TEST_CATEGORY(__is_function, void*, false);
+SA_TEST_CATEGORY(__is_function, void**, false);
+SA_TEST_CATEGORY(__is_function, std::nullptr_t, false);
+
+SA_TEST_CATEGORY(__is_function, AbstractClass, false);
+SA(!__is_function(int(&)(int)));
+SA(!__is_function(int(*)(int)));
+
+SA_TEST_CATEGORY(__is_function, A, false);
+SA_TEST_CATEGORY(__is_function, decltype(&A::fn), false);
+
+struct FnCallOverload
+{ void operator()(); };
+SA_TEST_CATEGORY(__is_function, FnCallOverload, false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_function, ClassType, false);
+SA_TEST_CATEGORY(__is_function, IncompleteClass, false);
+SA_TEST_CATEGORY(__is_function, IncompleteUnion, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 25/40] libstdc++: Optimize is_function trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (23 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 24/40] c++: Implement __is_function built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 26/40] libstdc++: Optimize is_object " Ken Matsui
` (15 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_function trait by dispatching
to the new __is_function built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_function): Use __is_function built-in
trait.
(is_function_v): Likewise. Optimize its implementation.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 36ad9814047..bd57488824b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -637,6 +637,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_function
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
+ template<typename _Tp>
+ struct is_function
+ : public __bool_constant<__is_function(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_function
: public __bool_constant<!is_const<const _Tp>::value> { };
@@ -648,6 +654,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_function<_Tp&&>
: public false_type { };
+#endif
#ifdef __cpp_lib_is_null_pointer // C++ >= 11
/// is_null_pointer (LWG 2247).
@@ -3269,8 +3276,18 @@ template <typename _Tp>
inline constexpr bool is_union_v = __is_union(_Tp);
template <typename _Tp>
inline constexpr bool is_class_v = __is_class(_Tp);
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
template <typename _Tp>
- inline constexpr bool is_function_v = is_function<_Tp>::value;
+ inline constexpr bool is_function_v = __is_function(_Tp);
+#else
+template <typename _Tp>
+ inline constexpr bool is_function_v = !is_const_v<const _Tp>;
+template <typename _Tp>
+ inline constexpr bool is_function_v<_Tp&> = false;
+template <typename _Tp>
+ inline constexpr bool is_function_v<_Tp&&> = false;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 26/40] libstdc++: Optimize is_object trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (24 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 25/40] libstdc++: Optimize is_function trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 27/40] c++: Implement __remove_pointer built-in trait Ken Matsui
` (14 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_object trait by dispatching to
the new __is_function and __is_reference built-in traits.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_object): Use __is_function and
__is_reference built-in traits.
(is_object_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index bd57488824b..674d398c075 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -725,11 +725,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_object
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
+ && _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+ template<typename _Tp>
+ struct is_object
+ : public __bool_constant<!(__is_function(_Tp) || __is_reference(_Tp)
+ || is_void<_Tp>::value)>
+ { };
+#else
template<typename _Tp>
struct is_object
: public __not_<__or_<is_function<_Tp>, is_reference<_Tp>,
is_void<_Tp>>>::type
{ };
+#endif
template<typename>
struct is_member_pointer;
@@ -3305,8 +3314,17 @@ template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
+ && _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+template <typename _Tp>
+ inline constexpr bool is_object_v
+ = !(__is_function(_Tp) || __is_reference(_Tp) || is_void<_Tp>::value);
+#else
template <typename _Tp>
inline constexpr bool is_object_v = is_object<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 27/40] c++: Implement __remove_pointer built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (25 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 26/40] libstdc++: Optimize is_object " Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 28/40] libstdc++: Optimize remove_pointer trait performance Ken Matsui
` (13 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::remove_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __remove_pointer.
* semantics.cc (finish_trait_type): Handle CPTK_REMOVE_POINTER.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __remove_pointer.
* g++.dg/ext/remove_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 5 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 +++++++++++++++++++++++
4 files changed, 60 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 3bb33a3d5c0..07cd14b6e85 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -97,6 +97,7 @@ DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_tempo
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
+DEFTRAIT_TYPE (REMOVE_POINTER, "__remove_pointer", 1)
DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7457475f646..4de2dbbf603 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12473,6 +12473,11 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
type1 = TREE_TYPE (type1);
return cv_unqualified (type1);
+ case CPTK_REMOVE_POINTER:
+ if (TYPE_PTR_P (type1))
+ type1 = TREE_TYPE (type1);
+ return type1;
+
case CPTK_REMOVE_REFERENCE:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4d3947572a4..bcab0599d1a 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -173,6 +173,9 @@
#if !__has_builtin (__remove_cvref)
# error "__has_builtin (__remove_cvref) failed"
#endif
+#if !__has_builtin (__remove_pointer)
+# error "__has_builtin (__remove_pointer) failed"
+#endif
#if !__has_builtin (__remove_reference)
# error "__has_builtin (__remove_reference) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/remove_pointer.C b/gcc/testsuite/g++.dg/ext/remove_pointer.C
new file mode 100644
index 00000000000..7b13db93950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/remove_pointer.C
@@ -0,0 +1,51 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_pointer(int), int));
+SA(__is_same(__remove_pointer(int*), int));
+SA(__is_same(__remove_pointer(int**), int*));
+
+SA(__is_same(__remove_pointer(const int*), const int));
+SA(__is_same(__remove_pointer(const int**), const int*));
+SA(__is_same(__remove_pointer(int* const), int));
+SA(__is_same(__remove_pointer(int** const), int*));
+SA(__is_same(__remove_pointer(int* const* const), int* const));
+
+SA(__is_same(__remove_pointer(volatile int*), volatile int));
+SA(__is_same(__remove_pointer(volatile int**), volatile int*));
+SA(__is_same(__remove_pointer(int* volatile), int));
+SA(__is_same(__remove_pointer(int** volatile), int*));
+SA(__is_same(__remove_pointer(int* volatile* volatile), int* volatile));
+
+SA(__is_same(__remove_pointer(const volatile int*), const volatile int));
+SA(__is_same(__remove_pointer(const volatile int**), const volatile int*));
+SA(__is_same(__remove_pointer(const int* volatile), const int));
+SA(__is_same(__remove_pointer(volatile int* const), volatile int));
+SA(__is_same(__remove_pointer(int* const volatile), int));
+SA(__is_same(__remove_pointer(const int** volatile), const int*));
+SA(__is_same(__remove_pointer(volatile int** const), volatile int*));
+SA(__is_same(__remove_pointer(int** const volatile), int*));
+SA(__is_same(__remove_pointer(int* const* const volatile), int* const));
+SA(__is_same(__remove_pointer(int* volatile* const volatile), int* volatile));
+SA(__is_same(__remove_pointer(int* const volatile* const volatile), int* const volatile));
+
+SA(__is_same(__remove_pointer(int&), int&));
+SA(__is_same(__remove_pointer(const int&), const int&));
+SA(__is_same(__remove_pointer(volatile int&), volatile int&));
+SA(__is_same(__remove_pointer(const volatile int&), const volatile int&));
+
+SA(__is_same(__remove_pointer(int&&), int&&));
+SA(__is_same(__remove_pointer(const int&&), const int&&));
+SA(__is_same(__remove_pointer(volatile int&&), volatile int&&));
+SA(__is_same(__remove_pointer(const volatile int&&), const volatile int&&));
+
+SA(__is_same(__remove_pointer(int[3]), int[3]));
+SA(__is_same(__remove_pointer(const int[3]), const int[3]));
+SA(__is_same(__remove_pointer(volatile int[3]), volatile int[3]));
+SA(__is_same(__remove_pointer(const volatile int[3]), const volatile int[3]));
+
+SA(__is_same(__remove_pointer(int(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*volatile)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const volatile)(int)), int(int)));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 28/40] libstdc++: Optimize remove_pointer trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (26 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 27/40] c++: Implement __remove_pointer built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 29/40] c++, libstdc++: Implement __is_pointer built-in trait Ken Matsui
` (12 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the remove_pointer trait by
dispatching to the new remove_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (remove_pointer): Use __remove_pointer
built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 674d398c075..9c56d15c0b7 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -2105,6 +2105,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Pointer modifications.
+ /// remove_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__remove_pointer)
+ template<typename _Tp>
+ struct remove_pointer
+ { using type = __remove_pointer(_Tp); };
+#else
template<typename _Tp, typename>
struct __remove_pointer_helper
{ using type = _Tp; };
@@ -2113,11 +2119,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __remove_pointer_helper<_Tp, _Up*>
{ using type = _Up; };
- /// remove_pointer
template<typename _Tp>
struct remove_pointer
: public __remove_pointer_helper<_Tp, __remove_cv_t<_Tp>>
{ };
+#endif
template<typename _Tp, typename = void>
struct __add_pointer_helper
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 29/40] c++, libstdc++: Implement __is_pointer built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (27 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 28/40] libstdc++: Optimize remove_pointer trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 30/40] libstdc++: Optimize is_pointer trait performance Ken Matsui
` (11 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_pointer.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_pointer.
* g++.dg/ext/is_pointer.C: New test.
* g++.dg/tm/pr46567.C (__is_pointer): Rename to ...
(__is_ptr): ... this.
* g++.dg/torture/20070621-1.C: Likewise.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_pointer): Rename to ...
(__is_ptr): ... this.
* include/bits/deque.tcc: Use __is_ptr instead.
* include/bits/stl_algobase.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_pointer.C | 51 +++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 22 ++++-----
gcc/testsuite/g++.dg/torture/20070621-1.C | 4 +-
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 6 +--
libstdc++-v3/include/bits/deque.tcc | 6 +--
libstdc++-v3/include/bits/stl_algobase.h | 6 +--
11 files changed, 86 insertions(+), 24 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 99a7e7247ce..c9d627fa782 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3787,6 +3787,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POD:
inform (loc, " %qT is not a POD type", t1);
break;
+ case CPTK_IS_POINTER:
+ inform (loc, " %qT is not a pointer", t1);
+ break;
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 07cd14b6e85..bc2bb5e5abb 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -81,6 +81,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
+DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 4de2dbbf603..4b73863ec77 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12196,6 +12196,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POD:
return pod_type_p (type1);
+ case CPTK_IS_POINTER:
+ return TYPE_PTR_P (type1);
+
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
@@ -12397,6 +12400,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
+ case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index bcab0599d1a..efce04fd09d 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -122,6 +122,9 @@
#if !__has_builtin (__is_pod)
# error "__has_builtin (__is_pod) failed"
#endif
+#if !__has_builtin (__is_pointer)
+# error "__has_builtin (__is_pointer) failed"
+#endif
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_pointer.C b/gcc/testsuite/g++.dg/ext/is_pointer.C
new file mode 100644
index 00000000000..d6e39565950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_pointer.C
@@ -0,0 +1,51 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(!__is_pointer(int));
+SA(__is_pointer(int*));
+SA(__is_pointer(int**));
+
+SA(__is_pointer(const int*));
+SA(__is_pointer(const int**));
+SA(__is_pointer(int* const));
+SA(__is_pointer(int** const));
+SA(__is_pointer(int* const* const));
+
+SA(__is_pointer(volatile int*));
+SA(__is_pointer(volatile int**));
+SA(__is_pointer(int* volatile));
+SA(__is_pointer(int** volatile));
+SA(__is_pointer(int* volatile* volatile));
+
+SA(__is_pointer(const volatile int*));
+SA(__is_pointer(const volatile int**));
+SA(__is_pointer(const int* volatile));
+SA(__is_pointer(volatile int* const));
+SA(__is_pointer(int* const volatile));
+SA(__is_pointer(const int** volatile));
+SA(__is_pointer(volatile int** const));
+SA(__is_pointer(int** const volatile));
+SA(__is_pointer(int* const* const volatile));
+SA(__is_pointer(int* volatile* const volatile));
+SA(__is_pointer(int* const volatile* const volatile));
+
+SA(!__is_pointer(int&));
+SA(!__is_pointer(const int&));
+SA(!__is_pointer(volatile int&));
+SA(!__is_pointer(const volatile int&));
+
+SA(!__is_pointer(int&&));
+SA(!__is_pointer(const int&&));
+SA(!__is_pointer(volatile int&&));
+SA(!__is_pointer(const volatile int&&));
+
+SA(!__is_pointer(int[3]));
+SA(!__is_pointer(const int[3]));
+SA(!__is_pointer(volatile int[3]));
+SA(!__is_pointer(const volatile int[3]));
+
+SA(!__is_pointer(int(int)));
+SA(__is_pointer(int(*const)(int)));
+SA(__is_pointer(int(*volatile)(int)));
+SA(__is_pointer(int(*const volatile)(int)));
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 6d791484448..f08bbf6fd7b 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -192,13 +192,13 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef __true_type __type;
};
template<typename _Tp>
- struct __is_pointer
+ struct __is_ptr
{
enum { __value = 0 };
typedef __false_type __type;
};
template<typename _Tp>
- struct __is_pointer<_Tp*>
+ struct __is_ptr<_Tp*>
{
enum { __value = 1 };
typedef __true_type __type;
@@ -226,7 +226,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
{ };
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
+ : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
struct __is_char
@@ -1202,8 +1202,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
typedef typename iterator_traits<_II>::iterator_category _Category;
const bool __simple = (__is_pod(_ValueTypeI)
- && __is_pointer<_II>::__value
- && __is_pointer<_OI>::__value
+ && __is_ptr<_II>::__value
+ && __is_ptr<_OI>::__value
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
return std::__copy_move<_IsMove, __simple,
_Category>::__copy_m(__first, __last, __result);
@@ -1294,8 +1294,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_BI2>::value_type _ValueType2;
typedef typename iterator_traits<_BI1>::iterator_category _Category;
const bool __simple = (__is_pod(_ValueType1)
- && __is_pointer<_BI1>::__value
- && __is_pointer<_BI2>::__value
+ && __is_ptr<_BI1>::__value
+ && __is_ptr<_BI2>::__value
&& __are_same<_ValueType1, _ValueType2>::__value);
return std::__copy_move_backward<_IsMove, __simple,
_Category>::__copy_move_b(__first,
@@ -1426,8 +1426,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple = (__is_integer<_ValueType1>::__value
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value
&& __are_same<_ValueType1, _ValueType2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
@@ -1515,8 +1515,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
&& !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
&& !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value);
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
__first2, __last2);
}
diff --git a/gcc/testsuite/g++.dg/torture/20070621-1.C b/gcc/testsuite/g++.dg/torture/20070621-1.C
index d8a6a76b6b0..b05136163e8 100644
--- a/gcc/testsuite/g++.dg/torture/20070621-1.C
+++ b/gcc/testsuite/g++.dg/torture/20070621-1.C
@@ -18,7 +18,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_pointer {
+ template<typename _Tp> struct __is_ptr {
enum {
__value = 0 };
};
@@ -49,7 +49,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
template<typename _II1, typename _II2> inline bool __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) {
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
- const bool __simple = (__is_integer<_ValueType1>::__value && __is_pointer<_II1>::__value && __is_pointer<_II2>::__value && __are_same<_ValueType1, _ValueType2>::__value);
+ const bool __simple = (__is_integer<_ValueType1>::__value && __is_ptr<_II1>::__value && __is_ptr<_II2>::__value && __are_same<_ValueType1, _ValueType2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
template<typename _II1, typename _II2> inline bool equal(_II1 __first1, _II1 __last1, _II2 __first2) {
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index 4dbd32bd298..be0689096fb 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -17,7 +17,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_pointer {
+ template<typename _Tp> struct __is_ptr {
enum {
__value = 0 };
};
@@ -27,7 +27,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Tp> struct __is_arithmetic : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> > {
+ template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 4312f32a4e0..3711e4be526 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -364,14 +364,14 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// Pointer types
//
template<typename _Tp>
- struct __is_pointer
+ struct __is_ptr
{
enum { __value = 0 };
typedef __false_type __type;
};
template<typename _Tp>
- struct __is_pointer<_Tp*>
+ struct __is_ptr<_Tp*>
{
enum { __value = 1 };
typedef __true_type __type;
@@ -390,7 +390,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
+ : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
{ };
//
diff --git a/libstdc++-v3/include/bits/deque.tcc b/libstdc++-v3/include/bits/deque.tcc
index a212b8a6940..08d888ee8af 100644
--- a/libstdc++-v3/include/bits/deque.tcc
+++ b/libstdc++-v3/include/bits/deque.tcc
@@ -1273,7 +1273,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
const bool __simple =
(__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
- && __is_pointer<_Ptr>::__value
+ && __is_ptr<_Ptr>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
@@ -1329,8 +1329,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
const bool __simple =
(__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
- && __is_pointer<_Ptr1>::__value
- && __is_pointer<_Ptr2>::__value
+ && __is_ptr<_Ptr1>::__value
+ && __is_ptr<_Ptr2>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 2f5a4bd4fd4..d1438429487 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -1217,7 +1217,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
typedef typename iterator_traits<_II1>::value_type _ValueType1;
const bool __simple = ((__is_integer<_ValueType1>::__value
- || __is_pointer<_ValueType1>::__value)
+ || __is_ptr<_ValueType1>::__value)
&& __memcmpable<_II1, _II2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
@@ -1380,8 +1380,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 30/40] libstdc++: Optimize is_pointer trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (28 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 29/40] c++, libstdc++: Implement __is_pointer built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait Ken Matsui
` (10 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui, Jonathan Wakely
This patch optimizes the performance of the is_pointer trait by dispatching to
the new __is_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_ptr): Use __is_pointer
built-in trait.
* include/std/type_traits (is_pointer): Likewise. Optimize its
implementation.
(is_pointer_v): Likewise.
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/bits/cpp_type_traits.h | 8 ++++
libstdc++-v3/include/std/type_traits | 44 +++++++++++++++++----
2 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 3711e4be526..4da1e7c407c 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -363,6 +363,13 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
// Pointer types
//
+#if __has_builtin(__is_pointer)
+ template<typename _Tp>
+ struct __is_ptr : __truth_type<__is_pointer(_Tp)>
+ {
+ enum { __value = __is_pointer(_Tp) };
+ };
+#else
template<typename _Tp>
struct __is_ptr
{
@@ -376,6 +383,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
enum { __value = 1 };
typedef __true_type __type;
};
+#endif
//
// An arithmetic type is an integer type or a floating point type
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 9c56d15c0b7..3acd843f2f2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -542,19 +542,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public true_type { };
#endif
- template<typename>
- struct __is_pointer_helper
+ /// is_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
+ template<typename _Tp>
+ struct is_pointer
+ : public __bool_constant<__is_pointer(_Tp)>
+ { };
+#else
+ template<typename _Tp>
+ struct is_pointer
: public false_type { };
template<typename _Tp>
- struct __is_pointer_helper<_Tp*>
+ struct is_pointer<_Tp*>
: public true_type { };
- /// is_pointer
template<typename _Tp>
- struct is_pointer
- : public __is_pointer_helper<__remove_cv_t<_Tp>>::type
- { };
+ struct is_pointer<_Tp* const>
+ : public true_type { };
+
+ template<typename _Tp>
+ struct is_pointer<_Tp* volatile>
+ : public true_type { };
+
+ template<typename _Tp>
+ struct is_pointer<_Tp* const volatile>
+ : public true_type { };
+#endif
/// is_lvalue_reference
template<typename>
@@ -3254,8 +3268,22 @@ template <typename _Tp, size_t _Num>
inline constexpr bool is_array_v<_Tp[_Num]> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
+template <typename _Tp>
+ inline constexpr bool is_pointer_v = __is_pointer(_Tp);
+#else
template <typename _Tp>
- inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
+ inline constexpr bool is_pointer_v = false;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp*> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* const> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* volatile> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* const volatile> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_lvalue_reference_v = false;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (29 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 30/40] libstdc++: Optimize is_pointer trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 32/40] libstdc++: Optimize is_arithmetic trait performance Ken Matsui
` (9 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_arithmetic.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_arithmetic.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARITHMETIC.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic.
* g++.dg/ext/is_arithmetic.C: New test.
* g++.dg/tm/pr46567.C (__is_arithmetic): Rename to ...
(__is_arith): ... this.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_arithmetic): Rename to ...
(__is_arith): ... this.
* include/c_global/cmath: Use __is_arith instead.
* include/c_std/cmath: Likewise.
* include/tr1/cmath: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_arithmetic.C | 33 ++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 6 +--
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 4 +-
libstdc++-v3/include/c_global/cmath | 48 ++++++++++-----------
libstdc++-v3/include/c_std/cmath | 24 +++++------
libstdc++-v3/include/tr1/cmath | 24 +++++------
11 files changed, 99 insertions(+), 55 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9d627fa782..3a7f968eae8 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_AGGREGATE:
inform (loc, " %qT is not an aggregate", t1);
break;
+ case CPTK_IS_ARITHMETIC:
+ inform (loc, " %qT is not an arithmetic type", t1);
+ break;
case CPTK_IS_ARRAY:
inform (loc, " %qT is not an array", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index bc2bb5e5abb..06c203ce4de 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 4b73863ec77..351f453356f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12128,6 +12128,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ARITHMETIC:
+ return ARITHMETIC_TYPE_P (type1);
+
case CPTK_IS_ARRAY:
return type_code1 == ARRAY_TYPE;
@@ -12391,6 +12394,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ARITHMETIC:
case CPTK_IS_ARRAY:
case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index efce04fd09d..4bc85f4babb 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_arithmetic)
+# error "__has_builtin (__is_arithmetic) failed"
+#endif
#if !__has_builtin (__is_array)
# error "__has_builtin (__is_array) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
new file mode 100644
index 00000000000..fd35831f646
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_arithmetic, void, false);
+
+SA_TEST_CATEGORY(__is_arithmetic, char, true);
+SA_TEST_CATEGORY(__is_arithmetic, signed char, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true);
+SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true);
+SA_TEST_CATEGORY(__is_arithmetic, short, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true);
+SA_TEST_CATEGORY(__is_arithmetic, int, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true);
+SA_TEST_CATEGORY(__is_arithmetic, long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true);
+SA_TEST_CATEGORY(__is_arithmetic, long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, float, true);
+SA_TEST_CATEGORY(__is_arithmetic, double, true);
+SA_TEST_CATEGORY(__is_arithmetic, long double, true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index f08bbf6fd7b..79d304e0309 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -217,16 +217,16 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef __true_type __type;
};
template<typename _Tp>
- struct __is_arithmetic
+ struct __is_arith
: public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
{ };
template<typename _Tp>
struct __is_fundamental
- : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
+ : public __traitor<__is_void<_Tp>, __is_arith<_Tp> >
{ };
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
+ : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
struct __is_char
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index be0689096fb..da592b9fd23 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -25,9 +25,9 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_arithmetic : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
+ template<typename _Tp> struct __is_arith : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> > {
+ template<typename _Tp> struct __is_scalar : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 4da1e7c407c..51ed5b07716 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -389,7 +389,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// An arithmetic type is an integer type or a floating point type
//
template<typename _Tp>
- struct __is_arithmetic
+ struct __is_arith
: public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
{ };
@@ -398,7 +398,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
+ : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
//
diff --git a/libstdc++-v3/include/c_global/cmath b/libstdc++-v3/include/c_global/cmath
index 6461c92ebfe..a0ddc1dbbeb 100644
--- a/libstdc++-v3/include/c_global/cmath
+++ b/libstdc++-v3/include/c_global/cmath
@@ -1259,8 +1259,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isgreater(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1285,8 +1285,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isgreaterequal(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1311,8 +1311,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isless(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1337,8 +1337,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
islessequal(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1363,8 +1363,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
islessgreater(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1389,8 +1389,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isunordered(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1401,7 +1401,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#else
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -1411,7 +1411,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -1420,7 +1420,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -1429,7 +1429,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -1438,7 +1438,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -1447,7 +1447,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -1456,7 +1456,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -1465,7 +1465,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -1474,7 +1474,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -1483,7 +1483,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -1492,7 +1492,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -1501,7 +1501,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
diff --git a/libstdc++-v3/include/c_std/cmath b/libstdc++-v3/include/c_std/cmath
index 588ee1e6dc4..c1db699ecdb 100644
--- a/libstdc++-v3/include/c_std/cmath
+++ b/libstdc++-v3/include/c_std/cmath
@@ -467,7 +467,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#undef isunordered
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -477,7 +477,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -486,7 +486,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -495,7 +495,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -504,7 +504,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -513,7 +513,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -522,7 +522,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -531,7 +531,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -540,7 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -549,7 +549,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -558,7 +558,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -567,7 +567,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
diff --git a/libstdc++-v3/include/tr1/cmath b/libstdc++-v3/include/tr1/cmath
index ba1b60cc945..2e80f1d0d00 100644
--- a/libstdc++-v3/include/tr1/cmath
+++ b/libstdc++-v3/include/tr1/cmath
@@ -307,7 +307,7 @@ namespace tr1
/// Function template definitions [8.16.3].
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -317,7 +317,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -326,7 +326,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -335,7 +335,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -344,7 +344,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -353,7 +353,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -362,7 +362,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -371,7 +371,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -380,7 +380,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -389,7 +389,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -398,7 +398,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -407,7 +407,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 32/40] libstdc++: Optimize is_arithmetic trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (30 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 33/40] libstdc++: Optimize is_fundamental " Ken Matsui
` (8 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_arithmetic trait by dispatching
to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_arithmetic): Use __is_arithmetic
built-in trait.
(is_arithmetic_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 3acd843f2f2..cc466e0f606 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -726,10 +726,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_arithmetic
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+ template<typename _Tp>
+ struct is_arithmetic
+ : public __bool_constant<__is_arithmetic(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_arithmetic
: public __or_<is_integral<_Tp>, is_floating_point<_Tp>>::type
{ };
+#endif
/// is_fundamental
template<typename _Tp>
@@ -3344,8 +3351,14 @@ template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&&> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+template <typename _Tp>
+ inline constexpr bool is_arithmetic_v = __is_arithmetic(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 33/40] libstdc++: Optimize is_fundamental trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (31 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 32/40] libstdc++: Optimize is_arithmetic trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 34/40] libstdc++: Optimize is_compound " Ken Matsui
` (7 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_fundamental trait by
dispatching to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_fundamental_v): Use __is_arithmetic
built-in trait.
(is_fundamental): Likewise. Optimize the original implementation.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index cc466e0f606..88171e1a672 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -739,11 +739,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_fundamental
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+ template<typename _Tp>
+ struct is_fundamental
+ : public __bool_constant<__is_arithmetic(_Tp)
+ || is_void<_Tp>::value
+ || is_null_pointer<_Tp>::value>
+ { };
+#else
template<typename _Tp>
struct is_fundamental
- : public __or_<is_arithmetic<_Tp>, is_void<_Tp>,
- is_null_pointer<_Tp>>::type
+ : public __bool_constant<is_arithmetic<_Tp>::value
+ || is_void<_Tp>::value
+ || is_null_pointer<_Tp>::value>
{ };
+#endif
/// is_object
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
@@ -3354,13 +3364,15 @@ template <typename _Tp>
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
template <typename _Tp>
inline constexpr bool is_arithmetic_v = __is_arithmetic(_Tp);
+template <typename _Tp>
+ inline constexpr bool is_fundamental_v
+ = __is_arithmetic(_Tp) || is_void_v<_Tp> || is_null_pointer_v<_Tp>;
#else
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
-#endif
-
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
&& _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 34/40] libstdc++: Optimize is_compound trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (32 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 33/40] libstdc++: Optimize is_fundamental " Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 35/40] c++: Implement __is_unsigned built-in trait Ken Matsui
` (6 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_compound trait by dispatching
to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_compound): Do not use __not_.
(is_compound_v): Use is_fundamental_v instead.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 88171e1a672..48d630a1478 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -784,7 +784,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// is_compound
template<typename _Tp>
struct is_compound
- : public __not_<is_fundamental<_Tp>>::type { };
+ : public __bool_constant<!is_fundamental<_Tp>::value> { };
/// is_member_pointer
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
@@ -3387,7 +3387,7 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
- inline constexpr bool is_compound_v = is_compound<_Tp>::value;
+ inline constexpr bool is_compound_v = !is_fundamental_v<_Tp>;
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 35/40] c++: Implement __is_unsigned built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (33 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 34/40] libstdc++: Optimize is_compound " Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 36/40] libstdc++: Optimize is_unsigned trait performance Ken Matsui
` (5 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_unsigned.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_unsigned.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNSIGNED.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_unsigned.
* g++.dg/ext/is_unsigned.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 ++++++++++++++++++++++++
5 files changed, 58 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 3a7f968eae8..c28dad702c3 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3829,6 +3829,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
+ case CPTK_IS_UNSIGNED:
+ inform (loc, " %qT is not an unsigned type", t1);
+ break;
case CPTK_IS_VOLATILE:
inform (loc, " %qT is not a volatile type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 06c203ce4de..611e84cbbfd 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -94,6 +94,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_UNSIGNED, "__is_unsigned", 1)
DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 351f453356f..74bfd5f0961 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12235,6 +12235,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
+ case CPTK_IS_UNSIGNED:
+ return TYPE_UNSIGNED (type1);
+
case CPTK_IS_VOLATILE:
return CP_TYPE_VOLATILE_P (type1);
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
+ case CPTK_IS_UNSIGNED:
case CPTK_IS_VOLATILE:
break;
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4bc85f4babb..3d380f94b06 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -164,6 +164,9 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
+#if !__has_builtin (__is_unsigned)
+# error "__has_builtin (__is_unsigned) failed"
+#endif
#if !__has_builtin (__is_volatile)
# error "__has_builtin (__is_volatile) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_unsigned.C b/gcc/testsuite/g++.dg/ext/is_unsigned.C
new file mode 100644
index 00000000000..2bb45d209a7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_unsigned.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_unsigned, void, false);
+
+SA_TEST_CATEGORY(__is_unsigned, bool, (bool(-1) > bool(0)));
+SA_TEST_CATEGORY(__is_unsigned, char, (char(-1) > char(0)));
+SA_TEST_CATEGORY(__is_unsigned, signed char, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned char, true);
+SA_TEST_CATEGORY(__is_unsigned, wchar_t, (wchar_t(-1) > wchar_t(0)));
+SA_TEST_CATEGORY(__is_unsigned, short, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned short, true);
+SA_TEST_CATEGORY(__is_unsigned, int, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned int, true);
+SA_TEST_CATEGORY(__is_unsigned, long, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned long, true);
+SA_TEST_CATEGORY(__is_unsigned, long long, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned long long, true);
+
+SA_TEST_CATEGORY(__is_unsigned, float, false);
+SA_TEST_CATEGORY(__is_unsigned, double, false);
+SA_TEST_CATEGORY(__is_unsigned, long double, false);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_unsigned, unsigned __int128, true);
+SA_TEST_CATEGORY(__is_unsigned, __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_unsigned, __float128, false);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_unsigned, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 36/40] libstdc++: Optimize is_unsigned trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (34 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 35/40] c++: Implement __is_unsigned built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 37/40] c++, libstdc++: Implement __is_signed built-in trait Ken Matsui
` (4 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_unsigned trait by dispatching
to the new __is_unsigned built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_unsigned): Use __is_unsigned built-in
trait.
(is_unsigned_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 48d630a1478..f7d3815f332 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -1001,10 +1001,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_unsigned
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
+ template<typename _Tp>
+ struct is_unsigned
+ : public __bool_constant<__is_unsigned(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_unsigned
: public __and_<is_arithmetic<_Tp>, __not_<is_signed<_Tp>>>::type
{ };
+#endif
/// @cond undocumented
template<typename _Tp, typename _Up = _Tp&&>
@@ -3440,8 +3447,14 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_signed_v = is_signed<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
+template <typename _Tp>
+ inline constexpr bool is_unsigned_v = __is_unsigned(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_unsigned_v = is_unsigned<_Tp>::value;
+#endif
template <typename _Tp, typename... _Args>
inline constexpr bool is_constructible_v = __is_constructible(_Tp, _Args...);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 37/40] c++, libstdc++: Implement __is_signed built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (35 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 36/40] libstdc++: Optimize is_unsigned trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 38/40] libstdc++: Optimize is_signed trait performance Ken Matsui
` (3 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_signed.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_signed.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SIGNED.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_signed.
* g++.dg/ext/is_signed.C: New test.
* g++.dg/tm/pr46567.C (__is_signed): Rename to ...
(__is_signed_type): ... this.
libstdc++-v3/ChangeLog:
* include/ext/numeric_traits.h (__is_signed): Rename to ...
(__is_signed_type): ... this.
* include/bits/charconv.h: Use __is_signed_type instead.
* include/bits/locale_facets.tcc: Likewise.
* include/bits/uniform_int_dist.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_signed.C | 47 ++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 12 ++---
libstdc++-v3/include/bits/charconv.h | 2 +-
libstdc++-v3/include/bits/locale_facets.tcc | 6 +--
libstdc++-v3/include/bits/uniform_int_dist.h | 4 +-
libstdc++-v3/include/ext/numeric_traits.h | 18 ++++----
10 files changed, 79 insertions(+), 21 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c28dad702c3..b161c9b2c9e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3802,6 +3802,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SIGNED:
+ inform (loc, " %qT is not a signed type", t1);
+ break;
case CPTK_IS_SCOPED_ENUM:
inform (loc, " %qT is not a scoped enum", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 611e84cbbfd..f0b5fe9cb3b 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -86,6 +86,7 @@ DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 74bfd5f0961..f5aa78adf16 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12211,6 +12211,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SIGNED:
+ return ARITHMETIC_TYPE_P (type1) && TYPE_SIGN (type1) == SIGNED;
+
case CPTK_IS_SCOPED_ENUM:
return SCOPED_ENUM_P (type1);
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
+ case CPTK_IS_SIGNED:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 3d380f94b06..aaf7254df4b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -140,6 +140,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_signed)
+# error "__has_builtin (__is_signed) failed"
+#endif
#if !__has_builtin (__is_scoped_enum)
# error "__has_builtin (__is_scoped_enum) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_signed.C b/gcc/testsuite/g++.dg/ext/is_signed.C
new file mode 100644
index 00000000000..a04b548105d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_signed.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_signed, void, false);
+
+SA_TEST_CATEGORY(__is_signed, bool, bool(-1) < bool(0));
+SA_TEST_CATEGORY(__is_signed, char, char(-1) < char(0));
+SA_TEST_CATEGORY(__is_signed, signed char, true);
+SA_TEST_CATEGORY(__is_signed, unsigned char, false);
+SA_TEST_CATEGORY(__is_signed, wchar_t, wchar_t(-1) < wchar_t(0));
+SA_TEST_CATEGORY(__is_signed, short, true);
+SA_TEST_CATEGORY(__is_signed, unsigned short, false);
+SA_TEST_CATEGORY(__is_signed, int, true);
+SA_TEST_CATEGORY(__is_signed, unsigned int, false);
+SA_TEST_CATEGORY(__is_signed, long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long, false);
+SA_TEST_CATEGORY(__is_signed, long long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long long, false);
+
+SA_TEST_CATEGORY(__is_signed, float, true);
+SA_TEST_CATEGORY(__is_signed, double, true);
+SA_TEST_CATEGORY(__is_signed, long double, true);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_signed, __int128, true);
+SA_TEST_CATEGORY(__is_signed, unsigned __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_signed, __float128, true);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_signed, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 79d304e0309..c891aff20f4 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -403,7 +403,7 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
{
static const _Value __min = (((_Value)(-1) < 0) ? (_Value)1 << (sizeof(_Value) * 8 - ((_Value)(-1) < 0)) : (_Value)0);
static const _Value __max = (((_Value)(-1) < 0) ? (((((_Value)1 << ((sizeof(_Value) * 8 - ((_Value)(-1) < 0)) - 1)) - 1) << 1) + 1) : ~(_Value)0);
- static const bool __is_signed = ((_Value)(-1) < 0);
+ static const bool __is_signed_type = ((_Value)(-1) < 0);
static const int __digits = (sizeof(_Value) * 8 - ((_Value)(-1) < 0));
};
template<typename _Value>
@@ -411,21 +411,21 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
template<typename _Value>
const _Value __numeric_traits_integer<_Value>::__max;
template<typename _Value>
- const bool __numeric_traits_integer<_Value>::__is_signed;
+ const bool __numeric_traits_integer<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_integer<_Value>::__digits;
template<typename _Value>
struct __numeric_traits_floating
{
static const int __max_digits10 = (2 + (std::__are_same<_Value, float>::__value ? 24 : std::__are_same<_Value, double>::__value ? 53 : 64) * 3010 / 10000);
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = (std::__are_same<_Value, float>::__value ? 6 : std::__are_same<_Value, double>::__value ? 15 : 18);
static const int __max_exponent10 = (std::__are_same<_Value, float>::__value ? 38 : std::__are_same<_Value, double>::__value ? 308 : 4932);
};
template<typename _Value>
const int __numeric_traits_floating<_Value>::__max_digits10;
template<typename _Value>
- const bool __numeric_traits_floating<_Value>::__is_signed;
+ const bool __numeric_traits_floating<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_floating<_Value>::__digits10;
template<typename _Value>
@@ -1513,8 +1513,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
- && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
- && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
+ && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed_type
+ && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed_type
&& __is_ptr<_II1>::__value
&& __is_ptr<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
diff --git a/libstdc++-v3/include/bits/charconv.h b/libstdc++-v3/include/bits/charconv.h
index 20da8303f7a..1acf1e46e4c 100644
--- a/libstdc++-v3/include/bits/charconv.h
+++ b/libstdc++-v3/include/bits/charconv.h
@@ -46,7 +46,7 @@ namespace __detail
// This accepts 128-bit integers even in strict mode.
template<typename _Tp>
constexpr bool __integer_to_chars_is_unsigned
- = ! __gnu_cxx::__int_traits<_Tp>::__is_signed;
+ = ! __gnu_cxx::__int_traits<_Tp>::__is_signed_type;
#endif
// Generic implementation for arbitrary bases.
diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc
index 6bfff7d6289..38a6920abe9 100644
--- a/libstdc++-v3/include/bits/locale_facets.tcc
+++ b/libstdc++-v3/include/bits/locale_facets.tcc
@@ -470,7 +470,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
bool __testfail = false;
bool __testoverflow = false;
const __unsigned_type __max =
- (__negative && __num_traits::__is_signed)
+ (__negative && __num_traits::__is_signed_type)
? -static_cast<__unsigned_type>(__num_traits::__min)
: __num_traits::__max;
const __unsigned_type __smax = __max / __base;
@@ -573,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
}
else if (__testoverflow)
{
- if (__negative && __num_traits::__is_signed)
+ if (__negative && __num_traits::__is_signed_type)
__v = __num_traits::__min;
else
__v = __num_traits::__max;
@@ -914,7 +914,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
if (__v >= 0)
{
if (bool(__flags & ios_base::showpos)
- && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
+ && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed_type)
*--__cs = __lit[__num_base::_S_oplus], ++__len;
}
else
diff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h
index 7ccf930a6d4..73b808e57f3 100644
--- a/libstdc++-v3/include/bits/uniform_int_dist.h
+++ b/libstdc++-v3/include/bits/uniform_int_dist.h
@@ -258,8 +258,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
using _Up_traits = __gnu_cxx::__int_traits<_Up>;
using _Wp_traits = __gnu_cxx::__int_traits<_Wp>;
- static_assert(!_Up_traits::__is_signed, "U must be unsigned");
- static_assert(!_Wp_traits::__is_signed, "W must be unsigned");
+ static_assert(!_Up_traits::__is_signed_type, "U must be unsigned");
+ static_assert(!_Wp_traits::__is_signed_type, "W must be unsigned");
static_assert(_Wp_traits::__digits == (2 * _Up_traits::__digits),
"W must be twice as wide as U");
diff --git a/libstdc++-v3/include/ext/numeric_traits.h b/libstdc++-v3/include/ext/numeric_traits.h
index dcbc2d12927..c618f211775 100644
--- a/libstdc++-v3/include/ext/numeric_traits.h
+++ b/libstdc++-v3/include/ext/numeric_traits.h
@@ -67,15 +67,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// NB: these two are also available in std::numeric_limits as compile
// time constants, but <limits> is big and we can avoid including it.
- static const bool __is_signed = (_Value)(-1) < 0;
+ static const bool __is_signed_type = (_Value)(-1) < 0;
static const int __digits
- = __is_integer_nonstrict<_Value>::__width - __is_signed;
+ = __is_integer_nonstrict<_Value>::__width - __is_signed_type;
// The initializers must be constants so that __max and __min are too.
- static const _Value __max = __is_signed
+ static const _Value __max = __is_signed_type
? (((((_Value)1 << (__digits - 1)) - 1) << 1) + 1)
: ~(_Value)0;
- static const _Value __min = __is_signed ? -__max - 1 : (_Value)0;
+ static const _Value __min = __is_signed_type ? -__max - 1 : (_Value)0;
};
template<typename _Value>
@@ -85,7 +85,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const _Value __numeric_traits_integer<_Value>::__max;
template<typename _Value>
- const bool __numeric_traits_integer<_Value>::__is_signed;
+ const bool __numeric_traits_integer<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_integer<_Value>::__digits;
@@ -161,7 +161,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static const int __max_digits10 = __glibcxx_max_digits10(_Value);
// See above comment...
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = __glibcxx_digits10(_Value);
static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
};
@@ -170,7 +170,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const int __numeric_traits_floating<_Value>::__max_digits10;
template<typename _Value>
- const bool __numeric_traits_floating<_Value>::__is_signed;
+ const bool __numeric_traits_floating<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_floating<_Value>::__digits10;
@@ -210,7 +210,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __numeric_traits_floating<__ibm128>
{
static const int __max_digits10 = 33;
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = 31;
static const int __max_exponent10 = 308;
};
@@ -224,7 +224,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __numeric_traits_floating<__ieee128>
{
static const int __max_digits10 = 36;
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = 33;
static const int __max_exponent10 = 4932;
};
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 38/40] libstdc++: Optimize is_signed trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (36 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 37/40] c++, libstdc++: Implement __is_signed built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 39/40] c++, libstdc++: Implement __is_scalar built-in trait Ken Matsui
` (2 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_signed trait by dispatching to
the new __is_signed built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_signed): Use __is_signed built-in trait.
(is_signed_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index f7d3815f332..7e93923f44b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -982,6 +982,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __bool_constant<__is_abstract(_Tp)>
{ };
+ /// is_signed
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_signed)
+ template<typename _Tp>
+ struct is_signed
+ : public __bool_constant<__is_signed(_Tp)>
+ { };
+#else
/// @cond undocumented
template<typename _Tp,
bool = is_arithmetic<_Tp>::value>
@@ -994,11 +1001,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// @endcond
- /// is_signed
template<typename _Tp>
struct is_signed
: public __is_signed_helper<_Tp>::type
{ };
+#endif
/// is_unsigned
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
@@ -3445,8 +3452,13 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_final_v = __is_final(_Tp);
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_signed)
+template <typename _Tp>
+ inline constexpr bool is_signed_v = __is_signed(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_signed_v = is_signed<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 39/40] c++, libstdc++: Implement __is_scalar built-in trait
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (37 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 38/40] libstdc++: Optimize is_signed trait performance Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-14 6:43 ` [PATCH v11 40/40] libstdc++: Optimize is_scalar trait performance Ken Matsui
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_scalar. The existent
__is_scalar codes were replaced with __is_scalar_type to avoid unintentional
macro replacement by the new built-in.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_scalar.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCALAR.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_scalar.
* g++.dg/ext/is_scalar.C: New test.
* g++.dg/tm/pr46567.C: Use __is_scalar_type instead.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_scalar): Rename to ...
(__is_scalar_type): ... this.
* include/bits/stl_algobase.h: Use __is_scalar_type instead.
* include/bits/valarray_array.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_scalar.C | 31 +++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 10 +++----
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +--
libstdc++-v3/include/bits/cpp_type_traits.h | 2 +-
libstdc++-v3/include/bits/stl_algobase.h | 8 +++---
libstdc++-v3/include/bits/valarray_array.h | 2 +-
10 files changed, 55 insertions(+), 13 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index b161c9b2c9e..78f100d2745 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3802,6 +3802,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SCALAR:
+ inform (loc, " %qT is not a scalar type", t1);
+ break;
case CPTK_IS_SIGNED:
inform (loc, " %qT is not a signed type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index f0b5fe9cb3b..4e220262020 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -86,6 +86,7 @@ DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SCALAR, "__is_scalar", 1)
DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index f5aa78adf16..91631c211ab 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12211,6 +12211,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SCALAR:
+ return SCALAR_TYPE_P (type1);
+
case CPTK_IS_SIGNED:
return ARITHMETIC_TYPE_P (type1) && TYPE_SIGN (type1) == SIGNED;
@@ -12413,6 +12416,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
+ case CPTK_IS_SCALAR:
case CPTK_IS_SIGNED:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index aaf7254df4b..f4f6fed6876 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -140,6 +140,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_scalar)
+# error "__has_builtin (__is_scalar) failed"
+#endif
#if !__has_builtin (__is_signed)
# error "__has_builtin (__is_signed) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_scalar.C b/gcc/testsuite/g++.dg/ext/is_scalar.C
new file mode 100644
index 00000000000..457fddc52fc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_scalar.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+
+#include <cstddef> // std::nullptr_t
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// volatile return type would cause a warning.
+#define SA_FN_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_scalar, int, true);
+SA_TEST_CATEGORY(__is_scalar, float, true);
+SA_TEST_CATEGORY(__is_scalar, EnumType, true);
+SA_TEST_CATEGORY(__is_scalar, int*, true);
+SA_FN_TEST_CATEGORY(__is_scalar, int(*)(int), true);
+SA_TEST_CATEGORY(__is_scalar, int (ClassType::*), true);
+SA_FN_TEST_CATEGORY(__is_scalar, int (ClassType::*) (int), true);
+SA_TEST_CATEGORY(__is_scalar, std::nullptr_t, true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_scalar, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index c891aff20f4..393f936ea72 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -225,7 +225,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
: public __traitor<__is_void<_Tp>, __is_arith<_Tp> >
{ };
template<typename _Tp>
- struct __is_scalar
+ struct __is_scalar_type
: public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
@@ -1325,7 +1325,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _ForwardIterator, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, void>::__type
__fill_a(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -1334,7 +1334,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _ForwardIterator, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, void>::__type
__fill_a(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -1362,7 +1362,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _OutputIterator, typename _Size, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
for (; __n > 0; --__n, ++__first)
@@ -1371,7 +1371,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _OutputIterator, typename _Size, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
const _Tp __tmp = __value;
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index da592b9fd23..4d2ef002e08 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -27,7 +27,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Tp> struct __is_arith : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
+ template<typename _Tp> struct __is_scalar_type : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
@@ -54,7 +54,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Iterator> inline typename _Niter_base<_Iterator>::iterator_type __niter_base(_Iterator __it) {
}
- template<typename _OutputIterator, typename _Size, typename _Tp> inline typename __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) {
+ template<typename _OutputIterator, typename _Size, typename _Tp> inline typename __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, _OutputIterator>::__type __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) {
for (__decltype(__n + 0) __niter = __n;
__niter > 0;
--__niter, ++__first) *__first = __value;
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 51ed5b07716..16980f5b356 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -397,7 +397,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// A scalar type is an arithmetic type or a pointer type
//
template<typename _Tp>
- struct __is_scalar
+ struct __is_scalar_type
: public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index d1438429487..4e334da0832 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -914,7 +914,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _ForwardIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, void>::__type
__fill_a1(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -925,7 +925,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _ForwardIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, void>::__type
__fill_a1(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -1063,7 +1063,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _OutputIterator, typename _Size, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
{
for (; __n > 0; --__n, (void) ++__first)
@@ -1074,7 +1074,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _OutputIterator, typename _Size, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
{
const _Tp __tmp = __value;
diff --git a/libstdc++-v3/include/bits/valarray_array.h b/libstdc++-v3/include/bits/valarray_array.h
index 222fd5fd900..558817329ce 100644
--- a/libstdc++-v3/include/bits/valarray_array.h
+++ b/libstdc++-v3/include/bits/valarray_array.h
@@ -90,7 +90,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline void
__valarray_default_construct(_Tp* __b, _Tp* __e)
{
- _Array_default_ctor<_Tp, __is_scalar<_Tp>::__value>::_S_do_it(__b, __e);
+ _Array_default_ctor<_Tp, __is_scalar_type<_Tp>::__value>::_S_do_it(__b, __e);
}
// Turn a raw-memory into an array of _Tp filled with __t
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v11 40/40] libstdc++: Optimize is_scalar trait performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (38 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 39/40] c++, libstdc++: Implement __is_scalar built-in trait Ken Matsui
@ 2023-09-14 6:43 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 6:43 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_scalar trait by dispatching to
the new __is_scalar built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_scalar): Use __is_scalar built-in
trait.
(is_scalar_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 7e93923f44b..eb16a642575 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -775,11 +775,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_member_pointer;
/// is_scalar
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scalar)
+ template<typename _Tp>
+ struct is_scalar
+ : public __bool_constant<__is_scalar(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_scalar
: public __or_<is_arithmetic<_Tp>, is_enum<_Tp>, is_pointer<_Tp>,
is_member_pointer<_Tp>, is_null_pointer<_Tp>>::type
{ };
+#endif
/// is_compound
template<typename _Tp>
@@ -3398,8 +3405,14 @@ template <typename _Tp>
inline constexpr bool is_object_v = is_object<_Tp>::value;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scalar)
+template <typename _Tp>
+ inline constexpr bool is_scalar_v = __is_scalar(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_compound_v = !is_fundamental_v<_Tp>;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* Re: [PATCH v11 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space
2023-09-14 6:42 ` [PATCH v11 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
@ 2023-09-14 17:53 ` Joseph Myers
2023-09-14 21:44 ` Ken Matsui
2023-09-15 23:26 ` Ken Matsui
0 siblings, 2 replies; 629+ messages in thread
From: Joseph Myers @ 2023-09-14 17:53 UTC (permalink / raw)
To: Ken Matsui; +Cc: gcc-patches, libstdc++
On Wed, 13 Sep 2023, Ken Matsui via Gcc-patches wrote:
> diff --git a/gcc/c/c-parser.h b/gcc/c/c-parser.h
> index 545f0f4d9eb..eed6deaf0f8 100644
> --- a/gcc/c/c-parser.h
> +++ b/gcc/c/c-parser.h
> @@ -51,14 +51,14 @@ enum c_id_kind {
> /* A single C token after string literal concatenation and conversion
> of preprocessing tokens to tokens. */
> struct GTY (()) c_token {
> + /* If this token is a keyword, this value indicates which keyword.
> + Otherwise, this value is RID_MAX. */
> + ENUM_BITFIELD (rid) keyword : 16;
> /* The kind of token. */
> ENUM_BITFIELD (cpp_ttype) type : 8;
> /* If this token is a CPP_NAME, this value indicates whether also
> declared as some kind of type. Otherwise, it is C_ID_NONE. */
> ENUM_BITFIELD (c_id_kind) id_kind : 8;
> - /* If this token is a keyword, this value indicates which keyword.
> - Otherwise, this value is RID_MAX. */
> - ENUM_BITFIELD (rid) keyword : 8;
> /* If this token is a CPP_PRAGMA, this indicates the pragma that
> was seen. Otherwise it is PRAGMA_NONE. */
> ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
If you want to optimize layout, I'd expect flags to move so it can share
the same 32-bit unit as the pragma_kind bit-field (not sure if any changes
should be made to the declaration of flags to maximise the chance of such
sharing across different host bit-field ABIs).
> diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
> index 6cbb9a8e031..3c3c482c6ce 100644
> --- a/gcc/cp/parser.h
> +++ b/gcc/cp/parser.h
> @@ -40,11 +40,11 @@ struct GTY(()) tree_check {
> /* A C++ token. */
>
> struct GTY (()) cp_token {
> - /* The kind of token. */
> - enum cpp_ttype type : 8;
> /* If this token is a keyword, this value indicates which keyword.
> Otherwise, this value is RID_MAX. */
> - enum rid keyword : 8;
> + enum rid keyword : 16;
> + /* The kind of token. */
> + enum cpp_ttype type : 8;
> /* Token flags. */
> unsigned char flags;
> /* True if this token is from a context where it is implicitly extern "C" */
You're missing an update to the "3 unused bits." comment further down.
> @@ -988,7 +988,7 @@ struct GTY(()) cpp_hashnode {
> unsigned int directive_index : 7; /* If is_directive,
> then index into directive table.
> Otherwise, a NODE_OPERATOR. */
> - unsigned int rid_code : 8; /* Rid code - for front ends. */
> + unsigned int rid_code : 16; /* Rid code - for front ends. */
> unsigned int flags : 9; /* CPP flags. */
> ENUM_BITFIELD(node_type) type : 2; /* CPP node type. */
You're missing an update to the "5 bits spare." comment further down.
Do you have any figures for the effects on compilation time or memory
usage from the increase in size of these structures?
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 629+ messages in thread
* Re: [PATCH v11 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space
2023-09-14 17:53 ` Joseph Myers
@ 2023-09-14 21:44 ` Ken Matsui
2023-09-15 23:26 ` Ken Matsui
1 sibling, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-14 21:44 UTC (permalink / raw)
To: Joseph Myers; +Cc: Ken Matsui, gcc-patches, libstdc++
On Thu, Sep 14, 2023 at 10:54 AM Joseph Myers <joseph@codesourcery.com> wrote:
>
> On Wed, 13 Sep 2023, Ken Matsui via Gcc-patches wrote:
>
> > diff --git a/gcc/c/c-parser.h b/gcc/c/c-parser.h
> > index 545f0f4d9eb..eed6deaf0f8 100644
> > --- a/gcc/c/c-parser.h
> > +++ b/gcc/c/c-parser.h
> > @@ -51,14 +51,14 @@ enum c_id_kind {
> > /* A single C token after string literal concatenation and conversion
> > of preprocessing tokens to tokens. */
> > struct GTY (()) c_token {
> > + /* If this token is a keyword, this value indicates which keyword.
> > + Otherwise, this value is RID_MAX. */
> > + ENUM_BITFIELD (rid) keyword : 16;
> > /* The kind of token. */
> > ENUM_BITFIELD (cpp_ttype) type : 8;
> > /* If this token is a CPP_NAME, this value indicates whether also
> > declared as some kind of type. Otherwise, it is C_ID_NONE. */
> > ENUM_BITFIELD (c_id_kind) id_kind : 8;
> > - /* If this token is a keyword, this value indicates which keyword.
> > - Otherwise, this value is RID_MAX. */
> > - ENUM_BITFIELD (rid) keyword : 8;
> > /* If this token is a CPP_PRAGMA, this indicates the pragma that
> > was seen. Otherwise it is PRAGMA_NONE. */
> > ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
>
> If you want to optimize layout, I'd expect flags to move so it can share
> the same 32-bit unit as the pragma_kind bit-field (not sure if any changes
> should be made to the declaration of flags to maximise the chance of such
> sharing across different host bit-field ABIs).
>
Thank you for your review!
I did not make this change aggressively, but we can do the following
to minimize the fragmentation:
struct GTY (()) c_token {
tree value; /* pointer, depends, but 4 or 8 bytes as usual */
location_t location; /* unsigned int, at least 2 bytes, 4 bytes as usual */
ENUM_BITFIELD (rid) keyword : 16; /* 2 bytes */
ENUM_BITFIELD (cpp_ttype) type : 8; /* 1 byte */
ENUM_BITFIELD (c_id_kind) id_kind : 8; /* 1 byte */
ENUM_BITFIELD (pragma_kind) pragma_kind : 8; /* 1 byte */
unsigned char flags; /* 1 byte */
}
Supposing a pointer size is 8 bytes and int is 4 bytes, the struct
size would be 24 bytes. The internal fragmentation would be 0 bytes,
and the external fragmentation is 6 bytes since the overall struct
alignment requirement is $K_{max} = 8$ from the pointer.
Here is the original struct before making keyword 16-bit. The overall
struct alignment requirement is $K_{max} = 8$ from the pointer. This
struct size would be 24 bytes since the internal fragmentation is 4
bytes (after location), and the external fragmentation is 3 bytes.
struct GTY (()) c_token {
ENUM_BITFIELD (cpp_ttype) type : 8; /* 1 byte */
ENUM_BITFIELD (c_id_kind) id_kind : 8; /* 1 byte */
ENUM_BITFIELD (rid) keyword : 8; /* 1 byte */
ENUM_BITFIELD (pragma_kind) pragma_kind : 8; /* 1 byte */
location_t location; /* unsigned int, at least 2 bytes, 4 bytes as usual */
tree value; /* pointer, depends, but 4 or 8 bytes as usual */
unsigned char flags; /* 1 byte */
}
If we keep the original order with the 16-bit keyword, the struct size
would be 32 bytes (my current implementation as well, I will update
this patch).
struct GTY (()) c_token {
ENUM_BITFIELD (cpp_ttype) type : 8; /* 1 byte */
ENUM_BITFIELD (c_id_kind) id_kind : 8; /* 1 byte */
ENUM_BITFIELD (rid) keyword : 16; /* 2 bytes */
ENUM_BITFIELD (pragma_kind) pragma_kind : 8; /* 1 byte */
location_t location; /* unsigned int, at least 2 bytes, 4 bytes as usual */
tree value; /* pointer, depends, but 4 or 8 bytes as usual */
unsigned char flags; /* 1 byte */
}
Likewise, the overall struct alignment requirement is $K_{max} = 8$
from the pointer. The internal fragmentation would be 7 bytes (3 bytes
after pragma_kind + 4 bytes after location), and the external
fragmentation would be 7 bytes.
I think optimizing the size is worth doing unless this breaks GCC.
> > diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
> > index 6cbb9a8e031..3c3c482c6ce 100644
> > --- a/gcc/cp/parser.h
> > +++ b/gcc/cp/parser.h
> > @@ -40,11 +40,11 @@ struct GTY(()) tree_check {
> > /* A C++ token. */
> >
> > struct GTY (()) cp_token {
> > - /* The kind of token. */
> > - enum cpp_ttype type : 8;
> > /* If this token is a keyword, this value indicates which keyword.
> > Otherwise, this value is RID_MAX. */
> > - enum rid keyword : 8;
> > + enum rid keyword : 16;
> > + /* The kind of token. */
> > + enum cpp_ttype type : 8;
> > /* Token flags. */
> > unsigned char flags;
> > /* True if this token is from a context where it is implicitly extern "C" */
>
> You're missing an update to the "3 unused bits." comment further down.
>
> > @@ -988,7 +988,7 @@ struct GTY(()) cpp_hashnode {
> > unsigned int directive_index : 7; /* If is_directive,
> > then index into directive table.
> > Otherwise, a NODE_OPERATOR. */
> > - unsigned int rid_code : 8; /* Rid code - for front ends. */
> > + unsigned int rid_code : 16; /* Rid code - for front ends. */
> > unsigned int flags : 9; /* CPP flags. */
> > ENUM_BITFIELD(node_type) type : 2; /* CPP node type. */
>
> You're missing an update to the "5 bits spare." comment further down.
>
Thank you!
> Do you have any figures for the effects on compilation time or memory
> usage from the increase in size of these structures?
>
Regarding only c_token, we will have the same size if we optimize the
size. Although I did not calculate the size of other structs, we might
not see any significant performance change? I am taking benchmarks and
will let you know once it is done.
> --
> Joseph S. Myers
> joseph@codesourcery.com
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 00/40] Optimize type traits performance
2023-09-14 6:42 [PATCH v11 00/40] Optimize type traits performance Ken Matsui
` (39 preceding siblings ...)
2023-09-14 6:43 ` [PATCH v11 40/40] libstdc++: Optimize is_scalar trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
` (40 more replies)
40 siblings, 41 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch series optimizes type traits performance by implementing
built-in type traits and using them in libstdc++.
Changes in v12:
* Evaluated all paddings affected by the enum rid change
Changes in v11:
* Merged all patches into one patch series
* Rebased on top of trunk
* Unified commit message style
* Used _GLIBCXX_USE_BUILTIN_TRAIT
Ken Matsui (40):
c++: Sort built-in identifiers alphabetically
c++: Implement __is_const built-in trait
libstdc++: Optimize is_const trait performance
c++: Implement __is_volatile built-in trait
libstdc++: Optimize is_volatile trait performance
c++: Implement __is_array built-in trait
libstdc++: Optimize is_array trait performance
c++: Implement __is_unbounded_array built-in trait
libstdc++: Optimize is_unbounded_array trait performance
c++: Implement __is_bounded_array built-in trait
libstdc++: Optimize is_bounded_array trait performance
c++: Implement __is_scoped_enum built-in trait
libstdc++: Optimize is_scoped_enum trait performance
c++: Implement __is_member_pointer built-in trait
libstdc++: Optimize is_member_pointer trait performance
c, c++: Use 16 bits for all use of enum rid for more keyword space
c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly
c++: Implement __is_member_function_pointer built-in trait
libstdc++: Optimize is_member_function_pointer trait performance
c++: Implement __is_member_object_pointer built-in trait
libstdc++: Optimize is_member_object_pointer trait performance
c++: Implement __is_reference built-in trait
libstdc++: Optimize is_reference trait performance
c++: Implement __is_function built-in trait
libstdc++: Optimize is_function trait performance
libstdc++: Optimize is_object trait performance
c++: Implement __remove_pointer built-in trait
libstdc++: Optimize remove_pointer trait performance
c++, libstdc++: Implement __is_pointer built-in trait
libstdc++: Optimize is_pointer trait performance
c++, libstdc++: Implement __is_arithmetic built-in trait
libstdc++: Optimize is_arithmetic trait performance
libstdc++: Optimize is_fundamental trait performance
libstdc++: Optimize is_compound trait performance
c++: Implement __is_unsigned built-in trait
libstdc++: Optimize is_unsigned trait performance
c++, libstdc++: Implement __is_signed built-in trait
libstdc++: Optimize is_signed trait performance
c++, libstdc++: Implement __is_scalar built-in trait
libstdc++: Optimize is_scalar trait performance
gcc/c-family/c-common.h | 2 +-
gcc/c-family/c-indentation.h | 2 +-
gcc/c/c-parser.cc | 6 +-
gcc/c/c-parser.h | 14 +-
gcc/cp/constraint.cc | 112 +++++--
gcc/cp/cp-trait.def | 27 +-
gcc/cp/parser.h | 8 +-
gcc/cp/semantics.cc | 157 +++++++---
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 117 ++++++--
gcc/testsuite/g++.dg/ext/is_arithmetic.C | 33 ++
gcc/testsuite/g++.dg/ext/is_array.C | 28 ++
gcc/testsuite/g++.dg/ext/is_bounded_array.C | 38 +++
gcc/testsuite/g++.dg/ext/is_const.C | 19 ++
gcc/testsuite/g++.dg/ext/is_function.C | 58 ++++
.../g++.dg/ext/is_member_function_pointer.C | 31 ++
.../g++.dg/ext/is_member_object_pointer.C | 30 ++
gcc/testsuite/g++.dg/ext/is_member_pointer.C | 30 ++
gcc/testsuite/g++.dg/ext/is_pointer.C | 51 ++++
gcc/testsuite/g++.dg/ext/is_reference.C | 34 +++
gcc/testsuite/g++.dg/ext/is_scalar.C | 31 ++
gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++
gcc/testsuite/g++.dg/ext/is_signed.C | 47 +++
gcc/testsuite/g++.dg/ext/is_unbounded_array.C | 37 +++
gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 +++
gcc/testsuite/g++.dg/ext/is_volatile.C | 19 ++
gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 ++++
gcc/testsuite/g++.dg/tm/pr46567.C | 48 +--
gcc/testsuite/g++.dg/torture/20070621-1.C | 4 +-
gcc/testsuite/g++.dg/torture/pr57107.C | 8 +-
libcpp/include/cpplib.h | 7 +-
libstdc++-v3/include/bits/charconv.h | 2 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 18 +-
libstdc++-v3/include/bits/deque.tcc | 6 +-
libstdc++-v3/include/bits/locale_facets.tcc | 6 +-
libstdc++-v3/include/bits/stl_algobase.h | 14 +-
libstdc++-v3/include/bits/uniform_int_dist.h | 4 +-
libstdc++-v3/include/bits/valarray_array.h | 2 +-
libstdc++-v3/include/c_global/cmath | 48 +--
libstdc++-v3/include/c_std/cmath | 24 +-
libstdc++-v3/include/ext/numeric_traits.h | 18 +-
libstdc++-v3/include/std/type_traits | 284 ++++++++++++++++--
libstdc++-v3/include/tr1/cmath | 24 +-
42 files changed, 1356 insertions(+), 257 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_bounded_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_const.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_function.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_reference.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_unbounded_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_volatile.C
create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 01/40] c++: Sort built-in identifiers alphabetically
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 02/40] c++: Implement __is_const built-in trait Ken Matsui
` (39 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch sorts built-in identifiers alphabetically for better code
readability.
gcc/cp/ChangeLog:
* constraint.cc (diagnose_trait_expr): Sort built-in identifiers
alphabetically.
* cp-trait.def: Likewise.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
(finish_trait_type): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Sort built-in identifiers
alphabetically.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 68 ++++++++---------
gcc/cp/cp-trait.def | 10 +--
gcc/cp/semantics.cc | 94 ++++++++++++------------
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 70 +++++++++---------
4 files changed, 121 insertions(+), 121 deletions(-)
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9e4e7043cd..722fc334e6f 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3702,18 +3702,36 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_HAS_TRIVIAL_DESTRUCTOR:
inform (loc, " %qT is not trivially destructible", t1);
break;
+ case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
+ inform (loc, " %qT does not have unique object representations", t1);
+ break;
case CPTK_HAS_VIRTUAL_DESTRUCTOR:
inform (loc, " %qT does not have a virtual destructor", t1);
break;
case CPTK_IS_ABSTRACT:
inform (loc, " %qT is not an abstract class", t1);
break;
+ case CPTK_IS_AGGREGATE:
+ inform (loc, " %qT is not an aggregate", t1);
+ break;
+ case CPTK_IS_ASSIGNABLE:
+ inform (loc, " %qT is not assignable from %qT", t1, t2);
+ break;
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
+ case CPTK_IS_CONSTRUCTIBLE:
+ if (!t2)
+ inform (loc, " %qT is not default constructible", t1);
+ else
+ inform (loc, " %qT is not constructible from %qE", t1, t2);
+ break;
+ case CPTK_IS_CONVERTIBLE:
+ inform (loc, " %qT is not convertible from %qE", t2, t1);
+ break;
case CPTK_IS_EMPTY:
inform (loc, " %qT is not an empty class", t1);
break;
@@ -3729,6 +3747,18 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_NOTHROW_ASSIGNABLE:
+ inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
+ break;
+ case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
+ if (!t2)
+ inform (loc, " %qT is not nothrow default constructible", t1);
+ else
+ inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
+ break;
+ case CPTK_IS_NOTHROW_CONVERTIBLE:
+ inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
+ break;
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
inform (loc, " %qT is not pointer-interconvertible base of %qT",
t1, t2);
@@ -3748,50 +3778,20 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_TRIVIAL:
inform (loc, " %qT is not a trivial type", t1);
break;
- case CPTK_IS_UNION:
- inform (loc, " %qT is not a union", t1);
- break;
- case CPTK_IS_AGGREGATE:
- inform (loc, " %qT is not an aggregate", t1);
- break;
- case CPTK_IS_TRIVIALLY_COPYABLE:
- inform (loc, " %qT is not trivially copyable", t1);
- break;
- case CPTK_IS_ASSIGNABLE:
- inform (loc, " %qT is not assignable from %qT", t1, t2);
- break;
case CPTK_IS_TRIVIALLY_ASSIGNABLE:
inform (loc, " %qT is not trivially assignable from %qT", t1, t2);
break;
- case CPTK_IS_NOTHROW_ASSIGNABLE:
- inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
- break;
- case CPTK_IS_CONSTRUCTIBLE:
- if (!t2)
- inform (loc, " %qT is not default constructible", t1);
- else
- inform (loc, " %qT is not constructible from %qE", t1, t2);
- break;
case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
if (!t2)
inform (loc, " %qT is not trivially default constructible", t1);
else
inform (loc, " %qT is not trivially constructible from %qE", t1, t2);
break;
- case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- if (!t2)
- inform (loc, " %qT is not nothrow default constructible", t1);
- else
- inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
- break;
- case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
- inform (loc, " %qT does not have unique object representations", t1);
- break;
- case CPTK_IS_CONVERTIBLE:
- inform (loc, " %qT is not convertible from %qE", t2, t1);
+ case CPTK_IS_TRIVIALLY_COPYABLE:
+ inform (loc, " %qT is not trivially copyable", t1);
break;
- case CPTK_IS_NOTHROW_CONVERTIBLE:
- inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
+ case CPTK_IS_UNION:
+ inform (loc, " %qT is not a union", t1);
break;
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
inform (loc, " %qT is not a reference that binds to a temporary "
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 8b7fece0cc8..ce3733df641 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -84,14 +84,14 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
-/* FIXME Added space to avoid direct usage in GCC 13. */
-DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible ", 2)
-
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
-DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
-DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
+DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+
+/* FIXME Added space to avoid direct usage in GCC 13. */
+DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible ", 2)
/* These traits yield a type pack, not a type, and are represented by
cp_parser_trait as a special BASES tree instead of a TRAIT_TYPE tree. */
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 0f7f4e87ae4..92f4f3fd4f6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12075,15 +12075,6 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& classtype_has_nothrow_assign_or_copy_p (type1,
true))));
- case CPTK_HAS_TRIVIAL_ASSIGN:
- /* ??? The standard seems to be missing the "or array of such a class
- type" wording for this trait. */
- type1 = strip_array_types (type1);
- return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
- && (trivial_type_p (type1)
- || (CLASS_TYPE_P (type1)
- && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
-
case CPTK_HAS_NOTHROW_CONSTRUCTOR:
type1 = strip_array_types (type1);
return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
@@ -12092,17 +12083,26 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& maybe_instantiate_noexcept (t)
&& TYPE_NOTHROW_P (TREE_TYPE (t))));
- case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
- type1 = strip_array_types (type1);
- return (trivial_type_p (type1)
- || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
-
case CPTK_HAS_NOTHROW_COPY:
type1 = strip_array_types (type1);
return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
|| (CLASS_TYPE_P (type1)
&& classtype_has_nothrow_assign_or_copy_p (type1, false)));
+ case CPTK_HAS_TRIVIAL_ASSIGN:
+ /* ??? The standard seems to be missing the "or array of such a class
+ type" wording for this trait. */
+ type1 = strip_array_types (type1);
+ return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
+ && (trivial_type_p (type1)
+ || (CLASS_TYPE_P (type1)
+ && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
+
+ case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
+ type1 = strip_array_types (type1);
+ return (trivial_type_p (type1)
+ || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
+
case CPTK_HAS_TRIVIAL_COPY:
/* ??? The standard seems to be missing the "or array of such a class
type" wording for this trait. */
@@ -12116,18 +12116,21 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
|| (CLASS_TYPE_P (type1)
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
- case CPTK_HAS_VIRTUAL_DESTRUCTOR:
- return type_has_virtual_destructor (type1);
-
case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
return type_has_unique_obj_representations (type1);
+ case CPTK_HAS_VIRTUAL_DESTRUCTOR:
+ return type_has_virtual_destructor (type1);
+
case CPTK_IS_ABSTRACT:
return ABSTRACT_CLASS_TYPE_P (type1);
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ASSIGNABLE:
+ return is_xible (MODIFY_EXPR, type1, type2);
+
case CPTK_IS_BASE_OF:
return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
@@ -12136,6 +12139,12 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
+ case CPTK_IS_CONSTRUCTIBLE:
+ return is_xible (INIT_EXPR, type1, type2);
+
+ case CPTK_IS_CONVERTIBLE:
+ return is_convertible (type1, type2);
+
case CPTK_IS_EMPTY:
return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
@@ -12151,6 +12160,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_NOTHROW_ASSIGNABLE:
+ return is_nothrow_xible (MODIFY_EXPR, type1, type2);
+
+ case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
+ return is_nothrow_xible (INIT_EXPR, type1, type2);
+
+ case CPTK_IS_NOTHROW_CONVERTIBLE:
+ return is_nothrow_convertible (type1, type2);
+
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
return pointer_interconvertible_base_of_p (type1, type2);
@@ -12181,24 +12199,6 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
- case CPTK_IS_ASSIGNABLE:
- return is_xible (MODIFY_EXPR, type1, type2);
-
- case CPTK_IS_CONSTRUCTIBLE:
- return is_xible (INIT_EXPR, type1, type2);
-
- case CPTK_IS_NOTHROW_ASSIGNABLE:
- return is_nothrow_xible (MODIFY_EXPR, type1, type2);
-
- case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- return is_nothrow_xible (INIT_EXPR, type1, type2);
-
- case CPTK_IS_CONVERTIBLE:
- return is_convertible (type1, type2);
-
- case CPTK_IS_NOTHROW_CONVERTIBLE:
- return is_nothrow_convertible (type1, type2);
-
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
@@ -12311,9 +12311,9 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ABSTRACT:
case CPTK_IS_EMPTY:
case CPTK_IS_POLYMORPHIC:
- case CPTK_IS_ABSTRACT:
case CPTK_HAS_VIRTUAL_DESTRUCTOR:
if (!check_trait_type (type1, /* kind = */ 3))
return error_mark_node;
@@ -12333,12 +12333,12 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
- case CPTK_IS_TRIVIALLY_ASSIGNABLE:
- case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
+ case CPTK_IS_CONVERTIBLE:
case CPTK_IS_NOTHROW_ASSIGNABLE:
case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- case CPTK_IS_CONVERTIBLE:
case CPTK_IS_NOTHROW_CONVERTIBLE:
+ case CPTK_IS_TRIVIALLY_ASSIGNABLE:
+ case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
case CPTK_REF_CONVERTS_FROM_TEMPORARY:
if (!check_trait_type (type1)
@@ -12357,8 +12357,8 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_ENUM:
- case CPTK_IS_UNION:
case CPTK_IS_SAME:
+ case CPTK_IS_UNION:
break;
case CPTK_IS_LAYOUT_COMPATIBLE:
@@ -12421,25 +12421,25 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
switch (kind)
{
- case CPTK_UNDERLYING_TYPE:
- return finish_underlying_type (type1);
-
case CPTK_REMOVE_CV:
return cv_unqualified (type1);
- case CPTK_REMOVE_REFERENCE:
+ case CPTK_REMOVE_CVREF:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
- return type1;
+ return cv_unqualified (type1);
- case CPTK_REMOVE_CVREF:
+ case CPTK_REMOVE_REFERENCE:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
- return cv_unqualified (type1);
+ return type1;
case CPTK_TYPE_PACK_ELEMENT:
return finish_type_pack_element (type1, type2, complain);
+ case CPTK_UNDERLYING_TYPE:
+ return finish_underlying_type (type1);
+
#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
case CPTK_##CODE:
#include "cp-trait.def"
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index f343e153e56..2223f08a628 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -8,9 +8,21 @@
#if !__has_builtin (__builtin_bit_cast)
# error "__has_builtin (__builtin_bit_cast) failed"
#endif
+#if !__has_builtin (__builtin_is_constant_evaluated)
+# error "__has_builtin (__builtin_is_constant_evaluated) failed"
+#endif
+#if !__has_builtin (__builtin_is_corresponding_member)
+# error "__has_builtin (__builtin_is_corresponding_member) failed"
+#endif
+#if !__has_builtin (__builtin_is_pointer_interconvertible_with_class)
+# error "__has_builtin (__builtin_is_pointer_interconvertible_with_class) failed"
+#endif
#if !__has_builtin (__builtin_launder)
# error "__has_builtin (__builtin_launder) failed"
#endif
+#if !__has_builtin (__builtin_source_location)
+# error "__has_builtin (__builtin_source_location) failed"
+#endif
#if !__has_builtin (__has_nothrow_assign)
# error "__has_builtin (__has_nothrow_assign) failed"
#endif
@@ -44,12 +56,21 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_assignable)
+# error "__has_builtin (__is_assignable) failed"
+#endif
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
+#if !__has_builtin (__is_constructible)
+# error "__has_builtin (__is_constructible) failed"
+#endif
+#if !__has_builtin (__is_convertible)
+# error "__has_builtin (__is_convertible) failed"
+#endif
#if !__has_builtin (__is_empty)
# error "__has_builtin (__is_empty) failed"
#endif
@@ -65,6 +86,15 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_nothrow_assignable)
+# error "__has_builtin (__is_nothrow_assignable) failed"
+#endif
+#if !__has_builtin (__is_nothrow_constructible)
+# error "__has_builtin (__is_nothrow_constructible) failed"
+#endif
+#if !__has_builtin (__is_nothrow_convertible)
+# error "__has_builtin (__is_nothrow_convertible) failed"
+#endif
#if !__has_builtin (__is_pointer_interconvertible_base_of)
# error "__has_builtin (__is_pointer_interconvertible_base_of) failed"
#endif
@@ -98,51 +128,21 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
-#if !__has_builtin (__underlying_type)
-# error "__has_builtin (__underlying_type) failed"
-#endif
-#if !__has_builtin (__is_assignable)
-# error "__has_builtin (__is_assignable) failed"
-#endif
-#if !__has_builtin (__is_constructible)
-# error "__has_builtin (__is_constructible) failed"
-#endif
-#if !__has_builtin (__is_nothrow_assignable)
-# error "__has_builtin (__is_nothrow_assignable) failed"
-#endif
-#if !__has_builtin (__is_nothrow_constructible)
-# error "__has_builtin (__is_nothrow_constructible) failed"
-#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) failed"
#endif
#if !__has_builtin (__reference_converts_from_temporary)
# error "__has_builtin (__reference_converts_from_temporary) failed"
#endif
-#if !__has_builtin (__builtin_is_constant_evaluated)
-# error "__has_builtin (__builtin_is_constant_evaluated) failed"
-#endif
-#if !__has_builtin (__builtin_source_location)
-# error "__has_builtin (__builtin_source_location) failed"
-#endif
-#if !__has_builtin (__builtin_is_corresponding_member)
-# error "__has_builtin (__builtin_is_corresponding_member) failed"
-#endif
-#if !__has_builtin (__builtin_is_pointer_interconvertible_with_class)
-# error "__has_builtin (__builtin_is_pointer_interconvertible_with_class) failed"
-#endif
-#if !__has_builtin (__is_convertible)
-# error "__has_builtin (__is_convertible) failed"
-#endif
-#if !__has_builtin (__is_nothrow_convertible)
-# error "__has_builtin (__is_nothrow_convertible) failed"
-#endif
#if !__has_builtin (__remove_cv)
# error "__has_builtin (__remove_cv) failed"
#endif
+#if !__has_builtin (__remove_cvref)
+# error "__has_builtin (__remove_cvref) failed"
+#endif
#if !__has_builtin (__remove_reference)
# error "__has_builtin (__remove_reference) failed"
#endif
-#if !__has_builtin (__remove_cvref)
-# error "__has_builtin (__remove_cvref) failed"
+#if !__has_builtin (__underlying_type)
+# error "__has_builtin (__underlying_type) failed"
#endif
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 02/40] c++: Implement __is_const built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
2023-09-15 2:21 ` [PATCH v12 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 03/40] libstdc++: Optimize is_const trait performance Ken Matsui
` (38 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_const.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_const.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_CONST.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_const.
* g++.dg/ext/is_const.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_const.C | 19 +++++++++++++++++++
5 files changed, 30 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_const.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 722fc334e6f..567dd35fe0a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3723,6 +3723,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
+ case CPTK_IS_CONST:
+ inform (loc, " %qT is not a const type", t1);
+ break;
case CPTK_IS_CONSTRUCTIBLE:
if (!t2)
inform (loc, " %qT is not default constructible", t1);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index ce3733df641..a4ebfd9f319 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
+DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 92f4f3fd4f6..17d6e6728f9 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12139,6 +12139,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
+ case CPTK_IS_CONST:
+ return CP_TYPE_CONST_P (type1);
+
case CPTK_IS_CONSTRUCTIBLE:
return is_xible (INIT_EXPR, type1, type2);
@@ -12356,6 +12359,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;
case CPTK_IS_CLASS:
+ case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 2223f08a628..e6e481b13c5 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -65,6 +65,9 @@
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
+#if !__has_builtin (__is_const)
+# error "__has_builtin (__is_const) failed"
+#endif
#if !__has_builtin (__is_constructible)
# error "__has_builtin (__is_constructible) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_const.C b/gcc/testsuite/g++.dg/ext/is_const.C
new file mode 100644
index 00000000000..8f2d7c2fce9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_const.C
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+SA(__is_const(const int));
+SA(__is_const(const volatile int));
+SA(__is_const(cClassType));
+SA(__is_const(cvClassType));
+
+// Negative tests.
+SA(!__is_const(int));
+SA(!__is_const(volatile int));
+SA(!__is_const(ClassType));
+SA(!__is_const(vClassType));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 03/40] libstdc++: Optimize is_const trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
2023-09-15 2:21 ` [PATCH v12 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
2023-09-15 2:21 ` [PATCH v12 02/40] c++: Implement __is_const built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 04/40] c++: Implement __is_volatile built-in trait Ken Matsui
` (37 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_const trait by dispatching to
the new __is_const built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_const): Use __is_const built-in trait.
(is_const_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 677cd934b94..686e38e47c3 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -784,6 +784,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Type properties.
/// is_const
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
+ template<typename _Tp>
+ struct is_const
+ : public __bool_constant<__is_const(_Tp)>
+ { };
+#else
template<typename>
struct is_const
: public false_type { };
@@ -791,6 +797,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_const<_Tp const>
: public true_type { };
+#endif
/// is_volatile
template<typename>
@@ -3218,10 +3225,17 @@ template <typename _Tp>
inline constexpr bool is_compound_v = is_compound<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_member_pointer_v = is_member_pointer<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
+template <typename _Tp>
+ inline constexpr bool is_const_v = __is_const(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_const_v = false;
template <typename _Tp>
inline constexpr bool is_const_v<const _Tp> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_volatile_v = false;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 04/40] c++: Implement __is_volatile built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (2 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 03/40] libstdc++: Optimize is_const trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 05/40] libstdc++: Optimize is_volatile trait performance Ken Matsui
` (36 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_volatile.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_volatile.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_VOLATILE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_volatile.
* g++.dg/ext/is_volatile.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_volatile.C | 19 +++++++++++++++++++
5 files changed, 30 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_volatile.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 567dd35fe0a..f031e022541 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3796,6 +3796,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
+ case CPTK_IS_VOLATILE:
+ inform (loc, " %qT is not a volatile type", t1);
+ break;
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
inform (loc, " %qT is not a reference that binds to a temporary "
"object of type %qT (direct-initialization)", t1, t2);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index a4ebfd9f319..60462cd9874 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -83,6 +83,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 17d6e6728f9..647124265a6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12202,6 +12202,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
+ case CPTK_IS_VOLATILE:
+ return CP_TYPE_VOLATILE_P (type1);
+
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
@@ -12363,6 +12366,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
case CPTK_IS_UNION:
+ case CPTK_IS_VOLATILE:
break;
case CPTK_IS_LAYOUT_COMPATIBLE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index e6e481b13c5..fb03dd20e84 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -131,6 +131,9 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
+#if !__has_builtin (__is_volatile)
+# error "__has_builtin (__is_volatile) failed"
+#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_volatile.C b/gcc/testsuite/g++.dg/ext/is_volatile.C
new file mode 100644
index 00000000000..004e397e5e7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_volatile.C
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+SA(__is_volatile(volatile int));
+SA(__is_volatile(const volatile int));
+SA(__is_volatile(vClassType));
+SA(__is_volatile(cvClassType));
+
+// Negative tests.
+SA(!__is_volatile(int));
+SA(!__is_volatile(const int));
+SA(!__is_volatile(ClassType));
+SA(!__is_volatile(cClassType));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 05/40] libstdc++: Optimize is_volatile trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (3 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 04/40] c++: Implement __is_volatile built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 06/40] c++: Implement __is_array built-in trait Ken Matsui
` (35 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_volatile trait by dispatching
to the new __is_volatile built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_volatile): Use __is_volatile built-in
trait.
(is_volatile_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 686e38e47c3..c01f65df22b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -800,6 +800,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_volatile
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_volatile)
+ template<typename _Tp>
+ struct is_volatile
+ : public __bool_constant<__is_volatile(_Tp)>
+ { };
+#else
template<typename>
struct is_volatile
: public false_type { };
@@ -807,6 +813,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_volatile<_Tp volatile>
: public true_type { };
+#endif
/// is_trivial
template<typename _Tp>
@@ -3236,10 +3243,15 @@ template <typename _Tp>
inline constexpr bool is_const_v<const _Tp> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_volatile)
+template <typename _Tp>
+ inline constexpr bool is_volatile_v = __is_volatile(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_volatile_v = false;
template <typename _Tp>
inline constexpr bool is_volatile_v<volatile _Tp> = true;
+#endif
template <typename _Tp>
inline constexpr bool is_trivial_v = __is_trivial(_Tp);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 06/40] c++: Implement __is_array built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (4 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 05/40] libstdc++: Optimize is_volatile trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 07/40] libstdc++: Optimize is_array trait performance Ken Matsui
` (34 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_array.
* g++.dg/ext/is_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_array.C | 28 ++++++++++++++++++++++++
5 files changed, 39 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index f031e022541..5e30a4a907a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_AGGREGATE:
inform (loc, " %qT is not an aggregate", t1);
break;
+ case CPTK_IS_ARRAY:
+ inform (loc, " %qT is not an array", t1);
+ break;
case CPTK_IS_ASSIGNABLE:
inform (loc, " %qT is not assignable from %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 60462cd9874..c9106242bc8 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 647124265a6..8d5d443d9a9 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12128,6 +12128,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ARRAY:
+ return type_code1 == ARRAY_TYPE;
+
case CPTK_IS_ASSIGNABLE:
return is_xible (MODIFY_EXPR, type1, type2);
@@ -12361,6 +12364,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index fb03dd20e84..645cabe088e 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_array)
+# error "__has_builtin (__is_array) failed"
+#endif
#if !__has_builtin (__is_assignable)
# error "__has_builtin (__is_assignable) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_array.C b/gcc/testsuite/g++.dg/ext/is_array.C
new file mode 100644
index 00000000000..facfed5c7cb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_array.C
@@ -0,0 +1,28 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_array, int[2], true);
+SA_TEST_CATEGORY(__is_array, int[], true);
+SA_TEST_CATEGORY(__is_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_array, int[][3], true);
+SA_TEST_CATEGORY(__is_array, float*[2], true);
+SA_TEST_CATEGORY(__is_array, float*[], true);
+SA_TEST_CATEGORY(__is_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[][3], true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_array, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 07/40] libstdc++: Optimize is_array trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (5 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 06/40] c++: Implement __is_array built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 08/40] c++: Implement __is_unbounded_array built-in trait Ken Matsui
` (33 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_array trait by dispatching to
the new __is_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_array): Use __is_array built-in trait.
(is_array_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index c01f65df22b..4e8165e5af5 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -523,6 +523,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_array
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_array)
+ template<typename _Tp>
+ struct is_array
+ : public __bool_constant<__is_array(_Tp)>
+ { };
+#else
template<typename>
struct is_array
: public false_type { };
@@ -534,6 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_array<_Tp[]>
: public true_type { };
+#endif
template<typename>
struct __is_pointer_helper
@@ -3183,12 +3190,17 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_floating_point_v = is_floating_point<_Tp>::value;
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_array)
+template <typename _Tp>
+ inline constexpr bool is_array_v = __is_array(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_array_v = false;
template <typename _Tp>
inline constexpr bool is_array_v<_Tp[]> = true;
template <typename _Tp, size_t _Num>
inline constexpr bool is_array_v<_Tp[_Num]> = true;
+#endif
template <typename _Tp>
inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 08/40] c++: Implement __is_unbounded_array built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (6 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 07/40] libstdc++: Optimize is_array trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 09/40] libstdc++: Optimize is_unbounded_array trait performance Ken Matsui
` (32 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_unbounded_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_unbounded_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNBOUNDED_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_unbounded_array.
* g++.dg/ext/is_unbounded_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_unbounded_array.C | 37 +++++++++++++++++++
5 files changed, 48 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_unbounded_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 5e30a4a907a..751ac61b25a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3796,6 +3796,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_TRIVIALLY_COPYABLE:
inform (loc, " %qT is not trivially copyable", t1);
break;
+ case CPTK_IS_UNBOUNDED_ARRAY:
+ inform (loc, " %qT is not an unbounded array", t1);
+ break;
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index c9106242bc8..1e67a3d2089 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -83,6 +83,7 @@ DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
+DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 8d5d443d9a9..fd7bdf7a293 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12202,6 +12202,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_TRIVIALLY_COPYABLE:
return trivially_copyable_p (type1);
+ case CPTK_IS_UNBOUNDED_ARRAY:
+ return array_of_unknown_bound_p (type1);
+
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
@@ -12369,6 +12372,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
+ case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
case CPTK_IS_VOLATILE:
break;
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 645cabe088e..90997210c12 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -131,6 +131,9 @@
#if !__has_builtin (__is_trivially_copyable)
# error "__has_builtin (__is_trivially_copyable) failed"
#endif
+#if !__has_builtin (__is_unbounded_array)
+# error "__has_builtin (__is_unbounded_array) failed"
+#endif
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_unbounded_array.C b/gcc/testsuite/g++.dg/ext/is_unbounded_array.C
new file mode 100644
index 00000000000..1307d24f5a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_unbounded_array.C
@@ -0,0 +1,37 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_unbounded_array, int[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, int[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, int(*)[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(*)[], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(&)[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(&)[], false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType, false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass, false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteUnion, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 09/40] libstdc++: Optimize is_unbounded_array trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (7 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 08/40] c++: Implement __is_unbounded_array built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 10/40] c++: Implement __is_bounded_array built-in trait Ken Matsui
` (31 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_unbounded_array trait by
dispatching to the new __is_unbounded_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_unbounded_array_v): Use
__is_unbounded_array built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 4e8165e5af5..cb3d9e238fa 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3541,11 +3541,16 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True for a type that is an array of unknown bound.
/// @ingroup variable_templates
/// @since C++20
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unbounded_array)
+ template<typename _Tp>
+ inline constexpr bool is_unbounded_array_v = __is_unbounded_array(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_unbounded_array_v = false;
template<typename _Tp>
inline constexpr bool is_unbounded_array_v<_Tp[]> = true;
+# endif
/// True for a type that is an array of known bound.
/// @since C++20
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 10/40] c++: Implement __is_bounded_array built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (8 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 09/40] libstdc++: Optimize is_unbounded_array trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 11/40] libstdc++: Optimize is_bounded_array trait performance Ken Matsui
` (30 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_bounded_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_bounded_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_BOUNDED_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_bounded_array.
* g++.dg/ext/is_bounded_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_bounded_array.C | 38 +++++++++++++++++++++
5 files changed, 49 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_bounded_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 751ac61b25a..d09252a56b6 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3723,6 +3723,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
+ case CPTK_IS_BOUNDED_ARRAY:
+ inform (loc, " %qT is not a bounded array", t1);
+ break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 1e67a3d2089..b6146c010f6 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
+DEFTRAIT_EXPR (IS_BOUNDED_ARRAY, "__is_bounded_array", 1)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index fd7bdf7a293..605cf03c18d 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12139,6 +12139,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
|| DERIVED_FROM_P (type1, type2)));
+ case CPTK_IS_BOUNDED_ARRAY:
+ return type_code1 == ARRAY_TYPE && TYPE_DOMAIN (type1);
+
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
@@ -12368,6 +12371,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;
case CPTK_IS_ARRAY:
+ case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 90997210c12..4142da518b1 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -65,6 +65,9 @@
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
+#if !__has_builtin (__is_bounded_array)
+# error "__has_builtin (__is_bounded_array) failed"
+#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_bounded_array.C b/gcc/testsuite/g++.dg/ext/is_bounded_array.C
new file mode 100644
index 00000000000..346790eba12
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_bounded_array.C
@@ -0,0 +1,38 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CONST(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_bounded_array, int[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, int[], false);
+SA_TEST_CATEGORY(__is_bounded_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, int[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, float*[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, float*[], false);
+SA_TEST_CATEGORY(__is_bounded_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, float*[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[], false);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(*)[2], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(*)[], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(&)[2], false);
+SA_TEST_CONST(__is_bounded_array, int(&)[], false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_bounded_array, ClassType, false);
+SA_TEST_CONST(__is_bounded_array, void(), false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 11/40] libstdc++: Optimize is_bounded_array trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (9 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 10/40] c++: Implement __is_bounded_array built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 12/40] c++: Implement __is_scoped_enum built-in trait Ken Matsui
` (29 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_bounded_array trait by
dispatching to the new __is_bounded_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_bounded_array_v): Use __is_bounded_array
built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index cb3d9e238fa..d306073a797 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3532,11 +3532,16 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True for a type that is an array of known bound.
/// @ingroup variable_templates
/// @since C++20
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_bounded_array)
+ template<typename _Tp>
+ inline constexpr bool is_bounded_array_v = __is_bounded_array(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_bounded_array_v = false;
template<typename _Tp, size_t _Size>
inline constexpr bool is_bounded_array_v<_Tp[_Size]> = true;
+# endif
/// True for a type that is an array of unknown bound.
/// @ingroup variable_templates
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 12/40] c++: Implement __is_scoped_enum built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (10 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 11/40] libstdc++: Optimize is_bounded_array trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 13/40] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui
` (28 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_scoped_enum.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_scoped_enum.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCOPED_ENUM.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_scoped_enum.
* g++.dg/ext/is_scoped_enum.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +
gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++++++++++++++++++++
5 files changed, 78 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d09252a56b6..1c0b2e0f178 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3781,6 +3781,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SCOPED_ENUM:
+ inform (loc, " %qT is not a scoped enum", t1);
+ break;
case CPTK_IS_STD_LAYOUT:
inform (loc, " %qT is not an standard layout type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index b6146c010f6..047307c95ce 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -79,6 +79,7 @@ DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertib
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 605cf03c18d..c971c34cf6f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12190,6 +12190,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SCOPED_ENUM:
+ return SCOPED_ENUM_P (type1);
+
case CPTK_IS_STD_LAYOUT:
return std_layout_type_p (type1);
@@ -12376,6 +12379,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
+ case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
case CPTK_IS_VOLATILE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4142da518b1..ba97beea3c3 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -119,6 +119,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_scoped_enum)
+# error "__has_builtin (__is_scoped_enum) failed"
+#endif
#if !__has_builtin (__is_standard_layout)
# error "__has_builtin (__is_standard_layout) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_scoped_enum.C b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C
new file mode 100644
index 00000000000..a563b6ee67d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C
@@ -0,0 +1,67 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT);
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+enum class E { e1, e2 };
+SA_TEST_CATEGORY(__is_scoped_enum, E, true);
+enum class Ec : char { e1, e2 };
+SA_TEST_CATEGORY(__is_scoped_enum, Ec, true);
+
+// negative tests
+enum U { u1, u2 };
+SA_TEST_CATEGORY(__is_scoped_enum, U, false);
+enum F : int { f1, f2 };
+SA_TEST_CATEGORY(__is_scoped_enum, F, false);
+struct S;
+SA_TEST_CATEGORY(__is_scoped_enum, S, false);
+struct S { };
+SA_TEST_CATEGORY(__is_scoped_enum, S, false);
+
+SA_TEST_CATEGORY(__is_scoped_enum, int, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[2], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[][2], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[2][3], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int*, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int&, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int*&, false);
+SA_TEST_FN(__is_scoped_enum, int(), false);
+SA_TEST_FN(__is_scoped_enum, int(*)(), false);
+SA_TEST_FN(__is_scoped_enum, int(&)(), false);
+
+enum opaque_unscoped : short;
+enum class opaque_scoped;
+enum class opaque_scoped_with_base : long;
+
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_unscoped, false);
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped, true);
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped_with_base, true);
+
+enum unscoped {
+ u_is_scoped = __is_scoped_enum(unscoped),
+};
+SA( ! unscoped::u_is_scoped );
+
+enum unscoped_fixed : char {
+ uf_is_scoped = __is_scoped_enum(unscoped_fixed),
+};
+SA( ! unscoped_fixed::uf_is_scoped );
+
+enum class scoped {
+ is_scoped = __is_scoped_enum(scoped),
+};
+SA( (bool) scoped::is_scoped );
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 13/40] libstdc++: Optimize is_scoped_enum trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (11 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 12/40] c++: Implement __is_scoped_enum built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 14/40] c++: Implement __is_member_pointer built-in trait Ken Matsui
` (27 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_scoped_enum trait
by dispatching to the new __is_scoped_enum built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_scoped_enum): Use
__is_scoped_enum built-in trait.
(is_scoped_enum_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index d306073a797..7fd29d8d9f2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3633,6 +3633,12 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True if the type is a scoped enumeration type.
/// @since C++23
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum)
+ template<typename _Tp>
+ struct is_scoped_enum
+ : bool_constant<__is_scoped_enum(_Tp)>
+ { };
+# else
template<typename _Tp>
struct is_scoped_enum
: false_type
@@ -3644,11 +3650,17 @@ template<typename _Ret, typename _Fn, typename... _Args>
struct is_scoped_enum<_Tp>
: bool_constant<!requires(_Tp __t, void(*__f)(int)) { __f(__t); }>
{ };
+# endif
/// @ingroup variable_templates
/// @since C++23
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum)
+ template<typename _Tp>
+ inline constexpr bool is_scoped_enum_v = __is_scoped_enum(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<_Tp>::value;
+# endif
#endif
#ifdef __cpp_lib_reference_from_temporary // C++ >= 23 && ref_{converts,constructs}_from_temp
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 14/40] c++: Implement __is_member_pointer built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (12 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 13/40] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 15/40] libstdc++: Optimize is_member_pointer trait performance Ken Matsui
` (26 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_pointer.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_MEMBER_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_member_pointer.
* g++.dg/ext/is_member_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_member_pointer.C | 30 ++++++++++++++++++++
5 files changed, 41 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 1c0b2e0f178..f0d3f89464c 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3756,6 +3756,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_POINTER:
+ inform (loc, " %qT is not a member pointer", t1);
+ break;
case CPTK_IS_NOTHROW_ASSIGNABLE:
inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 047307c95ce..7fed3483221 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -72,6 +72,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index c971c34cf6f..7091e581ac7 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12169,6 +12169,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_POINTER:
+ return TYPE_PTRMEM_P (type1);
+
case CPTK_IS_NOTHROW_ASSIGNABLE:
return is_nothrow_xible (MODIFY_EXPR, type1, type2);
@@ -12378,6 +12381,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index ba97beea3c3..994873f14e9 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -95,6 +95,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_pointer)
+# error "__has_builtin (__is_member_pointer) failed"
+#endif
#if !__has_builtin (__is_nothrow_assignable)
# error "__has_builtin (__is_nothrow_assignable) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_pointer.C
new file mode 100644
index 00000000000..7ee2e3ab90c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_pointer.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_NON_VOLATILE(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_member_pointer, int (ClassType::*), true);
+SA_TEST_CATEGORY(__is_member_pointer, ClassType (ClassType::*), true);
+
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(int), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(int) const, true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(float, ...), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, ClassType (ClassType::*)(ClassType), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer,
+ float (ClassType::*)(int, float, int[], int&), true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 15/40] libstdc++: Optimize is_member_pointer trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (13 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 14/40] c++: Implement __is_member_pointer built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
` (25 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_pointer trait
by dispatching to the new __is_member_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_pointer): Use __is_member_pointer
built-in trait.
(is_member_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 7fd29d8d9f2..d7f89cf7c06 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -716,6 +716,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_compound
: public __not_<is_fundamental<_Tp>>::type { };
+ /// is_member_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
+ template<typename _Tp>
+ struct is_member_pointer
+ : public __bool_constant<__is_member_pointer(_Tp)>
+ { };
+#else
/// @cond undocumented
template<typename _Tp>
struct __is_member_pointer_helper
@@ -726,11 +733,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public true_type { };
/// @endcond
- /// is_member_pointer
template<typename _Tp>
struct is_member_pointer
: public __is_member_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
template<typename, typename>
struct is_same;
@@ -3242,8 +3249,14 @@ template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_compound_v = is_compound<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_pointer_v = __is_member_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_pointer_v = is_member_pointer<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (14 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 15/40] libstdc++: Optimize is_member_pointer trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly Ken Matsui
` (24 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
Now that RID_MAX has reached 255, we need to update the bit sizes of every
use of the enum rid from 8 to 16 to support more keywords.
For struct token_indent_info, the 8-bit increase does not change the overall
struct size because the 8-bit just consumes 1 byte from 2 bytes of external
fragmentation. Since reordering the fields just changes 1 byte of internal
fragmentation to 1 byte of external fragmentation, I keep the original field
order.
For struct c_token, the 8-bit expansion increased the overall struct size from
24 bytes to 32 bytes. The original struct takes 4 bytes of internal
fragmentation (after the location field) and 3 bytes of external
fragmentation. Keeping the original order with the 8-bit expansion gives
7 bytes of internal fragmentation (3 bytes after the pragma_kind field + 4
bytes after the location field) and 7 bytes of external fragmentation. Since
the original field order was not optimal, reordering the fields results in the
same overall size as the original one.
For struct cp_token, reordering the fields only minimizes internal
fragmentation and does not minimize the overall struct size. I keep the
original field order. The original struct size was 16 bytes with 3 bits of
internal fragmentation. With this 8-bit update, the overall size would be
24 bytes. Since there is no external fragmentation and 7 bytes + 3 bits of
internal fragmentation, reordering the fields does not minimize the overall
size. I keep the orignal field order.
Suppose a pointer takes 8 bytes and int takes 4 bytes. Then, struct
ht_identifier takes 16 bytes, and union _cpp_hashnode_value takes 8 bytes.
For struct cpp_hashnode, the 8-bit increase consumes 1 more byte, resulting in
33 bytes except for paddings. The original overall size before the 8-bit
increase was 32 bytes. However, due to fragmentation, the overall struct size
would be 40 bytes. Since there is no external fragmentation and 3 bytes + 5
bits of internal fragmentation, reordering the fields does not minimize the
overall size. I keep the original field order.
gcc/c-family/ChangeLog:
* c-indentation.h (struct token_indent_info): Make keyword 16 bits.
gcc/c/ChangeLog:
* c-parser.cc (c_parse_init): Handle RID_MAX not to exceed the max
value of 16 bits.
* c-parser.h (struct c_token): Make keyword 16 bits. Reorder the
fields to minimize memory fragmentation.
gcc/cp/ChangeLog:
* parser.h (struct cp_token): Make keyword 16 bits.
(struct cp_lexer): Make saved_keyword 16 bits.
libcpp/ChangeLog:
* include/cpplib.h (struct cpp_hashnode): Make rid_code 16 bits.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/c-family/c-indentation.h | 2 +-
gcc/c/c-parser.cc | 6 +++---
gcc/c/c-parser.h | 14 +++++++-------
gcc/cp/parser.h | 8 +++++---
libcpp/include/cpplib.h | 7 +++++--
5 files changed, 21 insertions(+), 16 deletions(-)
diff --git a/gcc/c-family/c-indentation.h b/gcc/c-family/c-indentation.h
index c0e07bf49f1..6d2b88f01a3 100644
--- a/gcc/c-family/c-indentation.h
+++ b/gcc/c-family/c-indentation.h
@@ -26,7 +26,7 @@ struct token_indent_info
{
location_t location;
ENUM_BITFIELD (cpp_ttype) type : 8;
- ENUM_BITFIELD (rid) keyword : 8;
+ ENUM_BITFIELD (rid) keyword : 16;
};
/* Extract token information from TOKEN, which ought to either be a
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index b9a1b75ca43..2086f253923 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -115,9 +115,9 @@ c_parse_init (void)
tree id;
int mask = 0;
- /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
- the c_token structure. */
- gcc_assert (RID_MAX <= 255);
+ /* Make sure RID_MAX hasn't grown past the 16 bits used to hold the keyword
+ in the c_token structure. */
+ gcc_assert (RID_MAX <= 65535);
mask |= D_CXXONLY;
if (!flag_isoc99)
diff --git a/gcc/c/c-parser.h b/gcc/c/c-parser.h
index 545f0f4d9eb..6a9bd22a793 100644
--- a/gcc/c/c-parser.h
+++ b/gcc/c/c-parser.h
@@ -51,21 +51,21 @@ enum c_id_kind {
/* A single C token after string literal concatenation and conversion
of preprocessing tokens to tokens. */
struct GTY (()) c_token {
+ /* The value associated with this token, if any. */
+ tree value;
+ /* The location at which this token was found. */
+ location_t location;
+ /* If this token is a keyword, this value indicates which keyword.
+ Otherwise, this value is RID_MAX. */
+ ENUM_BITFIELD (rid) keyword : 16;
/* The kind of token. */
ENUM_BITFIELD (cpp_ttype) type : 8;
/* If this token is a CPP_NAME, this value indicates whether also
declared as some kind of type. Otherwise, it is C_ID_NONE. */
ENUM_BITFIELD (c_id_kind) id_kind : 8;
- /* If this token is a keyword, this value indicates which keyword.
- Otherwise, this value is RID_MAX. */
- ENUM_BITFIELD (rid) keyword : 8;
/* If this token is a CPP_PRAGMA, this indicates the pragma that
was seen. Otherwise it is PRAGMA_NONE. */
ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
- /* The location at which this token was found. */
- location_t location;
- /* The value associated with this token, if any. */
- tree value;
/* Token flags. */
unsigned char flags;
diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
index 6cbb9a8e031..7aa251d11b1 100644
--- a/gcc/cp/parser.h
+++ b/gcc/cp/parser.h
@@ -44,7 +44,7 @@ struct GTY (()) cp_token {
enum cpp_ttype type : 8;
/* If this token is a keyword, this value indicates which keyword.
Otherwise, this value is RID_MAX. */
- enum rid keyword : 8;
+ enum rid keyword : 16;
/* Token flags. */
unsigned char flags;
/* True if this token is from a context where it is implicitly extern "C" */
@@ -59,7 +59,9 @@ struct GTY (()) cp_token {
bool purged_p : 1;
bool tree_check_p : 1;
bool main_source_p : 1;
- /* 3 unused bits. */
+ /* These booleans use 5 bits within 1 byte, resulting in 3 unused bits.
+ Since there would be 3 bytes of internal fragmentation to the location
+ field, the total unused bits would be 27 (= 3 + 24). */
/* The location at which this token was found. */
location_t location;
@@ -102,7 +104,7 @@ struct GTY (()) cp_lexer {
/* Saved pieces of end token we replaced with the eof token. */
enum cpp_ttype saved_type : 8;
- enum rid saved_keyword : 8;
+ enum rid saved_keyword : 16;
/* The next lexer in a linked list of lexers. */
struct cp_lexer *next;
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index fcdaf082b09..389f8cfb1f6 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -988,11 +988,14 @@ struct GTY(()) cpp_hashnode {
unsigned int directive_index : 7; /* If is_directive,
then index into directive table.
Otherwise, a NODE_OPERATOR. */
- unsigned int rid_code : 8; /* Rid code - for front ends. */
+ unsigned int rid_code : 16; /* Rid code - for front ends. */
unsigned int flags : 9; /* CPP flags. */
ENUM_BITFIELD(node_type) type : 2; /* CPP node type. */
- /* 5 bits spare. */
+ /* Bitfields use 35 bits (= 1 + 7 + 16 + 9 + 2). The exceeded 3 bits in
+ terms of bytes leave 5 unused bits within 1 byte. Since there would be
+ 3 bytes of internal fragmentation to the deferred field, the total unused
+ bits would be 29 (= 5 + 24). */
/* The deferred cookie is applicable to NT_USER_MACRO or NT_VOID.
The latter for when a macro had a prevailing undef.
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (15 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 18/40] c++: Implement __is_member_function_pointer built-in trait Ken Matsui
` (23 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui, Andrew Pinski
This patch fixes incorrect handling for the new 16-bit rid code. Unsigned
char was previously used for the 8-bit rid code, but unsigned short is now
required.
gcc/c-family/ChangeLog:
* c-common.h (C_SET_RID_CODE): Use unsigned short instead of
unsigned char.
Ref: Initial discussion: https://gcc.gnu.org/pipermail/gcc/2023-September/242460.html
Code provided by Andrew: https://gcc.gnu.org/pipermail/gcc/2023-September/242461.html
Co-authored-by: Andrew Pinski <pinskia@gmail.com>
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/c-family/c-common.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 1fdba7ef3ea..73bc23fa49f 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -382,7 +382,7 @@ enum c_tree_index
#define C_RID_CODE(id) \
((enum rid) (((struct c_common_identifier *) (id))->node.rid_code))
#define C_SET_RID_CODE(id, code) \
- (((struct c_common_identifier *) (id))->node.rid_code = (unsigned char) code)
+ (((struct c_common_identifier *) (id))->node.rid_code = (unsigned short) code)
/* Identifier part common to the C front ends. Inherits from
tree_identifier, despite appearances. */
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 18/40] c++: Implement __is_member_function_pointer built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (16 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 19/40] libstdc++: Optimize is_member_function_pointer trait performance Ken Matsui
` (22 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_function_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_function_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_FUNCTION_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of
__is_member_function_pointer.
* g++.dg/ext/is_member_function_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
.../g++.dg/ext/is_member_function_pointer.C | 31 +++++++++++++++++++
5 files changed, 42 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index f0d3f89464c..d0464dd4f6a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3756,6 +3756,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ inform (loc, " %qT is not a member function pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 7fed3483221..6ebe3984d17 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -72,6 +72,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7091e581ac7..93e166923b8 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12169,6 +12169,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ return TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12381,6 +12384,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 994873f14e9..0dfe957474b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -95,6 +95,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_function_pointer)
+# error "__has_builtin (__is_member_function_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
new file mode 100644
index 00000000000..555123e8f07
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT);
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int), true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int) const, true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (float, ...), true);
+SA_TEST_FN(__is_member_function_pointer, ClassType (ClassType::*) (ClassType), true);
+SA_TEST_FN(__is_member_function_pointer, float (ClassType::*) (int, float, int[], int&), true);
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_member_function_pointer, int (ClassType::*), false);
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType (ClassType::*), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 19/40] libstdc++: Optimize is_member_function_pointer trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (17 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 18/40] c++: Implement __is_member_function_pointer built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 20/40] c++: Implement __is_member_object_pointer built-in trait Ken Matsui
` (21 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_function_pointer trait
by dispatching to the new __is_member_function_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_function_pointer): Use
__is_member_function_pointer built-in trait.
(is_member_function_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index d7f89cf7c06..e1b10240dc2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -588,6 +588,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __is_member_object_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
+ /// is_member_function_pointer
+ template<typename _Tp>
+ struct is_member_function_pointer
+ : public __bool_constant<__is_member_function_pointer(_Tp)>
+ { };
+#else
template<typename>
struct __is_member_function_pointer_helper
: public false_type { };
@@ -601,6 +608,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_member_function_pointer
: public __is_member_function_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
/// is_enum
template<typename _Tp>
@@ -3222,9 +3230,17 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_member_object_pointer_v =
is_member_object_pointer<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_function_pointer_v =
+ __is_member_function_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_function_pointer_v =
is_member_function_pointer<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_enum_v = __is_enum(_Tp);
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 20/40] c++: Implement __is_member_object_pointer built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (18 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 19/40] libstdc++: Optimize is_member_function_pointer trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 21/40] libstdc++: Optimize is_member_object_pointer trait performance Ken Matsui
` (20 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_object_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_object_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_OBJECT_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of
__is_member_object_pointer.
* g++.dg/ext/is_member_object_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
.../g++.dg/ext/is_member_object_pointer.C | 30 +++++++++++++++++++
5 files changed, 41 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d0464dd4f6a..98b1f004a68 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3759,6 +3759,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
inform (loc, " %qT is not a member function pointer", t1);
break;
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
+ inform (loc, " %qT is not a member object pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 6ebe3984d17..47649150ab5 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -73,6 +73,7 @@ DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
+DEFTRAIT_EXPR (IS_MEMBER_OBJECT_POINTER, "__is_member_object_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 93e166923b8..95b25c1348b 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12172,6 +12172,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
return TYPE_PTRMEMFUNC_P (type1);
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
+ return TYPE_PTRMEM_P (type1) && !TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12385,6 +12388,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 0dfe957474b..8d9cdc528cd 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -98,6 +98,9 @@
#if !__has_builtin (__is_member_function_pointer)
# error "__has_builtin (__is_member_function_pointer) failed"
#endif
+#if !__has_builtin (__is_member_object_pointer)
+# error "__has_builtin (__is_member_object_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
new file mode 100644
index 00000000000..835e48c8f8e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_NON_VOLATILE(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_CATEGORY(__is_member_object_pointer, int (ClassType::*), true);
+SA_TEST_CATEGORY(__is_member_object_pointer, ClassType (ClassType::*), true);
+
+// Negative tests.
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, int (ClassType::*) (int), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, int (ClassType::*) (float, ...), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, ClassType (ClassType::*) (ClassType), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, float (ClassType::*) (int, float, int[], int&), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_object_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 21/40] libstdc++: Optimize is_member_object_pointer trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (19 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 20/40] c++: Implement __is_member_object_pointer built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 22/40] c++: Implement __is_reference built-in trait Ken Matsui
` (19 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_object_pointer trait
by dispatching to the new __is_member_object_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_object_pointer): Use
__is_member_object_pointer built-in trait.
(is_member_object_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index e1b10240dc2..792213ebfe8 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -574,6 +574,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_rvalue_reference<_Tp&&>
: public true_type { };
+ /// is_member_object_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_object_pointer)
+ template<typename _Tp>
+ struct is_member_object_pointer
+ : public __bool_constant<__is_member_object_pointer(_Tp)>
+ { };
+#else
template<typename>
struct __is_member_object_pointer_helper
: public false_type { };
@@ -582,11 +589,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __is_member_object_pointer_helper<_Tp _Cp::*>
: public __not_<is_function<_Tp>>::type { };
- /// is_member_object_pointer
+
template<typename _Tp>
struct is_member_object_pointer
: public __is_member_object_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
/// is_member_function_pointer
@@ -3227,9 +3235,16 @@ template <typename _Tp>
inline constexpr bool is_rvalue_reference_v = false;
template <typename _Tp>
inline constexpr bool is_rvalue_reference_v<_Tp&&> = true;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_object_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_object_pointer_v =
+ __is_member_object_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_object_pointer_v =
is_member_object_pointer<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 22/40] c++: Implement __is_reference built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (20 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 21/40] libstdc++: Optimize is_member_object_pointer trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 23/40] libstdc++: Optimize is_reference trait performance Ken Matsui
` (18 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_reference.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_reference.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_REFERENCE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_reference.
* g++.dg/ext/is_reference.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_reference.C | 34 ++++++++++++++++++++++++
5 files changed, 45 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_reference.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 98b1f004a68..5cdb59d174e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3787,6 +3787,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
+ case CPTK_IS_REFERENCE:
+ inform (loc, " %qT is not a reference", t1);
+ break;
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 47649150ab5..ac9fa026b4e 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -81,6 +81,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
+DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 95b25c1348b..bee27b25974 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12196,6 +12196,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
+ case CPTK_IS_REFERENCE:
+ return type_code1 == REFERENCE_TYPE;
+
case CPTK_IS_SAME:
return same_type_p (type1, type2);
@@ -12390,6 +12393,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
+ case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 8d9cdc528cd..e112d317657 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -122,6 +122,9 @@
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif
+#if !__has_builtin (__is_reference)
+# error "__has_builtin (__is_reference) failed"
+#endif
#if !__has_builtin (__is_same)
# error "__has_builtin (__is_same) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_reference.C b/gcc/testsuite/g++.dg/ext/is_reference.C
new file mode 100644
index 00000000000..b5ce4db7afd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_reference.C
@@ -0,0 +1,34 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_CATEGORY(__is_reference, int&, true);
+SA_TEST_CATEGORY(__is_reference, ClassType&, true);
+SA(__is_reference(int(&)(int)));
+SA_TEST_CATEGORY(__is_reference, int&&, true);
+SA_TEST_CATEGORY(__is_reference, ClassType&&, true);
+SA(__is_reference(int(&&)(int)));
+SA_TEST_CATEGORY(__is_reference, IncompleteClass&, true);
+
+// Negative tests
+SA_TEST_CATEGORY(__is_reference, void, false);
+SA_TEST_CATEGORY(__is_reference, int*, false);
+SA_TEST_CATEGORY(__is_reference, int[3], false);
+SA(!__is_reference(int(int)));
+SA(!__is_reference(int(*const)(int)));
+SA(!__is_reference(int(*volatile)(int)));
+SA(!__is_reference(int(*const volatile)(int)));
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_reference, ClassType, false);
+SA_TEST_CATEGORY(__is_reference, IncompleteClass, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 23/40] libstdc++: Optimize is_reference trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (21 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 22/40] c++: Implement __is_reference built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 24/40] c++: Implement __is_function built-in trait Ken Matsui
` (17 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_reference trait by dispatching
to the new __is_reference built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_reference): Use __is_reference built-in
trait.
(is_reference_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 792213ebfe8..36ad9814047 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -682,6 +682,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Composite type categories.
/// is_reference
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+ template<typename _Tp>
+ struct is_reference
+ : public __bool_constant<__is_reference(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_reference
: public false_type
@@ -696,6 +702,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_reference<_Tp&&>
: public true_type
{ };
+#endif
/// is_arithmetic
template<typename _Tp>
@@ -3264,12 +3271,19 @@ template <typename _Tp>
inline constexpr bool is_class_v = __is_class(_Tp);
template <typename _Tp>
inline constexpr bool is_function_v = is_function<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+template <typename _Tp>
+ inline constexpr bool is_reference_v = __is_reference(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_reference_v = false;
template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&> = true;
template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&&> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 24/40] c++: Implement __is_function built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (22 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 23/40] libstdc++: Optimize is_reference trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 25/40] libstdc++: Optimize is_function trait performance Ken Matsui
` (16 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_function.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_function.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_FUNCTION.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_function.
* g++.dg/ext/is_function.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_function.C | 58 ++++++++++++++++++++++++
5 files changed, 69 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_function.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 5cdb59d174e..99a7e7247ce 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3750,6 +3750,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_FINAL:
inform (loc, " %qT is not a final class", t1);
break;
+ case CPTK_IS_FUNCTION:
+ inform (loc, " %qT is not a function", t1);
+ break;
case CPTK_IS_LAYOUT_COMPATIBLE:
inform (loc, " %qT is not layout compatible with %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index ac9fa026b4e..3bb33a3d5c0 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -70,6 +70,7 @@ DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
+DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index bee27b25974..a502c13ecc1 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12163,6 +12163,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_FINAL:
return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
+ case CPTK_IS_FUNCTION:
+ return type_code1 == FUNCTION_TYPE;
+
case CPTK_IS_LAYOUT_COMPATIBLE:
return layout_compatible_type_p (type1, type2);
@@ -12390,6 +12393,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_FUNCTION:
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index e112d317657..4d3947572a4 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -89,6 +89,9 @@
#if !__has_builtin (__is_final)
# error "__has_builtin (__is_final) failed"
#endif
+#if !__has_builtin (__is_function)
+# error "__has_builtin (__is_function) failed"
+#endif
#if !__has_builtin (__is_layout_compatible)
# error "__has_builtin (__is_layout_compatible) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_function.C b/gcc/testsuite/g++.dg/ext/is_function.C
new file mode 100644
index 00000000000..2e1594b12ad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_function.C
@@ -0,0 +1,58 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+struct A
+{ void fn(); };
+
+template<typename>
+struct AHolder { };
+
+template<class T, class U>
+struct AHolder<U T::*>
+{ using type = U; };
+
+// Positive tests.
+SA(__is_function(int (int)));
+SA(__is_function(ClassType (ClassType)));
+SA(__is_function(float (int, float, int[], int&)));
+SA(__is_function(int (int, ...)));
+SA(__is_function(bool (ClassType) const));
+SA(__is_function(AHolder<decltype(&A::fn)>::type));
+
+void fn();
+SA(__is_function(decltype(fn)));
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_function, int, false);
+SA_TEST_CATEGORY(__is_function, int*, false);
+SA_TEST_CATEGORY(__is_function, int&, false);
+SA_TEST_CATEGORY(__is_function, void, false);
+SA_TEST_CATEGORY(__is_function, void*, false);
+SA_TEST_CATEGORY(__is_function, void**, false);
+SA_TEST_CATEGORY(__is_function, std::nullptr_t, false);
+
+SA_TEST_CATEGORY(__is_function, AbstractClass, false);
+SA(!__is_function(int(&)(int)));
+SA(!__is_function(int(*)(int)));
+
+SA_TEST_CATEGORY(__is_function, A, false);
+SA_TEST_CATEGORY(__is_function, decltype(&A::fn), false);
+
+struct FnCallOverload
+{ void operator()(); };
+SA_TEST_CATEGORY(__is_function, FnCallOverload, false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_function, ClassType, false);
+SA_TEST_CATEGORY(__is_function, IncompleteClass, false);
+SA_TEST_CATEGORY(__is_function, IncompleteUnion, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 25/40] libstdc++: Optimize is_function trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (23 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 24/40] c++: Implement __is_function built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 26/40] libstdc++: Optimize is_object " Ken Matsui
` (15 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_function trait by dispatching
to the new __is_function built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_function): Use __is_function built-in
trait.
(is_function_v): Likewise. Optimize its implementation.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 36ad9814047..bd57488824b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -637,6 +637,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_function
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
+ template<typename _Tp>
+ struct is_function
+ : public __bool_constant<__is_function(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_function
: public __bool_constant<!is_const<const _Tp>::value> { };
@@ -648,6 +654,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_function<_Tp&&>
: public false_type { };
+#endif
#ifdef __cpp_lib_is_null_pointer // C++ >= 11
/// is_null_pointer (LWG 2247).
@@ -3269,8 +3276,18 @@ template <typename _Tp>
inline constexpr bool is_union_v = __is_union(_Tp);
template <typename _Tp>
inline constexpr bool is_class_v = __is_class(_Tp);
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
template <typename _Tp>
- inline constexpr bool is_function_v = is_function<_Tp>::value;
+ inline constexpr bool is_function_v = __is_function(_Tp);
+#else
+template <typename _Tp>
+ inline constexpr bool is_function_v = !is_const_v<const _Tp>;
+template <typename _Tp>
+ inline constexpr bool is_function_v<_Tp&> = false;
+template <typename _Tp>
+ inline constexpr bool is_function_v<_Tp&&> = false;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 26/40] libstdc++: Optimize is_object trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (24 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 25/40] libstdc++: Optimize is_function trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 27/40] c++: Implement __remove_pointer built-in trait Ken Matsui
` (14 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_object trait by dispatching to
the new __is_function and __is_reference built-in traits.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_object): Use __is_function and
__is_reference built-in traits.
(is_object_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index bd57488824b..674d398c075 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -725,11 +725,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_object
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
+ && _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+ template<typename _Tp>
+ struct is_object
+ : public __bool_constant<!(__is_function(_Tp) || __is_reference(_Tp)
+ || is_void<_Tp>::value)>
+ { };
+#else
template<typename _Tp>
struct is_object
: public __not_<__or_<is_function<_Tp>, is_reference<_Tp>,
is_void<_Tp>>>::type
{ };
+#endif
template<typename>
struct is_member_pointer;
@@ -3305,8 +3314,17 @@ template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
+ && _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+template <typename _Tp>
+ inline constexpr bool is_object_v
+ = !(__is_function(_Tp) || __is_reference(_Tp) || is_void<_Tp>::value);
+#else
template <typename _Tp>
inline constexpr bool is_object_v = is_object<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 27/40] c++: Implement __remove_pointer built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (25 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 26/40] libstdc++: Optimize is_object " Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 28/40] libstdc++: Optimize remove_pointer trait performance Ken Matsui
` (13 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::remove_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __remove_pointer.
* semantics.cc (finish_trait_type): Handle CPTK_REMOVE_POINTER.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __remove_pointer.
* g++.dg/ext/remove_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 5 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 +++++++++++++++++++++++
4 files changed, 60 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 3bb33a3d5c0..07cd14b6e85 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -97,6 +97,7 @@ DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_tempo
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
+DEFTRAIT_TYPE (REMOVE_POINTER, "__remove_pointer", 1)
DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index a502c13ecc1..10656017bbc 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12473,6 +12473,11 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
type1 = TREE_TYPE (type1);
return cv_unqualified (type1);
+ case CPTK_REMOVE_POINTER:
+ if (TYPE_PTR_P (type1))
+ type1 = TREE_TYPE (type1);
+ return type1;
+
case CPTK_REMOVE_REFERENCE:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4d3947572a4..bcab0599d1a 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -173,6 +173,9 @@
#if !__has_builtin (__remove_cvref)
# error "__has_builtin (__remove_cvref) failed"
#endif
+#if !__has_builtin (__remove_pointer)
+# error "__has_builtin (__remove_pointer) failed"
+#endif
#if !__has_builtin (__remove_reference)
# error "__has_builtin (__remove_reference) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/remove_pointer.C b/gcc/testsuite/g++.dg/ext/remove_pointer.C
new file mode 100644
index 00000000000..7b13db93950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/remove_pointer.C
@@ -0,0 +1,51 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_pointer(int), int));
+SA(__is_same(__remove_pointer(int*), int));
+SA(__is_same(__remove_pointer(int**), int*));
+
+SA(__is_same(__remove_pointer(const int*), const int));
+SA(__is_same(__remove_pointer(const int**), const int*));
+SA(__is_same(__remove_pointer(int* const), int));
+SA(__is_same(__remove_pointer(int** const), int*));
+SA(__is_same(__remove_pointer(int* const* const), int* const));
+
+SA(__is_same(__remove_pointer(volatile int*), volatile int));
+SA(__is_same(__remove_pointer(volatile int**), volatile int*));
+SA(__is_same(__remove_pointer(int* volatile), int));
+SA(__is_same(__remove_pointer(int** volatile), int*));
+SA(__is_same(__remove_pointer(int* volatile* volatile), int* volatile));
+
+SA(__is_same(__remove_pointer(const volatile int*), const volatile int));
+SA(__is_same(__remove_pointer(const volatile int**), const volatile int*));
+SA(__is_same(__remove_pointer(const int* volatile), const int));
+SA(__is_same(__remove_pointer(volatile int* const), volatile int));
+SA(__is_same(__remove_pointer(int* const volatile), int));
+SA(__is_same(__remove_pointer(const int** volatile), const int*));
+SA(__is_same(__remove_pointer(volatile int** const), volatile int*));
+SA(__is_same(__remove_pointer(int** const volatile), int*));
+SA(__is_same(__remove_pointer(int* const* const volatile), int* const));
+SA(__is_same(__remove_pointer(int* volatile* const volatile), int* volatile));
+SA(__is_same(__remove_pointer(int* const volatile* const volatile), int* const volatile));
+
+SA(__is_same(__remove_pointer(int&), int&));
+SA(__is_same(__remove_pointer(const int&), const int&));
+SA(__is_same(__remove_pointer(volatile int&), volatile int&));
+SA(__is_same(__remove_pointer(const volatile int&), const volatile int&));
+
+SA(__is_same(__remove_pointer(int&&), int&&));
+SA(__is_same(__remove_pointer(const int&&), const int&&));
+SA(__is_same(__remove_pointer(volatile int&&), volatile int&&));
+SA(__is_same(__remove_pointer(const volatile int&&), const volatile int&&));
+
+SA(__is_same(__remove_pointer(int[3]), int[3]));
+SA(__is_same(__remove_pointer(const int[3]), const int[3]));
+SA(__is_same(__remove_pointer(volatile int[3]), volatile int[3]));
+SA(__is_same(__remove_pointer(const volatile int[3]), const volatile int[3]));
+
+SA(__is_same(__remove_pointer(int(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*volatile)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const volatile)(int)), int(int)));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 28/40] libstdc++: Optimize remove_pointer trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (26 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 27/40] c++: Implement __remove_pointer built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 29/40] c++, libstdc++: Implement __is_pointer built-in trait Ken Matsui
` (12 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the remove_pointer trait by
dispatching to the new remove_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (remove_pointer): Use __remove_pointer
built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 674d398c075..9c56d15c0b7 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -2105,6 +2105,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Pointer modifications.
+ /// remove_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__remove_pointer)
+ template<typename _Tp>
+ struct remove_pointer
+ { using type = __remove_pointer(_Tp); };
+#else
template<typename _Tp, typename>
struct __remove_pointer_helper
{ using type = _Tp; };
@@ -2113,11 +2119,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __remove_pointer_helper<_Tp, _Up*>
{ using type = _Up; };
- /// remove_pointer
template<typename _Tp>
struct remove_pointer
: public __remove_pointer_helper<_Tp, __remove_cv_t<_Tp>>
{ };
+#endif
template<typename _Tp, typename = void>
struct __add_pointer_helper
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 29/40] c++, libstdc++: Implement __is_pointer built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (27 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 28/40] libstdc++: Optimize remove_pointer trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 30/40] libstdc++: Optimize is_pointer trait performance Ken Matsui
` (11 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_pointer.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_pointer.
* g++.dg/ext/is_pointer.C: New test.
* g++.dg/tm/pr46567.C (__is_pointer): Rename to ...
(__is_ptr): ... this.
* g++.dg/torture/20070621-1.C: Likewise.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_pointer): Rename to ...
(__is_ptr): ... this.
* include/bits/deque.tcc: Use __is_ptr instead.
* include/bits/stl_algobase.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_pointer.C | 51 +++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 22 ++++-----
gcc/testsuite/g++.dg/torture/20070621-1.C | 4 +-
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 6 +--
libstdc++-v3/include/bits/deque.tcc | 6 +--
libstdc++-v3/include/bits/stl_algobase.h | 6 +--
11 files changed, 86 insertions(+), 24 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 99a7e7247ce..c9d627fa782 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3787,6 +3787,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POD:
inform (loc, " %qT is not a POD type", t1);
break;
+ case CPTK_IS_POINTER:
+ inform (loc, " %qT is not a pointer", t1);
+ break;
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 07cd14b6e85..bc2bb5e5abb 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -81,6 +81,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
+DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 10656017bbc..131ca8b96e6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12196,6 +12196,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POD:
return pod_type_p (type1);
+ case CPTK_IS_POINTER:
+ return TYPE_PTR_P (type1);
+
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
@@ -12397,6 +12400,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
+ case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index bcab0599d1a..efce04fd09d 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -122,6 +122,9 @@
#if !__has_builtin (__is_pod)
# error "__has_builtin (__is_pod) failed"
#endif
+#if !__has_builtin (__is_pointer)
+# error "__has_builtin (__is_pointer) failed"
+#endif
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_pointer.C b/gcc/testsuite/g++.dg/ext/is_pointer.C
new file mode 100644
index 00000000000..d6e39565950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_pointer.C
@@ -0,0 +1,51 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(!__is_pointer(int));
+SA(__is_pointer(int*));
+SA(__is_pointer(int**));
+
+SA(__is_pointer(const int*));
+SA(__is_pointer(const int**));
+SA(__is_pointer(int* const));
+SA(__is_pointer(int** const));
+SA(__is_pointer(int* const* const));
+
+SA(__is_pointer(volatile int*));
+SA(__is_pointer(volatile int**));
+SA(__is_pointer(int* volatile));
+SA(__is_pointer(int** volatile));
+SA(__is_pointer(int* volatile* volatile));
+
+SA(__is_pointer(const volatile int*));
+SA(__is_pointer(const volatile int**));
+SA(__is_pointer(const int* volatile));
+SA(__is_pointer(volatile int* const));
+SA(__is_pointer(int* const volatile));
+SA(__is_pointer(const int** volatile));
+SA(__is_pointer(volatile int** const));
+SA(__is_pointer(int** const volatile));
+SA(__is_pointer(int* const* const volatile));
+SA(__is_pointer(int* volatile* const volatile));
+SA(__is_pointer(int* const volatile* const volatile));
+
+SA(!__is_pointer(int&));
+SA(!__is_pointer(const int&));
+SA(!__is_pointer(volatile int&));
+SA(!__is_pointer(const volatile int&));
+
+SA(!__is_pointer(int&&));
+SA(!__is_pointer(const int&&));
+SA(!__is_pointer(volatile int&&));
+SA(!__is_pointer(const volatile int&&));
+
+SA(!__is_pointer(int[3]));
+SA(!__is_pointer(const int[3]));
+SA(!__is_pointer(volatile int[3]));
+SA(!__is_pointer(const volatile int[3]));
+
+SA(!__is_pointer(int(int)));
+SA(__is_pointer(int(*const)(int)));
+SA(__is_pointer(int(*volatile)(int)));
+SA(__is_pointer(int(*const volatile)(int)));
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 6d791484448..f08bbf6fd7b 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -192,13 +192,13 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef __true_type __type;
};
template<typename _Tp>
- struct __is_pointer
+ struct __is_ptr
{
enum { __value = 0 };
typedef __false_type __type;
};
template<typename _Tp>
- struct __is_pointer<_Tp*>
+ struct __is_ptr<_Tp*>
{
enum { __value = 1 };
typedef __true_type __type;
@@ -226,7 +226,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
{ };
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
+ : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
struct __is_char
@@ -1202,8 +1202,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
typedef typename iterator_traits<_II>::iterator_category _Category;
const bool __simple = (__is_pod(_ValueTypeI)
- && __is_pointer<_II>::__value
- && __is_pointer<_OI>::__value
+ && __is_ptr<_II>::__value
+ && __is_ptr<_OI>::__value
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
return std::__copy_move<_IsMove, __simple,
_Category>::__copy_m(__first, __last, __result);
@@ -1294,8 +1294,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_BI2>::value_type _ValueType2;
typedef typename iterator_traits<_BI1>::iterator_category _Category;
const bool __simple = (__is_pod(_ValueType1)
- && __is_pointer<_BI1>::__value
- && __is_pointer<_BI2>::__value
+ && __is_ptr<_BI1>::__value
+ && __is_ptr<_BI2>::__value
&& __are_same<_ValueType1, _ValueType2>::__value);
return std::__copy_move_backward<_IsMove, __simple,
_Category>::__copy_move_b(__first,
@@ -1426,8 +1426,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple = (__is_integer<_ValueType1>::__value
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value
&& __are_same<_ValueType1, _ValueType2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
@@ -1515,8 +1515,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
&& !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
&& !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value);
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
__first2, __last2);
}
diff --git a/gcc/testsuite/g++.dg/torture/20070621-1.C b/gcc/testsuite/g++.dg/torture/20070621-1.C
index d8a6a76b6b0..b05136163e8 100644
--- a/gcc/testsuite/g++.dg/torture/20070621-1.C
+++ b/gcc/testsuite/g++.dg/torture/20070621-1.C
@@ -18,7 +18,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_pointer {
+ template<typename _Tp> struct __is_ptr {
enum {
__value = 0 };
};
@@ -49,7 +49,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
template<typename _II1, typename _II2> inline bool __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) {
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
- const bool __simple = (__is_integer<_ValueType1>::__value && __is_pointer<_II1>::__value && __is_pointer<_II2>::__value && __are_same<_ValueType1, _ValueType2>::__value);
+ const bool __simple = (__is_integer<_ValueType1>::__value && __is_ptr<_II1>::__value && __is_ptr<_II2>::__value && __are_same<_ValueType1, _ValueType2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
template<typename _II1, typename _II2> inline bool equal(_II1 __first1, _II1 __last1, _II2 __first2) {
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index 4dbd32bd298..be0689096fb 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -17,7 +17,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_pointer {
+ template<typename _Tp> struct __is_ptr {
enum {
__value = 0 };
};
@@ -27,7 +27,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Tp> struct __is_arithmetic : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> > {
+ template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 4312f32a4e0..3711e4be526 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -364,14 +364,14 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// Pointer types
//
template<typename _Tp>
- struct __is_pointer
+ struct __is_ptr
{
enum { __value = 0 };
typedef __false_type __type;
};
template<typename _Tp>
- struct __is_pointer<_Tp*>
+ struct __is_ptr<_Tp*>
{
enum { __value = 1 };
typedef __true_type __type;
@@ -390,7 +390,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
+ : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
{ };
//
diff --git a/libstdc++-v3/include/bits/deque.tcc b/libstdc++-v3/include/bits/deque.tcc
index a212b8a6940..08d888ee8af 100644
--- a/libstdc++-v3/include/bits/deque.tcc
+++ b/libstdc++-v3/include/bits/deque.tcc
@@ -1273,7 +1273,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
const bool __simple =
(__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
- && __is_pointer<_Ptr>::__value
+ && __is_ptr<_Ptr>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
@@ -1329,8 +1329,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
const bool __simple =
(__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
- && __is_pointer<_Ptr1>::__value
- && __is_pointer<_Ptr2>::__value
+ && __is_ptr<_Ptr1>::__value
+ && __is_ptr<_Ptr2>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 2f5a4bd4fd4..d1438429487 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -1217,7 +1217,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
typedef typename iterator_traits<_II1>::value_type _ValueType1;
const bool __simple = ((__is_integer<_ValueType1>::__value
- || __is_pointer<_ValueType1>::__value)
+ || __is_ptr<_ValueType1>::__value)
&& __memcmpable<_II1, _II2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
@@ -1380,8 +1380,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 30/40] libstdc++: Optimize is_pointer trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (28 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 29/40] c++, libstdc++: Implement __is_pointer built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait Ken Matsui
` (10 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui, Jonathan Wakely
This patch optimizes the performance of the is_pointer trait by dispatching to
the new __is_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_ptr): Use __is_pointer
built-in trait.
* include/std/type_traits (is_pointer): Likewise. Optimize its
implementation.
(is_pointer_v): Likewise.
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/bits/cpp_type_traits.h | 8 ++++
libstdc++-v3/include/std/type_traits | 44 +++++++++++++++++----
2 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 3711e4be526..4da1e7c407c 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -363,6 +363,13 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
// Pointer types
//
+#if __has_builtin(__is_pointer)
+ template<typename _Tp>
+ struct __is_ptr : __truth_type<__is_pointer(_Tp)>
+ {
+ enum { __value = __is_pointer(_Tp) };
+ };
+#else
template<typename _Tp>
struct __is_ptr
{
@@ -376,6 +383,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
enum { __value = 1 };
typedef __true_type __type;
};
+#endif
//
// An arithmetic type is an integer type or a floating point type
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 9c56d15c0b7..3acd843f2f2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -542,19 +542,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public true_type { };
#endif
- template<typename>
- struct __is_pointer_helper
+ /// is_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
+ template<typename _Tp>
+ struct is_pointer
+ : public __bool_constant<__is_pointer(_Tp)>
+ { };
+#else
+ template<typename _Tp>
+ struct is_pointer
: public false_type { };
template<typename _Tp>
- struct __is_pointer_helper<_Tp*>
+ struct is_pointer<_Tp*>
: public true_type { };
- /// is_pointer
template<typename _Tp>
- struct is_pointer
- : public __is_pointer_helper<__remove_cv_t<_Tp>>::type
- { };
+ struct is_pointer<_Tp* const>
+ : public true_type { };
+
+ template<typename _Tp>
+ struct is_pointer<_Tp* volatile>
+ : public true_type { };
+
+ template<typename _Tp>
+ struct is_pointer<_Tp* const volatile>
+ : public true_type { };
+#endif
/// is_lvalue_reference
template<typename>
@@ -3254,8 +3268,22 @@ template <typename _Tp, size_t _Num>
inline constexpr bool is_array_v<_Tp[_Num]> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
+template <typename _Tp>
+ inline constexpr bool is_pointer_v = __is_pointer(_Tp);
+#else
template <typename _Tp>
- inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
+ inline constexpr bool is_pointer_v = false;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp*> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* const> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* volatile> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* const volatile> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_lvalue_reference_v = false;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (29 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 30/40] libstdc++: Optimize is_pointer trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 32/40] libstdc++: Optimize is_arithmetic trait performance Ken Matsui
` (9 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_arithmetic.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_arithmetic.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARITHMETIC.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic.
* g++.dg/ext/is_arithmetic.C: New test.
* g++.dg/tm/pr46567.C (__is_arithmetic): Rename to ...
(__is_arith): ... this.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_arithmetic): Rename to ...
(__is_arith): ... this.
* include/c_global/cmath: Use __is_arith instead.
* include/c_std/cmath: Likewise.
* include/tr1/cmath: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_arithmetic.C | 33 ++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 6 +--
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 4 +-
libstdc++-v3/include/c_global/cmath | 48 ++++++++++-----------
libstdc++-v3/include/c_std/cmath | 24 +++++------
libstdc++-v3/include/tr1/cmath | 24 +++++------
11 files changed, 99 insertions(+), 55 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9d627fa782..3a7f968eae8 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_AGGREGATE:
inform (loc, " %qT is not an aggregate", t1);
break;
+ case CPTK_IS_ARITHMETIC:
+ inform (loc, " %qT is not an arithmetic type", t1);
+ break;
case CPTK_IS_ARRAY:
inform (loc, " %qT is not an array", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index bc2bb5e5abb..06c203ce4de 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 131ca8b96e6..553a51dc16d 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12128,6 +12128,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ARITHMETIC:
+ return ARITHMETIC_TYPE_P (type1);
+
case CPTK_IS_ARRAY:
return type_code1 == ARRAY_TYPE;
@@ -12391,6 +12394,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ARITHMETIC:
case CPTK_IS_ARRAY:
case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index efce04fd09d..4bc85f4babb 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_arithmetic)
+# error "__has_builtin (__is_arithmetic) failed"
+#endif
#if !__has_builtin (__is_array)
# error "__has_builtin (__is_array) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
new file mode 100644
index 00000000000..fd35831f646
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_arithmetic, void, false);
+
+SA_TEST_CATEGORY(__is_arithmetic, char, true);
+SA_TEST_CATEGORY(__is_arithmetic, signed char, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true);
+SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true);
+SA_TEST_CATEGORY(__is_arithmetic, short, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true);
+SA_TEST_CATEGORY(__is_arithmetic, int, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true);
+SA_TEST_CATEGORY(__is_arithmetic, long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true);
+SA_TEST_CATEGORY(__is_arithmetic, long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, float, true);
+SA_TEST_CATEGORY(__is_arithmetic, double, true);
+SA_TEST_CATEGORY(__is_arithmetic, long double, true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index f08bbf6fd7b..79d304e0309 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -217,16 +217,16 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef __true_type __type;
};
template<typename _Tp>
- struct __is_arithmetic
+ struct __is_arith
: public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
{ };
template<typename _Tp>
struct __is_fundamental
- : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
+ : public __traitor<__is_void<_Tp>, __is_arith<_Tp> >
{ };
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
+ : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
struct __is_char
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index be0689096fb..da592b9fd23 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -25,9 +25,9 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_arithmetic : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
+ template<typename _Tp> struct __is_arith : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> > {
+ template<typename _Tp> struct __is_scalar : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 4da1e7c407c..51ed5b07716 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -389,7 +389,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// An arithmetic type is an integer type or a floating point type
//
template<typename _Tp>
- struct __is_arithmetic
+ struct __is_arith
: public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
{ };
@@ -398,7 +398,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
+ : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
//
diff --git a/libstdc++-v3/include/c_global/cmath b/libstdc++-v3/include/c_global/cmath
index 6461c92ebfe..a0ddc1dbbeb 100644
--- a/libstdc++-v3/include/c_global/cmath
+++ b/libstdc++-v3/include/c_global/cmath
@@ -1259,8 +1259,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isgreater(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1285,8 +1285,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isgreaterequal(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1311,8 +1311,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isless(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1337,8 +1337,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
islessequal(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1363,8 +1363,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
islessgreater(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1389,8 +1389,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isunordered(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1401,7 +1401,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#else
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -1411,7 +1411,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -1420,7 +1420,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -1429,7 +1429,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -1438,7 +1438,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -1447,7 +1447,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -1456,7 +1456,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -1465,7 +1465,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -1474,7 +1474,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -1483,7 +1483,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -1492,7 +1492,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -1501,7 +1501,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
diff --git a/libstdc++-v3/include/c_std/cmath b/libstdc++-v3/include/c_std/cmath
index 588ee1e6dc4..c1db699ecdb 100644
--- a/libstdc++-v3/include/c_std/cmath
+++ b/libstdc++-v3/include/c_std/cmath
@@ -467,7 +467,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#undef isunordered
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -477,7 +477,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -486,7 +486,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -495,7 +495,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -504,7 +504,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -513,7 +513,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -522,7 +522,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -531,7 +531,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -540,7 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -549,7 +549,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -558,7 +558,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -567,7 +567,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
diff --git a/libstdc++-v3/include/tr1/cmath b/libstdc++-v3/include/tr1/cmath
index ba1b60cc945..2e80f1d0d00 100644
--- a/libstdc++-v3/include/tr1/cmath
+++ b/libstdc++-v3/include/tr1/cmath
@@ -307,7 +307,7 @@ namespace tr1
/// Function template definitions [8.16.3].
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -317,7 +317,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -326,7 +326,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -335,7 +335,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -344,7 +344,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -353,7 +353,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -362,7 +362,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -371,7 +371,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -380,7 +380,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -389,7 +389,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -398,7 +398,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -407,7 +407,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 32/40] libstdc++: Optimize is_arithmetic trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (30 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 33/40] libstdc++: Optimize is_fundamental " Ken Matsui
` (8 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_arithmetic trait by dispatching
to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_arithmetic): Use __is_arithmetic
built-in trait.
(is_arithmetic_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 3acd843f2f2..cc466e0f606 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -726,10 +726,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_arithmetic
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+ template<typename _Tp>
+ struct is_arithmetic
+ : public __bool_constant<__is_arithmetic(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_arithmetic
: public __or_<is_integral<_Tp>, is_floating_point<_Tp>>::type
{ };
+#endif
/// is_fundamental
template<typename _Tp>
@@ -3344,8 +3351,14 @@ template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&&> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+template <typename _Tp>
+ inline constexpr bool is_arithmetic_v = __is_arithmetic(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 33/40] libstdc++: Optimize is_fundamental trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (31 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 32/40] libstdc++: Optimize is_arithmetic trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 34/40] libstdc++: Optimize is_compound " Ken Matsui
` (7 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_fundamental trait by
dispatching to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_fundamental_v): Use __is_arithmetic
built-in trait.
(is_fundamental): Likewise. Optimize the original implementation.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index cc466e0f606..88171e1a672 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -739,11 +739,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_fundamental
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+ template<typename _Tp>
+ struct is_fundamental
+ : public __bool_constant<__is_arithmetic(_Tp)
+ || is_void<_Tp>::value
+ || is_null_pointer<_Tp>::value>
+ { };
+#else
template<typename _Tp>
struct is_fundamental
- : public __or_<is_arithmetic<_Tp>, is_void<_Tp>,
- is_null_pointer<_Tp>>::type
+ : public __bool_constant<is_arithmetic<_Tp>::value
+ || is_void<_Tp>::value
+ || is_null_pointer<_Tp>::value>
{ };
+#endif
/// is_object
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
@@ -3354,13 +3364,15 @@ template <typename _Tp>
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
template <typename _Tp>
inline constexpr bool is_arithmetic_v = __is_arithmetic(_Tp);
+template <typename _Tp>
+ inline constexpr bool is_fundamental_v
+ = __is_arithmetic(_Tp) || is_void_v<_Tp> || is_null_pointer_v<_Tp>;
#else
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
-#endif
-
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
&& _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 34/40] libstdc++: Optimize is_compound trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (32 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 33/40] libstdc++: Optimize is_fundamental " Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 35/40] c++: Implement __is_unsigned built-in trait Ken Matsui
` (6 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_compound trait by dispatching
to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_compound): Do not use __not_.
(is_compound_v): Use is_fundamental_v instead.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 88171e1a672..48d630a1478 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -784,7 +784,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// is_compound
template<typename _Tp>
struct is_compound
- : public __not_<is_fundamental<_Tp>>::type { };
+ : public __bool_constant<!is_fundamental<_Tp>::value> { };
/// is_member_pointer
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
@@ -3387,7 +3387,7 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
- inline constexpr bool is_compound_v = is_compound<_Tp>::value;
+ inline constexpr bool is_compound_v = !is_fundamental_v<_Tp>;
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 35/40] c++: Implement __is_unsigned built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (33 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 34/40] libstdc++: Optimize is_compound " Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 36/40] libstdc++: Optimize is_unsigned trait performance Ken Matsui
` (5 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_unsigned.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_unsigned.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNSIGNED.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_unsigned.
* g++.dg/ext/is_unsigned.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 ++++++++++++++++++++++++
5 files changed, 58 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 3a7f968eae8..c28dad702c3 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3829,6 +3829,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
+ case CPTK_IS_UNSIGNED:
+ inform (loc, " %qT is not an unsigned type", t1);
+ break;
case CPTK_IS_VOLATILE:
inform (loc, " %qT is not a volatile type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 06c203ce4de..611e84cbbfd 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -94,6 +94,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_UNSIGNED, "__is_unsigned", 1)
DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 553a51dc16d..b5c6b4992e5 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12235,6 +12235,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
+ case CPTK_IS_UNSIGNED:
+ return TYPE_UNSIGNED (type1);
+
case CPTK_IS_VOLATILE:
return CP_TYPE_VOLATILE_P (type1);
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
+ case CPTK_IS_UNSIGNED:
case CPTK_IS_VOLATILE:
break;
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4bc85f4babb..3d380f94b06 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -164,6 +164,9 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
+#if !__has_builtin (__is_unsigned)
+# error "__has_builtin (__is_unsigned) failed"
+#endif
#if !__has_builtin (__is_volatile)
# error "__has_builtin (__is_volatile) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_unsigned.C b/gcc/testsuite/g++.dg/ext/is_unsigned.C
new file mode 100644
index 00000000000..2bb45d209a7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_unsigned.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_unsigned, void, false);
+
+SA_TEST_CATEGORY(__is_unsigned, bool, (bool(-1) > bool(0)));
+SA_TEST_CATEGORY(__is_unsigned, char, (char(-1) > char(0)));
+SA_TEST_CATEGORY(__is_unsigned, signed char, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned char, true);
+SA_TEST_CATEGORY(__is_unsigned, wchar_t, (wchar_t(-1) > wchar_t(0)));
+SA_TEST_CATEGORY(__is_unsigned, short, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned short, true);
+SA_TEST_CATEGORY(__is_unsigned, int, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned int, true);
+SA_TEST_CATEGORY(__is_unsigned, long, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned long, true);
+SA_TEST_CATEGORY(__is_unsigned, long long, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned long long, true);
+
+SA_TEST_CATEGORY(__is_unsigned, float, false);
+SA_TEST_CATEGORY(__is_unsigned, double, false);
+SA_TEST_CATEGORY(__is_unsigned, long double, false);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_unsigned, unsigned __int128, true);
+SA_TEST_CATEGORY(__is_unsigned, __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_unsigned, __float128, false);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_unsigned, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 36/40] libstdc++: Optimize is_unsigned trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (34 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 35/40] c++: Implement __is_unsigned built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 37/40] c++, libstdc++: Implement __is_signed built-in trait Ken Matsui
` (4 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_unsigned trait by dispatching
to the new __is_unsigned built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_unsigned): Use __is_unsigned built-in
trait.
(is_unsigned_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 48d630a1478..f7d3815f332 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -1001,10 +1001,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_unsigned
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
+ template<typename _Tp>
+ struct is_unsigned
+ : public __bool_constant<__is_unsigned(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_unsigned
: public __and_<is_arithmetic<_Tp>, __not_<is_signed<_Tp>>>::type
{ };
+#endif
/// @cond undocumented
template<typename _Tp, typename _Up = _Tp&&>
@@ -3440,8 +3447,14 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_signed_v = is_signed<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
+template <typename _Tp>
+ inline constexpr bool is_unsigned_v = __is_unsigned(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_unsigned_v = is_unsigned<_Tp>::value;
+#endif
template <typename _Tp, typename... _Args>
inline constexpr bool is_constructible_v = __is_constructible(_Tp, _Args...);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 37/40] c++, libstdc++: Implement __is_signed built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (35 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 36/40] libstdc++: Optimize is_unsigned trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 38/40] libstdc++: Optimize is_signed trait performance Ken Matsui
` (3 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_signed.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_signed.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SIGNED.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_signed.
* g++.dg/ext/is_signed.C: New test.
* g++.dg/tm/pr46567.C (__is_signed): Rename to ...
(__is_signed_type): ... this.
libstdc++-v3/ChangeLog:
* include/ext/numeric_traits.h (__is_signed): Rename to ...
(__is_signed_type): ... this.
* include/bits/charconv.h: Use __is_signed_type instead.
* include/bits/locale_facets.tcc: Likewise.
* include/bits/uniform_int_dist.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_signed.C | 47 ++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 12 ++---
libstdc++-v3/include/bits/charconv.h | 2 +-
libstdc++-v3/include/bits/locale_facets.tcc | 6 +--
libstdc++-v3/include/bits/uniform_int_dist.h | 4 +-
libstdc++-v3/include/ext/numeric_traits.h | 18 ++++----
10 files changed, 79 insertions(+), 21 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c28dad702c3..b161c9b2c9e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3802,6 +3802,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SIGNED:
+ inform (loc, " %qT is not a signed type", t1);
+ break;
case CPTK_IS_SCOPED_ENUM:
inform (loc, " %qT is not a scoped enum", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 611e84cbbfd..f0b5fe9cb3b 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -86,6 +86,7 @@ DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index b5c6b4992e5..58011a45cc6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12211,6 +12211,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SIGNED:
+ return ARITHMETIC_TYPE_P (type1) && TYPE_SIGN (type1) == SIGNED;
+
case CPTK_IS_SCOPED_ENUM:
return SCOPED_ENUM_P (type1);
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
+ case CPTK_IS_SIGNED:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 3d380f94b06..aaf7254df4b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -140,6 +140,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_signed)
+# error "__has_builtin (__is_signed) failed"
+#endif
#if !__has_builtin (__is_scoped_enum)
# error "__has_builtin (__is_scoped_enum) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_signed.C b/gcc/testsuite/g++.dg/ext/is_signed.C
new file mode 100644
index 00000000000..a04b548105d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_signed.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_signed, void, false);
+
+SA_TEST_CATEGORY(__is_signed, bool, bool(-1) < bool(0));
+SA_TEST_CATEGORY(__is_signed, char, char(-1) < char(0));
+SA_TEST_CATEGORY(__is_signed, signed char, true);
+SA_TEST_CATEGORY(__is_signed, unsigned char, false);
+SA_TEST_CATEGORY(__is_signed, wchar_t, wchar_t(-1) < wchar_t(0));
+SA_TEST_CATEGORY(__is_signed, short, true);
+SA_TEST_CATEGORY(__is_signed, unsigned short, false);
+SA_TEST_CATEGORY(__is_signed, int, true);
+SA_TEST_CATEGORY(__is_signed, unsigned int, false);
+SA_TEST_CATEGORY(__is_signed, long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long, false);
+SA_TEST_CATEGORY(__is_signed, long long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long long, false);
+
+SA_TEST_CATEGORY(__is_signed, float, true);
+SA_TEST_CATEGORY(__is_signed, double, true);
+SA_TEST_CATEGORY(__is_signed, long double, true);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_signed, __int128, true);
+SA_TEST_CATEGORY(__is_signed, unsigned __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_signed, __float128, true);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_signed, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 79d304e0309..c891aff20f4 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -403,7 +403,7 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
{
static const _Value __min = (((_Value)(-1) < 0) ? (_Value)1 << (sizeof(_Value) * 8 - ((_Value)(-1) < 0)) : (_Value)0);
static const _Value __max = (((_Value)(-1) < 0) ? (((((_Value)1 << ((sizeof(_Value) * 8 - ((_Value)(-1) < 0)) - 1)) - 1) << 1) + 1) : ~(_Value)0);
- static const bool __is_signed = ((_Value)(-1) < 0);
+ static const bool __is_signed_type = ((_Value)(-1) < 0);
static const int __digits = (sizeof(_Value) * 8 - ((_Value)(-1) < 0));
};
template<typename _Value>
@@ -411,21 +411,21 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
template<typename _Value>
const _Value __numeric_traits_integer<_Value>::__max;
template<typename _Value>
- const bool __numeric_traits_integer<_Value>::__is_signed;
+ const bool __numeric_traits_integer<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_integer<_Value>::__digits;
template<typename _Value>
struct __numeric_traits_floating
{
static const int __max_digits10 = (2 + (std::__are_same<_Value, float>::__value ? 24 : std::__are_same<_Value, double>::__value ? 53 : 64) * 3010 / 10000);
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = (std::__are_same<_Value, float>::__value ? 6 : std::__are_same<_Value, double>::__value ? 15 : 18);
static const int __max_exponent10 = (std::__are_same<_Value, float>::__value ? 38 : std::__are_same<_Value, double>::__value ? 308 : 4932);
};
template<typename _Value>
const int __numeric_traits_floating<_Value>::__max_digits10;
template<typename _Value>
- const bool __numeric_traits_floating<_Value>::__is_signed;
+ const bool __numeric_traits_floating<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_floating<_Value>::__digits10;
template<typename _Value>
@@ -1513,8 +1513,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
- && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
- && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
+ && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed_type
+ && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed_type
&& __is_ptr<_II1>::__value
&& __is_ptr<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
diff --git a/libstdc++-v3/include/bits/charconv.h b/libstdc++-v3/include/bits/charconv.h
index 20da8303f7a..1acf1e46e4c 100644
--- a/libstdc++-v3/include/bits/charconv.h
+++ b/libstdc++-v3/include/bits/charconv.h
@@ -46,7 +46,7 @@ namespace __detail
// This accepts 128-bit integers even in strict mode.
template<typename _Tp>
constexpr bool __integer_to_chars_is_unsigned
- = ! __gnu_cxx::__int_traits<_Tp>::__is_signed;
+ = ! __gnu_cxx::__int_traits<_Tp>::__is_signed_type;
#endif
// Generic implementation for arbitrary bases.
diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc
index 6bfff7d6289..38a6920abe9 100644
--- a/libstdc++-v3/include/bits/locale_facets.tcc
+++ b/libstdc++-v3/include/bits/locale_facets.tcc
@@ -470,7 +470,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
bool __testfail = false;
bool __testoverflow = false;
const __unsigned_type __max =
- (__negative && __num_traits::__is_signed)
+ (__negative && __num_traits::__is_signed_type)
? -static_cast<__unsigned_type>(__num_traits::__min)
: __num_traits::__max;
const __unsigned_type __smax = __max / __base;
@@ -573,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
}
else if (__testoverflow)
{
- if (__negative && __num_traits::__is_signed)
+ if (__negative && __num_traits::__is_signed_type)
__v = __num_traits::__min;
else
__v = __num_traits::__max;
@@ -914,7 +914,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
if (__v >= 0)
{
if (bool(__flags & ios_base::showpos)
- && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
+ && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed_type)
*--__cs = __lit[__num_base::_S_oplus], ++__len;
}
else
diff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h
index 7ccf930a6d4..73b808e57f3 100644
--- a/libstdc++-v3/include/bits/uniform_int_dist.h
+++ b/libstdc++-v3/include/bits/uniform_int_dist.h
@@ -258,8 +258,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
using _Up_traits = __gnu_cxx::__int_traits<_Up>;
using _Wp_traits = __gnu_cxx::__int_traits<_Wp>;
- static_assert(!_Up_traits::__is_signed, "U must be unsigned");
- static_assert(!_Wp_traits::__is_signed, "W must be unsigned");
+ static_assert(!_Up_traits::__is_signed_type, "U must be unsigned");
+ static_assert(!_Wp_traits::__is_signed_type, "W must be unsigned");
static_assert(_Wp_traits::__digits == (2 * _Up_traits::__digits),
"W must be twice as wide as U");
diff --git a/libstdc++-v3/include/ext/numeric_traits.h b/libstdc++-v3/include/ext/numeric_traits.h
index dcbc2d12927..c618f211775 100644
--- a/libstdc++-v3/include/ext/numeric_traits.h
+++ b/libstdc++-v3/include/ext/numeric_traits.h
@@ -67,15 +67,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// NB: these two are also available in std::numeric_limits as compile
// time constants, but <limits> is big and we can avoid including it.
- static const bool __is_signed = (_Value)(-1) < 0;
+ static const bool __is_signed_type = (_Value)(-1) < 0;
static const int __digits
- = __is_integer_nonstrict<_Value>::__width - __is_signed;
+ = __is_integer_nonstrict<_Value>::__width - __is_signed_type;
// The initializers must be constants so that __max and __min are too.
- static const _Value __max = __is_signed
+ static const _Value __max = __is_signed_type
? (((((_Value)1 << (__digits - 1)) - 1) << 1) + 1)
: ~(_Value)0;
- static const _Value __min = __is_signed ? -__max - 1 : (_Value)0;
+ static const _Value __min = __is_signed_type ? -__max - 1 : (_Value)0;
};
template<typename _Value>
@@ -85,7 +85,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const _Value __numeric_traits_integer<_Value>::__max;
template<typename _Value>
- const bool __numeric_traits_integer<_Value>::__is_signed;
+ const bool __numeric_traits_integer<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_integer<_Value>::__digits;
@@ -161,7 +161,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static const int __max_digits10 = __glibcxx_max_digits10(_Value);
// See above comment...
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = __glibcxx_digits10(_Value);
static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
};
@@ -170,7 +170,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const int __numeric_traits_floating<_Value>::__max_digits10;
template<typename _Value>
- const bool __numeric_traits_floating<_Value>::__is_signed;
+ const bool __numeric_traits_floating<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_floating<_Value>::__digits10;
@@ -210,7 +210,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __numeric_traits_floating<__ibm128>
{
static const int __max_digits10 = 33;
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = 31;
static const int __max_exponent10 = 308;
};
@@ -224,7 +224,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __numeric_traits_floating<__ieee128>
{
static const int __max_digits10 = 36;
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = 33;
static const int __max_exponent10 = 4932;
};
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 38/40] libstdc++: Optimize is_signed trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (36 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 37/40] c++, libstdc++: Implement __is_signed built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 39/40] c++, libstdc++: Implement __is_scalar built-in trait Ken Matsui
` (2 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_signed trait by dispatching to
the new __is_signed built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_signed): Use __is_signed built-in trait.
(is_signed_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index f7d3815f332..7e93923f44b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -982,6 +982,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __bool_constant<__is_abstract(_Tp)>
{ };
+ /// is_signed
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_signed)
+ template<typename _Tp>
+ struct is_signed
+ : public __bool_constant<__is_signed(_Tp)>
+ { };
+#else
/// @cond undocumented
template<typename _Tp,
bool = is_arithmetic<_Tp>::value>
@@ -994,11 +1001,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// @endcond
- /// is_signed
template<typename _Tp>
struct is_signed
: public __is_signed_helper<_Tp>::type
{ };
+#endif
/// is_unsigned
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
@@ -3445,8 +3452,13 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_final_v = __is_final(_Tp);
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_signed)
+template <typename _Tp>
+ inline constexpr bool is_signed_v = __is_signed(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_signed_v = is_signed<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 39/40] c++, libstdc++: Implement __is_scalar built-in trait
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (37 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 38/40] libstdc++: Optimize is_signed trait performance Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:21 ` [PATCH v12 40/40] libstdc++: Optimize is_scalar trait performance Ken Matsui
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_scalar. The existent
__is_scalar codes were replaced with __is_scalar_type to avoid unintentional
macro replacement by the new built-in.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_scalar.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCALAR.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_scalar.
* g++.dg/ext/is_scalar.C: New test.
* g++.dg/tm/pr46567.C: Use __is_scalar_type instead.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_scalar): Rename to ...
(__is_scalar_type): ... this.
* include/bits/stl_algobase.h: Use __is_scalar_type instead.
* include/bits/valarray_array.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_scalar.C | 31 +++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 10 +++----
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +--
libstdc++-v3/include/bits/cpp_type_traits.h | 2 +-
libstdc++-v3/include/bits/stl_algobase.h | 8 +++---
libstdc++-v3/include/bits/valarray_array.h | 2 +-
10 files changed, 55 insertions(+), 13 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index b161c9b2c9e..78f100d2745 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3802,6 +3802,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SCALAR:
+ inform (loc, " %qT is not a scalar type", t1);
+ break;
case CPTK_IS_SIGNED:
inform (loc, " %qT is not a signed type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index f0b5fe9cb3b..4e220262020 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -86,6 +86,7 @@ DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SCALAR, "__is_scalar", 1)
DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 58011a45cc6..1a6a04586fc 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12211,6 +12211,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SCALAR:
+ return SCALAR_TYPE_P (type1);
+
case CPTK_IS_SIGNED:
return ARITHMETIC_TYPE_P (type1) && TYPE_SIGN (type1) == SIGNED;
@@ -12413,6 +12416,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
+ case CPTK_IS_SCALAR:
case CPTK_IS_SIGNED:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index aaf7254df4b..f4f6fed6876 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -140,6 +140,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_scalar)
+# error "__has_builtin (__is_scalar) failed"
+#endif
#if !__has_builtin (__is_signed)
# error "__has_builtin (__is_signed) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_scalar.C b/gcc/testsuite/g++.dg/ext/is_scalar.C
new file mode 100644
index 00000000000..457fddc52fc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_scalar.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+
+#include <cstddef> // std::nullptr_t
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// volatile return type would cause a warning.
+#define SA_FN_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_scalar, int, true);
+SA_TEST_CATEGORY(__is_scalar, float, true);
+SA_TEST_CATEGORY(__is_scalar, EnumType, true);
+SA_TEST_CATEGORY(__is_scalar, int*, true);
+SA_FN_TEST_CATEGORY(__is_scalar, int(*)(int), true);
+SA_TEST_CATEGORY(__is_scalar, int (ClassType::*), true);
+SA_FN_TEST_CATEGORY(__is_scalar, int (ClassType::*) (int), true);
+SA_TEST_CATEGORY(__is_scalar, std::nullptr_t, true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_scalar, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index c891aff20f4..393f936ea72 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -225,7 +225,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
: public __traitor<__is_void<_Tp>, __is_arith<_Tp> >
{ };
template<typename _Tp>
- struct __is_scalar
+ struct __is_scalar_type
: public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
@@ -1325,7 +1325,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _ForwardIterator, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, void>::__type
__fill_a(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -1334,7 +1334,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _ForwardIterator, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, void>::__type
__fill_a(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -1362,7 +1362,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _OutputIterator, typename _Size, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
for (; __n > 0; --__n, ++__first)
@@ -1371,7 +1371,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
}
template<typename _OutputIterator, typename _Size, typename _Tp>
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
const _Tp __tmp = __value;
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index da592b9fd23..4d2ef002e08 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -27,7 +27,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Tp> struct __is_arith : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
+ template<typename _Tp> struct __is_scalar_type : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
@@ -54,7 +54,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Iterator> inline typename _Niter_base<_Iterator>::iterator_type __niter_base(_Iterator __it) {
}
- template<typename _OutputIterator, typename _Size, typename _Tp> inline typename __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) {
+ template<typename _OutputIterator, typename _Size, typename _Tp> inline typename __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, _OutputIterator>::__type __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) {
for (__decltype(__n + 0) __niter = __n;
__niter > 0;
--__niter, ++__first) *__first = __value;
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 51ed5b07716..16980f5b356 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -397,7 +397,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// A scalar type is an arithmetic type or a pointer type
//
template<typename _Tp>
- struct __is_scalar
+ struct __is_scalar_type
: public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index d1438429487..4e334da0832 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -914,7 +914,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _ForwardIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, void>::__type
__fill_a1(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -925,7 +925,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _ForwardIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, void>::__type
__fill_a1(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
@@ -1063,7 +1063,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _OutputIterator, typename _Size, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<!__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
{
for (; __n > 0; --__n, (void) ++__first)
@@ -1074,7 +1074,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
template<typename _OutputIterator, typename _Size, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
- __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
+ __gnu_cxx::__enable_if<__is_scalar_type<_Tp>::__value, _OutputIterator>::__type
__fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
{
const _Tp __tmp = __value;
diff --git a/libstdc++-v3/include/bits/valarray_array.h b/libstdc++-v3/include/bits/valarray_array.h
index 222fd5fd900..558817329ce 100644
--- a/libstdc++-v3/include/bits/valarray_array.h
+++ b/libstdc++-v3/include/bits/valarray_array.h
@@ -90,7 +90,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline void
__valarray_default_construct(_Tp* __b, _Tp* __e)
{
- _Array_default_ctor<_Tp, __is_scalar<_Tp>::__value>::_S_do_it(__b, __e);
+ _Array_default_ctor<_Tp, __is_scalar_type<_Tp>::__value>::_S_do_it(__b, __e);
}
// Turn a raw-memory into an array of _Tp filled with __t
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v12 40/40] libstdc++: Optimize is_scalar trait performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (38 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 39/40] c++, libstdc++: Implement __is_scalar built-in trait Ken Matsui
@ 2023-09-15 2:21 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:21 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_scalar trait by dispatching to
the new __is_scalar built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_scalar): Use __is_scalar built-in
trait.
(is_scalar_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 7e93923f44b..eb16a642575 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -775,11 +775,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_member_pointer;
/// is_scalar
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scalar)
+ template<typename _Tp>
+ struct is_scalar
+ : public __bool_constant<__is_scalar(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_scalar
: public __or_<is_arithmetic<_Tp>, is_enum<_Tp>, is_pointer<_Tp>,
is_member_pointer<_Tp>, is_null_pointer<_Tp>>::type
{ };
+#endif
/// is_compound
template<typename _Tp>
@@ -3398,8 +3405,14 @@ template <typename _Tp>
inline constexpr bool is_object_v = is_object<_Tp>::value;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scalar)
+template <typename _Tp>
+ inline constexpr bool is_scalar_v = __is_scalar(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_compound_v = !is_fundamental_v<_Tp>;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 00/40] Optimize type traits performance
2023-09-15 2:21 ` [PATCH v12 00/40] Optimize type traits performance Ken Matsui
` (39 preceding siblings ...)
2023-09-15 2:21 ` [PATCH v12 40/40] libstdc++: Optimize is_scalar trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
` (40 more replies)
40 siblings, 41 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch series optimizes type traits performance by implementing
built-in type traits and using them in libstdc++.
Changes in v13:
* Fixed ambiguous commit message and comment
Changes in v12:
* Evaluated all paddings affected by the enum rid change
Changes in v11:
* Merged all patches into one patch series
* Rebased on top of trunk
* Unified commit message style
* Used _GLIBCXX_USE_BUILTIN_TRAIT
Ken Matsui (40):
c++: Sort built-in identifiers alphabetically
c++: Implement __is_const built-in trait
libstdc++: Optimize is_const trait performance
c++: Implement __is_volatile built-in trait
libstdc++: Optimize is_volatile trait performance
c++: Implement __is_array built-in trait
libstdc++: Optimize is_array trait performance
c++: Implement __is_unbounded_array built-in trait
libstdc++: Optimize is_unbounded_array trait performance
c++: Implement __is_bounded_array built-in trait
libstdc++: Optimize is_bounded_array trait performance
c++: Implement __is_scoped_enum built-in trait
libstdc++: Optimize is_scoped_enum trait performance
c++: Implement __is_member_pointer built-in trait
libstdc++: Optimize is_member_pointer trait performance
c, c++: Use 16 bits for all use of enum rid for more keyword space
c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly
c++: Implement __is_member_function_pointer built-in trait
libstdc++: Optimize is_member_function_pointer trait performance
c++: Implement __is_member_object_pointer built-in trait
libstdc++: Optimize is_member_object_pointer trait performance
c++: Implement __is_reference built-in trait
libstdc++: Optimize is_reference trait performance
c++: Implement __is_function built-in trait
libstdc++: Optimize is_function trait performance
libstdc++: Optimize is_object trait performance
c++: Implement __remove_pointer built-in trait
libstdc++: Optimize remove_pointer trait performance
c++, libstdc++: Implement __is_pointer built-in trait
libstdc++: Optimize is_pointer trait performance
c++, libstdc++: Implement __is_arithmetic built-in trait
libstdc++: Optimize is_arithmetic trait performance
libstdc++: Optimize is_fundamental trait performance
libstdc++: Optimize is_compound trait performance
c++: Implement __is_unsigned built-in trait
libstdc++: Optimize is_unsigned trait performance
c++, libstdc++: Implement __is_signed built-in trait
libstdc++: Optimize is_signed trait performance
c++, libstdc++: Implement __is_scalar built-in trait
libstdc++: Optimize is_scalar trait performance
gcc/c-family/c-common.h | 2 +-
gcc/c-family/c-indentation.h | 2 +-
gcc/c/c-parser.cc | 6 +-
gcc/c/c-parser.h | 14 +-
gcc/cp/constraint.cc | 112 +++++--
gcc/cp/cp-trait.def | 27 +-
gcc/cp/parser.h | 8 +-
gcc/cp/semantics.cc | 157 +++++++---
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 117 ++++++--
gcc/testsuite/g++.dg/ext/is_arithmetic.C | 33 ++
gcc/testsuite/g++.dg/ext/is_array.C | 28 ++
gcc/testsuite/g++.dg/ext/is_bounded_array.C | 38 +++
gcc/testsuite/g++.dg/ext/is_const.C | 19 ++
gcc/testsuite/g++.dg/ext/is_function.C | 58 ++++
.../g++.dg/ext/is_member_function_pointer.C | 31 ++
.../g++.dg/ext/is_member_object_pointer.C | 30 ++
gcc/testsuite/g++.dg/ext/is_member_pointer.C | 30 ++
gcc/testsuite/g++.dg/ext/is_pointer.C | 51 ++++
gcc/testsuite/g++.dg/ext/is_reference.C | 34 +++
gcc/testsuite/g++.dg/ext/is_scalar.C | 31 ++
gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++
gcc/testsuite/g++.dg/ext/is_signed.C | 47 +++
gcc/testsuite/g++.dg/ext/is_unbounded_array.C | 37 +++
gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 +++
gcc/testsuite/g++.dg/ext/is_volatile.C | 19 ++
gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 ++++
gcc/testsuite/g++.dg/tm/pr46567.C | 48 +--
gcc/testsuite/g++.dg/torture/20070621-1.C | 4 +-
gcc/testsuite/g++.dg/torture/pr57107.C | 8 +-
libcpp/include/cpplib.h | 7 +-
libstdc++-v3/include/bits/charconv.h | 2 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 18 +-
libstdc++-v3/include/bits/deque.tcc | 6 +-
libstdc++-v3/include/bits/locale_facets.tcc | 6 +-
libstdc++-v3/include/bits/stl_algobase.h | 14 +-
libstdc++-v3/include/bits/uniform_int_dist.h | 4 +-
libstdc++-v3/include/bits/valarray_array.h | 2 +-
libstdc++-v3/include/c_global/cmath | 48 +--
libstdc++-v3/include/c_std/cmath | 24 +-
libstdc++-v3/include/ext/numeric_traits.h | 18 +-
libstdc++-v3/include/std/type_traits | 284 ++++++++++++++++--
libstdc++-v3/include/tr1/cmath | 24 +-
42 files changed, 1356 insertions(+), 257 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_bounded_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_const.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_function.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_pointer.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_reference.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_scalar.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_unbounded_array.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C
create mode 100644 gcc/testsuite/g++.dg/ext/is_volatile.C
create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 01/40] c++: Sort built-in identifiers alphabetically
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 02/40] c++: Implement __is_const built-in trait Ken Matsui
` (39 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch sorts built-in identifiers alphabetically for better code
readability.
gcc/cp/ChangeLog:
* constraint.cc (diagnose_trait_expr): Sort built-in identifiers
alphabetically.
* cp-trait.def: Likewise.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
(finish_trait_type): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Sort built-in identifiers
alphabetically.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 68 ++++++++---------
gcc/cp/cp-trait.def | 10 +--
gcc/cp/semantics.cc | 94 ++++++++++++------------
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 70 +++++++++---------
4 files changed, 121 insertions(+), 121 deletions(-)
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9e4e7043cd..722fc334e6f 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3702,18 +3702,36 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_HAS_TRIVIAL_DESTRUCTOR:
inform (loc, " %qT is not trivially destructible", t1);
break;
+ case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
+ inform (loc, " %qT does not have unique object representations", t1);
+ break;
case CPTK_HAS_VIRTUAL_DESTRUCTOR:
inform (loc, " %qT does not have a virtual destructor", t1);
break;
case CPTK_IS_ABSTRACT:
inform (loc, " %qT is not an abstract class", t1);
break;
+ case CPTK_IS_AGGREGATE:
+ inform (loc, " %qT is not an aggregate", t1);
+ break;
+ case CPTK_IS_ASSIGNABLE:
+ inform (loc, " %qT is not assignable from %qT", t1, t2);
+ break;
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
+ case CPTK_IS_CONSTRUCTIBLE:
+ if (!t2)
+ inform (loc, " %qT is not default constructible", t1);
+ else
+ inform (loc, " %qT is not constructible from %qE", t1, t2);
+ break;
+ case CPTK_IS_CONVERTIBLE:
+ inform (loc, " %qT is not convertible from %qE", t2, t1);
+ break;
case CPTK_IS_EMPTY:
inform (loc, " %qT is not an empty class", t1);
break;
@@ -3729,6 +3747,18 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_NOTHROW_ASSIGNABLE:
+ inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
+ break;
+ case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
+ if (!t2)
+ inform (loc, " %qT is not nothrow default constructible", t1);
+ else
+ inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
+ break;
+ case CPTK_IS_NOTHROW_CONVERTIBLE:
+ inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
+ break;
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
inform (loc, " %qT is not pointer-interconvertible base of %qT",
t1, t2);
@@ -3748,50 +3778,20 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_TRIVIAL:
inform (loc, " %qT is not a trivial type", t1);
break;
- case CPTK_IS_UNION:
- inform (loc, " %qT is not a union", t1);
- break;
- case CPTK_IS_AGGREGATE:
- inform (loc, " %qT is not an aggregate", t1);
- break;
- case CPTK_IS_TRIVIALLY_COPYABLE:
- inform (loc, " %qT is not trivially copyable", t1);
- break;
- case CPTK_IS_ASSIGNABLE:
- inform (loc, " %qT is not assignable from %qT", t1, t2);
- break;
case CPTK_IS_TRIVIALLY_ASSIGNABLE:
inform (loc, " %qT is not trivially assignable from %qT", t1, t2);
break;
- case CPTK_IS_NOTHROW_ASSIGNABLE:
- inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
- break;
- case CPTK_IS_CONSTRUCTIBLE:
- if (!t2)
- inform (loc, " %qT is not default constructible", t1);
- else
- inform (loc, " %qT is not constructible from %qE", t1, t2);
- break;
case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
if (!t2)
inform (loc, " %qT is not trivially default constructible", t1);
else
inform (loc, " %qT is not trivially constructible from %qE", t1, t2);
break;
- case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- if (!t2)
- inform (loc, " %qT is not nothrow default constructible", t1);
- else
- inform (loc, " %qT is not nothrow constructible from %qE", t1, t2);
- break;
- case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
- inform (loc, " %qT does not have unique object representations", t1);
- break;
- case CPTK_IS_CONVERTIBLE:
- inform (loc, " %qT is not convertible from %qE", t2, t1);
+ case CPTK_IS_TRIVIALLY_COPYABLE:
+ inform (loc, " %qT is not trivially copyable", t1);
break;
- case CPTK_IS_NOTHROW_CONVERTIBLE:
- inform (loc, " %qT is not nothrow convertible from %qE", t2, t1);
+ case CPTK_IS_UNION:
+ inform (loc, " %qT is not a union", t1);
break;
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
inform (loc, " %qT is not a reference that binds to a temporary "
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 8b7fece0cc8..ce3733df641 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -84,14 +84,14 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
-/* FIXME Added space to avoid direct usage in GCC 13. */
-DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible ", 2)
-
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
-DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
-DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
+DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
+
+/* FIXME Added space to avoid direct usage in GCC 13. */
+DEFTRAIT_EXPR (IS_DEDUCIBLE, "__is_deducible ", 2)
/* These traits yield a type pack, not a type, and are represented by
cp_parser_trait as a special BASES tree instead of a TRAIT_TYPE tree. */
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 0f7f4e87ae4..92f4f3fd4f6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12075,15 +12075,6 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& classtype_has_nothrow_assign_or_copy_p (type1,
true))));
- case CPTK_HAS_TRIVIAL_ASSIGN:
- /* ??? The standard seems to be missing the "or array of such a class
- type" wording for this trait. */
- type1 = strip_array_types (type1);
- return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
- && (trivial_type_p (type1)
- || (CLASS_TYPE_P (type1)
- && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
-
case CPTK_HAS_NOTHROW_CONSTRUCTOR:
type1 = strip_array_types (type1);
return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
@@ -12092,17 +12083,26 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& maybe_instantiate_noexcept (t)
&& TYPE_NOTHROW_P (TREE_TYPE (t))));
- case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
- type1 = strip_array_types (type1);
- return (trivial_type_p (type1)
- || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
-
case CPTK_HAS_NOTHROW_COPY:
type1 = strip_array_types (type1);
return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
|| (CLASS_TYPE_P (type1)
&& classtype_has_nothrow_assign_or_copy_p (type1, false)));
+ case CPTK_HAS_TRIVIAL_ASSIGN:
+ /* ??? The standard seems to be missing the "or array of such a class
+ type" wording for this trait. */
+ type1 = strip_array_types (type1);
+ return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
+ && (trivial_type_p (type1)
+ || (CLASS_TYPE_P (type1)
+ && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
+
+ case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
+ type1 = strip_array_types (type1);
+ return (trivial_type_p (type1)
+ || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
+
case CPTK_HAS_TRIVIAL_COPY:
/* ??? The standard seems to be missing the "or array of such a class
type" wording for this trait. */
@@ -12116,18 +12116,21 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
|| (CLASS_TYPE_P (type1)
&& TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
- case CPTK_HAS_VIRTUAL_DESTRUCTOR:
- return type_has_virtual_destructor (type1);
-
case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
return type_has_unique_obj_representations (type1);
+ case CPTK_HAS_VIRTUAL_DESTRUCTOR:
+ return type_has_virtual_destructor (type1);
+
case CPTK_IS_ABSTRACT:
return ABSTRACT_CLASS_TYPE_P (type1);
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ASSIGNABLE:
+ return is_xible (MODIFY_EXPR, type1, type2);
+
case CPTK_IS_BASE_OF:
return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
@@ -12136,6 +12139,12 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
+ case CPTK_IS_CONSTRUCTIBLE:
+ return is_xible (INIT_EXPR, type1, type2);
+
+ case CPTK_IS_CONVERTIBLE:
+ return is_convertible (type1, type2);
+
case CPTK_IS_EMPTY:
return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
@@ -12151,6 +12160,15 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_NOTHROW_ASSIGNABLE:
+ return is_nothrow_xible (MODIFY_EXPR, type1, type2);
+
+ case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
+ return is_nothrow_xible (INIT_EXPR, type1, type2);
+
+ case CPTK_IS_NOTHROW_CONVERTIBLE:
+ return is_nothrow_convertible (type1, type2);
+
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
return pointer_interconvertible_base_of_p (type1, type2);
@@ -12181,24 +12199,6 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
- case CPTK_IS_ASSIGNABLE:
- return is_xible (MODIFY_EXPR, type1, type2);
-
- case CPTK_IS_CONSTRUCTIBLE:
- return is_xible (INIT_EXPR, type1, type2);
-
- case CPTK_IS_NOTHROW_ASSIGNABLE:
- return is_nothrow_xible (MODIFY_EXPR, type1, type2);
-
- case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- return is_nothrow_xible (INIT_EXPR, type1, type2);
-
- case CPTK_IS_CONVERTIBLE:
- return is_convertible (type1, type2);
-
- case CPTK_IS_NOTHROW_CONVERTIBLE:
- return is_nothrow_convertible (type1, type2);
-
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
@@ -12311,9 +12311,9 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ABSTRACT:
case CPTK_IS_EMPTY:
case CPTK_IS_POLYMORPHIC:
- case CPTK_IS_ABSTRACT:
case CPTK_HAS_VIRTUAL_DESTRUCTOR:
if (!check_trait_type (type1, /* kind = */ 3))
return error_mark_node;
@@ -12333,12 +12333,12 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
- case CPTK_IS_TRIVIALLY_ASSIGNABLE:
- case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
+ case CPTK_IS_CONVERTIBLE:
case CPTK_IS_NOTHROW_ASSIGNABLE:
case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
- case CPTK_IS_CONVERTIBLE:
case CPTK_IS_NOTHROW_CONVERTIBLE:
+ case CPTK_IS_TRIVIALLY_ASSIGNABLE:
+ case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
case CPTK_REF_CONVERTS_FROM_TEMPORARY:
if (!check_trait_type (type1)
@@ -12357,8 +12357,8 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_ENUM:
- case CPTK_IS_UNION:
case CPTK_IS_SAME:
+ case CPTK_IS_UNION:
break;
case CPTK_IS_LAYOUT_COMPATIBLE:
@@ -12421,25 +12421,25 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
switch (kind)
{
- case CPTK_UNDERLYING_TYPE:
- return finish_underlying_type (type1);
-
case CPTK_REMOVE_CV:
return cv_unqualified (type1);
- case CPTK_REMOVE_REFERENCE:
+ case CPTK_REMOVE_CVREF:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
- return type1;
+ return cv_unqualified (type1);
- case CPTK_REMOVE_CVREF:
+ case CPTK_REMOVE_REFERENCE:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
- return cv_unqualified (type1);
+ return type1;
case CPTK_TYPE_PACK_ELEMENT:
return finish_type_pack_element (type1, type2, complain);
+ case CPTK_UNDERLYING_TYPE:
+ return finish_underlying_type (type1);
+
#define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
case CPTK_##CODE:
#include "cp-trait.def"
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index f343e153e56..2223f08a628 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -8,9 +8,21 @@
#if !__has_builtin (__builtin_bit_cast)
# error "__has_builtin (__builtin_bit_cast) failed"
#endif
+#if !__has_builtin (__builtin_is_constant_evaluated)
+# error "__has_builtin (__builtin_is_constant_evaluated) failed"
+#endif
+#if !__has_builtin (__builtin_is_corresponding_member)
+# error "__has_builtin (__builtin_is_corresponding_member) failed"
+#endif
+#if !__has_builtin (__builtin_is_pointer_interconvertible_with_class)
+# error "__has_builtin (__builtin_is_pointer_interconvertible_with_class) failed"
+#endif
#if !__has_builtin (__builtin_launder)
# error "__has_builtin (__builtin_launder) failed"
#endif
+#if !__has_builtin (__builtin_source_location)
+# error "__has_builtin (__builtin_source_location) failed"
+#endif
#if !__has_builtin (__has_nothrow_assign)
# error "__has_builtin (__has_nothrow_assign) failed"
#endif
@@ -44,12 +56,21 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_assignable)
+# error "__has_builtin (__is_assignable) failed"
+#endif
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
+#if !__has_builtin (__is_constructible)
+# error "__has_builtin (__is_constructible) failed"
+#endif
+#if !__has_builtin (__is_convertible)
+# error "__has_builtin (__is_convertible) failed"
+#endif
#if !__has_builtin (__is_empty)
# error "__has_builtin (__is_empty) failed"
#endif
@@ -65,6 +86,15 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_nothrow_assignable)
+# error "__has_builtin (__is_nothrow_assignable) failed"
+#endif
+#if !__has_builtin (__is_nothrow_constructible)
+# error "__has_builtin (__is_nothrow_constructible) failed"
+#endif
+#if !__has_builtin (__is_nothrow_convertible)
+# error "__has_builtin (__is_nothrow_convertible) failed"
+#endif
#if !__has_builtin (__is_pointer_interconvertible_base_of)
# error "__has_builtin (__is_pointer_interconvertible_base_of) failed"
#endif
@@ -98,51 +128,21 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
-#if !__has_builtin (__underlying_type)
-# error "__has_builtin (__underlying_type) failed"
-#endif
-#if !__has_builtin (__is_assignable)
-# error "__has_builtin (__is_assignable) failed"
-#endif
-#if !__has_builtin (__is_constructible)
-# error "__has_builtin (__is_constructible) failed"
-#endif
-#if !__has_builtin (__is_nothrow_assignable)
-# error "__has_builtin (__is_nothrow_assignable) failed"
-#endif
-#if !__has_builtin (__is_nothrow_constructible)
-# error "__has_builtin (__is_nothrow_constructible) failed"
-#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) failed"
#endif
#if !__has_builtin (__reference_converts_from_temporary)
# error "__has_builtin (__reference_converts_from_temporary) failed"
#endif
-#if !__has_builtin (__builtin_is_constant_evaluated)
-# error "__has_builtin (__builtin_is_constant_evaluated) failed"
-#endif
-#if !__has_builtin (__builtin_source_location)
-# error "__has_builtin (__builtin_source_location) failed"
-#endif
-#if !__has_builtin (__builtin_is_corresponding_member)
-# error "__has_builtin (__builtin_is_corresponding_member) failed"
-#endif
-#if !__has_builtin (__builtin_is_pointer_interconvertible_with_class)
-# error "__has_builtin (__builtin_is_pointer_interconvertible_with_class) failed"
-#endif
-#if !__has_builtin (__is_convertible)
-# error "__has_builtin (__is_convertible) failed"
-#endif
-#if !__has_builtin (__is_nothrow_convertible)
-# error "__has_builtin (__is_nothrow_convertible) failed"
-#endif
#if !__has_builtin (__remove_cv)
# error "__has_builtin (__remove_cv) failed"
#endif
+#if !__has_builtin (__remove_cvref)
+# error "__has_builtin (__remove_cvref) failed"
+#endif
#if !__has_builtin (__remove_reference)
# error "__has_builtin (__remove_reference) failed"
#endif
-#if !__has_builtin (__remove_cvref)
-# error "__has_builtin (__remove_cvref) failed"
+#if !__has_builtin (__underlying_type)
+# error "__has_builtin (__underlying_type) failed"
#endif
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 02/40] c++: Implement __is_const built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
2023-09-15 2:34 ` [PATCH v13 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 03/40] libstdc++: Optimize is_const trait performance Ken Matsui
` (38 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_const.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_const.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_CONST.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_const.
* g++.dg/ext/is_const.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_const.C | 19 +++++++++++++++++++
5 files changed, 30 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_const.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 722fc334e6f..567dd35fe0a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3723,6 +3723,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
+ case CPTK_IS_CONST:
+ inform (loc, " %qT is not a const type", t1);
+ break;
case CPTK_IS_CONSTRUCTIBLE:
if (!t2)
inform (loc, " %qT is not default constructible", t1);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index ce3733df641..a4ebfd9f319 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
+DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 92f4f3fd4f6..17d6e6728f9 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12139,6 +12139,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
+ case CPTK_IS_CONST:
+ return CP_TYPE_CONST_P (type1);
+
case CPTK_IS_CONSTRUCTIBLE:
return is_xible (INIT_EXPR, type1, type2);
@@ -12356,6 +12359,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;
case CPTK_IS_CLASS:
+ case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 2223f08a628..e6e481b13c5 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -65,6 +65,9 @@
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
+#if !__has_builtin (__is_const)
+# error "__has_builtin (__is_const) failed"
+#endif
#if !__has_builtin (__is_constructible)
# error "__has_builtin (__is_constructible) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_const.C b/gcc/testsuite/g++.dg/ext/is_const.C
new file mode 100644
index 00000000000..8f2d7c2fce9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_const.C
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+SA(__is_const(const int));
+SA(__is_const(const volatile int));
+SA(__is_const(cClassType));
+SA(__is_const(cvClassType));
+
+// Negative tests.
+SA(!__is_const(int));
+SA(!__is_const(volatile int));
+SA(!__is_const(ClassType));
+SA(!__is_const(vClassType));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 03/40] libstdc++: Optimize is_const trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
2023-09-15 2:34 ` [PATCH v13 01/40] c++: Sort built-in identifiers alphabetically Ken Matsui
2023-09-15 2:34 ` [PATCH v13 02/40] c++: Implement __is_const built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 04/40] c++: Implement __is_volatile built-in trait Ken Matsui
` (37 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_const trait by dispatching to
the new __is_const built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_const): Use __is_const built-in trait.
(is_const_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 677cd934b94..686e38e47c3 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -784,6 +784,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Type properties.
/// is_const
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
+ template<typename _Tp>
+ struct is_const
+ : public __bool_constant<__is_const(_Tp)>
+ { };
+#else
template<typename>
struct is_const
: public false_type { };
@@ -791,6 +797,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_const<_Tp const>
: public true_type { };
+#endif
/// is_volatile
template<typename>
@@ -3218,10 +3225,17 @@ template <typename _Tp>
inline constexpr bool is_compound_v = is_compound<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_member_pointer_v = is_member_pointer<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
+template <typename _Tp>
+ inline constexpr bool is_const_v = __is_const(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_const_v = false;
template <typename _Tp>
inline constexpr bool is_const_v<const _Tp> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_volatile_v = false;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 04/40] c++: Implement __is_volatile built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (2 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 03/40] libstdc++: Optimize is_const trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 05/40] libstdc++: Optimize is_volatile trait performance Ken Matsui
` (36 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_volatile.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_volatile.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_VOLATILE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_volatile.
* g++.dg/ext/is_volatile.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_volatile.C | 19 +++++++++++++++++++
5 files changed, 30 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_volatile.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 567dd35fe0a..f031e022541 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3796,6 +3796,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
+ case CPTK_IS_VOLATILE:
+ inform (loc, " %qT is not a volatile type", t1);
+ break;
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
inform (loc, " %qT is not a reference that binds to a temporary "
"object of type %qT (direct-initialization)", t1, t2);
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index a4ebfd9f319..60462cd9874 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -83,6 +83,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 17d6e6728f9..647124265a6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12202,6 +12202,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
+ case CPTK_IS_VOLATILE:
+ return CP_TYPE_VOLATILE_P (type1);
+
case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
@@ -12363,6 +12366,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
case CPTK_IS_UNION:
+ case CPTK_IS_VOLATILE:
break;
case CPTK_IS_LAYOUT_COMPATIBLE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index e6e481b13c5..fb03dd20e84 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -131,6 +131,9 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
+#if !__has_builtin (__is_volatile)
+# error "__has_builtin (__is_volatile) failed"
+#endif
#if !__has_builtin (__reference_constructs_from_temporary)
# error "__has_builtin (__reference_constructs_from_temporary) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_volatile.C b/gcc/testsuite/g++.dg/ext/is_volatile.C
new file mode 100644
index 00000000000..004e397e5e7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_volatile.C
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+// Positive tests.
+SA(__is_volatile(volatile int));
+SA(__is_volatile(const volatile int));
+SA(__is_volatile(vClassType));
+SA(__is_volatile(cvClassType));
+
+// Negative tests.
+SA(!__is_volatile(int));
+SA(!__is_volatile(const int));
+SA(!__is_volatile(ClassType));
+SA(!__is_volatile(cClassType));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 05/40] libstdc++: Optimize is_volatile trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (3 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 04/40] c++: Implement __is_volatile built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 06/40] c++: Implement __is_array built-in trait Ken Matsui
` (35 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_volatile trait by dispatching
to the new __is_volatile built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_volatile): Use __is_volatile built-in
trait.
(is_volatile_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 686e38e47c3..c01f65df22b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -800,6 +800,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_volatile
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_volatile)
+ template<typename _Tp>
+ struct is_volatile
+ : public __bool_constant<__is_volatile(_Tp)>
+ { };
+#else
template<typename>
struct is_volatile
: public false_type { };
@@ -807,6 +813,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_volatile<_Tp volatile>
: public true_type { };
+#endif
/// is_trivial
template<typename _Tp>
@@ -3236,10 +3243,15 @@ template <typename _Tp>
inline constexpr bool is_const_v<const _Tp> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_volatile)
+template <typename _Tp>
+ inline constexpr bool is_volatile_v = __is_volatile(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_volatile_v = false;
template <typename _Tp>
inline constexpr bool is_volatile_v<volatile _Tp> = true;
+#endif
template <typename _Tp>
inline constexpr bool is_trivial_v = __is_trivial(_Tp);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 06/40] c++: Implement __is_array built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (4 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 05/40] libstdc++: Optimize is_volatile trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 07/40] libstdc++: Optimize is_array trait performance Ken Matsui
` (34 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_array.
* g++.dg/ext/is_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_array.C | 28 ++++++++++++++++++++++++
5 files changed, 39 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index f031e022541..5e30a4a907a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_AGGREGATE:
inform (loc, " %qT is not an aggregate", t1);
break;
+ case CPTK_IS_ARRAY:
+ inform (loc, " %qT is not an array", t1);
+ break;
case CPTK_IS_ASSIGNABLE:
inform (loc, " %qT is not assignable from %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 60462cd9874..c9106242bc8 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 647124265a6..8d5d443d9a9 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12128,6 +12128,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ARRAY:
+ return type_code1 == ARRAY_TYPE;
+
case CPTK_IS_ASSIGNABLE:
return is_xible (MODIFY_EXPR, type1, type2);
@@ -12361,6 +12364,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index fb03dd20e84..645cabe088e 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_array)
+# error "__has_builtin (__is_array) failed"
+#endif
#if !__has_builtin (__is_assignable)
# error "__has_builtin (__is_assignable) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_array.C b/gcc/testsuite/g++.dg/ext/is_array.C
new file mode 100644
index 00000000000..facfed5c7cb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_array.C
@@ -0,0 +1,28 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_array, int[2], true);
+SA_TEST_CATEGORY(__is_array, int[], true);
+SA_TEST_CATEGORY(__is_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_array, int[][3], true);
+SA_TEST_CATEGORY(__is_array, float*[2], true);
+SA_TEST_CATEGORY(__is_array, float*[], true);
+SA_TEST_CATEGORY(__is_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_array, ClassType[][3], true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_array, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 07/40] libstdc++: Optimize is_array trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (5 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 06/40] c++: Implement __is_array built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 08/40] c++: Implement __is_unbounded_array built-in trait Ken Matsui
` (33 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_array trait by dispatching to
the new __is_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_array): Use __is_array built-in trait.
(is_array_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index c01f65df22b..4e8165e5af5 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -523,6 +523,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_array
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_array)
+ template<typename _Tp>
+ struct is_array
+ : public __bool_constant<__is_array(_Tp)>
+ { };
+#else
template<typename>
struct is_array
: public false_type { };
@@ -534,6 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_array<_Tp[]>
: public true_type { };
+#endif
template<typename>
struct __is_pointer_helper
@@ -3183,12 +3190,17 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_floating_point_v = is_floating_point<_Tp>::value;
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_array)
+template <typename _Tp>
+ inline constexpr bool is_array_v = __is_array(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_array_v = false;
template <typename _Tp>
inline constexpr bool is_array_v<_Tp[]> = true;
template <typename _Tp, size_t _Num>
inline constexpr bool is_array_v<_Tp[_Num]> = true;
+#endif
template <typename _Tp>
inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 08/40] c++: Implement __is_unbounded_array built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (6 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 07/40] libstdc++: Optimize is_array trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 09/40] libstdc++: Optimize is_unbounded_array trait performance Ken Matsui
` (32 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_unbounded_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_unbounded_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNBOUNDED_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_unbounded_array.
* g++.dg/ext/is_unbounded_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_unbounded_array.C | 37 +++++++++++++++++++
5 files changed, 48 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_unbounded_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 5e30a4a907a..751ac61b25a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3796,6 +3796,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_TRIVIALLY_COPYABLE:
inform (loc, " %qT is not trivially copyable", t1);
break;
+ case CPTK_IS_UNBOUNDED_ARRAY:
+ inform (loc, " %qT is not an unbounded array", t1);
+ break;
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index c9106242bc8..1e67a3d2089 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -83,6 +83,7 @@ DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
+DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 8d5d443d9a9..fd7bdf7a293 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12202,6 +12202,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_TRIVIALLY_COPYABLE:
return trivially_copyable_p (type1);
+ case CPTK_IS_UNBOUNDED_ARRAY:
+ return array_of_unknown_bound_p (type1);
+
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
@@ -12369,6 +12372,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
+ case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
case CPTK_IS_VOLATILE:
break;
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 645cabe088e..90997210c12 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -131,6 +131,9 @@
#if !__has_builtin (__is_trivially_copyable)
# error "__has_builtin (__is_trivially_copyable) failed"
#endif
+#if !__has_builtin (__is_unbounded_array)
+# error "__has_builtin (__is_unbounded_array) failed"
+#endif
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_unbounded_array.C b/gcc/testsuite/g++.dg/ext/is_unbounded_array.C
new file mode 100644
index 00000000000..1307d24f5a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_unbounded_array.C
@@ -0,0 +1,37 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_unbounded_array, int[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, int[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, float*[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[], true);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass[2][3], false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass[][3], true);
+SA_TEST_CATEGORY(__is_unbounded_array, int(*)[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(*)[], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(&)[2], false);
+SA_TEST_CATEGORY(__is_unbounded_array, int(&)[], false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_unbounded_array, ClassType, false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteClass, false);
+SA_TEST_CATEGORY(__is_unbounded_array, IncompleteUnion, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 09/40] libstdc++: Optimize is_unbounded_array trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (7 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 08/40] c++: Implement __is_unbounded_array built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 10/40] c++: Implement __is_bounded_array built-in trait Ken Matsui
` (31 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_unbounded_array trait by
dispatching to the new __is_unbounded_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_unbounded_array_v): Use
__is_unbounded_array built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 4e8165e5af5..cb3d9e238fa 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3541,11 +3541,16 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True for a type that is an array of unknown bound.
/// @ingroup variable_templates
/// @since C++20
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unbounded_array)
+ template<typename _Tp>
+ inline constexpr bool is_unbounded_array_v = __is_unbounded_array(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_unbounded_array_v = false;
template<typename _Tp>
inline constexpr bool is_unbounded_array_v<_Tp[]> = true;
+# endif
/// True for a type that is an array of known bound.
/// @since C++20
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 10/40] c++: Implement __is_bounded_array built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (8 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 09/40] libstdc++: Optimize is_unbounded_array trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 11/40] libstdc++: Optimize is_bounded_array trait performance Ken Matsui
` (30 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_bounded_array.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_bounded_array.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_BOUNDED_ARRAY.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_bounded_array.
* g++.dg/ext/is_bounded_array.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_bounded_array.C | 38 +++++++++++++++++++++
5 files changed, 49 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_bounded_array.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 751ac61b25a..d09252a56b6 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3723,6 +3723,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
+ case CPTK_IS_BOUNDED_ARRAY:
+ inform (loc, " %qT is not a bounded array", t1);
+ break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 1e67a3d2089..b6146c010f6 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -62,6 +62,7 @@ DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
+DEFTRAIT_EXPR (IS_BOUNDED_ARRAY, "__is_bounded_array", 1)
DEFTRAIT_EXPR (IS_CLASS, "__is_class", 1)
DEFTRAIT_EXPR (IS_CONST, "__is_const", 1)
DEFTRAIT_EXPR (IS_CONSTRUCTIBLE, "__is_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index fd7bdf7a293..605cf03c18d 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12139,6 +12139,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
&& (same_type_ignoring_top_level_qualifiers_p (type1, type2)
|| DERIVED_FROM_P (type1, type2)));
+ case CPTK_IS_BOUNDED_ARRAY:
+ return type_code1 == ARRAY_TYPE && TYPE_DOMAIN (type1);
+
case CPTK_IS_CLASS:
return NON_UNION_CLASS_TYPE_P (type1);
@@ -12368,6 +12371,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
break;
case CPTK_IS_ARRAY:
+ case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 90997210c12..4142da518b1 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -65,6 +65,9 @@
#if !__has_builtin (__is_base_of)
# error "__has_builtin (__is_base_of) failed"
#endif
+#if !__has_builtin (__is_bounded_array)
+# error "__has_builtin (__is_bounded_array) failed"
+#endif
#if !__has_builtin (__is_class)
# error "__has_builtin (__is_class) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_bounded_array.C b/gcc/testsuite/g++.dg/ext/is_bounded_array.C
new file mode 100644
index 00000000000..346790eba12
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_bounded_array.C
@@ -0,0 +1,38 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CONST(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_bounded_array, int[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, int[], false);
+SA_TEST_CATEGORY(__is_bounded_array, int[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, int[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, float*[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, float*[], false);
+SA_TEST_CATEGORY(__is_bounded_array, float*[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, float*[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[2], true);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[], false);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[2][3], true);
+SA_TEST_CATEGORY(__is_bounded_array, ClassType[][3], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(*)[2], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(*)[], false);
+SA_TEST_CATEGORY(__is_bounded_array, int(&)[2], false);
+SA_TEST_CONST(__is_bounded_array, int(&)[], false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_bounded_array, ClassType, false);
+SA_TEST_CONST(__is_bounded_array, void(), false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 11/40] libstdc++: Optimize is_bounded_array trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (9 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 10/40] c++: Implement __is_bounded_array built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 12/40] c++: Implement __is_scoped_enum built-in trait Ken Matsui
` (29 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_bounded_array trait by
dispatching to the new __is_bounded_array built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_bounded_array_v): Use __is_bounded_array
built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index cb3d9e238fa..d306073a797 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3532,11 +3532,16 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True for a type that is an array of known bound.
/// @ingroup variable_templates
/// @since C++20
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_bounded_array)
+ template<typename _Tp>
+ inline constexpr bool is_bounded_array_v = __is_bounded_array(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_bounded_array_v = false;
template<typename _Tp, size_t _Size>
inline constexpr bool is_bounded_array_v<_Tp[_Size]> = true;
+# endif
/// True for a type that is an array of unknown bound.
/// @ingroup variable_templates
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 12/40] c++: Implement __is_scoped_enum built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (10 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 11/40] libstdc++: Optimize is_bounded_array trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 13/40] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui
` (28 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_scoped_enum.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_scoped_enum.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCOPED_ENUM.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_scoped_enum.
* g++.dg/ext/is_scoped_enum.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +
gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++++++++++++++++++++
5 files changed, 78 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d09252a56b6..1c0b2e0f178 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3781,6 +3781,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SCOPED_ENUM:
+ inform (loc, " %qT is not a scoped enum", t1);
+ break;
case CPTK_IS_STD_LAYOUT:
inform (loc, " %qT is not an standard layout type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index b6146c010f6..047307c95ce 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -79,6 +79,7 @@ DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertib
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 605cf03c18d..c971c34cf6f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12190,6 +12190,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SCOPED_ENUM:
+ return SCOPED_ENUM_P (type1);
+
case CPTK_IS_STD_LAYOUT:
return std_layout_type_p (type1);
@@ -12376,6 +12379,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_SAME:
+ case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
case CPTK_IS_VOLATILE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4142da518b1..ba97beea3c3 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -119,6 +119,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_scoped_enum)
+# error "__has_builtin (__is_scoped_enum) failed"
+#endif
#if !__has_builtin (__is_standard_layout)
# error "__has_builtin (__is_standard_layout) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_scoped_enum.C b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C
new file mode 100644
index 00000000000..a563b6ee67d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C
@@ -0,0 +1,67 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT);
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+enum class E { e1, e2 };
+SA_TEST_CATEGORY(__is_scoped_enum, E, true);
+enum class Ec : char { e1, e2 };
+SA_TEST_CATEGORY(__is_scoped_enum, Ec, true);
+
+// negative tests
+enum U { u1, u2 };
+SA_TEST_CATEGORY(__is_scoped_enum, U, false);
+enum F : int { f1, f2 };
+SA_TEST_CATEGORY(__is_scoped_enum, F, false);
+struct S;
+SA_TEST_CATEGORY(__is_scoped_enum, S, false);
+struct S { };
+SA_TEST_CATEGORY(__is_scoped_enum, S, false);
+
+SA_TEST_CATEGORY(__is_scoped_enum, int, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[2], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[][2], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int[2][3], false);
+SA_TEST_CATEGORY(__is_scoped_enum, int*, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int&, false);
+SA_TEST_CATEGORY(__is_scoped_enum, int*&, false);
+SA_TEST_FN(__is_scoped_enum, int(), false);
+SA_TEST_FN(__is_scoped_enum, int(*)(), false);
+SA_TEST_FN(__is_scoped_enum, int(&)(), false);
+
+enum opaque_unscoped : short;
+enum class opaque_scoped;
+enum class opaque_scoped_with_base : long;
+
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_unscoped, false);
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped, true);
+SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped_with_base, true);
+
+enum unscoped {
+ u_is_scoped = __is_scoped_enum(unscoped),
+};
+SA( ! unscoped::u_is_scoped );
+
+enum unscoped_fixed : char {
+ uf_is_scoped = __is_scoped_enum(unscoped_fixed),
+};
+SA( ! unscoped_fixed::uf_is_scoped );
+
+enum class scoped {
+ is_scoped = __is_scoped_enum(scoped),
+};
+SA( (bool) scoped::is_scoped );
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 13/40] libstdc++: Optimize is_scoped_enum trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (11 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 12/40] c++: Implement __is_scoped_enum built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 14/40] c++: Implement __is_member_pointer built-in trait Ken Matsui
` (27 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_scoped_enum trait
by dispatching to the new __is_scoped_enum built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_scoped_enum): Use
__is_scoped_enum built-in trait.
(is_scoped_enum_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index d306073a797..7fd29d8d9f2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -3633,6 +3633,12 @@ template<typename _Ret, typename _Fn, typename... _Args>
/// True if the type is a scoped enumeration type.
/// @since C++23
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum)
+ template<typename _Tp>
+ struct is_scoped_enum
+ : bool_constant<__is_scoped_enum(_Tp)>
+ { };
+# else
template<typename _Tp>
struct is_scoped_enum
: false_type
@@ -3644,11 +3650,17 @@ template<typename _Ret, typename _Fn, typename... _Args>
struct is_scoped_enum<_Tp>
: bool_constant<!requires(_Tp __t, void(*__f)(int)) { __f(__t); }>
{ };
+# endif
/// @ingroup variable_templates
/// @since C++23
+# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum)
+ template<typename _Tp>
+ inline constexpr bool is_scoped_enum_v = __is_scoped_enum(_Tp);
+# else
template<typename _Tp>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<_Tp>::value;
+# endif
#endif
#ifdef __cpp_lib_reference_from_temporary // C++ >= 23 && ref_{converts,constructs}_from_temp
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 14/40] c++: Implement __is_member_pointer built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (12 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 13/40] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 15/40] libstdc++: Optimize is_member_pointer trait performance Ken Matsui
` (26 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_pointer.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_MEMBER_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_member_pointer.
* g++.dg/ext/is_member_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_member_pointer.C | 30 ++++++++++++++++++++
5 files changed, 41 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 1c0b2e0f178..f0d3f89464c 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3756,6 +3756,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_POINTER:
+ inform (loc, " %qT is not a member pointer", t1);
+ break;
case CPTK_IS_NOTHROW_ASSIGNABLE:
inform (loc, " %qT is not nothrow assignable from %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 047307c95ce..7fed3483221 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -72,6 +72,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index c971c34cf6f..7091e581ac7 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12169,6 +12169,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_POINTER:
+ return TYPE_PTRMEM_P (type1);
+
case CPTK_IS_NOTHROW_ASSIGNABLE:
return is_nothrow_xible (MODIFY_EXPR, type1, type2);
@@ -12378,6 +12381,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index ba97beea3c3..994873f14e9 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -95,6 +95,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_pointer)
+# error "__has_builtin (__is_member_pointer) failed"
+#endif
#if !__has_builtin (__is_nothrow_assignable)
# error "__has_builtin (__is_nothrow_assignable) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_pointer.C
new file mode 100644
index 00000000000..7ee2e3ab90c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_pointer.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_NON_VOLATILE(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_member_pointer, int (ClassType::*), true);
+SA_TEST_CATEGORY(__is_member_pointer, ClassType (ClassType::*), true);
+
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(int), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(int) const, true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, int (ClassType::*)(float, ...), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer, ClassType (ClassType::*)(ClassType), true);
+SA_TEST_NON_VOLATILE(__is_member_pointer,
+ float (ClassType::*)(int, float, int[], int&), true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 15/40] libstdc++: Optimize is_member_pointer trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (13 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 14/40] c++: Implement __is_member_pointer built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
` (25 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_pointer trait
by dispatching to the new __is_member_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_pointer): Use __is_member_pointer
built-in trait.
(is_member_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 7fd29d8d9f2..d7f89cf7c06 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -716,6 +716,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_compound
: public __not_<is_fundamental<_Tp>>::type { };
+ /// is_member_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
+ template<typename _Tp>
+ struct is_member_pointer
+ : public __bool_constant<__is_member_pointer(_Tp)>
+ { };
+#else
/// @cond undocumented
template<typename _Tp>
struct __is_member_pointer_helper
@@ -726,11 +733,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public true_type { };
/// @endcond
- /// is_member_pointer
template<typename _Tp>
struct is_member_pointer
: public __is_member_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
template<typename, typename>
struct is_same;
@@ -3242,8 +3249,14 @@ template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_compound_v = is_compound<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_pointer_v = __is_member_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_pointer_v = is_member_pointer<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_const)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (14 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 15/40] libstdc++: Optimize is_member_pointer trait performance Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 23:50 ` [PATCH v14 00/40] Optimize type traits performance Ken Matsui
2023-09-15 2:34 ` [PATCH v13 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly Ken Matsui
` (24 subsequent siblings)
40 siblings, 1 reply; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
Now that RID_MAX has reached 255, we need to update the bit sizes of every
use of the enum rid from 8 to 16 to support more keywords.
For struct token_indent_info, the 8-bit increase does not change the overall
struct size because the 8-bit just consumes 1 byte from 2 bytes of external
fragmentation. Since reordering the fields just changes 1 byte of internal
fragmentation to 1 byte of external fragmentation, I keep the original field
order.
For struct c_token, the 8-bit expansion increased the overall struct size from
24 bytes to 32 bytes. The original struct takes 4 bytes of internal
fragmentation (after the location field) and 3 bytes of external
fragmentation. Keeping the original order with the 8-bit expansion gives
7 bytes of internal fragmentation (3 bytes after the pragma_kind field + 4
bytes after the location field) and 7 bytes of external fragmentation. Since
the original field order was not optimal, reordering the fields results in the
same overall size as the original one. I updated the field order to the most
efficient order.
For struct cp_token, reordering the fields only minimizes internal
fragmentation and does not minimize the overall struct size. I keep the
original field order. The original struct size was 16 bytes with 3 bits of
internal fragmentation. With this 8-bit update, the overall size would be
24 bytes. Since there is no external fragmentation and 7 bytes + 3 bits of
internal fragmentation, reordering the fields does not minimize the overall
size. I keep the orignal field order.
Suppose a pointer takes 8 bytes and int takes 4 bytes. Then, struct
ht_identifier takes 16 bytes, and union _cpp_hashnode_value takes 8 bytes.
For struct cpp_hashnode, the 8-bit increase consumes 1 more byte, resulting in
33 bytes except for paddings. The original overall size before the 8-bit
increase was 32 bytes. However, due to fragmentation, the overall struct size
would be 40 bytes. Since there is no external fragmentation and 3 bytes + 5
bits of internal fragmentation, reordering the fields does not minimize the
overall size. I keep the original field order.
gcc/c-family/ChangeLog:
* c-indentation.h (struct token_indent_info): Make keyword 16 bits.
gcc/c/ChangeLog:
* c-parser.cc (c_parse_init): Handle RID_MAX not to exceed the max
value of 16 bits.
* c-parser.h (struct c_token): Make keyword 16 bits. Reorder the
fields to minimize memory fragmentation.
gcc/cp/ChangeLog:
* parser.h (struct cp_token): Make keyword 16 bits.
(struct cp_lexer): Make saved_keyword 16 bits.
libcpp/ChangeLog:
* include/cpplib.h (struct cpp_hashnode): Make rid_code 16 bits.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/c-family/c-indentation.h | 2 +-
gcc/c/c-parser.cc | 6 +++---
gcc/c/c-parser.h | 14 +++++++-------
gcc/cp/parser.h | 8 +++++---
libcpp/include/cpplib.h | 7 +++++--
5 files changed, 21 insertions(+), 16 deletions(-)
diff --git a/gcc/c-family/c-indentation.h b/gcc/c-family/c-indentation.h
index c0e07bf49f1..6d2b88f01a3 100644
--- a/gcc/c-family/c-indentation.h
+++ b/gcc/c-family/c-indentation.h
@@ -26,7 +26,7 @@ struct token_indent_info
{
location_t location;
ENUM_BITFIELD (cpp_ttype) type : 8;
- ENUM_BITFIELD (rid) keyword : 8;
+ ENUM_BITFIELD (rid) keyword : 16;
};
/* Extract token information from TOKEN, which ought to either be a
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index b9a1b75ca43..2086f253923 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -115,9 +115,9 @@ c_parse_init (void)
tree id;
int mask = 0;
- /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
- the c_token structure. */
- gcc_assert (RID_MAX <= 255);
+ /* Make sure RID_MAX hasn't grown past the 16 bits used to hold the keyword
+ in the c_token structure. */
+ gcc_assert (RID_MAX <= 65535);
mask |= D_CXXONLY;
if (!flag_isoc99)
diff --git a/gcc/c/c-parser.h b/gcc/c/c-parser.h
index 545f0f4d9eb..6a9bd22a793 100644
--- a/gcc/c/c-parser.h
+++ b/gcc/c/c-parser.h
@@ -51,21 +51,21 @@ enum c_id_kind {
/* A single C token after string literal concatenation and conversion
of preprocessing tokens to tokens. */
struct GTY (()) c_token {
+ /* The value associated with this token, if any. */
+ tree value;
+ /* The location at which this token was found. */
+ location_t location;
+ /* If this token is a keyword, this value indicates which keyword.
+ Otherwise, this value is RID_MAX. */
+ ENUM_BITFIELD (rid) keyword : 16;
/* The kind of token. */
ENUM_BITFIELD (cpp_ttype) type : 8;
/* If this token is a CPP_NAME, this value indicates whether also
declared as some kind of type. Otherwise, it is C_ID_NONE. */
ENUM_BITFIELD (c_id_kind) id_kind : 8;
- /* If this token is a keyword, this value indicates which keyword.
- Otherwise, this value is RID_MAX. */
- ENUM_BITFIELD (rid) keyword : 8;
/* If this token is a CPP_PRAGMA, this indicates the pragma that
was seen. Otherwise it is PRAGMA_NONE. */
ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
- /* The location at which this token was found. */
- location_t location;
- /* The value associated with this token, if any. */
- tree value;
/* Token flags. */
unsigned char flags;
diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
index 6cbb9a8e031..7aa251d11b1 100644
--- a/gcc/cp/parser.h
+++ b/gcc/cp/parser.h
@@ -44,7 +44,7 @@ struct GTY (()) cp_token {
enum cpp_ttype type : 8;
/* If this token is a keyword, this value indicates which keyword.
Otherwise, this value is RID_MAX. */
- enum rid keyword : 8;
+ enum rid keyword : 16;
/* Token flags. */
unsigned char flags;
/* True if this token is from a context where it is implicitly extern "C" */
@@ -59,7 +59,9 @@ struct GTY (()) cp_token {
bool purged_p : 1;
bool tree_check_p : 1;
bool main_source_p : 1;
- /* 3 unused bits. */
+ /* These booleans use 5 bits within 1 byte, resulting in 3 unused bits.
+ Since there would be 3 bytes of internal fragmentation to the location
+ field, the total unused bits would be 27 (= 3 + 24). */
/* The location at which this token was found. */
location_t location;
@@ -102,7 +104,7 @@ struct GTY (()) cp_lexer {
/* Saved pieces of end token we replaced with the eof token. */
enum cpp_ttype saved_type : 8;
- enum rid saved_keyword : 8;
+ enum rid saved_keyword : 16;
/* The next lexer in a linked list of lexers. */
struct cp_lexer *next;
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index fcdaf082b09..7c37b861a77 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -988,11 +988,14 @@ struct GTY(()) cpp_hashnode {
unsigned int directive_index : 7; /* If is_directive,
then index into directive table.
Otherwise, a NODE_OPERATOR. */
- unsigned int rid_code : 8; /* Rid code - for front ends. */
+ unsigned int rid_code : 16; /* Rid code - for front ends. */
unsigned int flags : 9; /* CPP flags. */
ENUM_BITFIELD(node_type) type : 2; /* CPP node type. */
- /* 5 bits spare. */
+ /* These bitfields use 35 bits (= 1 + 7 + 16 + 9 + 2). The exceeded 3 bits
+ in terms of bytes leave 5 unused bits within 1 byte. Since there would
+ be 3 bytes of internal fragmentation to the deferred field, the total
+ unused bits would be 29 (= 5 + 24). */
/* The deferred cookie is applicable to NT_USER_MACRO or NT_VOID.
The latter for when a macro had a prevailing undef.
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (15 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 16/40] c, c++: Use 16 bits for all use of enum rid for more keyword space Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 18/40] c++: Implement __is_member_function_pointer built-in trait Ken Matsui
` (23 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui, Andrew Pinski
This patch fixes incorrect handling for the new 16-bit rid code. Unsigned
char was previously used for the 8-bit rid code, but unsigned short is now
required.
gcc/c-family/ChangeLog:
* c-common.h (C_SET_RID_CODE): Use unsigned short instead of
unsigned char.
Ref: Initial discussion: https://gcc.gnu.org/pipermail/gcc/2023-September/242460.html
Code provided by Andrew: https://gcc.gnu.org/pipermail/gcc/2023-September/242461.html
Co-authored-by: Andrew Pinski <pinskia@gmail.com>
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/c-family/c-common.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 1fdba7ef3ea..73bc23fa49f 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -382,7 +382,7 @@ enum c_tree_index
#define C_RID_CODE(id) \
((enum rid) (((struct c_common_identifier *) (id))->node.rid_code))
#define C_SET_RID_CODE(id, code) \
- (((struct c_common_identifier *) (id))->node.rid_code = (unsigned char) code)
+ (((struct c_common_identifier *) (id))->node.rid_code = (unsigned short) code)
/* Identifier part common to the C front ends. Inherits from
tree_identifier, despite appearances. */
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 18/40] c++: Implement __is_member_function_pointer built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (16 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 17/40] c-family: Fix C_SET_RID_CODE to handle 16-bit rid code correctly Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:34 ` [PATCH v13 19/40] libstdc++: Optimize is_member_function_pointer trait performance Ken Matsui
` (22 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_function_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_function_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_FUNCTION_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of
__is_member_function_pointer.
* g++.dg/ext/is_member_function_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
.../g++.dg/ext/is_member_function_pointer.C | 31 +++++++++++++++++++
5 files changed, 42 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index f0d3f89464c..d0464dd4f6a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3756,6 +3756,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ inform (loc, " %qT is not a member function pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 7fed3483221..6ebe3984d17 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -72,6 +72,7 @@ DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
+DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7091e581ac7..93e166923b8 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12169,6 +12169,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_LITERAL_TYPE:
return literal_type_p (type1);
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ return TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12381,6 +12384,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 994873f14e9..0dfe957474b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -95,6 +95,9 @@
#if !__has_builtin (__is_literal_type)
# error "__has_builtin (__is_literal_type) failed"
#endif
+#if !__has_builtin (__is_member_function_pointer)
+# error "__has_builtin (__is_member_function_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
new file mode 100644
index 00000000000..555123e8f07
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_function_pointer.C
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT);
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int), true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (int) const, true);
+SA_TEST_FN(__is_member_function_pointer, int (ClassType::*) (float, ...), true);
+SA_TEST_FN(__is_member_function_pointer, ClassType (ClassType::*) (ClassType), true);
+SA_TEST_FN(__is_member_function_pointer, float (ClassType::*) (int, float, int[], int&), true);
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_member_function_pointer, int (ClassType::*), false);
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType (ClassType::*), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_function_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 19/40] libstdc++: Optimize is_member_function_pointer trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (17 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 18/40] c++: Implement __is_member_function_pointer built-in trait Ken Matsui
@ 2023-09-15 2:34 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 20/40] c++: Implement __is_member_object_pointer built-in trait Ken Matsui
` (21 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:34 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_function_pointer trait
by dispatching to the new __is_member_function_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_function_pointer): Use
__is_member_function_pointer built-in trait.
(is_member_function_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index d7f89cf7c06..e1b10240dc2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -588,6 +588,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __is_member_object_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
+ /// is_member_function_pointer
+ template<typename _Tp>
+ struct is_member_function_pointer
+ : public __bool_constant<__is_member_function_pointer(_Tp)>
+ { };
+#else
template<typename>
struct __is_member_function_pointer_helper
: public false_type { };
@@ -601,6 +608,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_member_function_pointer
: public __is_member_function_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
/// is_enum
template<typename _Tp>
@@ -3222,9 +3230,17 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_member_object_pointer_v =
is_member_object_pointer<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_function_pointer_v =
+ __is_member_function_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_function_pointer_v =
is_member_function_pointer<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_enum_v = __is_enum(_Tp);
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 20/40] c++: Implement __is_member_object_pointer built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (18 preceding siblings ...)
2023-09-15 2:34 ` [PATCH v13 19/40] libstdc++: Optimize is_member_function_pointer trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 21/40] libstdc++: Optimize is_member_object_pointer trait performance Ken Matsui
` (20 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_member_object_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_member_object_pointer.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_MEMBER_OBJECT_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of
__is_member_object_pointer.
* g++.dg/ext/is_member_object_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
.../g++.dg/ext/is_member_object_pointer.C | 30 +++++++++++++++++++
5 files changed, 41 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index d0464dd4f6a..98b1f004a68 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3759,6 +3759,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
inform (loc, " %qT is not a member function pointer", t1);
break;
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
+ inform (loc, " %qT is not a member object pointer", t1);
+ break;
case CPTK_IS_MEMBER_POINTER:
inform (loc, " %qT is not a member pointer", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 6ebe3984d17..47649150ab5 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -73,6 +73,7 @@ DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
+DEFTRAIT_EXPR (IS_MEMBER_OBJECT_POINTER, "__is_member_object_pointer", 1)
DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 93e166923b8..95b25c1348b 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12172,6 +12172,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
return TYPE_PTRMEMFUNC_P (type1);
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
+ return TYPE_PTRMEM_P (type1) && !TYPE_PTRMEMFUNC_P (type1);
+
case CPTK_IS_MEMBER_POINTER:
return TYPE_PTRMEM_P (type1);
@@ -12385,6 +12388,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
case CPTK_IS_MEMBER_FUNCTION_POINTER:
+ case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 0dfe957474b..8d9cdc528cd 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -98,6 +98,9 @@
#if !__has_builtin (__is_member_function_pointer)
# error "__has_builtin (__is_member_function_pointer) failed"
#endif
+#if !__has_builtin (__is_member_object_pointer)
+# error "__has_builtin (__is_member_object_pointer) failed"
+#endif
#if !__has_builtin (__is_member_pointer)
# error "__has_builtin (__is_member_pointer) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C b/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
new file mode 100644
index 00000000000..835e48c8f8e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_member_object_pointer.C
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_NON_VOLATILE(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_CATEGORY(__is_member_object_pointer, int (ClassType::*), true);
+SA_TEST_CATEGORY(__is_member_object_pointer, ClassType (ClassType::*), true);
+
+// Negative tests.
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, int (ClassType::*) (int), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, int (ClassType::*) (float, ...), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, ClassType (ClassType::*) (ClassType), false);
+SA_TEST_NON_VOLATILE(__is_member_object_pointer, float (ClassType::*) (int, float, int[], int&), false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_member_object_pointer, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 21/40] libstdc++: Optimize is_member_object_pointer trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (19 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 20/40] c++: Implement __is_member_object_pointer built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 22/40] c++: Implement __is_reference built-in trait Ken Matsui
` (19 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_member_object_pointer trait
by dispatching to the new __is_member_object_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_member_object_pointer): Use
__is_member_object_pointer built-in trait.
(is_member_object_pointer_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index e1b10240dc2..792213ebfe8 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -574,6 +574,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_rvalue_reference<_Tp&&>
: public true_type { };
+ /// is_member_object_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_object_pointer)
+ template<typename _Tp>
+ struct is_member_object_pointer
+ : public __bool_constant<__is_member_object_pointer(_Tp)>
+ { };
+#else
template<typename>
struct __is_member_object_pointer_helper
: public false_type { };
@@ -582,11 +589,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __is_member_object_pointer_helper<_Tp _Cp::*>
: public __not_<is_function<_Tp>>::type { };
- /// is_member_object_pointer
+
template<typename _Tp>
struct is_member_object_pointer
: public __is_member_object_pointer_helper<__remove_cv_t<_Tp>>::type
{ };
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
/// is_member_function_pointer
@@ -3227,9 +3235,16 @@ template <typename _Tp>
inline constexpr bool is_rvalue_reference_v = false;
template <typename _Tp>
inline constexpr bool is_rvalue_reference_v<_Tp&&> = true;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_object_pointer)
+template <typename _Tp>
+ inline constexpr bool is_member_object_pointer_v =
+ __is_member_object_pointer(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_member_object_pointer_v =
is_member_object_pointer<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_function_pointer)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 22/40] c++: Implement __is_reference built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (20 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 21/40] libstdc++: Optimize is_member_object_pointer trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 23/40] libstdc++: Optimize is_reference trait performance Ken Matsui
` (18 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_reference.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_reference.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_REFERENCE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_reference.
* g++.dg/ext/is_reference.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 +++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++
gcc/testsuite/g++.dg/ext/is_reference.C | 34 ++++++++++++++++++++++++
5 files changed, 45 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_reference.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 98b1f004a68..5cdb59d174e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3787,6 +3787,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
+ case CPTK_IS_REFERENCE:
+ inform (loc, " %qT is not a reference", t1);
+ break;
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 47649150ab5..ac9fa026b4e 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -81,6 +81,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
+DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 95b25c1348b..bee27b25974 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12196,6 +12196,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
+ case CPTK_IS_REFERENCE:
+ return type_code1 == REFERENCE_TYPE;
+
case CPTK_IS_SAME:
return same_type_p (type1, type2);
@@ -12390,6 +12393,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
+ case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 8d9cdc528cd..e112d317657 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -122,6 +122,9 @@
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif
+#if !__has_builtin (__is_reference)
+# error "__has_builtin (__is_reference) failed"
+#endif
#if !__has_builtin (__is_same)
# error "__has_builtin (__is_same) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_reference.C b/gcc/testsuite/g++.dg/ext/is_reference.C
new file mode 100644
index 00000000000..b5ce4db7afd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_reference.C
@@ -0,0 +1,34 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+// Positive tests.
+SA_TEST_CATEGORY(__is_reference, int&, true);
+SA_TEST_CATEGORY(__is_reference, ClassType&, true);
+SA(__is_reference(int(&)(int)));
+SA_TEST_CATEGORY(__is_reference, int&&, true);
+SA_TEST_CATEGORY(__is_reference, ClassType&&, true);
+SA(__is_reference(int(&&)(int)));
+SA_TEST_CATEGORY(__is_reference, IncompleteClass&, true);
+
+// Negative tests
+SA_TEST_CATEGORY(__is_reference, void, false);
+SA_TEST_CATEGORY(__is_reference, int*, false);
+SA_TEST_CATEGORY(__is_reference, int[3], false);
+SA(!__is_reference(int(int)));
+SA(!__is_reference(int(*const)(int)));
+SA(!__is_reference(int(*volatile)(int)));
+SA(!__is_reference(int(*const volatile)(int)));
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_reference, ClassType, false);
+SA_TEST_CATEGORY(__is_reference, IncompleteClass, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 23/40] libstdc++: Optimize is_reference trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (21 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 22/40] c++: Implement __is_reference built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 24/40] c++: Implement __is_function built-in trait Ken Matsui
` (17 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_reference trait by dispatching
to the new __is_reference built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_reference): Use __is_reference built-in
trait.
(is_reference_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 792213ebfe8..36ad9814047 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -682,6 +682,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Composite type categories.
/// is_reference
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+ template<typename _Tp>
+ struct is_reference
+ : public __bool_constant<__is_reference(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_reference
: public false_type
@@ -696,6 +702,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct is_reference<_Tp&&>
: public true_type
{ };
+#endif
/// is_arithmetic
template<typename _Tp>
@@ -3264,12 +3271,19 @@ template <typename _Tp>
inline constexpr bool is_class_v = __is_class(_Tp);
template <typename _Tp>
inline constexpr bool is_function_v = is_function<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+template <typename _Tp>
+ inline constexpr bool is_reference_v = __is_reference(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_reference_v = false;
template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&> = true;
template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&&> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 24/40] c++: Implement __is_function built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (22 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 23/40] libstdc++: Optimize is_reference trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 25/40] libstdc++: Optimize is_function trait performance Ken Matsui
` (16 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_function.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_function.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_FUNCTION.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_function.
* g++.dg/ext/is_function.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_function.C | 58 ++++++++++++++++++++++++
5 files changed, 69 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_function.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 5cdb59d174e..99a7e7247ce 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3750,6 +3750,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_FINAL:
inform (loc, " %qT is not a final class", t1);
break;
+ case CPTK_IS_FUNCTION:
+ inform (loc, " %qT is not a function", t1);
+ break;
case CPTK_IS_LAYOUT_COMPATIBLE:
inform (loc, " %qT is not layout compatible with %qT", t1, t2);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index ac9fa026b4e..3bb33a3d5c0 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -70,6 +70,7 @@ DEFTRAIT_EXPR (IS_CONVERTIBLE, "__is_convertible", 2)
DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
+DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index bee27b25974..a502c13ecc1 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12163,6 +12163,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_FINAL:
return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
+ case CPTK_IS_FUNCTION:
+ return type_code1 == FUNCTION_TYPE;
+
case CPTK_IS_LAYOUT_COMPATIBLE:
return layout_compatible_type_p (type1, type2);
@@ -12390,6 +12393,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_CLASS:
case CPTK_IS_CONST:
case CPTK_IS_ENUM:
+ case CPTK_IS_FUNCTION:
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index e112d317657..4d3947572a4 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -89,6 +89,9 @@
#if !__has_builtin (__is_final)
# error "__has_builtin (__is_final) failed"
#endif
+#if !__has_builtin (__is_function)
+# error "__has_builtin (__is_function) failed"
+#endif
#if !__has_builtin (__is_layout_compatible)
# error "__has_builtin (__is_layout_compatible) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_function.C b/gcc/testsuite/g++.dg/ext/is_function.C
new file mode 100644
index 00000000000..2e1594b12ad
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_function.C
@@ -0,0 +1,58 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+struct A
+{ void fn(); };
+
+template<typename>
+struct AHolder { };
+
+template<class T, class U>
+struct AHolder<U T::*>
+{ using type = U; };
+
+// Positive tests.
+SA(__is_function(int (int)));
+SA(__is_function(ClassType (ClassType)));
+SA(__is_function(float (int, float, int[], int&)));
+SA(__is_function(int (int, ...)));
+SA(__is_function(bool (ClassType) const));
+SA(__is_function(AHolder<decltype(&A::fn)>::type));
+
+void fn();
+SA(__is_function(decltype(fn)));
+
+// Negative tests.
+SA_TEST_CATEGORY(__is_function, int, false);
+SA_TEST_CATEGORY(__is_function, int*, false);
+SA_TEST_CATEGORY(__is_function, int&, false);
+SA_TEST_CATEGORY(__is_function, void, false);
+SA_TEST_CATEGORY(__is_function, void*, false);
+SA_TEST_CATEGORY(__is_function, void**, false);
+SA_TEST_CATEGORY(__is_function, std::nullptr_t, false);
+
+SA_TEST_CATEGORY(__is_function, AbstractClass, false);
+SA(!__is_function(int(&)(int)));
+SA(!__is_function(int(*)(int)));
+
+SA_TEST_CATEGORY(__is_function, A, false);
+SA_TEST_CATEGORY(__is_function, decltype(&A::fn), false);
+
+struct FnCallOverload
+{ void operator()(); };
+SA_TEST_CATEGORY(__is_function, FnCallOverload, false);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_function, ClassType, false);
+SA_TEST_CATEGORY(__is_function, IncompleteClass, false);
+SA_TEST_CATEGORY(__is_function, IncompleteUnion, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 25/40] libstdc++: Optimize is_function trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (23 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 24/40] c++: Implement __is_function built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 26/40] libstdc++: Optimize is_object " Ken Matsui
` (15 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_function trait by dispatching
to the new __is_function built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_function): Use __is_function built-in
trait.
(is_function_v): Likewise. Optimize its implementation.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 36ad9814047..bd57488824b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -637,6 +637,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_function
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
+ template<typename _Tp>
+ struct is_function
+ : public __bool_constant<__is_function(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_function
: public __bool_constant<!is_const<const _Tp>::value> { };
@@ -648,6 +654,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct is_function<_Tp&&>
: public false_type { };
+#endif
#ifdef __cpp_lib_is_null_pointer // C++ >= 11
/// is_null_pointer (LWG 2247).
@@ -3269,8 +3276,18 @@ template <typename _Tp>
inline constexpr bool is_union_v = __is_union(_Tp);
template <typename _Tp>
inline constexpr bool is_class_v = __is_class(_Tp);
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function)
template <typename _Tp>
- inline constexpr bool is_function_v = is_function<_Tp>::value;
+ inline constexpr bool is_function_v = __is_function(_Tp);
+#else
+template <typename _Tp>
+ inline constexpr bool is_function_v = !is_const_v<const _Tp>;
+template <typename _Tp>
+ inline constexpr bool is_function_v<_Tp&> = false;
+template <typename _Tp>
+ inline constexpr bool is_function_v<_Tp&&> = false;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 26/40] libstdc++: Optimize is_object trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (24 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 25/40] libstdc++: Optimize is_function trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 27/40] c++: Implement __remove_pointer built-in trait Ken Matsui
` (14 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_object trait by dispatching to
the new __is_function and __is_reference built-in traits.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_object): Use __is_function and
__is_reference built-in traits.
(is_object_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index bd57488824b..674d398c075 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -725,11 +725,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_object
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
+ && _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+ template<typename _Tp>
+ struct is_object
+ : public __bool_constant<!(__is_function(_Tp) || __is_reference(_Tp)
+ || is_void<_Tp>::value)>
+ { };
+#else
template<typename _Tp>
struct is_object
: public __not_<__or_<is_function<_Tp>, is_reference<_Tp>,
is_void<_Tp>>>::type
{ };
+#endif
template<typename>
struct is_member_pointer;
@@ -3305,8 +3314,17 @@ template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
+ && _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
+template <typename _Tp>
+ inline constexpr bool is_object_v
+ = !(__is_function(_Tp) || __is_reference(_Tp) || is_void<_Tp>::value);
+#else
template <typename _Tp>
inline constexpr bool is_object_v = is_object<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 27/40] c++: Implement __remove_pointer built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (25 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 26/40] libstdc++: Optimize is_object " Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 28/40] libstdc++: Optimize remove_pointer trait performance Ken Matsui
` (13 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::remove_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __remove_pointer.
* semantics.cc (finish_trait_type): Handle CPTK_REMOVE_POINTER.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __remove_pointer.
* g++.dg/ext/remove_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 5 +++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/remove_pointer.C | 51 +++++++++++++++++++++++
4 files changed, 60 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/remove_pointer.C
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 3bb33a3d5c0..07cd14b6e85 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -97,6 +97,7 @@ DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_tempo
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
DEFTRAIT_TYPE (REMOVE_CV, "__remove_cv", 1)
DEFTRAIT_TYPE (REMOVE_CVREF, "__remove_cvref", 1)
+DEFTRAIT_TYPE (REMOVE_POINTER, "__remove_pointer", 1)
DEFTRAIT_TYPE (REMOVE_REFERENCE, "__remove_reference", 1)
DEFTRAIT_TYPE (TYPE_PACK_ELEMENT, "__type_pack_element", -1)
DEFTRAIT_TYPE (UNDERLYING_TYPE, "__underlying_type", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index a502c13ecc1..10656017bbc 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12473,6 +12473,11 @@ finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
type1 = TREE_TYPE (type1);
return cv_unqualified (type1);
+ case CPTK_REMOVE_POINTER:
+ if (TYPE_PTR_P (type1))
+ type1 = TREE_TYPE (type1);
+ return type1;
+
case CPTK_REMOVE_REFERENCE:
if (TYPE_REF_P (type1))
type1 = TREE_TYPE (type1);
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4d3947572a4..bcab0599d1a 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -173,6 +173,9 @@
#if !__has_builtin (__remove_cvref)
# error "__has_builtin (__remove_cvref) failed"
#endif
+#if !__has_builtin (__remove_pointer)
+# error "__has_builtin (__remove_pointer) failed"
+#endif
#if !__has_builtin (__remove_reference)
# error "__has_builtin (__remove_reference) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/remove_pointer.C b/gcc/testsuite/g++.dg/ext/remove_pointer.C
new file mode 100644
index 00000000000..7b13db93950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/remove_pointer.C
@@ -0,0 +1,51 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(__is_same(__remove_pointer(int), int));
+SA(__is_same(__remove_pointer(int*), int));
+SA(__is_same(__remove_pointer(int**), int*));
+
+SA(__is_same(__remove_pointer(const int*), const int));
+SA(__is_same(__remove_pointer(const int**), const int*));
+SA(__is_same(__remove_pointer(int* const), int));
+SA(__is_same(__remove_pointer(int** const), int*));
+SA(__is_same(__remove_pointer(int* const* const), int* const));
+
+SA(__is_same(__remove_pointer(volatile int*), volatile int));
+SA(__is_same(__remove_pointer(volatile int**), volatile int*));
+SA(__is_same(__remove_pointer(int* volatile), int));
+SA(__is_same(__remove_pointer(int** volatile), int*));
+SA(__is_same(__remove_pointer(int* volatile* volatile), int* volatile));
+
+SA(__is_same(__remove_pointer(const volatile int*), const volatile int));
+SA(__is_same(__remove_pointer(const volatile int**), const volatile int*));
+SA(__is_same(__remove_pointer(const int* volatile), const int));
+SA(__is_same(__remove_pointer(volatile int* const), volatile int));
+SA(__is_same(__remove_pointer(int* const volatile), int));
+SA(__is_same(__remove_pointer(const int** volatile), const int*));
+SA(__is_same(__remove_pointer(volatile int** const), volatile int*));
+SA(__is_same(__remove_pointer(int** const volatile), int*));
+SA(__is_same(__remove_pointer(int* const* const volatile), int* const));
+SA(__is_same(__remove_pointer(int* volatile* const volatile), int* volatile));
+SA(__is_same(__remove_pointer(int* const volatile* const volatile), int* const volatile));
+
+SA(__is_same(__remove_pointer(int&), int&));
+SA(__is_same(__remove_pointer(const int&), const int&));
+SA(__is_same(__remove_pointer(volatile int&), volatile int&));
+SA(__is_same(__remove_pointer(const volatile int&), const volatile int&));
+
+SA(__is_same(__remove_pointer(int&&), int&&));
+SA(__is_same(__remove_pointer(const int&&), const int&&));
+SA(__is_same(__remove_pointer(volatile int&&), volatile int&&));
+SA(__is_same(__remove_pointer(const volatile int&&), const volatile int&&));
+
+SA(__is_same(__remove_pointer(int[3]), int[3]));
+SA(__is_same(__remove_pointer(const int[3]), const int[3]));
+SA(__is_same(__remove_pointer(volatile int[3]), volatile int[3]));
+SA(__is_same(__remove_pointer(const volatile int[3]), const volatile int[3]));
+
+SA(__is_same(__remove_pointer(int(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*volatile)(int)), int(int)));
+SA(__is_same(__remove_pointer(int(*const volatile)(int)), int(int)));
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 28/40] libstdc++: Optimize remove_pointer trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (26 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 27/40] c++: Implement __remove_pointer built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 29/40] c++, libstdc++: Implement __is_pointer built-in trait Ken Matsui
` (12 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the remove_pointer trait by
dispatching to the new remove_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (remove_pointer): Use __remove_pointer
built-in trait.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 674d398c075..9c56d15c0b7 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -2105,6 +2105,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Pointer modifications.
+ /// remove_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__remove_pointer)
+ template<typename _Tp>
+ struct remove_pointer
+ { using type = __remove_pointer(_Tp); };
+#else
template<typename _Tp, typename>
struct __remove_pointer_helper
{ using type = _Tp; };
@@ -2113,11 +2119,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __remove_pointer_helper<_Tp, _Up*>
{ using type = _Up; };
- /// remove_pointer
template<typename _Tp>
struct remove_pointer
: public __remove_pointer_helper<_Tp, __remove_cv_t<_Tp>>
{ };
+#endif
template<typename _Tp, typename = void>
struct __add_pointer_helper
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 29/40] c++, libstdc++: Implement __is_pointer built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (27 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 28/40] libstdc++: Optimize remove_pointer trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 30/40] libstdc++: Optimize is_pointer trait performance Ken Matsui
` (11 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_pointer.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_POINTER.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_pointer.
* g++.dg/ext/is_pointer.C: New test.
* g++.dg/tm/pr46567.C (__is_pointer): Rename to ...
(__is_ptr): ... this.
* g++.dg/torture/20070621-1.C: Likewise.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_pointer): Rename to ...
(__is_ptr): ... this.
* include/bits/deque.tcc: Use __is_ptr instead.
* include/bits/stl_algobase.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_pointer.C | 51 +++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 22 ++++-----
gcc/testsuite/g++.dg/torture/20070621-1.C | 4 +-
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 6 +--
libstdc++-v3/include/bits/deque.tcc | 6 +--
libstdc++-v3/include/bits/stl_algobase.h | 6 +--
11 files changed, 86 insertions(+), 24 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_pointer.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 99a7e7247ce..c9d627fa782 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3787,6 +3787,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_POD:
inform (loc, " %qT is not a POD type", t1);
break;
+ case CPTK_IS_POINTER:
+ inform (loc, " %qT is not a pointer", t1);
+ break;
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 07cd14b6e85..bc2bb5e5abb 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -81,6 +81,7 @@ DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
+DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 10656017bbc..131ca8b96e6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12196,6 +12196,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POD:
return pod_type_p (type1);
+ case CPTK_IS_POINTER:
+ return TYPE_PTR_P (type1);
+
case CPTK_IS_POLYMORPHIC:
return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
@@ -12397,6 +12400,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_MEMBER_FUNCTION_POINTER:
case CPTK_IS_MEMBER_OBJECT_POINTER:
case CPTK_IS_MEMBER_POINTER:
+ case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
case CPTK_IS_SCOPED_ENUM:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index bcab0599d1a..efce04fd09d 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -122,6 +122,9 @@
#if !__has_builtin (__is_pod)
# error "__has_builtin (__is_pod) failed"
#endif
+#if !__has_builtin (__is_pointer)
+# error "__has_builtin (__is_pointer) failed"
+#endif
#if !__has_builtin (__is_polymorphic)
# error "__has_builtin (__is_polymorphic) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_pointer.C b/gcc/testsuite/g++.dg/ext/is_pointer.C
new file mode 100644
index 00000000000..d6e39565950
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_pointer.C
@@ -0,0 +1,51 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+SA(!__is_pointer(int));
+SA(__is_pointer(int*));
+SA(__is_pointer(int**));
+
+SA(__is_pointer(const int*));
+SA(__is_pointer(const int**));
+SA(__is_pointer(int* const));
+SA(__is_pointer(int** const));
+SA(__is_pointer(int* const* const));
+
+SA(__is_pointer(volatile int*));
+SA(__is_pointer(volatile int**));
+SA(__is_pointer(int* volatile));
+SA(__is_pointer(int** volatile));
+SA(__is_pointer(int* volatile* volatile));
+
+SA(__is_pointer(const volatile int*));
+SA(__is_pointer(const volatile int**));
+SA(__is_pointer(const int* volatile));
+SA(__is_pointer(volatile int* const));
+SA(__is_pointer(int* const volatile));
+SA(__is_pointer(const int** volatile));
+SA(__is_pointer(volatile int** const));
+SA(__is_pointer(int** const volatile));
+SA(__is_pointer(int* const* const volatile));
+SA(__is_pointer(int* volatile* const volatile));
+SA(__is_pointer(int* const volatile* const volatile));
+
+SA(!__is_pointer(int&));
+SA(!__is_pointer(const int&));
+SA(!__is_pointer(volatile int&));
+SA(!__is_pointer(const volatile int&));
+
+SA(!__is_pointer(int&&));
+SA(!__is_pointer(const int&&));
+SA(!__is_pointer(volatile int&&));
+SA(!__is_pointer(const volatile int&&));
+
+SA(!__is_pointer(int[3]));
+SA(!__is_pointer(const int[3]));
+SA(!__is_pointer(volatile int[3]));
+SA(!__is_pointer(const volatile int[3]));
+
+SA(!__is_pointer(int(int)));
+SA(__is_pointer(int(*const)(int)));
+SA(__is_pointer(int(*volatile)(int)));
+SA(__is_pointer(int(*const volatile)(int)));
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 6d791484448..f08bbf6fd7b 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -192,13 +192,13 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef __true_type __type;
};
template<typename _Tp>
- struct __is_pointer
+ struct __is_ptr
{
enum { __value = 0 };
typedef __false_type __type;
};
template<typename _Tp>
- struct __is_pointer<_Tp*>
+ struct __is_ptr<_Tp*>
{
enum { __value = 1 };
typedef __true_type __type;
@@ -226,7 +226,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
{ };
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
+ : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
struct __is_char
@@ -1202,8 +1202,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
typedef typename iterator_traits<_II>::iterator_category _Category;
const bool __simple = (__is_pod(_ValueTypeI)
- && __is_pointer<_II>::__value
- && __is_pointer<_OI>::__value
+ && __is_ptr<_II>::__value
+ && __is_ptr<_OI>::__value
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
return std::__copy_move<_IsMove, __simple,
_Category>::__copy_m(__first, __last, __result);
@@ -1294,8 +1294,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_BI2>::value_type _ValueType2;
typedef typename iterator_traits<_BI1>::iterator_category _Category;
const bool __simple = (__is_pod(_ValueType1)
- && __is_pointer<_BI1>::__value
- && __is_pointer<_BI2>::__value
+ && __is_ptr<_BI1>::__value
+ && __is_ptr<_BI2>::__value
&& __are_same<_ValueType1, _ValueType2>::__value);
return std::__copy_move_backward<_IsMove, __simple,
_Category>::__copy_move_b(__first,
@@ -1426,8 +1426,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple = (__is_integer<_ValueType1>::__value
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value
&& __are_same<_ValueType1, _ValueType2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
@@ -1515,8 +1515,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
&& !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
&& !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value);
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
__first2, __last2);
}
diff --git a/gcc/testsuite/g++.dg/torture/20070621-1.C b/gcc/testsuite/g++.dg/torture/20070621-1.C
index d8a6a76b6b0..b05136163e8 100644
--- a/gcc/testsuite/g++.dg/torture/20070621-1.C
+++ b/gcc/testsuite/g++.dg/torture/20070621-1.C
@@ -18,7 +18,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_pointer {
+ template<typename _Tp> struct __is_ptr {
enum {
__value = 0 };
};
@@ -49,7 +49,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
template<typename _II1, typename _II2> inline bool __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) {
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
- const bool __simple = (__is_integer<_ValueType1>::__value && __is_pointer<_II1>::__value && __is_pointer<_II2>::__value && __are_same<_ValueType1, _ValueType2>::__value);
+ const bool __simple = (__is_integer<_ValueType1>::__value && __is_ptr<_II1>::__value && __is_ptr<_II2>::__value && __are_same<_ValueType1, _ValueType2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
template<typename _II1, typename _II2> inline bool equal(_II1 __first1, _II1 __last1, _II2 __first2) {
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index 4dbd32bd298..be0689096fb 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -17,7 +17,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_pointer {
+ template<typename _Tp> struct __is_ptr {
enum {
__value = 0 };
};
@@ -27,7 +27,7 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
};
template<typename _Tp> struct __is_arithmetic : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> > {
+ template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 4312f32a4e0..3711e4be526 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -364,14 +364,14 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// Pointer types
//
template<typename _Tp>
- struct __is_pointer
+ struct __is_ptr
{
enum { __value = 0 };
typedef __false_type __type;
};
template<typename _Tp>
- struct __is_pointer<_Tp*>
+ struct __is_ptr<_Tp*>
{
enum { __value = 1 };
typedef __true_type __type;
@@ -390,7 +390,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
+ : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
{ };
//
diff --git a/libstdc++-v3/include/bits/deque.tcc b/libstdc++-v3/include/bits/deque.tcc
index a212b8a6940..08d888ee8af 100644
--- a/libstdc++-v3/include/bits/deque.tcc
+++ b/libstdc++-v3/include/bits/deque.tcc
@@ -1273,7 +1273,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
const bool __simple =
(__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
- && __is_pointer<_Ptr>::__value
+ && __is_ptr<_Ptr>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
@@ -1329,8 +1329,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
const bool __simple =
(__is_memcmp_ordered_with<_Tp1, _Tp2>::__value
- && __is_pointer<_Ptr1>::__value
- && __is_pointer<_Ptr2>::__value
+ && __is_ptr<_Ptr1>::__value
+ && __is_ptr<_Ptr2>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 2f5a4bd4fd4..d1438429487 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -1217,7 +1217,7 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
{
typedef typename iterator_traits<_II1>::value_type _ValueType1;
const bool __simple = ((__is_integer<_ValueType1>::__value
- || __is_pointer<_ValueType1>::__value)
+ || __is_ptr<_ValueType1>::__value)
&& __memcmpable<_II1, _II2>::__value);
return std::__equal<__simple>::equal(__first1, __last1, __first2);
}
@@ -1380,8 +1380,8 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_memcmp_ordered_with<_ValueType1, _ValueType2>::__value
- && __is_pointer<_II1>::__value
- && __is_pointer<_II2>::__value
+ && __is_ptr<_II1>::__value
+ && __is_ptr<_II2>::__value
#if __cplusplus > 201703L && __cpp_lib_concepts
// For C++20 iterator_traits<volatile T*>::value_type is non-volatile
// so __is_byte<T> could be true, but we can't use memcmp with
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 30/40] libstdc++: Optimize is_pointer trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (28 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 29/40] c++, libstdc++: Implement __is_pointer built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait Ken Matsui
` (10 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui, Jonathan Wakely
This patch optimizes the performance of the is_pointer trait by dispatching to
the new __is_pointer built-in trait.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_ptr): Use __is_pointer
built-in trait.
* include/std/type_traits (is_pointer): Likewise. Optimize its
implementation.
(is_pointer_v): Likewise.
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/bits/cpp_type_traits.h | 8 ++++
libstdc++-v3/include/std/type_traits | 44 +++++++++++++++++----
2 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 3711e4be526..4da1e7c407c 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -363,6 +363,13 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
// Pointer types
//
+#if __has_builtin(__is_pointer)
+ template<typename _Tp>
+ struct __is_ptr : __truth_type<__is_pointer(_Tp)>
+ {
+ enum { __value = __is_pointer(_Tp) };
+ };
+#else
template<typename _Tp>
struct __is_ptr
{
@@ -376,6 +383,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
enum { __value = 1 };
typedef __true_type __type;
};
+#endif
//
// An arithmetic type is an integer type or a floating point type
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 9c56d15c0b7..3acd843f2f2 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -542,19 +542,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public true_type { };
#endif
- template<typename>
- struct __is_pointer_helper
+ /// is_pointer
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
+ template<typename _Tp>
+ struct is_pointer
+ : public __bool_constant<__is_pointer(_Tp)>
+ { };
+#else
+ template<typename _Tp>
+ struct is_pointer
: public false_type { };
template<typename _Tp>
- struct __is_pointer_helper<_Tp*>
+ struct is_pointer<_Tp*>
: public true_type { };
- /// is_pointer
template<typename _Tp>
- struct is_pointer
- : public __is_pointer_helper<__remove_cv_t<_Tp>>::type
- { };
+ struct is_pointer<_Tp* const>
+ : public true_type { };
+
+ template<typename _Tp>
+ struct is_pointer<_Tp* volatile>
+ : public true_type { };
+
+ template<typename _Tp>
+ struct is_pointer<_Tp* const volatile>
+ : public true_type { };
+#endif
/// is_lvalue_reference
template<typename>
@@ -3254,8 +3268,22 @@ template <typename _Tp, size_t _Num>
inline constexpr bool is_array_v<_Tp[_Num]> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_pointer)
+template <typename _Tp>
+ inline constexpr bool is_pointer_v = __is_pointer(_Tp);
+#else
template <typename _Tp>
- inline constexpr bool is_pointer_v = is_pointer<_Tp>::value;
+ inline constexpr bool is_pointer_v = false;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp*> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* const> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* volatile> = true;
+template <typename _Tp>
+ inline constexpr bool is_pointer_v<_Tp* const volatile> = true;
+#endif
+
template <typename _Tp>
inline constexpr bool is_lvalue_reference_v = false;
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (29 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 30/40] libstdc++: Optimize is_pointer trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 32/40] libstdc++: Optimize is_arithmetic trait performance Ken Matsui
` (9 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_arithmetic.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_arithmetic.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_ARITHMETIC.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_arithmetic.
* g++.dg/ext/is_arithmetic.C: New test.
* g++.dg/tm/pr46567.C (__is_arithmetic): Rename to ...
(__is_arith): ... this.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_arithmetic): Rename to ...
(__is_arith): ... this.
* include/c_global/cmath: Use __is_arith instead.
* include/c_std/cmath: Likewise.
* include/tr1/cmath: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_arithmetic.C | 33 ++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 6 +--
gcc/testsuite/g++.dg/torture/pr57107.C | 4 +-
libstdc++-v3/include/bits/cpp_type_traits.h | 4 +-
libstdc++-v3/include/c_global/cmath | 48 ++++++++++-----------
libstdc++-v3/include/c_std/cmath | 24 +++++------
libstdc++-v3/include/tr1/cmath | 24 +++++------
11 files changed, 99 insertions(+), 55 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_arithmetic.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c9d627fa782..3a7f968eae8 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3714,6 +3714,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_AGGREGATE:
inform (loc, " %qT is not an aggregate", t1);
break;
+ case CPTK_IS_ARITHMETIC:
+ inform (loc, " %qT is not an arithmetic type", t1);
+ break;
case CPTK_IS_ARRAY:
inform (loc, " %qT is not an array", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index bc2bb5e5abb..06c203ce4de 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -59,6 +59,7 @@ DEFTRAIT_EXPR (HAS_UNIQUE_OBJ_REPRESENTATIONS, "__has_unique_object_representati
DEFTRAIT_EXPR (HAS_VIRTUAL_DESTRUCTOR, "__has_virtual_destructor", 1)
DEFTRAIT_EXPR (IS_ABSTRACT, "__is_abstract", 1)
DEFTRAIT_EXPR (IS_AGGREGATE, "__is_aggregate", 1)
+DEFTRAIT_EXPR (IS_ARITHMETIC, "__is_arithmetic", 1)
DEFTRAIT_EXPR (IS_ARRAY, "__is_array", 1)
DEFTRAIT_EXPR (IS_ASSIGNABLE, "__is_assignable", 2)
DEFTRAIT_EXPR (IS_BASE_OF, "__is_base_of", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 131ca8b96e6..553a51dc16d 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12128,6 +12128,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_AGGREGATE:
return CP_AGGREGATE_TYPE_P (type1);
+ case CPTK_IS_ARITHMETIC:
+ return ARITHMETIC_TYPE_P (type1);
+
case CPTK_IS_ARRAY:
return type_code1 == ARRAY_TYPE;
@@ -12391,6 +12394,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
return error_mark_node;
break;
+ case CPTK_IS_ARITHMETIC:
case CPTK_IS_ARRAY:
case CPTK_IS_BOUNDED_ARRAY:
case CPTK_IS_CLASS:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index efce04fd09d..4bc85f4babb 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -56,6 +56,9 @@
#if !__has_builtin (__is_aggregate)
# error "__has_builtin (__is_aggregate) failed"
#endif
+#if !__has_builtin (__is_arithmetic)
+# error "__has_builtin (__is_arithmetic) failed"
+#endif
#if !__has_builtin (__is_array)
# error "__has_builtin (__is_array) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_arithmetic.C b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
new file mode 100644
index 00000000000..fd35831f646
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_arithmetic.C
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \
+ SA(TRAIT(TYPE) == EXPECT); \
+ SA(TRAIT(const TYPE) == EXPECT); \
+ SA(TRAIT(volatile TYPE) == EXPECT); \
+ SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_arithmetic, void, false);
+
+SA_TEST_CATEGORY(__is_arithmetic, char, true);
+SA_TEST_CATEGORY(__is_arithmetic, signed char, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned char, true);
+SA_TEST_CATEGORY(__is_arithmetic, wchar_t, true);
+SA_TEST_CATEGORY(__is_arithmetic, short, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned short, true);
+SA_TEST_CATEGORY(__is_arithmetic, int, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned int, true);
+SA_TEST_CATEGORY(__is_arithmetic, long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long, true);
+SA_TEST_CATEGORY(__is_arithmetic, long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, unsigned long long, true);
+SA_TEST_CATEGORY(__is_arithmetic, float, true);
+SA_TEST_CATEGORY(__is_arithmetic, double, true);
+SA_TEST_CATEGORY(__is_arithmetic, long double, true);
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_arithmetic, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index f08bbf6fd7b..79d304e0309 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -217,16 +217,16 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef __true_type __type;
};
template<typename _Tp>
- struct __is_arithmetic
+ struct __is_arith
: public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
{ };
template<typename _Tp>
struct __is_fundamental
- : public __traitor<__is_void<_Tp>, __is_arithmetic<_Tp> >
+ : public __traitor<__is_void<_Tp>, __is_arith<_Tp> >
{ };
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
+ : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
template<typename _Tp>
struct __is_char
diff --git a/gcc/testsuite/g++.dg/torture/pr57107.C b/gcc/testsuite/g++.dg/torture/pr57107.C
index be0689096fb..da592b9fd23 100644
--- a/gcc/testsuite/g++.dg/torture/pr57107.C
+++ b/gcc/testsuite/g++.dg/torture/pr57107.C
@@ -25,9 +25,9 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
enum {
__value = 0 };
};
- template<typename _Tp> struct __is_arithmetic : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
+ template<typename _Tp> struct __is_arith : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> > {
};
- template<typename _Tp> struct __is_scalar : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> > {
+ template<typename _Tp> struct __is_scalar : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> > {
};
}
namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
diff --git a/libstdc++-v3/include/bits/cpp_type_traits.h b/libstdc++-v3/include/bits/cpp_type_traits.h
index 4da1e7c407c..51ed5b07716 100644
--- a/libstdc++-v3/include/bits/cpp_type_traits.h
+++ b/libstdc++-v3/include/bits/cpp_type_traits.h
@@ -389,7 +389,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
// An arithmetic type is an integer type or a floating point type
//
template<typename _Tp>
- struct __is_arithmetic
+ struct __is_arith
: public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
{ };
@@ -398,7 +398,7 @@ __INT_N(__GLIBCXX_TYPE_INT_N_3)
//
template<typename _Tp>
struct __is_scalar
- : public __traitor<__is_arithmetic<_Tp>, __is_ptr<_Tp> >
+ : public __traitor<__is_arith<_Tp>, __is_ptr<_Tp> >
{ };
//
diff --git a/libstdc++-v3/include/c_global/cmath b/libstdc++-v3/include/c_global/cmath
index 6461c92ebfe..a0ddc1dbbeb 100644
--- a/libstdc++-v3/include/c_global/cmath
+++ b/libstdc++-v3/include/c_global/cmath
@@ -1259,8 +1259,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isgreater(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1285,8 +1285,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isgreaterequal(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1311,8 +1311,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isless(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1337,8 +1337,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
islessequal(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1363,8 +1363,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
islessgreater(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1389,8 +1389,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#ifndef __CORRECT_ISO_CPP11_MATH_H_PROTO_INT
template<typename _Tp, typename _Up>
constexpr typename
- __gnu_cxx::__enable_if<(__is_arithmetic<_Tp>::__value
- && __is_arithmetic<_Up>::__value), bool>::__type
+ __gnu_cxx::__enable_if<(__is_arith<_Tp>::__value
+ && __is_arith<_Up>::__value), bool>::__type
isunordered(_Tp __x, _Up __y)
{
typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
@@ -1401,7 +1401,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#else
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -1411,7 +1411,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -1420,7 +1420,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -1429,7 +1429,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -1438,7 +1438,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -1447,7 +1447,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -1456,7 +1456,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -1465,7 +1465,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -1474,7 +1474,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -1483,7 +1483,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -1492,7 +1492,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -1501,7 +1501,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
diff --git a/libstdc++-v3/include/c_std/cmath b/libstdc++-v3/include/c_std/cmath
index 588ee1e6dc4..c1db699ecdb 100644
--- a/libstdc++-v3/include/c_std/cmath
+++ b/libstdc++-v3/include/c_std/cmath
@@ -467,7 +467,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#undef isunordered
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -477,7 +477,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -486,7 +486,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -495,7 +495,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -504,7 +504,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -513,7 +513,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -522,7 +522,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -531,7 +531,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -540,7 +540,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -549,7 +549,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -558,7 +558,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -567,7 +567,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
diff --git a/libstdc++-v3/include/tr1/cmath b/libstdc++-v3/include/tr1/cmath
index ba1b60cc945..2e80f1d0d00 100644
--- a/libstdc++-v3/include/tr1/cmath
+++ b/libstdc++-v3/include/tr1/cmath
@@ -307,7 +307,7 @@ namespace tr1
/// Function template definitions [8.16.3].
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
fpclassify(_Tp __f)
{
@@ -317,7 +317,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isfinite(_Tp __f)
{
@@ -326,7 +326,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isinf(_Tp __f)
{
@@ -335,7 +335,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnan(_Tp __f)
{
@@ -344,7 +344,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isnormal(_Tp __f)
{
@@ -353,7 +353,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
signbit(_Tp __f)
{
@@ -362,7 +362,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreater(_Tp __f1, _Tp __f2)
{
@@ -371,7 +371,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isgreaterequal(_Tp __f1, _Tp __f2)
{
@@ -380,7 +380,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isless(_Tp __f1, _Tp __f2)
{
@@ -389,7 +389,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessequal(_Tp __f1, _Tp __f2)
{
@@ -398,7 +398,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
islessgreater(_Tp __f1, _Tp __f2)
{
@@ -407,7 +407,7 @@ namespace tr1
}
template<typename _Tp>
- inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
+ inline typename __gnu_cxx::__enable_if<__is_arith<_Tp>::__value,
int>::__type
isunordered(_Tp __f1, _Tp __f2)
{
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 32/40] libstdc++: Optimize is_arithmetic trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (30 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 31/40] c++, libstdc++: Implement __is_arithmetic built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 33/40] libstdc++: Optimize is_fundamental " Ken Matsui
` (8 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_arithmetic trait by dispatching
to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_arithmetic): Use __is_arithmetic
built-in trait.
(is_arithmetic_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 3acd843f2f2..cc466e0f606 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -726,10 +726,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_arithmetic
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+ template<typename _Tp>
+ struct is_arithmetic
+ : public __bool_constant<__is_arithmetic(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_arithmetic
: public __or_<is_integral<_Tp>, is_floating_point<_Tp>>::type
{ };
+#endif
/// is_fundamental
template<typename _Tp>
@@ -3344,8 +3351,14 @@ template <typename _Tp>
inline constexpr bool is_reference_v<_Tp&&> = true;
#endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+template <typename _Tp>
+ inline constexpr bool is_arithmetic_v = __is_arithmetic(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
+#endif
+
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 33/40] libstdc++: Optimize is_fundamental trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (31 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 32/40] libstdc++: Optimize is_arithmetic trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 34/40] libstdc++: Optimize is_compound " Ken Matsui
` (7 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_fundamental trait by
dispatching to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_fundamental_v): Use __is_arithmetic
built-in trait.
(is_fundamental): Likewise. Optimize the original implementation.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index cc466e0f606..88171e1a672 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -739,11 +739,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
/// is_fundamental
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
+ template<typename _Tp>
+ struct is_fundamental
+ : public __bool_constant<__is_arithmetic(_Tp)
+ || is_void<_Tp>::value
+ || is_null_pointer<_Tp>::value>
+ { };
+#else
template<typename _Tp>
struct is_fundamental
- : public __or_<is_arithmetic<_Tp>, is_void<_Tp>,
- is_null_pointer<_Tp>>::type
+ : public __bool_constant<is_arithmetic<_Tp>::value
+ || is_void<_Tp>::value
+ || is_null_pointer<_Tp>::value>
{ };
+#endif
/// is_object
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
@@ -3354,13 +3364,15 @@ template <typename _Tp>
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_arithmetic)
template <typename _Tp>
inline constexpr bool is_arithmetic_v = __is_arithmetic(_Tp);
+template <typename _Tp>
+ inline constexpr bool is_fundamental_v
+ = __is_arithmetic(_Tp) || is_void_v<_Tp> || is_null_pointer_v<_Tp>;
#else
template <typename _Tp>
inline constexpr bool is_arithmetic_v = is_arithmetic<_Tp>::value;
-#endif
-
template <typename _Tp>
inline constexpr bool is_fundamental_v = is_fundamental<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_function) \
&& _GLIBCXX_USE_BUILTIN_TRAIT(__is_reference)
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 34/40] libstdc++: Optimize is_compound trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (32 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 33/40] libstdc++: Optimize is_fundamental " Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 35/40] c++: Implement __is_unsigned built-in trait Ken Matsui
` (6 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_compound trait by dispatching
to the new __is_arithmetic built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_compound): Do not use __not_.
(is_compound_v): Use is_fundamental_v instead.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 88171e1a672..48d630a1478 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -784,7 +784,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// is_compound
template<typename _Tp>
struct is_compound
- : public __not_<is_fundamental<_Tp>>::type { };
+ : public __bool_constant<!is_fundamental<_Tp>::value> { };
/// is_member_pointer
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
@@ -3387,7 +3387,7 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_scalar_v = is_scalar<_Tp>::value;
template <typename _Tp>
- inline constexpr bool is_compound_v = is_compound<_Tp>::value;
+ inline constexpr bool is_compound_v = !is_fundamental_v<_Tp>;
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_member_pointer)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 35/40] c++: Implement __is_unsigned built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (33 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 34/40] libstdc++: Optimize is_compound " Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 36/40] libstdc++: Optimize is_unsigned trait performance Ken Matsui
` (5 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_unsigned.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_unsigned.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_UNSIGNED.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_unsigned.
* g++.dg/ext/is_unsigned.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_unsigned.C | 47 ++++++++++++++++++++++++
5 files changed, 58 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/ext/is_unsigned.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 3a7f968eae8..c28dad702c3 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3829,6 +3829,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
+ case CPTK_IS_UNSIGNED:
+ inform (loc, " %qT is not an unsigned type", t1);
+ break;
case CPTK_IS_VOLATILE:
inform (loc, " %qT is not a volatile type", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 06c203ce4de..611e84cbbfd 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -94,6 +94,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_constructible", -1)
DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1)
DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1)
DEFTRAIT_EXPR (IS_UNION, "__is_union", 1)
+DEFTRAIT_EXPR (IS_UNSIGNED, "__is_unsigned", 1)
DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1)
DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_constructs_from_temporary", 2)
DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_from_temporary", 2)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 553a51dc16d..b5c6b4992e5 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12235,6 +12235,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_UNION:
return type_code1 == UNION_TYPE;
+ case CPTK_IS_UNSIGNED:
+ return TYPE_UNSIGNED (type1);
+
case CPTK_IS_VOLATILE:
return CP_TYPE_VOLATILE_P (type1);
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
+ case CPTK_IS_UNSIGNED:
case CPTK_IS_VOLATILE:
break;
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 4bc85f4babb..3d380f94b06 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -164,6 +164,9 @@
#if !__has_builtin (__is_union)
# error "__has_builtin (__is_union) failed"
#endif
+#if !__has_builtin (__is_unsigned)
+# error "__has_builtin (__is_unsigned) failed"
+#endif
#if !__has_builtin (__is_volatile)
# error "__has_builtin (__is_volatile) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_unsigned.C b/gcc/testsuite/g++.dg/ext/is_unsigned.C
new file mode 100644
index 00000000000..2bb45d209a7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_unsigned.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_unsigned, void, false);
+
+SA_TEST_CATEGORY(__is_unsigned, bool, (bool(-1) > bool(0)));
+SA_TEST_CATEGORY(__is_unsigned, char, (char(-1) > char(0)));
+SA_TEST_CATEGORY(__is_unsigned, signed char, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned char, true);
+SA_TEST_CATEGORY(__is_unsigned, wchar_t, (wchar_t(-1) > wchar_t(0)));
+SA_TEST_CATEGORY(__is_unsigned, short, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned short, true);
+SA_TEST_CATEGORY(__is_unsigned, int, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned int, true);
+SA_TEST_CATEGORY(__is_unsigned, long, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned long, true);
+SA_TEST_CATEGORY(__is_unsigned, long long, false);
+SA_TEST_CATEGORY(__is_unsigned, unsigned long long, true);
+
+SA_TEST_CATEGORY(__is_unsigned, float, false);
+SA_TEST_CATEGORY(__is_unsigned, double, false);
+SA_TEST_CATEGORY(__is_unsigned, long double, false);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_unsigned, unsigned __int128, true);
+SA_TEST_CATEGORY(__is_unsigned, __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_unsigned, __float128, false);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_unsigned, ClassType, false);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 36/40] libstdc++: Optimize is_unsigned trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (34 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 35/40] c++: Implement __is_unsigned built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 37/40] c++, libstdc++: Implement __is_signed built-in trait Ken Matsui
` (4 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_unsigned trait by dispatching
to the new __is_unsigned built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_unsigned): Use __is_unsigned built-in
trait.
(is_unsigned_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 48d630a1478..f7d3815f332 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -1001,10 +1001,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// is_unsigned
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
+ template<typename _Tp>
+ struct is_unsigned
+ : public __bool_constant<__is_unsigned(_Tp)>
+ { };
+#else
template<typename _Tp>
struct is_unsigned
: public __and_<is_arithmetic<_Tp>, __not_<is_signed<_Tp>>>::type
{ };
+#endif
/// @cond undocumented
template<typename _Tp, typename _Up = _Tp&&>
@@ -3440,8 +3447,14 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_signed_v = is_signed<_Tp>::value;
+
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
+template <typename _Tp>
+ inline constexpr bool is_unsigned_v = __is_unsigned(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_unsigned_v = is_unsigned<_Tp>::value;
+#endif
template <typename _Tp, typename... _Args>
inline constexpr bool is_constructible_v = __is_constructible(_Tp, _Args...);
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 37/40] c++, libstdc++: Implement __is_signed built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (35 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 36/40] libstdc++: Optimize is_unsigned trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 38/40] libstdc++: Optimize is_signed trait performance Ken Matsui
` (3 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_signed.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_signed.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SIGNED.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_signed.
* g++.dg/ext/is_signed.C: New test.
* g++.dg/tm/pr46567.C (__is_signed): Rename to ...
(__is_signed_type): ... this.
libstdc++-v3/ChangeLog:
* include/ext/numeric_traits.h (__is_signed): Rename to ...
(__is_signed_type): ... this.
* include/bits/charconv.h: Use __is_signed_type instead.
* include/bits/locale_facets.tcc: Likewise.
* include/bits/uniform_int_dist.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +
gcc/cp/semantics.cc | 4 ++
gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 ++
gcc/testsuite/g++.dg/ext/is_signed.C | 47 ++++++++++++++++++++
gcc/testsuite/g++.dg/tm/pr46567.C | 12 ++---
libstdc++-v3/include/bits/charconv.h | 2 +-
libstdc++-v3/include/bits/locale_facets.tcc | 6 +--
libstdc++-v3/include/bits/uniform_int_dist.h | 4 +-
libstdc++-v3/include/ext/numeric_traits.h | 18 ++++----
10 files changed, 79 insertions(+), 21 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/ext/is_signed.C
diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index c28dad702c3..b161c9b2c9e 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3802,6 +3802,9 @@ diagnose_trait_expr (tree expr, tree args)
case CPTK_IS_SAME:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
+ case CPTK_IS_SIGNED:
+ inform (loc, " %qT is not a signed type", t1);
+ break;
case CPTK_IS_SCOPED_ENUM:
inform (loc, " %qT is not a scoped enum", t1);
break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 611e84cbbfd..f0b5fe9cb3b 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -86,6 +86,7 @@ DEFTRAIT_EXPR (IS_POINTER, "__is_pointer", 1)
DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1)
DEFTRAIT_EXPR (IS_REFERENCE, "__is_reference", 1)
DEFTRAIT_EXPR (IS_SAME, "__is_same", 2)
+DEFTRAIT_EXPR (IS_SIGNED, "__is_signed", 1)
DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1)
DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1)
DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index b5c6b4992e5..58011a45cc6 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12211,6 +12211,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_SAME:
return same_type_p (type1, type2);
+ case CPTK_IS_SIGNED:
+ return ARITHMETIC_TYPE_P (type1) && TYPE_SIGN (type1) == SIGNED;
+
case CPTK_IS_SCOPED_ENUM:
return SCOPED_ENUM_P (type1);
@@ -12410,6 +12413,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
case CPTK_IS_POINTER:
case CPTK_IS_REFERENCE:
case CPTK_IS_SAME:
+ case CPTK_IS_SIGNED:
case CPTK_IS_SCOPED_ENUM:
case CPTK_IS_UNBOUNDED_ARRAY:
case CPTK_IS_UNION:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 3d380f94b06..aaf7254df4b 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -140,6 +140,9 @@
#if !__has_builtin (__is_same_as)
# error "__has_builtin (__is_same_as) failed"
#endif
+#if !__has_builtin (__is_signed)
+# error "__has_builtin (__is_signed) failed"
+#endif
#if !__has_builtin (__is_scoped_enum)
# error "__has_builtin (__is_scoped_enum) failed"
#endif
diff --git a/gcc/testsuite/g++.dg/ext/is_signed.C b/gcc/testsuite/g++.dg/ext/is_signed.C
new file mode 100644
index 00000000000..a04b548105d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_signed.C
@@ -0,0 +1,47 @@
+// { dg-do compile { target c++11 } }
+
+#include <testsuite_tr1.h>
+
+using namespace __gnu_test;
+
+#define SA(X) static_assert((X),#X)
+#define SA_TEST_CATEGORY(TRAIT, X, expect) \
+ SA(TRAIT(X) == expect); \
+ SA(TRAIT(const X) == expect); \
+ SA(TRAIT(volatile X) == expect); \
+ SA(TRAIT(const volatile X) == expect)
+
+SA_TEST_CATEGORY(__is_signed, void, false);
+
+SA_TEST_CATEGORY(__is_signed, bool, bool(-1) < bool(0));
+SA_TEST_CATEGORY(__is_signed, char, char(-1) < char(0));
+SA_TEST_CATEGORY(__is_signed, signed char, true);
+SA_TEST_CATEGORY(__is_signed, unsigned char, false);
+SA_TEST_CATEGORY(__is_signed, wchar_t, wchar_t(-1) < wchar_t(0));
+SA_TEST_CATEGORY(__is_signed, short, true);
+SA_TEST_CATEGORY(__is_signed, unsigned short, false);
+SA_TEST_CATEGORY(__is_signed, int, true);
+SA_TEST_CATEGORY(__is_signed, unsigned int, false);
+SA_TEST_CATEGORY(__is_signed, long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long, false);
+SA_TEST_CATEGORY(__is_signed, long long, true);
+SA_TEST_CATEGORY(__is_signed, unsigned long long, false);
+
+SA_TEST_CATEGORY(__is_signed, float, true);
+SA_TEST_CATEGORY(__is_signed, double, true);
+SA_TEST_CATEGORY(__is_signed, long double, true);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_signed, __int128, true);
+SA_TEST_CATEGORY(__is_signed, unsigned __int128, false);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_signed, __float128, true);
+#endif
+#endif
+
+// Sanity check.
+SA_TEST_CATEGORY(__is_signed, ClassType, false);
diff --git a/gcc/testsuite/g++.dg/tm/pr46567.C b/gcc/testsuite/g++.dg/tm/pr46567.C
index 79d304e0309..c891aff20f4 100644
--- a/gcc/testsuite/g++.dg/tm/pr46567.C
+++ b/gcc/testsuite/g++.dg/tm/pr46567.C
@@ -403,7 +403,7 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
{
static const _Value __min = (((_Value)(-1) < 0) ? (_Value)1 << (sizeof(_Value) * 8 - ((_Value)(-1) < 0)) : (_Value)0);
static const _Value __max = (((_Value)(-1) < 0) ? (((((_Value)1 << ((sizeof(_Value) * 8 - ((_Value)(-1) < 0)) - 1)) - 1) << 1) + 1) : ~(_Value)0);
- static const bool __is_signed = ((_Value)(-1) < 0);
+ static const bool __is_signed_type = ((_Value)(-1) < 0);
static const int __digits = (sizeof(_Value) * 8 - ((_Value)(-1) < 0));
};
template<typename _Value>
@@ -411,21 +411,21 @@ namespace __gnu_cxx __attribute__ ((__visibility__ ("default"))) {
template<typename _Value>
const _Value __numeric_traits_integer<_Value>::__max;
template<typename _Value>
- const bool __numeric_traits_integer<_Value>::__is_signed;
+ const bool __numeric_traits_integer<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_integer<_Value>::__digits;
template<typename _Value>
struct __numeric_traits_floating
{
static const int __max_digits10 = (2 + (std::__are_same<_Value, float>::__value ? 24 : std::__are_same<_Value, double>::__value ? 53 : 64) * 3010 / 10000);
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = (std::__are_same<_Value, float>::__value ? 6 : std::__are_same<_Value, double>::__value ? 15 : 18);
static const int __max_exponent10 = (std::__are_same<_Value, float>::__value ? 38 : std::__are_same<_Value, double>::__value ? 308 : 4932);
};
template<typename _Value>
const int __numeric_traits_floating<_Value>::__max_digits10;
template<typename _Value>
- const bool __numeric_traits_floating<_Value>::__is_signed;
+ const bool __numeric_traits_floating<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_floating<_Value>::__digits10;
template<typename _Value>
@@ -1513,8 +1513,8 @@ namespace std __attribute__ ((__visibility__ ("default"))) {
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
- && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
- && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
+ && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed_type
+ && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed_type
&& __is_ptr<_II1>::__value
&& __is_ptr<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
diff --git a/libstdc++-v3/include/bits/charconv.h b/libstdc++-v3/include/bits/charconv.h
index 20da8303f7a..1acf1e46e4c 100644
--- a/libstdc++-v3/include/bits/charconv.h
+++ b/libstdc++-v3/include/bits/charconv.h
@@ -46,7 +46,7 @@ namespace __detail
// This accepts 128-bit integers even in strict mode.
template<typename _Tp>
constexpr bool __integer_to_chars_is_unsigned
- = ! __gnu_cxx::__int_traits<_Tp>::__is_signed;
+ = ! __gnu_cxx::__int_traits<_Tp>::__is_signed_type;
#endif
// Generic implementation for arbitrary bases.
diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc
index 6bfff7d6289..38a6920abe9 100644
--- a/libstdc++-v3/include/bits/locale_facets.tcc
+++ b/libstdc++-v3/include/bits/locale_facets.tcc
@@ -470,7 +470,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
bool __testfail = false;
bool __testoverflow = false;
const __unsigned_type __max =
- (__negative && __num_traits::__is_signed)
+ (__negative && __num_traits::__is_signed_type)
? -static_cast<__unsigned_type>(__num_traits::__min)
: __num_traits::__max;
const __unsigned_type __smax = __max / __base;
@@ -573,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
}
else if (__testoverflow)
{
- if (__negative && __num_traits::__is_signed)
+ if (__negative && __num_traits::__is_signed_type)
__v = __num_traits::__min;
else
__v = __num_traits::__max;
@@ -914,7 +914,7 @@ _GLIBCXX_BEGIN_NAMESPACE_LDBL
if (__v >= 0)
{
if (bool(__flags & ios_base::showpos)
- && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
+ && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed_type)
*--__cs = __lit[__num_base::_S_oplus], ++__len;
}
else
diff --git a/libstdc++-v3/include/bits/uniform_int_dist.h b/libstdc++-v3/include/bits/uniform_int_dist.h
index 7ccf930a6d4..73b808e57f3 100644
--- a/libstdc++-v3/include/bits/uniform_int_dist.h
+++ b/libstdc++-v3/include/bits/uniform_int_dist.h
@@ -258,8 +258,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{
using _Up_traits = __gnu_cxx::__int_traits<_Up>;
using _Wp_traits = __gnu_cxx::__int_traits<_Wp>;
- static_assert(!_Up_traits::__is_signed, "U must be unsigned");
- static_assert(!_Wp_traits::__is_signed, "W must be unsigned");
+ static_assert(!_Up_traits::__is_signed_type, "U must be unsigned");
+ static_assert(!_Wp_traits::__is_signed_type, "W must be unsigned");
static_assert(_Wp_traits::__digits == (2 * _Up_traits::__digits),
"W must be twice as wide as U");
diff --git a/libstdc++-v3/include/ext/numeric_traits.h b/libstdc++-v3/include/ext/numeric_traits.h
index dcbc2d12927..c618f211775 100644
--- a/libstdc++-v3/include/ext/numeric_traits.h
+++ b/libstdc++-v3/include/ext/numeric_traits.h
@@ -67,15 +67,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// NB: these two are also available in std::numeric_limits as compile
// time constants, but <limits> is big and we can avoid including it.
- static const bool __is_signed = (_Value)(-1) < 0;
+ static const bool __is_signed_type = (_Value)(-1) < 0;
static const int __digits
- = __is_integer_nonstrict<_Value>::__width - __is_signed;
+ = __is_integer_nonstrict<_Value>::__width - __is_signed_type;
// The initializers must be constants so that __max and __min are too.
- static const _Value __max = __is_signed
+ static const _Value __max = __is_signed_type
? (((((_Value)1 << (__digits - 1)) - 1) << 1) + 1)
: ~(_Value)0;
- static const _Value __min = __is_signed ? -__max - 1 : (_Value)0;
+ static const _Value __min = __is_signed_type ? -__max - 1 : (_Value)0;
};
template<typename _Value>
@@ -85,7 +85,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const _Value __numeric_traits_integer<_Value>::__max;
template<typename _Value>
- const bool __numeric_traits_integer<_Value>::__is_signed;
+ const bool __numeric_traits_integer<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_integer<_Value>::__digits;
@@ -161,7 +161,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static const int __max_digits10 = __glibcxx_max_digits10(_Value);
// See above comment...
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = __glibcxx_digits10(_Value);
static const int __max_exponent10 = __glibcxx_max_exponent10(_Value);
};
@@ -170,7 +170,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const int __numeric_traits_floating<_Value>::__max_digits10;
template<typename _Value>
- const bool __numeric_traits_floating<_Value>::__is_signed;
+ const bool __numeric_traits_floating<_Value>::__is_signed_type;
template<typename _Value>
const int __numeric_traits_floating<_Value>::__digits10;
@@ -210,7 +210,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __numeric_traits_floating<__ibm128>
{
static const int __max_digits10 = 33;
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = 31;
static const int __max_exponent10 = 308;
};
@@ -224,7 +224,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __numeric_traits_floating<__ieee128>
{
static const int __max_digits10 = 36;
- static const bool __is_signed = true;
+ static const bool __is_signed_type = true;
static const int __digits10 = 33;
static const int __max_exponent10 = 4932;
};
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 38/40] libstdc++: Optimize is_signed trait performance
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (36 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 37/40] c++, libstdc++: Implement __is_signed built-in trait Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 39/40] c++, libstdc++: Implement __is_scalar built-in trait Ken Matsui
` (2 subsequent siblings)
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch optimizes the performance of the is_signed trait by dispatching to
the new __is_signed built-in trait.
libstdc++-v3/ChangeLog:
* include/std/type_traits (is_signed): Use __is_signed built-in trait.
(is_signed_v): Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
libstdc++-v3/include/std/type_traits | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index f7d3815f332..7e93923f44b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -982,6 +982,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: public __bool_constant<__is_abstract(_Tp)>
{ };
+ /// is_signed
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_signed)
+ template<typename _Tp>
+ struct is_signed
+ : public __bool_constant<__is_signed(_Tp)>
+ { };
+#else
/// @cond undocumented
template<typename _Tp,
bool = is_arithmetic<_Tp>::value>
@@ -994,11 +1001,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ };
/// @endcond
- /// is_signed
template<typename _Tp>
struct is_signed
: public __is_signed_helper<_Tp>::type
{ };
+#endif
/// is_unsigned
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
@@ -3445,8 +3452,13 @@ template <typename _Tp>
template <typename _Tp>
inline constexpr bool is_final_v = __is_final(_Tp);
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_signed)
+template <typename _Tp>
+ inline constexpr bool is_signed_v = __is_signed(_Tp);
+#else
template <typename _Tp>
inline constexpr bool is_signed_v = is_signed<_Tp>::value;
+#endif
#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_unsigned)
template <typename _Tp>
--
2.42.0
^ permalink raw reply [flat|nested] 629+ messages in thread
* [PATCH v13 39/40] c++, libstdc++: Implement __is_scalar built-in trait
2023-09-15 2:34 ` [PATCH v13 00/40] Optimize type traits performance Ken Matsui
` (37 preceding siblings ...)
2023-09-15 2:35 ` [PATCH v13 38/40] libstdc++: Optimize is_signed trait performance Ken Matsui
@ 2023-09-15 2:35 ` Ken Matsui
2023-09-15 2:35 ` [PATCH v13 40/40] libstdc++: Optimize is_scalar trait performance Ken Matsui
2023-10-10 9:46 ` [PATCH v15 00/39] Optimize type traits performance Ken Matsui
40 siblings, 0 replies; 629+ messages in thread
From: Ken Matsui @ 2023-09-15 2:35 UTC (permalink / raw)
To: gcc-patches; +Cc: libstdc++, Ken Matsui
This patch implements built-in trait for std::is_scalar. The existent
__is_scalar codes were replaced with __is_scalar_type to avoid unintentional
macro replacement by the new built-in.
gcc/cp/ChangeLog:
* cp-trait.def: Define __is_scalar.
* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCALAR.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __is_scalar.
* g++.dg/ext/is_scalar.C: New test.
* g++.dg/tm/pr46567.C: Use __is_scalar_type instead.
* g++.dg/torture/pr57107.C: Likewise.
libstdc++-v3/ChangeLog:
* include/bits/cpp_type_traits.h (__is_scalar): Rename to ...
(__is_scalar_type): ... this.
* include/bits/stl_algobase.h: Use __is_scalar_type instead.
* include/bits/valarray_array.h: Likewise.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
gcc/cp/constraint.cc | 3 ++
gcc/cp/cp-trait.def | 1 +