4 ms·
This reminds me, why do most high-level languages lack a bit rotation operator? It's an operation that is required in some cases, and it can easily be emulated
by magicalhippo 8y ago
This reminds me, why do most high-level languages lack a bit rotation operator?
It's an operation that is required in some cases, and it can easily be emulated by the compiler for targets which lack it. However deducing that you're trying to do bit rotation and emitting the CPU instruction when present is not easy for the compiler.
- kken 8y agoEspecially on microcontrollers or DSPs this could be quite useful. I have never seen a C-compiler able to infer a bit rotation instruction, though.
- pm215 8y agogcc will recognize the usual rotate idiom: unsigned int rotv(unsigned int x, int c) { return (x << c) | (x >> (32 - c)); } On x86 it emits a rol instruction, for instance.
- jlarcombe 8y agoThe original ARM C compiler ('Norcroft') did this a lot as I recall. Relevant to the original post here of course, as the original spec was a processor that could interpret BBC BASIC as fast as hand optimised 6502 assembler.
- phire 8y agoI always wondered if the PDP-11 (which c was originally developed on) was missing rotate instructions. Weirdly enough, it does have rotate instructions. Even weirder, there is no logical shift right instruction on the PDP-11. The original c compiler must have been emulating one by clearing the carry bit beteen rotates.
- pklausler 8y agoC came from B, which predated the PDP-11. https://www.bell-labs.com/usr/dmr/www/chist.html https://www.bell-labs.com/usr/dmr/www/chist.html
- 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...