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.

16bit unsigned bit fields

I'm porting a vendor's source code to TI DSP 6410 platform.

I get "nonstandard type for a bit field" warning from compiler when following structure declaration is compiled:

typedef  unsigned short int        cs_uint16 ;

typedef  unsigned int        cs_uint32 ;

typedef  union {
  struct {
    cs_uint16 HSIFe                :  1 ; /* bits 0:0 */
    cs_uint16 XFIe                 :  1 ; /* bits 1:1 */
    cs_uint16 N40Ge                :  1 ; /* bits 2:2 */
    cs_uint16 OHPPe                :  1 ; /* bits 3:3 */
    cs_uint16 N10G0e               :  1 ; /* bits 4:4 */
    cs_uint16 N10G1e               :  1 ; /* bits 5:5 */
    cs_uint16 N10G2e               :  1 ; /* bits 6:6 */
    cs_uint16 N10G3e               :  1 ; /* bits 7:7 */
    cs_uint16 PP10G0e              :  1 ; /* bits 8:8 */
    cs_uint16 PP10G1e              :  1 ; /* bits 9:9 */
    cs_uint16 PP10G2e              :  1 ; /* bits 10:10 */
    cs_uint16 PP10G3e              :  1 ; /* bits 11:11 */
    cs_uint16 PP40Ge               :  1 ; /* bits 12:12 */
    cs_uint16 CFPe                 :  1 ; /* bits 13:13 */
    cs_uint16 CUPLLe               :  1 ; /* bits 14:14 */
    cs_uint16 rsrvd1               :  1 ;
  } bf ;

cs_uint16     wrd ;
} TEN_MPIF_MODULE_B_INTENABLE_t;

In place of cs_unit16, if I use cs_unit32, problem goes away.

Does TI compiler not support 16bit packed bit-fields?

I can't change this structure to 32 bit wide because this structure is used to R/W hardware memory which is 16 bit wide.

Please help.

thanks,

Nitin

  • According to the C89 standard, which our compiler adheres to, "unsigned short" is not a standard type for bit fields. Some compilers may support it as a non standard extension, however that will make the code not guaranteed to be portable between compilers. Some discussion about it is here: http://processors.wiki.ti.com/index.php/C6000_EABI_Migration#Bit-Field_Layout

    The -pr option(relaxed ANSI check) will help avoid the warning as it tells the compiler to not perform strict ANSI C checking.

  • Aarti never says it explicitly, but note that the TI compiler does support bit-fields with type unsigned short; it just warns that it is nonstandard (with respect to C89).

    Bit-fields themselves are not all that portable because the rules for layout in memory allow some variation.  Most compilers will support this kind of bit-field (and it is standard in C++). So portability is not likely to be further compromised by non-standard integral types in most situations. 

    Note that you can suppress just that specific warning with the -pds=232 option (long form: --diag_suppress=232) or by using the equivalent pragma in the source.

  • Hi Paul,

    So, If I defined bit-field structure

    typedef struct {

    char     fieldA   :   4;

    char     fieldB   :   4;

    char     fieldC   :   4;

    char     fieldD   :   4;

    }MyField;

    How will be order of fields in the C67xx memory (little endian) ?

    Such as below or not ?

    |         Memory  Bits          |  Offset

    | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |

    |     fieldA    |     fieldB    |  0 byte

    |     fieldC    |     fieldD    |  1 byte

    Thanks

  • Hi Nikolay ,
    Sorry no-one has replied to your question.
    Supposing you have a union defined with your structure the memory allocation would be as shown below.
    typedef union uTest_u
    {
    unsigned char b[2];
    struct {
    char fieldA : 4;
    char fieldB : 4;
    char fieldC : 4;
    char fieldD : 4;
    } bits;

    } utest_u

    uTest_u uTest;

    |         Memory  Bits          |  Offset | 
    | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
    | fieldB | fieldA | 0 byte | uTest.b[0]
    | fieldD | fieldC | 1 byte | uTest.b[1]
    
    
    Regards,
    Kirem