From: Sergey Matyukevich <geomatsi@gmail.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 1/2] include: Add support for byteorder/endianness conversion
Date: Tue, 31 Jan 2023 11:11:08 +0300 [thread overview]
Message-ID: <Y9jNHM1dHedBPmAf@curiosity> (raw)
In-Reply-To: <20230128074617.1917265-2-rpathak@ventanamicro.com>
On Sat, Jan 28, 2023 at 01:16:16PM +0530, Rahul Pathak wrote:
> Define macros for general byteorder conversion
> Define functions for endianness conversion
> using general byteorder conversion macros
>
> Signed-off-by: Rahul Pathak <rpathak@ventanamicro.com>
> ---
> include/sbi/sbi_byteorder.h | 57 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 57 insertions(+)
> create mode 100644 include/sbi/sbi_byteorder.h
Reviewed-by: Sergey Matyukevich <sergey.matyukevich@syntacore.com>
By the way, is there any advantage of using custom macros instead of
compiler builtins ? E.g. see my patch loosely based on FreeBSD code:
http://lists.infradead.org/pipermail/opensbi/2022-December/003766.html
Regards,
Sergey
> diff --git a/include/sbi/sbi_byteorder.h b/include/sbi/sbi_byteorder.h
> new file mode 100644
> index 000000000000..680710fe6d91
> --- /dev/null
> +++ b/include/sbi/sbi_byteorder.h
> @@ -0,0 +1,57 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2023 Ventana Micro Systems Inc.
> + */
> +
> +#ifndef __SBI_BYTEORDER_H__
> +#define __SBI_BYTEORDER_H__
> +
> +#include <sbi/sbi_types.h>
> +
> +#define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n])
> +
> +#define BSWAP16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
> +#define BSWAP32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
> + (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
> +#define BSWAP64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
> + (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
> + (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
> + (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
> +
> +
> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* CPU(little-endian) */
> +#define cpu_to_be16(x) ((uint16_t)BSWAP16(x))
> +#define cpu_to_be32(x) ((uint32_t)BSWAP32(x))
> +#define cpu_to_be64(x) ((uint64_t)BSWAP64(x))
> +
> +#define be16_to_cpu(x) ((uint16_t)BSWAP16(x))
> +#define be32_to_cpu(x) ((uint32_t)BSWAP32(x))
> +#define be64_to_cpu(x) ((uint64_t)BSWAP64(x))
> +
> +#define cpu_to_le16(x) ((uint16_t)(x))
> +#define cpu_to_le32(x) ((uint32_t)(x))
> +#define cpu_to_le64(x) ((uint64_t)(x))
> +
> +#define le16_to_cpu(x) ((uint16_t)(x))
> +#define le32_to_cpu(x) ((uint32_t)(x))
> +#define le64_to_cpu(x) ((uint64_t)(x))
> +#else /* CPU(big-endian) */
> +#define cpu_to_be16(x) ((uint16_t)(x))
> +#define cpu_to_be32(x) ((uint32_t)(x))
> +#define cpu_to_be64(x) ((uint64_t)(x))
> +
> +#define be16_to_cpu(x) ((uint16_t)(x))
> +#define be32_to_cpu(x) ((uint32_t)(x))
> +#define be64_to_cpu(x) ((uint64_t)(x))
> +
> +#define cpu_to_le16(x) ((uint16_t)BSWAP16(x))
> +#define cpu_to_le32(x) ((uint32_t)BSWAP32(x))
> +#define cpu_to_le64(x) ((uint64_t)BSWAP64(x))
> +
> +#define le16_to_cpu(x) ((uint16_t)BSWAP16(x))
> +#define le32_to_cpu(x) ((uint32_t)BSWAP32(x))
> +#define le64_to_cpu(x) ((uint64_t)BSWAP64(x))
> +#endif
> +
> +#endif /* __SBI_BYTEORDER_H__ */
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
next prev parent reply other threads:[~2023-01-31 8:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-28 7:46 [PATCH 0/2] Add endianness conversion support Rahul Pathak
2023-01-28 7:46 ` [PATCH 1/2] include: Add support for byteorder/endianness conversion Rahul Pathak
2023-01-28 13:12 ` Xiang W
2023-01-31 8:11 ` Sergey Matyukevich [this message]
2023-02-01 7:38 ` Rahul Pathak
2023-02-02 9:24 ` Sergey Matyukevich
2023-02-02 9:32 ` Rahul Pathak
2023-01-28 7:46 ` [PATCH 2/2] lib: utils/fdt: Use byteorder conversion functions in libfdt_env.h Rahul Pathak
2023-01-28 13:13 ` Xiang W
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=Y9jNHM1dHedBPmAf@curiosity \
--to=geomatsi@gmail.com \
--cc=opensbi@lists.infradead.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