aimsdata  4.7.0
Neuroimaging data handling
byteswap.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_IO_BYTESWAP_H
36 #define AIMS_IO_BYTESWAP_H
37 
39 #include <aims/def/assert.h>
40 
41 namespace aims
42 {
43 
44  template <typename T> inline AIMSDATA_API T byteswap( const T & x )
45  {
46  ASSERT( sizeof( T ) == 4 || sizeof( T ) == 2 || sizeof( T ) == 8 );
47  if( sizeof( T ) == 4 )
48  return byteswap32( x );
49  else if( sizeof( T ) == 2 )
50  return byteswap16( x );
51  else
52  return byteswap64( x );
53  }
54 
55  template<typename T> inline AIMSDATA_API T byteswap16( const T & x )
56  {
57  unsigned char *tmp = (unsigned char *) &x;
58  union {
59  unsigned short t2;
60  T t1;
61  } t;
62  *( (unsigned char *) &t.t2) = *(tmp+1);
63  *( ( (unsigned char *) &t.t2) + 1 ) = *tmp;
64  return t.t1;
65  }
66 
67  template<typename T> inline AIMSDATA_API T byteswap32( const T & x )
68  {
69  unsigned char *tmp = (unsigned char *) &x;
70  union {
71  unsigned t2;
72  T t1;
73  } t;
74  *( (unsigned char *) &t.t2) = *(tmp+3);
75  *( ( (unsigned char *) &t.t2) + 1 ) = *(tmp+2);
76  *( ( (unsigned char *) &t.t2) + 2 ) = *(tmp+1);
77  *( ( (unsigned char *) &t.t2) + 3 ) = *tmp;
78  return t.t1;
79  }
80 
81  template<typename T> inline AIMSDATA_API T byteswap64( const T & x )
82  {
83  unsigned char *tmp = (unsigned char *) &x;
84  union {
85  double t2;
86  T t1;
87  } t;
88  *( (unsigned char *) &t.t2) = *(tmp+7);
89  *( ( (unsigned char *) &t.t2) + 1 ) = *(tmp+6);
90  *( ( (unsigned char *) &t.t2) + 2 ) = *(tmp+5);
91  *( ( (unsigned char *) &t.t2) + 3 ) = *(tmp+4);
92  *( ( (unsigned char *) &t.t2) + 4 ) = *(tmp+3);
93  *( ( (unsigned char *) &t.t2) + 5 ) = *(tmp+2);
94  *( ( (unsigned char *) &t.t2) + 6 ) = *(tmp+1);
95  *( ( (unsigned char *) &t.t2) + 7 ) = *tmp;
96  return t.t1;
97  }
98 
99 
100  // specializations
101 
102  inline AIMSDATA_API short byteswap( const short & x )
103  {
104  return( byteswap16( x ) );
105  }
106 
107  inline AIMSDATA_API unsigned short byteswap( const unsigned short & x )
108  {
109  return( byteswap16( x ) );
110  }
111 
112  inline AIMSDATA_API int byteswap( const int & x )
113  {
114  return( byteswap32( x ) );
115  }
116 
117  inline AIMSDATA_API unsigned int byteswap( unsigned int x )
118  {
119  return( byteswap32( x ) );
120  }
121 
122  inline AIMSDATA_API float byteswap( const float & x )
123  {
124  return( byteswap32( x ) );
125  }
126 
127 }
128 
129 
130 #endif
131 
#define AIMSDATA_API
The class for EcatSino data write operation.
Definition: border.h:42
AIMSDATA_API T byteswap32(const T &x)
Definition: byteswap.h:67
AIMSDATA_API T byteswap64(const T &x)
Definition: byteswap.h:81
AIMSDATA_API T byteswap(const T &x)
Definition: byteswap.h:44
#define ASSERT(EX)
AIMSDATA_API T byteswap16(const T &x)
Definition: byteswap.h:55