Logo ROOT   6.18/05
Reference Guide
Byteswap.h
Go to the documentation of this file.
1/* @(#)root/base:$Id$ */
2
3/*************************************************************************
4 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_Byteswap
12#define ROOT_Byteswap
13
14/* Originally (mid-1990s), this file contained copy/pasted assembler from RH6.0's
15 * version of <bits/byteswap.h>. Hence, we keep a copy of the FSF copyright below.
16 * I believe all the original code has been excised, perhaps with exception of the
17 * R__bswap_constant_* functions. To be on the safe side, we are keeping the
18 * copyright below.
19 * -- Brian Bockelman, August 2018
20 */
21
22/* Copyright (C) 1997 Free Software Foundation, Inc.
23 This file is part of the GNU C Library.
24
25 The GNU C Library is free software; you can redistribute it and/or
26 modify it under the terms of the GNU Library General Public License as
27 published by the Free Software Foundation; either version 2 of the
28 License, or (at your option) any later version.
29
30 The GNU C Library is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 Library General Public License for more details.
34
35 You should have received a copy of the GNU Library General Public
36 License along with the GNU C Library; see the file COPYING.LIB. If not,
37 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
38 Boston, MA 02111-1307, USA. */
39
40#if (defined(__linux) || defined(__APPLE__)) && \
41 (defined(__i386__) || defined(__x86_64__)) && \
42 (defined(__GNUC__))
43#ifndef R__USEASMSWAP
44#define R__USEASMSWAP
45#endif
46#endif
47
48/* Swap bytes in 16 bit value. */
49#define R__bswap_constant_16(x) \
50 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
51
52#if defined R__USEASMSWAP
53# define R__bswap_16(x) __builtin_bswap16(x)
54#else
55# define R__bswap_16(x) R__bswap_constant_16 (x)
56#endif
57
58
59/* Swap bytes in 32 bit value. */
60#define R__bswap_constant_32(x) \
61 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
62 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
63
64#if defined R__USEASMSWAP
65# define R__bswap_32(x) __builtin_bswap32(x)
66#else
67# define R__bswap_32(x) R__bswap_constant_32 (x)
68#endif
69
70
71/* Return a value with all bytes in the 16 bit argument swapped. */
72#define Rbswap_16(x) R__bswap_16 (x)
73
74/* Return a value with all bytes in the 32 bit argument swapped. */
75#define Rbswap_32(x) R__bswap_32 (x)
76
77/* Return a value with all bytes in the 64 bit argument swapped. */
78
79/* For reasons that were lost to history, Rbswap_64 used to only
80 * be defined wherever GNUC was available. To simplify the macro
81 * definitions, we extend this to wherever we can use the gcc-like
82 * builtins.
83 * -- Brian Bockelman, August 2018
84 */
85#ifdef R__USEASMSWAP
86# define Rbswap_64(x) __builtin_bswap64(x)
87#endif
88
89#endif /* Byteswap.h */