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


Groups > linux.kernel > #1592640

Re: [PATCH 1/3] futex: remove duplicated code

From Stafford Horne <shorne@gmail.com>
Newsgroups linux.kernel
Subject Re: [PATCH 1/3] futex: remove duplicated code
Date 2017-03-05 00:40 +0100
Message-ID <thrrj-89Z-5@gated-at.bofh.it> (permalink)
References <tgUF4-1cK-7@gated-at.bofh.it> <thhLk-18A-9@gated-at.bofh.it> <thnxn-5re-7@gated-at.bofh.it> <thpzb-6Um-13@gated-at.bofh.it> <thr7X-81G-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Sat, Mar 04, 2017 at 03:08:50PM -0800, H. Peter Anvin wrote:
> <davem@davemloft.net>,Chris Metcalf <cmetcalf@mellanox.com>,Thomas Gleixner <tglx@linutronix.de>,Ingo Molnar <mingo@redhat.com>,Chris Zankel <chris@zankel.net>,Max Filippov <jcmvbkbc@gmail.com>,Arnd Bergmann <arnd@arndb.de>,x86@kernel.org,linux-alpha@vger.kernel.org,linux-snps-arc@lists.infradead.org,linux-arm-kernel@lists.infradead.org,linux-hexagon@vger.kernel.org,linux-ia64@vger.kernel.org,linux-mips@linux-mips.org,openrisc@lists.librecores.org,linux-parisc@vger.kernel.org,linuxppc-dev@lists.ozlabs.org,linux-s390@vger.kernel.org,linux-sh@vger.kernel.org,sparclinux@vger.kernel.org,linux-xtensa@linux-xtensa.org,linux-arch@vger.kernel.org
> From: hpa@zytor.com
> Message-ID: <CF18535E-39E7-44D3-88D0-80B9961E6681@zytor.com>
> 
> On March 4, 2017 1:38:05 PM PST, Stafford Horne <shorne@gmail.com> wrote:
> >On Sat, Mar 04, 2017 at 11:15:17AM -0800, H. Peter Anvin wrote:
> >> On 03/04/17 05:05, Russell King - ARM Linux wrote:
> >> >>  
> >> >> +static int futex_atomic_op_inuser(int encoded_op, u32 __user
> >*uaddr)
> >> >> +{
> >> >> +	int op = (encoded_op >> 28) & 7;
> >> >> +	int cmp = (encoded_op >> 24) & 15;
> >> >> +	int oparg = (encoded_op << 8) >> 20;
> >> >> +	int cmparg = (encoded_op << 20) >> 20;
> >> > 
> >> > Hmm.  oparg and cmparg look like they're doing these shifts to get
> >sign
> >> > extension of the 12-bit values by assuming that "int" is 32-bit -
> >> > probably worth a comment, or for safety, they should be "s32" so
> >it's
> >> > not dependent on the bit-width of "int".
> >> > 
> >> 
> >> For readability, perhaps we should make sign- and zero-extension an
> >> explicit facility?
> >
> >There is some of this in already here, 32 and 64 bit versions:
> >
> >  include/linux/bitops.h
> >
> >Do we really need zero extension? It seems the same.
> >
> >Example implementation from bitops.h
> >
> >static inline __s32 sign_extend32(__u32 value, int index)
> >{
> >        __u8 shift = 31 - index;
> >        return (__s32)(value << shift) >> shift;
> >}
> >
> >> /*
> >>  * Truncate an integer x to n bits, using sign- or
> >>  * zero-extension, respectively.
> >>  */
> >> static inline __const_func__ s32 sex32(s32 x, int n)
> >> {
> >>   return (x << (32-n)) >> (32-n);
> >> }
> >> 
> >> static inline __const_func__ s64 sex64(s64 x, int n)
> >> {
> >>   return (x << (64-n)) >> (64-n);
> >> }
> >> 
> >> #define sex(x,y)						\
> >> 	((__typeof__(x))					\
> >> 	 (((__builtin_constant_p(y) && ((y) <= 32)) ||		\
> >> 	   (sizeof(x) <= sizeof(s32)))				\
> >> 	  ? sex32((x),(y)) : sex64((x),(y))))
> >> 
> >> static inline __const_func__ u32 zex32(u32 x, int n)
> >> {
> >>   return (x << (32-n)) >> (32-n);
> >> }
> >> 
> >> static inline __const_func__ u64 zex64(u64 x, int n)
> >> {
> >>   return (x << (64-n)) >> (64-n);
> >> }
> >> 
> >> #define zex(x,y)						\
> >> 	((__typeof__(x))					\
> >> 	 (((__builtin_constant_p(y) && ((y) <= 32)) ||		\
> >> 	   (sizeof(x) <= sizeof(u32)))				\
> >> 	  ? zex32((x),(y)) : zex64((x),(y))))
> >> 
> 
> Also, i strongly believe that making it syntactically cumbersome encodes people to open-code it which is bad...

Right, I missed the signed vs unsigned bit.

And it is cumbersome, this would be better

> -- 
> Sent from my Android device with K-9 Mail. Please excuse my brevity.

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


Thread

[PATCH 1/3] futex: remove duplicated code Jiri Slaby <jslaby@suse.cz> - 2017-03-03 13:40 +0100
  [PATCH 2/3] futex: fix decoding of operation Jiri Slaby <jslaby@suse.cz> - 2017-03-03 19:30 +0100
  [PATCH 3/3] futex: make the encoded_op decoding readable Jiri Slaby <jslaby@suse.cz> - 2017-03-03 19:30 +0100
    Re: [PATCH 3/3] futex: make the encoded_op decoding readable Jiri Slaby <jslaby@suse.cz> - 2017-03-05 09:00 +0100
  Re: [PATCH 1/3] futex: remove duplicated code Heiko Carstens <heiko.carstens@de.ibm.com> - 2017-03-03 21:10 +0100
  Re: [PATCH 1/3] futex: remove duplicated code Michael Ellerman <mpe@ellerman.id.au> - 2017-03-04 14:00 +0100
  Re: [PATCH 1/3] futex: remove duplicated code Russell King - ARM Linux <linux@armlinux.org.uk> - 2017-03-04 14:20 +0100
    Re: [PATCH 1/3] futex: remove duplicated code "H. Peter Anvin" <hpa@zytor.com> - 2017-03-04 20:30 +0100
      Re: [PATCH 1/3] futex: remove duplicated code Stafford Horne <shorne@gmail.com> - 2017-03-04 22:40 +0100
        Re: [PATCH 1/3] futex: remove duplicated code "H. Peter Anvin" <hpa@zytor.com> - 2017-03-05 00:20 +0100
          Re: [PATCH 1/3] futex: remove duplicated code Stafford Horne <shorne@gmail.com> - 2017-03-05 00:40 +0100
            Re: [PATCH 1/3] futex: remove duplicated code Jiri Slaby <jslaby@suse.cz> - 2017-03-06 09:50 +0100
              Re: [PATCH 1/3] futex: remove duplicated code "H. Peter Anvin" <hpa@zytor.com> - 2017-03-06 10:20 +0100
              Re: [PATCH 1/3] futex: remove duplicated code Geert Uytterhoeven <geert@linux-m68k.org> - 2017-03-06 15:20 +0100
        Re: [PATCH 1/3] futex: remove duplicated code "H. Peter Anvin" <hpa@zytor.com> - 2017-03-05 00:20 +0100
    Re: [PATCH 1/3] futex: remove duplicated code Rob Landley <rob@landley.net> - 2017-03-09 05:20 +0100
      Re: [PATCH 1/3] futex: remove duplicated code "H. Peter Anvin" <hpa@zytor.com> - 2017-03-09 06:10 +0100
        Re: [PATCH 1/3] futex: remove duplicated code Rich Felker <dalias@libc.org> - 2017-03-09 23:50 +0100
  Re: [PATCH 1/3] futex: remove duplicated code Rich Felker <dalias@libc.org> - 2017-03-06 03:20 +0100
  Re: [PATCH 1/3] futex: remove duplicated code Vineet Gupta <Vineet.Gupta1@synopsys.com> - 2017-03-09 23:50 +0100

csiph-web