From mboxrd@z Thu Jan 1 00:00:00 1970 From: Detlev Zundel Date: Wed, 13 Jul 2011 13:28:22 +0200 Subject: [U-Boot] [RESEND PATCH v2 1/5] Tegra2: Add macros to calculate bitfield shifts and masks In-Reply-To: (Anton Staaf's message of "Tue, 12 Jul 2011 16:11:08 -0700") References: <1309884558-7700-1-git-send-email-sjg@chromium.org> <1309884558-7700-2-git-send-email-sjg@chromium.org> <4E185DFC.9010605@aribaud.net> <20110711061633.7A28E1579E14@gemini.denx.de> <20110712193029.5571F16F12CE@gemini.denx.de> <20110712211832.0D2ED1579E09@gemini.denx.de> Message-ID: List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Hi Anton, [...] > The only problem with this is that there is one piece of missing > information, which is how do you get the value of the field that is > masked as if it were a 4-bit or 3-bit number. ?That is, if I want the > IPS_DIV value, I probably want: > > ? ?(value & SCFR1_IPS_DIV_MASK) >> 20 > > Likewise, if I want to set the IPS divisor to 5 say, I would have to do: > > ? ?(value & ~SCFR1_IPS_DIV_MASK) | (5 << 20) > > In both cases the value 20 needs to come from somewhere. ?So you would > probably end up having: > > ? ?#define SCFR1_IPS_DIV_MASK ? ? ?0x03800000 > ? ?#define SCFR1_IPS_DIV_SHIFT ? ? ?20 > > and > > ? ?(value & SCFR1_IPS_DIV_MASK) >> SCFR1_IPS_DIV_SHIFT > ? ?(value & ~SCFR1_IPS_DIV_MASK) | (5 << SCFR1_IPS_DIV_SHIFT) > > And I think it would be great if these could turn into: > > ? ?field_value = GET_FIELD(register_value, SCFR1_IPS_DIV) > ? ?register_value = SET_FIELD(register_value, SCFR1_IPS_DIV, 5) Let me take this opportunity to once more explain why I don't like this. As a big fan of functional programming, I personally have grown used to code as explicit as possible. So _all_ arguments to a function should be explicit in the call - reliance on state or such "hidden arguments" violate this principle strongly. I learnt that code following these principles written by other people is much easier for me to understand. Cheers Detlev -- There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors. -- DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: dzu at denx.de