1 /*
2  * Copyright (c) 2012 Paulo Alcantara <pcacjr@zytor.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write the Free Software Foundation,
15  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  */
17 
18 #ifndef MISC_H_
19 #define MISC_H_
20 
21 /* Return a 64-bit litte-endian value from a given 64-bit big-endian one */
22 static inline uint64_t be64_to_cpu(uint64_t val)
23 {
24     return (uint64_t)((((uint64_t)val & (uint64_t)0x00000000000000ffULL) << 56) |
25 		      (((uint64_t)val & (uint64_t)0x000000000000ff00ULL) << 40) |
26 		      (((uint64_t)val & (uint64_t)0x0000000000ff0000ULL) << 24) |
27 		      (((uint64_t)val & (uint64_t)0x00000000ff000000ULL) <<  8) |
28 		      (((uint64_t)val & (uint64_t)0x000000ff00000000ULL) >>  8) |
29 		      (((uint64_t)val & (uint64_t)0x0000ff0000000000ULL) >> 24) |
30 		      (((uint64_t)val & (uint64_t)0x00ff000000000000ULL) >> 40) |
31 		      (((uint64_t)val & (uint64_t)0xff00000000000000ULL) >> 56));
32 }
33 
34 /* Return a 32-bit litte-endian value from a given 32-bit big-endian one */
35 static inline uint32_t be32_to_cpu(uint32_t val)
36 {
37     return (uint32_t)((((uint32_t)val & (uint32_t)0x000000ffUL) << 24) |
38 		      (((uint32_t)val & (uint32_t)0x0000ff00UL) <<  8) |
39 		      (((uint32_t)val & (uint32_t)0x00ff0000UL) >>  8) |
40 		      (((uint32_t)val & (uint32_t)0xff000000UL) >> 24));
41 }
42 
43 /* Return a 16-bit litte-endian value from a given 16-bit big-endian one */
44 static inline uint16_t be16_to_cpu(uint16_t val)
45 {
46     return (uint16_t)((((uint16_t)val & (uint16_t)0x00ffU) << 8) |
47 		      (((uint16_t)val & (uint16_t)0xff00U) >> 8));
48 }
49 
50 #endif /* MISC_H_ */
51