Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpEndian.cpp
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  * Functions for correct endianness handling.
32  *
33 *****************************************************************************/
34 
39 #include <stdexcept>
40 #include <visp3/core/vpEndian.h>
41 
42 BEGIN_VISP_NAMESPACE
43 namespace vpEndian
44 {
49 uint16_t swap16bits(uint16_t val)
50 {
51  const unsigned int magic_8 = 8;
52  const unsigned int magic_0x00FF = 0x00FF;
53  const unsigned int magic_0xFF00 = 0xFF00;
54  return (((val >> magic_8) & magic_0x00FF) | ((val << magic_8) & magic_0xFF00));
55 }
56 
61 uint32_t swap32bits(uint32_t val)
62 {
63  const unsigned int magic_8 = 8;
64  const unsigned int magic_24 = 24;
65  const unsigned int magic_0x000000FF = 0x000000FFU;
66  const unsigned int magic_0x0000FF00 = 0x0000FF00U;
67  const unsigned int magic_0x00FF0000 = 0x00FF0000U;
68  const unsigned int magic_0xFF000000 = 0xFF000000U;
69  return (((val >> magic_24) & magic_0x000000FF) | ((val >> magic_8) & magic_0x0000FF00) | ((val << magic_8) & magic_0x00FF0000) |
70  ((val << magic_24) & magic_0xFF000000));
71 }
72 
77 float swapFloat(float f)
78 {
79  union
80  {
81  float f;
82  unsigned char b[4];
83  } dat1, dat2;
84 
85  const unsigned int index_0 = 0;
86  const unsigned int index_1 = 1;
87  const unsigned int index_2 = 2;
88  const unsigned int index_3 = 3;
89  dat1.f = f;
90  dat2.b[index_0] = dat1.b[index_3];
91  dat2.b[index_1] = dat1.b[index_2];
92  dat2.b[index_2] = dat1.b[index_1];
93  dat2.b[index_3] = dat1.b[index_0];
94  return dat2.f;
95 }
96 
101 double swapDouble(double d)
102 {
103  union
104  {
105  double d;
106  unsigned char b[8];
107  } dat1, dat2;
108 
109  const unsigned int index_0 = 0;
110  const unsigned int index_1 = 1;
111  const unsigned int index_2 = 2;
112  const unsigned int index_3 = 3;
113  const unsigned int index_4 = 4;
114  const unsigned int index_5 = 5;
115  const unsigned int index_6 = 6;
116  const unsigned int index_7 = 7;
117  dat1.d = d;
118  dat2.b[index_0] = dat1.b[index_7];
119  dat2.b[index_1] = dat1.b[index_6];
120  dat2.b[index_2] = dat1.b[index_5];
121  dat2.b[index_3] = dat1.b[index_4];
122  dat2.b[index_4] = dat1.b[index_3];
123  dat2.b[index_5] = dat1.b[index_2];
124  dat2.b[index_6] = dat1.b[index_1];
125  dat2.b[index_7] = dat1.b[index_0];
126  return dat2.d;
127 }
128 
134 uint16_t reinterpret_cast_uchar_to_uint16_LE(unsigned char *const ptr)
135 {
136 #ifdef VISP_LITTLE_ENDIAN
137  return *reinterpret_cast<uint16_t *>(ptr);
138 #elif defined(VISP_BIG_ENDIAN)
139  return swap16bits(*reinterpret_cast<uint16_t *>(ptr));
140 #else
141  throw std::runtime_error("Not supported endianness for correct custom reinterpret_cast() function.");
142 #endif
143 }
144 } // namespace vpEndian
145 END_VISP_NAMESPACE
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