Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751956AbdLKGq4 (ORCPT ); Mon, 11 Dec 2017 01:46:56 -0500 Received: from smtprelay.synopsys.com ([198.182.47.9]:50275 "EHLO smtprelay.synopsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750709AbdLKGqv (ORCPT ); Mon, 11 Dec 2017 01:46:51 -0500 Subject: Re: [RFC][PATCH] apparent big-endian bugs in dwc-xlgmac To: Al Viro , Jie Deng CC: , , Linus Torvalds References: <20171210045326.GO21978@ZenIV.linux.org.uk> <420a198d-61f8-81cf-646d-10446cb41def@synopsys.com> <20171211050520.GV21978@ZenIV.linux.org.uk> <20171211053803.GW21978@ZenIV.linux.org.uk> From: Jie Deng Message-ID: <8a5f768c-1db5-e892-0396-0dcc975d3a5d@synopsys.com> Date: Mon, 11 Dec 2017 14:46:47 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20171211053803.GW21978@ZenIV.linux.org.uk> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Language: en-US X-Originating-IP: [10.13.184.19] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2770 Lines: 85 On 2017/12/11 13:38, Al Viro wrote: > 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... Sounds good. As a driver developer, I'm happy to see this.