From b196c6498d22e47bb9d0da0153068ec54eac7956 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 11:44:33 +0200 Subject: Adding upstream version 1.6.0. Signed-off-by: Daniel Baumann --- mocktracer/src/utility.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 mocktracer/src/utility.cpp (limited to 'mocktracer/src/utility.cpp') diff --git a/mocktracer/src/utility.cpp b/mocktracer/src/utility.cpp new file mode 100644 index 0000000..4a08d82 --- /dev/null +++ b/mocktracer/src/utility.cpp @@ -0,0 +1,57 @@ +#include "utility.h" +#include + +namespace opentracing { +BEGIN_OPENTRACING_ABI_NAMESPACE +namespace mocktracer { + +// Converts swaps the endianness of a number. +// +// Taken from https://stackoverflow.com/a/105339/4447365 +template +static T SwapEndian(T u) { + static_assert(CHAR_BIT == 8, "CHAR_BIT != 8"); + + union { + T u; + unsigned char u8[sizeof(T)]; + } source, dest; + + source.u = u; + + for (size_t k = 0; k < sizeof(T); k++) + dest.u8[k] = source.u8[sizeof(T) - k - 1]; + + return dest.u; +} + +// Determines whether the architecture is big endian. +// +// Taken from https://stackoverflow.com/a/1001373/4447365 +static bool IsBigEndian() { + union { + uint32_t i; + char c[4]; + } bint = {0x01020304}; + + return bint.c[0] == 1; +} + +uint64_t SwapEndianIfBig(uint64_t x) { + if (IsBigEndian()) { + return SwapEndian(x); + } else { + return x; + } +} + +uint32_t SwapEndianIfBig(uint32_t x) { + if (IsBigEndian()) { + return SwapEndian(x); + } else { + return x; + } +} +} // namespace mocktracer +END_OPENTRACING_ABI_NAMESPACE +} // namespace opentracing -- cgit v1.2.3