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.

_nassert

_nassert() is one function I’ve been using to improve DSP compiler piplining C64X+ platform. It’s supposed to help tell the compiler how the memory accesses are aligned. For example, while looping through an array of ints, each element is separated by 4 bytes. In this case, the line would be _nassert((int)(x) % 8 == 0), where x is a pointer to an int array.

I’m wondering how I can use this with an array of a more complicated data structure, like with a struct containing 5 ints. 

Thanks!

  • Have you had a look at sections 7.5.10 and 7.5.11 of the C6000 Compiler Users Guide which discuss the methods to align different data types? If this does not answer your question please provide more detail, preferably with an example, of what you are looking for.

  • The examples in the Users Guide all use simple data types. I was wondering how something like a struct would be handled. 

    For example, if it is an array of my_struct which contain 2 ints, would it be sensible to write _nassert((int) my_struct % 16 == 0) where 16 comes from simply doubling 8?

  • Jingtao Xia said:

    _nassert((int)(x) % 8 == 0), where x is a pointer to an int array.

    Keep in mind is that x is a pointer.  This _nassert is dealing with the value of the pointer as an integer; thus, the expression you are making the assertion about is (int)(x).

    Jingtao Xia said:

    For example, if it is an array of my_struct which contain 2 ints, would it be sensible to write _nassert((int) my_struct % 16 == 0) where 16 comes from simply doubling 8?

    Your example is a bit unclear.  Here are some quick facts:

    • a struct may not be cast to an integer
    • a pointer may be cast to an integer
    • the name of an array, without an indexing expression, gets turned into a pointer
    • the first element in a struct or array is at offset 0
    • each element in a struct or array occupies space equal to sizeof its type
    • each element in a struct or array is aligned to the proper alignment of its type

    If my_struct is a plain struct, you must do this:

    _nassert((int)&my_struct % 16 == 0)

    If my_struct is actually an array of struct, you can do this:

    _nassert((int)my_struct % 16 == 0)

    This will align my_struct, but will have no effect on the layout of the elements of the struct.

    Is it truly just the struct you want aligned, or do you want each element in that struct to be aligned to 8 bytes?

  • Archaeologist said:
    This will align my_struct

    Correction: this will assert that my_struct is aligned...