Skip to content

Commit 5c9a735

Browse files
dramforeveravpatel
authored andcommitted
include: sbi: Support byteorder macros in assembly
Avoid using C types and casts if sbi/sbi_byteorder.h is included in assembly code Signed-off-by: Vivian Wang <dramforever@live.com> Reviewed-by: Anup Patel <anup@brainfault.org>
1 parent 06fc453 commit 5c9a735

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

include/sbi/sbi_byteorder.h

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
#ifndef __SBI_BYTEORDER_H__
88
#define __SBI_BYTEORDER_H__
99

10-
#include <sbi/sbi_types.h>
10+
#ifdef __ASSEMBLER__
11+
# define _conv_cast(type, val) (val)
12+
#else
13+
# include <sbi/sbi_types.h>
14+
# define _conv_cast(type, val) ((type)(val))
15+
#endif
1116

1217
#define BSWAP16(x) ((((x) & 0x00ff) << 8) | \
1318
(((x) & 0xff00) >> 8))
@@ -25,37 +30,37 @@
2530
(((x) & 0xff00000000000000ULL) >> 56))
2631

2732
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ /* CPU(little-endian) */
28-
#define cpu_to_be16(x) ((uint16_t)BSWAP16(x))
29-
#define cpu_to_be32(x) ((uint32_t)BSWAP32(x))
30-
#define cpu_to_be64(x) ((uint64_t)BSWAP64(x))
33+
#define cpu_to_be16(x) _conv_cast(uint16_t, BSWAP16(x))
34+
#define cpu_to_be32(x) _conv_cast(uint32_t, BSWAP32(x))
35+
#define cpu_to_be64(x) _conv_cast(uint64_t, BSWAP64(x))
3136

32-
#define be16_to_cpu(x) ((uint16_t)BSWAP16(x))
33-
#define be32_to_cpu(x) ((uint32_t)BSWAP32(x))
34-
#define be64_to_cpu(x) ((uint64_t)BSWAP64(x))
37+
#define be16_to_cpu(x) _conv_cast(uint16_t, BSWAP16(x))
38+
#define be32_to_cpu(x) _conv_cast(uint32_t, BSWAP32(x))
39+
#define be64_to_cpu(x) _conv_cast(uint64_t, BSWAP64(x))
3540

36-
#define cpu_to_le16(x) ((uint16_t)(x))
37-
#define cpu_to_le32(x) ((uint32_t)(x))
38-
#define cpu_to_le64(x) ((uint64_t)(x))
41+
#define cpu_to_le16(x) _conv_cast(uint16_t, (x))
42+
#define cpu_to_le32(x) _conv_cast(uint32_t, (x))
43+
#define cpu_to_le64(x) _conv_cast(uint64_t, (x))
3944

40-
#define le16_to_cpu(x) ((uint16_t)(x))
41-
#define le32_to_cpu(x) ((uint32_t)(x))
42-
#define le64_to_cpu(x) ((uint64_t)(x))
45+
#define le16_to_cpu(x) _conv_cast(uint16_t, (x))
46+
#define le32_to_cpu(x) _conv_cast(uint32_t, (x))
47+
#define le64_to_cpu(x) _conv_cast(uint64_t, (x))
4348
#else /* CPU(big-endian) */
44-
#define cpu_to_be16(x) ((uint16_t)(x))
45-
#define cpu_to_be32(x) ((uint32_t)(x))
46-
#define cpu_to_be64(x) ((uint64_t)(x))
49+
#define cpu_to_be16(x) _conv_cast(uint16_t, (x))
50+
#define cpu_to_be32(x) _conv_cast(uint32_t, (x))
51+
#define cpu_to_be64(x) _conv_cast(uint64_t, (x))
4752

48-
#define be16_to_cpu(x) ((uint16_t)(x))
49-
#define be32_to_cpu(x) ((uint32_t)(x))
50-
#define be64_to_cpu(x) ((uint64_t)(x))
53+
#define be16_to_cpu(x) _conv_cast(uint16_t, (x))
54+
#define be32_to_cpu(x) _conv_cast(uint32_t, (x))
55+
#define be64_to_cpu(x) _conv_cast(uint64_t, (x))
5156

52-
#define cpu_to_le16(x) ((uint16_t)BSWAP16(x))
53-
#define cpu_to_le32(x) ((uint32_t)BSWAP32(x))
54-
#define cpu_to_le64(x) ((uint64_t)BSWAP64(x))
57+
#define cpu_to_le16(x) _conv_cast(uint16_t, BSWAP16(x))
58+
#define cpu_to_le32(x) _conv_cast(uint32_t, BSWAP32(x))
59+
#define cpu_to_le64(x) _conv_cast(uint64_t, BSWAP64(x))
5560

56-
#define le16_to_cpu(x) ((uint16_t)BSWAP16(x))
57-
#define le32_to_cpu(x) ((uint32_t)BSWAP32(x))
58-
#define le64_to_cpu(x) ((uint64_t)BSWAP64(x))
61+
#define le16_to_cpu(x) _conv_cast(uint16_t, BSWAP16(x))
62+
#define le32_to_cpu(x) _conv_cast(uint32_t, BSWAP32(x))
63+
#define le64_to_cpu(x) _conv_cast(uint64_t, BSWAP64(x))
5964
#endif
6065

6166
#if __riscv_xlen == 64

0 commit comments

Comments
 (0)