Visual Servoing Platform  version 3.6.1 under development (2024-06-16)
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 
44 namespace vpEndian
45 {
50 uint16_t swap16bits(uint16_t val) { return (((val >> 8) & 0x00FF) | ((val << 8) & 0xFF00)); }
51 
56 uint32_t swap32bits(uint32_t val)
57 {
58  return (((val >> 24) & 0x000000FF) | ((val >> 8) & 0x0000FF00) | ((val << 8) & 0x00FF0000) |
59  ((val << 24) & 0xFF000000));
60 }
61 
66 float swapFloat(float f)
67 {
68  union
69  {
70  float f;
71  unsigned char b[4];
72  } dat1, dat2;
73 
74  dat1.f = f;
75  dat2.b[0] = dat1.b[3];
76  dat2.b[1] = dat1.b[2];
77  dat2.b[2] = dat1.b[1];
78  dat2.b[3] = dat1.b[0];
79  return dat2.f;
80 }
81 
86 double swapDouble(double d)
87 {
88  union
89  {
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 
111 uint16_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 } // namespace vpEndian
VISP_EXPORT float swapFloat(float f)
Definition: vpEndian.cpp:66
VISP_EXPORT uint32_t swap32bits(uint32_t val)
Definition: vpEndian.cpp:56
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:86
VISP_EXPORT uint16_t swap16bits(uint16_t val)
Definition: vpEndian.cpp:50