4 ms·
> However deducing that you're trying to do bit rotation and emitting the CPU instruction when present is not easy for the compiler. Uh, no. a rotate_left b is
by jcranmer 8y ago
> However deducing that you're trying to do bit rotation and emitting the CPU instruction when present is not easy for the compiler.
Uh, no. a rotate_left b is equivalent to a << b | a >>> (sizeof_in_bits(a) - b). As far as common instructions not present in language standards go, regular rotates are the easiest for the compiler to recognize.
- magicalhippo 8y agoWell then, I've must have been using the wrong compilers, as they did not output the rol/ror instruction last I tried. edit: For example, VS2017 x86 target does not output rol instruction for your code with /O1.
- magicalhippo 8y agoAh, it does it with 32bit operands but not 8bit. That must be why I missed it.
- vardump 8y agoCan you show your code for 8 bit case that fails to generate rol?
- magicalhippo 8y agoSeems I must have messed something up in the initial testing. Now it outputs rol with uint8_t too. Well, that's good to know. For the record, here was what I was testing: #include <iostream> uint8_t rol(uint8_t x, uint8_t c) { return (x << c | x >> (sizeof(x) * 8 - c)); } int main() { volatile uint8_t a = 0b01010000; volatile uint8_t b = 3; uint8_t c = rol(a, b); std::cout << static_cast<unsigned int>(0b10000010) << std::endl; std::cout << static_cast<unsigned int>(c) << std::endl; return 0; }
- db48x 8y agoC programmers have the worst time with this; your compilers are so unextensible and opaque. Here's an example of extending the SBCL compiler to support a new instruction: https://www.pvk.ca/Blog/2014/08/16/how-to-define-new-intrinsics-in-sbcl/ https://www.pvk.ca/Blog/2014/08/16/how-to-define-new-intrins...
- magicalhippo 8y agoStill I wonder, why go through this guesswork in order to try to figure out what the user wants, instead of having an operator which clearly states what the user wants?