Power of two alignment (bit masks)

R

RazvanD

Hi!

I am trying to get the page-aligned address for an ordinary memory
address.

As I am using an x86-based CPU the page size is 4KB[1]. Thus, assuming
addr is the memory address, I could get the page-aligned address by
using
the following methods:

unsigned long uaddr = (unsigned long) addr;

char *page_addr = (char *) (uaddr - uaddr % 4096);
char *page_addr = (char *) (uaddr / 4096 * 4096);
char *page_addr = (char *) ((uaddr >> 12) << 12);
char *page_addr = (char *) (uaddr & (~0xfff));

My humble hardware knowledge tells me the last method (bitwise AND) is
the
fastest.

Its only problem is that it's not page-size portable. If the page size
is
8KB or 16KB or 64KB I would be using a different mask.

In a megalomaniacal attempt to achieve "ultimate" portability I
devised a
macro-based solution[2]. I'm not sure about three things: * how
portable
is this solution? (using unsigned long etc.) * does it make sense to
use
this? From my knowledge, operating systems and other low level
applications would have portability layers and one could define a
specific
mask according to the CPU architecture. * are there other real world
applications that would use a similar solution to get a proper bit
mask?
I'm considering any possible use, not only for page sizes.

I've been reading the c.l.c posts for some time but this is my first
post.
I apologize in advance for any mistakes or problems regarding this
message.

Razvan

[1] I am intentionally ignoring the possibility of using the
granularity
bit in the segment descriptor
[2] http://rafb.net/p/UZwEfH47.html
 
C

christian.bau

Hi!

I am trying to get the page-aligned address for an ordinary memory
address.

As I am using an x86-based CPU the page size is 4KB[1]. Thus, assuming
addr is the memory address, I could get the page-aligned address by
using
the following methods:

unsigned long uaddr = (unsigned long) addr;

char *page_addr = (char *) (uaddr - uaddr % 4096);
char *page_addr = (char *) (uaddr / 4096 * 4096);
char *page_addr = (char *) ((uaddr >> 12) << 12);
char *page_addr = (char *) (uaddr & (~0xfff));

My humble hardware knowledge tells me the last method (bitwise AND) is
the
fastest.

Its only problem is that it's not page-size portable. If the page size
is
8KB or 16KB or 64KB I would be using a different mask.

In a megalomaniacal attempt to achieve "ultimate" portability I
devised a
macro-based solution[2]. I'm not sure about three things: * how
portable
is this solution? (using unsigned long etc.) * does it make sense to
use
this? From my knowledge, operating systems and other low level
applications would have portability layers and one could define a
specific
mask according to the CPU architecture. * are there other real world
applications that would use a similar solution to get a proper bit
mask?
I'm considering any possible use, not only for page sizes.

I've been reading the c.l.c posts for some time but this is my first
post.
I apologize in advance for any mistakes or problems regarding this
message.

All these methods have the disadvantage that they break on machines
where pointer variables use more bits than unsigned long does, and
they obviously break badly if the relation between a pointer and the
pointer cast to unsigned long is not what you think it is. This can be
avoided very easily by using one of

char *page_addr = ((char *) addr) - uaddr % 4096;
char *page_addr = ((char *) addr) - uaddr & 0xfff;

I wouldn't worry about speed, any smart compiler will produce the same
code for either of them. As long as (uaddr % 4096) gets the alignment
right, this code will work.

Your last version

char *page_addr = (char *) (uaddr & (~0xfff));

has the problem that 0xfff is not an unsigned long. What happens if
pointer = unsigned long = 64 bit, and int = 32 bit? (I think you have
to study the C Standard _very_ carefully to see what this code
does. ).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top