5 ms·
You can just try it and see [1], but tl;dr, no, that problem doesn't occur here. The elements are power-of-2 sized, so at worst there would be a shift instead
by dataflow 5y ago
You can just try it and see [1], but tl;dr, no, that problem doesn't occur here.
The elements are power-of-2 sized, so at worst there would be a shift instead of a multiplication. On x86 there's 'lea' which could encode the shift-and-add in a single (fast) instruction. (Actually, even 'mov' can encode this. See [1].) And one nice thing about simple linear indexing (with power-of-2-sized elements) is that the compiler could use index registers on x86 (si/di), which can sometimes result in even better code than with pointers.
But note that there isn't even a need for a shift to begin with here (let alone multiplication), because it'd be a no-op... since sizeof(char) == 1. So the whole concern is moot.
(If these were generic iterators in C++ I'd have coded them differently, both due to the reason you're mentioning and also because I shouldn't assume they'd be randomly indexable to begin with.)
[1] https://gcc.godbolt.org/z/dqxj5WzTd https://gcc.godbolt.org/z/dqxj5WzTd