File: swap.h

package info (click to toggle)
binutils-djgpp 2.35.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 321,256 kB
  • sloc: ansic: 1,162,947; asm: 671,219; cpp: 134,012; exp: 70,783; makefile: 55,860; sh: 22,254; yacc: 14,459; lisp: 13,806; perl: 2,008; lex: 1,649; pascal: 307; sed: 195; awk: 25
file content (122 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Interface to byteswapping functions.
   Copyright (C) 2006-2020 Free Software Foundation, Inc.

   This file is part of libctf.

   libctf is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not see
   <http://www.gnu.org/licenses/>.  */

#ifndef _CTF_SWAP_H
#define _CTF_SWAP_H

#include "config.h"
#include <stdint.h>

#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#endif /* defined(HAVE_BYTESWAP_H) */

#ifndef __DJGPP__
/* Provide our own versions of the byteswap functions.  */

#if !HAVE_DECL_BSWAP_16
static inline uint16_t
bswap_16 (uint16_t v)
{
  return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
}
#endif /* !HAVE_DECL_BSWAP16 */

#if !HAVE_DECL_BSWAP_32
static inline uint32_t
bswap_32 (uint32_t v)
{
  return (  ((v & 0xff000000) >> 24)
	  | ((v & 0x00ff0000) >>  8)
	  | ((v & 0x0000ff00) <<  8)
	  | ((v & 0x000000ff) << 24));
}
#endif /* !HAVE_DECL_BSWAP32 */

#if !HAVE_DECL_BSWAP_64
static inline uint64_t
bswap_64 (uint64_t v)
{
  return (  ((v & 0xff00000000000000ULL) >> 56)
	  | ((v & 0x00ff000000000000ULL) >> 40)
	  | ((v & 0x0000ff0000000000ULL) >> 24)
	  | ((v & 0x000000ff00000000ULL) >>  8)
	  | ((v & 0x00000000ff000000ULL) <<  8)
	  | ((v & 0x0000000000ff0000ULL) << 24)
	  | ((v & 0x000000000000ff00ULL) << 40)
	  | ((v & 0x00000000000000ffULL) << 56));
}
#endif /* !HAVE_DECL_BSWAP64 */
#else  /* __DJGPP__ */

/*  The inline functions make trouble with diferent versions of gcc thus use macros.  */
# if defined (__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
#  define __gnuc_extension__  __extension__
# else
#  define __gnuc_extension__
# endif

# define bswap_identity_64(v)                                      \
  (__gnuc_extension__                                              \
    ({                                                             \
       uint64_t value = (v);                                       \
       value;                                                      \
    })                                                             \
  )

# define bswap_16(v)                                               \
  (__gnuc_extension__                                              \
    ({                                                             \
       uint16_t _v = (v);                                          \
       uint16_t value = (  ((_v >> 8) & 0xFF)                      \
                         | ((_v & 0xFF) << 8));                    \
       value;                                                      \
    })                                                             \
  )

# define bswap_32(v)                                               \
  (__gnuc_extension__                                              \
    ({                                                             \
       uint32_t _v = (v);                                          \
       uint32_t value = (  ((_v & 0xFF000000) >> 24)               \
                         | ((_v & 0x00FF0000) >>  8)               \
                         | ((_v & 0x0000FF00) <<  8)               \
                         | ((_v & 0x000000FF) << 24));             \
       value;                                                      \
    })                                                             \
  )

# define bswap_64(v)                                               \
  (__gnuc_extension__                                              \
    ({                                                             \
       uint64_t _v = (v);                                          \
       uint64_t value = (  ((_v & 0xFF00000000000000ULL) >> 56)    \
                         | ((_v & 0x00FF000000000000ULL) >> 40)    \
                         | ((_v & 0x0000FF0000000000ULL) >> 24)    \
                         | ((_v & 0x000000FF00000000ULL) >>  8)    \
                         | ((_v & 0x00000000FF000000ULL) <<  8)    \
                         | ((_v & 0x0000000000FF0000ULL) << 24)    \
                         | ((_v & 0x000000000000FF00ULL) << 40)    \
                         | ((_v & 0x00000000000000FFULL) << 56));  \
       value;                                                      \
    })                                                             \
  )
#endif /* __DJGPP__ */

#endif /* !defined(_CTF_SWAP_H) */