libcdio  0.83
bytesex.h
Go to the documentation of this file.
1 /*
2  $Id: bytesex.h,v 1.5 2008/03/25 15:59:08 karl Exp $
3 
4  Copyright (C) 2000, 2004 Herbert Valerio Riedel <hvr@gnu.org>
5  Copyright (C) 2005, 2008 Rocky Bernstein <rocky@gnu.org>
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
28 #ifndef __CDIO_BYTESEX_H__
29 #define __CDIO_BYTESEX_H__
30 
31 #include <cdio/types.h>
32 #include <cdio/bytesex_asm.h>
33 #include <cdio/logging.h>
34 
36 #define UINT16_SWAP_LE_BE_C(val) ((uint16_t) ( \
37  (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
38  (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
39 
41 #define UINT32_SWAP_LE_BE_C(val) ((uint32_t) ( \
42  (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
43  (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
44  (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
45  (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
46 
48 #define UINT64_SWAP_LE_BE_C(val) ((uint64_t) ( \
49  (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000000000ff)) << 56) | \
50  (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000000000ff00)) << 40) | \
51  (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000000000ff0000)) << 24) | \
52  (((uint64_t) (val) & (uint64_t) UINT64_C(0x00000000ff000000)) << 8) | \
53  (((uint64_t) (val) & (uint64_t) UINT64_C(0x000000ff00000000)) >> 8) | \
54  (((uint64_t) (val) & (uint64_t) UINT64_C(0x0000ff0000000000)) >> 24) | \
55  (((uint64_t) (val) & (uint64_t) UINT64_C(0x00ff000000000000)) >> 40) | \
56  (((uint64_t) (val) & (uint64_t) UINT64_C(0xff00000000000000)) >> 56)))
57 
58 #ifndef UINT16_SWAP_LE_BE
59 # define UINT16_SWAP_LE_BE UINT16_SWAP_LE_BE_C
60 #endif
61 
62 #ifndef UINT32_SWAP_LE_BE
63 # define UINT32_SWAP_LE_BE UINT32_SWAP_LE_BE_C
64 #endif
65 
66 #ifndef UINT64_SWAP_LE_BE
67 # define UINT64_SWAP_LE_BE UINT64_SWAP_LE_BE_C
68 #endif
69 
70 inline static
71 uint16_t uint16_swap_le_be (const uint16_t val)
72 {
73  return UINT16_SWAP_LE_BE (val);
74 }
75 
76 inline static
77 uint32_t uint32_swap_le_be (const uint32_t val)
78 {
79  return UINT32_SWAP_LE_BE (val);
80 }
81 
82 inline static
83 uint64_t uint64_swap_le_be (const uint64_t val)
84 {
85  return UINT64_SWAP_LE_BE (val);
86 }
87 
88 # define UINT8_TO_BE(val) ((uint8_t) (val))
89 # define UINT8_TO_LE(val) ((uint8_t) (val))
90 #ifdef WORDS_BIGENDIAN
91 # define UINT16_TO_BE(val) ((uint16_t) (val))
92 # define UINT16_TO_LE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
93 
94 # define UINT32_TO_BE(val) ((uint32_t) (val))
95 # define UINT32_TO_LE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
96 
97 # define UINT64_TO_BE(val) ((uint64_t) (val))
98 # define UINT64_TO_LE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
99 #else
100 # define UINT16_TO_BE(val) ((uint16_t) UINT16_SWAP_LE_BE(val))
101 # define UINT16_TO_LE(val) ((uint16_t) (val))
102 
103 # define UINT32_TO_BE(val) ((uint32_t) UINT32_SWAP_LE_BE(val))
104 # define UINT32_TO_LE(val) ((uint32_t) (val))
105 
106 # define UINT64_TO_BE(val) ((uint64_t) UINT64_SWAP_LE_BE(val))
107 # define UINT64_TO_LE(val) ((uint64_t) (val))
108 #endif
109 
111 #define UINT8_FROM_BE(val) (UINT8_TO_BE (val))
112 #define UINT8_FROM_LE(val) (UINT8_TO_LE (val))
113 #define UINT16_FROM_BE(val) (UINT16_TO_BE (val))
114 #define UINT16_FROM_LE(val) (UINT16_TO_LE (val))
115 #define UINT32_FROM_BE(val) (UINT32_TO_BE (val))
116 #define UINT32_FROM_LE(val) (UINT32_TO_LE (val))
117 #define UINT64_FROM_BE(val) (UINT64_TO_BE (val))
118 #define UINT64_FROM_LE(val) (UINT64_TO_LE (val))
119 
121 #define CVT_TO_FUNC(bits) \
122  static inline uint ## bits ## _t \
123  uint ## bits ## _to_be (uint ## bits ## _t val) \
124  { return UINT ## bits ## _TO_BE (val); } \
125  static inline uint ## bits ## _t \
126  uint ## bits ## _to_le (uint ## bits ## _t val) \
127  { return UINT ## bits ## _TO_LE (val); } \
128 
129 CVT_TO_FUNC(8)
130 CVT_TO_FUNC(16)
131 CVT_TO_FUNC(32)
132 CVT_TO_FUNC(64)
133 
134 #undef CVT_TO_FUNC
135 
136 #define uint8_from_be(val) (uint8_to_be (val))
137 #define uint8_from_le(val) (uint8_to_le (val))
138 #define uint16_from_be(val) (uint16_to_be (val))
139 #define uint16_from_le(val) (uint16_to_le (val))
140 #define uint32_from_be(val) (uint32_to_be (val))
141 #define uint32_from_le(val) (uint32_to_le (val))
142 #define uint64_from_be(val) (uint64_to_be (val))
143 #define uint64_from_le(val) (uint64_to_le (val))
144 
148 #define to_711(i) uint8_to_le(i)
149 
151 #define from_711(i) uint8_from_le(i)
152 
154 #define to_721(i) uint16_to_le(i)
155 
157 #define from_721(i) uint16_from_le(i)
158 
160 #define to_722(i) uint16_to_be(i)
161 
163 #define from_722(i) uint16_from_be(i)
164 
166 static inline uint32_t
167 to_723(uint16_t i)
168 {
169  return uint32_swap_le_be(i) | i;
170 }
171 
173 static inline uint16_t
174 from_723 (uint32_t p)
175 {
176  if (uint32_swap_le_be (p) != p)
177  cdio_warn ("from_723: broken byte order");
178 
179  return (0xFFFF & p);
180 }
181 
183 #define to_731(i) uint32_to_le(i)
184 
186 #define from_731(i) uint32_from_le(i)
187 
189 #define to_732(i) uint32_to_be(i)
190 
192 #define from_732(i) uint32_from_be(i)
193 
195 static inline uint64_t
196 to_733(uint32_t i)
197 {
198  return uint64_swap_le_be(i) | i;
199 }
200 
202 static inline uint32_t
203 from_733 (uint64_t p)
204 {
205  if (uint64_swap_le_be (p) != p)
206  cdio_warn ("from_733: broken byte order");
207 
208  return (UINT32_C(0xFFFFFFFF) & p);
209 }
210 
211 #endif /* __CDIO_BYTESEX_H__ */
212 
213 
214 /*
215  * Local variables:
216  * c-file-style: "gnu"
217  * tab-width: 8
218  * indent-tabs-mode: nil
219  * End:
220  */

Generated for libcdio by doxygen 1.8.1.1