Drop BeRTOS SD and FAT modules
[rmslog.git] / FAT16 / byteordering.h
1
2 /*
3  * Copyright (c) 2006-2009 by Roland Riegel <feedback@roland-riegel.de>
4  *
5  * This file is free software; you can redistribute it and/or modify
6  * it under the terms of either the GNU General Public License version 2
7  * or the GNU Lesser General Public License version 2.1, both as
8  * published by the Free Software Foundation.
9  */
10
11 #ifndef BYTEORDERING_H
12 #define BYTEORDERING_H
13
14 #include <stdint.h>
15
16 #ifdef __cplusplus
17 extern "C"
18 {
19 #endif
20
21 /**
22  * \addtogroup byteordering
23  *
24  * @{
25  */
26 /**
27  * \file
28  * Byte-order handling header (license: GPLv2 or LGPLv2.1)
29  *
30  * \author Roland Riegel
31  */
32
33 /**
34  * \def HTOL16(val)
35  *
36  * Converts a 16-bit integer to little-endian byte order.
37  *
38  * Use this macro for compile time constants only. For variable values
39  * use the function htol16() instead. This saves code size.
40  *
41  * \param[in] val A 16-bit integer in host byte order.
42  * \returns The given 16-bit integer converted to little-endian byte order.
43  */
44 /**
45  * \def HTOL32(val)
46  *
47  * Converts a 32-bit integer to little-endian byte order.
48  *
49  * Use this macro for compile time constants only. For variable values
50  * use the function htol32() instead. This saves code size.
51  *
52  * \param[in] val A 32-bit integer in host byte order.
53  * \returns The given 32-bit integer converted to little-endian byte order.
54  */
55
56 #if __AVR__
57 #define HTOL16(val) (val)
58 #define HTOL32(val) (val)
59 #elif defined(BIG_ENDIAN)
60 #define HTOL16(val) ((((uint16_t) (val)) << 8) | \
61                      (((uint16_t) (val)) >> 8)   \
62                     )
63 #define HTOL32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \
64                      ((((uint32_t) (val)) & 0x0000ff00) <<  8) | \
65                      ((((uint32_t) (val)) & 0x00ff0000) >>  8) | \
66                      ((((uint32_t) (val)) & 0xff000000) >> 24)   \
67                     )
68 #else
69 #error "Endianess undefined! Please define LITTLE_ENDIAN=1 or BIG_ENDIAN=1."
70 #endif
71
72 uint16_t htol16(uint16_t h);
73 uint32_t htol32(uint32_t h);
74
75 /**
76  * Converts a 16-bit integer to host byte order.
77  *
78  * Use this macro for compile time constants only. For variable values
79  * use the function ltoh16() instead. This saves code size.
80  *
81  * \param[in] val A 16-bit integer in little-endian byte order.
82  * \returns The given 16-bit integer converted to host byte order.
83  */
84 #define LTOH16(val) HTOL16(val)
85
86 /**
87  * Converts a 32-bit integer to host byte order.
88  *
89  * Use this macro for compile time constants only. For variable values
90  * use the function ltoh32() instead. This saves code size.
91  *
92  * \param[in] val A 32-bit integer in little-endian byte order.
93  * \returns The given 32-bit integer converted to host byte order.
94  */
95 #define LTOH32(val) HTOL32(val)
96
97 /**
98  * Converts a 16-bit integer to host byte order.
99  *
100  * Use this function on variable values instead of the
101  * macro LTOH16(). This saves code size.
102  *
103  * \param[in] l A 16-bit integer in little-endian byte order.
104  * \returns The given 16-bit integer converted to host byte order.
105  */
106 #define ltoh16(l) htol16(l)
107
108 /**
109  * Converts a 32-bit integer to host byte order.
110  *
111  * Use this function on variable values instead of the
112  * macro LTOH32(). This saves code size.
113  *
114  * \param[in] l A 32-bit integer in little-endian byte order.
115  * \returns The given 32-bit integer converted to host byte order.
116  */
117 #define ltoh32(l) htol32(l)
118
119
120 /**
121  * @}
122  */
123
124 #if __AVR__
125 #define htol16(h) (h)
126 #define htol32(h) (h)
127 #else
128 uint16_t htol16(uint16_t h);
129 uint32_t htol32(uint32_t h);
130 #endif
131
132 #ifdef __cplusplus
133 }
134 #endif
135
136 #endif
137