patternpythonMinor
Wrapping every expression with % 2 ** 32 to get a C-like behavior
Viewed 0 times
expressionwithbehavioreverylikegetwrapping
Problem
I'm trying to port the following C loop to Python:
The problem is that the these integers are 32-bit unsigned and the code above relies on integer wrapping. My first attempt was to introduce the
It looks strange and I'm not sure it's the best idea. What do you think of it? Is it something that needs improving? If so, how?
for (int32_t i = 32; --i >= 0;) {
v0 += ((v1 > 5) + v1) ^ (sum + k[sum & 3]);
sum -= delta;
v1 += ((v0 > 5) + v0) ^ (sum + k[sum >> 11 & 3]);
}The problem is that the these integers are 32-bit unsigned and the code above relies on integer wrapping. My first attempt was to introduce the
U32 class (see here), but after porting this code to Python 3, the solution got so ugly (see here) that I started to looking for other ones. My best idea at the moment is to wrap every expression with % 2 ** 32, so that it wraps around. Here's how this looks:for _ in range(32):
v0 = (
v0
+
(
(
(v1 > 5) % 2**32
) % 2**32
+
v1 ^ (sum_ + k[sum_ & 3]) % 2**32
) % 2**32
) % 2**32
sum_ = (sum_ - delta) % 2 ** 32
v1 = (
v1
+
(
(
(
(v0 > 5) % 2**32
) % 2**32 + v0
) % 2**32
^
(sum_ + k[(sum_ >> 11) % 2**32 & 3]) % 2**32
) % 2**32
) % 2**32It looks strange and I'm not sure it's the best idea. What do you think of it? Is it something that needs improving? If so, how?
Solution
Firstly, the expensive modulo operation
As a next step, you could use the following properties to optimize the equations:
That allows you get rid of many of the
% 232 can be replaced by a cheap bitwise and: & 232-1. Make sure to familiarize yourself with operator precedence, because it's not intuitive when it comes to bitwise operators (if you haven't done that yet).As a next step, you could use the following properties to optimize the equations:
(a % m + b % m) % m = (a + b) % m
(a & n ^ b & n) & n = (a ^ b) & n
That allows you get rid of many of the
& 2**32-1 operations at the cost of bigger intermediate results. If you want to make sure to get the highest speed, do some perfs. In any case, it will make the code much more readable.Context
StackExchange Code Review Q#36830, answer score: 4
Revisions (0)
No revisions yet.