• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: When is an unaligned memory access illegal on ARM?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: When is an unaligned memory access illegal on ARM?


  • Subject: Re: When is an unaligned memory access illegal on ARM?
  • From: Roland King <email@hidden>
  • Date: Mon, 23 Jun 2014 11:18:01 +0800


On 23 Jun, 2014, at 10:42 am, Roland King <email@hidden> wrote:

indeed .. and if you google for clang ldrd there's a thread going back to 2012 talking about exactly this. And it's a very heated thread too. 

The argument - although I don't fully understand it - is warning about the dangers of casting void* to other types, I'm still reading. 

Ok now I do understand the thread. The gist of it is that if you cast from a pointer of one type to a pointer of another and the data is not aligned for the second type, the behaviour is undefined. 

What 'aligned' is for any given type is up to the compiler. The only way I can see that the compiler is 'right' in this instance is if uint32_t pointers are defined to be 8 byte aligned (even though you'd think it would be 4 byte), in which case the compiler is at liberty to replace those two loads with a single 64 bit one. 

The only two suggestions I've seen to 'fix' this is either to ensure your original data is 8 byte aligned or memcpy() the data to a correctly aligned pointer before you use it. 

I wish I knew how to figure out what the alignment rules clang uses for ARM are but i really don't. 



On 23 Jun, 2014, at 10:18 am, Jens Alfke <email@hidden> wrote:


On Jun 22, 2014, at 4:22 PM, Roland King <email@hidden> wrote:

LDR supports unaligned access on newer hardware (as long as the correct bit is set in the CPU/coprocessor?). LDRD doesn't support it and the address needs to be 8-byte aligned. 

Then the optimizer seems to have made a mistake by being overly eager to use ldrd. Here’s the function in question. At the point of the crash the optimizer noticed that two consecutive uint32_t values were being read from the pointer ‘cur’, so ldrd would be an efficient way to read them; except that there’s no guarantee in the code that ‘cur’ is 8-byte aligned. The input is an arbitrary unaligned pointer.

—Jens


uint32_t crc32_8(void* data, size_t len, uint32_t prev_value)
{
    uint32_t *cur = (uint32_t*) data;
    uint32_t crc = ~prev_value;

    while (len >= 8) {
        uint32_t _one_ = *cur++ ^ crc;    // <---CRASHES
        uint32_t two = *cur++;
        crc =
            crc_lookup[7][(one    ) & 0xFF] ^
            crc_lookup[6][(one>> 8) & 0xFF] ^
            crc_lookup[5][(one>>16) & 0xFF] ^
            crc_lookup[4][(one>>24) & 0xFF] ^
            crc_lookup[3][(two    ) & 0xFF] ^
            crc_lookup[2][(two>> 8) & 0xFF] ^
            crc_lookup[1][(two>>16) & 0xFF] ^
            crc_lookup[0][(two>>24) & 0xFF];
        len -= 8;
    }

    unsigned char *cur_byte = (unsigned char*) cur;
    while (len--)
        crc = (crc >> 8) ^ crc_lookup[0][(crc & 0xFF) ^ *cur_byte++];

    return ~crc;
}


—Jens


 _______________________________________________
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

  • Follow-Ups:
    • Re: When is an unaligned memory access illegal on ARM?
      • From: Jens Alfke <email@hidden>
References: 
 >When is an unaligned memory access illegal on ARM? (From: Jens Alfke <email@hidden>)
 >Re: When is an unaligned memory access illegal on ARM? (From: Roland King <email@hidden>)
 >Re: When is an unaligned memory access illegal on ARM? (From: Jens Alfke <email@hidden>)
 >Re: When is an unaligned memory access illegal on ARM? (From: Roland King <email@hidden>)

  • Prev by Date: Re: When is an unaligned memory access illegal on ARM?
  • Next by Date: Re: When is an unaligned memory access illegal on ARM?
  • Previous by thread: Re: When is an unaligned memory access illegal on ARM?
  • Next by thread: Re: When is an unaligned memory access illegal on ARM?
  • Index(es):
    • Date
    • Thread