This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C66x alignment problem

Other Parts Discussed in Thread: TEST2

Hello

I have a problem of alignment. This program can't get the result I expect. The instruction is used by LDW instead of LDNW. Could you give me an advice?
Best regards

- DSP: C66xx
- CCS: v5.1.1
- CGT: v7.4.4

#include <stdio.h>

#pragma pack(2)

struct t_test2 {
 short vs;
 struct t_test1 *pointer; // condition-1: This pointer doesn't align to 4-byte boundary.
} str2;

struct t_test1 {
 char c1;
 char c2;
} str1;

void main(void) {
 char *p1;
 char *p2;

 str2.pointer = &str1;

 p1 = &str2.pointer->c1;  // condition-2: This pointer indicated by "->" is the top of the structure.
 printf ("p1=0x%08x\n", p1); // p1=0xece60000: bad address by LDW

 p2 = &str2.pointer->c2;
 printf ("p2=0x%08x\n", p2); // p2=0x0000ece7: good address by LDNW

  • By declaring the pointer as "struct t_test1 *pointer", you have told the compiler that it is a pointer to an aligned instance of "struct t_test1."  If you want that pointer to point to something with a different alignment, you should use "void *pointer" or "char *pointer".

  • Hello pf

    Thank you for your reply. I changed as following program, but it could not resolve.

    struct t_test2 {
     short vs;
     void *pointer;
    } str2;

    p1 = &((struct t_test1 *)str2.pointer)->c1;
    p2 = &((struct t_test1 *)str2.pointer)->c2;

    I also found that this problem depends on CGT version.

      NG : v7.4.x
      OK : v7.3.x (e.g. v7.3.8)

    My customer told me that there are a lot of potentials by this problem in customer program.
    They are implementing for porting from existing CPU to C66x. They want to use v7.4.x.
    Please advise me.

    Regards

  • This appears to be a bug in the compiler.  In the case you indicate, it is using LDW to read the "pointer" structure member when LDNW should be used instead.  I filed SDSCM00047774 in the SDOWP system to have this addressed.  Please feel free to follow it with the SDOWP link below in my structure.

    KAZUHITO KONDO said:
      OK : v7.3.x (e.g. v7.3.8)

    That's because #pragma pack is not supported in version 7.3.x.  That feature is introduced in version 7.4.0.

    Thanks and regards,

    -George

  • Hello pf,

    Please see an sample code below.

    This example can be built by both of C6000 CGT 7.3.x and 7.4.x with GNU extension.
    This example runs correctly when it is built by 7.3.8-7.3.12,
    but does not run correctly when it is built by 7.3.7 or earlier, or 7.4.x.
    I guess this issue has been fixed on 7.3.8.


    #include <stdio.h>

    struct __attribute__((__packed__)) t_test2 {
        short vs;    // Padding
        struct t_test1 *pointer;
    } str2;

    struct __attribute__((__packed__)) t_test1 {
        char c1;
        char c2;
    } str1;

    void main(void) {
        char *p1;
        char *p2;

        str2.pointer = &str1;

        p1 = &str2.pointer->c1;
        printf ("p1=0x%08x\n", p1);

        p2 = &str2.pointer->c2;
        printf ("p2=0x%08x\n", p2);
    }


    Best Regards,
    Nobuhiro Masaoka

  • I think I misunderstood you at the very beginning.

    The problem is not that you want the pointer in the field "pointer" to point at an unaligned instance of t_test1, it's that the field "pointer" itself is not aligned to a 32-bit boundary.  Correct?

    If so, could you please check what happens when t_test1 has more than two fields?  Is the problem only with the first field?  Only with the second and third (and subsequent)?

    Thanks.

  • Hello pf,

    Yes, the problem happens when CPU reads the field "pointer" that is not aligned to a 32-bit boundary.
    And this happens when the "pointer" pointed only first element of the instance "t_test1".
    This does not happen on second field and subsequents.

    Best Regards,
    Nobuhiro Masaoka

  • Hello pf,

    This problem looks like fixed at 7.3.8 in 7.3 series, as I mentioned above.
    Is it correct?

    But I couldn't find its description in the release note...

    I have to use 7.4 series CGT because I need "#pragma pack" function.


    Best Regards,
    Nobuhiro Masaoka


  • Hello  George

    Thank you for reporting of SDSCM00047774. I understand support version for #pragma pack.

    I tried the __attribute__ ((__packed__)) in CGT7.3.x. As Nobuhiro said above, v7.3.8 or later ran correctly, but v7.3.7 or earlier didn't.

    I expect that the problem of SDSCM00047774 can be resolved at an early stage. Would it be possible for you to tell me the release timing of fixed version?

    Regards

  • KAZUHITO KONDO said:

    I expect that the problem of SDSCM00047774 can be resolved at an early stage. Would it be possible for you to tell me the release timing of fixed version?

    I would suggest using the SDOWP link in our signature to track the status of the bug. Once the bug has been fixed in a particular release the bug report will be updated to reflect that information.

    This wiki page shows the list of pending compiler releases and scheduled release date, so once the bug has been fixed you can check the schedule for the fixed version in this page: http://processors.wiki.ti.com/index.php/Pending_Compiler_Releases