]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libctf/swap.h
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / libctf / swap.h
CommitLineData
72f33921 1/* Interface to byteswapping functions.
250d07de 2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
72f33921
NA
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>
53651de8 25#include <assert.h>
72f33921
NA
26
27#ifdef HAVE_BYTESWAP_H
28#include <byteswap.h>
e755667f 29#endif /* defined(HAVE_BYTESWAP_H) */
72f33921
NA
30
31/* Provide our own versions of the byteswap functions. */
e755667f
NA
32
33#if !HAVE_DECL_BSWAP_16
cbbbc402 34static inline uint16_t
a0486bac 35bswap_16 (uint16_t v)
72f33921
NA
36{
37 return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
38}
e755667f 39#endif /* !HAVE_DECL_BSWAP16 */
72f33921 40
e755667f 41#if !HAVE_DECL_BSWAP_32
cbbbc402 42static inline uint32_t
a0486bac 43bswap_32 (uint32_t v)
72f33921
NA
44{
45 return ( ((v & 0xff000000) >> 24)
46 | ((v & 0x00ff0000) >> 8)
47 | ((v & 0x0000ff00) << 8)
48 | ((v & 0x000000ff) << 24));
49}
e755667f 50#endif /* !HAVE_DECL_BSWAP32 */
72f33921 51
e755667f 52#if !HAVE_DECL_BSWAP_64
cbbbc402 53static inline uint64_t
a0486bac 54bswap_64 (uint64_t v)
72f33921
NA
55{
56 return ( ((v & 0xff00000000000000ULL) >> 56)
57 | ((v & 0x00ff000000000000ULL) >> 40)
58 | ((v & 0x0000ff0000000000ULL) >> 24)
59 | ((v & 0x000000ff00000000ULL) >> 8)
60 | ((v & 0x00000000ff000000ULL) << 8)
61 | ((v & 0x0000000000ff0000ULL) << 24)
62 | ((v & 0x000000000000ff00ULL) << 40)
63 | ((v & 0x00000000000000ffULL) << 56));
64}
e755667f 65#endif /* !HAVE_DECL_BSWAP64 */
72f33921 66
53651de8
NA
67/* < C11? define away static assertions. */
68
69#if !defined (__STDC_VERSION__) || __STDC_VERSION__ < 201112L
70#define _Static_assert(cond, err)
71#endif
72
73/* Swap the endianness of something. */
74
75#define swap_thing(x) \
76 do { \
77 _Static_assert (sizeof (x) == 1 || (sizeof (x) % 2 == 0 \
78 && sizeof (x) <= 8), \
79 "Invalid size, update endianness code"); \
80 switch (sizeof (x)) { \
81 case 2: x = bswap_16 (x); break; \
82 case 4: x = bswap_32 (x); break; \
83 case 8: x = bswap_64 (x); break; \
84 case 1: /* Nothing needs doing */ \
85 break; \
86 } \
87 } while (0);
88
89
72f33921 90#endif /* !defined(_CTF_SWAP_H) */