tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: aligned_alloc c11 function



> On 4 Nov 2015, at 12:41 , Terry Moore <tmm%mcci.com@localhost> wrote:
> 
> I always avoid wasted motion at run time if I possibly can, because I think
> wasted motion is a very bad habit to get into. A wasted clock cycle is lost
> forever. I don't think that's a silly point at all.

I think you go too far when you imply that doing it your way will save clock
cycles.  For all that effort to improve on the code generated by the two-line
while() statement in real life use may additionally depend on both

- someone actually calling the function with alignment < sizeof (void *),
  something that seems kind of unlikely; and

- the compiler not being smart enough to deduce your optimization on its
  own from the while() code (assuming the two code sequences are always
  equivalent).

If we’re to write code that assumes a not-smart compiler, however, then it
seems like we’re leaving much lower-hanging fruit in the if() statement
above which would very likely always save a few clock cycles, independent
of the value of alignment, if it were executed more like:

	if (((alignment | size) & (alignment - 1)) != 0) {

Personally, though, I would leave the if() code the way it is since it is easier
to understand what it is checking for when written as two checks; the compiler
can figure out how best to do the algebra.  I would also prefer the two-line
while() to the macro-ized constant calculation if performance, and not functionality,
were the only concern, since the while() concisely makes the intent perfectly
clear to the reader.  I think it is better to write the C code for humans and
leave the clock-cycle-wastage avoidance to the compiler.

That said, I do think this should be done your way for functional reasons.  Your
code does what I think is the right thing in the case where both size and
alignment are zero-valued, but the while() loop code probably doesn’t.

Dennis Ferguson


Home | Main Index | Thread Index | Old Index