endian.H 3.28 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
    Copyright (C) 2016-2019 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Description
    Help with architecture-specific aspects.

    Defines WM_BIG_ENDIAN or WM_LITTLE_ENDIAN as well as providing a
30 31
    few runtime methods. Primarily used as a namespace, but defined as
    a class to allow pTraits specialization.
32 33 34 35 36 37

SourceFiles
    endianI.H

\*---------------------------------------------------------------------------*/

38
#ifndef foamEndian_H  // Prefixed with 'foam' to avoid any name clashes
39 40 41 42 43 44
#define foamEndian_H

#include <cstdint>

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

45 46 47
#ifdef __BYTE_ORDER__
    // Clang, Gcc, Icc, Pgi
    #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
48
        #define WM_LITTLE_ENDIAN
49
    #elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
50 51
        #define WM_BIG_ENDIAN
    #else
52
        #error "__BYTE_ORDER__ is not BIG or LITTLE endian"
53 54 55
    #endif
#endif

56
// Could downgrade to a warning, but then user always needs runtime check.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#if !defined(WM_BIG_ENDIAN) && !defined(WM_LITTLE_ENDIAN)
    #error "Cannot determine BIG or LITTLE endian."
    #error "Please add to compilation options"
#endif

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

/*---------------------------------------------------------------------------*\
                           Class endian Declaration
\*---------------------------------------------------------------------------*/

class endian
{
public:

75
    // Public Methods
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

        //- Runtime check for big endian.
        inline static bool isBig();

        //- Runtime check for little endian.
        inline static bool isLittle();

        //- Byte endian swapping for 32-bits
        inline static uint32_t swap32(uint32_t);

        //- Byte endian swapping for 64-bits
        inline static uint64_t swap64(uint64_t);
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#include "endianI.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //