linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Syed Nayyar Waris <syednwaris@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	William Breathitt Gray <vilhelm.gray@gmail.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Linux-Arch <linux-arch@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v11 1/4] bitops: Introduce the for_each_set_clump macro
Date: Fri, 16 Oct 2020 12:17:04 +0300	[thread overview]
Message-ID: <20201016091704.GE4077@smile.fi.intel.com> (raw)
In-Reply-To: <CACG_h5pYL+HbJpPcCTp=dR8rDbm07RsRDaX8Uc0HYc2LG--w_Q@mail.gmail.com>

On Fri, Oct 16, 2020 at 04:23:05AM +0530, Syed Nayyar Waris wrote:
> On Tue, Oct 6, 2020 at 4:56 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> > On Tue, Oct 06, 2020 at 02:52:16PM +0530, Syed Nayyar Waris wrote:

...

> > > +             return (map[index] >> offset) & GENMASK(nbits - 1, 0);
> >
> > Have you considered to use rather BIT{_ULL}(nbits) - 1?
> > It maybe better for code generation.
> 
> Yes I have considered using BIT{_ULL} in earlier versions of patchset.
> It has a problem:
> 
> This macro when used in both bitmap_get_value and
> bitmap_set_value functions, it will give unexpected results when nbits or clump
> size is BITS_PER_LONG (32 or 64 depending on arch).
> 
> Actually when nbits (clump size) is 64 (BITS_PER_LONG is 64, for example),
> (BIT(nbits) - 1)
> gives a value of zero and when this zero is ANDed with any value, it
> makes it full zero. This is unexpected, and incorrect calculation occurs.
> 
> What actually happens is in the macro expansion of BIT(64), that is 1
> << 64, the '1' overflows from leftmost bit position (most significant
> bit) and re-enters at the rightmost bit position (least significant
> bit), therefore 1 << 64 becomes '0x1', and when another '1' is
> subtracted from this, the final result becomes 0.
> 
> This is undefined behavior in the C standard (section 6.5.7 in the N1124)

I see, indeed, for 64/32 it is like this.

...

> Yes I have incorporated your suggestion to use the '<<' operator. Thank You.

One side note, consider the use round_up() vs. roundup(). I don't remember
which one is optimized to divisor being power of 2.

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2020-10-16  9:16 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-06  9:20 [PATCH v11 0/4] Introduce the for_each_set_clump macro Syed Nayyar Waris
2020-10-06  9:22 ` [PATCH v11 1/4] bitops: " Syed Nayyar Waris
2020-10-06 11:27   ` Andy Shevchenko
2020-10-15 22:53     ` Syed Nayyar Waris
2020-10-16  9:17       ` Andy Shevchenko [this message]
2020-10-16 11:45         ` Syed Nayyar Waris
2020-10-07  8:38 ` [PATCH v11 0/4] " Linus Walleij
2020-10-23 13:20   ` Syed Nayyar Waris

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=20201016091704.GE4077@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syednwaris@gmail.com \
    --cc=vilhelm.gray@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).