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