Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #41176 > unrolled thread
| Started by | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| First post | 2016-02-23 14:17 -0500 |
| Last post | 2016-02-27 20:15 -0500 |
| Articles | 20 on this page of 31 — 15 participants |
Back to article view | Back to comp.lang.c++
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 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
Re: Union of bitfields and larger type on ARM Öö Tiib <ootiib@hot.ee> - 2016-02-27 07:21 -0800
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-27 16:34 +0100
Re: Union of bitfields and larger type on ARM Öö Tiib <ootiib@hot.ee> - 2016-02-27 10:14 -0800
Re: Union of bitfields and larger type on ARM David Brown <david.brown@hesbynett.no> - 2016-02-28 15:26 +0100
Re: Union of bitfields and larger type on ARM Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-27 16:40 -0500
Re: Union of bitfields and larger type on ARM Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-28 00:47 +0200
Re: Union of bitfields and larger type on ARM Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-27 18:21 -0500
Re: Union of bitfields and larger type on ARM Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-28 01:33 +0200
Re: Union of bitfields and larger type on ARM Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-27 20:15 -0500
Page 1 of 2 [1] 2 Next page →
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2016-02-23 14:17 -0500 |
| Subject | Union of bitfields and larger type on ARM |
| Message-ID | <If2zy.23937$Nf2.15642@fx14.iad> |
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;
[toc] | [next] | [standalone]
| From | John Devereux <john@devereux.me.uk> |
|---|---|
| Date | 2016-02-23 19:36 +0000 |
| Message-ID | <87fuwj6wyi.fsf@devereux.me.uk> |
| In reply to | #41176 |
bitrex <bitrex@de.lete.earthlink.net> writes:
> 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 am not an expert (ask on comp.arch.embedded) but my understanding is
that this sort of thing is guaranteed by the EABI (bugs aside).
--
John Devereux
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-23 22:34 +0100 |
| Message-ID | <naij2s$lsf$1@dont-email.me> |
| In reply to | #41178 |
On 23/02/16 20:36, John Devereux wrote:
> bitrex <bitrex@de.lete.earthlink.net> writes:
>
>> 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 am not an expert (ask on comp.arch.embedded) but my understanding is
> that this sort of thing is guaranteed by the EABI (bugs aside).
>
I can't even figure out why it would be considered "naughty" at all.
The only implementation-dependent aspects I can see are the alignment of
the parts (on the ARM, 8-bit, 16-bit and 32-bit types all have "natural"
alignments), and the ordering of the bitfields (which is low bits first
- guaranteed by the ARM EABI, and thus consistent across compilers).
[toc] | [prev] | [next] | [standalone]
| From | bitrex <bitrex@de.lete.earthlink.net> |
|---|---|
| Date | 2016-02-23 16:45 -0500 |
| Message-ID | <aq4zy.56$uT1.16@fx27.iad> |
| In reply to | #41191 |
On 02/23/2016 04:34 PM, David Brown wrote:
> On 23/02/16 20:36, John Devereux wrote:
>> bitrex <bitrex@de.lete.earthlink.net> writes:
>>
>>> 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 am not an expert (ask on comp.arch.embedded) but my understanding is
>> that this sort of thing is guaranteed by the EABI (bugs aside).
>>
>
> I can't even figure out why it would be considered "naughty" at all. The
> only implementation-dependent aspects I can see are the alignment of the
> parts (on the ARM, 8-bit, 16-bit and 32-bit types all have "natural"
> alignments), and the ordering of the bitfields (which is low bits first
> - guaranteed by the ARM EABI, and thus consistent across compilers).
>
>
"Naughty" if one wants to port the code as-is to another architecture, I
guess.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-23 23:21 +0100 |
| Message-ID | <nailqs$sd$1@dont-email.me> |
| In reply to | #41193 |
On 23/02/16 22:45, bitrex wrote:
> On 02/23/2016 04:34 PM, David Brown wrote:
>> On 23/02/16 20:36, John Devereux wrote:
>>> bitrex <bitrex@de.lete.earthlink.net> writes:
>>>
>>>> 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 am not an expert (ask on comp.arch.embedded) but my understanding is
>>> that this sort of thing is guaranteed by the EABI (bugs aside).
>>>
>>
>> I can't even figure out why it would be considered "naughty" at all. The
>> only implementation-dependent aspects I can see are the alignment of the
>> parts (on the ARM, 8-bit, 16-bit and 32-bit types all have "natural"
>> alignments), and the ordering of the bitfields (which is low bits first
>> - guaranteed by the ARM EABI, and thus consistent across compilers).
>>
>>
>
> "Naughty" if one wants to port the code as-is to another architecture, I
> guess.
I would say "non-portable" rather than "naughty". "Naughty" implies
that this might not work in some circumstances, or perhaps depends on
undefined behaviour. This is merely non-portable and relies on
implementation-dependent behaviour (which happens to be defined by the
ARM EABI, and thus should apply to all ARM compilers).
[toc] | [prev] | [next] | [standalone]
| From | spope33@speedymail.org (Steve Pope) |
|---|---|
| Date | 2016-02-25 00:06 +0000 |
| Message-ID | <nalgif$8oi$1@blue-new.rahul.net> |
| In reply to | #41191 |
David Brown <david.brown@hesbynett.no> wrote: >On 23/02/16 20:36, John Devereux wrote: >> bitrex <bitrex@de.lete.earthlink.net> writes: >> I am not an expert (ask on comp.arch.embedded) but my understanding is >> that this sort of thing is guaranteed by the EABI (bugs aside). >I can't even figure out why it would be considered "naughty" at all. >The only implementation-dependent aspects I can see are the alignment of >the parts (on the ARM, 8-bit, 16-bit and 32-bit types all have "natural" >alignments), and the ordering of the bitfields (which is low bits first >- guaranteed by the ARM EABI, and thus consistent across compilers). Under K&R C the ordering of fields was not guaranteed, but I think this was cleaned up by C99 or possibly somewhat later. OP should see which language standard their compiler uses and refer to that standard and comment appropriately. (In addition to testing the code...) Steve
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-25 01:29 +0100 |
| Message-ID | <nalhnc$t1g$1@dont-email.me> |
| In reply to | #41235 |
On 25/02/16 01:06, Steve Pope wrote: > David Brown <david.brown@hesbynett.no> wrote: > >> On 23/02/16 20:36, John Devereux wrote: > >>> bitrex <bitrex@de.lete.earthlink.net> writes: > >>> I am not an expert (ask on comp.arch.embedded) but my understanding is >>> that this sort of thing is guaranteed by the EABI (bugs aside). > >> I can't even figure out why it would be considered "naughty" at all. >> The only implementation-dependent aspects I can see are the alignment of >> the parts (on the ARM, 8-bit, 16-bit and 32-bit types all have "natural" >> alignments), and the ordering of the bitfields (which is low bits first >> - guaranteed by the ARM EABI, and thus consistent across compilers). > > Under K&R C the ordering of fields was not guaranteed, but I > think this was cleaned up by C99 or possibly somewhat later. I haven't looked at K&R for an older definition, but C11 certainly says that the order of allocation of bit-fields is implementation-dependent. There are some rules about packing, but there is plenty of scope for weird implementations if the compiler writers so fancy. > > OP should see which language standard their compiler uses and > refer to that standard and comment appropriately. (In addition to > testing the code...) > As long as his compiler follows the ARM EABI (and all serious embedded ARM compilers will do so), there should be no problem - the EABI gives more details for this sort of thing than any of the C or C++ standards. (He could make it a little easier by dropping the bitfields - they are not necessary when the struct fields are uint8_t and the bitfields are 8 bits.)
[toc] | [prev] | [next] | [standalone]
| From | Tim Wescott <seemywebsite@myfooter.really> |
|---|---|
| Date | 2016-02-23 15:53 -0600 |
| Message-ID | <g6qdnaMZvfFJSVHLnZ2dnUU7-fOdnZ2d@giganews.com> |
| In reply to | #41176 |
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]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-23 23:48 +0100 |
| Message-ID | <naindd$71s$1@dont-email.me> |
| In reply to | #41194 |
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]
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Date | 2016-02-24 09:44 +0100 |
| Message-ID | <najqc2$a2r$1@dont-email.me> |
| In reply to | #41200 |
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]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-24 10:39 +0100 |
| Message-ID | <najti9$kub$1@dont-email.me> |
| In reply to | #41215 |
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]
| From | Tauno Voipio <tauno.voipio@notused.fi.invalid> |
|---|---|
| Date | 2016-02-24 09:54 +0200 |
| Message-ID | <najneu$ule$1@dont-email.me> |
| In reply to | #41194 |
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]
| From | M Philbrook <jamie_ka1lpa@charter.net> |
|---|---|
| Date | 2016-02-23 20:39 -0500 |
| Message-ID | <MPG.3136d3f791b98659989e8b@news.eternal-september.org> |
| In reply to | #41176 |
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]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2016-02-24 14:48 +1300 |
| Message-ID | <dj4gg1Fj7j3U7@mid.individual.net> |
| In reply to | #41210 |
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]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-24 10:41 +0100 |
| Message-ID | <najtml$kub$2@dont-email.me> |
| In reply to | #41176 |
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]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2016-02-25 12:05 -0600 |
| Message-ID | <nanf3n$t2j$1@dont-email.me> |
| In reply to | #41176 |
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]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-26 17:09 +0100 |
| Message-ID | <napt6d$1us$1@dont-email.me> |
| In reply to | #41241 |
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]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2016-02-26 17:29 +0000 |
| Message-ID | <SX%zy.610$LP.435@fx16.iad> |
| In reply to | #41254 |
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]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2016-02-26 18:42 +0100 |
| Message-ID | <naq2l0$nbf$1@dont-email.me> |
| In reply to | #41260 |
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]
| From | Les Cargill <lcargill99@comcast.com> |
|---|---|
| Date | 2016-02-26 19:07 -0600 |
| Message-ID | <naqs8m$nao$2@dont-email.me> |
| In reply to | #41254 |
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]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c++
csiph-web