SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Endian.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file Endian.h
33 ///@brief Detect host endianness and provide tools to swap bytes if necessary.
34 ///
35 ///@author
36 /// Kurt Zimmerman
37 /// Department of Computer Science
38 /// University of Utah
39 ///@date January 2001
40 ///
41 
42 #ifndef SCI_Endianness_h
43 #define SCI_Endianness_h
44 
45 #include <sci_defs/config_defs.h>
46 
47 //#if HAVE_INTTYPES_H
48 //# include <inttypes.h>
49 //#endif
50 #ifdef _WIN32
51 typedef signed char int8_t;
52 typedef unsigned char uint8_t;
53 typedef signed short int16_t;
54 typedef unsigned short uint16_t;
55 typedef signed int int32_t;
56 typedef unsigned int uint32_t;
57 typedef signed long long int64_t;
58 typedef unsigned long long uint64_t;
59 #endif
60 
61 #include <string>
62 
64 
65 namespace SCIRun {
66 
67 #define SWAP_2(u2)/* IronDoc macro to swap two byte quantity */ \
68  { unsigned char* _p = reinterpret_cast<unsigned char*>(&(u2)); \
69  unsigned char _c = *_p; *_p = _p[1]; _p[1] = _c; }
70 #define SWAP_4(u4)/* IronDoc macro to swap four byte quantity */ \
71  { unsigned char* _p = reinterpret_cast<unsigned char*>(&(u4)); \
72  unsigned char _c = *_p; *_p = _p[3]; _p[3] = _c; \
73  _c = *++_p; *_p = _p[1]; _p[1] = _c; }
74 #define SWAP_8(u8)/* IronDoc macro to swap eight byte quantity */ \
75  { unsigned char* _p = reinterpret_cast<unsigned char*>(&(u8)); \
76  unsigned char _c = *_p; *_p = _p[7]; _p[7] = _c; \
77  _c = *++_p; *_p = _p[5]; _p[5] = _c; \
78  _c = *++_p; *_p = _p[3]; _p[3] = _c; \
79  _c = *++_p; *_p = _p[1]; _p[1] = _c; }
80 
81 SCISHARE void swapbytes(bool& i);
82 SCISHARE void swapbytes(int8_t& i);
83 SCISHARE void swapbytes(uint8_t& i);
84 SCISHARE void swapbytes(int16_t& i);
85 SCISHARE void swapbytes(uint16_t& i);
86 SCISHARE void swapbytes(int32_t& i);
87 SCISHARE void swapbytes(uint32_t& i);
88 SCISHARE void swapbytes(int64_t& i);
89 SCISHARE void swapbytes(uint64_t& i);
90 SCISHARE void swapbytes(float& i);
91 SCISHARE void swapbytes(double& i);
92 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
93 SCISHARE void swapbytes(Point &i);
94 SCISHARE void swapbytes(Vector &i);
95 #endif
96 
97 SCISHARE bool isBigEndian();
98 
100 
101 SCISHARE std::string endianness();
102 
103 
104 
105 } //end namespace SCIRun
106 #endif
#define SCISHARE
Definition: share.h:39
std::string endianness()
Definition: Endian.cc:80
bool isBigEndian()
Definition: Endian.cc:62
bool isLittleEndian()
Definition: Endian.cc:75
void swapbytes(bool &)
Definition: Endian.cc:34