Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4A787C77B73 for ; Mon, 22 May 2023 12:25:58 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 11CE985021; Mon, 22 May 2023 14:24:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="XBsnnBEB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D93E884667; Mon, 22 May 2023 14:23:19 +0200 (CEST) Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DE7D7847C8 for ; Mon, 22 May 2023 14:23:08 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jens.wiklander@linaro.org Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-4f3baf04f0cso1925139e87.1 for ; Mon, 22 May 2023 05:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684758188; x=1687350188; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M2CVbqNckF43m3ty4BotwDE58WAGFSlKLdkpEY7V8JY=; b=XBsnnBEBxOg0EE/geZVsCvMl8WOUCesvlS7tMnpzZWExxFnoe+ZW6WN2RCeQmxVvkz 9LWlh69ynmKgBH8EE7OJLPoVw4pVYzShp8Elf+012cyVZ6IQEW/VL2K2z0xP5KtBF2cJ dPO+dXKS9Zf/+aZ1vp1hyDZXAH/I3KSMCVESK47xN7SeMbyyS1LP/VCG2Zl1quqjirGO qzO6hVBzfaZHqLhLnLV138pTkh2ihcY8vQpDUekKFk5Po8RTJbFn+n2juSKTR8cpIOCr aOs6klcR+1eICyqloJTe2/3qglmfssFe2ZXo0vDpDaj9d+V375XdtgCV4F8DVplroVfb YjMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684758188; x=1687350188; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M2CVbqNckF43m3ty4BotwDE58WAGFSlKLdkpEY7V8JY=; b=Ul9gON7QhAZLKbqUFAR8dnJqfIZ3YF+fn5SO64OgXBzPl9IK7I04JGrf0522JnqnxE w1YDxGp7cjrY8s8S/vxS8qVPxqaaWT+Jxidgy2pUeVGKJUPt+ziv2z58j8LktJwgWm8F aGOE0ISqqjDwLXJrvua1/sXXVE0NQUwtRmS6gH4+y6fWxL8Y4jDjK5XeASuTU1YNNDvO s2bIlvqbvOvOwMwvyPxiuIjngrngEFMK+WlxnMd5iGw179CLmccYCXhZBrfvRq8URE9d VWuuLJFbDeWEoFv5Oxe7e9C3LecEUd/wc5jvr//63p063ZVeJOdeLQmbNppqpq7j66Gh WbnA== X-Gm-Message-State: AC+VfDxFQtM9g38SdW8tzt9YhpFt57ESjYRvmWxd2MCgdaQUxWOmsX4m N140sXlBhITibhAbF4jRV7+WJeIq+vU/O8gb2ls= X-Google-Smtp-Source: ACHHUZ6vXBs+yoSqZqT3/15SvcvJIboKiRqZ7ovmbUF14Bbh1pV+ByRcEL2+X7pAVnLKAEYCWI0SCg== X-Received: by 2002:ac2:44d4:0:b0:4f4:7a2:643b with SMTP id d20-20020ac244d4000000b004f407a2643bmr1062913lfm.14.1684758188390; Mon, 22 May 2023 05:23:08 -0700 (PDT) Received: from rayden.urgonet (h-46-59-78-111.A175.priv.bahnhof.se. [46.59.78.111]) by smtp.gmail.com with ESMTPSA id y9-20020ac24469000000b004efe8174586sm974633lfl.126.2023.05.22.05.23.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 05:23:08 -0700 (PDT) From: Jens Wiklander To: u-boot@lists.denx.de, linux-btrfs@vger.kernel.org Cc: Ilias Apalodimas , Arnd Bergmann , Huan Wang , Angelo Dureghello , Daniel Schwierzeck , Wolfgang Denk , Marek Vasut , Nobuhiro Iwamatsu , Marek Behun , Qu Wenruo , Tom Rini , Jens Wiklander Subject: [PATCH 8/8] asm-generic: simplify unaligned.h Date: Mon, 22 May 2023 14:22:38 +0200 Message-Id: <20230522122238.4191762-9-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230522122238.4191762-1-jens.wiklander@linaro.org> References: <20230522122238.4191762-1-jens.wiklander@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean The get_unaligned()/put_unaligned() implementations are more complex than necessary. Move everything into one file and use a more compact implementation based on packed struct access and byte swapping macros. This patch is based on the Linux kernel commit 803f4e1eab7a ("asm-generic: simplify asm/unaligned.h") by Arnd Bergmann. Signed-off-by: Jens Wiklander --- include/asm-generic/unaligned.h | 89 +++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/include/asm-generic/unaligned.h b/include/asm-generic/unaligned.h index 3d33a5a063e8..9e5d93ec3041 100644 --- a/include/asm-generic/unaligned.h +++ b/include/asm-generic/unaligned.h @@ -1,24 +1,81 @@ +/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _GENERIC_UNALIGNED_H #define _GENERIC_UNALIGNED_H #include -#include -#include -#include - -/* - * Select endianness - */ -#if defined(__LITTLE_ENDIAN) -#define get_unaligned __get_unaligned_le -#define put_unaligned __put_unaligned_le -#elif defined(__BIG_ENDIAN) -#define get_unaligned __get_unaligned_be -#define put_unaligned __put_unaligned_be -#else -#error invalid endian -#endif +#define __get_unaligned_t(type, ptr) ({ \ + const struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \ + __pptr->x; \ +}) + +#define __put_unaligned_t(type, val, ptr) do { \ + struct { type x; } __packed * __pptr = (typeof(__pptr))(ptr); \ + __pptr->x = (val); \ +} while (0) + +#define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr)) +#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr)) + +static inline u16 get_unaligned_le16(const void *p) +{ + return le16_to_cpu(__get_unaligned_t(__le16, p)); +} + +static inline u32 get_unaligned_le32(const void *p) +{ + return le32_to_cpu(__get_unaligned_t(__le32, p)); +} + +static inline u64 get_unaligned_le64(const void *p) +{ + return le64_to_cpu(__get_unaligned_t(__le64, p)); +} + +static inline void put_unaligned_le16(u16 val, void *p) +{ + __put_unaligned_t(__le16, cpu_to_le16(val), p); +} + +static inline void put_unaligned_le32(u32 val, void *p) +{ + __put_unaligned_t(__le32, cpu_to_le32(val), p); +} + +static inline void put_unaligned_le64(u64 val, void *p) +{ + __put_unaligned_t(__le64, cpu_to_le64(val), p); +} + +static inline u16 get_unaligned_be16(const void *p) +{ + return be16_to_cpu(__get_unaligned_t(__be16, p)); +} + +static inline u32 get_unaligned_be32(const void *p) +{ + return be32_to_cpu(__get_unaligned_t(__be32, p)); +} + +static inline u64 get_unaligned_be64(const void *p) +{ + return be64_to_cpu(__get_unaligned_t(__be64, p)); +} + +static inline void put_unaligned_be16(u16 val, void *p) +{ + __put_unaligned_t(__be16, cpu_to_be16(val), p); +} + +static inline void put_unaligned_be32(u32 val, void *p) +{ + __put_unaligned_t(__be32, cpu_to_be32(val), p); +} + +static inline void put_unaligned_be64(u64 val, void *p) +{ + __put_unaligned_t(__be64, cpu_to_be64(val), p); +} /* Allow unaligned memory access */ void allow_unaligned(void); -- 2.34.1