mtd-utils: allow host-tools to compile on Darwin
[openadk.git] / package / mtd-utils / src / include / byteswap.h
blob6f9839f7a17ac51dc78259d170f70caf21aa607c
1 /*
2 * Copyright (c) Bernhard Walle <bernhard@bwalle.de>, 2012
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Comatibility with BSD-like userland.
20 #ifndef BYTESWAP_H_
21 #define BYTESWAP_H_
23 #ifdef __linux__
24 #include_next <byteswap.h>
25 #else
27 #include <stdint.h>
29 static inline uint16_t bswap_16(uint16_t value)
31 return ((value & 0xff00) >> 8) | ((value & 0xff) << 8);
34 static inline uint32_t bswap_32(uint32_t value)
36 return ((value & 0xff000000) >> 24) |
37 ((value & 0x00ff0000) >> 8) |
38 ((value & 0x0000ff00) << 8) |
39 ((value & 0x000000ff) << 24);
42 static inline uint64_t bswap_64(uint64_t value)
44 return ((value & 0xff00000000000000ull) >> 56) |
45 ((value & 0x00ff000000000000ull) >> 40) |
46 ((value & 0x0000ff0000000000ull) >> 24) |
47 ((value & 0x000000ff00000000ull) >> 8) |
48 ((value & 0x00000000ff000000ull) << 8) |
49 ((value & 0x0000000000ff0000ull) << 24) |
50 ((value & 0x000000000000ff00ull) << 40) |
51 ((value & 0x00000000000000ffull) << 56);
54 #endif
56 #endif /* BYTESWAP_H_ */