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

Manu turkeyman at gmail.com
Mon Oct 17 13:53:42 PDT 2011


Some trivial binary operations that never had an expression in C/C++, I'd
love consideration for an operator or some sort of intrinsic for these.

*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)); ... and
I'm yet to see a compiler that will interpret that correctly.
  Additionally, if a vector type is every added, a rotate operator will
become even more useful.

*Count leading/trailing zeroes:* I don't know of any even slightly recent
architecture that doesn't have opcodes to count loading/trailing zeroes,
although they do exist, so perhaps this is a little dubious. I'm sure this
could be emulated for such architectures, but it might be unreasonably slow
if used...

*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.

*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.
'a > b ? a : b' seems like a simple enough expression for the compiler to
detect potential for a predecated select opcode (but in my experience, it
NEVER does), however, when considering vector types, the logic isn't so
clear in that format. Since hardware vectors implement component-wise
selection, the logical nature of the ?: operator doesn't really make sense.
  This could easily be considered an expansion of min/max... 'a <? b', 'a >?
b', 'a ==? b', 'a !=? b', etc. seems pretty natural if you're happy to
accept GCC's '<?' operators, and give the code generator the opportunity to
implement these things using hardware support.


C is terrible at expressing these concepts, resulting in
architecture/compiler specific intrinsics for each of them. Every time I've
ever written a maths library, or even just optimised some maths heavy
routines, these things come up, and I end up with code full of
architecture/platform/compiler ifdef's. I'd like to think they should be
standardised intrinsic features of the language (not implemented in the
standard library), so the code generator/back end has the most information
to generate proper code...

Cheers guys
- Manu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20111017/bb3abbb6/attachment.html>


More information about the Digitalmars-d mailing list