Simple features that I've always missed from C...

bearophile bearophileHUGS at lycos.com
Mon Oct 17 16:45:01 PDT 2011


Manu:

> *Roll/Rotate:* I'm loving the '>>>' operator, but I could often really do
> with a rotate operator useful in many situations... '>>|' perhaps...
> something like that?
>   This is ugly: a = (a << x) | ((unsigned)a >> (sizeof(a)/8 - x));

I have asked for a rotate intrinsic in Phobos, but Walter has added a rewrite rule instead, that turns D code to a rot.
Personal experience has shown me that it's easy to write the operation in a slightly different way (like with signed instead of unsigned values) that causes a missed optimization. So I prefer still something specific, like a Phobos intrinsic, to explicitly ask for this operation to every present and future D compiler, with no risk of mistakes.


> *Min/Max operators:* GCC has the lovely <? and >? operators... a <? b ==
> min(a, b) .. Why this hasn't been adopted by all C compilers is beyond me.
> Surely this couldn't be much trouble to add? Again, super useful in
> vector/maths heavy code too.

This is cute. Surely max/min is a common operation to do, but often I have to find a max or min of a collection, where I think this operator can't be used. I don't think this operator is necessary, and it makes D code a bit less readable for people that don't know D.



> *Predecated selection:* Float, vector, and often enough even int math can
> really benefit from using hardware select opcodes to avoid loads/stores. In
> C there is no way to express this short of vendor specific intrinsics again.

I don't understand what you are asking here. Please show an example.

There is an enhancement request that asks to support vector operations like this too (some CPUs support something like this in hardware):
int[] a = [1,2,3,4];
int[] b = [4,3,2,1];
auto c = a[] > b[];
assert(c == [false, false, true, true]);

Are operations like this what you are asking for here?

Bye,
bearophile


More information about the Digitalmars-d mailing list