Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.electronics.design > #400092 > unrolled thread

Union of bitfields and larger type on ARM

Started bybitrex <bitrex@de.lete.earthlink.net>
First post2016-02-23 14:17 -0500
Last post2016-02-27 14:37 +0100
Articles 15 on this page of 35 — 13 participants

Back to article view | Back to sci.electronics.design


Contents

  Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 14:17 -0500
    Re: Union of bitfields and larger type on ARM Lasse Langwadt Christensen <langwadt@fonz.dk> - 2016-02-23 11:36 -0800
      Re: Union of bitfields and larger type on ARM John Devereux <john@devereux.me.uk> - 2016-02-23 19:40 +0000
        Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 14:42 -0500
      Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 14:46 -0500
        Re: Union of bitfields and larger type on ARM Lasse Langwadt Christensen <langwadt@fonz.dk> - 2016-02-23 12:23 -0800
          Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 16:50 -0500
            Re: Union of bitfields and larger type on ARM Lasse Langwadt Christensen <langwadt@fonz.dk> - 2016-02-23 14:11 -0800
              Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 17:16 -0500
                Re: Union of bitfields and larger type on ARM Lasse Langwadt Christensen <langwadt@fonz.dk> - 2016-02-23 14:29 -0800
        Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-24 08:51 +0100
          Re: Union of bitfields and larger type on ARM Les Cargill <lcargill99@comcast.com> - 2016-02-25 12:08 -0600
            Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-25 13:43 -0500
      Re: Union of bitfields and larger type on ARM Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-02-23 22:02 +0200
    Re: Union of bitfields and larger type on ARM John Devereux <john@devereux.me.uk> - 2016-02-23 19:36 +0000
      Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-23 22:34 +0100
        Re: Union of bitfields and larger type on ARM bitrex <bitrex@de.lete.earthlink.net> - 2016-02-23 16:45 -0500
          Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-23 23:21 +0100
        Re: Union of bitfields and larger type on ARM spope33@speedymail.org (Steve Pope) - 2016-02-25 00:06 +0000
          Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-25 01:29 +0100
    Re: Union of bitfields and larger type on ARM Tim Wescott <seemywebsite@myfooter.really> - 2016-02-23 15:53 -0600
      Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-23 23:48 +0100
        Re: Union of bitfields and larger type on ARM Christian Gollwitzer <auriocus@gmx.de> - 2016-02-24 09:44 +0100
          Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-24 10:39 +0100
      Re: Union of bitfields and larger type on ARM Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-02-24 09:54 +0200
    Re: Union of bitfields and larger type on ARM M Philbrook <jamie_ka1lpa@charter.net> - 2016-02-23 20:39 -0500
      Re: Union of bitfields and larger type on ARM Ian Collins <ian-news@hotmail.com> - 2016-02-24 14:48 +1300
    Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-24 10:41 +0100
    Re: Union of bitfields and larger type on ARM Les Cargill <lcargill99@comcast.com> - 2016-02-25 12:05 -0600
      Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-26 17:09 +0100
        Re: Union of bitfields and larger type on ARM scott@slp53.sl.home (Scott Lurndal) - 2016-02-26 17:29 +0000
          Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-26 18:42 +0100
        Re: Union of bitfields and larger type on ARM Les Cargill <lcargill99@comcast.com> - 2016-02-26 19:07 -0600
        Re: Union of bitfields and larger type on ARM Allan Herriman <allanherriman@hotmail.com> - 2016-02-27 08:58 +0000
          Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-27 14:37 +0100

Page 2 of 2 — ← Prev page 1 [2]


#400124

FromTim Wescott <seemywebsite@myfooter.really>
Date2016-02-23 15:53 -0600
Message-ID<g6qdnaMZvfFJSVHLnZ2dnUU7-fOdnZ2d@giganews.com>
In reply to#400092
On Tue, 23 Feb 2016 14:17:59 -0500, bitrex wrote:

> Does anyone know if the following "naughty" (implementation dependent)
> construct would work "correctly" on the ARM Cortex M4, compiled with
> arm-gcc-embedded?
> 
> typedef union {
>        uint32_t phase_accumulator_full;
>        struct {
>          uint8_t integer_high : 8,
>                  integer_low : 8,
>                  frac_high  : 8, frac_low  : 8;
> 
>        };
> } phase_accumulator;

First:

Yes, it'll work as long as you want the least significant byte to be 
named "integer_high" and the most significant byte to be named "frac_low".

Second:

You need to name the bitfield.  I'll assume you've named it "bits" -- 
i.e., phase_accumulator.bits.frac_high.

Third:

It should be just as fast to use:

uint32_t phase_accumulator;

static inline uint8_t integer_high(uint32_t x) {return (x >> 24) & 0xff;}
static inline uint8_t integer_low (uint32_t x) {return (x >> 16) & 0xff;}
static inline uint8_t frac_high   (uint32_t x) {return (x >>  8) & 0xff;}
static inline uint8_t frac_low    (uint32_t x) {return (x >>  0) & 0xff;}

This will be portable, and integer_high(phase_accumulator) should read as 
easily (or more so) as phase_accumulator.bits.integer_high.

-- 

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

[toc] | [prev] | [next] | [standalone]


#400129

FromDavid Brown <david.brown@hesbynett.no>
Date2016-02-23 23:48 +0100
Message-ID<naindd$71s$1@dont-email.me>
In reply to#400124
On 23/02/16 22:53, Tim Wescott wrote:
> On Tue, 23 Feb 2016 14:17:59 -0500, bitrex wrote:
>
>> Does anyone know if the following "naughty" (implementation dependent)
>> construct would work "correctly" on the ARM Cortex M4, compiled with
>> arm-gcc-embedded?
>>
>> typedef union {
>>         uint32_t phase_accumulator_full;
>>         struct {
>>           uint8_t integer_high : 8,
>>                   integer_low : 8,
>>                   frac_high  : 8, frac_low  : 8;
>>
>>         };
>> } phase_accumulator;
>
> First:
>
> Yes, it'll work as long as you want the least significant byte to be
> named "integer_high" and the most significant byte to be named "frac_low".
>

True.

> Second:
>
> You need to name the bitfield.  I'll assume you've named it "bits" --
> i.e., phase_accumulator.bits.frac_high.

Not necessary true, assuming you are using C11 (rather than C++), or gcc 
or clang, or any other compiler that supports anonymous structs as an 
extension.  Since this is specifically for the ARM, and not meant to be 
portable, it is perhaps not unreasonable to use an extension that is 
available the most common tools for that target.  But that's a decision 
for the OP to make.

>
> Third:
>
> It should be just as fast to use:
>
> uint32_t phase_accumulator;
>
> static inline uint8_t integer_high(uint32_t x) {return (x >> 24) & 0xff;}
> static inline uint8_t integer_low (uint32_t x) {return (x >> 16) & 0xff;}
> static inline uint8_t frac_high   (uint32_t x) {return (x >>  8) & 0xff;}
> static inline uint8_t frac_low    (uint32_t x) {return (x >>  0) & 0xff;}
>
> This will be portable, and integer_high(phase_accumulator) should read as
> easily (or more so) as phase_accumulator.bits.integer_high.
>

No, these functions are not a good substitute.  First, they do not do 
the same thing if "phase_accumulator" is used as a volatile - and such 
bitfield structures are usually used for hardware registers where 
volatile is relevant.  Using the bitfields, accesses to the separate 
fields will be done as 8-bit accesses - using the functions, they will 
be 32-bit accesses.

Second, your functions give read-only access, not read-write access - 
another set is needed for writing.  Those are messy.

static inline uint32_t set_integer_low (uint32_t x, uint8_t y) {
   return (x & 0xff00ffff) | ((uint32_t) y << 16);
}


Third, your functions will /not/ be as fast for volatile types, and the 
write versions may be significantly slower.

Fourth, your functions are ugly in use, especially for setting:

	phase_accumulator = set_integer_low(phase_accumulator, y)

(I'm continuing your slight mixup of treating "phase_accumulator" as the 
instance of the union, rather than the type.)

Fifth, you lose one of the major reasons for using bitfields rather than 
bitmasks.  With bitfields, the field is tied tightly to the type, and 
therefore the instance of the type.  Mistakes are compile-time errors, 
your IDE can quickly give you assistance at filling out the field names, 
and your debugger can parse the data.  With manual shift-and-mask 
systems you lose all of that.


The cost of using bitfields here is that the code is not directly 
portable to other targets unless they have the same assumptions about 
bitfield ordering and alignment (and in reality, most targets do match 
here).  IMHO, it's a small price to pay.

[toc] | [prev] | [next] | [standalone]


#400188

FromChristian Gollwitzer <auriocus@gmx.de>
Date2016-02-24 09:44 +0100
Message-ID<najqc2$a2r$1@dont-email.me>
In reply to#400129
Am 23.02.16 um 23:48 schrieb David Brown:
> On 23/02/16 22:53, Tim Wescott wrote:
>> static inline uint8_t integer_high(uint32_t x) {return (x >> 24) & 0xff;}
>> static inline uint8_t integer_low (uint32_t x) {return (x >> 16) & 0xff;}
>> static inline uint8_t frac_high   (uint32_t x) {return (x >>  8) & 0xff;}
>> static inline uint8_t frac_low    (uint32_t x) {return (x >>  0) & 0xff;}
>>
>> This will be portable, and integer_high(phase_accumulator) should read as
>> easily (or more so) as phase_accumulator.bits.integer_high.
>>
>
> No, these functions are not a good substitute.  First, they do not do
> the same thing if "phase_accumulator" is used as a volatile - and such
> bitfield structures are usually used for hardware registers where
> volatile is relevant.  Using the bitfields, accesses to the separate
> fields will be done as 8-bit accesses - using the functions, they will
> be 32-bit accesses.

Are you sure? Have you checked the assembly output for this code? For 
non-volatile variables at least, I expect that these inline functions 
are optimized to the same instructions. I don'T have an ARM compiler 
handy, but maybe you could check if it also works with "volatile unit32_t x"

>
> Second, your functions give read-only access, not read-write access -
> another set is needed for writing.  Those are messy.
>
> static inline uint32_t set_integer_low (uint32_t x, uint8_t y) {
>    return (x & 0xff00ffff) | ((uint32_t) y << 16);
> }
>
>
> Third, your functions will /not/ be as fast for volatile types, and the
> write versions may be significantly slower.
>
> Fourth, your functions are ugly in use, especially for setting:
>
>      phase_accumulator = set_integer_low(phase_accumulator, y)

If C++ is used, an inline function with a reference parameter is also 
easily optimized:

static inline void set_integer_low (uint32_t x&, uint8_t y) {
    x= (x & 0xff00ffff) | ((uint32_t) y << 16);
}

set_integer_low(phase_accumulator, y);

I'm not so sure about a pointer version for pure C

static inline void set_integer_low (uint32_t *x, uint8_t y) {
x= (x & 0xff00ffff) | ((uint32_t) y << 16);
}

set_integer_low(&phase_accumulator, y);

I think the OP should check the assembly output of an optimized build to 
decide.

> Fifth, you lose one of the major reasons for using bitfields rather than
> bitmasks.  With bitfields, the field is tied tightly to the type, and
> therefore the instance of the type.  Mistakes are compile-time errors,
> your IDE can quickly give you assistance at filling out the field names,
> and your debugger can parse the data.  With manual shift-and-mask
> systems you lose all of that.

That is a valid reason. But I think that even in pure C, you could get a 
warning if you make a typedef unit32_t mybitask; or by embedding it into 
a struct with just one member

typedef struct { unit32_t bits; } mybitmask;

	Christian

[toc] | [prev] | [next] | [standalone]


#400191

FromDavid Brown <david.brown@hesbynett.no>
Date2016-02-24 10:39 +0100
Message-ID<najti9$kub$1@dont-email.me>
In reply to#400188
On 24/02/16 09:44, Christian Gollwitzer wrote:
> Am 23.02.16 um 23:48 schrieb David Brown:
>> On 23/02/16 22:53, Tim Wescott wrote:
>>> static inline uint8_t integer_high(uint32_t x) {return (x >> 24) &
>>> 0xff;}
>>> static inline uint8_t integer_low (uint32_t x) {return (x >> 16) &
>>> 0xff;}
>>> static inline uint8_t frac_high   (uint32_t x) {return (x >>  8) &
>>> 0xff;}
>>> static inline uint8_t frac_low    (uint32_t x) {return (x >>  0) &
>>> 0xff;}
>>>
>>> This will be portable, and integer_high(phase_accumulator) should
>>> read as
>>> easily (or more so) as phase_accumulator.bits.integer_high.
>>>
>>
>> No, these functions are not a good substitute.  First, they do not do
>> the same thing if "phase_accumulator" is used as a volatile - and such
>> bitfield structures are usually used for hardware registers where
>> volatile is relevant.  Using the bitfields, accesses to the separate
>> fields will be done as 8-bit accesses - using the functions, they will
>> be 32-bit accesses.
> 
> Are you sure? Have you checked the assembly output for this code? For
> non-volatile variables at least, I expect that these inline functions
> are optimized to the same instructions. I don'T have an ARM compiler
> handy, but maybe you could check if it also works with "volatile
> unit32_t x"
> 

Yes, I have checked - though not for a wide variety of versions,
optimisation flags, etc.  The inline functions will result in the same
ARM code in the non-volatile case (assuming at least -O1 optimisation).
 But in the volatile case, the compiler correctly generates different
code for the structure access and the manual bitfield extraction.

>>
>> Second, your functions give read-only access, not read-write access -
>> another set is needed for writing.  Those are messy.
>>
>> static inline uint32_t set_integer_low (uint32_t x, uint8_t y) {
>>    return (x & 0xff00ffff) | ((uint32_t) y << 16);
>> }
>>
>>
>> Third, your functions will /not/ be as fast for volatile types, and the
>> write versions may be significantly slower.
>>
>> Fourth, your functions are ugly in use, especially for setting:
>>
>>      phase_accumulator = set_integer_low(phase_accumulator, y)
> 
> If C++ is used, an inline function with a reference parameter is also
> easily optimized:
> 
> static inline void set_integer_low (uint32_t x&, uint8_t y) {
>    x= (x & 0xff00ffff) | ((uint32_t) y << 16);
> }
> 
> set_integer_low(phase_accumulator, y);

It is still a mess compared to the bitfield struct access, and it is
still different in the volatile case.  Yes, the compiler can optimise
the ugly, unmaintainable and error-prone source code into good object
code - but why not just write clear, simple source code that gets
optimised into good object code?

> 
> I'm not so sure about a pointer version for pure C
> 
> static inline void set_integer_low (uint32_t *x, uint8_t y) {
> x= (x & 0xff00ffff) | ((uint32_t) y << 16);
> }
> 
> set_integer_low(&phase_accumulator, y);

I would expect the compiler to manage this optimisation.

> 
> I think the OP should check the assembly output of an optimized build to
> decide.

I think the OP should look first and foremost at the /source/ code, and
decide from that.  The code generation details are up to the compiler -
enable optimisation, give it clear and correct source code, and let it
do its job.  It is only in very rare situations that the generated code
should be the guide for a decision like this.

> 
>> Fifth, you lose one of the major reasons for using bitfields rather than
>> bitmasks.  With bitfields, the field is tied tightly to the type, and
>> therefore the instance of the type.  Mistakes are compile-time errors,
>> your IDE can quickly give you assistance at filling out the field names,
>> and your debugger can parse the data.  With manual shift-and-mask
>> systems you lose all of that.
> 
> That is a valid reason. But I think that even in pure C, you could get a
> warning if you make a typedef unit32_t mybitask; or by embedding it into
> a struct with just one member
> 
> typedef struct { unit32_t bits; } mybitmask;
> 

In both C and C++, you have to embed the item in a struct - "typedef"
itself does not introduce a new type.  Yes, this technique can make the
code safer.

But you are going round in circles, writing lots of extra source code,
writing access functions with potentially error-prone masks (imagine a
case where the bitfields were not as simple, or where they were changed
later), with duplicate information in multiple places, and finally
resulting in some unnecessarily verbose and ugly usage.

In the end, all you have done is duplicate a feature that C and C++
support as part of the language.  It is a pointless exercise.


[toc] | [prev] | [next] | [standalone]


#400184

FromTauno Voipio <tauno.voipio@notused.fi.invalid>
Date2016-02-24 09:54 +0200
Message-ID<najneu$ule$1@dont-email.me>
In reply to#400124
On 23.2.16 23:53, Tim Wescott wrote:
> On Tue, 23 Feb 2016 14:17:59 -0500, bitrex wrote:
>
>> Does anyone know if the following "naughty" (implementation dependent)
>> construct would work "correctly" on the ARM Cortex M4, compiled with
>> arm-gcc-embedded?
>>
>> typedef union {
>>         uint32_t phase_accumulator_full;
>>         struct {
>>           uint8_t integer_high : 8,
>>                   integer_low : 8,
>>                   frac_high  : 8, frac_low  : 8;
>>
>>         };
>> } phase_accumulator;
>
> First:
>
> Yes, it'll work as long as you want the least significant byte to be
> named "integer_high" and the most significant byte to be named "frac_low".
>
> Second:
>
> You need to name the bitfield.  I'll assume you've named it "bits" --
> i.e., phase_accumulator.bits.frac_high.
>
> Third:
>
> It should be just as fast to use:
>
> uint32_t phase_accumulator;
>
> static inline uint8_t integer_high(uint32_t x) {return (x >> 24) & 0xff;}
> static inline uint8_t integer_low (uint32_t x) {return (x >> 16) & 0xff;}
> static inline uint8_t frac_high   (uint32_t x) {return (x >>  8) & 0xff;}
> static inline uint8_t frac_low    (uint32_t x) {return (x >>  0) & 0xff;}
>
> This will be portable, and integer_high(phase_accumulator) should read as
> easily (or more so) as phase_accumulator.bits.integer_high.


A vote on this: At least GCC optimizes these to single bit field
extract instructions on a Cortex.

Alternative format of the same:

static inline uint8_t integer_high(uint32_t x)
{
return (uint8_t)(x >> 24);
}

This should work correctly even without the cast.

-- 

-TV

[toc] | [prev] | [next] | [standalone]


#400150

FromM Philbrook <jamie_ka1lpa@charter.net>
Date2016-02-23 20:39 -0500
Message-ID<MPG.3136d3f791b98659989e8b@news.eternal-september.org>
In reply to#400092
In article <If2zy.23937$Nf2.15642@fx14.iad>, 
bitrex@de.lete.earthlink.net says...
> 
> Does anyone know if the following "naughty" (implementation dependent) 
> construct would work "correctly" on the ARM Cortex M4, compiled with 
> arm-gcc-embedded?
> 
> typedef union {
>        uint32_t phase_accumulator_full;
>        struct {
>          uint8_t integer_high : 8,
>                  integer_low : 8,
>                  frac_high  : 8,
>                  frac_low  : 8;
> 
>        };
> } phase_accumulator;

sumg ting does not look correct? Assuming I understand what you're
after.

struct _phase_acc {
 
	union { uint32_t phase_accumulator_full };
	union { uint_8 integer_high:8,
			 integer_low:8,
			 frac_high:8,
			 frc_low : 8;
 		};
    } phase_accumulator;

You can include a condition to test for the second union to
determine the endious of the platform to correctly compile
the code for the bit field order.
 
 The above struct should overlay to generate a single 32 bit image.

Jamie

[toc] | [prev] | [next] | [standalone]


#400155

FromIan Collins <ian-news@hotmail.com>
Date2016-02-24 14:48 +1300
Message-ID<dj4gg1Fj7j3U7@mid.individual.net>
In reply to#400150
M Philbrook wrote:
> In article <If2zy.23937$Nf2.15642@fx14.iad>,
> bitrex@de.lete.earthlink.net says...
>>
>> Does anyone know if the following "naughty" (implementation dependent)
>> construct would work "correctly" on the ARM Cortex M4, compiled with
>> arm-gcc-embedded?
>>
>> typedef union {
>>         uint32_t phase_accumulator_full;
>>         struct {
>>           uint8_t integer_high : 8,
>>                   integer_low : 8,
>>                   frac_high  : 8,
>>                   frac_low  : 8;
>>
>>         };
>> } phase_accumulator;
>
> sumg ting does not look correct? Assuming I understand what you're
> after.
>
> struct _phase_acc {
>
> 	union { uint32_t phase_accumulator_full };

typo..

> 	union { uint_8 integer_high:8,

typo..
> 			 integer_low:8,
> 			 frac_high:8,
> 			 frc_low : 8;
>   		};
>      } phase_accumulator;
>
> You can include a condition to test for the second union to
> determine the endious of the platform to correctly compile
> the code for the bit field order.
>
>   The above struct should overlay to generate a single 32 bit image.

No, it won't.  You have two 32 bit unions members of a struct, witch 
gives a 64 bit struct.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#400192

FromDavid Brown <david.brown@hesbynett.no>
Date2016-02-24 10:41 +0100
Message-ID<najtml$kub$2@dont-email.me>
In reply to#400092
On 23/02/16 20:17, bitrex wrote:
> Does anyone know if the following "naughty" (implementation dependent)
> construct would work "correctly" on the ARM Cortex M4, compiled with
> arm-gcc-embedded?
> 
> typedef union {
>       uint32_t phase_accumulator_full;
>       struct {
>         uint8_t integer_high : 8,
>                 integer_low : 8,
>                 frac_high  : 8,
>                 frac_low  : 8;
> 
>       };
> } phase_accumulator;

I should have mentioned this before, but in the case where your bitfield
sizes match the type, you don't need the bitfield specifiers:

typedef union {
	uint32_t phase_accumulator_full;
	struct {
		uint8_t integer_high;
		uint8_t integer_low;
		uint8_t frac_high;
		uint8_t frac_low;
	};
} phase_accumulator;

(Of course, the byte ordering is still wrong - I just wrote it that way
to keep it consistent.)

[toc] | [prev] | [next] | [standalone]


#400349

FromLes Cargill <lcargill99@comcast.com>
Date2016-02-25 12:05 -0600
Message-ID<nanf3n$t2j$1@dont-email.me>
In reply to#400092
bitrex wrote:
> Does anyone know if the following "naughty" (implementation dependent)
> construct would work "correctly" on the ARM Cortex M4, compiled with
> arm-gcc-embedded?
>
> typedef union {
>        uint32_t phase_accumulator_full;
>        struct {
>          uint8_t integer_high : 8,
>                  integer_low : 8,
>                  frac_high  : 8,
>                  frac_low  : 8;
>
>        };
> } phase_accumulator;


If it doesn't, fix it. Endianness is more or less undefined behavior in 
'C' so you have to test it.

Walk a 1 through phase_accumulator_full and print out ( or just store 
the result and dump it with a JTAG if you are without a console ).

-- 
Les Cargill

[toc] | [prev] | [next] | [standalone]


#400489

FromDavid Brown <david.brown@hesbynett.no>
Date2016-02-26 17:09 +0100
Message-ID<napt6d$1us$1@dont-email.me>
In reply to#400349
On 25/02/16 19:05, Les Cargill wrote:
> bitrex wrote:
>> Does anyone know if the following "naughty" (implementation dependent)
>> construct would work "correctly" on the ARM Cortex M4, compiled with
>> arm-gcc-embedded?
>>
>> typedef union {
>>        uint32_t phase_accumulator_full;
>>        struct {
>>          uint8_t integer_high : 8,
>>                  integer_low : 8,
>>                  frac_high  : 8,
>>                  frac_low  : 8;
>>
>>        };
>> } phase_accumulator;
> 
> 
> If it doesn't, fix it. Endianness is more or less undefined behavior in
> 'C' so you have to test it.

No, endianness is not remotely "undefined behaviour" - it is
/implementation/ defined behaviour.  There is a huge difference.

This means that the implementation has to document exactly what
endianness is used - and in the case of ARM compilers for embedded
systems, this is specified in the ARM EABI to be little endian.

Being implementation-defined behaviour, this also means that you /can/
test it - sometimes that is easier than trying to find the relevant
documentation!  Once you have tested it, you know exactly how it works
for that implementation.  And while in theory it could change in later
versions of the compiler, making such a change between compiler versions
would be astoundingly stupid and unhelpful - especially in an embedded
compiler - so it is not going to happen.  (I know of only one case where
this happened - and that was on MS's x86 compiler, a long time ago,
where they changed the bitfield endianness.)

If endianness (or bitfield endianness) really were undefined behaviour,
you could not rely on it being the same between two runs of the same
compiler on the same source code - testing would be useless.

> 
> Walk a 1 through phase_accumulator_full and print out ( or just store
> the result and dump it with a JTAG if you are without a console ).
> 

[toc] | [prev] | [next] | [standalone]


#400509

Fromscott@slp53.sl.home (Scott Lurndal)
Date2016-02-26 17:29 +0000
Message-ID<SX%zy.610$LP.435@fx16.iad>
In reply to#400489
David Brown <david.brown@hesbynett.no> writes:
>On 25/02/16 19:05, Les Cargill wrote:
>> bitrex wrote:
>>> Does anyone know if the following "naughty" (implementation dependent)
>>> construct would work "correctly" on the ARM Cortex M4, compiled with
>>> arm-gcc-embedded?
>>>
>>> typedef union {
>>>        uint32_t phase_accumulator_full;
>>>        struct {
>>>          uint8_t integer_high : 8,
>>>                  integer_low : 8,
>>>                  frac_high  : 8,
>>>                  frac_low  : 8;
>>>
>>>        };
>>> } phase_accumulator;
>> 
>> 
>> If it doesn't, fix it. Endianness is more or less undefined behavior in
>> 'C' so you have to test it.
>
>No, endianness is not remotely "undefined behaviour" - it is
>/implementation/ defined behaviour.  There is a huge difference.

We have software that is built for both big-endian and little-endian
hosts (and more interestingly, it must internally simulate both big-endian
and little-endian processors).

We have this in one of the project header files.

#include <endian.h>
#ifndef __BYTE_ORDER
    #if defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
        #define __BYTE_ORDER __BIG_ENDIAN
    #elif !defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN)
        #define __BYTE_ORDER __LITTLE_ENDIAN
        #define __BIG_ENDIAN 4321
    #elif !defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
        #define __BIG_ENDIAN 4321
        #define __BYTE_ORDER __BIG_ENDIAN
    #else
        #error Unable to determine Endian mode
    #endif
#endif

#if !defined(htobe16)
# if __BYTE_ORDER == __LITTLE_ENDIAN
#  define htobe16(x) __bswap_16 (x)
#  define htole16(x) (x)
#  define be16toh(x) __bswap_16 (x)
#  define le16toh(x) (x)

#  define htobe32(x) __bswap_32 (x)
#  define htole32(x) (x)
#  define be32toh(x) __bswap_32 (x)
#  define le32toh(x) (x)

#  define htobe64(x) __bswap_64 (x)
#  define htole64(x) (x)
#  define be64toh(x) __bswap_64 (x)
#  define le64toh(x) (x)
# else
#  define htobe16(x) (x)
#  define htole16(x) __bswap_16 (x)
#  define be16toh(x) (x)
#  define le16toh(x) __bswap_16 (x)

#  define htobe32(x) (x)
#  define htole32(x) __bswap_32 (x)
#  define be32toh(x) (x)
#  define le32toh(x) __bswap_32 (x)

#  define htobe64(x) (x)
#  define htole64(x) __bswap_64 (x)
#  define be64toh(x) (x)
#  define le64toh(x) __bswap_64 (x)
# endif
#endif

Then use hto{bl}eXX and {bl}e64tohXX in
the code without the need for local ifdefs.

[toc] | [prev] | [next] | [standalone]


#400510

FromDavid Brown <david.brown@hesbynett.no>
Date2016-02-26 18:42 +0100
Message-ID<naq2l0$nbf$1@dont-email.me>
In reply to#400509
On 26/02/16 18:29, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 25/02/16 19:05, Les Cargill wrote:
>>> bitrex wrote:
>>>> Does anyone know if the following "naughty" (implementation dependent)
>>>> construct would work "correctly" on the ARM Cortex M4, compiled with
>>>> arm-gcc-embedded?
>>>>
>>>> typedef union {
>>>>         uint32_t phase_accumulator_full;
>>>>         struct {
>>>>           uint8_t integer_high : 8,
>>>>                   integer_low : 8,
>>>>                   frac_high  : 8,
>>>>                   frac_low  : 8;
>>>>
>>>>         };
>>>> } phase_accumulator;
>>>
>>>
>>> If it doesn't, fix it. Endianness is more or less undefined behavior in
>>> 'C' so you have to test it.
>>
>> No, endianness is not remotely "undefined behaviour" - it is
>> /implementation/ defined behaviour.  There is a huge difference.
>
> We have software that is built for both big-endian and little-endian
> hosts (and more interestingly, it must internally simulate both big-endian
> and little-endian processors).
>
> We have this in one of the project header files.

<snip details to save electrons>

> Then use hto{bl}eXX and {bl}e64tohXX in
> the code without the need for local ifdefs.
>

Yes, that looks like a reasonable solution if you need to handle 
different endiannesses.  Another possibility will come with gcc 6, which 
supports attributes for structs with specific endiannesses (and I know 
at least one compiler that has had that feature for perhaps a couple of 
decades).

But if your code does not have to be portable - such as if you are 
modelling a hardware register on a particular device - you can just use 
the one fixed layout.

[toc] | [prev] | [next] | [standalone]


#400548

FromLes Cargill <lcargill99@comcast.com>
Date2016-02-26 19:07 -0600
Message-ID<naqs8m$nao$2@dont-email.me>
In reply to#400489
David Brown wrote:
> On 25/02/16 19:05, Les Cargill wrote:
>> bitrex wrote:
>>> Does anyone know if the following "naughty" (implementation dependent)
>>> construct would work "correctly" on the ARM Cortex M4, compiled with
>>> arm-gcc-embedded?
>>>
>>> typedef union {
>>>         uint32_t phase_accumulator_full;
>>>         struct {
>>>           uint8_t integer_high : 8,
>>>                   integer_low : 8,
>>>                   frac_high  : 8,
>>>                   frac_low  : 8;
>>>
>>>         };
>>> } phase_accumulator;
>>
>>
>> If it doesn't, fix it. Endianness is more or less undefined behavior in
>> 'C' so you have to test it.
>
> No, endianness is not remotely "undefined behaviour" - it is
> /implementation/ defined behaviour.  There is a huge difference.
>

Ah, there you go. Right! I'd forgotten the term of art.

<snip>

-- 
Les Cargill

[toc] | [prev] | [next] | [standalone]


#400582

FromAllan Herriman <allanherriman@hotmail.com>
Date2016-02-27 08:58 +0000
Message-ID<56d1654a$0$2784$c3e8da3$76491128@news.astraweb.com>
In reply to#400489
On Fri, 26 Feb 2016 17:09:37 +0100, David Brown wrote:

> On 25/02/16 19:05, Les Cargill wrote:
>> bitrex wrote:
>>> Does anyone know if the following "naughty" (implementation dependent)
>>> construct would work "correctly" on the ARM Cortex M4, compiled with
>>> arm-gcc-embedded?
>>>
>>> typedef union {
>>>        uint32_t phase_accumulator_full;
>>>        struct {
>>>          uint8_t integer_high : 8,
>>>                  integer_low : 8,
>>>                  frac_high  : 8, frac_low  : 8;
>>>
>>>        };
>>> } phase_accumulator;
>> 
>> 
>> If it doesn't, fix it. Endianness is more or less undefined behavior in
>> 'C' so you have to test it.
> 
> No, endianness is not remotely "undefined behaviour" - it is
> /implementation/ defined behaviour.  There is a huge difference.
> 
> This means that the implementation has to document exactly what
> endianness is used - and in the case of ARM compilers for embedded
> systems, this is specified in the ARM EABI to be little endian.
> 
> Being implementation-defined behaviour, this also means that you /can/
> test it - sometimes that is easier than trying to find the relevant
> documentation!  Once you have tested it, you know exactly how it works
> for that implementation.  And while in theory it could change in later
> versions of the compiler, making such a change between compiler versions
> would be astoundingly stupid and unhelpful - especially in an embedded
> compiler - so it is not going to happen.  (I know of only one case where
> this happened - and that was on MS's x86 compiler, a long time ago,
> where they changed the bitfield endianness.)

It happened to me on an 8051 C compiler in the '90s.  The endianness of 
bitfields changed between one compiler version and the next.
I learned a lesson, and have not used bitfields in C since then.

I believe the compiler vendor is still in business today (well, they were 
bought by a microcontroller manufacturer).

Regards,
Allan

[toc] | [prev] | [next] | [standalone]


#400597

FromDavid Brown <david.brown@hesbynett.no>
Date2016-02-27 14:37 +0100
Message-ID<nas8lm$k21$1@dont-email.me>
In reply to#400582
On 27/02/16 09:58, Allan Herriman wrote:
> On Fri, 26 Feb 2016 17:09:37 +0100, David Brown wrote:
>
>> On 25/02/16 19:05, Les Cargill wrote:
>>> bitrex wrote:
>>>> Does anyone know if the following "naughty" (implementation dependent)
>>>> construct would work "correctly" on the ARM Cortex M4, compiled with
>>>> arm-gcc-embedded?
>>>>
>>>> typedef union {
>>>>         uint32_t phase_accumulator_full;
>>>>         struct {
>>>>           uint8_t integer_high : 8,
>>>>                   integer_low : 8,
>>>>                   frac_high  : 8, frac_low  : 8;
>>>>
>>>>         };
>>>> } phase_accumulator;
>>>
>>>
>>> If it doesn't, fix it. Endianness is more or less undefined behavior in
>>> 'C' so you have to test it.
>>
>> No, endianness is not remotely "undefined behaviour" - it is
>> /implementation/ defined behaviour.  There is a huge difference.
>>
>> This means that the implementation has to document exactly what
>> endianness is used - and in the case of ARM compilers for embedded
>> systems, this is specified in the ARM EABI to be little endian.
>>
>> Being implementation-defined behaviour, this also means that you /can/
>> test it - sometimes that is easier than trying to find the relevant
>> documentation!  Once you have tested it, you know exactly how it works
>> for that implementation.  And while in theory it could change in later
>> versions of the compiler, making such a change between compiler versions
>> would be astoundingly stupid and unhelpful - especially in an embedded
>> compiler - so it is not going to happen.  (I know of only one case where
>> this happened - and that was on MS's x86 compiler, a long time ago,
>> where they changed the bitfield endianness.)
>
> It happened to me on an 8051 C compiler in the '90s.  The endianness of
> bitfields changed between one compiler version and the next.

Thanks - now I know of /two/ cases where compilers have changed bit 
ordering!

> I learned a lesson, and have not used bitfields in C since then.

You are throwing out the baby with the bathwater.  If you avoid using a 
feature just because a small-time compiler writer for a brain-dead and 
C-unfriendly processor made an inconvenient decision two decades ago, 
you would have to give up programming altogether.  Bitfields have 
portability issues, but can be extremely useful - they are perfectly 
safe to use once you understand them.

>
> I believe the compiler vendor is still in business today (well, they were
> bought by a microcontroller manufacturer).
>
> Regards,
> Allan
>

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | sci.electronics.design


csiph-web