6 ms·
> In modern times, modern compilers can perform all kinds of optimizations, including this one. So it’s up to you to decide if making things harder to read is w
by kevin_thibedeau 5y ago
> In modern times, modern compilers can perform all kinds of optimizations, including this one. So it’s up to you to decide if making things harder to read is worth it.
It is still worthwhile to manually perform these arithmetic optimizations because you may want to build with optimizations off and switching to actual multiplication in hot code paths can cause performance regressions.
An unrelated area where I wouldn't do this sort of thing is bit twiddling tricks to implement branchless code. That generally defeats modern compilers' abilities to generate branchless code with conditional moves and ends up being slower.
- chrisseaton 5y ago> switching to actual multiplication in hot code paths can cause performance regressions Why would you turn off optimisations if you were worried about performance optimisations?
- matheusmoreira 5y agoSometimes the compilers will do insane stuff that causes bugs if you enable too much optimization. Usually because of strict aliasing violations causing undefined behavior.
- kroltan 5y ago...don't violate strict aliasing then?
- matheusmoreira 5y agoAliasing is ubiquitous in systems programming. Reinterpreting arbitrary types as arrays of bytes, for example. Honestly, it's kind of amazing that C even has this rule. Seems to be an attempt to be competitive with Fortran.
- eperdew 5y agoThis is why the aliasing rules in C explicitly allow aliasing any pointer type with a char*.
- matheusmoreira 5y agoYeah, but char sucks. It's not a synonym for octet. Not guaranteed to be 8 bits. Its signedness is even ambiguous unless unsigned is specified. The correct type is uint8_t/u8. Sadly, using that to alias other types is undefined. I've also seen hashing algorithms which used uint16_t/u16, with similar aliasing optimization bugs. Sometimes you want to reinterpret things as a struct, too. This is the reason why Linux compiles with strict aliasing disabled.
- quietbritishjim 5y agoAliasing with unsigned char* is also allowed. (Oddly, signed char is not, even if char is signed.) uint8_t is not guaranteed to be unsigned char but in practice almost always is. GCC did originally have separate 8 bit types when stdint.h was introduced but quickly changed to a typedef for char-based types precisely to allow using it for aliasing. Yes, technically char may not be 8 bits but in practice that is very rare (and you can statically assert it). Overall IMO the best solution is always use uint8_t and turn off optimisations on those rare weird platforms where it's not an alias for unsigned char for whatever reason.
- matheusmoreira 5y ago> uint8_t is not guaranteed to be unsigned char but in practice almost always is. Does this imply any unsigned char typedef is able to alias anything? Or is uint8_t a compiler special case?
- simias 5y ago`char` is not guaranteed to be 8 bits and in some more exotic environments (DSPs for instance) may not be. IIRC POSIX guarantees that char is 8 bits though (but I still think that the sign is implementation-dependent). But as I said in a parent comment, I don't understand why it's even relevant. If you want to alias any type then use `char *` and not anything else. I don't understand why one would prefer using stdint for that.
- garaetjjte 5y agoUse -fno-strict-aliasing if you do have such code.
- matheusmoreira 5y agoI do. The -fno-strict-aliasing and -fwrapv are standard flags for me. They essentially fix C the language by making it behave reasonably.
- rurban 5y agoBut -fwrapv is broken for decades and will not be fixed.
- matheusmoreira 5y agoHow is it broken?
- rurban 5y agoEg https://marc.info/?l=gnulib-bug&m=124788622422245&w=2 https://marc.info/?l=gnulib-bug&m=124788622422245&w=2 it disables signed integer overflow checks
- tom_ 5y agoSimplify use of a debugger. Would be nice if the performance is at least bearable when you’re doing this.