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


Groups > linux.kernel > #1436926

RE: [PATCHv3 wl-drv-next 1/2] add basic register-field manipulation macros

From David Laight <David.Laight@ACULAB.COM>
Newsgroups linux.kernel
Subject RE: [PATCHv3 wl-drv-next 1/2] add basic register-field manipulation macros
Date 2016-07-05 13:10 +0200
Message-ID <rRwoN-21i-17@gated-at.bofh.it> (permalink)
References <rQeaB-3g8-3@gated-at.bofh.it> <rQeaB-3g8-17@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Jakub Kicinski
> Sent: 01 July 2016 22:27
> 
> C bitfields are problematic and best avoided.  Developers
> interacting with hardware registers find themselves searching
> for easy-to-use alternatives.  Common approach is to define
> structures or sets of macros containing mask and shift pair.
> Operations on the register are then performed as follows:
> 
>  field = (reg >> shift) & mask;
> 
>  reg &= ~(mask << shift);
>  reg |= (field & mask) << shift;
> 
> Defining shift and mask separately is tedious.  Ivo van Doorn
> came up with an idea of computing them at compilation time
> based on a single shifted mask (later refined by Felix) which
> can be used like this:
> 
>  #define REG_FIELD 0x000ff000
> 
>  field = FIELD_GET(REG_FIELD, reg);
> 
>  reg &= ~REG_FIELD;
>  reg |= FIELD_PUT(REG_FIELD, field);

My problem with these sort of 'helpers' is that they make it much harder
to read code unless you happen to know exactly what the helpers do.
Unexpected issues (like values being sign extended) can be hard to spot.

A lot of the time you can make things simpler by not doing the shifts
(ie define shifted constants).

	David

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCHv3 wl-drv-next 1/2] add basic register-field manipulation macros Jakub Kicinski <jakub.kicinski@netronome.com> - 2016-07-01 23:30 +0200
  Re: [PATCHv3 wl-drv-next 1/2] add basic register-field manipulation  macros Hannes Frederic Sowa <hannes@stressinduktion.org> - 2016-07-03 22:00 +0200
  RE: [PATCHv3 wl-drv-next 1/2] add basic register-field manipulation  macros David Laight <David.Laight@ACULAB.COM> - 2016-07-05 13:10 +0200
    Re: [PATCHv3 wl-drv-next 1/2] add basic register-field manipulation  macros Jakub Kicinski <jakub.kicinski@netronome.com> - 2016-07-05 13:30 +0200

csiph-web