TH is the "standard" way to do user-controlled loop unrolling.
Loop and Recursion unrolling
-
03-12-2021 - |
Question
W_t = M_t[i] if 0 <= t <= 15
W_t = ROTL_1(W_(t-3) XOR W_(t-8) XOR W_(t-14) XOR W_(t-16)) if 16 <= t <= 79
This is from the SHA-1 standards. In haskell what you would trivially do is write a recursive function to do this, but to make it more efficient I would like to unroll the whole recursion. Inlining will not work as this might result in exponential blowup of code. What I have in mind is to write TH to generate the constants line W_0
, W_1
, W_2
and so on upto W_79
.
Another example is for loop unrolling in the case
For t=0 to 79:
{
T = ROTL_5(a) + f_t(b, c, d ) + e + K_t + W_t
e = d
d = c
c = ROTL_30(b)
b = a
a = T
}
I would like to unroll this loop as well to avoid recursive function calls (and I don't think ghc will be performing this kind of optimizations).
So before I go and write the TH for this I wanted to ask if there is any better way of doing this. Just to say that optimization is very critical here.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow