Cortex  10.0.0-a4
ByteOrder.h
1 //
3 // Copyright (c) 2007-2009, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_BYTEORDER_H
36 #define IE_CORE_BYTEORDER_H
37 
38 #include <stdint.h>
39 
40 #include "boost/static_assert.hpp"
41 
42 #include "OpenEXR/ImfInt64.h"
43 
44 namespace IECore
45 {
46 
47 #if defined(__PPC__) || defined(__ppc__) || defined(__POWERPC__) || defined(__powerpc__)
48  #define IE_CORE_BIG_ENDIAN
49 #else
50  #define IE_CORE_LITTLE_ENDIAN
51 #endif
52 
55 inline bool littleEndian()
56 {
57 #ifdef IE_CORE_LITTLE_ENDIAN
58  return true;
59 #else
60  return false;
61 #endif
62 }
63 
66 inline bool bigEndian()
67 {
68 #ifdef IE_CORE_BIG_ENDIAN
69  return true;
70 #else
71  return false;
72 #endif
73 }
74 
76 template<typename T>
77 inline T reverseBytes( const T &x )
78 {
79  // needs specialising for each type
80  BOOST_STATIC_ASSERT(sizeof(T)==0);
81 }
82 
83 template<>
84 inline char reverseBytes<char>( const char &x )
85 {
86  return x;
87 }
88 
89 template<>
90 inline unsigned char reverseBytes<unsigned char>( const unsigned char &x )
91 {
92  return x;
93 }
94 
95 template<>
96 inline int16_t reverseBytes<int16_t>( const int16_t &x )
97 {
98  return ((x & 255) << 8) |
99  ((x >> 8) & 255 );
100 }
101 
102 template<>
103 inline uint16_t reverseBytes<uint16_t>( const uint16_t &x )
104 {
105  return ((x & 255) << 8) |
106  ((x >> 8) & 255 );
107 }
108 
109 template<>
110 inline int32_t reverseBytes<int32_t>( const int32_t &x )
111 {
112  return ((x & 255) << 24) |
113  (((x >> 8) & 255 ) << 16 ) |
114  (((x >> 16) & 255 ) << 8 ) |
115  ((x >> 24) & 255 );
116 }
117 
118 template<>
119 inline uint32_t reverseBytes<uint32_t>( const uint32_t &x )
120 {
121  return ((x & 255) << 24) |
122  (((x >> 8) & 255 ) << 16 ) |
123  (((x >> 16) & 255 ) << 8 ) |
124  ((x >> 24) & 255 );
125 }
126 
127 template<>
128 inline float reverseBytes<float>( const float &x )
129 {
130  BOOST_STATIC_ASSERT( sizeof(uint32_t) == sizeof(float) );
131  union {
132  uint32_t i;
133  float f;
134  } xx;
135  xx.f = x;
136  xx.i = reverseBytes( xx.i );
137  return xx.f;
138 }
139 
140 template<>
141 inline Imf::Int64 reverseBytes<Imf::Int64>( const Imf::Int64 &x )
142 {
143  return ((x & 255) << 56) |
144  (((x >> 8) & 255) << 48) |
145  (((x >> 16) & 255 ) << 40 ) |
146  (((x >> 24) & 255 ) << 32 ) |
147  (((x >> 32) & 255 ) << 24 ) |
148  (((x >> 40) & 255 ) << 16 ) |
149  (((x >> 48) & 255 ) << 8 ) |
150  ((x >> 56) & 255 );
151 }
152 
153 template<>
154 inline double reverseBytes<double>( const double &x )
155 {
156  BOOST_STATIC_ASSERT( sizeof(Imf::Int64) == sizeof(double) );
157  union {
158  Imf::Int64 i;
159  double d;
160  } xx;
161  xx.d = x;
162  xx.i = reverseBytes<Imf::Int64>(xx.i);
163  return xx.d;
164 }
165 
169 template<typename T>
170 inline T asLittleEndian( const T &x )
171 {
172  if( bigEndian() )
173  {
174  return reverseBytes( x );
175  }
176  else
177  {
178  return x;
179  }
180 }
181 
185 template<typename T>
186 inline T asBigEndian( const T &x )
187 {
188  if( littleEndian() )
189  {
190  return reverseBytes( x );
191  }
192  else
193  {
194  return x;
195  }
196 }
197 
198 }
199 
200 #endif // IE_CORE_BYTEORDER_H
bool littleEndian()
Definition: ByteOrder.h:55
T asLittleEndian(const T &x)
Definition: ByteOrder.h:170
T asBigEndian(const T &x)
Definition: ByteOrder.h:186
T reverseBytes(const T &x)
Returns a copy of x with reversed byte order.
Definition: ByteOrder.h:77
bool bigEndian()
Definition: ByteOrder.h:66
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43