tech-userlevel archive

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

RE: aligned_alloc c11 function



> > (On all real platforms, MIN_ALIGNMENT == sizeof(void *)
> 
> Only for strange values of "real".  Platforms where sizeof(void *) is not
> a power of two predate C (and are part of the reason why C has that
> latitude built into it).
> 
> Perhaps all, what was the phrase, "industrially relevant" platforms....

Yes, I suppose. 

> > but a purist might want to use compile-time log2 macros to compute
> > MIN_ALIGNMENT.)
> 
> How does one do that?  I've never managed to figure out a way to compute
> lg - neither floor(lg(x)) nor ceil(lg(x)) - at compile time, unless you
> count looping by conditionally multiply #including a file (and that won't
> work here, because the argument is a sizeof return value, which is not a
> preprocessor constant).  What have I missed?

Using ?: for a binary search comes to mind. I think that's what the macros
in the current NetBSD header files do. Done at compile time, there's no
run-time cost.  MIN_ALIGNMENT won't be a preprocessor constant in this case,
but it will certainly be a compile-time constant, and that's all we need.
Most "industrially relevant" compilers are good at folding constants.
Folding constant while loops is more advanced. 

In this application, you don't even need the full macro.

And anyway my objection is style: it's misleading to write a while() loop
when what you're really doing is if().

Best regards,
--Terry




Home | Main Index | Thread Index | Old Index