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


Groups > comp.lang.c++ > #41215

Re: Union of bitfields and larger type on ARM

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.c++, comp.dsp, sci.electronics.design
Subject Re: Union of bitfields and larger type on ARM
Date 2016-02-24 09:44 +0100
Organization A noiseless patient Spider
Message-ID <najqc2$a2r$1@dont-email.me> (permalink)
References <If2zy.23937$Nf2.15642@fx14.iad> <g6qdnaMZvfFJSVHLnZ2dnUU7-fOdnZ2d@giganews.com> <naindd$71s$1@dont-email.me>

Cross-posted to 3 groups.

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web