Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
vpEndian.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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 * Functions for correct endianness handling.
33 *
34*****************************************************************************/
35
40#include <stdexcept>
41#include <visp3/core/vpEndian.h>
42
43namespace vpEndian
44{
49uint16_t swap16bits(uint16_t val) { return (((val >> 8) & 0x00FF) | ((val << 8) & 0xFF00)); }
50
55uint32_t swap32bits(uint32_t val)
56{
57 return (((val >> 24) & 0x000000FF) | ((val >> 8) & 0x0000FF00) | ((val << 8) & 0x00FF0000) |
58 ((val << 24) & 0xFF000000));
59}
60
65float swapFloat(float f)
66{
67 union {
68 float f;
69 unsigned char b[4];
70 } dat1, dat2;
71
72 dat1.f = f;
73 dat2.b[0] = dat1.b[3];
74 dat2.b[1] = dat1.b[2];
75 dat2.b[2] = dat1.b[1];
76 dat2.b[3] = dat1.b[0];
77 return dat2.f;
78}
79
84double swapDouble(double d)
85{
86 union {
87 double d;
88 unsigned char b[8];
89 } dat1, dat2;
90
91 dat1.d = d;
92 dat2.b[0] = dat1.b[7];
93 dat2.b[1] = dat1.b[6];
94 dat2.b[2] = dat1.b[5];
95 dat2.b[3] = dat1.b[4];
96 dat2.b[4] = dat1.b[3];
97 dat2.b[5] = dat1.b[2];
98 dat2.b[6] = dat1.b[1];
99 dat2.b[7] = dat1.b[0];
100 return dat2.d;
101}
102
108uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
109{
110#ifdef VISP_LITTLE_ENDIAN
111 return *reinterpret_cast<uint16_t *>(ptr);
112#elif defined(VISP_BIG_ENDIAN)
113 return swap16bits(*reinterpret_cast<uint16_t *>(ptr));
114#else
115 throw std::runtime_error("Not supported endianness for correct custom reinterpret_cast() function.");
116#endif
117}
118} // namespace vpEndian
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