473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Power of two alignment (bit masks)

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
Jun 27 '08 #1
1 4284
On Apr 29, 12:34 pm, RazvanD <razv...@gmail.comwrote:
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. ).
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
13
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the...
17
by: puzzlecracker | last post by:
> 2) >>struct B >>{ >> int i; >>short int si; >> char c; >> char d; >> int j; >>};
0
by: James Dean | last post by:
Ok i had a method for finding the colours in a bitmap file but its very inefficient so i will have to go through each bit individually and check its value. I will have to go through the bytes in...
6
by: Charles Law | last post by:
I have the following structure (for example) that I wish to convert for use in VB.NET. What would be the best way to do it? Ideally, it would convert directly to allow access to the bit fields in...
9
by: Charles Law | last post by:
Sorry for asking this one again, but with the newsgroup now holding things for an ever shorter time, I can't find my original question (and the answers) from a few months ago. I know that...
18
by: richard_l | last post by:
Hello All, I am writing an application which receives a word which is a bitmap. I have created a word typedef which contains a bitfield defining each bit. however, I was wondering however if it...
8
by: nickooooola | last post by:
Hello to all I'm about to write a simulator for a microcontroller in python (why python? because I love it!!!) but I have a problem. The registry of this processor are all 8 bit long (and 10...
32
by: .rhavin grobert | last post by:
guess you have a processor that can handle 32bit natively and you have a 32-bit-int. im now looking for some *ultrafast* way to determine if an int has more than one bit set. any ideas?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.