Re: Possible to set the armv7 chip into using unaligned memory accesses?
Re: Possible to set the armv7 chip into using unaligned memory accesses?
- Subject: Re: Possible to set the armv7 chip into using unaligned memory accesses?
- From: Andreas Grosam <email@hidden>
- Date: Wed, 11 Jan 2012 10:11:33 +0100
On Jan 10, 2012, at 11:24 PM, David Hoerl wrote:
> I have an open source library that parses a complex file and reads items like longs and doubles that are not properly aligned.
>
> In googling around I've read people claiming you can get the processor to do this but details are scant.
>
> The one concrete one suggested setting "cp15 sctlr[1] (alignment bit) to 0".
>
> If this would work (just for my app) how would I do this? Any suggest other than re-write 2000 lines of code most welcome!
>
> David
Unfortunately, I've no idea if you can set a compiler-switch to make an arm v7 CPU to accept unaligned data within Xcode.
You should have found this article on SO:
<http://stackoverflow.com/questions/5119496/arm-unaligned-memory-access-workaround>
I fear, in order to solve the issue *properly*, you need to fix the open source library. Alignment errors are a portability issue, that is one CPU might be able to access "unaligned data" where another can't.
Alignment errors may occur when a certain data of a primitive type T (e.g. double, long, ..) or a user defined struct is serialized at a memory address where the CPU is unable to access it, e.g.:
// This works on Intel
int main(int argc, const char * argv[])
{
char* buffer = (char*)malloc(100);
char* p = buffer + 1;
long double v = 1.0;
memcpy(buffer, &v, sizeof(v));
long double x = *((long double*)p); // <= may cause hardware exception
printf("x: %Lf, address of x: %p, address of p: %p\n", x, &x, p);
free(buffer);
}
In order to get the alignment of types, I know of these possibilities:
1) Compiler vendor specific: __alignof operator (available in gcc and clang)
2) The boost library has an alignment_of traits
3) C++11 has alignof operator (not sure if this is implemented in clang yet)
This article is very informative and has further pointers, too:
<http://www.ibm.com/developerworks/library/pa-dalign/>
Andreas _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden