AGX Dynamics 2.40.0.0
Loading...
Searching...
No Matches
byteswap.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agx/agx.h>
20
21#ifdef _MSC_VER
22#else
23#ifdef __APPLE__
24#include <CoreFoundation/CoreFoundation.h>
25#else //LINUX
26// The byteswap_.* family of macros inject code containing the deprecated 'register' keyword.
28#include <byteswap.h>
29#endif
30#endif
31
32namespace agx
33{
34
43 static void ByteSwapArray( uint8_t* byte_array, size_t len )
44 {
45 size_t i_left = 0; // index for left side of the array
46 size_t i_right = len - 1; // index for right side of the array
47 while (i_left < i_right)
48 {
49 // swap left and right bytes
50 uint8_t left_copy = byte_array[i_left];
51 byte_array[i_left] = byte_array[i_right];
52 byte_array[i_right] = left_copy;
53 i_left++;
54 i_right--;
55 }
56 }
57
58 static inline double ByteSwap64( const double& x )
59 {
60 double val = x;
61 ByteSwapArray((uint8_t*)(&val), sizeof(val));
62 return val;
63 }
64
65 static inline float ByteSwap32( const float& x )
66 {
67 float val = x;
68 ByteSwapArray((uint8_t*)(&val), sizeof(val));
69 return val;
70 }
71
72#ifdef _MSC_VER
73 static inline uint16_t ByteSwap16( const uint16_t& x )
74 {
75 uint16_t result = x;
76 ByteSwapArray((uint8_t*)&result, sizeof(result));
77 return result;
78 }
79
80 static inline int16_t ByteSwap16( const int16_t& x )
81 {
82 int16_t result = x;
83 ByteSwapArray((uint8_t*)&result, sizeof(result));
84 return result;
85 }
86
87 static inline uint32_t ByteSwap32( const uint32_t& x )
88 {
89 uint32_t result = x;
90 ByteSwapArray((uint8_t*) & result, sizeof(result));
91 return result;
92 }
93
94 static inline int32_t ByteSwap32(const int32_t& x)
95 {
96 uint32_t result = x;
97 ByteSwapArray((uint8_t*)&result, sizeof(result));
98 return result;
99 }
100
101 static inline uint64_t ByteSwap64 ( const uint64_t& x )
102 {
103 uint64_t result = x;
104 ByteSwapArray((uint8_t*)&result, sizeof(result));
105 return result;
106 }
107
108 static inline int64_t ByteSwap64(const int64_t& x)
109 {
110 int64_t result = x;
111 ByteSwapArray((uint8_t*)&result, sizeof(result));
112 return result;
113 }
114
115
116#else
117#ifdef __APPLE__
118 static inline uint16_t ByteSwap16( uint16_t x )
119 {
120 return CFSwapInt16(x);
121 }
122
123 static inline int16_t ByteSwap16( int16_t x )
124 {
125 return (int16_t)CFSwapInt16((uint16_t)x);
126 }
127
128 static inline uint32_t ByteSwap32( uint32_t x )
129 {
130 return CFSwapInt32(x);
131 }
132
133 static inline int32_t ByteSwap32( int32_t x )
134 {
135 return (int32_t)CFSwapInt32((uint32_t)x);
136 }
137
138 static inline uint64_t ByteSwap64( uint64_t x )
139 {
140 return CFSwapInt64(x);
141 }
142
143 static inline int64_t ByteSwap64( int64_t x )
144 {
145 return (int64_t)CFSwapInt64((uint64_t)x);
146 }
147
148#else //LINUX
149
150 static inline uint16_t ByteSwap16( uint16_t x )
151 {
152 return bswap_16(x);
153 }
154
155 static inline int16_t ByteSwap16( int16_t x )
156 {
157 return (int16_t)bswap_16((uint16_t)x);
158 }
159
160 static inline uint32_t ByteSwap32( uint32_t x )
161 {
162 return bswap_32(x);
163 }
164
165 static inline int32_t ByteSwap32( int32_t x )
166 {
167 return (int32_t)bswap_32((uint32_t)x);
168 }
169
170 static inline uint64_t ByteSwap64( uint64_t x )
171 {
172 return bswap_64(x);
173 }
174
175 static inline int64_t ByteSwap64( int64_t x )
176 {
177 return (int64_t)bswap_64((uint64_t)x);
178 }
179
180#endif
181#endif
182}
183
184#ifdef __linux__
186#endif
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
static void ByteSwapArray(uint8_t *byte_array, size_t len)
Swap all the bytes in an array to convert from little - endian byte order to big-endian byte order,...
Definition: byteswap.h:43
static double ByteSwap64(const double &x)
Definition: byteswap.h:58
static uint16_t ByteSwap16(uint16_t x)
Definition: byteswap.h:150
static float ByteSwap32(const float &x)
Definition: byteswap.h:65