Visual Servoing Platform  version 3.5.1 under development (2023-03-29)
vpEndian.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2022 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Determine machine endianness and define VISP_LITTLE_ENDIAN, VISP_BIG_ENDIAN and VISP_PDP_ENDIAN macros.
33  *
34  *****************************************************************************/
35 
41 #ifndef vpEndian_h
42 #define vpEndian_h
43 
44 // Visual Studio 2010 or previous is missing inttypes.h
45 #if defined(_MSC_VER) && (_MSC_VER < 1700)
46 typedef unsigned short uint16_t;
47 #else
48 #include <inttypes.h>
49 #endif
50 #include <stdint.h> //for uint32_t related types ; works also with >= VS2010 / _MSC_VER >= 1600
51 #include <visp3/core/vpConfig.h>
52 
53 // Detect endianness of the host machine
54 // Reference: http://www.boost.org/doc/libs/1_36_0/boost/detail/endian.hpp
55 #if defined(__GLIBC__) || (defined(__GNUC__) && !defined(__llvm__) && !defined(__MINGW32__) && \
56  !defined(__FreeBSD__) && defined(__BYTE_ORDER__))
57 #include <endian.h>
58 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
59 #define VISP_LITTLE_ENDIAN
60 #elif (__BYTE_ORDER == __BIG_ENDIAN)
61 #define VISP_BIG_ENDIAN
62 #elif (__BYTE_ORDER == __PDP_ENDIAN)
63 // Currently not supported when reading / writing binary file
64 #define VISP_PDP_ENDIAN
65 //#error PDP endian is not supported. //Uncomment if needed/happens
66 #else
67 #error Unknown machine endianness detected.
68 #endif
69 #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) || defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
70 #define VISP_BIG_ENDIAN
71 #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) || defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
72 #define VISP_LITTLE_ENDIAN
73 #elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || \
74  defined(__hpux) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
75 
76 #define VISP_BIG_ENDIAN
77 #elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || \
78  defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || \
79  defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__ANDROID__)
80 // It appears that all Android systems are little endian.
81 // Refer https://stackoverflow.com/questions/6212951/endianness-of-android-ndk
82 #define VISP_LITTLE_ENDIAN
83 #elif defined(WINRT) // For UWP
84 // Refer
85 // https://social.msdn.microsoft.com/Forums/en-US/04c92ef9-e38e-415f-8958-ec9f7c196fd3/arm-endianess-under-windows-mobile?forum=windowsmobiledev
86 #define VISP_LITTLE_ENDIAN
87 #else
88 #error Cannot detect host machine endianness.
89 #endif
90 
91 namespace vpEndian
92 {
93 VISP_EXPORT uint16_t swap16bits(uint16_t val);
94 
95 VISP_EXPORT uint32_t swap32bits(uint32_t val);
96 
97 VISP_EXPORT float swapFloat(float f);
98 
99 VISP_EXPORT double swapDouble(double d);
100 
101 VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr);
102 } // namespace vpEndian
103 
104 #endif
VISP_EXPORT float swapFloat(float f)
Definition: vpEndian.cpp:65
VISP_EXPORT uint32_t swap32bits(uint32_t val)
Definition: vpEndian.cpp:55
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
Definition: vpEndian.cpp:108
VISP_EXPORT double swapDouble(double d)
Definition: vpEndian.cpp:84
VISP_EXPORT uint16_t swap16bits(uint16_t val)
Definition: vpEndian.cpp:49