473,799 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4340
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
10762
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 other. All pointers to union types shall have the same representation and alignment requirements as each other. Does it mean that *all* structure (or union) types have the same alignment? Eg. type
13
684
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 memory alignment?
17
1829
by: puzzlecracker | last post by:
> 2) >>struct B >>{ >> int i; >>short int si; >> char c; >> char d; >> int j; >>};
0
1148
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 the file first i have the bytes read in........i should only be getting a black and white value either 255 or 0 but i am getting these other values like 66 in the byte array........ input = new FileStream(fileName,FileMode.Open,FileAccess.Read);...
6
3425
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 the same way as in C++, but I cannot see any mention of bit field declarations in VB.NET. <structure> typedef struct _DCB { DWORD DCBlength; DWORD BaudRate; DWORD fBinary :1;
9
6193
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 structures in VB.NET don't have bit fields, but I am looking for a convenient way to reproduce the convenience afforded in C/C++. If I have typedef struct _MyStruct {
18
4732
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 would be better to write a macro to access each bit instead of the bitfield. I have read the C-FAQ on bit fields, but was wondering if there were any advantages/disadvantages to using bit shifting. To my mind I think using bitfields are more...
8
2643
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 bit for some other strange register) and I need to simulate the fixed point behaviour of the register, and to access the single bit.
32
9680
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
9540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.