[PATCH] P0556R3 Integral power-of-2 operations, P0553R2 Bit operations
Jonathan Wakely
jwakely@redhat.com
Wed Jul 4 08:08:00 GMT 2018
On 03/07/18 23:23 +0100, Jonathan Wakely wrote:
>On 03/07/18 23:40 +0200, Jakub Jelinek wrote:
>>On Tue, Jul 03, 2018 at 10:02:47PM +0100, Jonathan Wakely wrote:
>>>+#ifndef _GLIBCXX_BIT
>>>+#define _GLIBCXX_BIT 1
>>>+
>>>+#pragma GCC system_header
>>>+
>>>+#if __cplusplus >= 201402L
>>>+
>>>+#include <type_traits>
>>>+#include <limits>
>>>+
>>>+namespace std _GLIBCXX_VISIBILITY(default)
>>>+{
>>>+_GLIBCXX_BEGIN_NAMESPACE_VERSION
>>>+
>>>+ template<typename _Tp>
>>>+ constexpr _Tp
>>>+ __rotl(_Tp __x, unsigned int __s) noexcept
>>>+ {
>>>+ constexpr auto _Nd = numeric_limits<_Tp>::digits;
>>>+ const unsigned __sN = __s % _Nd;
>>>+ if (__sN)
>>>+ return (__x << __sN) | (__x >> (_Nd - __sN));
>>
>>Wouldn't it be better to use some branchless pattern that
>>GCC can also optimize well, like:
>> return (__x << __sN) | (__x >> ((-_sN) & (_Nd - 1)));
>>(iff _Nd is always power of two),
>
>_Nd is 20 for one of the INT_N types on msp340, but we could have a
>special case for the rare integer types with unusual sizes.
>
>>or perhaps
>> return (__x << __sN) | (__x >> ((-_sN) % _Nd));
>>which is going to be folded into the above one for power of two constants?
>
>That looks good.
Committed as attached (with a test I forgot to add for popcount).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 5987 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20180704/7e685612/attachment.bin>
More information about the Libstdc++
mailing list