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: Steve Christensen <email@hidden>
- Date: Thu, 12 Jan 2012 16:42:06 -0800
On Jan 10, 2012, at 2: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!
How about just writing a set of wrapper accessor functions that you call any time you need to read or write longs, doubles, etc., then call them instead of accessing memory directly? Something like this. (Written in Mail so completely untested, but you get the idea.) Probably a lot less work than rewriting the whole thing.
uint32_t SafeReadUInt32(uint32_t const* buffer)
{
if (buffer & (sizeof(uint32_t) - 1) == 0)
{
return *buffer;
}
else
{
#if defined(LITTLE_ENDIAN) && LITTLE_ENDIAN
return (((uint8_t*)buffer)[0] << 0) |
(((uint8_t*)buffer)[1] << 8) |
(((uint8_t*)buffer)[2] << 16) |
(((uint8_t*)buffer)[3] << 24);
#else
return (((uint8_t*)buffer)[0] << 24) |
(((uint8_t*)buffer)[1] << 16) |
(((uint8_t*)buffer)[2] << 8) |
(((uint8_t*)buffer)[3] << 0);
#endif
}
}
void SafeWriteUInt32(uint32_t* buffer, uint32_t value)
{
if (buffer & (sizeof(uint32_t) - 1) == 0)
{
*buffer = value;
}
else
{
#if defined(LITTLE_ENDIAN) && LITTLE_ENDIAN
((uint8_t*)buffer)[0] = value >> 0;
((uint8_t*)buffer)[1] = value >> 8;
((uint8_t*)buffer)[2] = value >> 16;
((uint8_t*)buffer)[3] = value >> 24;
#else
((uint8_t*)buffer)[0] = value >> 24;
((uint8_t*)buffer)[1] = value >> 16;
((uint8_t*)buffer)[2] = value >> 8;
((uint8_t*)buffer)[3] = value >> 0;
#endif
}
}
_______________________________________________
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