endian.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Description
27  Help with architecture-specific aspects.
28 
29  Defines WM_BIG_ENDIAN or WM_LITTLE_ENDIAN as well as providing a
30  few runtime methods. Primarily used as a namespace, but defined as
31  a class to allow pTraits specialization.
32 
33 SourceFiles
34  endianI.H
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_endian_H
39 #define Foam_endian_H
40 
41 #include <cstdint>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 #ifdef __BYTE_ORDER__
46  // Clang, Gcc, Icc, Pgi
47  #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
48  #define WM_LITTLE_ENDIAN
49  #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
50  #define WM_BIG_ENDIAN
51  #else
52  #error "__BYTE_ORDER__ is not BIG or LITTLE endian"
53  #endif
54 #endif
55 
56 // Could downgrade to a warning, but then user always needs runtime check.
57 #if !defined(WM_BIG_ENDIAN) && !defined(WM_LITTLE_ENDIAN)
58  #error "Cannot determine BIG or LITTLE endian."
59  #error "Please add to compilation options"
60 #endif
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 /*---------------------------------------------------------------------------*\
68  Class endian Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class endian
72 {
73 public:
74 
75  // Public Methods
76 
77  //- Runtime check for big endian.
78  inline static bool isBig() noexcept;
79 
80  //- Runtime check for little endian.
81  inline static bool isLittle() noexcept;
82 
83  //- Byte endian swapping for 32-bits
84  inline static uint32_t swap32(uint32_t);
85 
86  //- Byte endian swapping for 64-bits
87  inline static uint64_t swap64(uint64_t);
88 };
89 
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 } // End namespace Foam
94 
95 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
96 
97 #include "endianI.H"
98 
99 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
100 
101 #endif
102 
103 // ************************************************************************* //
static uint32_t swap32(uint32_t)
Byte endian swapping for 32-bits.
Definition: endianI.H:54
const direction noexcept
Definition: Scalar.H:258
static bool isLittle() noexcept
Runtime check for little endian.
Definition: endianI.H:45
static bool isBig() noexcept
Runtime check for big endian.
Definition: endianI.H:36
static uint64_t swap64(uint64_t)
Byte endian swapping for 64-bits.
Definition: endianI.H:76
Namespace for OpenFOAM.