Visual Servoing Platform version 3.5.0
vpEndian.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 * 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)
50{
51 return (((val >> 8) & 0x00FF) | ((val << 8) & 0xFF00));
52}
53
58uint32_t swap32bits(uint32_t val)
59{
60 return (((val >> 24) & 0x000000FF) | ((val >> 8) & 0x0000FF00) | ((val << 8) & 0x00FF0000) |
61 ((val << 24) & 0xFF000000));
62}
63
68float swapFloat(float f)
69{
70 union {
71 float f;
72 unsigned char b[4];
73 } dat1, dat2;
74
75 dat1.f = f;
76 dat2.b[0] = dat1.b[3];
77 dat2.b[1] = dat1.b[2];
78 dat2.b[2] = dat1.b[1];
79 dat2.b[3] = dat1.b[0];
80 return dat2.f;
81}
82
87double swapDouble(double d)
88{
89 union {
90 double d;
91 unsigned char b[8];
92 } dat1, dat2;
93
94 dat1.d = d;
95 dat2.b[0] = dat1.b[7];
96 dat2.b[1] = dat1.b[6];
97 dat2.b[2] = dat1.b[5];
98 dat2.b[3] = dat1.b[4];
99 dat2.b[4] = dat1.b[3];
100 dat2.b[5] = dat1.b[2];
101 dat2.b[6] = dat1.b[1];
102 dat2.b[7] = dat1.b[0];
103 return dat2.d;
104}
105
111uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char * const ptr)
112{
113#ifdef VISP_LITTLE_ENDIAN
114 return *reinterpret_cast<uint16_t *>(ptr);
115#elif defined(VISP_BIG_ENDIAN)
116 return swap16bits(*reinterpret_cast<uint16_t *>(ptr));
117#else
118 throw std::runtime_error("Not supported endianness for correct custom reinterpret_cast() function.");
119#endif
120}
121}
VISP_EXPORT float swapFloat(float f)
Definition: vpEndian.cpp:68
VISP_EXPORT uint32_t swap32bits(uint32_t val)
Definition: vpEndian.cpp:58
VISP_EXPORT uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
Definition: vpEndian.cpp:111
VISP_EXPORT double swapDouble(double d)
Definition: vpEndian.cpp:87
VISP_EXPORT uint16_t swap16bits(uint16_t val)
Definition: vpEndian.cpp:49