Re:Ask HN: When has switching the language/framework made an...
by valbaca on Tuesday July 18, 2017 @06:54PM
(#14800700)
Interesting. And the tl;dr for the bug is:
byte_count := (n+7) / 8;
When n is near max size, it overflows to a small number, then (small number) / 8 = 0
the fix is to restrict n to [0,MAX-7)
Re:Ask HN: When has switching the language/framework made an...
by jjnoakes on Tuesday July 18, 2017 @10:39PM
(#14801733)
One can do it without restricting the range as well, if the full range of inputs is needed.
In C:
(n / 8) + !!(n % 8);
Re:Ask HN: When has switching the language/framework made an...
by adrianratnapala on Wednesday July 19, 2017 @03:22AM
(#14802520)
This is correct, and is sometimes necessary, but is a last-ditch save, since as written, it involves a branch.
Much better if you can let the n+7 expand to a wider integer type. Or better yet, if you can ensure that it won't overflow in the first place -- which is what they did by restricting the type.
Re:Ask HN: When has switching the language/framework made an...
by einmaliger on Wednesday July 19, 2017 @09:05AM
(#14803965)
My x86-assembly has become very rusty, but couldn't it work like this, not using a branch:
(with eax being the unsigned 32-bit input word)
xor ebx, ebx
test al, 7
stnz bl
shr eax, 3
add eax, ebx
(edit: replaced a movzx with an xor)
Re:Ask HN: When has switching the language/framework made an...
by adrianratnapala on Wednesday July 19, 2017 @10:06AM
(#14804340)
My assembly is probably worse than yours: and that is why I weaseled with "as written". I expect good compiler -- such as the ones we use every day will get this right.
But clever compiler still give me the willies, with their less-than-literal interpretations of the meaning of what I wrote.
I prefer code that is efficient good (whether that means fast, or something else appropriate to the circumstance) when taken literally, and then trust trust the compiler to smooth over my naivity about what efficiency really means.
http://www.adacore.com/knowledge/technical-papers/sparkskein...