[v2,2/3] libstdc++: Optimize is_arithmetic performance by __is_arithmetic built-in

Message ID 20230715045519.50684-2-kmatsui@gcc.gnu.org
State Accepted
Headers
Series [v2,1/3] c++, libstdc++: Implement __is_arithmetic built-in trait |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Ken Matsui July 15, 2023, 4:55 a.m. UTC
  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 | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
  

Comments

François Dumont July 16, 2023, 12:32 p.m. UTC | #1
On 15/07/2023 06:55, Ken Matsui via Libstdc++ wrote:
> 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 | 14 ++++++++++++++
>   1 file changed, 14 insertions(+)
>
> diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
> index 0e7a9c9c7f3..7ebbe04c77b 100644
> --- a/libstdc++-v3/include/std/type_traits
> +++ b/libstdc++-v3/include/std/type_traits
> @@ -655,10 +655,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>       { };
>   
>     /// is_arithmetic
> +#if __has_builtin(__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>
> @@ -3198,8 +3205,15 @@ template <typename _Tp>
>     inline constexpr bool is_reference_v<_Tp&> = true;
>   template <typename _Tp>
>     inline constexpr bool is_reference_v<_Tp&&> = true;
> +
> +#if __has_builtin(__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;
>   template <typename _Tp>

Same remark as the one I did for __is_pointer in cpp_type_traits.h. You 
could implement it as:

   template<typename _Tp>
     struct __is_arithmetic_t
     : public __truth_type<__is_arithmetic(_Tp)>
     { };

François
  
Ken Matsui July 17, 2023, 4:48 a.m. UTC | #2
On Sun, Jul 16, 2023 at 5:32 AM François Dumont <frs.dumont@gmail.com> wrote:
>
>
> On 15/07/2023 06:55, Ken Matsui via Libstdc++ wrote:
> > 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 | 14 ++++++++++++++
> >   1 file changed, 14 insertions(+)
> >
> > diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
> > index 0e7a9c9c7f3..7ebbe04c77b 100644
> > --- a/libstdc++-v3/include/std/type_traits
> > +++ b/libstdc++-v3/include/std/type_traits
> > @@ -655,10 +655,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
> >       { };
> >
> >     /// is_arithmetic
> > +#if __has_builtin(__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>
> > @@ -3198,8 +3205,15 @@ template <typename _Tp>
> >     inline constexpr bool is_reference_v<_Tp&> = true;
> >   template <typename _Tp>
> >     inline constexpr bool is_reference_v<_Tp&&> = true;
> > +
> > +#if __has_builtin(__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;
> >   template <typename _Tp>
>
> Same remark as the one I did for __is_pointer in cpp_type_traits.h. You
> could implement it as:
>
>    template<typename _Tp>
>      struct __is_arithmetic_t
>      : public __truth_type<__is_arithmetic(_Tp)>
>      { };
>
> François
>
Thank you for your review! This is from the type_traits header, so the
name should be as-is.
  
François Dumont July 22, 2023, 1:39 p.m. UTC | #3
On 17/07/2023 06:48, Ken Matsui wrote:
> On Sun, Jul 16, 2023 at 5:32 AM François Dumont <frs.dumont@gmail.com> wrote:
>>
>> On 15/07/2023 06:55, Ken Matsui via Libstdc++ wrote:
>>> 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 | 14 ++++++++++++++
>>>    1 file changed, 14 insertions(+)
>>>
>>> diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
>>> index 0e7a9c9c7f3..7ebbe04c77b 100644
>>> --- a/libstdc++-v3/include/std/type_traits
>>> +++ b/libstdc++-v3/include/std/type_traits
>>> @@ -655,10 +655,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>>>        { };
>>>
>>>      /// is_arithmetic
>>> +#if __has_builtin(__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>
>>> @@ -3198,8 +3205,15 @@ template <typename _Tp>
>>>      inline constexpr bool is_reference_v<_Tp&> = true;
>>>    template <typename _Tp>
>>>      inline constexpr bool is_reference_v<_Tp&&> = true;
>>> +
>>> +#if __has_builtin(__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;
>>>    template <typename _Tp>
>> Same remark as the one I did for __is_pointer in cpp_type_traits.h. You
>> could implement it as:
>>
>>     template<typename _Tp>
>>       struct __is_arithmetic_t
>>       : public __truth_type<__is_arithmetic(_Tp)>
>>       { };
>>
>> François
>>
> Thank you for your review! This is from the type_traits header, so the
> name should be as-is.

Here I meant that current libstdc++ implementation of 
std::__is_arithmetic in cpp_type_traits.h should also make use of the 
builtin __is_arithmetic that you are introducing. That is to say replace 
this:

   template<typename _Tp>
     struct __is_arithmetic
     : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
     { };

by:

#if __has_builtin(__is_arithmetic)

   template<typename _Tp>
     struct __is_arithmetic_t
     : public __truth_type<__is_arithmetic<_Tp>>
     { };

#else

   template<typename _Tp>
     struct __is_arithmetic_t
     : public __traitor<__is_integer<_Tp>, __is_floating<_Tp> >
     { };

#endif

if you replace '__is_arithmetic' by '__is_arithmetic_t' for the 
libstdc++, just adapt to the name you eventually adopt.
  
Jonathan Wakely Aug. 8, 2023, 8:09 p.m. UTC | #4
On Sat, 15 Jul 2023 at 05:57, Ken Matsui via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:

> 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>
>

OK for trunk (if the front-end changes are approved).




> ---
>  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 0e7a9c9c7f3..7ebbe04c77b 100644
> --- a/libstdc++-v3/include/std/type_traits
> +++ b/libstdc++-v3/include/std/type_traits
> @@ -655,10 +655,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>      { };
>
>    /// is_arithmetic
> +#if __has_builtin(__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>
> @@ -3198,8 +3205,15 @@ template <typename _Tp>
>    inline constexpr bool is_reference_v<_Tp&> = true;
>  template <typename _Tp>
>    inline constexpr bool is_reference_v<_Tp&&> = true;
> +
> +#if __has_builtin(__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;
>  template <typename _Tp>
> --
> 2.41.0
>
>
  
Jonathan Wakely Aug. 8, 2023, 8:28 p.m. UTC | #5
On Tue, 8 Aug 2023 at 21:09, Jonathan Wakely <jwakely@redhat.com> wrote:

>
>
> On Sat, 15 Jul 2023 at 05:57, Ken Matsui via Libstdc++ <
> libstdc++@gcc.gnu.org> wrote:
>
>> 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>
>>
>
> OK for trunk (if the front-end changes are approved).
>

Oh, this is the v2 patch and there's a v4 ... but I think they're the same.



>
>
>
>
>> ---
>>  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 0e7a9c9c7f3..7ebbe04c77b 100644
>> --- a/libstdc++-v3/include/std/type_traits
>> +++ b/libstdc++-v3/include/std/type_traits
>> @@ -655,10 +655,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>>      { };
>>
>>    /// is_arithmetic
>> +#if __has_builtin(__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>
>> @@ -3198,8 +3205,15 @@ template <typename _Tp>
>>    inline constexpr bool is_reference_v<_Tp&> = true;
>>  template <typename _Tp>
>>    inline constexpr bool is_reference_v<_Tp&&> = true;
>> +
>> +#if __has_builtin(__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;
>>  template <typename _Tp>
>> --
>> 2.41.0
>>
>>
  

Patch

diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 0e7a9c9c7f3..7ebbe04c77b 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -655,10 +655,17 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     { };
 
   /// is_arithmetic
+#if __has_builtin(__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>
@@ -3198,8 +3205,15 @@  template <typename _Tp>
   inline constexpr bool is_reference_v<_Tp&> = true;
 template <typename _Tp>
   inline constexpr bool is_reference_v<_Tp&&> = true;
+
+#if __has_builtin(__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;
 template <typename _Tp>