KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 arch/dreamcast/include/arch/byteorder.h
4 Copyright (C) 2015 Lawrence Sebald
5
6*/
7
8/** \file arch/byteorder.h
9 \brief Byte-order related macros.
10 \ingroup system_arch
11
12 This file contains architecture-specific byte-order related macros and/or
13 functions. Each platform should define six macros/functions in this file:
14 arch_swap16, arch_swap32, arch_ntohs, arch_ntohl, arch_htons, and
15 arch_htonl. The first two of these swap the byte order of 16-bit and 32-bit
16 integers, respectively. The other four macros will be used by the kernel to
17 implement the network-related byte order functions.
18
19 \author Lawrence Sebald
20*/
21
22#ifndef __ARCH_BYTEORDER_H
23#define __ARCH_BYTEORDER_H
24
25#include <sys/cdefs.h>
26__BEGIN_DECLS
27
28#ifdef BYTE_ORDER
29/* If we've included <arch/types.h>, this might already be defined... */
30#undef BYTE_ORDER
31#endif
32
33/** \defgroup system_arch Byte Order
34 \brief Byte-order management for the SH4 architecture
35 \ingroup arch
36
37 @{
38*/
39
40/** \brief Define the byte-order of the platform in use. */
41#define BYTE_ORDER LITTLE_ENDIAN
42
43/** \brief Swap the byte order of a 16-bit integer.
44
45 This macro swaps the byte order of a 16-bit integer in an architecture-
46 defined manner.
47
48 \param x The value to be byte-swapped. This should be a uint16,
49 or equivalent.
50 \return The swapped value.
51*/
52#define arch_swap16(x) ({ \
53 uint16_t __x = (x); \
54 __asm__ __volatile__("swap.b %0, %0" : "=r" (__x) : "0" (__x)); \
55 __x; \
56})
57
58/** \brief Swap the byte order of a 32-bit integer.
59
60 This macro swaps the byte order of a 32-bit integer in an architecture-
61 defined manner.
62
63 \param x The value to be byte-swapped. This should be a uint32,
64 or equivalent.
65 \return The swapped value.
66*/
67#define arch_swap32(x) ({ \
68 uint32_t __x = (x); \
69 __asm__ __volatile__("swap.b %0, %0\n\t" \
70 "swap.w %0, %0\n\t" \
71 "swap.b %0, %0\n\t" : "=r"(__x) : "0" (__x)); \
72 __x; \
73})
74
75/** \brief Convert network-to-host short.
76
77 This macro converts a network byte order (big endian) value to the host's
78 native byte order. On a little endian system (like the Dreamcast), this
79 should just call arch_swap16(). On a big endian system, this should be a
80 no-op.
81
82 \param x The value to be converted. This should be a uint16,
83 or equivalent.
84 \return The converted value.
85*/
86#define arch_ntohs(x) arch_swap16(x)
87
88/** \brief Convert network-to-host long.
89
90 This macro converts a network byte order (big endian) value to the host's
91 native byte order. On a little endian system (like the Dreamcast), this
92 should just call arch_swap32(). On a big endian system, this should be a
93 no-op.
94
95 \param x The value to be converted. This should be a uint32,
96 or equivalent.
97 \return The converted value.
98*/
99#define arch_ntohl(x) arch_swap32(x)
100
101/** \brief Convert host-to-network short.
102
103 This macro converts a value in the host's native byte order to network byte
104 order (big endian). On a little endian system (like the Dreamcast), this
105 should just call arch_swap16(). On a big endian system, this should be a
106 no-op.
107
108 \param x The value to be converted. This should be a uint16,
109 or equivalent.
110 \return The converted value.
111*/
112#define arch_htons(x) arch_swap16(x)
113
114/** \brief Convert host-to-network long.
115
116 This macro converts a value in the host's native byte order to network byte
117 order (big endian). On a little endian system (like the Dreamcast), this
118 should just call arch_swap32(). On a big endian system, this should be a
119 no-op.
120
121 \param x The value to be converted. This should be a uint32,
122 or equivalent.
123 \return The converted value.
124*/
125#define arch_htonl(x) arch_swap32(x)
126
127/** @} */
128
129__END_DECLS
130
131#endif /* !__ARCH_BYTEORDER_H */