473,405 Members | 2,282 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,405 software developers and data experts.

32 bit allignment

Hi ,
I am facing some allignment issue on ARM. So how to check the a address is 32 bit alligned or not. so then i can round off with next 32bit boundary .

please let me know that if any proper pseudo method ( C or arm assembly )is there to check .

Thanks
mcarthy
Jan 28 '10 #1

✓ answered by Banfa

You can not align anything to a specific bit but you can check alignment to bytes so really rather than checking for 32bit alignment you want to be checking for 4 byte alignment (because the ARM has 8 bits per byte).

You will probably need to do something platform specific because you will need to examine the actual value of the pointer which is platform dependent. It would therefore be best to put this in a function that can be changed if the software gets ported to a platform that does not support the method.

You will need to convert the address to an integer type, on an ARM a simple cast will do the trick you probably should cast it to a long. On an ARM int and long are both 32bit IIRC but long is like to be more portable to say a 64bit platform. Once you have the value as an integer it is easy to see if it is 4 byte aligned using the % operator because the value will be exactly divisible by 4 if it is (i.e. % 4 will return 0).

Your function could do 1 of 3 things
  • Return a boolean true/false to indictae if the address is 4 byte aligned
  • Return the address if it is already 4 byte aligned or the next higher 4 byte aligned address if it is not
  • Return the address if it is already 4 byte aligned or the next lower 4 byte aligned address if it is not
Actually 3 is not very useful but 2 can be if you are using an alignment that is not native to the processor. For instance on 1 project we needed memory that was 8 byte aligned on an ARM. By allocting all buffers with an extra 7 bytes you can then shift your working-address in the allocated buffer to an 8 byte aligned address and still have the right amount of data to avoid array boundary problems.

Anyway that is sort of what you do. Like I said very platform specific.

7 9714
Banfa
9,065 Expert Mod 8TB
You can not align anything to a specific bit but you can check alignment to bytes so really rather than checking for 32bit alignment you want to be checking for 4 byte alignment (because the ARM has 8 bits per byte).

You will probably need to do something platform specific because you will need to examine the actual value of the pointer which is platform dependent. It would therefore be best to put this in a function that can be changed if the software gets ported to a platform that does not support the method.

You will need to convert the address to an integer type, on an ARM a simple cast will do the trick you probably should cast it to a long. On an ARM int and long are both 32bit IIRC but long is like to be more portable to say a 64bit platform. Once you have the value as an integer it is easy to see if it is 4 byte aligned using the % operator because the value will be exactly divisible by 4 if it is (i.e. % 4 will return 0).

Your function could do 1 of 3 things
  • Return a boolean true/false to indictae if the address is 4 byte aligned
  • Return the address if it is already 4 byte aligned or the next higher 4 byte aligned address if it is not
  • Return the address if it is already 4 byte aligned or the next lower 4 byte aligned address if it is not
Actually 3 is not very useful but 2 can be if you are using an alignment that is not native to the processor. For instance on 1 project we needed memory that was 8 byte aligned on an ARM. By allocting all buffers with an extra 7 bytes you can then shift your working-address in the allocated buffer to an 8 byte aligned address and still have the right amount of data to avoid array boundary problems.

Anyway that is sort of what you do. Like I said very platform specific.
Jan 28 '10 #2
yeah .. i have got and implemented .. . Cool one.
Jan 28 '10 #3
hi ,

i understood but one short of question i have that . I dont know in this forum i can ask this question or not. ..still in ARM why unalligned address operation will give data abort ? I am facing this but is this thing will be happened for any special case. If you have any idea or proper link. please help me to know.
Jan 28 '10 #4
Banfa
9,065 Expert Mod 8TB
In the most basic terms it is a stipulation/requirement of the platform. The ARM specification states it can't do unaligned memory accesses so it can't. If it tries to the chip raises a hardware exception.

Why would you do that? Normally for either efficiency or simplification. You would need to carefully read the ARM data sheet or consult an ARM specific forum to get a more detailed answer.
Jan 28 '10 #5
alexis4
113 100+
This is generally a RISC problem. Fortunately I have never faced the need of unaligned data access...
Maybe you could solve your problem if you used a __packed structure to access unaligned data. I have never tried it, it's just a mad idea. Check it out in both ARM and THUMB and tell us if that worked...
Jan 28 '10 #6
Banfa
9,065 Expert Mod 8TB
Since malloc is supposed to return a pointer that is aligned to support any object that it might be being used for in general it should never occur except when dealing with data from an outside source, a communications channel that may supply a byte stream with objects not at the correct alignment to external hardware that has more stringent alignment requirements than the processor.
Jan 28 '10 #7
donbock
2,426 Expert 2GB
@Banfa
I recommend unsigned long rather than long.

Masking the address value ought to be quicker than modulo division:
4-byte aligned if (address & (~0x03uL) == address)
4-byte aligned if (address & 0x03uL == 0)
Jan 28 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
1
by: Anthony | last post by:
Hi, I've got a question about the allignment of data in a structure. What happens in memory when i declare the following struct A { int x; } stuct B : public A {
1
by: Aitham alama | last post by:
How can i make a dynamically created web label control to to be Center allignment Thanks
1
by: alex | last post by:
Hi, two datagrid questions. 1: how can i allign the column header text i.e. have it centered. the data fields are aligned with HorizontalAligment.xxx and that works... but on the header.? ...
3
by: Mangesh | last post by:
Hi all, Though I am new registered member of tthis group, I used to read posts frequently and they were really helpful. I have one query. Allignment of my aspx pages created in .NET 2.0 getting...
1
by: Paresh Shah | last post by:
We have names and addresses data base of people across the world in note pad in vertical form as under. name Designation company name Address 1 Address 2 City pin code Country
7
by: sukeshchand | last post by:
Friends here i am posting some function thats usefull for visual basic programmers. If you have some please Post. 'Get A Value From Database Public Function GetField(ByRef conn As...
1
by: baichuan0698 | last post by:
#include <stdlib.h> #include <stdio.h> int main (int argc, char*argv) { unsigned char c = {0,1,2,3,4,5,6,7,8,9,10}; unsigned int *i1, *i2, *i3; i1 = (unsigned int *)&c;
1
by: refl | last post by:
I have to generate a sequence of div in a loop. I planned to align 3div in a column. The width are same for all cases. but the length may vary based on the div contents. My real problem is,...
1
by: mcarthybn | last post by:
Hi , Can someone please help me to know how can i check that a 32 bit address is alligned or invalid address ? Can it possible by C code? I am waiting for your reply. Thanks mcarthybn
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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,...
0
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...

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.