SipHash icon indicating copy to clipboard operation
SipHash copied to clipboard

Simplify switch?

Open veorq opened this issue 1 year ago • 2 comments

from https://twitter.com/vmload/status/1552613272532172801

Switch case could be simplified to this?

for (i = 1; i <= left; i++) {
b |= ((uint64_t)ni[i - 1]) << (8 * (i - 1));
}

veorq avatar Jul 28 '22 11:07 veorq

On Thu, 28 Jul 2022 at 13:16, Jean-Philippe Aumasson < @.***> wrote:

from https://twitter.com/vmload/status/1552613272532172801

Switch case could be simplified to this?

for (i = 1; i <= left; i++) { b |= ((uint64_t)ni[i - 1]) << (8 * (i - 1)); }

FWIW that doesn't look like a simplification to me. The switch fallthrough trick is so common, and elegant, for this task that anyone remotely exposed to reading blocks will have encountered it. The code above I have to think about to understand that it is correct, and maybe even run it, whereas the switch is obviously correct at a glance. And the switch is more efficient without relying on the compiler to optimize it. I guess it really depends on how you define "simplify".

Yves

-- perl -Mre=debug -e "/just|another|perl|hacker/"

demerphq avatar Jul 28 '22 13:07 demerphq

I agree that it doesn't simplify anything. The code is shorter, but less readable.

jedisct1 avatar Jul 28 '22 14:07 jedisct1

let's stick to switch, thanks @jedisct1 @demerphq for your input.

veorq avatar Oct 22 '22 05:10 veorq