Skip to content

Commit 6a496b8

Browse files
committed
Fix MVSC vs MinGW differences in byteswap semantics
1 parent 6d311dc commit 6a496b8

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

mock/include/ByteSwap.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
// Cross-platform byte swap utilities
4+
// MSVC uses _byteswap_*, GCC/Clang use __builtin_bswap*
5+
6+
#ifdef _MSC_VER
7+
#include <cstdlib> // for _byteswap_*
8+
9+
inline uint16_t bswap16(uint16_t value) { return _byteswap_ushort(value); }
10+
inline int16_t bswap16(int16_t value) { return static_cast<int16_t>(_byteswap_ushort(static_cast<uint16_t>(value))); }
11+
inline uint32_t bswap32(uint32_t value) { return _byteswap_ulong(value); }
12+
inline int32_t bswap32(int32_t value) { return static_cast<int32_t>(_byteswap_ulong(static_cast<uint32_t>(value))); }
13+
inline uint64_t bswap64(uint64_t value) { return _byteswap_uint64(value); }
14+
inline int64_t bswap64(int64_t value) { return static_cast<int64_t>(_byteswap_uint64(static_cast<uint64_t>(value))); }
15+
16+
#else
17+
// GCC/Clang
18+
19+
inline uint16_t bswap16(uint16_t value) { return __builtin_bswap16(value); }
20+
inline int16_t bswap16(int16_t value) { return static_cast<int16_t>(__builtin_bswap16(static_cast<uint16_t>(value))); }
21+
inline uint32_t bswap32(uint32_t value) { return __builtin_bswap32(value); }
22+
inline int32_t bswap32(int32_t value) { return static_cast<int32_t>(__builtin_bswap32(static_cast<uint32_t>(value))); }
23+
inline uint64_t bswap64(uint64_t value) { return __builtin_bswap64(value); }
24+
inline int64_t bswap64(int64_t value) { return static_cast<int64_t>(__builtin_bswap64(static_cast<uint64_t>(value))); }
25+
26+
#endif

mock/src/DataStreamGenerator.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "DataStreamGenerator.h"
2+
#include "ByteSwap.h"
23
#include <chrono>
34
#include <cstring>
45
#include <iostream>
@@ -166,8 +167,8 @@ void DataStreamGenerator::generateSyntheticData(std::vector<uint8_t>& buffer) {
166167

167168
// Write header (network byte order = big endian)
168169
AmpDataPacketHeader header;
169-
header.ampID = __builtin_bswap64(state.ampId);
170-
header.length = __builtin_bswap64(dataSize);
170+
header.ampID = bswap64(state.ampId);
171+
header.length = bswap64(dataSize);
171172
std::memcpy(buffer.data(), &header, sizeof(header));
172173

173174
// Generate sample packets
@@ -186,8 +187,8 @@ void DataStreamGenerator::generateSyntheticData(std::vector<uint8_t>& buffer) {
186187

187188
// Write header
188189
AmpDataPacketHeader header;
189-
header.ampID = __builtin_bswap64(state.ampId);
190-
header.length = __builtin_bswap64(dataSize);
190+
header.ampID = bswap64(state.ampId);
191+
header.length = bswap64(dataSize);
191192
std::memcpy(buffer.data(), &header, sizeof(header));
192193

193194
// Generate sample packets

mock/src/ECIHandler.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ECIHandler.h"
2+
#include "ByteSwap.h"
23
#include <cstring>
34
#include <iostream>
45

@@ -208,7 +209,7 @@ void ECIHandler::handleNTPReturnSynch(std::shared_ptr<asio::ip::tcp::socket> soc
208209
// Send back 8-byte NTP time (simplified - just use our sync time)
209210
int64_t ntpTime = syncTime_;
210211
if (bigEndian_) {
211-
ntpTime = __builtin_bswap64(ntpTime);
212+
ntpTime = bswap64(ntpTime);
212213
}
213214
asio::write(*socket, asio::buffer(&ntpTime, 8));
214215
}
@@ -286,14 +287,14 @@ bool ECIHandler::readBytes(std::shared_ptr<asio::ip::tcp::socket> socket, void*
286287

287288
int32_t ECIHandler::swapIfNeeded(int32_t value) const {
288289
if (bigEndian_) {
289-
return __builtin_bswap32(value);
290+
return bswap32(value);
290291
}
291292
return value;
292293
}
293294

294295
int16_t ECIHandler::swapIfNeeded(int16_t value) const {
295296
if (bigEndian_) {
296-
return __builtin_bswap16(value);
297+
return bswap16(value);
297298
}
298299
return value;
299300
}

0 commit comments

Comments
 (0)