Groups | Search | Server Info | Login | Register


Groups > comp.lang.c.moderated > #500

Re: Alignment calculation or something like that

From Jasen Betts <jasen@xnet.co.nz>
Newsgroups comp.lang.c.moderated
Subject Re: Alignment calculation or something like that
Date 2011-02-04 12:46 -0600
Organization Dis (not Dat) Organisation
Message-ID <clcm-20110204-0004@plethora.net> (permalink)
References <clcm-20110201-0008@plethora.net>

Show all headers | View raw


On 2011-02-01, vijk <vijayx@gmail.com> wrote:
> /** alignment is a power of 2, it is passed in as a hard coded int **/
>
> 1. void*p1,**p2
>
> 2. int offset = alignment - 1 + sizeof(void*);
>
> 3. p2 = (void**)(((size_t)(p1) + offset) & ~(alignment - 1));
>
> 4. p2[-1] = p1;
>
>
> I am trying to understand what/how lines 2 and 3 are doing/calculated.

when alignment is a power of 2,  

  & ~(alignment - 1)
  
rounds down to the nearest multiple of alignment

it's hard to explain, run some numbers through it and 
see how it behaves. 

It strikes me as the sort of code that might come
from someone who would rather be programming in assembler.
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

Back to comp.lang.c.moderated | Previous | Next | Find similar


Thread

Re: Alignment calculation or something like that Jasen Betts <jasen@xnet.co.nz> - 2011-02-04 12:46 -0600

csiph-web