tech-userlevel archive

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

Re: aligned_alloc c11 function

   Date: Wed, 4 Nov 2015 18:34:56 +0100
   From: Niclas Rosenvik <>

   Then talking about portability here I guess you mean machine
   portability? I worry about another portability as well, compiler
   portability. My problem with ilog2 in bitops.h is that it uses
   __builtin_constant_p which is a GNU extension and thus not compiler
   portable. From a performance perspective there is also a problem if
   the compiler don't think it's a built in constant it will use fls32
   in the if every time aligned_alloc runs. This is a higher
   performance penalty than the while loop that Robert suggested.

We assume that ilog2(<constant>) is itself a constant in various
places throughout the tree, including ilog2(sizeof(T)) for a type T.

If that's a problem for a new compiler, it's just a matter of
implementing __builtin_constant_p -- an extension, yes, but not an
onerous one, and it's supported by gcc, clang, and pcc.  If
__builtin_constant_p is not implemented, it will fail noisily rather
than falling back to fls32/fls64.

So there's no need for __SIZEOF_POINTER__ and all the preprocessor

(As an aside, fls32/fls64 should be pretty fast even if we do have to
evaluate them at run-time, not much slower than your while loop.)

   The while loop will at most be run 10 times on a system with a 
   1024 bit address bus. This is very little. And it will only happen
   if alignment is lower than sizeof(void *) as Robert points out.
   Due to the above considerations I think I will commit aligned_alloc.c
   with the while (without the preprocessor code).

Either ilog2(sizeof(void *)), without preprocessor conditionals, or
the while loop is good.

Home | Main Index | Thread Index | Old Index