1 /* Interface to byteswapping functions.
2    Copyright (C) 2006-2020 Free Software Foundation, Inc.
3 
4    This file is part of libctf.
5 
6    libctf is free software; you can redistribute it and/or modify it under
7    the terms of the GNU General Public License as published by the Free
8    Software Foundation; either version 3, or (at your option) any later
9    version.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14    See the GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; see the file COPYING.  If not see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef _CTF_SWAP_H
21 #define _CTF_SWAP_H
22 
23 #include "config.h"
24 #include <stdint.h>
25 
26 #ifdef HAVE_BYTESWAP_H
27 #include <byteswap.h>
28 #else
29 
30 /* Provide our own versions of the byteswap functions.  */
31 static inline uint16_t
32 bswap_16 (uint16_t v)
33 {
34   return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
35 }
36 
37 static inline uint32_t
38 bswap_32 (uint32_t v)
39 {
40   return (  ((v & 0xff000000) >> 24)
41 	  | ((v & 0x00ff0000) >>  8)
42 	  | ((v & 0x0000ff00) <<  8)
43 	  | ((v & 0x000000ff) << 24));
44 }
45 
46 inline uint64_t
47 bswap_identity_64 (uint64_t v)
48 {
49   return v;
50 }
51 
52 static inline uint64_t
53 bswap_64 (uint64_t v)
54 {
55   return (  ((v & 0xff00000000000000ULL) >> 56)
56 	  | ((v & 0x00ff000000000000ULL) >> 40)
57 	  | ((v & 0x0000ff0000000000ULL) >> 24)
58 	  | ((v & 0x000000ff00000000ULL) >>  8)
59 	  | ((v & 0x00000000ff000000ULL) <<  8)
60 	  | ((v & 0x0000000000ff0000ULL) << 24)
61 	  | ((v & 0x000000000000ff00ULL) << 40)
62 	  | ((v & 0x00000000000000ffULL) << 56));
63 }
64 #endif /* !defined(HAVE_BYTESWAP_H) */
65 
66 #endif /* !defined(_CTF_SWAP_H) */
67