Other suggesgtions (untested, i.e. unmeasured)

 - use shift operator instead of *2 and /2

 - put your 4 alternatives (i.e. key % 4) as lambdas into another hash
and then do
   algo[key % 4][subkey]

Kind regards

robert