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*/
52static inline uint16_t arch_swap16(uint16_t x) {
53 return __builtin_bswap16(x);
54}
55
56/** \brief Swap the byte order of a 32-bit integer.
57
58 This macro swaps the byte order of a 32-bit integer in an architecture-
59 defined manner.
60
61 \param x The value to be byte-swapped. This should be a uint32,
62 or equivalent.
63 \return The swapped value.
64*/
65static inline uint32_t arch_swap32(uint32_t x) {
66 return __builtin_bswap32(x);
67}
68
69/** \brief Convert network-to-host short.
70
71 This macro converts a network byte order (big endian) value to the host's
72 native byte order. On a little endian system (like the Dreamcast), this
73 should just call arch_swap16(). On a big endian system, this should be a
74 no-op.
75
76 \param x The value to be converted. This should be a uint16,
77 or equivalent.
78 \return The converted value.
79*/
80static inline uint16_t arch_ntohs(uint16_t x) {
81 return __builtin_bswap16(x);
82}
83
84/** \brief Convert network-to-host long.
85
86 This macro converts a network byte order (big endian) value to the host's
87 native byte order. On a little endian system (like the Dreamcast), this
88 should just call arch_swap32(). On a big endian system, this should be a
89 no-op.
90
91 \param x The value to be converted. This should be a uint32,
92 or equivalent.
93 \return The converted value.
94*/
95static inline uint32_t arch_ntohl(uint32_t x) {
96 return __builtin_bswap32(x);
97}
98
99/** \brief Convert host-to-network short.
100
101 This macro converts a value in the host's native byte order to network byte
102 order (big endian). On a little endian system (like the Dreamcast), this
103 should just call arch_swap16(). On a big endian system, this should be a
104 no-op.
105
106 \param x The value to be converted. This should be a uint16,
107 or equivalent.
108 \return The converted value.
109*/
110static inline uint16_t arch_htons(uint16_t x) {
111 return __builtin_bswap16(x);
112}
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*/
125static inline uint32_t arch_htonl(uint32_t x) {
126 return __builtin_bswap32(x);
127}
128
129/** @} */
130
131__END_DECLS
132
133#endif /* !__ARCH_BYTEORDER_H */
static uint32_t arch_ntohl(uint32_t x)
Convert network-to-host long.
Definition byteorder.h:95
static uint32_t arch_htonl(uint32_t x)
Convert host-to-network long.
Definition byteorder.h:125
static uint32_t arch_swap32(uint32_t x)
Swap the byte order of a 32-bit integer.
Definition byteorder.h:65
static uint16_t arch_htons(uint16_t x)
Convert host-to-network short.
Definition byteorder.h:110
static uint16_t arch_ntohs(uint16_t x)
Convert network-to-host short.
Definition byteorder.h:80
static uint16_t arch_swap16(uint16_t x)
Swap the byte order of a 16-bit integer.
Definition byteorder.h:52