Thad Smith said:
I disagree. How do you define rotate right?
Since the proposal was to mimic the operation of hardware rotates, this is
straightforward, eg. a rotation of 8, 16 or 32 bits for x86-32, regardless
of whether the values are signed or unsigned.
Does the high bit of ((someexpression | 1) rotateright 1) become
(<typeofleftexpression>_MAX+1)/2? Does integer promotion make it
impossible to rotate right using a size less than int?
When I implemented rotate ops, they worked on 8, 16 or 32 bits depending on
the type of the left operand, as you might expect.
This causes a problem in C because intermediate results must be ints.
But this is responsible for all sorts of other problems too: eg.:
char c=0xC0;
int a = c<<1;
Should a be 0x80 or 0x180? There is a case for both results.
James's expression describes exactly what is produced, regardless of the
register size, assuming that the type of x is at least 32 bits long.
I think the main requirements will be to rotate entire bytes, shorts and
ints, rather than either the bottom N bits of a value, or an arbitrary
bitfield. The latter are always possible via the normal bit operations.
I like the gcc approach: the programmer codes the arithmetic result and
the compiler generates the shortcut where possible.
I think you've got that back to front: usually the source code uses the
simplest, most obvious expression, while the compiler generates the
nightmare code needed to achieve it.
Rotates can be generated for any supported word size for which the target
has a rotate instruction and the resulting code is more portable, not
depending on hardware specifics.
If it's that critical then rotates can be implemented as built-in functions
with 3 operands: value to rotate, shift count, and field width. I wouldn't
object to that, although it would be nice to be able to write:
a rol=1;
instead of:
a = rotateleft(a,1,32);