Skip to content

Commit

Permalink
Squashed 'externals/coda-oss/' changes from 8abcb1825..7b90b5a46
Browse files Browse the repository at this point in the history
7b90b5a46 Merge branch 'main' into cpp17
ff4f820ed xml.lite tweaks to support SIDD 3.0 ISM (#672)
b1de8c0e5 std::byte should be a unique type (#671)
c05bf9a02 allow enums to be byte-swapped too
1f9fd88d6 remove spurious
47684c45b byteSwap now uses byte buffers (#670)
cbc659db2 add swapBytes() utility from SIX (#669)
891481b64 simplify byte-swapping (#668)
540ae763e more byteSwap() tweaks
0774c03c4 threaded byteSwap() (#667)

git-subtree-dir: externals/coda-oss
git-subtree-split: 7b90b5a46bad1bb67b9e2cd43a17b73e4c20b390
  • Loading branch information
Dan Smith authored and Dan Smith committed May 30, 2023
1 parent 6490576 commit fe6920a
Show file tree
Hide file tree
Showing 27 changed files with 1,241 additions and 519 deletions.
6 changes: 4 additions & 2 deletions UnitTest/UnitTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<PlatformToolset>v143</PlatformToolset>
<UseOfMfc>false</UseOfMfc>
<CharacterSet>Unicode</CharacterSet>
<EnableASAN>true</EnableASAN>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
Expand All @@ -34,7 +33,6 @@
<WholeProgramOptimization>true</WholeProgramOptimization>
<UseOfMfc>false</UseOfMfc>
<CharacterSet>Unicode</CharacterSet>
<EnableASAN>true</EnableASAN>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -259,6 +257,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\modules\c++\mt\unittests\test_mt_byte_swap.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\modules\c++\mt\unittests\ThreadGroupTest.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
Expand Down
3 changes: 3 additions & 0 deletions UnitTest/UnitTest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<ClCompile Include="..\modules\c++\re\unittests\test_regex.cpp">
<Filter>re</Filter>
</ClCompile>
<ClCompile Include="..\modules\c++\mt\unittests\test_mt_byte_swap.cpp">
<Filter>mt</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand Down
4 changes: 4 additions & 0 deletions UnitTest/mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ TEST_CLASS(work_sharing_balanced_runnable_1d_test){ public:
#include "mt/unittests/work_sharing_balanced_runnable_1d_test.cpp"
};

TEST_CLASS(test_mt_byte_swap){ public:
#include "mt/unittests/test_mt_byte_swap.cpp"
};

}
2 changes: 2 additions & 0 deletions UnitTest/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include <sys/Thread.h>
#include <sys/Mutex.h>
#include <sys/FileFinder.h>
#include <sys/ByteSwap.h>
#include <import/sys.h>
#include <import/math/linear.h>
#include <import/math/poly.h>
Expand All @@ -87,6 +88,7 @@
#include <mt/RequestQueue.h>
#include <mt/ThreadPoolException.h>
#include <mt/GenerationThreadPool.h>
#include <mt/ThreadedByteSwap.h>
#include <import/mt.h>
#include <avx/extractf.h>
#include <logging/ExceptionLogger.h>
Expand Down
6 changes: 4 additions & 2 deletions modules/c++/coda-oss-lite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<ClInclude Include="mt\include\mt\RequestQueue.h" />
<ClInclude Include="mt\include\mt\Runnable1D.h" />
<ClInclude Include="mt\include\mt\Singleton.h" />
<ClInclude Include="mt\include\mt\ThreadedByteSwap.h" />
<ClInclude Include="mt\include\mt\ThreadGroup.h" />
<ClInclude Include="mt\include\mt\ThreadPlanner.h" />
<ClInclude Include="mt\include\mt\ThreadPoolException.h" />
Expand Down Expand Up @@ -191,6 +192,8 @@
<ClInclude Include="sys\include\sys\AtomicCounter.h" />
<ClInclude Include="sys\include\sys\AtomicCounterCpp11.h" />
<ClInclude Include="sys\include\sys\Backtrace.h" />
<ClInclude Include="sys\include\sys\ByteSwap.h" />
<ClInclude Include="sys\include\sys\ByteSwapValue.h" />
<ClInclude Include="sys\include\sys\ConditionVar.h" />
<ClInclude Include="sys\include\sys\ConditionVarInterface.h" />
<ClInclude Include="sys\include\sys\ConditionVarPosix.h" />
Expand Down Expand Up @@ -228,6 +231,7 @@
<ClInclude Include="sys\include\sys\SemaphoreInterface.h" />
<ClInclude Include="sys\include\sys\SemaphorePosix.h" />
<ClInclude Include="sys\include\sys\SemaphoreWin32.h" />
<ClInclude Include="sys\include\sys\Span.h" />
<ClInclude Include="sys\include\sys\StopWatch.h" />
<ClInclude Include="sys\include\sys\String.h" />
<ClInclude Include="sys\include\sys\SystemException.h" />
Expand Down Expand Up @@ -421,14 +425,12 @@
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<EnableASAN>true</EnableASAN>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<EnableASAN>true</EnableASAN>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down
12 changes: 12 additions & 0 deletions modules/c++/coda-oss-lite.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,18 @@
<ClInclude Include="config\include\config\disable_compiler_warnings.h">
<Filter>config</Filter>
</ClInclude>
<ClInclude Include="sys\include\sys\ByteSwap.h">
<Filter>sys</Filter>
</ClInclude>
<ClInclude Include="sys\include\sys\ByteSwapValue.h">
<Filter>sys</Filter>
</ClInclude>
<ClInclude Include="mt\include\mt\ThreadedByteSwap.h">
<Filter>mt</Filter>
</ClInclude>
<ClInclude Include="sys\include\sys\Span.h">
<Filter>sys</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp" />
Expand Down
3 changes: 2 additions & 1 deletion modules/c++/coda_oss/include/coda_oss/CPlusPlus.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@
#endif // CODA_OSS_cplusplus

// Define a few macros as that's less verbose than testing against a version number
// https://en.cppreference.com/w/cpp/preprocessor/replace#Predefined_macros
#define CODA_OSS_cpp11 (CODA_OSS_cplusplus >= 201103L)
#define CODA_OSS_cpp14 (CODA_OSS_cplusplus >= 201402L)
#define CODA_OSS_cpp17 (CODA_OSS_cplusplus >= 201703L)
#define CODA_OSS_cpp20 (CODA_OSS_cplusplus >= 202002L)
#define CODA_OSS_cpp23 0
#define CODA_OSS_cpp23 (CODA_OSS_cplusplus >= 202302L)

#if !CODA_OSS_cpp17
#error "Must compile with C++17 or greater."
Expand Down
60 changes: 60 additions & 0 deletions modules/c++/coda_oss/include/coda_oss/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
#define CODA_OSS_coda_oss_bit_h_INCLUDED_
#pragma once

#include <stdlib.h>
#include <stdint.h>
#ifdef __GNUC__
#include <byteswap.h> // "These functions are GNU extensions."
#endif

#include <type_traits>

#include "coda_oss/namespace_.h"
namespace coda_oss
{
Expand All @@ -38,6 +46,58 @@ namespace coda_oss
native = __BYTE_ORDER__
#endif
};

// https://en.cppreference.com/w/cpp/numeric/byteswap
namespace details
{
// Overloads for common types
inline constexpr uint8_t byteswap(uint8_t val) noexcept
{
return val; // no-op
}
#if defined(_MSC_VER)
// These routines should generate a single instruction; see
// https://devblogs.microsoft.com/cppblog/a-tour-of-4-msvc-backend-improvements/
inline uint16_t byteswap(uint16_t val)
{
return _byteswap_ushort(val);
}
inline uint32_t byteswap(uint32_t val)
{
return _byteswap_ulong(val);
}
inline uint64_t byteswap(uint64_t val)
{
return _byteswap_uint64(val);
}
#elif defined(__GNUC__)
inline uint16_t byteswap(uint16_t val)
{
return bswap_16(val);
}
inline uint32_t byteswap(uint32_t val)
{
return bswap_32(val);
}
inline uint64_t byteswap(uint64_t val)
{
return bswap_64(val);
}
#else
#error "No platform-specific byteswap()" // TODO: do something else?
#endif
}

template <typename T>
inline T byteswap(T n) noexcept
{
// "std::byteswap participates in overload resolution only if T satisfies integral, i.e., T is an integer type. The program is
// ill-formed if T has padding bits."
static_assert(std::is_integral<T>::value, "T must be integral");

using unsigned_t = std::make_unsigned_t<T>; // "Since C++14" https://en.cppreference.com/w/cpp/types/make_unsigned
return details::byteswap(static_cast<unsigned_t>(n));
}
}

#endif // CODA_OSS_coda_oss_bit_h_INCLUDED_
4 changes: 4 additions & 0 deletions modules/c++/coda_oss/include/coda_oss/cstddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#define CODA_OSS_coda_oss_cstddef_h_INCLUDED_
#pragma once

#include <stdint.h>

#include <cstddef>
#include <type_traits>

// Need a fairly decent C++ compiler to use the real GSL. This brings in more than
// we really need for span (e.g., gsl::narrow()), but it keeps things simple.
Expand All @@ -32,5 +35,6 @@ namespace coda_oss
{
using byte = std::byte;
}
static_assert(!std::is_same<coda_oss::byte, uint8_t>::value, "'coda_oss::byte' should be a unique type.");

#endif // CODA_OSS_coda_oss_cstddef_h_INCLUDED_
21 changes: 21 additions & 0 deletions modules/c++/coda_oss/include/coda_oss/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "coda_oss/namespace_.h"
#include "coda_oss/span_.h"
#include "coda_oss/cstddef.h" // byte

// Need a fairly decent C++ compiler to use the real GSL. This brings in more than
// we really need for span (e.g., gsl::narrow()), but it keeps things simple.
Expand All @@ -37,6 +38,26 @@ namespace coda_oss
#else // no gsl::span, use our own
using details::span;
#endif // GSL_SPAN_H

// https://en.cppreference.com/w/cpp/container/span/as_bytes
template <typename T>
span<const byte> as_bytes(span<const T> s) noexcept
{
const void* const p = s.data();
return span<const byte>(static_cast<const byte*>(p), s.size_bytes());
}
template <typename T>
span<const byte> as_bytes(span<T> s) noexcept
{
return as_bytes(span<const T>(s.data(), s.size()));
}
template <typename T>
span<byte> as_writable_bytes(span<T> s) noexcept
{
void* const p = s.data();
return span<byte>(static_cast<byte*>(p), s.size_bytes());
}

}

#endif // CODA_OSS_coda_oss_span_h_INCLUDED_
36 changes: 36 additions & 0 deletions modules/c++/mem/unittests/test_Span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <string>

#include <coda_oss/span.h>
#include <sys/Span.h>

#include "TestCase.h"

Expand Down Expand Up @@ -94,8 +95,43 @@ TEST_CASE(testGslNarrow)
TEST_THROWS(gsl::narrow<int>(d));
}

TEST_CASE(test_sys_make_span)
{
int i = 314;
int* const p = &i;
auto s = sys::make_span(p, 1);
TEST_ASSERT(s.data() == p);
TEST_ASSERT_EQ(s[0], i);
s[0] = 123;
TEST_ASSERT_EQ(i, 123);
s[0] = 314;

const int* const q = &i;
auto cs = sys::make_span(q, 1);
TEST_ASSERT(cs.data() == q);
TEST_ASSERT_EQ(cs[0], i);
//cs[0] = 123; // cs = span<const>
TEST_ASSERT_EQ(i, 314);

std::vector<int> v{314};
s = sys::make_span(v);
TEST_ASSERT(s.data() == v.data());
TEST_ASSERT_EQ(s[0], v[0]);
s[0] = 123;
TEST_ASSERT_EQ(v[0], 123);
s[0] = 314;

const std::vector<int>& u = v;
cs = sys::make_span(u);
TEST_ASSERT(cs.data() == u.data());
TEST_ASSERT_EQ(cs[0], u[0]);
// cs[0] = 123; // cs = span<const>
TEST_ASSERT_EQ(u[0], 314);
}

TEST_MAIN(
TEST_CHECK(testSpanBuffer);
TEST_CHECK(testSpanVector);
TEST_CHECK(testGslNarrow);
TEST_CHECK(test_sys_make_span);
)
Loading

0 comments on commit fe6920a

Please sign in to comment.