[PATCH] libstdc++: Simplify metaprogramming in <random>

Jonathan Wakely jwakely@redhat.com
Sat Oct 10 10:44:31 GMT 2020


On 09/10/20 18:09 +0100, Jonathan Wakely wrote:
>This removes the __detail::_Shift class template, replacing it with a
>constexpr function template __pow2m1. Instead of using the _Mod class
>template to calculate a modulus just perform a bitwise AND with the
>result of __pow2m1. This works because the places that change all
>perform a modulus operation with a power of two, x mod 2^w, which can be
>replaced with x & (2^w - 1).

Actually that's not quite true, there are two kinds of usage of
_Shift. The first is used with _Mod to do x mod 2^w as mentioned
above.

The second is to calculate the factor to multiply a 32-bit value by
when shifting it into the higher bits of a 64-bit or 128-bit value:

>-	_UIntType __factor = 1u;
> 	_UIntType __sum = 0u;
> 	for (size_t __j = 0; __j < __k; ++__j)
> 	  {
>-	    __sum += __arr[__j + 3] * __factor;
>-	    __factor *= __detail::_Shift<_UIntType, 32>::__value;
>+	    __sum += __arr[__j + 3] << (32 * __j);
> 	  }

But we don't need the _Shift template here either. Instead of
multiplying by (j+1) * 2^32 we can just shift by j * 32 instead.

I'll update the commit msg to mention that usage too.



More information about the Gcc-patches mailing list