Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752316AbdLKFiI (ORCPT ); Mon, 11 Dec 2017 00:38:08 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:60122 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752165AbdLKFiF (ORCPT ); Mon, 11 Dec 2017 00:38:05 -0500 Date: Mon, 11 Dec 2017 05:38:03 +0000 From: Al Viro To: Jie Deng Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds Subject: Re: [RFC][PATCH] apparent big-endian bugs in dwc-xlgmac Message-ID: <20171211053803.GW21978@ZenIV.linux.org.uk> References: <20171210045326.GO21978@ZenIV.linux.org.uk> <420a198d-61f8-81cf-646d-10446cb41def@synopsys.com> <20171211050520.GV21978@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171211050520.GV21978@ZenIV.linux.org.uk> User-Agent: Mutt/1.9.0 (2017-09-02) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2536 Lines: 83 On Mon, Dec 11, 2017 at 05:05:20AM +0000, Al Viro wrote: > What for? Sure, this variant will work, but why bother with > a = le32_to_cpu(b); > (cpu_to_le32(a) & ....) | .... > and how is that better than > (b & ...) | ... > > IDGI... Mind you, I'm not sure if there is any point keeping _var in that thing, > seeing that we use var only once - might be better off with > ((var) & ~cpu_to_le32(GENMASK(_pos + _len - 1, _pos))) | \ > cpu_to_le32(_val); \ FWIW, seeing how many drivers end up open-coding that, I'm rather tempted to add to linux/bitops.h or linux/byteorder/generic.h the following: static inline __le16 le16_replace_bits(__le16 old, u16 v, int bit, int size) { __le16 mask = cpu_to_le16(GENMASK(bit + size - 1, bit)); return (old & ~mask) | (cpu_to_le16(v << bit) & mask); } static inline __le32 le32_replace_bits(__le32 old, u32 v, int bit, int size) { __le32 mask = cpu_to_le32(GENMASK(bit + size - 1, bit)); return (old & ~mask) | (cpu_to_le32(v << bit) & mask); } static inline __le64 le64_replace_bits(__le64 old, u64 v, int bit, int size) { __le64 mask = cpu_to_le64(GENMASK_ULL(bit + size - 1, bit)); return (old & ~mask) | (cpu_to_le64(v << bit) & mask); } static inline __be16 be16_replace_bits(__be16 old, u16 v, int bit, int size) { __be16 mask = cpu_to_be16(GENMASK(bit + size - 1, bit)); return (old & ~mask) | (cpu_to_be16(v << bit) & mask); } static inline __be32 be32_replace_bits(__be32 old, u32 v, int bit, int size) { __be32 mask = cpu_to_be32(GENMASK(bit + size - 1, bit)); return (old & ~mask) | (cpu_to_be32(v << bit) & mask); } static inline __be64 be64_replace_bits(__be64 old, u64 v, int bit, int size) { __be64 mask = cpu_to_be64(GENMASK_ULL(bit + size - 1, bit)); return (old & ~mask) | (cpu_to_be64(v << bit) & mask); } static inline u16 le16_get_bits(__le16 v, int bit, int size) { return (le16_to_cpu(v) >> bit) & (BIT(size) - 1); } static inline u32 le32_get_bits(__le32 v, int bit, int size) { return (le32_to_cpu(v) >> bit) & (BIT(size) - 1); } static inline u64 le64_get_bits(__le64 v, int bit, int size) { return (le64_to_cpu(v) >> bit) & (BIT_ULL(size) - 1); } static inline u16 be16_get_bits(__be16 v, int bit, int size) { return (be16_to_cpu(v) >> bit) & (BIT(size) - 1); } static inline u32 be32_get_bits(__be32 v, int bit, int size) { return (be32_to_cpu(v) >> bit) & (BIT(size) - 1); } static inline u64 be64_get_bits(__be64 v, int bit, int size) { return (be64_to_cpu(v) >> bit) & (BIT_ULL(size) - 1); } and let drivers use those...