32 bit allignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcarthybn
    New Member
    • Jun 2009
    • 7

    #1

    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • mcarthybn
      New Member
      • Jun 2009
      • 7

      #3
      yeah .. i have got and implemented .. . Cool one.

      Comment

      • mcarthybn
        New Member
        • Jun 2009
        • 7

        #4
        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.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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.

          Comment

          • alexis4
            New Member
            • Dec 2009
            • 113

            #6
            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...

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              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.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Originally posted by Banfa
                ...
                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.
                ...
                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).
                ...
                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)

                Comment

                Working...