Logo ROOT   6.14/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 /* Copyright (C) 1997 Free Software Foundation, Inc.
15  This file is part of the GNU C Library.
16 
17  The GNU C Library is free software; you can redistribute it and/or
18  modify it under the terms of the GNU Library General Public License as
19  published by the Free Software Foundation; either version 2 of the
20  License, or (at your option) any later version.
21 
22  The GNU C Library is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  Library General Public License for more details.
26 
27  You should have received a copy of the GNU Library General Public
28  License along with the GNU C Library; see the file COPYING.LIB. If not,
29  write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30  Boston, MA 02111-1307, USA. */
31 
32 #if (defined(__linux) || defined(__APPLE__)) && \
33  (defined(__i386__) || defined(__x86_64__)) && \
34  (defined(__GNUC__) && __GNUC__ >= 2)
35 #ifndef R__USEASMSWAP
36 #define R__USEASMSWAP
37 #endif
38 #endif
39 
40 /* Get the machine specific, optimized definitions. */
41 /* The following is copied from <bits/byteswap.h> (only from RH6.0 and above) */
42 
43 /* Swap bytes in 16 bit value. */
44 #define R__bswap_constant_16(x) \
45  ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
46 
47 #if defined R__USEASMSWAP
48 # define R__bswap_16(x) \
49  (__extension__ \
50  ({ unsigned short int __v; \
51  if (__builtin_constant_p (x)) \
52  __v = R__bswap_constant_16 (x); \
53  else \
54  __asm__ __volatile__ ("rorw $8, %w0" \
55  : "=r" (__v) \
56  : "0" ((unsigned short int) (x)) \
57  : "cc"); \
58  __v; }))
59 #else
60 /* This is better than nothing. */
61 # define R__bswap_16(x) R__bswap_constant_16 (x)
62 #endif
63 
64 
65 /* Swap bytes in 32 bit value. */
66 #define R__bswap_constant_32(x) \
67  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
68  (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
69 
70 #if defined R__USEASMSWAP
71 /* To swap the bytes in a word the i486 processors and up provide the
72  `bswap' opcode. On i386 we have to use three instructions. */
73 # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ && \
74  !defined __pentium4__ && !defined __x86_64__
75 # define R__bswap_32(x) \
76  (__extension__ \
77  ({ unsigned int __v; \
78  if (__builtin_constant_p (x)) \
79  __v = R__bswap_constant_32 (x); \
80  else \
81  __asm__ __volatile__ ("rorw $8, %w0;" \
82  "rorl $16, %0;" \
83  "rorw $8, %w0" \
84  : "=r" (__v) \
85  : "0" ((unsigned int) (x)) \
86  : "cc"); \
87  __v; }))
88 # else
89 # define R__bswap_32(x) \
90  (__extension__ \
91  ({ unsigned int __v; \
92  if (__builtin_constant_p (x)) \
93  __v = R__bswap_constant_32 (x); \
94  else \
95  __asm__ __volatile__ ("bswap %0" \
96  : "=r" (__v) \
97  : "0" ((unsigned int) (x))); \
98  __v; }))
99 # endif
100 #else
101 # define R__bswap_32(x) R__bswap_constant_32 (x)
102 #endif
103 
104 
105 #if defined __GNUC__ && __GNUC__ >= 2
106 /* Swap bytes in 64 bit value. */
107 # define R__bswap_64(x) \
108  (__extension__ \
109  ({ union { __extension__ unsigned long long int __ll; \
110  UInt_t __l[2]; } __w, __r; \
111  __w.__ll = (x); \
112  __r.__l[0] = R__bswap_32 (__w.__l[1]); \
113  __r.__l[1] = R__bswap_32 (__w.__l[0]); \
114  __r.__ll; }))
115 #endif /* bits/byteswap.h */
116 
117 
118 /* The following definitions must all be macros since otherwise some
119  of the possible optimizations are not possible. */
120 
121 /* Return a value with all bytes in the 16 bit argument swapped. */
122 #define Rbswap_16(x) R__bswap_16 (x)
123 
124 /* Return a value with all bytes in the 32 bit argument swapped. */
125 #define Rbswap_32(x) R__bswap_32 (x)
126 
127 #if defined __GNUC__ && __GNUC__ >= 2
128 /* Return a value with all bytes in the 64 bit argument swapped. */
129 # define Rbswap_64(x) R__bswap_64 (x)
130 #endif
131 
132 #endif /* Byteswap.h */