mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: Nina Schoetterl-Glausch <nsg@linux.ibm.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Janosch Frank <frankja@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
	David Hildenbrand <david@redhat.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] KVM: s390: selftest: memop: Fix undefined behavior
Date: Mon, 18 Dec 2023 15:58:59 +0100	[thread overview]
Message-ID: <20231218153358.14acb611@p-imbrenda> (raw)
In-Reply-To: <59621e88c5c29bdff8bc06f68b02b2c7a420a09a.camel@linux.ibm.com>

On Mon, 18 Dec 2023 13:18:14 +0100
Nina Schoetterl-Glausch <nsg@linux.ibm.com> wrote:

[...]

> > > diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c
> > > index bb3ca9a5d731..2eba9575828e 100644
> > > --- a/tools/testing/selftests/kvm/s390x/memop.c
> > > +++ b/tools/testing/selftests/kvm/s390x/memop.c
> > > @@ -485,11 +485,13 @@ static bool popcount_eq(__uint128_t a, __uint128_t b)
> > >  
> > >  static __uint128_t rotate(int size, __uint128_t val, int amount)
> > >  {
> > > -	unsigned int bits = size * 8;
> > > +	unsigned int left, right, bits = size * 8;
> > >    
> > 
> > ...why not just:
> > 
> > if (!amount)
> > 	return val;
> > 
> > ?  
> 
> That works if you move it one statement down (128 would also trigger UB).

oops, yes it has to be after

> % 128 does the trick, is branchless and there is a bit of a symmetry going
> on between right and left.
> But I can use an early return if you want.

I think it's more readable, and furthermore...

> 
> >   
> > > -	amount = (amount + bits) % bits;
> > > +	right = (amount + bits) % bits;
> > > +	/* % 128 prevents left shift UB if size == 16 && right == 0 */
> > > +	left = (bits - right) % 128;
> > >  	val = cut_to_size(size, val);
> > > -	return (val << (bits - amount)) | (val >> amount);

...this is a more idiomatic syntax for a rotate operation 

> > > +	return (val << left) | (val >> right);
> > >  }
> > >  
> > >  const unsigned int max_block = 16;
> > > 
> > > base-commit: 305230142ae0637213bf6e04f6d9f10bbcb74af8  
> >   
> 


      reply	other threads:[~2023-12-18 14:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-15 16:11 Nina Schoetterl-Glausch
2023-12-15 17:02 ` Claudio Imbrenda
2023-12-18 12:18   ` Nina Schoetterl-Glausch
2023-12-18 14:58     ` Claudio Imbrenda [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=20231218153358.14acb611@p-imbrenda \
    --to=imbrenda@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=david@redhat.com \
    --cc=frankja@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=nsg@linux.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=shuah@kernel.org \
    /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

all inboxes | Powered by JetHome®