17d62b00eSchristos /* Interface to byteswapping functions. 2*6881a400Schristos Copyright (C) 2006-2022 Free Software Foundation, Inc. 37d62b00eSchristos 47d62b00eSchristos This file is part of libctf. 57d62b00eSchristos 67d62b00eSchristos libctf is free software; you can redistribute it and/or modify it under 77d62b00eSchristos the terms of the GNU General Public License as published by the Free 87d62b00eSchristos Software Foundation; either version 3, or (at your option) any later 97d62b00eSchristos version. 107d62b00eSchristos 117d62b00eSchristos This program is distributed in the hope that it will be useful, but 127d62b00eSchristos WITHOUT ANY WARRANTY; without even the implied warranty of 137d62b00eSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 147d62b00eSchristos See the GNU General Public License for more details. 157d62b00eSchristos 167d62b00eSchristos You should have received a copy of the GNU General Public License 177d62b00eSchristos along with this program; see the file COPYING. If not see 187d62b00eSchristos <http://www.gnu.org/licenses/>. */ 197d62b00eSchristos 207d62b00eSchristos #ifndef _CTF_SWAP_H 217d62b00eSchristos #define _CTF_SWAP_H 227d62b00eSchristos 237d62b00eSchristos #include "config.h" 247d62b00eSchristos #include <stdint.h> 25*6881a400Schristos #include <assert.h> 267d62b00eSchristos 277d62b00eSchristos #ifdef HAVE_BYTESWAP_H 287d62b00eSchristos #include <byteswap.h> 297d62b00eSchristos #endif /* defined(HAVE_BYTESWAP_H) */ 307d62b00eSchristos 317d62b00eSchristos /* Provide our own versions of the byteswap functions. */ 327d62b00eSchristos 337d62b00eSchristos #if !HAVE_DECL_BSWAP_16 347d62b00eSchristos static inline uint16_t 357d62b00eSchristos bswap_16 (uint16_t v) 367d62b00eSchristos { 377d62b00eSchristos return ((v >> 8) & 0xff) | ((v & 0xff) << 8); 387d62b00eSchristos } 397d62b00eSchristos #endif /* !HAVE_DECL_BSWAP16 */ 407d62b00eSchristos 417d62b00eSchristos #if !HAVE_DECL_BSWAP_32 427d62b00eSchristos static inline uint32_t 437d62b00eSchristos bswap_32 (uint32_t v) 447d62b00eSchristos { 457d62b00eSchristos return ( ((v & 0xff000000) >> 24) 467d62b00eSchristos | ((v & 0x00ff0000) >> 8) 477d62b00eSchristos | ((v & 0x0000ff00) << 8) 487d62b00eSchristos | ((v & 0x000000ff) << 24)); 497d62b00eSchristos } 507d62b00eSchristos #endif /* !HAVE_DECL_BSWAP32 */ 517d62b00eSchristos 527d62b00eSchristos #if !HAVE_DECL_BSWAP_64 537d62b00eSchristos static inline uint64_t 547d62b00eSchristos bswap_64 (uint64_t v) 557d62b00eSchristos { 567d62b00eSchristos return ( ((v & 0xff00000000000000ULL) >> 56) 577d62b00eSchristos | ((v & 0x00ff000000000000ULL) >> 40) 587d62b00eSchristos | ((v & 0x0000ff0000000000ULL) >> 24) 597d62b00eSchristos | ((v & 0x000000ff00000000ULL) >> 8) 607d62b00eSchristos | ((v & 0x00000000ff000000ULL) << 8) 617d62b00eSchristos | ((v & 0x0000000000ff0000ULL) << 24) 627d62b00eSchristos | ((v & 0x000000000000ff00ULL) << 40) 637d62b00eSchristos | ((v & 0x00000000000000ffULL) << 56)); 647d62b00eSchristos } 657d62b00eSchristos #endif /* !HAVE_DECL_BSWAP64 */ 667d62b00eSchristos 67*6881a400Schristos /* < C11? define away static assertions. */ 68*6881a400Schristos 69*6881a400Schristos #if !defined (__STDC_VERSION__) || __STDC_VERSION__ < 201112L 70*6881a400Schristos #define _Static_assert(cond, err) 71*6881a400Schristos #endif 72*6881a400Schristos 73*6881a400Schristos /* Swap the endianness of something. */ 74*6881a400Schristos 75*6881a400Schristos #define swap_thing(x) \ 76*6881a400Schristos do \ 77*6881a400Schristos { \ 78*6881a400Schristos _Static_assert (sizeof (x) == 1 || (sizeof (x) % 2 == 0 \ 79*6881a400Schristos && sizeof (x) <= 8), \ 80*6881a400Schristos "Invalid size, update endianness code"); \ 81*6881a400Schristos switch (sizeof (x)) { \ 82*6881a400Schristos case 2: x = bswap_16 (x); break; \ 83*6881a400Schristos case 4: x = bswap_32 (x); break; \ 84*6881a400Schristos case 8: x = bswap_64 (x); break; \ 85*6881a400Schristos case 1: /* Nothing needs doing */ \ 86*6881a400Schristos break; \ 87*6881a400Schristos } \ 88*6881a400Schristos } \ 89*6881a400Schristos while (0); 90*6881a400Schristos 91*6881a400Schristos 927d62b00eSchristos #endif /* !defined(_CTF_SWAP_H) */ 93