ProteoWizard
endian.hpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2007 Spielberg Family Center for Applied Proteomics
8// Cedars-Sinai Medical Center, Los Angeles, California 90048
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
24#ifndef _ENDIAN_HPP_
25#define _ENDIAN_HPP_
26
27
28#include "boost/static_assert.hpp"
29
30
31namespace pwiz {
32namespace util {
33
34
35#if defined(__GLIBC__) || defined(__GLIBCXX__)
36#define PWIZ_GCC
37#endif
38
39
40#if defined(_MSC_VER)
41#define PWIZ_MSVC
42#endif
43
44
45#if (defined(PWIZ_GCC) && defined(__BYTE_ORDER) && __BYTE_ORDER==__LITTLE_ENDIAN) || \
46 (defined(__DARWIN_BYTE_ORDER) && __DARWIN_BYTE_ORDER==__DARWIN_LITTLE_ENDIAN) || \
47 (defined(__LITTLE_ENDIAN__)) || \
48 (defined(__MINGW32__)) || \
49 (defined(__i386__)) || \
50 (defined(PWIZ_MSVC))
51#define PWIZ_LITTLE_ENDIAN
52#endif
53
54
55#if (defined(PWIZ_GCC) && defined(__BYTE_ORDER) && __BYTE_ORDER==__BIG_ENDIAN)
56#define PWIZ_BIG_ENDIAN
57#endif
58
59
60#if defined(PWIZ_LITTLE_ENDIAN) && defined(PWIZ_BIG_ENDIAN)
61#error "This isn't happening."
62#endif
63
64
65#if !defined(PWIZ_LITTLE_ENDIAN) && !defined(PWIZ_BIG_ENDIAN)
66#error "Unsupported platform: probably need a platform-specific define above."
67#endif
68
69
70BOOST_STATIC_ASSERT(sizeof(unsigned int) == 4); // 32 bits
71BOOST_STATIC_ASSERT(sizeof(unsigned long long) == 8); // 64 bits
72
73
74inline unsigned int endianize32(unsigned int n)
75{
76 return ((n&0xff)<<24) | ((n&0xff00)<<8) | ((n&0xff0000)>>8) | ((n&0xff000000)>>24);
77}
78
79
80inline unsigned long long endianize64(unsigned long long n)
81{
82 return ((n&0x00000000000000ffll)<<56) |
83 ((n&0x000000000000ff00ll)<<40) |
84 ((n&0x0000000000ff0000ll)<<24) |
85 ((n&0x00000000ff000000ll)<<8) |
86 ((n&0x000000ff00000000ll)>>8) |
87 ((n&0x0000ff0000000000ll)>>24) |
88 ((n&0x00ff000000000000ll)>>40) |
89 ((n&0xff00000000000000ll)>>56);
90}
91
92
93//
94// notes:
95//
96// To dump gcc's defined macros:
97// gcc -dM -E source.cpp
98//
99// glibc defines __BYTE_ORDER in <endian.h>
100//
101
102
103} // namespace util
104} // namespace pwiz
105
106
107#endif // _ENDIAN_HPP_
108
109
unsigned long long endianize64(unsigned long long n)
Definition endian.hpp:80
BOOST_STATIC_ASSERT(sizeof(unsigned int)==4)
unsigned int endianize32(unsigned int n)
Definition endian.hpp:74