LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: "Haavard Skinnemoen" <hskinnemoen@gmail.com>
To: "Pavel Pisa" <pisa@cmp.felk.cvut.cz>
Cc: "Russell King - ARM Linux" <linux@arm.linux.org.uk>,
	linux-kernel@vger.kernel.org,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Pierre Ossman" <drzeus@drzeus.cx>
Subject: Re: Coding style question
Date: Fri, 9 Feb 2007 21:46:33 +0100	[thread overview]
Message-ID: <1defaf580702091246i1f49a787g482e25817517d7ed@mail.gmail.com> (raw)
In-Reply-To: <200702091132.09741.pisa@cmp.felk.cvut.cz>

On 2/9/07, Pavel Pisa <pisa@cmp.felk.cvut.cz> wrote:

> #define __val2mfld(mask,val) (((mask)&~((mask)<<1))*(val)&(mask))
>
> #define __mfld2val(mask,val) (((val)&(mask))/((mask)&~((mask)<<1)))

Looks a bit similar to the style I tend to use a lot:

/* Bit manipulation macros */
#define MACB_BIT(name)                                  \
        (1 << MACB_##name##_OFFSET)
#define MACB_BF(name,value)                             \
        (((value) & ((1 << MACB_##name##_SIZE) - 1))    \
         << MACB_##name##_OFFSET)
#define MACB_BFEXT(name,value)\
        (((value) >> MACB_##name##_OFFSET)              \
         & ((1 << MACB_##name##_SIZE) - 1))
#define MACB_BFINS(name,value,old)                      \
        (((old) & ~(((1 << MACB_##name##_SIZE) - 1)     \
                    << MACB_##name##_OFFSET))           \
         | MACB_BF(name,value))

where BF stands for bitfield, EXT for extract and INS for insert.

The macros are butt ugly, but code using them is hopefully quite easy
to read (I'm of course not qualified to judge code I wrote myself.)
The somewhat excessive pasting ensures that if you ever switch the
name and value arguments, the compiler will let you know.

Example usage:

macb_writel(bp, REG, MACB_BF(FIELD, value));
regval = macb_readl(bp, REG);
value = MACB_BFEXT(FIELD, regval);

Haavard

      parent reply	other threads:[~2007-02-09 20:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-09 10:32 Pavel Pisa
2007-02-09 11:18 ` Jesper Juhl
2007-02-09 16:02 ` Stefan Richter
2007-02-09 20:46 ` Haavard Skinnemoen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1defaf580702091246i1f49a787g482e25817517d7ed@mail.gmail.com \
    --to=hskinnemoen@gmail.com \
    --cc=drzeus@drzeus.cx \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=pisa@cmp.felk.cvut.cz \
    --cc=s.hauer@pengutronix.de \
    --subject='Re: Coding style question' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).