Initial commit
This commit is contained in:
commit
acadc92e68
780 files changed
+468285
No files matched your search
@@ -0,0 +1,169 @@
|
||||
# clang-format 14
|
||||
|
||||
Language: Cpp
|
||||
Standard: c++20
|
||||
|
||||
AccessModifierOffset: -4 #?
|
||||
AlignAfterOpenBracket: AlwaysBreak
|
||||
AlignArrayOfStructures: Left
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveBitFields: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignConsecutiveMacros: true
|
||||
AlignEscapedNewlines: DontAlign
|
||||
AlignOperands: AlignAfterOperator
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllConstructorInitializersOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLambdasOnASingleLine: Inline
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
AttributeMacros: []
|
||||
BinPackArguments: true
|
||||
BinPackParameters: false
|
||||
BitFieldColonSpacing: Both
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterControlStatement: MultiLine
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
#AfterObjCDeclaration
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: false
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: false
|
||||
SplitEmptyNamespace: true
|
||||
#BreakAfterJavaFieldAnnotations
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeBraces: Custom
|
||||
BreakBeforeConceptDeclarations: true
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
BreakInheritanceList: BeforeComma
|
||||
BreakStringLiterals: false
|
||||
ColumnLimit: 0
|
||||
CommentPragmas: '' #?
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4 #?
|
||||
ContinuationIndentWidth: 4 #?
|
||||
Cpp11BracedListStyle: true
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: false
|
||||
EmptyLineAfterAccessModifier: Leave
|
||||
EmptyLineBeforeAccessModifier: Leave
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros: []
|
||||
IfMacros: ['MPT_MAYBE_CONSTANT_IF']
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories: [] #?
|
||||
IncludeIsMainRegex: '' #?
|
||||
IncludeIsMainSourceRegex: '' #?
|
||||
IndentAccessModifiers: false
|
||||
IndentCaseLabels: true
|
||||
IndentCaseBlocks: true
|
||||
IndentExternBlock: NoIndent
|
||||
IndentGotoLabels: false
|
||||
IndentPPDirectives: None
|
||||
#IndentRequiresClause: true
|
||||
InsertTrailingCommas: None
|
||||
#BeforeHash
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: true
|
||||
#JavaImportGroups
|
||||
#JavaScriptQuotes
|
||||
#JavaScriptWrapImports
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
LambdaBodyIndentation: OuterScope
|
||||
MacroBlockBegin: '^MPT_TEST_GROUP_BEGIN$' #?
|
||||
MacroBlockEnd: '^MPT_TEST_GROUP_END$' #?
|
||||
MaxEmptyLinesToKeep: 5
|
||||
NamespaceIndentation: None
|
||||
NamespaceMacros: [] #?
|
||||
#ObjCBinPackProtocolList
|
||||
#ObjCBlockIndentWidth
|
||||
#ObjCBreakBeforeNestedBlockParam
|
||||
#ObjCSpaceAfterProperty
|
||||
#ObjCSpaceBeforeProtocolList
|
||||
PackConstructorInitializers: Never
|
||||
#PenaltyBreakAssignment
|
||||
#PenaltyBreakBeforeFirstCallParameter
|
||||
#PenaltyBreakComment
|
||||
#PenaltyBreakFirstLessLess
|
||||
#PenaltyBreakOpenParenthesis
|
||||
#PenaltyBreakString
|
||||
#PenaltyBreakTemplateDeclaration
|
||||
#PenaltyExcessCharacter
|
||||
#PenaltyIndentedWhitespace
|
||||
#PenaltyReturnTypeOnItsOwnLine
|
||||
PointerAlignment: Middle
|
||||
PPIndentWidth: -1
|
||||
#RawStringFormats
|
||||
QualifierAlignment: Leave
|
||||
#QualifierOrder: ['static', 'inline', 'constexpr', 'volatile', 'const', 'restrict', 'type']
|
||||
ReferenceAlignment: Pointer
|
||||
ReflowComments: false
|
||||
RemoveBracesLLVM: false
|
||||
SeparateDefinitionBlocks: Leave
|
||||
ShortNamespaceLines: 1
|
||||
SortIncludes: false
|
||||
#SortJavaStaticImport
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterForeachMacros: true
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterIfMacros: true
|
||||
AfterOverloadedOperator: false
|
||||
#AfterRequiresInClause: false
|
||||
#AfterRequiresInExpression: false
|
||||
BeforeNonEmptyParentheses: false
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceBeforeSquareBrackets: false
|
||||
SpaceInEmptyBlock: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInAngles: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
StatementAttributeLikeMacros: []
|
||||
StatementMacros: [ '_Pragma', '__pragma', 'MPT_WARNING', 'MPT_TEST_GROUP_INLINE_IDENTIFIER', 'MPT_TEST_GROUP_INLINE', 'MPT_TEST_GROUP_STATIC' ] #?
|
||||
TabWidth: 4
|
||||
TypenameMacros: [] #?
|
||||
UseCRLF: false
|
||||
UseTab: ForContinuationAndIndentation
|
||||
WhitespaceSensitiveMacros:
|
||||
- MPT_PP_STRINGIFY
|
||||
@@ -0,0 +1,25 @@
|
||||
Copyright (c) 2004-2026, OpenMPT Project Developers and Contributors
|
||||
Copyright (c) 1997-2003, Olivier Lapicque
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the OpenMPT project nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,23 @@
|
||||
Boost Software License - Version 1.0 - August 17th, 2003
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
@@ -0,0 +1,200 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ARCH_ARCH_HPP
|
||||
#define MPT_ARCH_ARCH_HPP
|
||||
|
||||
|
||||
#include "mpt/arch/feature_fence.hpp"
|
||||
#include "mpt/arch/feature_flags.hpp"
|
||||
#include "mpt/arch/x86_amd64.hpp"
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace arch {
|
||||
|
||||
|
||||
|
||||
namespace unknown {
|
||||
|
||||
template <typename tag>
|
||||
struct no_flags {
|
||||
constexpr no_flags() noexcept = default;
|
||||
constexpr no_flags operator~() noexcept {
|
||||
return no_flags{};
|
||||
}
|
||||
constexpr no_flags & operator&=(no_flags) noexcept {
|
||||
return *this;
|
||||
}
|
||||
constexpr no_flags & operator|=(no_flags) noexcept {
|
||||
return *this;
|
||||
}
|
||||
constexpr no_flags & operator^=(no_flags) noexcept {
|
||||
return *this;
|
||||
}
|
||||
friend constexpr no_flags operator&(no_flags, no_flags) noexcept {
|
||||
return no_flags{};
|
||||
}
|
||||
friend constexpr no_flags operator|(no_flags, no_flags) noexcept {
|
||||
return no_flags{};
|
||||
}
|
||||
friend constexpr no_flags operator^(no_flags, no_flags) noexcept {
|
||||
return no_flags{};
|
||||
}
|
||||
friend constexpr bool operator==(no_flags, no_flags) noexcept {
|
||||
return true;
|
||||
}
|
||||
friend constexpr bool operator!=(no_flags, no_flags) noexcept {
|
||||
return false;
|
||||
}
|
||||
constexpr bool supports(no_flags) noexcept {
|
||||
return true;
|
||||
}
|
||||
explicit constexpr operator bool() const noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool operator!() const noexcept {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct no_feature_flags_tag {
|
||||
};
|
||||
|
||||
struct no_mode_flags_tag {
|
||||
};
|
||||
|
||||
using feature_flags = mpt::arch::basic_feature_flags<no_flags<no_feature_flags_tag>>;
|
||||
using mode_flags = mpt::arch::basic_feature_flags<no_flags<no_mode_flags_tag>>;
|
||||
|
||||
namespace feature {
|
||||
inline constexpr feature_flags none = feature_flags{};
|
||||
} // namespace feature
|
||||
|
||||
namespace mode {
|
||||
inline constexpr mode_flags none = mode_flags{};
|
||||
} // namespace mode
|
||||
|
||||
struct cpu_info {
|
||||
public:
|
||||
cpu_info() = default;
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE bool operator[](feature_flags) const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE bool has_features(feature_flags) const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE feature_flags get_features() const noexcept {
|
||||
return {};
|
||||
}
|
||||
MPT_CONSTEXPRINLINE bool operator[](mode_flags) const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE bool enabled_modes(mode_flags) const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE mode_flags get_modes() const noexcept {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] MPT_CONSTEVAL feature_flags assumed_features() noexcept {
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] MPT_CONSTEVAL mode_flags assumed_modes() noexcept {
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace unknown
|
||||
|
||||
|
||||
|
||||
#if MPT_ARCH_X86
|
||||
namespace current = x86;
|
||||
#elif MPT_ARCH_AMD64
|
||||
namespace current = amd64;
|
||||
#else
|
||||
namespace current = unknown;
|
||||
#endif
|
||||
|
||||
using cpu_info = mpt::arch::current::cpu_info;
|
||||
|
||||
inline const cpu_info & get_cpu_info() {
|
||||
static cpu_info info;
|
||||
return info;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
struct info_initializer {
|
||||
inline info_initializer() noexcept {
|
||||
get_cpu_info();
|
||||
}
|
||||
};
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif // MPT_COMPILER_CLANG
|
||||
inline info_initializer g_info_initializer;
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif // MPT_COMPILER_CLANG
|
||||
|
||||
|
||||
} // namespace detail
|
||||
|
||||
struct flags_cache {
|
||||
private:
|
||||
const mpt::arch::current::feature_flags Features;
|
||||
const mpt::arch::current::mode_flags Modes;
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE flags_cache(const mpt::arch::cpu_info & info) noexcept
|
||||
: Features(info.get_features())
|
||||
, Modes(info.get_modes()) {
|
||||
return;
|
||||
}
|
||||
[[nodiscard]] MPT_CONSTEXPRINLINE bool operator[](mpt::arch::current::feature_flags query_features) const noexcept {
|
||||
return ((Features & query_features) == query_features);
|
||||
}
|
||||
[[nodiscard]] MPT_CONSTEXPRINLINE bool has_features(mpt::arch::current::feature_flags query_features) const noexcept {
|
||||
return ((Features & query_features) == query_features);
|
||||
}
|
||||
[[nodiscard]] MPT_CONSTEXPRINLINE mpt::arch::current::feature_flags get_features() const noexcept {
|
||||
return Features;
|
||||
}
|
||||
[[nodiscard]] MPT_CONSTEXPRINLINE bool operator[](mpt::arch::current::mode_flags query_modes) const noexcept {
|
||||
return ((Modes & query_modes) == query_modes);
|
||||
}
|
||||
[[nodiscard]] MPT_CONSTEXPRINLINE bool has_features(mpt::arch::current::mode_flags query_modes) const noexcept {
|
||||
return ((Modes & query_modes) == query_modes);
|
||||
}
|
||||
[[nodiscard]] MPT_CONSTEXPRINLINE mpt::arch::current::mode_flags get_modes() const noexcept {
|
||||
return Modes;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace arch
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ARCH_ARCH_HPP
|
||||
@@ -0,0 +1,57 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ARCH_FEATURE_FENCE_HPP
|
||||
#define MPT_ARCH_FEATURE_FENCE_HPP
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace arch {
|
||||
|
||||
|
||||
|
||||
MPT_FORCEINLINE void feature_fence_aquire() noexcept {
|
||||
std::atomic_signal_fence(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE void feature_fence_release() noexcept {
|
||||
std::atomic_signal_fence(std::memory_order_release);
|
||||
}
|
||||
|
||||
class feature_fence_guard {
|
||||
public:
|
||||
MPT_FORCEINLINE feature_fence_guard() noexcept {
|
||||
mpt::arch::feature_fence_aquire();
|
||||
}
|
||||
MPT_FORCEINLINE ~feature_fence_guard() noexcept {
|
||||
mpt::arch::feature_fence_release();
|
||||
}
|
||||
feature_fence_guard(feature_fence_guard &&) = delete;
|
||||
feature_fence_guard(const feature_fence_guard &) = delete;
|
||||
feature_fence_guard & operator=(feature_fence_guard &&) = delete;
|
||||
feature_fence_guard & operator=(const feature_fence_guard &) = delete;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace arch
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ARCH_FEATURE_FENCE_HPP
|
||||
@@ -0,0 +1,82 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ARCH_FEATURE_FLAGS_HPP
|
||||
#define MPT_ARCH_FEATURE_FLAGS_HPP
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace arch {
|
||||
|
||||
|
||||
|
||||
template <typename bitset>
|
||||
struct [[nodiscard]] basic_feature_flags {
|
||||
private:
|
||||
bitset m_flags{};
|
||||
public:
|
||||
constexpr basic_feature_flags() noexcept = default;
|
||||
explicit constexpr basic_feature_flags(bitset flags) noexcept
|
||||
: m_flags(flags) {
|
||||
return;
|
||||
}
|
||||
[[nodiscard]] constexpr basic_feature_flags operator~() noexcept {
|
||||
return basic_feature_flags{~m_flags};
|
||||
}
|
||||
constexpr basic_feature_flags & operator&=(basic_feature_flags o) noexcept {
|
||||
m_flags &= o.m_flags;
|
||||
return *this;
|
||||
}
|
||||
constexpr basic_feature_flags & operator|=(basic_feature_flags o) noexcept {
|
||||
m_flags |= o.m_flags;
|
||||
return *this;
|
||||
}
|
||||
constexpr basic_feature_flags & operator^=(basic_feature_flags o) noexcept {
|
||||
m_flags ^= o.m_flags;
|
||||
return *this;
|
||||
}
|
||||
[[nodiscard]] friend constexpr basic_feature_flags operator&(basic_feature_flags a, basic_feature_flags b) noexcept {
|
||||
return basic_feature_flags{static_cast<bitset>(a.m_flags & b.m_flags)};
|
||||
}
|
||||
[[nodiscard]] friend constexpr basic_feature_flags operator|(basic_feature_flags a, basic_feature_flags b) noexcept {
|
||||
return basic_feature_flags{static_cast<bitset>(a.m_flags | b.m_flags)};
|
||||
}
|
||||
[[nodiscard]] friend constexpr basic_feature_flags operator^(basic_feature_flags a, basic_feature_flags b) noexcept {
|
||||
return basic_feature_flags{static_cast<bitset>(a.m_flags ^ b.m_flags)};
|
||||
}
|
||||
[[nodiscard]] friend constexpr bool operator==(basic_feature_flags a, basic_feature_flags b) noexcept {
|
||||
return a.m_flags == b.m_flags;
|
||||
}
|
||||
[[nodiscard]] friend constexpr bool operator!=(basic_feature_flags a, basic_feature_flags b) noexcept {
|
||||
return a.m_flags != b.m_flags;
|
||||
}
|
||||
[[nodiscard]] constexpr bool supports(basic_feature_flags query) noexcept {
|
||||
return (m_flags & query.m_flags) == query.m_flags;
|
||||
}
|
||||
[[nodiscard]] explicit constexpr operator bool() const noexcept {
|
||||
return m_flags ? true : false;
|
||||
}
|
||||
[[nodiscard]] constexpr bool operator!() const noexcept {
|
||||
return m_flags ? false : true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace arch
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ARCH_FEATURE_FLAGS_HPP
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,35 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_AUDIO_SAMPLE_HPP
|
||||
#define MPT_AUDIO_SAMPLE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/float.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
using audio_sample_int = int16;
|
||||
using audio_sample_float = nativefloat;
|
||||
|
||||
using audio_sample = std::conditional<mpt::float_traits<audio_sample_float>::is_hard, audio_sample_float, audio_sample_int>::type;
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BINARY_HEX_HPP
|
||||
@@ -0,0 +1,446 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_AUDIO_SPAN_HPP
|
||||
#define MPT_AUDIO_SPAN_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
// LRLRLRLRLR
|
||||
|
||||
template <typename SampleType>
|
||||
struct audio_span_interleaved {
|
||||
public:
|
||||
using sample_type = SampleType;
|
||||
|
||||
private:
|
||||
sample_type * const m_buffer;
|
||||
std::size_t m_channels;
|
||||
std::size_t m_frames;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE audio_span_interleaved(sample_type * buffer, std::size_t channels, std::size_t frames) noexcept
|
||||
: m_buffer(buffer)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
return;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * data() const noexcept {
|
||||
return m_buffer;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
|
||||
return m_buffer[m_channels * frame + channel];
|
||||
}
|
||||
MPT_FORCEINLINE bool is_contiguous() const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_channels() const noexcept {
|
||||
return m_channels;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_frames() const noexcept {
|
||||
return m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_samples() const noexcept {
|
||||
return m_channels * m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
|
||||
return m_channels;
|
||||
}
|
||||
};
|
||||
|
||||
struct audio_span_frames_are_contiguous_t {
|
||||
};
|
||||
|
||||
inline constexpr audio_span_frames_are_contiguous_t audio_span_frames_are_contiguous;
|
||||
|
||||
|
||||
|
||||
// LLLLLRRRRR
|
||||
|
||||
template <typename SampleType>
|
||||
struct audio_span_contiguous {
|
||||
public:
|
||||
using sample_type = SampleType;
|
||||
|
||||
private:
|
||||
sample_type * const m_buffer;
|
||||
std::size_t m_channels;
|
||||
std::size_t m_frames;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE audio_span_contiguous(sample_type * buffer, std::size_t channels, std::size_t frames) noexcept
|
||||
: m_buffer(buffer)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
return;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * data() const noexcept {
|
||||
return m_buffer;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
|
||||
return m_buffer[(m_frames * channel) + frame];
|
||||
}
|
||||
MPT_FORCEINLINE bool is_contiguous() const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_channels() const noexcept {
|
||||
return m_channels;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_frames() const noexcept {
|
||||
return m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_samples() const noexcept {
|
||||
return m_channels * m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
struct audio_span_channels_are_contiguous_t {
|
||||
};
|
||||
|
||||
inline constexpr audio_span_channels_are_contiguous_t audio_span_channels_are_contiguous;
|
||||
|
||||
|
||||
|
||||
// LLLLL RRRRR
|
||||
|
||||
template <typename SampleType>
|
||||
struct audio_span_planar {
|
||||
public:
|
||||
using sample_type = SampleType;
|
||||
|
||||
private:
|
||||
sample_type * const * m_buffers;
|
||||
std::size_t m_channels;
|
||||
std::size_t m_frames;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE audio_span_planar(sample_type * const * buffers, std::size_t channels, std::size_t frames) noexcept
|
||||
: m_buffers(buffers)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
return;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
|
||||
return m_buffers;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * data() const noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
|
||||
return m_buffers[channel][frame];
|
||||
}
|
||||
MPT_FORCEINLINE bool is_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
|
||||
return true;
|
||||
}
|
||||
MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_channels() const noexcept {
|
||||
return m_channels;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_frames() const noexcept {
|
||||
return m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_samples() const noexcept {
|
||||
return m_channels * m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
struct audio_span_channels_are_planar_t {
|
||||
};
|
||||
|
||||
inline constexpr audio_span_channels_are_planar_t audio_span_channels_are_planar;
|
||||
|
||||
|
||||
|
||||
// LxxxLxxxLxxxLxxxLxxx xRxxxRxxxRxxxRxxxRxx
|
||||
template <typename SampleType>
|
||||
struct audio_span_planar_strided {
|
||||
public:
|
||||
using sample_type = SampleType;
|
||||
|
||||
private:
|
||||
sample_type * const * m_buffers;
|
||||
std::ptrdiff_t m_frame_stride;
|
||||
std::size_t m_channels;
|
||||
std::size_t m_frames;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE audio_span_planar_strided(sample_type * const * buffers, std::size_t channels, std::size_t frames, std::ptrdiff_t frame_stride) noexcept
|
||||
: m_buffers(buffers)
|
||||
, m_frame_stride(frame_stride)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
return;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
|
||||
return m_buffers;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * data() const noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
|
||||
return m_buffers[channel][static_cast<std::ptrdiff_t>(frame) * m_frame_stride];
|
||||
}
|
||||
MPT_FORCEINLINE bool is_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
|
||||
return false;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_channels() const noexcept {
|
||||
return m_channels;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_frames() const noexcept {
|
||||
return m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_samples() const noexcept {
|
||||
return m_channels * m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
|
||||
return m_frame_stride;
|
||||
}
|
||||
};
|
||||
|
||||
struct audio_span_channels_are_planar_and_strided_t {
|
||||
};
|
||||
|
||||
inline constexpr audio_span_channels_are_planar_and_strided_t audio_span_channels_are_planar_and_strided;
|
||||
|
||||
|
||||
|
||||
template <typename SampleType>
|
||||
struct audio_span {
|
||||
public:
|
||||
using sample_type = SampleType;
|
||||
|
||||
private:
|
||||
union {
|
||||
sample_type * const contiguous;
|
||||
sample_type * const * const planes;
|
||||
} m_buffer;
|
||||
std::ptrdiff_t m_frame_stride;
|
||||
std::ptrdiff_t m_channel_stride;
|
||||
std::size_t m_channels;
|
||||
std::size_t m_frames;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE audio_span(audio_span_interleaved<sample_type> buffer) noexcept
|
||||
: m_frame_stride(static_cast<std::ptrdiff_t>(buffer.size_channels()))
|
||||
, m_channel_stride(1)
|
||||
, m_channels(buffer.size_channels())
|
||||
, m_frames(buffer.size_frames()) {
|
||||
m_buffer.contiguous = buffer.data();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(sample_type * buffer, std::size_t channels, std::size_t frames, audio_span_frames_are_contiguous_t) noexcept
|
||||
: m_frame_stride(static_cast<std::ptrdiff_t>(channels))
|
||||
, m_channel_stride(1)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
m_buffer.contiguous = buffer;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(audio_span_contiguous<sample_type> buffer) noexcept
|
||||
: m_frame_stride(1)
|
||||
, m_channel_stride(buffer.size_frames())
|
||||
, m_channels(buffer.size_channels())
|
||||
, m_frames(buffer.size_frames()) {
|
||||
m_buffer.contiguous = buffer.data();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(sample_type * buffer, std::size_t channels, std::size_t frames, audio_span_channels_are_contiguous_t) noexcept
|
||||
: m_frame_stride(1)
|
||||
, m_channel_stride(static_cast<std::ptrdiff_t>(frames))
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
m_buffer.contiguous = buffer;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(audio_span_planar<sample_type> buffer) noexcept
|
||||
: m_frame_stride(1)
|
||||
, m_channel_stride(0)
|
||||
, m_channels(buffer.size_channels())
|
||||
, m_frames(buffer.size_frames()) {
|
||||
m_buffer.planes = buffer.data_planar();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(sample_type * const * planes, std::size_t channels, std::size_t frames, audio_span_channels_are_planar_t) noexcept
|
||||
: m_frame_stride(1)
|
||||
, m_channel_stride(0)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
m_buffer.planes = planes;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(audio_span_planar_strided<sample_type> buffer) noexcept
|
||||
: m_frame_stride(static_cast<std::ptrdiff_t>(buffer.frame_stride()))
|
||||
, m_channel_stride(0)
|
||||
, m_channels(buffer.size_channels())
|
||||
, m_frames(buffer.size_frames()) {
|
||||
m_buffer.planes = buffer.data_planar();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE audio_span(sample_type * const * planes, std::size_t channels, std::size_t frames, std::ptrdiff_t frame_stride, audio_span_channels_are_planar_and_strided_t) noexcept
|
||||
: m_frame_stride(frame_stride)
|
||||
, m_channel_stride(0)
|
||||
, m_channels(channels)
|
||||
, m_frames(frames) {
|
||||
m_buffer.planes = planes;
|
||||
}
|
||||
MPT_FORCEINLINE bool is_contiguous() const noexcept {
|
||||
return (m_channel_stride != 0);
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
|
||||
return (!is_contiguous()) ? m_buffer.planes : nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * data() const noexcept {
|
||||
return is_contiguous() ? m_buffer.contiguous : nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
|
||||
return is_contiguous() ? m_buffer.contiguous[(m_channel_stride * static_cast<std::ptrdiff_t>(channel)) + (m_frame_stride * static_cast<std::ptrdiff_t>(frame))] : m_buffer.planes[channel][frame * static_cast<std::ptrdiff_t>(m_frame_stride)];
|
||||
}
|
||||
MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
|
||||
return (m_frame_stride == 1);
|
||||
}
|
||||
MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
|
||||
return (m_channel_stride == 1);
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_channels() const noexcept {
|
||||
return m_channels;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_frames() const noexcept {
|
||||
return m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_samples() const noexcept {
|
||||
return m_channels * m_frames;
|
||||
}
|
||||
MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
|
||||
return m_frame_stride;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename Taudio_span>
|
||||
struct audio_span_with_offset {
|
||||
public:
|
||||
using sample_type = typename Taudio_span::sample_type;
|
||||
|
||||
private:
|
||||
Taudio_span m_buffer;
|
||||
std::size_t m_offset;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE audio_span_with_offset(Taudio_span buffer, std::size_t offsetFrames) noexcept
|
||||
: m_buffer(buffer)
|
||||
, m_offset(offsetFrames) {
|
||||
return;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * const * data_planar() const noexcept {
|
||||
return nullptr;
|
||||
}
|
||||
MPT_FORCEINLINE sample_type * data() const noexcept {
|
||||
if (!is_contiguous()) {
|
||||
return nullptr;
|
||||
}
|
||||
return m_buffer.data() + (size_channels() * m_offset);
|
||||
}
|
||||
MPT_FORCEINLINE sample_type & operator()(std::size_t channel, std::size_t frame) const {
|
||||
return m_buffer(channel, m_offset + frame);
|
||||
}
|
||||
MPT_FORCEINLINE bool is_contiguous() const noexcept {
|
||||
return m_buffer.is_contiguous() && m_buffer.frames_are_contiguous();
|
||||
}
|
||||
MPT_FORCEINLINE bool channels_are_contiguous() const noexcept {
|
||||
return m_buffer.channels_are_contiguous();
|
||||
}
|
||||
MPT_FORCEINLINE bool frames_are_contiguous() const noexcept {
|
||||
return m_buffer.frames_are_contiguous();
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_channels() const noexcept {
|
||||
return m_buffer.size_channels();
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_frames() const noexcept {
|
||||
return m_buffer.size_frames() - m_offset;
|
||||
}
|
||||
MPT_FORCEINLINE std::size_t size_samples() const noexcept {
|
||||
return size_channels() * size_frames();
|
||||
}
|
||||
MPT_FORCEINLINE std::ptrdiff_t frame_stride() const noexcept {
|
||||
return m_buffer.frame_stride();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename Tspan, typename Tfunc>
|
||||
MPT_FORCEINLINE auto audio_span_optimized_visit(Tspan span, Tfunc && f) -> decltype(std::forward<Tfunc>(f)(span)) const {
|
||||
using sample_type = typename Tspan::sample_type;
|
||||
std::variant<Tspan, audio_span_interleaved<sample_type>, audio_span_contiguous<sample_type>, audio_span_planar<sample_type>, audio_span_planar_strided<sample_type>> v{span};
|
||||
if (span.data() && span.is_contiguous() && span.frames_are_contiguous()) {
|
||||
v = audio_span_interleaved<sample_type>{span.data(), span.size_channels(), span.size_frames()};
|
||||
} else if (span.data() && span.is_contiguous() && span.channels_are_contiguous()) {
|
||||
v = audio_span_contiguous<sample_type>{span.data(), span.size_channels(), span.size_frames()};
|
||||
} else if (span.data_planar() && span.channels_are_contiguous()) {
|
||||
v = audio_span_planar<sample_type>{span.data_planar(), span.size_channels(), span.size_frames()};
|
||||
} else if (span.data_planar()) {
|
||||
v = audio_span_planar_strided<sample_type>{span.data_planar(), span.size_channels(), span.size_frames(), span.frame_stride()};
|
||||
}
|
||||
return std::visit(std::forward<Tfunc>(f), v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename BufferType>
|
||||
inline audio_span_with_offset<BufferType> make_audio_span_with_offset(BufferType buf, std::size_t offsetFrames) noexcept {
|
||||
assert(offsetFrames <= buf.size_frames());
|
||||
return audio_span_with_offset<BufferType>{buf, offsetFrames};
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_AUDIO_SPAN_HPP
|
||||
@@ -0,0 +1,109 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_ALGORITHM_HPP
|
||||
#define MPT_BASE_ALGORITHM_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/saturate_cast.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
// Grows x with an exponential factor suitable for increasing buffer sizes.
|
||||
// Clamps the result at limit.
|
||||
// And avoids integer overflows while doing its business.
|
||||
// The growth factor is 1.5, rounding down, execpt for the initial x==1 case.
|
||||
template <typename T, typename Tlimit>
|
||||
inline T exponential_grow(const T & x, const Tlimit & limit) {
|
||||
if (x <= 1) {
|
||||
return 2;
|
||||
}
|
||||
T add = std::min(x >> 1, std::numeric_limits<T>::max() - x);
|
||||
return std::min(x + add, mpt::saturate_cast<T>(limit));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T exponential_grow(const T & x) {
|
||||
return mpt::exponential_grow(x, std::numeric_limits<T>::max());
|
||||
}
|
||||
|
||||
|
||||
// Check if val is in [lo,hi] without causing compiler warnings
|
||||
// if theses checks are always true due to the domain of T.
|
||||
// GCC does not warn if the type is templated.
|
||||
template <typename T, typename C>
|
||||
constexpr bool is_in_range(const T & val, const C & lo, const C & hi) {
|
||||
return lo <= val && val <= hi;
|
||||
}
|
||||
|
||||
|
||||
namespace detail {
|
||||
namespace contains {
|
||||
|
||||
template <typename Container, typename Signature>
|
||||
struct has_find : std::false_type {
|
||||
};
|
||||
|
||||
template <typename Container, typename Ret, typename... Args>
|
||||
struct has_find<Container, Ret(Args...)> {
|
||||
private:
|
||||
template <typename T>
|
||||
static constexpr inline auto check(T *) -> typename std::is_same<decltype(std::declval<T>().find(std::declval<Args>()...)), Ret>::type {
|
||||
return {};
|
||||
}
|
||||
template <typename T>
|
||||
static constexpr inline std::false_type check(...) {
|
||||
return {};
|
||||
}
|
||||
public:
|
||||
typedef decltype(check<Container>(nullptr)) type;
|
||||
static constexpr inline bool value = type::value;
|
||||
};
|
||||
|
||||
template <typename Tcontainer, typename Tval>
|
||||
MPT_CONSTEXPR20_FUN bool contains_class_find_impl(const Tcontainer & container, const Tval & value, std::true_type) noexcept(noexcept(container.find(value) != container.end())) {
|
||||
return container.find(value) != container.end();
|
||||
}
|
||||
|
||||
template <typename Tcontainer, typename Tval>
|
||||
MPT_CONSTEXPR20_FUN bool contains_class_find_impl(const Tcontainer & container, const Tval & value, std::false_type) noexcept(noexcept(std::find(std::begin(container), std::end(container), value))) {
|
||||
return std::find(std::begin(container), std::end(container), value) != std::end(container);
|
||||
}
|
||||
|
||||
template <typename Tcontainer, typename Tval>
|
||||
MPT_CONSTEXPR20_FUN bool contains_class_impl(const Tcontainer & container, const Tval & value, std::true_type) noexcept(noexcept(mpt::detail::contains::contains_class_find_impl<Tcontainer, Tval>(container, value, typename mpt::detail::contains::has_find<Tcontainer, typename Tcontainer::iterator(Tval)>::type{}))) {
|
||||
return mpt::detail::contains::contains_class_find_impl<Tcontainer, Tval>(container, value, typename mpt::detail::contains::has_find<Tcontainer, typename Tcontainer::iterator(Tval)>::type{});
|
||||
}
|
||||
|
||||
template <typename Tcontainer, typename Tval>
|
||||
MPT_CONSTEXPR20_FUN bool contains_class_impl(const Tcontainer & container, const Tval & value, std::false_type) noexcept(noexcept(std::find(std::begin(container), std::end(container), value))) {
|
||||
return std::find(std::begin(container), std::end(container), value) != std::end(container);
|
||||
}
|
||||
|
||||
} // namespace contains
|
||||
} // namespace detail
|
||||
|
||||
template <typename Tcontainer, typename Tval>
|
||||
MPT_CONSTEXPR20_FUN bool contains(const Tcontainer & container, const Tval & value) noexcept(noexcept(mpt::detail::contains::contains_class_impl<Tcontainer, Tval>(container, value, typename std::is_class<Tcontainer>::type{}))) {
|
||||
return mpt::detail::contains::contains_class_impl<Tcontainer, Tval>(container, value, typename std::is_class<Tcontainer>::type{});
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_ALGORITHM_HPP
|
||||
@@ -0,0 +1,99 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_ALIGNED_ARRAY_HPP
|
||||
#define MPT_BASE_ALIGNED_ARRAY_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
template <typename T, std::size_t count, std::align_val_t alignment>
|
||||
struct alignas(static_cast<std::size_t>(alignment)) aligned_array
|
||||
: std::array<T, count> {
|
||||
static_assert(static_cast<std::size_t>(alignment) >= alignof(T));
|
||||
static_assert(((count * sizeof(T)) % static_cast<std::size_t>(alignment)) == 0);
|
||||
static_assert(sizeof(std::array<T, count>) == (sizeof(T) * count));
|
||||
};
|
||||
|
||||
static_assert(sizeof(mpt::aligned_array<float, 4, std::align_val_t{sizeof(float) * 4}>) == sizeof(std::array<float, 4>));
|
||||
|
||||
|
||||
|
||||
template <std::size_t alignment_elements, std::size_t expected_elements, typename T, std::size_t N>
|
||||
T * align_elements(std::array<T, N> & a) {
|
||||
static_assert(mpt::has_single_bit(alignof(T)));
|
||||
static_assert(mpt::has_single_bit(sizeof(T)));
|
||||
static_assert(mpt::has_single_bit(alignment_elements));
|
||||
static_assert((expected_elements + alignment_elements - 1) <= N);
|
||||
void * buf = a.data();
|
||||
std::size_t size = N * sizeof(T);
|
||||
void * result = std::align(alignment_elements * sizeof(T), expected_elements * sizeof(T), buf, size);
|
||||
assert(result);
|
||||
return reinterpret_cast<T *>(result);
|
||||
}
|
||||
|
||||
template <std::size_t alignment_elements, std::size_t expected_elements, typename T, std::size_t N>
|
||||
T * align_elements(T (&a)[N]) {
|
||||
static_assert(mpt::has_single_bit(alignof(T)));
|
||||
static_assert(mpt::has_single_bit(sizeof(T)));
|
||||
static_assert(mpt::has_single_bit(alignment_elements));
|
||||
static_assert((expected_elements + alignment_elements - 1) <= N);
|
||||
void * buf = a;
|
||||
std::size_t size = N * sizeof(T);
|
||||
void * result = std::align(alignment_elements * sizeof(T), expected_elements * sizeof(T), buf, size);
|
||||
assert(result);
|
||||
return reinterpret_cast<T *>(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <std::size_t alignment_bytes, std::size_t expected_elements, typename T, std::size_t N>
|
||||
T * align_bytes(std::array<T, N> & a) {
|
||||
static_assert(mpt::has_single_bit(alignof(T)));
|
||||
static_assert(mpt::has_single_bit(sizeof(T)));
|
||||
static_assert(mpt::has_single_bit(alignment_bytes));
|
||||
static_assert((alignment_bytes % alignof(T)) == 0);
|
||||
static_assert(((expected_elements * sizeof(T)) + alignment_bytes - 1) <= (N * sizeof(T)));
|
||||
void * buf = a.data();
|
||||
std::size_t size = N * sizeof(T);
|
||||
void * result = std::align(alignment_bytes, expected_elements * sizeof(T), buf, size);
|
||||
assert(result);
|
||||
return reinterpret_cast<T *>(result);
|
||||
}
|
||||
|
||||
template <std::size_t alignment_bytes, std::size_t expected_elements, typename T, std::size_t N>
|
||||
T * align_bytes(T (&a)[N]) {
|
||||
static_assert(mpt::has_single_bit(alignof(T)));
|
||||
static_assert(mpt::has_single_bit(sizeof(T)));
|
||||
static_assert(mpt::has_single_bit(alignment_bytes));
|
||||
static_assert((alignment_bytes % alignof(T)) == 0);
|
||||
static_assert(((expected_elements * sizeof(T)) + alignment_bytes - 1) <= (N * sizeof(T)));
|
||||
void * buf = a;
|
||||
std::size_t size = N * sizeof(T);
|
||||
void * result = std::align(alignment_bytes, expected_elements * sizeof(T), buf, size);
|
||||
assert(result);
|
||||
return reinterpret_cast<T *>(result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_ALIGNED_ARRAY_HPP
|
||||
@@ -0,0 +1,192 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_ALLOC_HPP
|
||||
#define MPT_BASE_ALLOC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/span.hpp"
|
||||
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline mpt::span<T> as_span(std::vector<T> & cont) {
|
||||
return mpt::span<T>(cont.data(), cont.data() + cont.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline mpt::span<const T> as_span(const std::vector<T> & cont) {
|
||||
return mpt::span<const T>(cont.data(), cont.data() + cont.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline span<T> as_span(std::basic_string<T> & str) {
|
||||
return span<T>(str.data(), str.size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline span<const T> as_span(const std::basic_string<T> & str) {
|
||||
return span<const T>(str.data(), str.size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<typename std::remove_const<T>::type> make_vector(T * beg, T * end) {
|
||||
return std::vector<typename std::remove_const<T>::type>(beg, end);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<typename std::remove_const<T>::type> make_vector(T * data, std::size_t size) {
|
||||
return std::vector<typename std::remove_const<T>::type>(data, data + size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<typename std::remove_const<T>::type> make_vector(mpt::span<T> data) {
|
||||
return std::vector<typename std::remove_const<T>::type>(data.data(), data.data() + data.size());
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline std::vector<typename std::remove_const<T>::type> make_vector(T (&arr)[N]) {
|
||||
return std::vector<typename std::remove_const<T>::type>(std::begin(arr), std::end(arr));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<typename std::remove_const<T>::type> make_vector(const std::basic_string<T> & str) {
|
||||
return std::vector<typename std::remove_const<T>::type>(str.begin(), str.end());
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline std::basic_string<typename std::remove_const<T>::type> make_basic_string(T * beg, T * end) {
|
||||
return std::basic_string<typename std::remove_const<T>::type>(beg, end);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::basic_string<typename std::remove_const<T>::type> make_basic_string(T * data, std::size_t size) {
|
||||
return std::basic_string<typename std::remove_const<T>::type>(data, data + size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::basic_string<typename std::remove_const<T>::type> make_basic_string(mpt::span<T> data) {
|
||||
return std::basic_string<typename std::remove_const<T>::type>(data.data(), data.data() + data.size());
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline std::basic_string<typename std::remove_const<T>::type> make_basic_string(T (&arr)[N]) {
|
||||
return std::basic_string<typename std::remove_const<T>::type>(std::begin(arr), std::end(arr));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::basic_string<typename std::remove_const<T>::type> make_basic_string(const std::vector<T> & str) {
|
||||
return std::vector<typename std::remove_const<T>::type>(str.begin(), str.end());
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename Tcont2, typename Tcont1>
|
||||
inline Tcont1 & append(Tcont1 & cont1, const Tcont2 & cont2) {
|
||||
cont1.insert(cont1.end(), cont2.begin(), cont2.end());
|
||||
return cont1;
|
||||
}
|
||||
|
||||
template <typename Tit2, typename Tcont1>
|
||||
inline Tcont1 & append(Tcont1 & cont1, Tit2 beg, Tit2 end) {
|
||||
cont1.insert(cont1.end(), beg, end);
|
||||
return cont1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
struct buffer_cast_impl {
|
||||
inline Tdst operator()(const Tsrc & src) const {
|
||||
return Tdst(mpt::byte_cast<const typename Tdst::value_type *>(src.data()), mpt::byte_cast<const typename Tdst::value_type *>(src.data()) + src.size());
|
||||
}
|
||||
};
|
||||
|
||||
// casts between vector<->string of byte-castable types
|
||||
template <typename Tdst, typename Tsrc>
|
||||
inline Tdst buffer_cast(const Tsrc & src) {
|
||||
return buffer_cast_impl<Tdst, Tsrc>()(src);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct as_raw_memory_impl<std::vector<T>> {
|
||||
inline mpt::const_byte_span operator()(const std::vector<T> & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
|
||||
}
|
||||
inline mpt::byte_span operator()(std::vector<T> & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<std::byte *>(v.data()), v.size() * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct as_raw_memory_impl<const std::vector<T>> {
|
||||
inline mpt::const_byte_span operator()(const std::vector<T> & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
class heap_value {
|
||||
private:
|
||||
std::unique_ptr<T> m_value{};
|
||||
|
||||
public:
|
||||
template <typename... Targs>
|
||||
heap_value(Targs &&... args)
|
||||
: m_value(std::make_unique<T>(std::forward<Targs>(args)...)) {
|
||||
return;
|
||||
}
|
||||
const T & operator*() const {
|
||||
return *m_value;
|
||||
}
|
||||
T & operator*() {
|
||||
return *m_value;
|
||||
}
|
||||
const T * operator->() const {
|
||||
return m_value.get();
|
||||
}
|
||||
T * operator->() {
|
||||
return m_value.get();
|
||||
}
|
||||
const T * get() const {
|
||||
return m_value.get();
|
||||
}
|
||||
T * get() {
|
||||
return m_value.get();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_ALLOC_HPP
|
||||
@@ -0,0 +1,133 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_ARITHMETIC_SHIFT_HPP
|
||||
#define MPT_BASE_ARITHMETIC_SHIFT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
// mpt::rshift_signed
|
||||
// mpt::lshift_signed
|
||||
// Shift a signed integer value in a well-defined manner.
|
||||
// Does the same thing as MSVC would do. This is verified by the test suite.
|
||||
|
||||
template <typename T>
|
||||
constexpr auto rshift_signed_portable(T x, int y) noexcept -> decltype(x >> y) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::numeric_limits<T>::is_signed);
|
||||
using result_type = decltype(x >> y);
|
||||
using unsigned_result_type = typename std::make_unsigned<result_type>::type;
|
||||
const unsigned_result_type roffset = static_cast<unsigned_result_type>(1) << ((sizeof(result_type) * 8) - 1);
|
||||
result_type rx = x;
|
||||
unsigned_result_type urx = static_cast<unsigned_result_type>(rx);
|
||||
urx += roffset;
|
||||
urx >>= y;
|
||||
urx -= roffset >> y;
|
||||
return static_cast<result_type>(urx);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto lshift_signed_portable(T x, int y) noexcept -> decltype(x << y) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::numeric_limits<T>::is_signed);
|
||||
using result_type = decltype(x << y);
|
||||
using unsigned_result_type = typename std::make_unsigned<result_type>::type;
|
||||
const unsigned_result_type roffset = static_cast<unsigned_result_type>(1) << ((sizeof(result_type) * 8) - 1);
|
||||
result_type rx = x;
|
||||
unsigned_result_type urx = static_cast<unsigned_result_type>(rx);
|
||||
urx += roffset;
|
||||
urx <<= y;
|
||||
urx -= roffset << y;
|
||||
return static_cast<result_type>(urx);
|
||||
}
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) || MPT_COMPILER_SHIFT_SIGNED
|
||||
|
||||
template <typename T>
|
||||
constexpr auto rshift_signed_cxx20(T x, int y) noexcept -> decltype(x >> y) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::numeric_limits<T>::is_signed);
|
||||
#if MPT_COMPILER_GCC
|
||||
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826>
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wshift-negative-value"
|
||||
#endif // MPT_COMPILER_GCC
|
||||
return x >> y;
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // MPT_COMPILER_GCC
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto lshift_signed_cxx20(T x, int y) noexcept -> decltype(x << y) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::numeric_limits<T>::is_signed);
|
||||
#if MPT_COMPILER_GCC
|
||||
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826>
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wshift-negative-value"
|
||||
#endif // MPT_COMPILER_GCC
|
||||
return x << y;
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // MPT_COMPILER_GCC
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto rshift_signed(T x, int y) noexcept -> decltype(x >> y) {
|
||||
return mpt::rshift_signed_cxx20(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto lshift_signed(T x, int y) noexcept -> decltype(x << y) {
|
||||
return mpt::lshift_signed_cxx20(x, y);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template <typename T>
|
||||
constexpr auto rshift_signed(T x, int y) noexcept -> decltype(x >> y) {
|
||||
return mpt::rshift_signed_portable(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto lshift_signed(T x, int y) noexcept -> decltype(x << y) {
|
||||
return mpt::lshift_signed_portable(x, y);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
constexpr auto arithmetic_shift_right(T x, int y) noexcept -> decltype(x >> y) {
|
||||
return mpt::rshift_signed(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto arithmetic_shift_right(T x, int y) noexcept -> decltype(x << y) {
|
||||
return mpt::lshift_signed(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto sar(T x, int y) noexcept -> decltype(x >> y) {
|
||||
return mpt::rshift_signed(x, y);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr auto sal(T x, int y) noexcept -> decltype(x << y) {
|
||||
return mpt::lshift_signed(x, y);
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_ARITHMETIC_SHIFT_HPP
|
||||
@@ -0,0 +1,92 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_ARRAY_HPP
|
||||
#define MPT_BASE_ARRAY_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct stdarray_extent : std::integral_constant<std::size_t, 0> { };
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct stdarray_extent<std::array<T, N>> : std::integral_constant<std::size_t, N> { };
|
||||
|
||||
template <typename T>
|
||||
struct is_stdarray : std::false_type { };
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct is_stdarray<std::array<T, N>> : std::true_type { };
|
||||
|
||||
// mpt::extent is the same as std::extent,
|
||||
// but also works for std::array,
|
||||
// and asserts that the given type is actually an array type instead of returning 0.
|
||||
// use as:
|
||||
// mpt::extent<decltype(expr)>()
|
||||
// mpt::extent<decltype(variable)>()
|
||||
// mpt::extent<decltype(type)>()
|
||||
// mpt::extent<type>()
|
||||
template <typename T>
|
||||
constexpr std::size_t extent() noexcept {
|
||||
using Tarray = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
||||
static_assert(std::is_array<Tarray>::value || mpt::is_stdarray<Tarray>::value);
|
||||
if constexpr (mpt::is_stdarray<Tarray>::value) {
|
||||
return mpt::stdarray_extent<Tarray>();
|
||||
} else {
|
||||
return std::extent<Tarray>();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename>
|
||||
struct array_size;
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct array_size<std::array<T, N>> {
|
||||
static constexpr std::size_t size = N;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct array_size<T[N]> {
|
||||
static constexpr std::size_t size = N;
|
||||
};
|
||||
|
||||
|
||||
template <typename T, std::size_t N, typename Tx>
|
||||
constexpr std::array<T, N> init_array(const Tx & x) {
|
||||
std::array<T, N> result{};
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
result[i] = x;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, std::size_t N, typename Fgen>
|
||||
constexpr std::array<T, N> generate_array(Fgen generator) {
|
||||
std::array<T, N> result{};
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
result[i] = generator(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_ARRAY_HPP
|
||||
@@ -0,0 +1,529 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_BIT_HPP
|
||||
#define MPT_BASE_BIT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
|
||||
#if MPT_CXX_BEFORE(20)
|
||||
#include <array>
|
||||
#endif // !C++20
|
||||
#if defined(MPT_COMPILER_QUIRK_BROKEN_BITCAST)
|
||||
#include <atomic>
|
||||
#endif
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include <bit>
|
||||
#endif // C++20
|
||||
#if MPT_CXX_BEFORE(23) || MPT_COMPILER_MSVC || MPT_LIBCXX_GNU_BEFORE(12) || MPT_LIBCXX_LLVM_BEFORE(14000)
|
||||
#include <limits>
|
||||
#endif // !C++23
|
||||
#include <type_traits>
|
||||
|
||||
#if MPT_CXX_BEFORE(20) || MPT_LIBCXX_GNU_BEFORE(11) || MPT_LIBCXX_LLVM_BEFORE(14000) || defined(MPT_COMPILER_QUIRK_BROKEN_BITCAST)
|
||||
#include <cstring>
|
||||
#endif // !C++20
|
||||
|
||||
#if MPT_CXX_BEFORE(23) && MPT_COMPILER_MSVC
|
||||
#include <intrin.h>
|
||||
#endif // !C++23
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
#if defined(MPT_COMPILER_QUIRK_BROKEN_BITCAST)
|
||||
// VS2022 17.6.0 ARM64 gets confused about alignment in std::bit_cast (or equivalent code),
|
||||
// causing an ICE with LTCG turned on.
|
||||
// We try to work-around this problem by placing signal fences as an optimization barrier around the (presumably) confused operation.
|
||||
template <typename Tdst, typename Tsrc>
|
||||
MPT_FORCEINLINE typename std::enable_if<(sizeof(Tdst) == sizeof(Tsrc)) && std::is_trivially_copyable<Tsrc>::value && std::is_trivially_copyable<Tdst>::value, Tdst>::type bit_cast(const Tsrc & src) noexcept {
|
||||
Tdst dst{};
|
||||
std::atomic_signal_fence(std::memory_order_seq_cst);
|
||||
std::memcpy(&dst, &src, sizeof(Tdst));
|
||||
std::atomic_signal_fence(std::memory_order_seq_cst);
|
||||
return dst;
|
||||
}
|
||||
#elif MPT_CXX_AT_LEAST(20) && !MPT_LIBCXX_GNU_BEFORE(11) && !MPT_LIBCXX_LLVM_BEFORE(14000)
|
||||
using std::bit_cast;
|
||||
#else // !C++20
|
||||
// C++2a compatible bit_cast.
|
||||
// Not implementing constexpr because this is not easily possible pre C++20.
|
||||
template <typename Tdst, typename Tsrc>
|
||||
MPT_FORCEINLINE typename std::enable_if<(sizeof(Tdst) == sizeof(Tsrc)) && std::is_trivially_copyable<Tsrc>::value && std::is_trivially_copyable<Tdst>::value, Tdst>::type bit_cast(const Tsrc & src) noexcept {
|
||||
Tdst dst{};
|
||||
std::memcpy(&dst, &src, sizeof(Tdst));
|
||||
return dst;
|
||||
}
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
|
||||
using std::endian;
|
||||
|
||||
static_assert(mpt::endian::big != mpt::endian::little, "platform with all scalar types having size 1 is not supported");
|
||||
|
||||
constexpr mpt::endian get_endian() noexcept {
|
||||
return mpt::endian::native;
|
||||
}
|
||||
|
||||
constexpr bool endian_is_little() noexcept {
|
||||
return get_endian() == mpt::endian::little;
|
||||
}
|
||||
|
||||
constexpr bool endian_is_big() noexcept {
|
||||
return get_endian() == mpt::endian::big;
|
||||
}
|
||||
|
||||
constexpr bool endian_is_weird() noexcept {
|
||||
return !endian_is_little() && !endian_is_big();
|
||||
}
|
||||
|
||||
#else // !C++20
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
// same definition as VS2022 C++20 in order to be compatible with debugvis
|
||||
enum class endian {
|
||||
little = 0,
|
||||
big = 1,
|
||||
weird = -1,
|
||||
native = little,
|
||||
};
|
||||
#else // !MPT_COMPILER_MSVC
|
||||
enum class endian {
|
||||
little = 0x78563412u,
|
||||
big = 0x12345678u,
|
||||
weird = 1u,
|
||||
#if MPT_COMPILER_GENERIC
|
||||
native = 0u,
|
||||
#elif defined(MPT_ARCH_LITTLE_ENDIAN)
|
||||
native = little,
|
||||
#elif defined(MPT_ARCH_BIG_ENDIAN)
|
||||
native = big,
|
||||
#else
|
||||
native = 0u,
|
||||
#endif
|
||||
};
|
||||
#endif // MPT_COMPILER_MSVC
|
||||
|
||||
static_assert(mpt::endian::big != mpt::endian::little, "platform with all scalar types having size 1 is not supported");
|
||||
|
||||
MPT_FORCEINLINE mpt::endian endian_probe() noexcept {
|
||||
using endian_probe_type = uint32;
|
||||
static_assert(sizeof(endian_probe_type) == 4);
|
||||
constexpr endian_probe_type endian_probe_big = 0x12345678u;
|
||||
constexpr endian_probe_type endian_probe_little = 0x78563412u;
|
||||
const std::array<std::byte, sizeof(endian_probe_type)> probe{
|
||||
{std::byte{0x12}, std::byte{0x34}, std::byte{0x56}, std::byte{0x78}}
|
||||
};
|
||||
const endian_probe_type test = mpt::bit_cast<endian_probe_type>(probe);
|
||||
mpt::endian result = mpt::endian::native;
|
||||
switch (test) {
|
||||
case endian_probe_big:
|
||||
result = mpt::endian::big;
|
||||
break;
|
||||
case endian_probe_little:
|
||||
result = mpt::endian::little;
|
||||
break;
|
||||
default:
|
||||
result = mpt::endian::weird;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE mpt::endian get_endian() noexcept {
|
||||
#if MPT_COMPILER_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 6285) // false-positive: (<non-zero constant> || <non-zero constant>) is always a non-zero constant.
|
||||
#endif // MPT_COMPILER_MSVC
|
||||
if constexpr ((mpt::endian::native == mpt::endian::little) || (mpt::endian::native == mpt::endian::big)) {
|
||||
return mpt::endian::native;
|
||||
} else {
|
||||
return mpt::endian_probe();
|
||||
}
|
||||
#if MPT_COMPILER_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif // MPT_COMPILER_MSVC
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE bool endian_is_little() noexcept {
|
||||
return get_endian() == mpt::endian::little;
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE bool endian_is_big() noexcept {
|
||||
return get_endian() == mpt::endian::big;
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE bool endian_is_weird() noexcept {
|
||||
return !endian_is_little() && !endian_is_big();
|
||||
}
|
||||
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && MPT_MSVC_AT_LEAST(2022, 1) && !MPT_LIBCXX_GNU_BEFORE(10) && !MPT_LIBCXX_LLVM_BEFORE(12000)
|
||||
|
||||
// Disabled for VS2022.0 because of
|
||||
// <https://developercommunity.visualstudio.com/t/vs2022-cl-193030705-generates-non-universally-avai/1578571>
|
||||
// / <https://github.com/microsoft/STL/issues/2330> with fix already queued
|
||||
// (<https://github.com/microsoft/STL/pull/2333>).
|
||||
|
||||
using std::bit_ceil;
|
||||
using std::bit_floor;
|
||||
using std::bit_width;
|
||||
using std::countl_one;
|
||||
using std::countl_zero;
|
||||
using std::countr_one;
|
||||
using std::countr_zero;
|
||||
using std::has_single_bit;
|
||||
using std::popcount;
|
||||
using std::rotl;
|
||||
using std::rotr;
|
||||
|
||||
#else // !C++20
|
||||
|
||||
// C++20 <bit> header.
|
||||
// Note that we do not use SFINAE here but instead rely on static_assert.
|
||||
|
||||
template <typename T>
|
||||
constexpr int popcount(T val) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
int result = 0;
|
||||
while (val > 0) {
|
||||
if (val & 0x1) {
|
||||
result++;
|
||||
}
|
||||
val >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr bool has_single_bit(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
return mpt::popcount(x) == 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T bit_ceil(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
T result = 1;
|
||||
while (result < x) {
|
||||
T newresult = result << 1;
|
||||
if (newresult < result) {
|
||||
return 0;
|
||||
}
|
||||
result = newresult;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T bit_floor(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
if (x == 0) {
|
||||
return 0;
|
||||
}
|
||||
T result = 1;
|
||||
do {
|
||||
T newresult = result << 1;
|
||||
if (newresult < result) {
|
||||
return result;
|
||||
}
|
||||
result = newresult;
|
||||
} while (result <= x);
|
||||
return result >> 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr int bit_width(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
int result = 0;
|
||||
while (x > 0) {
|
||||
x >>= 1;
|
||||
result += 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr int countl_zero(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
int count = 0;
|
||||
for (int bit = std::numeric_limits<T>::digits - 1; bit >= 0; --bit) {
|
||||
if ((x & (1u << bit)) == 0u) {
|
||||
count++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr int countl_one(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
int count = 0;
|
||||
for (int bit = std::numeric_limits<T>::digits - 1; bit >= 0; --bit) {
|
||||
if ((x & (1u << bit)) != 0u) {
|
||||
count++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr int countr_zero(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
int count = 0;
|
||||
for (int bit = 0; bit < std::numeric_limits<T>::digits; ++bit) {
|
||||
if ((x & (1u << bit)) == 0u) {
|
||||
count++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr int countr_one(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
int count = 0;
|
||||
for (int bit = 0; bit < std::numeric_limits<T>::digits; ++bit) {
|
||||
if ((x & (1u << bit)) != 0u) {
|
||||
count++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T rotl_impl(T x, int r) noexcept {
|
||||
auto N = std::numeric_limits<T>::digits;
|
||||
return (x >> (N - r)) | (x << r);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T rotr_impl(T x, int r) noexcept {
|
||||
auto N = std::numeric_limits<T>::digits;
|
||||
return (x << (N - r)) | (x >> r);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T rotl(T x, int s) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
auto N = std::numeric_limits<T>::digits;
|
||||
auto r = s % N;
|
||||
return (s < 0) ? mpt::rotr_impl(x, -s) : ((x >> (N - r)) | (x << r));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr T rotr(T x, int s) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(std::is_unsigned<T>::value);
|
||||
auto N = std::numeric_limits<T>::digits;
|
||||
auto r = s % N;
|
||||
return (s < 0) ? mpt::rotl_impl(x, -s) : ((x << (N - r)) | (x >> r));
|
||||
}
|
||||
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(23) && !MPT_LIBCXX_GNU_BEFORE(12) && !MPT_LIBCXX_LLVM_BEFORE(14000) && !MPT_MSVC_BEFORE(2022, 1)
|
||||
|
||||
using std::byteswap;
|
||||
|
||||
#else // !C++23
|
||||
|
||||
constexpr inline uint16 byteswap_impl_constexpr16(uint16 x) noexcept {
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wconversion"
|
||||
#endif // MPT_COMPILER_GCC
|
||||
return uint16(0)
|
||||
| ((x >> 8) & 0x00FFu)
|
||||
| ((x << 8) & 0xFF00u);
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // MPT_COMPILER_GCC
|
||||
}
|
||||
|
||||
constexpr inline uint32 byteswap_impl_constexpr32(uint32 x) noexcept {
|
||||
return uint32(0)
|
||||
| ((x & 0x000000FFu) << 24)
|
||||
| ((x & 0x0000FF00u) << 8)
|
||||
| ((x & 0x00FF0000u) >> 8)
|
||||
| ((x & 0xFF000000u) >> 24);
|
||||
}
|
||||
|
||||
constexpr inline uint64 byteswap_impl_constexpr64(uint64 x) noexcept {
|
||||
return uint64(0)
|
||||
| (((x >> 0) & 0xffull) << 56)
|
||||
| (((x >> 8) & 0xffull) << 48)
|
||||
| (((x >> 16) & 0xffull) << 40)
|
||||
| (((x >> 24) & 0xffull) << 32)
|
||||
| (((x >> 32) & 0xffull) << 24)
|
||||
| (((x >> 40) & 0xffull) << 16)
|
||||
| (((x >> 48) & 0xffull) << 8)
|
||||
| (((x >> 56) & 0xffull) << 0);
|
||||
}
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
// Clang also supports these,
|
||||
// however <https://github.com/llvm/llvm-project/issues/58470>.
|
||||
#define MPT_byteswap_impl16 __builtin_bswap16
|
||||
#define MPT_byteswap_impl32 __builtin_bswap32
|
||||
#define MPT_byteswap_impl64 __builtin_bswap64
|
||||
#elif MPT_COMPILER_MSVC
|
||||
#define MPT_byteswap_impl16 _byteswap_ushort
|
||||
#define MPT_byteswap_impl32 _byteswap_ulong
|
||||
#define MPT_byteswap_impl64 _byteswap_uint64
|
||||
#endif
|
||||
|
||||
// No intrinsics available
|
||||
#ifndef MPT_byteswap_impl16
|
||||
#define MPT_byteswap_impl16(x) byteswap_impl_constexpr16(x)
|
||||
#endif
|
||||
#ifndef MPT_byteswap_impl32
|
||||
#define MPT_byteswap_impl32(x) byteswap_impl_constexpr32(x)
|
||||
#endif
|
||||
#ifndef MPT_byteswap_impl64
|
||||
#define MPT_byteswap_impl64(x) byteswap_impl_constexpr64(x)
|
||||
#endif
|
||||
|
||||
MPT_CONSTEXPR20_FUN uint64 byteswap_impl(uint64 value) noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
return byteswap_impl_constexpr64(value);
|
||||
} else {
|
||||
return MPT_byteswap_impl64(value);
|
||||
}
|
||||
}
|
||||
|
||||
MPT_CONSTEXPR20_FUN uint32 byteswap_impl(uint32 value) noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
return byteswap_impl_constexpr32(value);
|
||||
} else {
|
||||
return MPT_byteswap_impl32(value);
|
||||
}
|
||||
}
|
||||
|
||||
MPT_CONSTEXPR20_FUN uint16 byteswap_impl(uint16 value) noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
return byteswap_impl_constexpr16(value);
|
||||
} else {
|
||||
return MPT_byteswap_impl16(value);
|
||||
}
|
||||
}
|
||||
|
||||
MPT_CONSTEXPR20_FUN int64 byteswap_impl(int64 value) noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
return byteswap_impl_constexpr64(value);
|
||||
} else {
|
||||
return MPT_byteswap_impl64(value);
|
||||
}
|
||||
}
|
||||
|
||||
MPT_CONSTEXPR20_FUN int32 byteswap_impl(int32 value) noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
return byteswap_impl_constexpr32(value);
|
||||
} else {
|
||||
return MPT_byteswap_impl32(value);
|
||||
}
|
||||
}
|
||||
|
||||
MPT_CONSTEXPR20_FUN int16 byteswap_impl(int16 value) noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
return byteswap_impl_constexpr16(value);
|
||||
} else {
|
||||
return MPT_byteswap_impl16(value);
|
||||
}
|
||||
}
|
||||
|
||||
// Do NOT remove these overloads, even if they seem useless.
|
||||
// We do not want risking to extend 8bit integers to int and then
|
||||
// endian-converting and casting back to int.
|
||||
// Thus these overloads.
|
||||
|
||||
constexpr inline uint8 byteswap_impl(uint8 value) noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
constexpr inline int8 byteswap_impl(int8 value) noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
constexpr inline char byteswap_impl(char value) noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
#undef MPT_byteswap_impl16
|
||||
#undef MPT_byteswap_impl32
|
||||
#undef MPT_byteswap_impl64
|
||||
|
||||
template <typename T>
|
||||
constexpr T byteswap(T x) noexcept {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return byteswap_impl(x);
|
||||
}
|
||||
|
||||
#endif // C++23
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
constexpr int lower_bound_entropy_bits(T x_) {
|
||||
typename std::make_unsigned<T>::type x = static_cast<typename std::make_unsigned<T>::type>(x_);
|
||||
return (static_cast<unsigned int>(mpt::bit_width(x)) == static_cast<typename std::make_unsigned<T>::type>(mpt::popcount(x))) ? mpt::bit_width(x) : mpt::bit_width(x) - 1;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
constexpr bool is_mask(T x) {
|
||||
static_assert(std::is_integral<T>::value);
|
||||
typedef typename std::make_unsigned<T>::type unsigned_T;
|
||||
unsigned_T ux = static_cast<unsigned_T>(x);
|
||||
unsigned_T mask = 0;
|
||||
for (std::size_t bits = 0; bits <= (sizeof(unsigned_T) * 8); ++bits) {
|
||||
mask = (mask << 1) | 1u;
|
||||
if (ux == mask) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_BIT_HPP
|
||||
@@ -0,0 +1,97 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_CHECK_PLATFORM_HPP
|
||||
#define MPT_BASE_CHECK_PLATFORM_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/pointer.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#if MPT_LIBC_DJGPP
|
||||
#include <crt0.h>
|
||||
#endif // MPT_LIBC_DJGPP
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
static_assert(sizeof(std::uintptr_t) == sizeof(void *));
|
||||
static_assert(std::numeric_limits<unsigned char>::digits == 8);
|
||||
|
||||
static_assert(sizeof(char) == 1);
|
||||
|
||||
static_assert(sizeof(std::byte) == 1);
|
||||
static_assert(alignof(std::byte) == 1);
|
||||
|
||||
static_assert(mpt::arch_bits == static_cast<int>(mpt::pointer_size) * 8);
|
||||
|
||||
|
||||
#if MPT_LIBC_DJGPP
|
||||
|
||||
namespace platform {
|
||||
|
||||
namespace detail {
|
||||
|
||||
struct libc_checker {
|
||||
private:
|
||||
bool m_libc_check_failure = false;
|
||||
bool m_libc_has_implicit_code_locking = false;
|
||||
public:
|
||||
[[nodiscard]] inline libc_checker() {
|
||||
assert(((_crt0_startup_flags & _CRT0_FLAG_NONMOVE_SBRK) == _CRT0_FLAG_NONMOVE_SBRK) && ((_crt0_startup_flags & _CRT0_FLAG_UNIX_SBRK) == 0));
|
||||
if (((_crt0_startup_flags & _CRT0_FLAG_NONMOVE_SBRK) == _CRT0_FLAG_NONMOVE_SBRK) && ((_crt0_startup_flags & _CRT0_FLAG_UNIX_SBRK) != 0)) {
|
||||
m_libc_check_failure = true;
|
||||
}
|
||||
assert((_crt0_startup_flags & _CRT0_DISABLE_SBRK_ADDRESS_WRAP) == _CRT0_DISABLE_SBRK_ADDRESS_WRAP);
|
||||
if ((_crt0_startup_flags & _CRT0_DISABLE_SBRK_ADDRESS_WRAP) != _CRT0_DISABLE_SBRK_ADDRESS_WRAP) {
|
||||
m_libc_check_failure = true;
|
||||
}
|
||||
if ((_crt0_startup_flags & _CRT0_FLAG_LOCK_MEMORY) == _CRT0_FLAG_LOCK_MEMORY) {
|
||||
m_libc_has_implicit_code_locking = true;
|
||||
}
|
||||
}
|
||||
libc_checker(const libc_checker &) = delete;
|
||||
libc_checker & operator=(const libc_checker &) = delete;
|
||||
[[nodiscard]] inline bool is_ok() const noexcept {
|
||||
return !m_libc_check_failure;
|
||||
}
|
||||
[[nodiscard]] inline bool has_implicit_code_locking() const noexcept {
|
||||
return m_libc_has_implicit_code_locking;
|
||||
}
|
||||
};
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif // MPT_COMPILER_CLANG
|
||||
inline const libc_checker g_libc_checker;
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif // MPT_COMPILER_CLANG
|
||||
|
||||
} // namespace detail
|
||||
|
||||
[[nodiscard]] inline const mpt::platform::detail::libc_checker & libc() noexcept {
|
||||
return mpt::platform::detail::g_libc_checker;
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
|
||||
#endif // MPT_LIBC_DJGPP
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_CHECK_PLATFORM_HPP
|
||||
@@ -0,0 +1,42 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_COMPILETIME_WARNING_HPP
|
||||
#define MPT_BASE_COMPILETIME_WARNING_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/preprocessor.hpp"
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
|
||||
#define MPT_WARNING(text) __pragma(message(__FILE__ "(" MPT_PP_DEFER(MPT_PP_STRINGIFY, __LINE__) "): Warning: " text))
|
||||
#define MPT_WARNING_STATEMENT(text) __pragma(message(__FILE__ "(" MPT_PP_DEFER(MPT_PP_STRINGIFY, __LINE__) "): Warning: " text))
|
||||
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
|
||||
#define MPT_WARNING(text) _Pragma(MPT_PP_STRINGIFY(GCC warning text))
|
||||
#define MPT_WARNING_STATEMENT(text) _Pragma(MPT_PP_STRINGIFY(GCC warning text))
|
||||
|
||||
#else
|
||||
|
||||
// portable #pragma message or #warning replacement
|
||||
#define MPT_WARNING(text) \
|
||||
static inline int MPT_PP_UNIQUE_IDENTIFIER(MPT_WARNING_NAME)() noexcept { \
|
||||
int warning [[deprecated("Warning: " text)]] = 0; \
|
||||
return warning; \
|
||||
} \
|
||||
/**/
|
||||
#define MPT_WARNING_STATEMENT(text) \
|
||||
int MPT_PP_UNIQUE_IDENTIFIER(MPT_WARNING_NAME) = []() { \
|
||||
int warning [[deprecated("Warning: " text)]] = 0; \
|
||||
return warning; \
|
||||
}() /**/
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_COMPILETIME_WARNING_HPP
|
||||
@@ -0,0 +1,56 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_CONSTEXPR_THROW_HPP
|
||||
#define MPT_BASE_CONSTEXPR_THROW_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
// Work-around for the requirement of at least 1 non-throwing function argument combination in C++ (17,2a).
|
||||
|
||||
template <typename Exception>
|
||||
constexpr bool constexpr_throw_helper(Exception && e, bool really = true) {
|
||||
//return !really ? really : throw std::forward<Exception>(e);
|
||||
if (really) {
|
||||
throw std::forward<Exception>(e);
|
||||
}
|
||||
// cppcheck-suppress identicalConditionAfterEarlyExit
|
||||
return really;
|
||||
}
|
||||
|
||||
template <typename Exception>
|
||||
constexpr bool constexpr_throw(Exception && e) {
|
||||
return mpt::constexpr_throw_helper(std::forward<Exception>(e));
|
||||
}
|
||||
|
||||
template <typename T, typename Exception>
|
||||
constexpr T constexpr_throw_helper(Exception && e, bool really = true) {
|
||||
//return !really ? really : throw std::forward<Exception>(e);
|
||||
if (really) {
|
||||
throw std::forward<Exception>(e);
|
||||
}
|
||||
return T{};
|
||||
}
|
||||
|
||||
template <typename T, typename Exception>
|
||||
constexpr T constexpr_throw(Exception && e) {
|
||||
return mpt::constexpr_throw_helper<T>(std::forward<Exception>(e));
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_CONSTEXPR_THROW_HPP
|
||||
@@ -0,0 +1,228 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DEBUGGING_HPP
|
||||
#define MPT_BASE_DEBUGGING_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
|
||||
#if MPT_CXX_AT_LEAST(26)
|
||||
#include <debugging>
|
||||
#endif // C++26
|
||||
|
||||
#if MPT_CXX_BEFORE(26)
|
||||
#if MPT_OS_LINUX
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#endif
|
||||
#endif // !C++26
|
||||
|
||||
#if MPT_CXX_BEFORE(26)
|
||||
#if MPT_OS_LINUX
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#endif // !C++26
|
||||
|
||||
#if MPT_CXX_BEFORE(26)
|
||||
#if MPT_OS_WINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#endif // !C++26
|
||||
|
||||
#if MPT_CXX_BEFORE(26)
|
||||
#if MPT_COMPILER_MSVC
|
||||
#include <intrin.h>
|
||||
#endif
|
||||
#endif // !C++26
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(26)
|
||||
|
||||
using std::breakpoint;
|
||||
using std::breakpoint_if_debugging;
|
||||
using std::is_debugger_present;
|
||||
|
||||
#else // !C++26
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
|
||||
inline bool is_debugger_present() noexcept {
|
||||
return (IsDebuggerPresent() != FALSE);
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE void breakpoint() noexcept {
|
||||
#if MPT_COMPILER_MSVC
|
||||
__debugbreak();
|
||||
#elif MPT_COMPILER_CLANG
|
||||
__builtin_debugtrap();
|
||||
#elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
|
||||
__asm__ __volatile__("int 3");
|
||||
#else
|
||||
DebugBreak();
|
||||
#endif
|
||||
}
|
||||
|
||||
#elif MPT_OS_LINUX
|
||||
|
||||
namespace detail {
|
||||
namespace debugging {
|
||||
|
||||
inline bool parse_proc_status_line(char * buf, std::size_t size) noexcept {
|
||||
if (std::strncmp(buf, "TracerPid:\t", 11) != 0) {
|
||||
return false;
|
||||
}
|
||||
unsigned long long pid = 0;
|
||||
std::size_t pos = 11;
|
||||
while (pos < size) {
|
||||
unsigned char byte = static_cast<unsigned char>(buf[pos]);
|
||||
if (!(static_cast<unsigned char>('0') <= byte && byte <= static_cast<unsigned char>('9'))) {
|
||||
return false;
|
||||
}
|
||||
uint8 digit = static_cast<uint8>(byte - static_cast<unsigned char>('0'));
|
||||
pid = (pid * 10) + digit;
|
||||
pos++;
|
||||
}
|
||||
return (pid != 0);
|
||||
}
|
||||
|
||||
inline bool parse_proc_status() noexcept {
|
||||
int olderrno = errno;
|
||||
bool detected_debugger = false;
|
||||
int error = 0;
|
||||
int fd = -1;
|
||||
bool open_done = false;
|
||||
while (!open_done) {
|
||||
int res = open("/proc/self/status", O_RDONLY);
|
||||
if (res >= 0) {
|
||||
fd = res;
|
||||
open_done = true;
|
||||
} else if (errno != EINTR) {
|
||||
error = errno;
|
||||
open_done = true;
|
||||
}
|
||||
}
|
||||
if (error != 0) {
|
||||
errno = olderrno;
|
||||
return false;
|
||||
}
|
||||
if (fd < 0) {
|
||||
errno = olderrno;
|
||||
return false;
|
||||
}
|
||||
bool eof = false;
|
||||
uint8 iobuf[1024];
|
||||
std::size_t iobuf_size = 0;
|
||||
char linebuf[128];
|
||||
std::size_t linebuf_size = 0;
|
||||
while (!eof) {
|
||||
ssize_t bytes_read = read(fd, iobuf, 1024);
|
||||
if (bytes_read == -1) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
} else if (bytes_read == 0) {
|
||||
eof = true;
|
||||
}
|
||||
iobuf_size = static_cast<std::size_t>(bytes_read);
|
||||
for (std::size_t i = 0; i < iobuf_size; ++i) {
|
||||
if (static_cast<unsigned char>(iobuf[i]) == static_cast<unsigned char>('\n')) {
|
||||
if (parse_proc_status_line(linebuf, linebuf_size)) {
|
||||
detected_debugger = true;
|
||||
}
|
||||
linebuf_size = 0;
|
||||
} else {
|
||||
if (linebuf_size < 128) {
|
||||
linebuf[linebuf_size] = static_cast<char>(static_cast<unsigned char>(iobuf[i]));
|
||||
linebuf_size++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (linebuf_size > 0) {
|
||||
if (parse_proc_status_line(linebuf, linebuf_size)) {
|
||||
detected_debugger = true;
|
||||
}
|
||||
linebuf_size = 0;
|
||||
}
|
||||
}
|
||||
bool close_done = false;
|
||||
while (!close_done) {
|
||||
int res = close(fd);
|
||||
if (res == 0) {
|
||||
fd = -1;
|
||||
close_done = true;
|
||||
} else if (errno != EINTR) {
|
||||
error = errno;
|
||||
close_done = true;
|
||||
}
|
||||
}
|
||||
if (error != 0) {
|
||||
errno = olderrno;
|
||||
return false;
|
||||
}
|
||||
errno = olderrno;
|
||||
return detected_debugger;
|
||||
}
|
||||
|
||||
} // namespace debugging
|
||||
} // namespace detail
|
||||
|
||||
inline MPT_NOINLINE bool is_debugger_present() noexcept {
|
||||
return mpt::detail::debugging::parse_proc_status();
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE void breakpoint() noexcept {
|
||||
#if MPT_COMPILER_CLANG
|
||||
__builtin_debugtrap();
|
||||
#elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
|
||||
__asm__ __volatile__("int 3");
|
||||
#else
|
||||
kill(getpid(), SIGTRAP);
|
||||
#endif
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
inline bool is_debugger_present() noexcept {
|
||||
return false;
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE void breakpoint() noexcept {
|
||||
#if MPT_COMPILER_MSVC
|
||||
__debugbreak();
|
||||
#elif MPT_COMPILER_CLANG
|
||||
__builtin_debugtrap();
|
||||
#elif MPT_COMPILER_GCC && (MPT_ARCH_X86 || MPT_ARCH_AMD64)
|
||||
__asm__ __volatile__("int 3");
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
MPT_FORCEINLINE void breakpoint_if_debugging() noexcept {
|
||||
if (mpt::is_debugger_present()) {
|
||||
mpt::breakpoint();
|
||||
}
|
||||
}
|
||||
|
||||
#endif // C++26
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DEBUGGING_HPP
|
||||
@@ -0,0 +1,17 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_HPP
|
||||
#define MPT_BASE_DETECT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_arch.hpp"
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/detect_libc.hpp"
|
||||
#include "mpt/base/detect_libcxx.hpp"
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
#include "mpt/base/detect_quirks.hpp"
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_HPP
|
||||
@@ -0,0 +1,520 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_ARCH_HPP
|
||||
#define MPT_BASE_DETECT_ARCH_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
|
||||
|
||||
|
||||
// The order of the checks matters!
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_GENERIC
|
||||
|
||||
|
||||
|
||||
#define MPT_ARCH_GENERIC 1
|
||||
|
||||
|
||||
|
||||
#elif MPT_COMPILER_MSVC
|
||||
|
||||
|
||||
|
||||
#if defined(_M_ARM64) || defined(_M_ARM64EC)
|
||||
#define MPT_ARCH_AARCH64 1
|
||||
|
||||
#elif defined(_M_ARM)
|
||||
#define MPT_ARCH_ARM 1
|
||||
|
||||
#elif defined(_M_AMD64) || defined(_M_X64)
|
||||
#define MPT_ARCH_AMD64 1
|
||||
|
||||
#elif defined(_M_IX86)
|
||||
#define MPT_ARCH_X86 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
|
||||
|
||||
|
||||
#if defined(__wasm64__) || defined(__wasm64)
|
||||
#define MPT_ARCH_WASM64 1
|
||||
|
||||
#elif defined(__wasm32__) || defined(__wasm32)
|
||||
#define MPT_ARCH_WASM32 1
|
||||
|
||||
#elif defined(__aarch64__)
|
||||
#define MPT_ARCH_AARCH64 1
|
||||
|
||||
#elif defined(__arm__)
|
||||
#define MPT_ARCH_ARM 1
|
||||
|
||||
#elif defined(__amd64__) || defined(__x86_64__)
|
||||
#define MPT_ARCH_AMD64 1
|
||||
|
||||
#elif defined(__i386__) || defined(_X86_)
|
||||
#define MPT_ARCH_X86 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#else // MPT_COMPILER
|
||||
|
||||
|
||||
|
||||
#if defined(__wasm64__) || defined(__wasm64)
|
||||
#define MPT_ARCH_WASM64 1
|
||||
|
||||
#elif defined(__wasm32__) || defined(__wasm32)
|
||||
#define MPT_ARCH_WASM32 1
|
||||
|
||||
#elif defined(__aarch64__) || defined(_M_ARM64)
|
||||
#define MPT_ARCH_AARCH64 1
|
||||
|
||||
#elif defined(__arm__) || defined(_M_ARM)
|
||||
#define MPT_ARCH_ARM 1
|
||||
|
||||
#elif defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64) || defined(__amd64) || defined(__x86_64)
|
||||
#define MPT_ARCH_AMD64 1
|
||||
|
||||
#elif defined(__i386__) || defined(_X86_) || defined(_M_IX86) || defined(__i386) || defined(__X86__)
|
||||
#define MPT_ARCH_X86 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // MPT_COMPILER
|
||||
|
||||
|
||||
|
||||
#ifndef MPT_ARCH_GENERIC
|
||||
#define MPT_ARCH_GENERIC 0
|
||||
#endif
|
||||
#ifndef MPT_ARCH_WASM64
|
||||
#define MPT_ARCH_WASM64 0
|
||||
#endif
|
||||
#ifndef MPT_ARCH_WASM32
|
||||
#define MPT_ARCH_WASM32 0
|
||||
#endif
|
||||
#ifndef MPT_ARCH_AARCH64
|
||||
#define MPT_ARCH_AARCH64 0
|
||||
#endif
|
||||
#ifndef MPT_ARCH_ARM
|
||||
#define MPT_ARCH_ARM 0
|
||||
#endif
|
||||
#ifndef MPT_ARCH_AMD64
|
||||
#define MPT_ARCH_AMD64 0
|
||||
#endif
|
||||
#ifndef MPT_ARCH_X86
|
||||
#define MPT_ARCH_X86 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if !MPT_COMPILER_GENERIC
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
#define MPT_ARCH_LITTLE_ENDIAN
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define MPT_ARCH_BIG_ENDIAN
|
||||
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define MPT_ARCH_LITTLE_ENDIAN
|
||||
#endif
|
||||
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && defined(__ORDER_LITTLE_ENDIAN__)
|
||||
#if __ORDER_BIG_ENDIAN__ != __ORDER_LITTLE_ENDIAN__
|
||||
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
#define MPT_ARCH_BIG_ENDIAN
|
||||
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
#define MPT_ARCH_LITTLE_ENDIAN
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// fallback:
|
||||
#if !defined(MPT_ARCH_BIG_ENDIAN) && !defined(MPT_ARCH_LITTLE_ENDIAN)
|
||||
#if (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) \
|
||||
|| (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) \
|
||||
|| (defined(_STLP_BIG_ENDIAN) && !defined(_STLP_LITTLE_ENDIAN))
|
||||
#define MPT_ARCH_BIG_ENDIAN
|
||||
#elif (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) \
|
||||
|| (defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) \
|
||||
|| (defined(_STLP_LITTLE_ENDIAN) && !defined(_STLP_BIG_ENDIAN))
|
||||
#define MPT_ARCH_LITTLE_ENDIAN
|
||||
#elif defined(__hpux) || defined(__hppa) \
|
||||
|| defined(_MIPSEB) \
|
||||
|| defined(__s390__)
|
||||
#define MPT_ARCH_BIG_ENDIAN
|
||||
#elif defined(__i386__) || defined(_M_IX86) \
|
||||
|| defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) \
|
||||
|| defined(__bfin__)
|
||||
#define MPT_ARCH_LITTLE_ENDIAN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // !MPT_COMPILER_GENERIC
|
||||
|
||||
|
||||
|
||||
// compiler assumed instruction set support
|
||||
|
||||
// clang-format off
|
||||
|
||||
#if MPT_ARCH_X86 || MPT_ARCH_AMD64
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
|
||||
#if defined(_M_X64)
|
||||
#define MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#define MPT_ARCH_X86_I486
|
||||
#define MPT_ARCH_X86_CPUID
|
||||
#define MPT_ARCH_X86_TSC
|
||||
#define MPT_ARCH_X86_CX8
|
||||
#define MPT_ARCH_X86_CMOV
|
||||
#define MPT_ARCH_X86_MMX
|
||||
#define MPT_ARCH_X86_MMXEXT
|
||||
#define MPT_ARCH_X86_FXSR
|
||||
#define MPT_ARCH_X86_SSE
|
||||
#define MPT_ARCH_X86_SSE2
|
||||
#elif defined(_M_IX86) && defined(_M_IX86_FP)
|
||||
#if (_M_IX86_FP >= 2)
|
||||
#define MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#define MPT_ARCH_X86_I486
|
||||
#define MPT_ARCH_X86_CPUID
|
||||
#define MPT_ARCH_X86_TSC
|
||||
#define MPT_ARCH_X86_CX8
|
||||
#define MPT_ARCH_X86_CMOV
|
||||
#define MPT_ARCH_X86_MMX
|
||||
#define MPT_ARCH_X86_MMXEXT
|
||||
#define MPT_ARCH_X86_FXSR
|
||||
#define MPT_ARCH_X86_SSE
|
||||
#define MPT_ARCH_X86_SSE2
|
||||
#elif (_M_IX86_FP == 1)
|
||||
#define MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#define MPT_ARCH_X86_I486
|
||||
#define MPT_ARCH_X86_CPUID
|
||||
#define MPT_ARCH_X86_TSC
|
||||
#define MPT_ARCH_X86_CX8
|
||||
#define MPT_ARCH_X86_CMOV
|
||||
#define MPT_ARCH_X86_MMX
|
||||
#define MPT_ARCH_X86_MMXEXT
|
||||
#define MPT_ARCH_X86_FXSR
|
||||
#define MPT_ARCH_X86_SSE
|
||||
#elif MPT_MSVC_AT_LEAST(2008, 0)
|
||||
#define MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#define MPT_ARCH_X86_I486
|
||||
#define MPT_ARCH_X86_CPUID
|
||||
#define MPT_ARCH_X86_TSC
|
||||
#define MPT_ARCH_X86_CX8
|
||||
#elif MPT_MSVC_AT_LEAST(2005, 0)
|
||||
#define MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#define MPT_ARCH_X86_I486
|
||||
#elif MPT_MSVC_AT_LEAST(1998, 0)
|
||||
#define MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#else
|
||||
#define MPT_ARCH_X86_I386
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__AVX__)
|
||||
#define MPT_ARCH_X86_3DNOWPREFETCH
|
||||
#ifndef MPT_ARCH_X86_XSAVE
|
||||
#define MPT_ARCH_X86_XSAVE
|
||||
#endif
|
||||
#define MPT_ARCH_X86_AVX
|
||||
#endif
|
||||
#if defined(__AVX2__)
|
||||
#ifndef MPT_ARCH_X86_XSAVE
|
||||
#define MPT_ARCH_X86_XSAVE
|
||||
#endif
|
||||
#define MPT_ARCH_X86_AVX2
|
||||
#define MPT_ARCH_X86_FMA
|
||||
#define MPT_ARCH_X86_BMI1
|
||||
#endif
|
||||
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
|
||||
#define MPT_ARCH_X86_I386
|
||||
#if !defined(_SOFT_FLOAT)
|
||||
#define MPT_ARCH_X86_FPU
|
||||
// GCC does not provide a macro for FSIN. Deduce it from 486 later.
|
||||
#endif
|
||||
#if defined(__i486__)
|
||||
// GCC does not consistently provide i486, deduce it later from cpuid.
|
||||
#define MPT_ARCH_X86_I486
|
||||
#endif
|
||||
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
|
||||
// GCC does not provide TSC or CPUID.
|
||||
// Imply it by CX8.
|
||||
#define MPT_ARCH_X86_CX8
|
||||
#define MPT_ARCH_X86_TSC
|
||||
#define MPT_ARCH_X86_CPUID
|
||||
#endif
|
||||
#if defined(__i686__) || defined(__athlon__)
|
||||
// GCC is broken here and does not set __i686__ for various non-Intel and even modern Intel CPUs
|
||||
// Imply __i686__ by __SSE__ as a work-around.
|
||||
#define MPT_ARCH_X86_CMOV
|
||||
#endif
|
||||
#if defined(MPT_ARCH_X86_CPUID)
|
||||
#ifndef MPT_ARCH_X86_I486
|
||||
#define MPT_ARCH_X86_I486
|
||||
#endif
|
||||
#endif
|
||||
#if defined(MPT_ARCH_X86_I486) && defined(MPT_ARCH_X86_FPU)
|
||||
#define MPT_ARCH_X86_FSIN
|
||||
#endif
|
||||
#ifdef __MMX__
|
||||
#define MPT_ARCH_X86_MMX
|
||||
#endif
|
||||
#ifdef __3dNOW__
|
||||
#define MPT_ARCH_X86_3DNOW
|
||||
#endif
|
||||
#ifdef __3dNOW_A__
|
||||
#define MPT_ARCH_X86_MMXEXT
|
||||
#define MPT_ARCH_X86_3DNOWEXT
|
||||
#endif
|
||||
#ifdef __PRFCHW__
|
||||
#define MPT_ARCH_X86_3DNOWPREFETCH
|
||||
#endif
|
||||
#ifdef __FXSR__
|
||||
#define MPT_ARCH_X86_FXSR
|
||||
#endif
|
||||
#ifdef __SSE__
|
||||
#ifndef MPT_ARCH_X86_MMXEXT
|
||||
#define MPT_ARCH_X86_MMXEXT
|
||||
#endif
|
||||
#define MPT_ARCH_X86_SSE
|
||||
#ifndef MPT_ARCH_X86_CMOV
|
||||
#define MPT_ARCH_X86_CMOV
|
||||
#endif
|
||||
#endif
|
||||
#ifdef __SSE2__
|
||||
#define MPT_ARCH_X86_SSE2
|
||||
#endif
|
||||
#ifdef __SSE3__
|
||||
#define MPT_ARCH_X86_SSE3
|
||||
#endif
|
||||
#ifdef __SSSE3__
|
||||
#define MPT_ARCH_X86_SSSE3
|
||||
#endif
|
||||
#ifdef __SSE4_1__
|
||||
#define MPT_ARCH_X86_SSE4_1
|
||||
#endif
|
||||
#ifdef __SSE4_2__
|
||||
#define MPT_ARCH_X86_SSE4_2
|
||||
#endif
|
||||
#ifdef __XSAVE__
|
||||
#define MPT_ARCH_X86_XSAVE
|
||||
#endif
|
||||
#ifdef __AVX__
|
||||
#define MPT_ARCH_X86_AVX
|
||||
#endif
|
||||
#ifdef __AVX2__
|
||||
#define MPT_ARCH_X86_AVX2
|
||||
#endif
|
||||
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16
|
||||
#define MPT_ARCH_X86_CX16
|
||||
#endif
|
||||
#ifdef __LAHF_SAHF__
|
||||
#define MPT_ARCH_X86_LAHF
|
||||
#endif
|
||||
#ifdef __POPCNT__
|
||||
#define MPT_ARCH_X86_POPCNT
|
||||
#endif
|
||||
#ifdef __BMI__
|
||||
#define MPT_ARCH_X86_BMI1
|
||||
#endif
|
||||
#ifdef __BMI2__
|
||||
#define MPT_ARCH_X86_BMI2
|
||||
#endif
|
||||
#ifdef __F16C__
|
||||
#define MPT_ARCH_X86_F16C
|
||||
#endif
|
||||
#ifdef __FMA__
|
||||
#define MPT_ARCH_X86_FMA
|
||||
#endif
|
||||
#ifdef __LZCNT__
|
||||
#define MPT_ARCH_X86_LZCNT
|
||||
#endif
|
||||
#ifdef __MOVBE__
|
||||
#define MPT_ARCH_X86_MOVBE
|
||||
#endif
|
||||
|
||||
#endif // MPT_COMPILER
|
||||
|
||||
#endif // MPT_ARCH
|
||||
|
||||
// clang-format on
|
||||
|
||||
|
||||
|
||||
// compiler supported instrinsics
|
||||
|
||||
// clang-format off
|
||||
|
||||
#if MPT_ARCH_X86 || MPT_ARCH_AMD64
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
|
||||
#define MPT_ARCH_INTRINSICS_X86_I386
|
||||
#define MPT_ARCH_INTRINSICS_X86_FPU
|
||||
#define MPT_ARCH_INTRINSICS_X86_FSIN
|
||||
#define MPT_ARCH_INTRINSICS_X86_CPUID
|
||||
#define MPT_ARCH_INTRINSICS_X86_TSC
|
||||
#define MPT_ARCH_INTRINSICS_X86_CX8
|
||||
#define MPT_ARCH_INTRINSICS_X86_CMOV
|
||||
#define MPT_ARCH_INTRINSICS_X86_MMX
|
||||
#define MPT_ARCH_INTRINSICS_X86_MMXEXT
|
||||
#define MPT_ARCH_INTRINSICS_X86_3DNOW
|
||||
#define MPT_ARCH_INTRINSICS_X86_3DNOWEXT
|
||||
#define MPT_ARCH_INTRINSICS_X86_3DNOWPREFETCH
|
||||
#if MPT_MSVC_AT_LEAST(2003, 0)
|
||||
#define MPT_ARCH_INTRINSICS_X86_FXSR
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE2
|
||||
#endif
|
||||
#if MPT_MSVC_AT_LEAST(2008, 0)
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE3
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSSE3
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE4_1
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE4_2
|
||||
#endif
|
||||
#if MPT_MSVC_AT_LEAST(2010, 1)
|
||||
#define MPT_ARCH_INTRINSICS_X86_XSAVE
|
||||
#define MPT_ARCH_INTRINSICS_X86_AVX
|
||||
#endif
|
||||
#if MPT_MSVC_AT_LEAST(2012, 0)
|
||||
#define MPT_ARCH_INTRINSICS_X86_AVX2
|
||||
#define MPT_ARCH_INTRINSICS_X86_FMA
|
||||
#define MPT_ARCH_INTRINSICS_X86_BMI1
|
||||
#endif
|
||||
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
|
||||
#ifdef MPT_ARCH_X86_I386
|
||||
#define MPT_ARCH_INTRINSICS_X86_I386
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_FPU
|
||||
#define MPT_ARCH_INTRINSICS_X86_FPU
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_FSIN
|
||||
#define MPT_ARCH_INTRINSICS_X86_FSIN
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_I486
|
||||
#define MPT_ARCH_INTRINSICS_X86_I486
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_CPUID
|
||||
#define MPT_ARCH_INTRINSICS_X86_CPUID
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_TSC
|
||||
#define MPT_ARCH_INTRINSICS_X86_TSC
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_CX8
|
||||
#define MPT_ARCH_INTRINSICS_X86_CX8
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_CMOV
|
||||
#define MPT_ARCH_INTRINSICS_X86_CMOV
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_MMX
|
||||
#define MPT_ARCH_INTRINSICS_X86_MMX
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_MMXEXT
|
||||
#define MPT_ARCH_INTRINSICS_X86_MMXEXT
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_3DNOW
|
||||
#define MPT_ARCH_INTRINSICS_X86_3DNOW
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_3DNOWEXT
|
||||
#define MPT_ARCH_INTRINSICS_X86_3DNOWEXT
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_3DNOWPREFETCH
|
||||
#define MPT_ARCH_INTRINSICS_X86_3DNOWPREFETCH
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_FXSR
|
||||
#define MPT_ARCH_INTRINSICS_X86_FXSR
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_SSE
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_SSE2
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE2
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_SSE3
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE3
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_SSSE3
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSSE3
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_SSE4_1
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE4_1
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_SSE4_2
|
||||
#define MPT_ARCH_INTRINSICS_X86_SSE4_2
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_XSAVE
|
||||
#define MPT_ARCH_INTRINSICS_X86_XSAVE
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_AVX
|
||||
#define MPT_ARCH_INTRINSICS_X86_AVX
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_AVX2
|
||||
#define MPT_ARCH_INTRINSICS_X86_AVX2
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_CX16
|
||||
#define MPT_ARCH_INTRINSICS_X86_CX16
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_LAHF
|
||||
#define MPT_ARCH_INTRINSICS_X86_LAHF
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_POPCNT
|
||||
#define MPT_ARCH_INTRINSICS_X86_POPCNT
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_BMI1
|
||||
#define MPT_ARCH_INTRINSICS_X86_BMI1
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_BMI2
|
||||
#define MPT_ARCH_INTRINSICS_X86_BMI2
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_F16C
|
||||
#define MPT_ARCH_INTRINSICS_X86_F16C
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_FMA
|
||||
#define MPT_ARCH_INTRINSICS_X86_FMA
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_LZCNT
|
||||
#define MPT_ARCH_INTRINSICS_X86_LZCNT
|
||||
#endif
|
||||
#ifdef MPT_ARCH_X86_MOVBE
|
||||
#define MPT_ARCH_INTRINSICS_X86_MOVBE
|
||||
#endif
|
||||
|
||||
#endif // MPT_COMPILER
|
||||
|
||||
#endif // MPT_ARCH
|
||||
|
||||
// clang-format on
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_ARCH_HPP
|
||||
@@ -0,0 +1,264 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_COMPILER_HPP
|
||||
#define MPT_BASE_DETECT_COMPILER_HPP
|
||||
|
||||
|
||||
|
||||
#define MPT_COMPILER_MAKE_VERSION2(version, sp) ((version)*100 + (sp))
|
||||
#define MPT_COMPILER_MAKE_VERSION3(major, minor, patch) ((major)*10000 + (minor)*100 + (patch))
|
||||
|
||||
|
||||
|
||||
#if defined(MPT_COMPILER_GENERIC)
|
||||
|
||||
#undef MPT_COMPILER_GENERIC
|
||||
#define MPT_COMPILER_GENERIC 1
|
||||
|
||||
#elif defined(__clang__) && defined(_MSC_VER) && defined(__c2__)
|
||||
|
||||
#error "Clang/C2 is not supported. Please use Clang/LLVM for Windows instead."
|
||||
|
||||
#elif defined(__clang__)
|
||||
|
||||
#define MPT_COMPILER_CLANG 1
|
||||
#define MPT_COMPILER_CLANG_VERSION MPT_COMPILER_MAKE_VERSION3(__clang_major__, __clang_minor__, __clang_patchlevel__)
|
||||
#define MPT_CLANG_AT_LEAST(major, minor, patch) (MPT_COMPILER_CLANG_VERSION >= MPT_COMPILER_MAKE_VERSION3((major), (minor), (patch)))
|
||||
#define MPT_CLANG_BEFORE(major, minor, patch) (MPT_COMPILER_CLANG_VERSION < MPT_COMPILER_MAKE_VERSION3((major), (minor), (patch)))
|
||||
|
||||
#if MPT_CLANG_BEFORE(6, 0, 0)
|
||||
#error "clang version 6 required"
|
||||
#endif
|
||||
|
||||
#if defined(__clang_analyzer__)
|
||||
#ifndef MPT_BUILD_ANALYZED
|
||||
#define MPT_BUILD_ANALYZED
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#elif defined(__GNUC__)
|
||||
|
||||
#define MPT_COMPILER_GCC 1
|
||||
#define MPT_COMPILER_GCC_VERSION MPT_COMPILER_MAKE_VERSION3(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
|
||||
#define MPT_GCC_AT_LEAST(major, minor, patch) (MPT_COMPILER_GCC_VERSION >= MPT_COMPILER_MAKE_VERSION3((major), (minor), (patch)))
|
||||
#define MPT_GCC_BEFORE(major, minor, patch) (MPT_COMPILER_GCC_VERSION < MPT_COMPILER_MAKE_VERSION3((major), (minor), (patch)))
|
||||
|
||||
#if MPT_GCC_BEFORE(7, 1, 0)
|
||||
#error "GCC version 7.1 required"
|
||||
#endif
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
|
||||
#define MPT_COMPILER_MSVC 1
|
||||
#if (_MSC_VER >= 1944)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 14)
|
||||
#elif (_MSC_VER >= 1943)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 13)
|
||||
#elif (_MSC_VER >= 1942)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 12)
|
||||
#elif (_MSC_VER >= 1941)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 11)
|
||||
#elif (_MSC_VER >= 1940)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 10)
|
||||
#elif (_MSC_VER >= 1939)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 9)
|
||||
#elif (_MSC_VER >= 1938)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 8)
|
||||
#elif (_MSC_VER >= 1937)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 7)
|
||||
#elif (_MSC_VER >= 1936)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 6)
|
||||
#elif (_MSC_VER >= 1935)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 5)
|
||||
#elif (_MSC_VER >= 1934)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 4)
|
||||
#elif (_MSC_VER >= 1933)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 3)
|
||||
#elif (_MSC_VER >= 1932)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 2)
|
||||
#elif (_MSC_VER >= 1931)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 1)
|
||||
#elif (_MSC_VER >= 1930)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2022, 0)
|
||||
#elif (_MSC_VER >= 1929)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 10)
|
||||
#elif (_MSC_VER >= 1928)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 8)
|
||||
#elif (_MSC_VER >= 1927)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 7)
|
||||
#elif (_MSC_VER >= 1926)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 6)
|
||||
#elif (_MSC_VER >= 1925)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 5)
|
||||
#elif (_MSC_VER >= 1924)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 4)
|
||||
#elif (_MSC_VER >= 1923)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 3)
|
||||
#elif (_MSC_VER >= 1922)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 2)
|
||||
#elif (_MSC_VER >= 1921)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 1)
|
||||
#elif (_MSC_VER >= 1920)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2019, 0)
|
||||
#elif (_MSC_VER >= 1916)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 9)
|
||||
#elif (_MSC_VER >= 1915)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 8)
|
||||
#elif (_MSC_VER >= 1914)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 7)
|
||||
#elif (_MSC_VER >= 1913)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 6)
|
||||
#elif (_MSC_VER >= 1912)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 5)
|
||||
#elif (_MSC_VER >= 1911)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 3)
|
||||
#elif (_MSC_VER >= 1910)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2017, 0)
|
||||
#elif (_MSC_VER >= 1900) && defined(_MSVC_LANG)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2015, 3)
|
||||
#elif (_MSC_VER >= 1900)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2015, 0)
|
||||
#elif (_MSC_VER >= 1800)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2013, 0)
|
||||
#elif (_MSC_VER >= 1700)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2012, 0)
|
||||
#elif (_MSC_VER >= 1600)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2010, 0)
|
||||
#elif (_MSC_VER >= 1500)
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2008, 0)
|
||||
#else
|
||||
#define MPT_COMPILER_MSVC_VERSION MPT_COMPILER_MAKE_VERSION2(2005, 0)
|
||||
#endif
|
||||
#define MPT_MSVC_AT_LEAST(version, sp) (MPT_COMPILER_MSVC_VERSION >= MPT_COMPILER_MAKE_VERSION2((version), (sp)))
|
||||
#define MPT_MSVC_BEFORE(version, sp) (MPT_COMPILER_MSVC_VERSION < MPT_COMPILER_MAKE_VERSION2((version), (sp)))
|
||||
|
||||
#if MPT_MSVC_BEFORE(2017, 9)
|
||||
#error "MSVC version 2017 15.9 required"
|
||||
#endif
|
||||
|
||||
#if defined(_PREFAST_)
|
||||
#ifndef MPT_BUILD_ANALYZED
|
||||
#define MPT_BUILD_ANALYZED
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define MPT_COMPILER_GENERIC 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef MPT_COMPILER_GENERIC
|
||||
#define MPT_COMPILER_GENERIC 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_CLANG
|
||||
#define MPT_COMPILER_CLANG 0
|
||||
#define MPT_CLANG_AT_LEAST(major, minor, patch) 0
|
||||
#define MPT_CLANG_BEFORE(major, minor, patch) 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_GCC
|
||||
#define MPT_COMPILER_GCC 0
|
||||
#define MPT_GCC_AT_LEAST(major, minor, patch) 0
|
||||
#define MPT_GCC_BEFORE(major, minor, patch) 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_MSVC
|
||||
#define MPT_COMPILER_MSVC 0
|
||||
#define MPT_MSVC_AT_LEAST(version, sp) 0
|
||||
#define MPT_MSVC_BEFORE(version, sp) 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_GENERIC
|
||||
|
||||
#if (__cplusplus >= 202302)
|
||||
#define MPT_CXX 23
|
||||
#elif (__cplusplus >= 202002)
|
||||
#define MPT_CXX 20
|
||||
#elif (__cplusplus >= 201703)
|
||||
#define MPT_CXX 17
|
||||
#endif
|
||||
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
|
||||
#if (__cplusplus >= 202302)
|
||||
#define MPT_CXX 23
|
||||
#elif (__cplusplus >= 202002)
|
||||
#if defined(__APPLE__) && MPT_CLANG_BEFORE(13, 0, 0)
|
||||
// XCode 12.5 has a really weird mix of Clang and libc++. Just black-list C++20 support for XCode <= 12.
|
||||
#define MPT_CXX 17
|
||||
#else
|
||||
#define MPT_CXX 20
|
||||
#endif
|
||||
#elif (__cplusplus >= 201703)
|
||||
#define MPT_CXX 17
|
||||
#endif
|
||||
|
||||
#elif MPT_COMPILER_MSVC
|
||||
|
||||
#if MPT_MSVC_AT_LEAST(2015, 3)
|
||||
#if (_MSVC_LANG >= 202302)
|
||||
#define MPT_CXX 23
|
||||
#elif (_MSVC_LANG >= 202002)
|
||||
#define MPT_CXX 20
|
||||
#elif (_MSVC_LANG >= 201703)
|
||||
#define MPT_CXX 17
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
// default to C++17
|
||||
#ifndef MPT_CXX
|
||||
#define MPT_CXX 17
|
||||
#endif
|
||||
|
||||
// MPT_CXX is stricter than just using __cplusplus directly.
|
||||
// We will only claim a language version as supported IFF all core language and
|
||||
// library fatures that we need are actually supported AND working correctly
|
||||
// (to our needs).
|
||||
|
||||
#define MPT_CXX_AT_LEAST(version) (MPT_CXX >= (version))
|
||||
#define MPT_CXX_BEFORE(version) (MPT_CXX < (version))
|
||||
|
||||
|
||||
|
||||
// detect compiler quirks
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#if defined(__APPLE__)
|
||||
#define MPT_COMPILER_QUIRK_APPLE_CLANG
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// detect compiler setting quirks
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
#if (MPT_GCC_AT_LEAST(14, 0, 0) && MPT_GCC_BEFORE(14, 2, 0)) || (MPT_GCC_AT_LEAST(13, 0, 0) && MPT_GCC_BEFORE(13, 4, 0)) || (MPT_GCC_AT_LEAST(12, 0, 0) && MPT_GCC_BEFORE(12, 5, 0)) || MPT_GCC_BEFORE(12, 0, 0)
|
||||
// GCC 14 causes severe miscompilation of inline functions on MinGW.
|
||||
// See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115049>.
|
||||
// Current investigation suggests a general problem with -fipa-ra on non-ELF
|
||||
// platforms.
|
||||
// As far as we understand the issue, it could possibly also manifest with
|
||||
// other inter-procedure-optimizations and with older GCC versions.
|
||||
// Fixed in GCC 15
|
||||
// (<https://gcc.gnu.org/git/?p=gcc.git;h=5080840d8fbf25a321dd27543a1462d393d338bc>),
|
||||
// GCC 14.2
|
||||
// (<https://gcc.gnu.org/git/?p=gcc.git;h=747c4b58573ea00419f64293a61537eb69f43307>).
|
||||
// GCC 13.4
|
||||
// (<https://gcc.gnu.org/git/?p=gcc.git;h=953bf37690d22de956d75c6aef7a9690ad55b9a7>).
|
||||
// and GCC 12.5
|
||||
// (<https://gcc.gnu.org/git/?p=gcc.git;h=2c5f48a43f26223cb8603b826d7c0d52cdbcfb46>).
|
||||
#if !defined(__ELF__)
|
||||
#define MPT_COMPILER_SETTING_QUIRK_GCC_BROKEN_IPA
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_COMPILER_HPP
|
||||
@@ -0,0 +1,248 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_LIBC_HPP
|
||||
#define MPT_BASE_DETECT_LIBC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
// Version numbers try to follow MinGW32
|
||||
// <https://sourceforge.net/p/mingw/mingw-org-wsl/ci/5.1-trunk/tree/mingwrt/include/msvcrtver.h>,
|
||||
// and MinGW-w64 version numbers are adjusted for
|
||||
// <https://github.com/mingw-w64/mingw-w64/commit/94a28397eec79e54ce5a8354512f43da88f815d2>,
|
||||
#define MPT_LIBC_MS_VER_CRTDLL 0x0000
|
||||
#define MPT_LIBC_MS_VER_MSVCR10 0x0100
|
||||
#define MPT_LIBC_MS_VER_MSVCR20 0x0200
|
||||
#define MPT_LIBC_MS_VER_MSVCR40 0x0400
|
||||
#define MPT_LIBC_MS_VER_MSVCRT40 0x0400
|
||||
#define MPT_LIBC_MS_VER_MSVCRT 0x0600
|
||||
#define MPT_LIBC_MS_VER_MSVCR60 0x0600
|
||||
#define MPT_LIBC_MS_VER_MSVCR61 0x0601
|
||||
#define MPT_LIBC_MS_VER_MSVCR70 0x0700
|
||||
#define MPT_LIBC_MS_VER_MSVCR71 0x0701
|
||||
#define MPT_LIBC_MS_VER_MSVCR80 0x0800
|
||||
#define MPT_LIBC_MS_VER_MSVCR90 0x0900
|
||||
#define MPT_LIBC_MS_VER_MSVCR100 0x1000
|
||||
#define MPT_LIBC_MS_VER_MSVCR110 0x1100
|
||||
#define MPT_LIBC_MS_VER_MSVCR120 0x1200
|
||||
#define MPT_LIBC_MS_VER_UCRT 0x1400
|
||||
|
||||
|
||||
|
||||
// order of checks is important!
|
||||
#if MPT_COMPILER_GENERIC
|
||||
#define MPT_LIBC_GENERIC 1
|
||||
#elif (defined(__MINGW32__) || defined(__MINGW64__))
|
||||
#define MPT_LIBC_MINGW 1
|
||||
#if defined(__MINGW64_VERSION_MAJOR) && defined(__MINGW64_VERSION_MINOR)
|
||||
#define MPT_LIBC_MINGW_MINGWW64 1
|
||||
#define MPT_LIBC_MINGW_MINGWW64_AT_LEAST(major, minor) (((major) > __MINGW64_VERSION_MAJOR) || (((major) == __MINGW64_VERSION_MAJOR) && ((minor) >= __MINGW64_VERSION_MINOR)))
|
||||
#define MPT_LIBC_MINGW_MINGWW64_BEFORE(major, minor) (((major) < __MINGW64_VERSION_MAJOR) || (((major) == __MINGW64_VERSION_MAJOR) && ((minor) < __MINGW64_VERSION_MINOR)))
|
||||
#elif defined(__MINGW64_VERSION_MAJOR)
|
||||
#define MPT_LIBC_MINGW_MINGWW64 1
|
||||
#define MPT_LIBC_MINGW_MINGWW64_AT_LEAST(major, minor) ((major) >= __MINGW64_VERSION_MAJOR)
|
||||
#define MPT_LIBC_MINGW_MINGWW64_BEFORE(major, minor) ((major) < __MINGW64_VERSION_MAJOR)
|
||||
#elif defined(__MINGW64_VERSION_MINOR)
|
||||
#define MPT_LIBC_MINGW_MINGWW64 1
|
||||
#define MPT_LIBC_MINGW_MINGWW64_AT_LEAST(major, minor) 0
|
||||
#define MPT_LIBC_MINGW_MINGWW64_BEFORE(major, minor) 1
|
||||
#elif defined(__MINGW32_VERSION_MAJOR) && defined(__MINGW32_VERSION_MINOR)
|
||||
#define MPT_LIBC_MINGW_MINGW32 1
|
||||
#define MPT_LIBC_MINGW_MINGW32_AT_LEAST(major, minor) (((major) > __MINGW32_VERSION_MAJOR) || (((major) == __MINGW32_VERSION_MAJOR) && ((minor) >= __MINGW32_VERSION_MINOR)))
|
||||
#define MPT_LIBC_MINGW_MINGW32_BEFORE(major, minor) (((major) < __MINGW32_VERSION_MAJOR) || (((major) == __MINGW32_VERSION_MAJOR) && ((minor) < __MINGW32_VERSION_MINOR)))
|
||||
#elif defined(__MINGW32_VERSION_MAJOR)
|
||||
#define MPT_LIBC_MINGW_MINGW32 1
|
||||
#define MPT_LIBC_MINGW_MINGW32_AT_LEAST(major, minor) ((major) >= __MINGW32_VERSION_MAJOR)
|
||||
#define MPT_LIBC_MINGW_MINGW32_BEFORE(major, minor) ((major) < __MINGW32_VERSION_MAJOR)
|
||||
#elif defined(__MINGW32_VERSION_MINOR)
|
||||
#define MPT_LIBC_MINGW_MINGW32 1
|
||||
#define MPT_LIBC_MINGW_MINGW32_AT_LEAST(major, minor) 0
|
||||
#define MPT_LIBC_MINGW_MINGW32_BEFORE(major, minor) 1
|
||||
#endif
|
||||
#if defined(MPT_LIBC_MINGW_MINGW32)
|
||||
#ifdef __MSVCR60_DLL
|
||||
static_assert(__MSVCR60_DLL == MPT_LIBC_MS_VER_MSVCR60);
|
||||
#endif
|
||||
#ifdef __MSVCR61_DLL
|
||||
static_assert(__MSVCR61_DLL == MPT_LIBC_MS_VER_MSVCR61);
|
||||
#endif
|
||||
#ifdef __MSVCR70_DLL
|
||||
static_assert(__MSVCR70_DLL == MPT_LIBC_MS_VER_MSVCR70);
|
||||
#endif
|
||||
#ifdef __MSVCR71_DLL
|
||||
static_assert(__MSVCR71_DLL == MPT_LIBC_MS_VER_MSVCR71);
|
||||
#endif
|
||||
#ifdef __MSVCR80_DLL
|
||||
static_assert(__MSVCR80_DLL == MPT_LIBC_MS_VER_MSVCR80);
|
||||
#endif
|
||||
#ifdef __MSVCR90_DLL
|
||||
static_assert(__MSVCR90_DLL == MPT_LIBC_MS_VER_MSVCR90);
|
||||
#endif
|
||||
#ifdef __MSVCR100_DLL
|
||||
static_assert(__MSVCR100_DLL == MPT_LIBC_MS_VER_MSVCR100);
|
||||
#endif
|
||||
#ifdef __MSVCR110_DLL
|
||||
static_assert(__MSVCR110_DLL == MPT_LIBC_MS_VER_MSVCR110);
|
||||
#endif
|
||||
#ifdef __MSVCR120_DLL
|
||||
static_assert(__MSVCR120_DLL == MPT_LIBC_MS_VER_MSVCR120);
|
||||
#endif
|
||||
#endif
|
||||
#if defined(_UCRT)
|
||||
#define MPT_LIBC_MS_UCRT 1
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_UCRT
|
||||
#elif defined(__MSVCRT__)
|
||||
#define MPT_LIBC_MS_MSVCRT 1
|
||||
#if defined(__MSVCRT_VERSION__)
|
||||
#if defined(MPT_LIBC_MINGW_MINGWW64)
|
||||
#if (__MSVCRT_VERSION__ > 0x09ff)
|
||||
// MinGW-w64 completely fucked up version numbers in
|
||||
// <https://github.com/mingw-w64/mingw-w64/commit/94a28397eec79e54ce5a8354512f43da88f815d2>,
|
||||
// so we need to compensate for that mother of brainfarts.
|
||||
#define MPT_LIBC_MS_VERSION (__MSVCRT_VERSION__ + (0x1000 - 0x0A00))
|
||||
#else
|
||||
#define MPT_LIBC_MS_VERSION __MSVCRT_VERSION__
|
||||
#endif
|
||||
#define MPT_LIBC_MS_VERSION __MSVCRT_VERSION__
|
||||
#elif defined(MPT_LIBC_MINGW_MINGW32)
|
||||
#define MPT_LIBC_MS_VERSION __MSVCRT_VERSION__
|
||||
#else
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCRT
|
||||
#endif
|
||||
#endif
|
||||
#elif defined(__CRTDLL__)
|
||||
#define MPT_LIBC_MS_CRTDLL 1
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_CRTDLL
|
||||
#endif
|
||||
#elif (defined(__GLIBC__) || defined(__GNU_LIBRARY__))
|
||||
#define MPT_LIBC_GLIBC 1
|
||||
#elif defined(_UCRT)
|
||||
#define MPT_LIBC_MS 1
|
||||
#define MPT_LIBC_MS_UCRT 1
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_UCRT
|
||||
#elif MPT_COMPILER_MSVC
|
||||
#define MPT_LIBC_MS 1
|
||||
#define MPT_LIBC_MS_MSVCRT 1
|
||||
#if MPT_MSVC_AT_LEAST(2015, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_UCRT
|
||||
#elif MPT_MSVC_AT_LEAST(2013, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR120
|
||||
#elif MPT_MSVC_AT_LEAST(2012, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR110
|
||||
#elif MPT_MSVC_AT_LEAST(2010, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR100
|
||||
#elif MPT_MSVC_AT_LEAST(2008, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR90
|
||||
#elif MPT_MSVC_AT_LEAST(2005, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR80
|
||||
#elif MPT_MSVC_AT_LEAST(2003, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR71
|
||||
#elif MPT_MSVC_AT_LEAST(2002, 0)
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCR70
|
||||
#else
|
||||
#define MPT_LIBC_MS_VERSION MPT_LIBC_MS_VER_MSVCRT
|
||||
#endif
|
||||
#elif MPT_COMPILER_CLANG && MPT_OS_WINDOWS
|
||||
#define MPT_LIBC_MS 1
|
||||
#elif defined(__BIONIC__)
|
||||
#define MPT_LIBC_BIONIC 1
|
||||
#elif defined(__APPLE__)
|
||||
#define MPT_LIBC_APPLE 1
|
||||
#elif defined(__DJGPP__)
|
||||
#define MPT_LIBC_DJGPP 1
|
||||
#else
|
||||
#define MPT_LIBC_GENERIC 1
|
||||
#endif
|
||||
|
||||
#if defined(MPT_LIBC_MS_VERSION)
|
||||
#define MPT_LIBC_MS_AT_LEAST(v) (MPT_LIBC_MS_VERSION >= (v))
|
||||
#define MPT_LIBC_MS_BEFORE(v) (MPT_LIBC_MS_VERSION < (v))
|
||||
#endif
|
||||
|
||||
#ifndef MPT_LIBC_GENERIC
|
||||
#define MPT_LIBC_GENERIC 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_GLIBC
|
||||
#define MPT_LIBC_GLIBC 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW
|
||||
#define MPT_LIBC_MINGW 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW_MINGWW64
|
||||
#define MPT_LIBC_MINGW_MINGWW64 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW_MINGW32
|
||||
#define MPT_LIBC_MINGW_MINGW32 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW_MINGWW64_AT_LEAST
|
||||
#define MPT_LIBC_MINGW_MINGWW64_AT_LEAST(major, minor) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW_MINGWW64_BEFORE
|
||||
#define MPT_LIBC_MINGW_MINGWW64_BEFORE(major, minor) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW_MINGW32_AT_LEAST
|
||||
#define MPT_LIBC_MINGW_MINGW32_AT_LEAST(major, minor) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MINGW_MINGW32_BEFORE
|
||||
#define MPT_LIBC_MINGW_MINGW32_BEFORE(major, minor) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS
|
||||
#define MPT_LIBC_MS 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_UCRT
|
||||
#define MPT_LIBC_MS_UCRT 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_MSVCRT
|
||||
#define MPT_LIBC_MS_MSVCRT 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_CRTDLL
|
||||
#define MPT_LIBC_MS_CRTDLL 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_AT_LEAST
|
||||
#define MPT_LIBC_MS_AT_LEAST(v) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_BEFORE
|
||||
#define MPT_LIBC_MS_BEFORE(v) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_BIONIC
|
||||
#define MPT_LIBC_BIONIC 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_APPLE
|
||||
#define MPT_LIBC_APPLE 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_DJGPP
|
||||
#define MPT_LIBC_DJGPP 0
|
||||
#endif
|
||||
|
||||
#if MPT_LIBC_MS
|
||||
#if defined(_DEBUG)
|
||||
#define MPT_LIBC_MS_DEBUG 1
|
||||
#endif
|
||||
#if defined(_DLL)
|
||||
#define MPT_LIBC_MS_SHARED 1
|
||||
#define MPT_LIBC_MS_STATIC 0
|
||||
#else
|
||||
#define MPT_LIBC_MS_SHARED 0
|
||||
#define MPT_LIBC_MS_STATIC 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_LIBC_MS_DEBUG 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_SHARED
|
||||
#define MPT_LIBC_MS_SHARED 0
|
||||
#endif
|
||||
#ifndef MPT_LIBC_MS_STATIC
|
||||
#define MPT_LIBC_MS_STATIC 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_LIBC_HPP
|
||||
@@ -0,0 +1,61 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_LIBCXX_HPP
|
||||
#define MPT_BASE_DETECT_LIBCXX_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include <version>
|
||||
#else // !C++20
|
||||
#include <array>
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
// order of checks is important!
|
||||
#if MPT_COMPILER_GENERIC
|
||||
#define MPT_LIBCXX_GENERIC 1
|
||||
#elif defined(_LIBCPP_VERSION)
|
||||
#define MPT_LIBCXX_LLVM 1
|
||||
#define MPT_LIBCXX_LLVM_VERSION _LIBCPP_VERSION
|
||||
#define MPT_LIBCXX_LLVM_AT_LEAST(ver) (MPT_LIBCXX_LLVM_VERSION >= (ver))
|
||||
#define MPT_LIBCXX_LLVM_BEFORE(ver) (MPT_LIBCXX_LLVM_VERSION < (ver))
|
||||
#elif defined(__GLIBCXX__) || defined(__GLIBCPP__)
|
||||
#define MPT_LIBCXX_GNU 1
|
||||
#define MPT_LIBCXX_GNU_VERSION _GLIBCXX_RELEASE
|
||||
#define MPT_LIBCXX_GNU_AT_LEAST(ver) (MPT_LIBCXX_GNU_VERSION >= (ver))
|
||||
#define MPT_LIBCXX_GNU_BEFORE(ver) (MPT_LIBCXX_GNU_VERSION < (ver))
|
||||
#elif MPT_COMPILER_MSVC
|
||||
#define MPT_LIBCXX_MS 1
|
||||
#elif MPT_COMPILER_CLANG && MPT_OS_WINDOWS
|
||||
#define MPT_LIBCXX_MS 1
|
||||
#else
|
||||
#define MPT_LIBCXX_GENERIC 1
|
||||
#endif
|
||||
|
||||
#ifndef MPT_LIBCXX_GENERIC
|
||||
#define MPT_LIBCXX_GENERIC 0
|
||||
#endif
|
||||
#ifndef MPT_LIBCXX_LLVM
|
||||
#define MPT_LIBCXX_LLVM 0
|
||||
#define MPT_LIBCXX_LLVM_VERSION 0
|
||||
#define MPT_LIBCXX_LLVM_AT_LEAST(ver) 0
|
||||
#define MPT_LIBCXX_LLVM_BEFORE(ver) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBCXX_GNU
|
||||
#define MPT_LIBCXX_GNU 0
|
||||
#define MPT_LIBCXX_GNU_VERSION 0
|
||||
#define MPT_LIBCXX_GNU_AT_LEAST(ver) 0
|
||||
#define MPT_LIBCXX_GNU_BEFORE(ver) 0
|
||||
#endif
|
||||
#ifndef MPT_LIBCXX_MS
|
||||
#define MPT_LIBCXX_MS 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_LIBCXX_HPP
|
||||
@@ -0,0 +1,556 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_OS_HPP
|
||||
#define MPT_BASE_DETECT_OS_HPP
|
||||
|
||||
|
||||
|
||||
#define MPT_WIN_MAKE_VERSION(major, minor, sp, build) ((major << 24) + (minor << 16) + (sp << 8) + (build << 0))
|
||||
|
||||
// clang-format off
|
||||
|
||||
#define MPT_WIN_WIN32S MPT_WIN_MAKE_VERSION(0x03, 0x00, 0x00, 0x00)
|
||||
|
||||
#define MPT_WIN_WIN95 MPT_WIN_MAKE_VERSION(0x04, 0x00, 0x00, 0x00)
|
||||
#define MPT_WIN_WIN98 MPT_WIN_MAKE_VERSION(0x04, 0x10, 0x00, 0x00)
|
||||
#define MPT_WIN_WINME MPT_WIN_MAKE_VERSION(0x04, 0x90, 0x00, 0x00)
|
||||
|
||||
#define MPT_WIN_NT3 MPT_WIN_MAKE_VERSION(0x03, 0x00, 0x00, 0x00)
|
||||
#define MPT_WIN_NT4 MPT_WIN_MAKE_VERSION(0x04, 0x00, 0x00, 0x00)
|
||||
#define MPT_WIN_2000 MPT_WIN_MAKE_VERSION(0x05, 0x00, 0x00, 0x00)
|
||||
#define MPT_WIN_2000SP1 MPT_WIN_MAKE_VERSION(0x05, 0x00, 0x01, 0x00)
|
||||
#define MPT_WIN_2000SP2 MPT_WIN_MAKE_VERSION(0x05, 0x00, 0x02, 0x00)
|
||||
#define MPT_WIN_2000SP3 MPT_WIN_MAKE_VERSION(0x05, 0x00, 0x03, 0x00)
|
||||
#define MPT_WIN_2000SP4 MPT_WIN_MAKE_VERSION(0x05, 0x00, 0x04, 0x00)
|
||||
#define MPT_WIN_XP MPT_WIN_MAKE_VERSION(0x05, 0x01, 0x00, 0x00)
|
||||
#define MPT_WIN_XPSP1 MPT_WIN_MAKE_VERSION(0x05, 0x01, 0x01, 0x00)
|
||||
#define MPT_WIN_XPSP2 MPT_WIN_MAKE_VERSION(0x05, 0x01, 0x02, 0x00)
|
||||
#define MPT_WIN_XPSP3 MPT_WIN_MAKE_VERSION(0x05, 0x01, 0x03, 0x00)
|
||||
#define MPT_WIN_XPSP4 MPT_WIN_MAKE_VERSION(0x05, 0x01, 0x04, 0x00) // unused
|
||||
#define MPT_WIN_XP64 MPT_WIN_MAKE_VERSION(0x05, 0x02, 0x00, 0x00) // unused
|
||||
#define MPT_WIN_XP64SP1 MPT_WIN_MAKE_VERSION(0x05, 0x02, 0x01, 0x00)
|
||||
#define MPT_WIN_XP64SP2 MPT_WIN_MAKE_VERSION(0x05, 0x02, 0x02, 0x00)
|
||||
#define MPT_WIN_XP64SP3 MPT_WIN_MAKE_VERSION(0x05, 0x02, 0x03, 0x00) // unused
|
||||
#define MPT_WIN_XP64SP4 MPT_WIN_MAKE_VERSION(0x05, 0x02, 0x04, 0x00) // unused
|
||||
#define MPT_WIN_VISTA MPT_WIN_MAKE_VERSION(0x06, 0x00, 0x00, 0x00)
|
||||
#define MPT_WIN_VISTASP1 MPT_WIN_MAKE_VERSION(0x06, 0x00, 0x01, 0x00)
|
||||
#define MPT_WIN_VISTASP2 MPT_WIN_MAKE_VERSION(0x06, 0x00, 0x02, 0x00)
|
||||
#define MPT_WIN_VISTASP3 MPT_WIN_MAKE_VERSION(0x06, 0x00, 0x03, 0x00) // unused
|
||||
#define MPT_WIN_VISTASP4 MPT_WIN_MAKE_VERSION(0x06, 0x00, 0x04, 0x00) // unused
|
||||
#define MPT_WIN_7 MPT_WIN_MAKE_VERSION(0x06, 0x01, 0x00, 0x00)
|
||||
#define MPT_WIN_8 MPT_WIN_MAKE_VERSION(0x06, 0x02, 0x00, 0x00)
|
||||
#define MPT_WIN_81 MPT_WIN_MAKE_VERSION(0x06, 0x03, 0x00, 0x00)
|
||||
|
||||
// Now, this is some very elaborate serious clusterfuck with basically no documentation.
|
||||
|
||||
// https://blog.yuo.be/2025/02/12/what-do-all-the-recent-ntddi-version-values-mean/
|
||||
// https://learn.microsoft.com/en-gb/windows/win32/winprog/using-the-windows-headers#macros-for-conditional-declarations
|
||||
// https://en.wikipedia.org/wiki/Windows_10_version_history
|
||||
// https://en.wikipedia.org/wiki/Windows_11_version_history
|
||||
// https://www.windowslatest.com/2021/09/03/windows-11-build-22449-is-now-available-with-new-loading-animation/
|
||||
// https://en.wikipedia.org/wiki/List_of_Microsoft_codenames#Windows_platform_engineering_milestones
|
||||
|
||||
// Release NTDDI-version NTDDI-constant Release Build.Sub Codename Semester Semester Semester Marketing-Name min-SDK-Version WDK_NTDDI_VERSION
|
||||
// .Version Codename Date
|
||||
|
||||
#define MPT_WIN_10_PRE MPT_WIN_MAKE_VERSION(0x06, 0x04, 0x00, 0x00) // Threshold - -
|
||||
#define MPT_WIN_10 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x00) // NTDDI_WIN10 Win10.1507 10240 Threshold 1507 - 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1511 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x01) // NTDDI_WIN10_TH2 Win10.1511 10586 Threshold 2 1511 November Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1607 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x02) // NTDDI_WIN10_RS1 Win10.1607 14393 Redstone 1607 Anniversary Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1703 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x03) // NTDDI_WIN10_RS2 Win10.1703 15063 Redstone 2 1703 Creators Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1709 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x04) // NTDDI_WIN10_RS3 Win10.1709 16299 Redstone 3 1709 Fall Creators Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1803 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x05) // NTDDI_WIN10_RS4 Win10.1803 17134 Redstone 4 1803 April 2018 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1809 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x06) // NTDDI_WIN10_RS5 Win10.1809 17763 Redstone 5 1809 October 2018 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_1903 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x07) // NTDDI_WIN10_19H1 Win10.1903 18362 Titanium R1 Titanium 19H1 May 2019 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
// MPT_WIN_10_1909 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x07) // Win10.1909 18363 Titanium R2 Vanadium 19H2 November 2019 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
#define MPT_WIN_10_2004 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x08) // NTDDI_WIN10_VB Win10.2004 19041 Vibranium R1 Vibranium 20H1 May 2020 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
// MPT_WIN_10_20H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x08) // Win10.20H2 19042 Vibranium R2 Manganese 20H2 October 2020 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
// MPT_WIN_10_21H1 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x08) // Win10.21H1 19043 Vibranium R3 Iron 21H1 May 2021 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
// MPT_WIN_10_21H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x08) // Win10.21H2 19044 Vibranium R4 Cobalt 21H2 November 2021 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
// MPT_WIN_10_22H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x08) // Win10.22H2 19045 Vibranium R5 Nickel 22H2 2022 Update 10.0.19041.0 NTDDI_WIN10_VB
|
||||
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x09) // NTDDI_WIN10_MN 20H2 Manganese 20H2 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0a) // NTDDI_WIN10_FE 21H1 Iron 21H1 10.0.22621.? NTDDI_WIN10_NI
|
||||
#define MPT_WIN_11 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0b) // NTDDI_WIN10_CO Win11.21H2 22000 Sun Valley Cobalt Cobalt 21H2 - 10.0.22621.? NTDDI_WIN10_NI
|
||||
#define MPT_WIN_11_22H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // NTDDI_WIN10_NI Win11.22H2 22621 Sun Valley 2 Nickel R1 Nickel 22H2 2022 Update 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.22H2 22621.675 Sun Valley 2 Nickel R1 Nickel 22H2 2022 Update Moment 1 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.22H2 22621.1344 Sun Valley 2 Nickel R1 Nickel 22H2 2022 Update Moment 2 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.22H2 22621.1778 Sun Valley 2 Nickel R1 Nickel 22H2 2022 Update Moment 3 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.22H2 22621.2361 Sun Valley 2 Nickel R1 Nickel 22H2 2022 Update Moment 4 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.22H2 22621.3235 Sun Valley 2 Nickel R1 Nickel 22H2 2022 Update Moment 5 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_11_23H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.23H2 22631 Sun Valley 3 Nickel R2 Zinc 23H2 2023 Update 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0c) // Win11.23H2 22631.3235 Sun Valley 3 Nickel R2 Zinc 23H2 2023 Update Moment 5 10.0.22621.? NTDDI_WIN10_NI
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0d) // NTDDI_WIN10_CU 23H1 Copper 23H1 10.0.26100.? NTDDI_WIN11_GE
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0e) // NTDDI_WIN11_ZN 23H2 Zinc 23H2 10.0.26100.? NTDDI_WIN11_GE
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x0f) // NTDDI_WIN11_GA 24H1 Gallium 24H1 10.0.26100.? NTDDI_WIN11_GE
|
||||
#define MPT_WIN_11_24H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x10) // NTDDI_WIN11_GE Win11.24H2 26100 Hudson Valley Germanium R1 Germanium 24H2 2024 Update 10.0.26100.? NTDDI_WIN11_GE
|
||||
// MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x10) // 25H1 Dilithium 25H1 10.0.28000.? NTDDI_WIN11_BR
|
||||
#define MPT_WIN_11_25H2 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x11) // NTDDI_WIN11_DT Win11.25H2 26200 Hudson Valley 2 ? ? 25H2 2025 Update 10.0.28000.? NTDDI_WIN11_BR
|
||||
#define MPT_WIN_11_26H1 MPT_WIN_MAKE_VERSION(0x0a, 0x00, 0x00, 0x12) // NTDDI_WIN11_BR Win11.26H1 28000 ? ? Bromine 26H2 10.0.28000.? NTDDI_WIN11_BR
|
||||
|
||||
// MPT_WIN_API_DESKTOP : Windows 8/10 Desktop Application (Win32)
|
||||
// MPT_WIN_API_UNIVERSAL : Windows 10 Store App / Universal App
|
||||
// MPT_WIN_API_STORE_PC : Windows 8 Store Desktop App
|
||||
// MPT_WIN_API_STORE_PHONE : Windows 8 Store Phone App
|
||||
|
||||
// clang-format on
|
||||
|
||||
|
||||
|
||||
// The order of the checks matters!
|
||||
#if defined(__DJGPP__)
|
||||
#define MPT_OS_DJGPP 1
|
||||
|
||||
|
||||
#elif defined(__EMSCRIPTEN__)
|
||||
#define MPT_OS_EMSCRIPTEN 1
|
||||
#if !defined(__EMSCRIPTEN_MAJOR__) || !defined(__EMSCRIPTEN_MINOR__) || !defined(__EMSCRIPTEN_TINY__) || !defined(__EMSCRIPTEN_major__) || !defined(__EMSCRIPTEN_minor__) || !defined(__EMSCRIPTEN_tiny__)
|
||||
#include <emscripten/version.h>
|
||||
#endif
|
||||
#if defined(__EMSCRIPTEN_MAJOR__)
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_MAJOR __EMSCRIPTEN_MAJOR__
|
||||
#elif defined(__EMSCRIPTEN_major__)
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_MAJOR __EMSCRIPTEN_major__
|
||||
#else
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_MAJOR 0
|
||||
#endif
|
||||
#if defined(__EMSCRIPTEN_MINOR__)
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_MINOR __EMSCRIPTEN_MINOR__
|
||||
#elif defined(__EMSCRIPTEN_minor__)
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_MINOR __EMSCRIPTEN_minor__
|
||||
#else
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_MINOR 0
|
||||
#endif
|
||||
#if defined(__EMSCRIPTEN_TINY__)
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_TINY __EMSCRIPTEN_TINY__
|
||||
#elif defined(__EMSCRIPTEN_tiny__)
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_TINY __EMSCRIPTEN_tiny__
|
||||
#else
|
||||
#define MPT_OS_EMSCRIPTEN_VERSION_TINY 0
|
||||
#endif
|
||||
#if (MPT_OS_EMSCRIPTEN_VERSION_MAJOR > 3)
|
||||
// ok
|
||||
#elif (MPT_OS_EMSCRIPTEN_VERSION_MAJOR == 3) && (MPT_OS_EMSCRIPTEN_VERSION_MINOR > 1)
|
||||
// ok
|
||||
#elif (MPT_OS_EMSCRIPTEN_VERSION_MAJOR == 3) && (MPT_OS_EMSCRIPTEN_VERSION_MINOR == 1) && (MPT_OS_EMSCRIPTEN_VERSION_TINY >= 51)
|
||||
// ok
|
||||
#else
|
||||
#error "Emscripten >= 3.1.51 is required."
|
||||
#endif
|
||||
|
||||
|
||||
#elif defined(__CYGWIN__)
|
||||
#define MPT_OS_CYGWIN 1
|
||||
|
||||
|
||||
#elif defined(_WIN32)
|
||||
#define MPT_OS_WINDOWS 1
|
||||
#if !defined(_WIN32_WINDOWS) && !defined(WINVER)
|
||||
// include modern SDK version header if not targeting Win9x
|
||||
#include <sdkddkver.h>
|
||||
#ifdef _WIN32_WINNT_NT4
|
||||
static_assert((_WIN32_WINNT_NT4 << 16) == MPT_WIN_NT4);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WIN2K
|
||||
static_assert((_WIN32_WINNT_WIN2K << 16) == MPT_WIN_2000);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WINXP
|
||||
static_assert((_WIN32_WINNT_WINXP << 16) == MPT_WIN_XP);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WS03
|
||||
static_assert((_WIN32_WINNT_WS03 << 16) == MPT_WIN_XP64);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_VISTA
|
||||
static_assert((_WIN32_WINNT_VISTA << 16) == MPT_WIN_VISTA);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WIN7
|
||||
static_assert((_WIN32_WINNT_WIN7 << 16) == MPT_WIN_7);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WIN8
|
||||
static_assert((_WIN32_WINNT_WIN8 << 16) == MPT_WIN_8);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WINBLUE
|
||||
static_assert((_WIN32_WINNT_WINBLUE << 16) == MPT_WIN_81);
|
||||
#endif
|
||||
#ifdef _WIN32_WINNT_WIN10
|
||||
static_assert((_WIN32_WINNT_WIN10 << 16) == MPT_WIN_10);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN4
|
||||
static_assert(NTDDI_WIN4 == MPT_WIN_NT4);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN2K
|
||||
static_assert(NTDDI_WIN2K == MPT_WIN_2000);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN2KSP1
|
||||
static_assert(NTDDI_WIN2KSP1 == MPT_WIN_2000SP1);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN2KSP2
|
||||
static_assert(NTDDI_WIN2KSP2 == MPT_WIN_2000SP2);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN2KSP3
|
||||
static_assert(NTDDI_WIN2KSP3 == MPT_WIN_2000SP3);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN2KSP4
|
||||
static_assert(NTDDI_WIN2KSP4 == MPT_WIN_2000SP4);
|
||||
#endif
|
||||
#ifdef NTDDI_WINXP
|
||||
static_assert(NTDDI_WINXP == MPT_WIN_XP);
|
||||
#endif
|
||||
#ifdef NTDDI_WINXPSP1
|
||||
static_assert(NTDDI_WINXPSP1 == MPT_WIN_XPSP1);
|
||||
#endif
|
||||
#ifdef NTDDI_WINXPSP2
|
||||
static_assert(NTDDI_WINXPSP2 == MPT_WIN_XPSP2);
|
||||
#endif
|
||||
#ifdef NTDDI_WINXPSP3
|
||||
static_assert(NTDDI_WINXPSP3 == MPT_WIN_XPSP3);
|
||||
#endif
|
||||
#ifdef NTDDI_WINXPSP4
|
||||
static_assert(NTDDI_WINXPSP4 == MPT_WIN_XPSP4);
|
||||
#endif
|
||||
#ifdef NTDDI_WS03
|
||||
static_assert(NTDDI_WS03 == MPT_WIN_XP64);
|
||||
#endif
|
||||
#ifdef NTDDI_WS03SP1
|
||||
static_assert(NTDDI_WS03SP1 == MPT_WIN_XP64SP1);
|
||||
#endif
|
||||
#ifdef NTDDI_WS03SP2
|
||||
static_assert(NTDDI_WS03SP2 == MPT_WIN_XP64SP2);
|
||||
#endif
|
||||
#ifdef NTDDI_WS03SP3
|
||||
static_assert(NTDDI_WS03SP3 == MPT_WIN_XP64SP3);
|
||||
#endif
|
||||
#ifdef NTDDI_WS03SP4
|
||||
static_assert(NTDDI_WS03SP4 == MPT_WIN_XP64SP4);
|
||||
#endif
|
||||
#ifdef NTDDI_VISTA
|
||||
static_assert(NTDDI_VISTA == MPT_WIN_VISTA);
|
||||
#endif
|
||||
#ifdef NTDDI_VISTASP1
|
||||
static_assert(NTDDI_VISTASP1 == MPT_WIN_VISTASP1);
|
||||
#endif
|
||||
#ifdef NTDDI_VISTASP2
|
||||
static_assert(NTDDI_VISTASP2 == MPT_WIN_VISTASP2);
|
||||
#endif
|
||||
#ifdef NTDDI_VISTASP3
|
||||
static_assert(NTDDI_VISTASP3 == MPT_WIN_VISTASP3);
|
||||
#endif
|
||||
#ifdef NTDDI_VISTASP4
|
||||
static_assert(NTDDI_VISTASP4 == MPT_WIN_VISTASP4);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN7
|
||||
static_assert(NTDDI_WIN7 == MPT_WIN_7);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN8
|
||||
static_assert(NTDDI_WIN8 == MPT_WIN_8);
|
||||
#endif
|
||||
#ifdef NTDDI_WINBLUE
|
||||
static_assert(NTDDI_WINBLUE == MPT_WIN_81);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10
|
||||
static_assert(NTDDI_WIN10 == MPT_WIN_10);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_TH2
|
||||
static_assert(NTDDI_WIN10_TH2 == MPT_WIN_10_1511);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_RS1
|
||||
static_assert(NTDDI_WIN10_RS1 == MPT_WIN_10_1607);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_RS2
|
||||
static_assert(NTDDI_WIN10_RS2 == MPT_WIN_10_1703);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_RS3
|
||||
static_assert(NTDDI_WIN10_RS3 == MPT_WIN_10_1709);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_RS4
|
||||
static_assert(NTDDI_WIN10_RS4 == MPT_WIN_10_1803);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_RS5
|
||||
static_assert(NTDDI_WIN10_RS5 == MPT_WIN_10_1809);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_19H1
|
||||
static_assert(NTDDI_WIN10_19H1 == MPT_WIN_10_1903);
|
||||
#endif
|
||||
//#ifdef NTDDI_WIN10_19H1
|
||||
//static_assert(NTDDI_WIN10_19H1 == MPT_WIN_10_1909);
|
||||
//#endif
|
||||
#ifdef NTDDI_WIN10_VB
|
||||
static_assert(NTDDI_WIN10_VB == MPT_WIN_10_2004);
|
||||
#endif
|
||||
//#ifdef NTDDI_WIN10_VB
|
||||
//static_assert(NTDDI_WIN10_VB == MPT_WIN_10_20H2);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_VB
|
||||
//static_assert(NTDDI_WIN10_VB == MPT_WIN_10_21H1);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_VB
|
||||
//static_assert(NTDDI_WIN10_VB == MPT_WIN_10_21H2);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_VB
|
||||
//static_assert(NTDDI_WIN10_VB == MPT_WIN_10_22H2);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_MN
|
||||
//static_assert(NTDDI_WIN10_MN == ???);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_FE
|
||||
//static_assert(NTDDI_WIN10_FE == ???);
|
||||
//#endif
|
||||
#ifdef NTDDI_WIN10_CO
|
||||
static_assert(NTDDI_WIN10_CO == MPT_WIN_11);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN10_NI
|
||||
static_assert(NTDDI_WIN10_NI == MPT_WIN_11_22H2);
|
||||
#endif
|
||||
//#ifdef NTDDI_WIN10_NI
|
||||
//static_assert(NTDDI_WIN10_NI == MPT_WIN_11_23H2);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_CU
|
||||
//static_assert(NTDDI_WIN10_CU == ???);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN10_ZN
|
||||
//static_assert(NTDDI_WIN10_ZN == ???);
|
||||
//#endif
|
||||
//#ifdef NTDDI_WIN11_GA
|
||||
//static_assert(NTDDI_WIN11_GA == ???);
|
||||
//#endif
|
||||
#ifdef NTDDI_WIN11_GE
|
||||
static_assert(NTDDI_WIN11_GE == MPT_WIN_11_24H2);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN11_DT
|
||||
static_assert(NTDDI_WIN11_DT == MPT_WIN_11_25H2);
|
||||
#endif
|
||||
#ifdef NTDDI_WIN11_BR
|
||||
static_assert(NTDDI_WIN11_BR == MPT_WIN_11_26H1);
|
||||
#endif
|
||||
#endif
|
||||
#if defined(WINAPI_FAMILY)
|
||||
#include <winapifamily.h>
|
||||
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
#define MPT_OS_WINDOWS_WINRT 0
|
||||
#else
|
||||
#define MPT_OS_WINDOWS_WINRT 1
|
||||
#endif
|
||||
#define MPT_WIN_API_DESKTOP WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
#define MPT_WIN_API_UNIVERSAL WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PC_APP)
|
||||
#define MPT_WIN_API_STORE_PC WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
|
||||
#define MPT_WIN_API_STORE_PHONE WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
|
||||
#else // !WINAPI_FAMILY
|
||||
#define MPT_OS_WINDOWS_WINRT 0
|
||||
#define MPT_WIN_API_DESKTOP 1
|
||||
#define MPT_WIN_API_UNIVERSAL 0
|
||||
#define MPT_WIN_API_STORE_PC 0
|
||||
#define MPT_WIN_API_STORE_PHONE 0
|
||||
#endif // WINAPI_FAMILY
|
||||
#if defined(NTDDI_VERSION) || defined(_WIN32_WINNT)
|
||||
#define MPT_OS_WINDOWS_WINNT 1
|
||||
#define MPT_OS_WINDOWS_WIN9X 0
|
||||
#define MPT_OS_WINDOWS_WIN32 0
|
||||
#if defined(NTDDI_VERSION)
|
||||
#define MPT_WIN_VERSION NTDDI_VERSION
|
||||
#else
|
||||
#define MPT_WIN_VERSION (_WIN32_WINNT << 16)
|
||||
#endif
|
||||
#elif defined(_WIN32_WINDOWS)
|
||||
#define MPT_OS_WINDOWS_WINNT 0
|
||||
#define MPT_OS_WINDOWS_WIN9X 1
|
||||
#define MPT_OS_WINDOWS_WIN32 0
|
||||
#define MPT_WIN_VERSION (_WIN32_WINDOWS << 16)
|
||||
#elif defined(WINVER)
|
||||
#define MPT_OS_WINDOWS_WINNT 0
|
||||
#define MPT_OS_WINDOWS_WIN9X 0
|
||||
#define MPT_OS_WINDOWS_WIN32 1
|
||||
#define MPT_WIN_VERSION (WINVER << 16)
|
||||
#else
|
||||
// assume modern
|
||||
#define MPT_OS_WINDOWS_WINNT 1
|
||||
#define MPT_OS_WINDOWS_WIN9X 0
|
||||
#define MPT_OS_WINDOWS_WIN32 0
|
||||
#define MPT_WIN_VERSION MPT_WIN_NT4
|
||||
#endif
|
||||
#define MPT_WINRT_AT_LEAST(v) (MPT_OS_WINDOWS_WINRT && MPT_OS_WINDOWS_WINNT && (MPT_WIN_VERSION >= (v)))
|
||||
#define MPT_WINRT_BEFORE(v) (MPT_OS_WINDOWS_WINRT && MPT_OS_WINDOWS_WINNT && (MPT_WIN_VERSION < (v)))
|
||||
#define MPT_WINNT_AT_LEAST(v) (MPT_OS_WINDOWS_WINNT && (MPT_WIN_VERSION >= (v)))
|
||||
#define MPT_WINNT_BEFORE(v) (MPT_OS_WINDOWS_WINNT && (MPT_WIN_VERSION < (v)))
|
||||
#define MPT_WIN9X_AT_LEAST(v) ((MPT_OS_WINDOWS_WINNT || MPT_OS_WINDOWS_WIN9X) && (MPT_WIN_VERSION >= (v)))
|
||||
#define MPT_WIN9X_BEFORE(v) ((MPT_OS_WINDOWS_WINNT || MPT_OS_WINDOWS_WIN9X) && (MPT_WIN_VERSION < (v)))
|
||||
#define MPT_WIN32_AT_LEAST(v) ((MPT_OS_WINDOWS_WINNT || MPT_OS_WINDOWS_WIN9X || MPT_OS_WINDOWS_WIN32) && (MPT_WIN_VERSION >= (v)))
|
||||
#define MPT_WIN32_BEFORE(v) ((MPT_OS_WINDOWS_WINNT || MPT_OS_WINDOWS_WIN9X || MPT_OS_WINDOWS_WIN32) && (MPT_WIN_VERSION < (v)))
|
||||
#if MPT_OS_WINDOWS_WINRT
|
||||
#define MPT_WIN_AT_LEAST(v) MPT_WINRT_AT_LEAST(v)
|
||||
#define MPT_WIN_BEFORE(v) MPT_WINRT_BEFORE(v)
|
||||
#elif MPT_OS_WINDOWS_WINNT
|
||||
#define MPT_WIN_AT_LEAST(v) MPT_WINNT_AT_LEAST(v)
|
||||
#define MPT_WIN_BEFORE(v) MPT_WINNT_BEFORE(v)
|
||||
#elif MPT_OS_WINDOWS_WIN9X
|
||||
#define MPT_WIN_AT_LEAST(v) MPT_WIN9X_AT_LEAST(v)
|
||||
#define MPT_WIN_BEFORE(v) MPT_WIN9X_BEFORE(v)
|
||||
#elif MPT_OS_WINDOWS_WIN32
|
||||
#define MPT_WIN_AT_LEAST(v) MPT_WIN32_AT_LEAST(v)
|
||||
#define MPT_WIN_BEFORE(v) MPT_WIN32_BEFORE(v)
|
||||
#else
|
||||
#define MPT_WIN_AT_LEAST(v) 0
|
||||
#define MPT_WIN_BEFORE(v) 1
|
||||
#endif
|
||||
#if (MPT_OS_WINDOWS_WINNT || MPT_OS_WINDOWS_WINRT) && defined(WDK_NTDDI_VERSION)
|
||||
static_assert(WDK_NTDDI_VERSION >= MPT_WIN_VERSION);
|
||||
#endif
|
||||
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
#define MPT_OS_MACOSX_OR_IOS 1
|
||||
#include <TargetConditionals.h>
|
||||
#if defined(TARGET_OS_OSX)
|
||||
#if (TARGET_OS_OSX != 0)
|
||||
#include <AvailabilityMacros.h>
|
||||
#endif
|
||||
#endif
|
||||
//#if TARGET_IPHONE_SIMULATOR
|
||||
//#elif TARGET_OS_IPHONE
|
||||
//#elif TARGET_OS_MAC
|
||||
//#else
|
||||
//#endif
|
||||
|
||||
|
||||
#elif defined(__HAIKU__)
|
||||
#define MPT_OS_HAIKU 1
|
||||
|
||||
|
||||
#elif defined(__ANDROID__) || defined(ANDROID)
|
||||
#define MPT_OS_ANDROID 1
|
||||
|
||||
|
||||
#elif defined(__linux__)
|
||||
#define MPT_OS_LINUX 1
|
||||
|
||||
|
||||
#elif defined(__DragonFly__)
|
||||
#define MPT_OS_DRAGONFLYBSD 1
|
||||
|
||||
|
||||
#elif defined(__FreeBSD__)
|
||||
#define MPT_OS_FREEBSD 1
|
||||
|
||||
|
||||
#elif defined(__OpenBSD__)
|
||||
#define MPT_OS_OPENBSD 1
|
||||
|
||||
|
||||
#elif defined(__NetBSD__)
|
||||
#define MPT_OS_NETBSD 1
|
||||
|
||||
|
||||
#elif defined(__unix__)
|
||||
#define MPT_OS_GENERIC_UNIX 1
|
||||
|
||||
|
||||
#else
|
||||
#define MPT_OS_UNKNOWN 1
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef MPT_OS_DJGPP
|
||||
#define MPT_OS_DJGPP 0
|
||||
#endif
|
||||
#ifndef MPT_OS_EMSCRIPTEN
|
||||
#define MPT_OS_EMSCRIPTEN 0
|
||||
#endif
|
||||
#ifndef MPT_OS_CYGWIN
|
||||
#define MPT_OS_CYGWIN 0
|
||||
#endif
|
||||
#ifndef MPT_OS_WINDOWS
|
||||
#define MPT_OS_WINDOWS 0
|
||||
#endif
|
||||
#ifndef MPT_OS_WINDOWS_WINRT
|
||||
#define MPT_OS_WINDOWS_WINRT 0
|
||||
#endif
|
||||
#ifndef MPT_OS_WINDOWS_WINNT
|
||||
#define MPT_OS_WINDOWS_WINNT 0
|
||||
#endif
|
||||
#ifndef MPT_OS_WINDOWS_WIN9X
|
||||
#define MPT_OS_WINDOWS_WIN9X 0
|
||||
#endif
|
||||
#ifndef MPT_OS_WINDOWS_WIN32
|
||||
#define MPT_OS_WINDOWS_WIN32 0
|
||||
#endif
|
||||
#ifndef MPT_WINRT_AT_LEAST
|
||||
#define MPT_WINRT_AT_LEAST(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WINRT_BEFORE
|
||||
#define MPT_WINRT_BEFORE(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WINNT_AT_LEAST
|
||||
#define MPT_WINNT_AT_LEAST(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WINNT_BEFORE
|
||||
#define MPT_WINNT_BEFORE(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN9X_AT_LEAST
|
||||
#define MPT_WIN9X_AT_LEAST(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN9X_BEFORE
|
||||
#define MPT_WIN9X_BEFORE(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN32_AT_LEAST
|
||||
#define MPT_WIN32_AT_LEAST(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN32_BEFORE
|
||||
#define MPT_WIN32_BEFORE(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN_AT_LEAST
|
||||
#define MPT_WIN_AT_LEAST(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN_BEFORE
|
||||
#define MPT_WIN_BEFORE(v) 0
|
||||
#endif
|
||||
#ifndef MPT_WIN_API_DESKTOP
|
||||
#define MPT_WIN_API_DESKTOP 0
|
||||
#endif
|
||||
#ifndef MPT_WIN_API_UNIVERSAL
|
||||
#define MPT_WIN_API_UNIVERSAL 0
|
||||
#endif
|
||||
#ifndef MPT_WIN_API_STORE_PC
|
||||
#define MPT_WIN_API_STORE_PC 0
|
||||
#endif
|
||||
#ifndef MPT_WIN_API_STORE_PHONE
|
||||
#define MPT_WIN_API_STORE_PHONE 0
|
||||
#endif
|
||||
#ifndef MPT_OS_MACOSX_OR_IOS
|
||||
#define MPT_OS_MACOSX_OR_IOS 0
|
||||
#endif
|
||||
#ifndef MPT_OS_HAIKU
|
||||
#define MPT_OS_HAIKU 0
|
||||
#endif
|
||||
#ifndef MPT_OS_ANDROID
|
||||
#define MPT_OS_ANDROID 0
|
||||
#endif
|
||||
#ifndef MPT_OS_LINUX
|
||||
#define MPT_OS_LINUX 0
|
||||
#endif
|
||||
#ifndef MPT_OS_DRAGONFLYBSD
|
||||
#define MPT_OS_DRAGONFLYBSD 0
|
||||
#endif
|
||||
#ifndef MPT_OS_FREEBSD
|
||||
#define MPT_OS_FREEBSD 0
|
||||
#endif
|
||||
#ifndef MPT_OS_OPENBSD
|
||||
#define MPT_OS_OPENBSD 0
|
||||
#endif
|
||||
#ifndef MPT_OS_NETBSD
|
||||
#define MPT_OS_NETBSD 0
|
||||
#endif
|
||||
#ifndef MPT_OS_GENERIC_UNIX
|
||||
#define MPT_OS_GENERIC_UNIX 0
|
||||
#endif
|
||||
#ifndef MPT_OS_UNKNOWN
|
||||
#define MPT_OS_UNKNOWN 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define MPT_MODE_KERNEL 0
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_OS_HPP
|
||||
@@ -0,0 +1,432 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_DETECT_QUIRKS_HPP
|
||||
#define MPT_BASE_DETECT_QUIRKS_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_arch.hpp"
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/detect_libc.hpp"
|
||||
#include "mpt/base/detect_libcxx.hpp"
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include <version>
|
||||
#else // !C++20
|
||||
#include <array>
|
||||
#endif // C++20
|
||||
|
||||
#if MPT_LIBCXX_MS
|
||||
// for _ITERATOR_DEBUG_LEVEL
|
||||
#include <array>
|
||||
#endif // MPT_LIBCXX_MS
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
#define MPT_ARCH_QUIRK_NO_SIMD256
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
#if !defined(_MSVC_TRADITIONAL)
|
||||
#define MPT_COMPILER_QUIRK_MSVC_OLD_PREPROCESSOR
|
||||
#else
|
||||
#if _MSVC_TRADITIONAL
|
||||
#define MPT_COMPILER_QUIRK_MSVC_OLD_PREPROCESSOR
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_GCC_BEFORE(9, 1, 0)
|
||||
#define MPT_COMPILER_QUIRK_NO_CONSTEXPR_THROW
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_MSVC && MPT_MSVC_AT_LEAST(2022, 6) && MPT_MSVC_BEFORE(2022, 8) && MPT_ARCH_AARCH64
|
||||
// VS2022 17.6.0 ARM64 gets confused about alignment in std::bit_cast (or equivalent code),
|
||||
// causing an ICE.
|
||||
// See <https://developercommunity.visualstudio.com/t/ICE-when-compiling-for-ARM64-due-to-alig/10367205>.
|
||||
#define MPT_COMPILER_QUIRK_BROKEN_BITCAST
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_BEFORE(20) && MPT_COMPILER_MSVC
|
||||
// Compiler has multiplication/division semantics when shifting signed integers.
|
||||
// In C++20, this behaviour is required by the standard.
|
||||
#define MPT_COMPILER_SHIFT_SIGNED 1
|
||||
#endif
|
||||
|
||||
#ifndef MPT_COMPILER_SHIFT_SIGNED
|
||||
#define MPT_COMPILER_SHIFT_SIGNED 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// This should really be based on __STDCPP_THREADS__,
|
||||
// but that is not defined consistently by GCC or clang. Stupid.
|
||||
// Just assume multithreaded and disable for platforms we know are
|
||||
// singlethreaded later on.
|
||||
#define MPT_PLATFORM_MULTITHREADED 1
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
#undef MPT_PLATFORM_MULTITHREADED
|
||||
#define MPT_PLATFORM_MULTITHREADED 0
|
||||
#endif
|
||||
|
||||
#if (MPT_OS_EMSCRIPTEN && !defined(__EMSCRIPTEN_PTHREADS__))
|
||||
#undef MPT_PLATFORM_MULTITHREADED
|
||||
#define MPT_PLATFORM_MULTITHREADED 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_MSVC_BEFORE(2019, 0) || MPT_GCC_BEFORE(8, 1, 0)
|
||||
#define MPT_COMPILER_QUIRK_NO_AUTO_TEMPLATE_ARGUMENT
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_GCC_BEFORE(11, 1, 0)
|
||||
#define MPT_COMPILER_QUIRK_NO_STDCPP_THREADS
|
||||
#elif MPT_CLANG_BEFORE(12, 0, 0)
|
||||
#define MPT_COMPILER_QUIRK_NO_STDCPP_THREADS
|
||||
#elif (defined(__MINGW32__) || defined(__MINGW64__)) && MPT_LIBCXX_GNU && defined(_GLIBCXX_HAS_GTHREADS) && !defined(__STDCPP_THREADS__)
|
||||
#define MPT_COMPILER_QUIRK_NO_STDCPP_THREADS
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if !MPT_PLATFORM_MULTITHREADED
|
||||
#define MPT_LIBCXX_QUIRK_NO_STD_THREAD
|
||||
#elif !MPT_COMPILER_GENERIC && MPT_OS_WINDOWS && MPT_LIBCXX_GNU && !defined(_GLIBCXX_HAS_GTHREADS)
|
||||
#define MPT_LIBCXX_QUIRK_NO_STD_THREAD
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_WINDOWS && MPT_COMPILER_MSVC
|
||||
#if MPT_WINNT_AT_LEAST(MPT_WIN_VISTA)
|
||||
#define MPT_LIBCXX_QUIRK_COMPLEX_STD_MUTEX
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_EMSCRIPTEN && defined(MPT_BUILD_AUDIOWORKLETPROCESSOR)
|
||||
#define MPT_COMPILER_QUIRK_CHRONO_NO_HIGH_RESOLUTION_CLOCK
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_EMSCRIPTEN && defined(MPT_BUILD_AUDIOWORKLETPROCESSOR)
|
||||
#define MPT_COMPILER_QUIRK_RANDOM_NO_RANDOM_DEVICE
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBCXX_GNU
|
||||
#if !defined(_GLIBCXX_USE_WCHAR_T)
|
||||
#ifndef MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
#define MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__MINGW32__) && !defined(__MINGW64__) && (MPT_OS_WINDOWS_WIN9X || MPT_OS_WINDOWS_WIN32)
|
||||
#ifndef MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
#define MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBCXX_LLVM_BEFORE(14000)
|
||||
#define MPT_COMPILER_QUIRK_NO_STRING_VIEW_ITERATOR_CTOR
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBCXX_GNU_BEFORE(9)
|
||||
#define MPT_COMPILER_QUIRK_NO_FILESYSTEM
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_WINDOWS && MPT_GCC_BEFORE(9, 1, 0)
|
||||
// GCC C++ library has no wchar_t overloads
|
||||
#define MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
|
||||
#if defined(__FAST_MATH__)
|
||||
#define MPT_COMPILER_QUIRK_FASTMATH 1
|
||||
#endif
|
||||
#if defined(__FINITE_MATH_ONLY__)
|
||||
#if (__FINITE_MATH_ONLY__ >= 1)
|
||||
#define MPT_COMPILER_QUIRK_FINITEMATH 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#elif MPT_COMPILER_MSVC
|
||||
|
||||
#if defined(_M_FP_FAST)
|
||||
#define MPT_COMPILER_QUIRK_FASTMATH 1
|
||||
#define MPT_COMPILER_QUIRK_FINITEMATH 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef MPT_COMPILER_QUIRK_FASTMATH
|
||||
#define MPT_COMPILER_QUIRK_FASTMATH 0
|
||||
#endif
|
||||
|
||||
#ifndef MPT_COMPILER_QUIRK_FINITEMATH
|
||||
#define MPT_COMPILER_QUIRK_FINITEMATH 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_GCC && !defined(__arm__)
|
||||
#if defined(_SOFT_FLOAT)
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_EMULATED 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__arm__)
|
||||
|
||||
#if defined(__SOFTFP__)
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_EMULATED 1
|
||||
#else
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_EMULATED 0
|
||||
#endif
|
||||
#if defined(__VFP_FP__)
|
||||
// native-endian IEEE754
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN 0
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754 0
|
||||
#elif defined(__MAVERICK__)
|
||||
// little-endian IEEE754, we assume native-endian though
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN 1
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754 0
|
||||
#else
|
||||
// not IEEE754
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN 1
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754 1
|
||||
#endif
|
||||
|
||||
#elif defined(__mips__)
|
||||
|
||||
#if defined(__mips_soft_float)
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_EMULATED 1
|
||||
#else
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_EMULATED 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if MPT_OS_EMSCRIPTEN
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_PREFER64 1
|
||||
#endif
|
||||
|
||||
#ifndef MPT_COMPILER_QUIRK_FLOAT_PREFER32
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_PREFER32 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_QUIRK_FLOAT_PREFER64
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_PREFER64 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_QUIRK_FLOAT_EMULATED
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_EMULATED 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN 0
|
||||
#endif
|
||||
#ifndef MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754
|
||||
#define MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBC_MINGW
|
||||
// MinGW32 runtime headers require __off64_t when including some C and/or C++ stdlib headers.
|
||||
// This is declared in <sys/types.h>, which howeger is not included in some header chains.
|
||||
#if (defined(__MINGW32__) && !defined(__MINGW64__))
|
||||
#define MPT_LIBC_QUIRK_REQUIRES_SYS_TYPES_H
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBC_DJGPP
|
||||
#define MPT_LIBC_QUIRK_NO_FENV
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_CYGWIN
|
||||
#define MPT_LIBCXX_QUIRK_BROKEN_USER_LOCALE
|
||||
// #define MPT_LIBCXX_QUIRK_ASSUME_USER_LOCALE_UTF8
|
||||
#elif MPT_OS_HAIKU
|
||||
#define MPT_LIBCXX_QUIRK_BROKEN_USER_LOCALE
|
||||
#define MPT_LIBCXX_QUIRK_ASSUME_USER_LOCALE_UTF8
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// #define MPT_LIBCXX_QUIRK_BROKEN_ACTIVE_LOCALE
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_WINDOWS && MPT_LIBCXX_GNU
|
||||
#define MPT_LIBCXX_QUIRK_INCOMPLETE_IS_FUNCTION
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#if MPT_LIBCXX_GNU_BEFORE(10) || MPT_LIBCXX_LLVM_BEFORE(13000) || (MPT_LIBCXX_MS && MPT_MSVC_BEFORE(2022, 0)) || (MPT_LIBCXX_MS && !MPT_COMPILER_MSVC)
|
||||
#define MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_ALGORITHM
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#if MPT_LIBCXX_GNU_BEFORE(12) || MPT_LIBCXX_LLVM_BEFORE(15000) || (MPT_LIBCXX_MS && MPT_MSVC_BEFORE(2022, 0)) || (MPT_LIBCXX_MS && !MPT_COMPILER_MSVC)
|
||||
#ifndef MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_CONTAINER
|
||||
#define MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_CONTAINER
|
||||
#endif
|
||||
#endif
|
||||
#if MPT_LIBCXX_MS
|
||||
// So, in 2025, Microsoft still ships a STL that by default is not standard-compliant with its own default Debug options.
|
||||
// constexpr auto foo = std::vector<int>{}; does not compile with iterator debugging enabled (i.e. in Debug builds).
|
||||
// See <https://developercommunity.visualstudio.com/t/Iterator-Debugging-breaks-C20-constexp/10861623>.
|
||||
#if defined(_ITERATOR_DEBUG_LEVEL)
|
||||
#if (_ITERATOR_DEBUG_LEVEL >= 1)
|
||||
#ifndef MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_CONTAINER
|
||||
#define MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_CONTAINER
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
// Clang 14 is incompatible with libstdc++ 13 in C++20 mode
|
||||
#if MPT_CLANG_BEFORE(15, 0, 0) && MPT_LIBCXX_GNU_AT_LEAST(13)
|
||||
#define MPT_LIBCXX_QUIRK_NO_CHRONO
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#if MPT_LIBCXX_MS && MPT_OS_WINDOWS
|
||||
#if MPT_WIN_BEFORE(MPT_WIN_10_1903)
|
||||
// std::chrono timezones require Windows 10 1903 with VS2022 as of 2022-01-22.
|
||||
// See <https://github.com/microsoft/STL/issues/1911> and
|
||||
// <https://github.com/microsoft/STL/issues/2163>.
|
||||
#define MPT_LIBCXX_QUIRK_NO_CHRONO_DATE
|
||||
#endif
|
||||
#endif
|
||||
#if MPT_LIBCXX_GNU_BEFORE(11)
|
||||
#define MPT_LIBCXX_QUIRK_NO_CHRONO_DATE
|
||||
#elif MPT_LIBCXX_LLVM_BEFORE(7000)
|
||||
#define MPT_LIBCXX_QUIRK_NO_CHRONO_DATE
|
||||
#endif
|
||||
#if MPT_LIBCXX_MS && (MPT_MSVC_BEFORE(2022, 2) || !MPT_COMPILER_MSVC)
|
||||
#elif MPT_LIBCXX_GNU
|
||||
#define MPT_LIBCXX_QUIRK_NO_CHRONO_DATE_PARSE
|
||||
#endif
|
||||
#if MPT_LIBCXX_MS && (MPT_MSVC_BEFORE(2022, 15) || !MPT_COMPILER_MSVC)
|
||||
// Causes massive memory leaks.
|
||||
// See
|
||||
// <https://developercommunity.visualstudio.com/t/stdchronoget-tzdb-list-memory-leak/1644641>
|
||||
// / <https://github.com/microsoft/STL/issues/2504>.
|
||||
#define MPT_LIBCXX_QUIRK_CHRONO_TZ_MEMLEAK
|
||||
#endif
|
||||
#endif
|
||||
#if MPT_LIBCXX_GNU_BEFORE(13)
|
||||
#define MPT_LIBCXX_QUIRK_CHRONO_DATE_NO_ZONED_TIME
|
||||
#endif
|
||||
#if MPT_LIBCXX_LLVM
|
||||
// See <https://github.com/llvm/llvm-project/issues/99982>
|
||||
#define MPT_LIBCXX_QUIRK_CHRONO_DATE_NO_ZONED_TIME
|
||||
#endif
|
||||
#if MPT_MSVC_AT_LEAST(2022, 6) && MPT_MSVC_BEFORE(2022, 7)
|
||||
// std::chrono triggers ICE in VS2022 17.6.0, see <https://developercommunity.visualstudio.com/t/INTERNAL-COMPILER-ERROR-when-compiling-s/10366948>.
|
||||
#define MPT_LIBCXX_QUIRK_CHRONO_DATE_BROKEN_ZONED_TIME
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBCXX_GNU_BEFORE(8)
|
||||
#define MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#elif MPT_LIBCXX_LLVM_BEFORE(7000)
|
||||
#define MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#elif MPT_OS_ANDROID && MPT_LIBCXX_LLVM_BEFORE(8000)
|
||||
#define MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#elif MPT_OS_MACOSX_OR_IOS
|
||||
#if defined(TARGET_OS_OSX)
|
||||
#if TARGET_OS_OSX
|
||||
#if !defined(MAC_OS_X_VERSION_10_15)
|
||||
#define MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#else
|
||||
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_15)
|
||||
#define MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if (MPT_LIBCXX_MS && (MPT_MSVC_BEFORE(2019, 4) || !MPT_COMPILER_MSVC)) || MPT_LIBCXX_GNU_BEFORE(11) || MPT_LIBCXX_LLVM || MPT_LIBCXX_GENERIC
|
||||
#define MPT_LIBCXX_QUIRK_NO_TO_CHARS_FLOAT
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_ANDROID && MPT_LIBCXX_LLVM_BEFORE(7000)
|
||||
#define MPT_LIBCXX_QUIRK_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_ANDROID && MPT_LIBCXX_LLVM_BEFORE(170000)
|
||||
#define MPT_LIBCXX_QUIRK_NO_NUMBERS
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_LIBCXX_GNU_BEFORE(13) || (MPT_LIBCXX_MS && !MPT_MSVC_AT_LEAST(2022, 7)) || MPT_LIBCXX_LLVM
|
||||
#define MPT_LIBCXX_QUIRK_NO_STDFLOAT
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_OS_MACOSX_OR_IOS
|
||||
#if defined(TARGET_OS_OSX)
|
||||
#if TARGET_OS_OSX
|
||||
#if !defined(MAC_OS_X_VERSION_10_14)
|
||||
#define MPT_LIBCXX_QUIRK_NO_OPTIONAL_VALUE
|
||||
#else
|
||||
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_14)
|
||||
#define MPT_LIBCXX_QUIRK_NO_OPTIONAL_VALUE
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_DETECT_QUIRKS_HPP
|
||||
@@ -0,0 +1,188 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_FLOAT_HPP
|
||||
#define MPT_BASE_FLOAT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <limits>
|
||||
#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
|
||||
#include <stdfloat>
|
||||
#endif // C++23
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
// fp half
|
||||
// n/a
|
||||
|
||||
// fp single
|
||||
using single_float = float;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL single_float operator""_fs(long double lit) noexcept {
|
||||
return static_cast<single_float>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
// fp double
|
||||
using double_float = double;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL double_float operator""_fd(long double lit) noexcept {
|
||||
return static_cast<double_float>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
// fp extended
|
||||
using extended_float = long double;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL extended_float operator""_fe(long double lit) noexcept {
|
||||
return static_cast<extended_float>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
// fp quad
|
||||
// n/a
|
||||
|
||||
#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
|
||||
#if defined(__STDCPP_FLOAT16_T__)
|
||||
#if (__STDCPP_FLOAT16_T__ == 1)
|
||||
#define MPT_BASE_STDFLOAT_FLOAT16
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
|
||||
#if defined(__STDCPP_FLOAT32_T__)
|
||||
#if (__STDCPP_FLOAT32_T__ == 1)
|
||||
#define MPT_BASE_STDFLOAT_FLOAT32
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
|
||||
#if defined(__STDCPP_FLOAT64_T__)
|
||||
#if (__STDCPP_FLOAT64_T__ == 1)
|
||||
#define MPT_BASE_STDFLOAT_FLOAT64
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#if MPT_CXX_AT_LEAST(23) && !defined(MPT_LIBCXX_QUIRK_NO_STDFLOAT)
|
||||
#if defined(__STDCPP_FLOAT128_T__)
|
||||
#if (__STDCPP_FLOAT128_T__ == 1)
|
||||
#define MPT_BASE_STDFLOAT_FLOAT128
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(MPT_BASE_STDFLOAT_FLOAT16)
|
||||
using stdfloat16 = std::float16_t;
|
||||
#else
|
||||
using stdfloat16 = std::conditional<sizeof(float) == 2, float, std::conditional<sizeof(double) == 2, double, std::conditional<sizeof(long double) == 2, long double, float>::type>::type>::type;
|
||||
#endif
|
||||
|
||||
#if defined(MPT_BASE_STDFLOAT_FLOAT32)
|
||||
using stdfloat32 = std::float32_t;
|
||||
#else
|
||||
using stdfloat32 = std::conditional<sizeof(float) == 4, float, std::conditional<sizeof(double) == 4, double, std::conditional<sizeof(long double) == 4, long double, float>::type>::type>::type;
|
||||
#endif
|
||||
|
||||
#if defined(MPT_BASE_STDFLOAT_FLOAT64)
|
||||
using stdfloat64 = std::float64_t;
|
||||
#else
|
||||
using stdfloat64 = std::conditional<sizeof(float) == 8, float, std::conditional<sizeof(double) == 8, double, std::conditional<sizeof(long double) == 8, long double, double>::type>::type>::type;
|
||||
#endif
|
||||
|
||||
#if defined(MPT_BASE_STDFLOAT_FLOAT128)
|
||||
using stdfloat128 = std::float128_t;
|
||||
#else
|
||||
using stdfloat128 = std::conditional<sizeof(float) == 16, float, std::conditional<sizeof(double) == 16, double, std::conditional<sizeof(long double) == 16, long double, long double>::type>::type>::type;
|
||||
#endif
|
||||
|
||||
#undef MPT_BASE_STDFLOAT_FLOAT16
|
||||
#undef MPT_BASE_STDFLOAT_FLOAT32
|
||||
#undef MPT_BASE_STDFLOAT_FLOAT64
|
||||
#undef MPT_BASE_STDFLOAT_FLOAT128
|
||||
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL stdfloat16 operator""_stdf16(long double lit) noexcept {
|
||||
return static_cast<stdfloat16>(lit);
|
||||
}
|
||||
MPT_CONSTEVAL stdfloat32 operator""_stdf32(long double lit) noexcept {
|
||||
return static_cast<stdfloat32>(lit);
|
||||
}
|
||||
MPT_CONSTEVAL stdfloat64 operator""_stdf64(long double lit) noexcept {
|
||||
return static_cast<stdfloat64>(lit);
|
||||
}
|
||||
MPT_CONSTEVAL stdfloat128 operator""_stdf128(long double lit) noexcept {
|
||||
return static_cast<stdfloat128>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
// fast floating point types of roughly requested size
|
||||
|
||||
using fastfloat32 = std::conditional<sizeof(float) == 4, float, std::conditional<sizeof(double) == 4, double, std::conditional<sizeof(long double) == 4, long double, float>::type>::type>::type;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL fastfloat32 operator""_ff32(long double lit) noexcept {
|
||||
return static_cast<fastfloat32>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
using fastfloat64 = std::conditional<sizeof(float) == 8, float, std::conditional<sizeof(double) == 8, double, std::conditional<sizeof(long double) == 8, long double, double>::type>::type>::type;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL fastfloat64 operator""_ff64(long double lit) noexcept {
|
||||
return static_cast<fastfloat64>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
// floating point type of roughly requested size
|
||||
|
||||
using somefloat32 = std::conditional<sizeof(fastfloat32) == 4, fastfloat32, std::conditional<sizeof(stdfloat32) == 4, stdfloat32, float>::type>::type;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL somefloat32 operator""_sf32(long double lit) noexcept {
|
||||
return static_cast<somefloat32>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
using somefloat64 = std::conditional<sizeof(fastfloat64) == 8, fastfloat64, std::conditional<sizeof(stdfloat64) == 8, stdfloat64, double>::type>::type;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL somefloat64 operator""_sf64(long double lit) noexcept {
|
||||
return static_cast<somefloat64>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
template <typename T>
|
||||
struct float_traits {
|
||||
static constexpr bool is_float = !std::numeric_limits<T>::is_integer;
|
||||
static constexpr bool is_hard = is_float && !MPT_COMPILER_QUIRK_FLOAT_EMULATED;
|
||||
static constexpr bool is_soft = is_float && MPT_COMPILER_QUIRK_FLOAT_EMULATED;
|
||||
static constexpr bool is_float16 = is_float && (sizeof(T) == 2);
|
||||
static constexpr bool is_float32 = is_float && (sizeof(T) == 4);
|
||||
static constexpr bool is_float64 = is_float && (sizeof(T) == 8);
|
||||
static constexpr bool is_float128 = is_float && (sizeof(T) == 16);
|
||||
static constexpr bool is_native_endian = is_float && !MPT_COMPILER_QUIRK_FLOAT_NOTNATIVEENDIAN;
|
||||
static constexpr bool is_ieee754_binary = is_float && std::numeric_limits<T>::is_iec559 && !MPT_COMPILER_QUIRK_FLOAT_NOTIEEE754;
|
||||
static constexpr bool is_preferred = is_float && ((is_float32 && MPT_COMPILER_QUIRK_FLOAT_PREFER32) || (is_float64 && MPT_COMPILER_QUIRK_FLOAT_PREFER64));
|
||||
};
|
||||
|
||||
// prefer smaller floats, but try to use IEEE754 floats
|
||||
using nativefloat =
|
||||
std::conditional<mpt::float_traits<somefloat32>::is_preferred, somefloat32, std::conditional<mpt::float_traits<somefloat64>::is_preferred, somefloat64, std::conditional<std::numeric_limits<float>::is_iec559, float, std::conditional<std::numeric_limits<double>::is_iec559, double, std::conditional<std::numeric_limits<long double>::is_iec559, long double, float>::type>::type>::type>::type>::type;
|
||||
namespace float_literals {
|
||||
MPT_CONSTEVAL nativefloat operator""_nf(long double lit) noexcept {
|
||||
return static_cast<nativefloat>(lit);
|
||||
}
|
||||
} // namespace float_literals
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_FLOAT_HPP
|
||||
@@ -0,0 +1,33 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_INTEGER_HPP
|
||||
#define MPT_BASE_INTEGER_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
using int8 = std::int8_t;
|
||||
using int16 = std::int16_t;
|
||||
using int32 = std::int32_t;
|
||||
using int64 = std::int64_t;
|
||||
using uint8 = std::uint8_t;
|
||||
using uint16 = std::uint16_t;
|
||||
using uint32 = std::uint32_t;
|
||||
using uint64 = std::uint64_t;
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_INTEGER_HPP
|
||||
@@ -0,0 +1,218 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_MACROS_HPP
|
||||
#define MPT_BASE_MACROS_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#if MPT_COMPILER_MSVC && MPT_OS_WINDOWS
|
||||
#include <windows.h>
|
||||
#endif // MPT_COMPILER_MSVC && MPT_OS_WINDOWS
|
||||
|
||||
|
||||
|
||||
// Advanced inline attributes
|
||||
#if MPT_COMPILER_MSVC
|
||||
#define MPT_FORCEINLINE __forceinline
|
||||
#define MPT_NOINLINE __declspec(noinline)
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
#define MPT_FORCEINLINE __attribute__((always_inline)) inline
|
||||
#define MPT_NOINLINE __attribute__((noinline))
|
||||
#else
|
||||
#define MPT_FORCEINLINE inline
|
||||
#define MPT_NOINLINE
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// constexpr
|
||||
|
||||
#define MPT_CONSTEXPRINLINE constexpr MPT_FORCEINLINE
|
||||
|
||||
#if MPT_CXX_AT_LEAST(23)
|
||||
#define MPT_CONSTEXPR20_FUN constexpr MPT_FORCEINLINE
|
||||
#define MPT_CONSTEXPR20_VAR constexpr
|
||||
#define MPT_CONSTEXPR23_FUN constexpr MPT_FORCEINLINE
|
||||
#define MPT_CONSTEXPR23_VAR constexpr
|
||||
#elif MPT_CXX_AT_LEAST(20)
|
||||
#define MPT_CONSTEXPR20_FUN constexpr MPT_FORCEINLINE
|
||||
#define MPT_CONSTEXPR20_VAR constexpr
|
||||
#define MPT_CONSTEXPR23_FUN MPT_FORCEINLINE
|
||||
#define MPT_CONSTEXPR23_VAR const
|
||||
#else // C++
|
||||
#define MPT_CONSTEXPR20_FUN MPT_FORCEINLINE
|
||||
#define MPT_CONSTEXPR20_VAR const
|
||||
#define MPT_CONSTEXPR23_FUN MPT_FORCEINLINE
|
||||
#define MPT_CONSTEXPR23_VAR const
|
||||
#endif // C++
|
||||
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_ALGORITHM)
|
||||
#define MPT_CONSTEXPR20_ALGORITHM_FUN MPT_CONSTEXPR20_FUN
|
||||
#define MPT_CONSTEXPR20_ALGORITHM_VAR MPT_CONSTEXPR20_VAR
|
||||
#else
|
||||
#define MPT_CONSTEXPR20_ALGORITHM_FUN MPT_CONSTEXPR23_FUN
|
||||
#define MPT_CONSTEXPR20_ALGORITHM_VAR MPT_CONSTEXPR23_VAR
|
||||
#endif
|
||||
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_CONTAINER)
|
||||
#define MPT_CONSTEXPR20_CONTAINER_FUN MPT_CONSTEXPR20_FUN
|
||||
#define MPT_CONSTEXPR20_CONTAINER_VAR MPT_CONSTEXPR20_VAR
|
||||
#else
|
||||
#define MPT_CONSTEXPR20_CONTAINER_FUN MPT_CONSTEXPR23_FUN
|
||||
#define MPT_CONSTEXPR20_CONTAINER_VAR MPT_CONSTEXPR23_VAR
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#define MPT_CONSTEVAL consteval
|
||||
#else // !C++20
|
||||
// fallback to constexpr
|
||||
#define MPT_CONSTEVAL MPT_CONSTEXPRINLINE
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#define MPT_CONSTEVAL_NOEXCEPT noexcept
|
||||
#else // !C++20
|
||||
#define MPT_CONSTEVAL_NOEXCEPT
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#define MPT_FORCE_CONSTEXPR_EXPRESSION(expr) [&]() { \
|
||||
constexpr auto x = (expr); \
|
||||
return x; \
|
||||
}()
|
||||
#define MPT_FORCE_CONSTEXPR_VALUE(val) []() { \
|
||||
constexpr auto x = (val); \
|
||||
return x; \
|
||||
}()
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
// this assumes that for C++20, a consteval function will be used
|
||||
#define MPT_FORCE_CONSTEVAL_EXPRESSION(expr) (expr)
|
||||
#define MPT_FORCE_CONSTEVAL_VALUE(val) (val)
|
||||
#else // !C++20
|
||||
#define MPT_FORCE_CONSTEVAL_EXPRESSION(expr) [&]() { \
|
||||
constexpr auto x = (expr); \
|
||||
return x; \
|
||||
}()
|
||||
#define MPT_FORCE_CONSTEVAL_VALUE(val) []() { \
|
||||
constexpr auto x = (val); \
|
||||
return x; \
|
||||
}()
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#define MPT_CONSTINIT constinit
|
||||
#else // !C++20
|
||||
// fallback to nothing
|
||||
#define MPT_CONSTINIT
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#define MPT_IS_CONSTANT_EVALUATED20() std::is_constant_evaluated()
|
||||
#define MPT_IS_CONSTANT_EVALUATED() std::is_constant_evaluated()
|
||||
#else // !C++20
|
||||
#define MPT_IS_CONSTANT_EVALUATED20() false
|
||||
// this pessimizes the case for C++17 by always assuming constexpr context, which implies always running constexpr-friendly code
|
||||
#define MPT_IS_CONSTANT_EVALUATED() true
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
#define MPT_MAYBE_CONSTANT_IF(x) \
|
||||
__pragma(warning(push)) \
|
||||
__pragma(warning(disable : 4127)) \
|
||||
if (x) \
|
||||
__pragma(warning(pop)) \
|
||||
/**/
|
||||
#endif
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
#define MPT_MAYBE_CONSTANT_IF(x) \
|
||||
_Pragma("GCC diagnostic push") \
|
||||
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"") \
|
||||
if (x) \
|
||||
_Pragma("GCC diagnostic pop") \
|
||||
/**/
|
||||
#endif
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#define MPT_MAYBE_CONSTANT_IF(x) \
|
||||
_Pragma("clang diagnostic push") \
|
||||
_Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") \
|
||||
_Pragma("clang diagnostic ignored \"-Wunreachable-code\"") \
|
||||
_Pragma("clang diagnostic ignored \"-Wtautological-constant-out-of-range-compare\"") \
|
||||
_Pragma("clang diagnostic ignored \"-Wtype-limits\"") \
|
||||
if (x) \
|
||||
_Pragma("clang diagnostic pop") \
|
||||
/**/
|
||||
#endif
|
||||
|
||||
#if !defined(MPT_MAYBE_CONSTANT_IF)
|
||||
// MPT_MAYBE_CONSTANT_IF disables compiler warnings for conditions that may in some case be either always false or always true (this may turn out to be useful in ASSERTions in some cases).
|
||||
#define MPT_MAYBE_CONSTANT_IF(x) if (x)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_COMPILER_MSVC && MPT_OS_WINDOWS
|
||||
#define MPT_UNUSED(x) UNREFERENCED_PARAMETER(x)
|
||||
#else
|
||||
#define MPT_UNUSED(x) static_cast<void>(x)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define MPT_DISCARD(expr) static_cast<void>(expr)
|
||||
|
||||
|
||||
|
||||
// Use MPT_RESTRICT to indicate that a pointer is guaranteed to not be aliased.
|
||||
#if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
#define MPT_RESTRICT __restrict
|
||||
#else
|
||||
#define MPT_RESTRICT
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(23) && !MPT_GCC_BEFORE(13, 0, 0) && !MPT_CLANG_BEFORE(19, 0, 0) && !MPT_COMPILER_MSVC
|
||||
#define MPT_ASSUME(expr) [[assume(expr)]]
|
||||
#else // !C++23
|
||||
#if MPT_COMPILER_CLANG
|
||||
#define MPT_ASSUME(expr) __builtin_assume(expr)
|
||||
#endif
|
||||
#if MPT_COMPILER_MSVC
|
||||
#define MPT_ASSUME(expr) __assume(expr)
|
||||
#endif
|
||||
#if MPT_COMPILER_GCC
|
||||
#define MPT_ASSUME(expr) \
|
||||
do { \
|
||||
if (!expr) { \
|
||||
__builtin_unreachable(); \
|
||||
} \
|
||||
} while (0)
|
||||
#endif
|
||||
#if !defined(MPT_ASSUME)
|
||||
#define MPT_ASSUME(expr) MPT_DISCARD(expr)
|
||||
#endif
|
||||
#endif // C++23
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_MACROS_HPP
|
||||
@@ -0,0 +1,110 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_MATH_HPP
|
||||
#define MPT_BASE_MATH_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
|
||||
inline long double log2(const long double val) {
|
||||
return static_cast<long double>(::log2(static_cast<double>(val)));
|
||||
}
|
||||
|
||||
inline double log2(const double val) {
|
||||
return ::log2(val);
|
||||
}
|
||||
|
||||
inline float log2(const float val) {
|
||||
return ::log2f(val);
|
||||
}
|
||||
|
||||
#else // !MPT_OS_DJGPP
|
||||
|
||||
// C++11 std::log2
|
||||
using std::log2;
|
||||
|
||||
#endif // MPT_OS_DJGPP
|
||||
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
|
||||
inline long double round(const long double val) {
|
||||
return ::roundl(val);
|
||||
}
|
||||
|
||||
inline double round(const double val) {
|
||||
return ::round(val);
|
||||
}
|
||||
|
||||
inline float round(const float val) {
|
||||
return ::roundf(val);
|
||||
}
|
||||
|
||||
#else // !MPT_OS_DJGPP
|
||||
|
||||
// C++11 std::round
|
||||
using std::round;
|
||||
|
||||
#endif // MPT_OS_DJGPP
|
||||
|
||||
|
||||
#if MPT_OS_DJGPP
|
||||
|
||||
inline long double trunc(const long double val) {
|
||||
return ::truncl(val);
|
||||
}
|
||||
|
||||
inline double trunc(const double val) {
|
||||
return ::trunc(val);
|
||||
}
|
||||
|
||||
inline float trunc(const float val) {
|
||||
return ::truncf(val);
|
||||
}
|
||||
|
||||
#else // !MPT_OS_DJGPP
|
||||
|
||||
// C++11 std::trunc
|
||||
using std::trunc;
|
||||
|
||||
#endif // MPT_OS_DJGPP
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline T sanitize_nan(T val) {
|
||||
static_assert(std::is_floating_point<T>::value);
|
||||
if (std::isnan(val)) {
|
||||
return T(0.0);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline T safe_clamp(T v, T lo, T hi) {
|
||||
static_assert(std::is_floating_point<T>::value);
|
||||
return std::clamp(mpt::sanitize_nan(v), lo, hi);
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_MATH_HPP
|
||||
@@ -0,0 +1,300 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_MEMORY_HPP
|
||||
#define MPT_BASE_MEMORY_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/span.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
using byte_span = mpt::span<std::byte>;
|
||||
using const_byte_span = mpt::span<const std::byte>;
|
||||
|
||||
|
||||
|
||||
// Tell which types are safe for mpt::byte_cast.
|
||||
// signed char is actually not allowed to alias into an object representation,
|
||||
// which means that, if the actual type is not itself signed char but char or
|
||||
// unsigned char instead, dereferencing the signed char pointer is undefined
|
||||
// behaviour.
|
||||
template <typename T>
|
||||
struct is_byte_castable : public std::false_type { };
|
||||
template <>
|
||||
struct is_byte_castable<char> : public std::true_type { };
|
||||
template <>
|
||||
struct is_byte_castable<unsigned char> : public std::true_type { };
|
||||
template <>
|
||||
struct is_byte_castable<std::byte> : public std::true_type { };
|
||||
template <>
|
||||
struct is_byte_castable<const char> : public std::true_type { };
|
||||
template <>
|
||||
struct is_byte_castable<const unsigned char> : public std::true_type { };
|
||||
template <>
|
||||
struct is_byte_castable<const std::byte> : public std::true_type { };
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct is_byte : public std::false_type { };
|
||||
template <>
|
||||
struct is_byte<std::byte> : public std::true_type { };
|
||||
template <>
|
||||
struct is_byte<const std::byte> : public std::true_type { };
|
||||
|
||||
|
||||
template <typename T>
|
||||
constexpr bool declare_binary_safe(const T &) noexcept {
|
||||
return false;
|
||||
}
|
||||
|
||||
constexpr bool declare_binary_safe(const char &) noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint8 &) noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int8 &) noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const std::byte &) noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Tell which types are safe to binary write into files.
|
||||
// By default, no types are safe.
|
||||
// When a safe type gets defined,
|
||||
// also specialize this template so that IO functions will work.
|
||||
template <typename T>
|
||||
struct is_binary_safe : public std::conditional<declare_binary_safe(T{}), std::true_type, std::false_type>::type { };
|
||||
|
||||
// Generic Specialization for arrays.
|
||||
template <typename T, std::size_t N>
|
||||
struct is_binary_safe<T[N]> : public is_binary_safe<typename std::remove_const<T>::type> { };
|
||||
template <typename T, std::size_t N>
|
||||
struct is_binary_safe<const T[N]> : public is_binary_safe<typename std::remove_const<T>::type> { };
|
||||
template <typename T, std::size_t N>
|
||||
struct is_binary_safe<std::array<T, N>> : public is_binary_safe<typename std::remove_const<T>::type> { };
|
||||
template <typename T, std::size_t N>
|
||||
struct is_binary_safe<const std::array<T, N>> : public is_binary_safe<typename std::remove_const<T>::type> { };
|
||||
|
||||
template <typename T>
|
||||
struct is_binary_safe<mpt::span<T>> : public is_binary_safe<typename std::remove_const<T>::type> { };
|
||||
template <typename T>
|
||||
struct is_binary_safe<const mpt::span<T>> : public is_binary_safe<typename std::remove_const<T>::type> { };
|
||||
|
||||
|
||||
template <typename T>
|
||||
constexpr bool check_binary_size(std::size_t size) noexcept {
|
||||
return true
|
||||
&& (sizeof(T) == size)
|
||||
&& (alignof(T) == 1)
|
||||
&& std::is_standard_layout<T>::value
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_HAS_UNIQUE_OBJECT_REPRESENTATIONS)
|
||||
&& std::has_unique_object_representations<T>::value
|
||||
#endif
|
||||
&& mpt::is_binary_safe<T>::value;
|
||||
}
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
struct byte_cast_impl {
|
||||
inline Tdst operator()(Tsrc src) const noexcept {
|
||||
static_assert(sizeof(Tsrc) == sizeof(std::byte));
|
||||
static_assert(sizeof(Tdst) == sizeof(std::byte));
|
||||
// not checking is_byte_castable here because we are actually
|
||||
// doing a static_cast and converting the value
|
||||
static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
|
||||
static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
|
||||
return static_cast<Tdst>(src);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
struct byte_cast_impl<mpt::span<Tdst>, mpt::span<Tsrc>> {
|
||||
inline mpt::span<Tdst> operator()(mpt::span<Tsrc> src) const noexcept {
|
||||
static_assert(sizeof(Tsrc) == sizeof(std::byte));
|
||||
static_assert(sizeof(Tdst) == sizeof(std::byte));
|
||||
static_assert(mpt::is_byte_castable<Tsrc>::value);
|
||||
static_assert(mpt::is_byte_castable<Tdst>::value);
|
||||
static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
|
||||
static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
|
||||
return mpt::as_span(mpt::byte_cast_impl<Tdst *, Tsrc *>()(src.data()), mpt::byte_cast_impl<Tdst *, Tsrc *>()(src.data() + src.size()));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
struct byte_cast_impl<Tdst *, Tsrc *> {
|
||||
inline Tdst * operator()(Tsrc * src) const noexcept {
|
||||
static_assert(sizeof(Tsrc) == sizeof(std::byte));
|
||||
static_assert(sizeof(Tdst) == sizeof(std::byte));
|
||||
static_assert(mpt::is_byte_castable<Tsrc>::value);
|
||||
static_assert(mpt::is_byte_castable<Tdst>::value);
|
||||
static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
|
||||
static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
|
||||
return reinterpret_cast<Tdst *>(src);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
struct void_cast_impl;
|
||||
|
||||
template <typename Tdst>
|
||||
struct void_cast_impl<Tdst *, void *> {
|
||||
inline Tdst * operator()(void * src) const noexcept {
|
||||
static_assert(sizeof(Tdst) == sizeof(std::byte));
|
||||
static_assert(mpt::is_byte_castable<Tdst>::value);
|
||||
static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
|
||||
return reinterpret_cast<Tdst *>(src);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tdst>
|
||||
struct void_cast_impl<Tdst *, const void *> {
|
||||
inline Tdst * operator()(const void * src) const noexcept {
|
||||
static_assert(sizeof(Tdst) == sizeof(std::byte));
|
||||
static_assert(mpt::is_byte_castable<Tdst>::value);
|
||||
static_assert(std::is_integral<Tdst>::value || mpt::is_byte<Tdst>::value);
|
||||
return reinterpret_cast<Tdst *>(src);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tsrc>
|
||||
struct void_cast_impl<void *, Tsrc *> {
|
||||
inline void * operator()(Tsrc * src) const noexcept {
|
||||
static_assert(sizeof(Tsrc) == sizeof(std::byte));
|
||||
static_assert(mpt::is_byte_castable<Tsrc>::value);
|
||||
static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
|
||||
return reinterpret_cast<void *>(src);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tsrc>
|
||||
struct void_cast_impl<const void *, Tsrc *> {
|
||||
inline const void * operator()(Tsrc * src) const noexcept {
|
||||
static_assert(sizeof(Tsrc) == sizeof(std::byte));
|
||||
static_assert(mpt::is_byte_castable<Tsrc>::value);
|
||||
static_assert(std::is_integral<Tsrc>::value || mpt::is_byte<Tsrc>::value);
|
||||
return reinterpret_cast<const void *>(src);
|
||||
}
|
||||
};
|
||||
|
||||
// casts between different byte (char) types or pointers to these types
|
||||
template <typename Tdst, typename Tsrc>
|
||||
inline Tdst byte_cast(Tsrc src) noexcept {
|
||||
return byte_cast_impl<Tdst, Tsrc>()(src);
|
||||
}
|
||||
|
||||
// casts between pointers to void and pointers to byte
|
||||
template <typename Tdst, typename Tsrc>
|
||||
inline Tdst void_cast(Tsrc src) noexcept {
|
||||
return void_cast_impl<Tdst, Tsrc>()(src);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
MPT_CONSTEXPRINLINE std::byte as_byte(T src) noexcept {
|
||||
static_assert(std::is_integral<T>::value);
|
||||
return static_cast<std::byte>(static_cast<uint8>(src));
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct as_raw_memory_impl {
|
||||
inline mpt::const_byte_span operator()(const T & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(&v), sizeof(T));
|
||||
}
|
||||
inline mpt::byte_span operator()(T & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<std::byte *>(&v), sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct as_raw_memory_impl<T[N]> {
|
||||
inline mpt::const_byte_span operator()(const T (&v)[N]) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(v), N * sizeof(T));
|
||||
}
|
||||
inline mpt::byte_span operator()(T (&v)[N]) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<std::byte *>(v), N * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct as_raw_memory_impl<const T[N]> {
|
||||
inline mpt::const_byte_span operator()(const T (&v)[N]) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(v), N * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct as_raw_memory_impl<mpt::span<T>> {
|
||||
inline mpt::const_byte_span operator()(const mpt::span<const T> & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
|
||||
}
|
||||
inline mpt::byte_span operator()(const mpt::span<T> & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<std::byte *>(v.data()), v.size() * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct as_raw_memory_impl<mpt::span<const T>> {
|
||||
inline mpt::const_byte_span operator()(const mpt::span<const T> & v) const {
|
||||
static_assert(mpt::is_binary_safe<typename std::remove_const<T>::type>::value);
|
||||
return mpt::as_span(reinterpret_cast<const std::byte *>(v.data()), v.size() * sizeof(T));
|
||||
}
|
||||
};
|
||||
|
||||
// In order to be able to partially specialize it,
|
||||
// as_raw_memory is implemented via a class template.
|
||||
// Do not overload or specialize as_raw_memory directly.
|
||||
// Using a wrapper (by default just around a cast to const std::byte *),
|
||||
// allows for implementing raw memory access
|
||||
// via on-demand generating a cached serialized representation.
|
||||
template <typename T>
|
||||
inline mpt::const_byte_span as_raw_memory(const T & v) {
|
||||
return mpt::as_raw_memory_impl<T>()(v);
|
||||
}
|
||||
template <typename T>
|
||||
inline mpt::byte_span as_raw_memory(T & v) {
|
||||
return mpt::as_raw_memory_impl<T>()(v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <class T>
|
||||
inline void memclear(T & x) {
|
||||
static_assert(std::is_standard_layout<T>::value);
|
||||
static_assert((std::is_trivially_default_constructible<T>::value && std::is_trivially_copyable<T>::value) || mpt::is_binary_safe<T>::value);
|
||||
std::memset(&x, 0, sizeof(T));
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_MEMORY_HPP
|
||||
@@ -0,0 +1,149 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_NAMESPACE_HPP
|
||||
#define MPT_BASE_NAMESPACE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/version.hpp"
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
|
||||
|
||||
#if !defined(MPT_INLINE_NS)
|
||||
|
||||
#define MPT_BUILD_VERSION_NAMESPACE_IMPL(a, b, c, d) v##a##_##b##_##c##_##d
|
||||
#define MPT_BUILD_VERSION_NAMESPACE(a, b, c, d) MPT_BUILD_VERSION_NAMESPACE_IMPL(a, b, c, d)
|
||||
|
||||
#define MPT_VERSION_NAMESPACE MPT_BUILD_VERSION_NAMESPACE(MPT_VERSION_MAJOR, MPT_VERSION_MINOR, MPT_VERSION_PATCH, MPT_VERSION_BUILD)
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
#ifdef UNICODE
|
||||
#define MPT_VERSION_ABI_OS u
|
||||
#else
|
||||
#define MPT_VERSION_ABI_OS 8
|
||||
#endif
|
||||
#else
|
||||
#define MPT_VERSION_ABI_OS _
|
||||
#endif
|
||||
|
||||
#if MPT_LIBC_GENERIC
|
||||
#define MPT_VERSION_ABI_LIBC _
|
||||
#elif MPT_LIBC_MS
|
||||
#if MPT_LIBC_MS_UCRT
|
||||
#if MPT_LIBC_MS_SHARED
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC UMDd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC UMDr
|
||||
#endif
|
||||
#elif MPT_LIBC_MS_STATIC
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC UMTd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC UMTr
|
||||
#endif
|
||||
#else
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC UMd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC UMr
|
||||
#endif
|
||||
#endif
|
||||
#elif MPT_LIBC_MS_MSVCRT
|
||||
#if MPT_LIBC_MS_SHARED
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC MMDd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC MMDr
|
||||
#endif
|
||||
#elif MPT_LIBC_MS_STATIC
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC MMTd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC MMTr
|
||||
#endif
|
||||
#else
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC MMd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC MMr
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
#if MPT_LIBC_MS_SHARED
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC MDd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC MDr
|
||||
#endif
|
||||
#elif MPT_LIBC_MS_STATIC
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC MTd
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC MTr
|
||||
#endif
|
||||
#else
|
||||
#ifdef MPT_LIBC_MS_DEBUG
|
||||
#define MPT_VERSION_ABI_LIBC Md
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC Mr
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#elif MPT_LIBC_GLIBC
|
||||
#define MPT_VERSION_ABI_LIBC G
|
||||
#elif MPT_LIBC_MINGW
|
||||
#if MPT_LIBC_MINGW_UCRT
|
||||
#define MPT_VERSION_ABI_LIBC MWU
|
||||
#elif MPT_LIBC_MINGW_MSVCRT
|
||||
#define MPT_VERSION_ABI_LIBC MWM
|
||||
#elif MPT_LIBC_MINGW_CRTDLL
|
||||
#define MPT_VERSION_ABI_LIBC MWC
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC MW
|
||||
#endif
|
||||
#elif MPT_LIBC_BIONIC
|
||||
#define MPT_VERSION_ABI_LIBC B
|
||||
#elif MPT_LIBC_APPLE
|
||||
#define MPT_VERSION_ABI_LIBC A
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC _
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define MPT_VERSION_ABI_LIBC_DEBUG _
|
||||
#else
|
||||
#define MPT_VERSION_ABI_LIBC_DEBUG D
|
||||
#endif
|
||||
|
||||
#define MPT_BUILD_ABI_NAMESPACE_IMPL(a, b, c) ABI_##a##_##b
|
||||
#define MPT_BUILD_ABI_NAMESPACE(a, b, c) MPT_BUILD_ABI_NAMESPACE_IMPL(a, b, c)
|
||||
|
||||
#define MPT_ABI_NAMESPACE MPT_BUILD_ABI_NAMESPACE(MPT_VERSION_ABI_OS, MPT_VERSION_ABI_LIBC, MPT_VERSION_ABI_LIBC_DEBUG)
|
||||
|
||||
#if !defined(MPT_PROJECT_NAMESPACE)
|
||||
MPT_WARNING("Please #define MPT_PROJECT_NAMESPACE or #define MPT_INLINE_NS in build configuration.")
|
||||
#define MPT_PROJECT_NAMESPACE x
|
||||
#endif // !MPT_PROJECT_NAMESPACE
|
||||
|
||||
#define MPT_BUILD_INLINE_NS_IMPL(a, b, c) a##_##b##_##c
|
||||
#define MPT_BUILD_INLINE_NS(a, b, c) MPT_BUILD_INLINE_NS_IMPL(a, b, c)
|
||||
|
||||
#define MPT_INLINE_NS MPT_BUILD_INLINE_NS(MPT_VERSION_NAMESPACE, MPT_ABI_NAMESPACE, MPT_PROJECT_NAMESPACE)
|
||||
|
||||
#endif // !MPT_INLINE_NS
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_NAMESPACE_HPP
|
||||
@@ -0,0 +1,178 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_NUMBRES_HPP
|
||||
#define MPT_BASE_NUMBRES_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/detect_quirks.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && !defined(MPT_LIBCXX_QUIRK_NO_NUMBERS)
|
||||
#include <numbers>
|
||||
#else
|
||||
#include <type_traits>
|
||||
#include <cmath>
|
||||
#include <math.h>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace numbers {
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && !defined(MPT_LIBCXX_QUIRK_NO_NUMBERS)
|
||||
|
||||
template <typename T>
|
||||
inline constexpr T e_v = std::numbers::e_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T log2e_v = std::numbers::log2e_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T log10e_v = std::numbers::log10e_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T pi_v = std::numbers::pi_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T inv_pi_v = std::numbers::inv_pi_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T inv_sqrtpi_v = std::numbers::inv_sqrtpi_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T ln2_v = std::numbers::ln2_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T ln10_v = std::numbers::ln10_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T sqrt2_v = std::numbers::sqrt2_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T sqrt3_v = std::numbers::sqrt3_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T inv_sqrt3_v = std::numbers::inv_sqrt3_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T egamma_v = std::numbers::egamma_v<T>;
|
||||
template <typename T>
|
||||
inline constexpr T phi_v = std::numbers::phi_v<T>;
|
||||
|
||||
inline constexpr double e = e_v<double>;
|
||||
inline constexpr double log2e = log2e_v<double>;
|
||||
inline constexpr double log10e = log10e_v<double>;
|
||||
inline constexpr double pi = pi_v<double>;
|
||||
inline constexpr double inv_pi = inv_pi_v<double>;
|
||||
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
|
||||
inline constexpr double ln2 = ln2_v<double>;
|
||||
inline constexpr double ln10 = ln10_v<double>;
|
||||
inline constexpr double sqrt2 = sqrt2_v<double>;
|
||||
inline constexpr double sqrt3 = sqrt3_v<double>;
|
||||
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
|
||||
inline constexpr double egamma = egamma_v<double>;
|
||||
inline constexpr double phi = phi_v<double>;
|
||||
|
||||
#else
|
||||
|
||||
#ifdef M_E
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T e_v = static_cast<T>(M_E);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T e_v = static_cast<T>(2.71828182845904523536);
|
||||
#endif
|
||||
|
||||
#ifdef M_LOG2E
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T log2e_v = static_cast<T>(M_LOG2E);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T log2e_v = static_cast<T>(1.44269504088896340736);
|
||||
#endif
|
||||
|
||||
#ifdef M_LOG10E
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T log10e_v = static_cast<T>(M_LOG10E);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T log10e_v = static_cast<T>(0.434294481903251827651);
|
||||
#endif
|
||||
|
||||
#ifdef M_PI
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T pi_v = static_cast<T>(M_PI);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T pi_v = static_cast<T>(3.14159265358979323846);
|
||||
#endif
|
||||
|
||||
#ifdef M_1_PI
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T inv_pi_v = static_cast<T>(M_1_PI);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T inv_pi_v = static_cast<T>(0.318309886183790671538);
|
||||
#endif
|
||||
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T inv_sqrtpi_v = static_cast<T>(0.564189583547756286948079451560772586);
|
||||
|
||||
#ifdef M_LN2
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T ln2_v = static_cast<T>(M_LN2);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T ln2_v = static_cast<T>(0.693147180559945309417);
|
||||
#endif
|
||||
|
||||
#ifdef M_LN10
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T ln10_v = static_cast<T>(M_LN10);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T ln10_v = static_cast<T>(2.30258509299404568402);
|
||||
#endif
|
||||
|
||||
#ifdef M_SQRT2
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T sqrt2_v = static_cast<T>(M_SQRT2);
|
||||
#else
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T sqrt2_v = static_cast<T>(1.41421356237309504880);
|
||||
#endif
|
||||
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T sqrt3_v = static_cast<T>(1.732050807568877293527446341505872367);
|
||||
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T inv_sqrt3_v = static_cast<T>(0.577350269189625764509148780501957456);
|
||||
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T egamma_v = static_cast<T>(0.577215664901532860606512090082402431);
|
||||
|
||||
template <typename T, typename std::enable_if<std::is_floating_point<T>::value, bool>::type = true>
|
||||
inline constexpr T phi_v = static_cast<T>(1.618033988749894848204586834365638118);
|
||||
|
||||
inline constexpr double e = e_v<double>;
|
||||
inline constexpr double log2e = log2e_v<double>;
|
||||
inline constexpr double log10e = log10e_v<double>;
|
||||
inline constexpr double pi = pi_v<double>;
|
||||
inline constexpr double inv_pi = inv_pi_v<double>;
|
||||
inline constexpr double inv_sqrtpi = inv_sqrtpi_v<double>;
|
||||
inline constexpr double ln2 = ln2_v<double>;
|
||||
inline constexpr double ln10 = ln10_v<double>;
|
||||
inline constexpr double sqrt2 = sqrt2_v<double>;
|
||||
inline constexpr double sqrt3 = sqrt3_v<double>;
|
||||
inline constexpr double inv_sqrt3 = inv_sqrt3_v<double>;
|
||||
inline constexpr double egamma = egamma_v<double>;
|
||||
inline constexpr double phi = phi_v<double>;
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace numbers
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_NUMBRES_HPP
|
||||
@@ -0,0 +1,104 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_NUMERIC_HPP
|
||||
#define MPT_BASE_NUMERIC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/saturate_cast.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
template <typename Tmod, Tmod m>
|
||||
struct ModIfNotZeroImpl {
|
||||
template <typename Tval>
|
||||
constexpr Tval mod(Tval x) {
|
||||
static_assert(std::numeric_limits<Tmod>::is_integer);
|
||||
static_assert(!std::numeric_limits<Tmod>::is_signed);
|
||||
static_assert(std::numeric_limits<Tval>::is_integer);
|
||||
static_assert(!std::numeric_limits<Tval>::is_signed);
|
||||
return static_cast<Tval>(x % m);
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct ModIfNotZeroImpl<uint8, 0> {
|
||||
template <typename Tval>
|
||||
constexpr Tval mod(Tval x) {
|
||||
return x;
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct ModIfNotZeroImpl<uint16, 0> {
|
||||
template <typename Tval>
|
||||
constexpr Tval mod(Tval x) {
|
||||
return x;
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct ModIfNotZeroImpl<uint32, 0> {
|
||||
template <typename Tval>
|
||||
constexpr Tval mod(Tval x) {
|
||||
return x;
|
||||
}
|
||||
};
|
||||
template <>
|
||||
struct ModIfNotZeroImpl<uint64, 0> {
|
||||
template <typename Tval>
|
||||
constexpr Tval mod(Tval x) {
|
||||
return x;
|
||||
}
|
||||
};
|
||||
|
||||
// Returns x % m if m != 0, x otherwise.
|
||||
// i.e. "return (m == 0) ? x : (x % m);", but without causing a warning with stupid older compilers
|
||||
template <typename Tmod, Tmod m, typename Tval>
|
||||
constexpr Tval modulo_if_not_zero(Tval x) {
|
||||
return ModIfNotZeroImpl<Tmod, m>().mod(x);
|
||||
}
|
||||
|
||||
// rounds x up to multiples of target
|
||||
template <typename T>
|
||||
constexpr T align_up(T x, T target) {
|
||||
return ((x + (target - 1)) / target) * target;
|
||||
}
|
||||
|
||||
// rounds x down to multiples of target
|
||||
template <typename T>
|
||||
constexpr T align_down(T x, T target) {
|
||||
return (x / target) * target;
|
||||
}
|
||||
|
||||
// rounds x up to multiples of target or saturation of T
|
||||
template <typename T>
|
||||
constexpr T saturate_align_up(T x, T target) {
|
||||
if (x > (std::numeric_limits<T>::max() - (target - 1))) {
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
return ((x + (target - 1)) / target) * target;
|
||||
}
|
||||
|
||||
// Returns sign of a number (-1 for negative numbers, 1 for positive numbers, 0 for 0)
|
||||
template <class T>
|
||||
constexpr int signum(T value) {
|
||||
return (value > T(0)) - (value < T(0));
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_ALGORITHM_HPP
|
||||
@@ -0,0 +1,76 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_POINTER_HPP
|
||||
#define MPT_BASE_POINTER_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
inline constexpr int arch_bits = sizeof(void *) * 8;
|
||||
inline constexpr std::size_t pointer_size = sizeof(void *);
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
struct pointer_cast_helper {
|
||||
static constexpr Tdst cast(const Tsrc & src) noexcept {
|
||||
return src;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tdst, typename Tptr>
|
||||
struct pointer_cast_helper<Tdst, const Tptr *> {
|
||||
static constexpr Tdst cast(const Tptr * const & src) noexcept {
|
||||
return reinterpret_cast<const Tdst>(src);
|
||||
}
|
||||
};
|
||||
template <typename Tdst, typename Tptr>
|
||||
struct pointer_cast_helper<Tdst, Tptr *> {
|
||||
static constexpr Tdst cast(const Tptr * const & src) noexcept {
|
||||
return reinterpret_cast<const Tdst>(src);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
constexpr Tdst pointer_cast(const Tsrc & src) noexcept {
|
||||
return pointer_cast_helper<Tdst, Tsrc>::cast(src);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
class void_ptr {
|
||||
private:
|
||||
T * m_ptr = nullptr;
|
||||
public:
|
||||
MPT_FORCEINLINE explicit void_ptr(void * ptr)
|
||||
: m_ptr(reinterpret_cast<T *>(ptr)) {
|
||||
return;
|
||||
}
|
||||
MPT_FORCEINLINE T & operator*() {
|
||||
return *m_ptr;
|
||||
}
|
||||
MPT_FORCEINLINE T * operator->() {
|
||||
return m_ptr;
|
||||
}
|
||||
MPT_FORCEINLINE operator void *() {
|
||||
return m_ptr;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_POINTER_HPP
|
||||
@@ -0,0 +1,19 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_PREPROCESSOR_HPP
|
||||
#define MPT_BASE_PREPROCESSOR_HPP
|
||||
|
||||
|
||||
|
||||
#define MPT_PP_DEFER(m, ...) m(__VA_ARGS__)
|
||||
|
||||
#define MPT_PP_STRINGIFY(x) #x
|
||||
|
||||
#define MPT_PP_JOIN_HELPER(a, b) a##b
|
||||
#define MPT_PP_JOIN(a, b) MPT_PP_JOIN_HELPER(a, b)
|
||||
|
||||
#define MPT_PP_UNIQUE_IDENTIFIER(prefix) MPT_PP_JOIN(prefix, __LINE__)
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_PREPROCESSOR_HPP
|
||||
@@ -0,0 +1,72 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SATURATE_CAST_HPP
|
||||
#define MPT_BASE_SATURATE_CAST_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#if MPT_CXX_AT_LEAST(26)
|
||||
#include <numeric>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(26)
|
||||
|
||||
using std::saturate_cast;
|
||||
|
||||
#else
|
||||
|
||||
// Saturate the value of src to the domain of Tdst
|
||||
template <typename Tdst, typename Tsrc>
|
||||
constexpr Tdst saturate_cast(Tsrc src) noexcept {
|
||||
// This code tries not only to obviously avoid overflows but also to avoid signed/unsigned comparison warnings and type truncation warnings (which in fact would be safe here) by explicit casting.
|
||||
static_assert(std::numeric_limits<Tdst>::is_integer);
|
||||
static_assert(std::numeric_limits<Tsrc>::is_integer);
|
||||
if constexpr (std::numeric_limits<Tdst>::is_signed && std::numeric_limits<Tsrc>::is_signed) {
|
||||
if constexpr (sizeof(Tdst) >= sizeof(Tsrc)) {
|
||||
return static_cast<Tdst>(src);
|
||||
} else {
|
||||
return static_cast<Tdst>(std::max(static_cast<Tsrc>(std::numeric_limits<Tdst>::min()), std::min(src, static_cast<Tsrc>(std::numeric_limits<Tdst>::max()))));
|
||||
}
|
||||
} else if constexpr (!std::numeric_limits<Tdst>::is_signed && !std::numeric_limits<Tsrc>::is_signed) {
|
||||
if constexpr (sizeof(Tdst) >= sizeof(Tsrc)) {
|
||||
return static_cast<Tdst>(src);
|
||||
} else {
|
||||
return static_cast<Tdst>(std::min(src, static_cast<Tsrc>(std::numeric_limits<Tdst>::max())));
|
||||
}
|
||||
} else if constexpr (std::numeric_limits<Tdst>::is_signed && !std::numeric_limits<Tsrc>::is_signed) {
|
||||
if constexpr (sizeof(Tdst) > sizeof(Tsrc)) {
|
||||
return static_cast<Tdst>(src);
|
||||
} else if constexpr (sizeof(Tdst) == sizeof(Tsrc)) {
|
||||
return static_cast<Tdst>(std::min(src, static_cast<Tsrc>(std::numeric_limits<Tdst>::max())));
|
||||
} else {
|
||||
return static_cast<Tdst>(std::min(src, static_cast<Tsrc>(std::numeric_limits<Tdst>::max())));
|
||||
}
|
||||
} else { // Tdst unsigned, Tsrc signed
|
||||
if constexpr (sizeof(Tdst) >= sizeof(Tsrc)) {
|
||||
return static_cast<Tdst>(std::max(static_cast<Tsrc>(0), src));
|
||||
} else {
|
||||
return static_cast<Tdst>(std::max(static_cast<Tsrc>(0), std::min(src, static_cast<Tsrc>(std::numeric_limits<Tdst>::max()))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SATURATE_CAST_HPP
|
||||
@@ -0,0 +1,69 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SATURATE_ROUND_HPP
|
||||
#define MPT_BASE_SATURATE_ROUND_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include "mpt/base/math.hpp"
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
constexpr Tdst saturate_trunc(Tsrc src) {
|
||||
static_assert(std::is_floating_point<Tsrc>::value);
|
||||
if (src >= static_cast<Tsrc>(std::numeric_limits<Tdst>::max())) {
|
||||
return std::numeric_limits<Tdst>::max();
|
||||
}
|
||||
if (src <= static_cast<Tsrc>(std::numeric_limits<Tdst>::min())) {
|
||||
return std::numeric_limits<Tdst>::min();
|
||||
}
|
||||
return static_cast<Tdst>(src);
|
||||
}
|
||||
|
||||
|
||||
// Rounds given double value to nearest integer value of type T.
|
||||
// Out-of-range values are saturated to the specified integer type's limits.
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
inline Tdst saturate_round(Tsrc val) {
|
||||
static_assert(std::is_floating_point<Tsrc>::value);
|
||||
static_assert(std::numeric_limits<Tdst>::is_integer);
|
||||
return mpt::saturate_trunc<Tdst>(mpt::round(val));
|
||||
}
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
inline Tdst saturate_ceil(Tsrc val) {
|
||||
static_assert(std::is_floating_point<Tsrc>::value);
|
||||
static_assert(std::numeric_limits<Tdst>::is_integer);
|
||||
return mpt::saturate_trunc<Tdst>(std::ceil(val));
|
||||
}
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
inline Tdst saturate_floor(Tsrc val) {
|
||||
static_assert(std::is_floating_point<Tsrc>::value);
|
||||
static_assert(std::numeric_limits<Tdst>::is_integer);
|
||||
return mpt::saturate_trunc<Tdst>(std::floor(val));
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SATURATE_ROUND_HPP
|
||||
@@ -0,0 +1,212 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SECURE_HPP
|
||||
#define MPT_BASE_SECURE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
// C23 memset_explicit
|
||||
inline MPT_NOINLINE void * memset_explicit(void * const dst, int const value, std::size_t const len) noexcept {
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
volatile unsigned char * volatile p = static_cast<volatile unsigned char *>(dst);
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
for (volatile std::size_t i = 0; i < len; ++i) {
|
||||
p[i] = static_cast<unsigned char>(value);
|
||||
}
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace secure {
|
||||
|
||||
|
||||
|
||||
inline MPT_NOINLINE void memzero(std::byte * const dst, std::size_t const len) noexcept {
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
volatile std::byte * volatile p = static_cast<volatile std::byte *>(dst);
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
for (volatile std::size_t i = 0; i < len; ++i) {
|
||||
p[i] = std::byte{0};
|
||||
}
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
inline MPT_NOINLINE void memzero(void * const dst, std::size_t const len) noexcept {
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
volatile std::byte * volatile p = static_cast<volatile std::byte *>(dst);
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
for (volatile std::size_t i = 0; i < len; ++i) {
|
||||
p[i] = std::byte{0};
|
||||
}
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
inline MPT_NOINLINE void memzero(char * const dst, std::size_t const len) noexcept {
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
volatile std::byte * volatile p = reinterpret_cast<volatile std::byte *>(dst);
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
for (volatile std::size_t i = 0; i < len; ++i) {
|
||||
p[i] = std::byte{0};
|
||||
}
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
inline MPT_NOINLINE void memzero(uint8 * const dst, std::size_t const len) noexcept {
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
volatile std::byte * volatile p = reinterpret_cast<volatile std::byte *>(dst);
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
for (volatile std::size_t i = 0; i < len; ++i) {
|
||||
p[i] = std::byte{0};
|
||||
}
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
inline MPT_NOINLINE void clear(T & val) {
|
||||
std::atomic_signal_fence(std::memory_order_seq_cst);
|
||||
volatile T * volatile v = &val;
|
||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
||||
*v = T{};
|
||||
std::atomic_signal_fence(std::memory_order_seq_cst);
|
||||
}
|
||||
|
||||
|
||||
|
||||
class byte {
|
||||
private:
|
||||
std::byte value;
|
||||
|
||||
public:
|
||||
byte() noexcept
|
||||
: value(std::byte{0}) {
|
||||
return;
|
||||
}
|
||||
explicit byte(std::byte value) noexcept
|
||||
: value(value) {
|
||||
return;
|
||||
}
|
||||
byte(const byte & other) noexcept
|
||||
: value(other.value) {
|
||||
return;
|
||||
}
|
||||
byte(byte && other) noexcept
|
||||
: value(std::move(other.value)) {
|
||||
mpt::secure::clear(other.value);
|
||||
}
|
||||
byte & operator=(const byte & other) noexcept {
|
||||
if (&other == this) {
|
||||
return *this;
|
||||
}
|
||||
value = other.value;
|
||||
return *this;
|
||||
}
|
||||
byte & operator==(byte && other) noexcept {
|
||||
if (&other == this) {
|
||||
return *this;
|
||||
}
|
||||
value = std::move(other.value);
|
||||
mpt::secure::clear(other.value);
|
||||
return *this;
|
||||
}
|
||||
explicit operator std::byte() const noexcept {
|
||||
return value;
|
||||
}
|
||||
~byte() {
|
||||
mpt::secure::clear(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
class buffer {
|
||||
private:
|
||||
std::vector<std::byte> m_data;
|
||||
|
||||
public:
|
||||
buffer()
|
||||
: m_data(0) {
|
||||
return;
|
||||
}
|
||||
explicit buffer(const std::vector<std::byte> & data)
|
||||
: m_data(data) {
|
||||
return;
|
||||
}
|
||||
explicit buffer(const std::byte * beg, const std::byte * end)
|
||||
: m_data(beg, end) {
|
||||
return;
|
||||
}
|
||||
buffer(const buffer & other)
|
||||
: m_data(other.m_data) {
|
||||
return;
|
||||
}
|
||||
buffer(buffer && other) noexcept
|
||||
: m_data(std::move(other.m_data)) {
|
||||
mpt::secure::memzero(other.m_data.data(), other.m_data.size());
|
||||
}
|
||||
buffer & operator=(const buffer & other) {
|
||||
if (&other == this) {
|
||||
return *this;
|
||||
}
|
||||
m_data = other.m_data;
|
||||
return *this;
|
||||
}
|
||||
buffer & operator=(buffer && other) noexcept {
|
||||
if (&other == this) {
|
||||
return *this;
|
||||
}
|
||||
m_data = std::move(other.m_data);
|
||||
mpt::secure::memzero(other.m_data.data(), other.m_data.size());
|
||||
return *this;
|
||||
}
|
||||
~buffer() {
|
||||
mpt::secure::memzero(m_data.data(), m_data.size());
|
||||
m_data.resize(0);
|
||||
m_data.shrink_to_fit();
|
||||
}
|
||||
explicit operator std::vector<std::byte>() const {
|
||||
return m_data;
|
||||
}
|
||||
const std::byte * data() const {
|
||||
return m_data.data();
|
||||
}
|
||||
std::byte * data() {
|
||||
return m_data.data();
|
||||
}
|
||||
std::size_t size() const {
|
||||
return m_data.size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace secure
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SECURE_HPP
|
||||
@@ -0,0 +1,97 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SEMANTIC_VERSION_HPP
|
||||
#define MPT_BASE_SEMANTIC_VERSION_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/version.hpp"
|
||||
|
||||
|
||||
|
||||
#include <tuple>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
struct semantic_version {
|
||||
unsigned long long major = 0;
|
||||
unsigned long long minor = 0;
|
||||
unsigned long long patch = 0;
|
||||
constexpr std::tuple<unsigned long long, unsigned long long, unsigned long long> as_tuple() const noexcept {
|
||||
return std::make_tuple(major, minor, patch);
|
||||
}
|
||||
};
|
||||
|
||||
constexpr bool operator==(const semantic_version a, const semantic_version b) noexcept {
|
||||
return a.as_tuple() == b.as_tuple();
|
||||
}
|
||||
constexpr bool operator!=(const semantic_version a, const semantic_version b) noexcept {
|
||||
return a.as_tuple() != b.as_tuple();
|
||||
}
|
||||
constexpr bool operator<(const semantic_version a, const semantic_version b) noexcept {
|
||||
return a.as_tuple() < b.as_tuple();
|
||||
}
|
||||
constexpr bool operator>(const semantic_version a, const semantic_version b) noexcept {
|
||||
return a.as_tuple() > b.as_tuple();
|
||||
}
|
||||
constexpr bool operator<=(const semantic_version a, const semantic_version b) noexcept {
|
||||
return a.as_tuple() <= b.as_tuple();
|
||||
}
|
||||
constexpr bool operator>=(const semantic_version a, const semantic_version b) noexcept {
|
||||
return a.as_tuple() >= b.as_tuple();
|
||||
}
|
||||
|
||||
struct version_info {
|
||||
semantic_version semver{};
|
||||
unsigned long long build = 0;
|
||||
constexpr std::tuple<std::tuple<unsigned long long, unsigned long long, unsigned long long>, unsigned long long> as_tuple() const noexcept {
|
||||
return std::make_tuple(semver.as_tuple(), build);
|
||||
}
|
||||
template <typename Tostream>
|
||||
friend Tostream & operator<<(Tostream & os, const version_info vi) {
|
||||
if (vi.build > 0) {
|
||||
os << vi.semver.major << "." << vi.semver.minor << "." << vi.semver.patch << "+build." << vi.build;
|
||||
} else {
|
||||
os << vi.semver.major << "." << vi.semver.minor << "." << vi.semver.patch;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
constexpr bool operator==(const version_info a, const version_info b) noexcept {
|
||||
return a.as_tuple() == b.as_tuple();
|
||||
}
|
||||
constexpr bool operator!=(const version_info a, const version_info b) noexcept {
|
||||
return a.as_tuple() != b.as_tuple();
|
||||
}
|
||||
constexpr bool operator<(const version_info a, const version_info b) noexcept {
|
||||
return a.as_tuple() < b.as_tuple();
|
||||
}
|
||||
constexpr bool operator>(const version_info a, const version_info b) noexcept {
|
||||
return a.as_tuple() > b.as_tuple();
|
||||
}
|
||||
constexpr bool operator<=(const version_info a, const version_info b) noexcept {
|
||||
return a.as_tuple() <= b.as_tuple();
|
||||
}
|
||||
constexpr bool operator>=(const version_info a, const version_info b) noexcept {
|
||||
return a.as_tuple() >= b.as_tuple();
|
||||
}
|
||||
|
||||
constexpr inline version_info Version = {
|
||||
{MPT_VERSION_MAJOR, MPT_VERSION_MINOR, MPT_VERSION_PATCH},
|
||||
MPT_VERSION_BUILD
|
||||
};
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SEMANTIC_VERSION_HPP
|
||||
@@ -0,0 +1,72 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SIZE_HPP
|
||||
#define MPT_BASE_SIZE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include "mpt/base/constexpr_throw.hpp"
|
||||
#endif // C++20
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include "mpt/base/utility.hpp"
|
||||
#endif // C++20
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include <stdexcept>
|
||||
#endif // C++20
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
using usize = std::size_t;
|
||||
using ssize = std::ptrdiff_t;
|
||||
|
||||
|
||||
namespace size_literals {
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
consteval usize operator""_uz(unsigned long long val) noexcept {
|
||||
if (!mpt::in_range<usize>(val)) {
|
||||
mpt::constexpr_throw(std::domain_error(""));
|
||||
}
|
||||
return static_cast<usize>(val);
|
||||
}
|
||||
#else // !C++20
|
||||
constexpr usize operator""_uz(unsigned long long val) noexcept {
|
||||
return static_cast<usize>(val);
|
||||
}
|
||||
#endif // C++20
|
||||
} // namespace size_literals
|
||||
|
||||
namespace size_literals {
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
consteval ssize operator""_z(unsigned long long val) noexcept {
|
||||
if (!mpt::in_range<ssize>(val)) {
|
||||
mpt::constexpr_throw(std::domain_error(""));
|
||||
}
|
||||
return static_cast<ssize>(val);
|
||||
}
|
||||
#else // !C++20
|
||||
constexpr ssize operator""_z(unsigned long long val) noexcept {
|
||||
return static_cast<ssize>(val);
|
||||
}
|
||||
#endif // C++20
|
||||
} // namespace size_literals
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SIZE_HPP
|
||||
@@ -0,0 +1,119 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SOURCE_LOCATION_HPP
|
||||
#define MPT_BASE_SOURCE_LOCATION_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && !MPT_MSVC_BEFORE(2022, 0) && !MPT_COMPILER_CLANG
|
||||
#include <source_location>
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && !MPT_MSVC_BEFORE(2022, 0) && !MPT_COMPILER_CLANG
|
||||
|
||||
using std::source_location;
|
||||
|
||||
#define MPT_SOURCE_LOCATION_CURRENT() std::source_location::current()
|
||||
|
||||
#else // !C++20
|
||||
|
||||
#if MPT_COMPILER_MSVC && MPT_MSVC_AT_LEAST(2019, 6)
|
||||
|
||||
#define MPT_SOURCE_LOCATION_FILE __builtin_FILE()
|
||||
#define MPT_SOURCE_LOCATION_FUNCTION __builtin_FUNCTION()
|
||||
#define MPT_SOURCE_LOCATION_LINE __builtin_LINE()
|
||||
#define MPT_SOURCE_LOCATION_COLUMN __builtin_COLUMN()
|
||||
|
||||
#elif MPT_COMPILER_GCC
|
||||
|
||||
#define MPT_SOURCE_LOCATION_FILE __builtin_FILE()
|
||||
#define MPT_SOURCE_LOCATION_FUNCTION __builtin_FUNCTION()
|
||||
#define MPT_SOURCE_LOCATION_LINE __builtin_LINE()
|
||||
#define MPT_SOURCE_LOCATION_COLUMN 0
|
||||
|
||||
#elif MPT_COMPILER_CLANG && ((!MPT_OS_MACOSX_OR_IOS && MPT_CLANG_AT_LEAST(9, 0, 0)) || (MPT_OS_MACOSX_OR_IOS && MPT_CLANG_AT_LEAST(12, 0, 0)))
|
||||
|
||||
// We do not know which Apple Clang version introduced __builtin_FILE().
|
||||
// It fails with 10.x (see <https://github.com/fink/fink-distributions/issues/1202>),
|
||||
// and IRC dicussion decided on 12.x as a somewhat safe choice.
|
||||
|
||||
#define MPT_SOURCE_LOCATION_FILE __builtin_FILE()
|
||||
#define MPT_SOURCE_LOCATION_FUNCTION __builtin_FUNCTION()
|
||||
#define MPT_SOURCE_LOCATION_LINE __builtin_LINE()
|
||||
#define MPT_SOURCE_LOCATION_COLUMN __builtin_COLUMN()
|
||||
|
||||
#else
|
||||
|
||||
#define MPT_SOURCE_LOCATION_FILE __FILE__
|
||||
#define MPT_SOURCE_LOCATION_FUNCTION ""
|
||||
#define MPT_SOURCE_LOCATION_LINE __LINE__
|
||||
#define MPT_SOURCE_LOCATION_COLUMN 0
|
||||
|
||||
#endif
|
||||
|
||||
// compatible with C++20 std::source_location
|
||||
struct source_location {
|
||||
private:
|
||||
const char * m_file_name;
|
||||
const char * m_function_name;
|
||||
uint32 m_line;
|
||||
uint32 m_column;
|
||||
|
||||
public:
|
||||
constexpr source_location() noexcept
|
||||
: m_file_name("")
|
||||
, m_function_name("")
|
||||
, m_line(0)
|
||||
, m_column(0) {
|
||||
}
|
||||
constexpr source_location(const char * file, const char * function, uint32 line, uint32 column) noexcept
|
||||
: m_file_name(file)
|
||||
, m_function_name(function)
|
||||
, m_line(line)
|
||||
, m_column(column) {
|
||||
}
|
||||
source_location(const source_location &) = default;
|
||||
source_location(source_location &&) = default;
|
||||
static constexpr source_location current(const char * file = MPT_SOURCE_LOCATION_FILE, const char * function = MPT_SOURCE_LOCATION_FUNCTION, uint32 line = MPT_SOURCE_LOCATION_LINE, uint32 column = MPT_SOURCE_LOCATION_COLUMN) noexcept {
|
||||
return source_location(file, function, line, column);
|
||||
}
|
||||
constexpr uint32 line() const noexcept {
|
||||
return m_line;
|
||||
}
|
||||
constexpr uint32 column() const noexcept {
|
||||
return m_column;
|
||||
}
|
||||
constexpr const char * file_name() const noexcept {
|
||||
return m_file_name;
|
||||
}
|
||||
constexpr const char * function_name() const noexcept {
|
||||
return m_function_name;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#if (MPT_COMPILER_MSVC && MPT_MSVC_AT_LEAST(2019, 6)) || MPT_COMPILER_GCC || (MPT_COMPILER_CLANG && MPT_CLANG_AT_LEAST(9, 0, 0))
|
||||
#define MPT_SOURCE_LOCATION_CURRENT() mpt::source_location::current()
|
||||
#else
|
||||
#define MPT_SOURCE_LOCATION_CURRENT() mpt::source_location::current(__FILE__, __func__, __LINE__, 0)
|
||||
#endif
|
||||
|
||||
#endif // C++20
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SOURCE_LOCATION_HPP
|
||||
@@ -0,0 +1,193 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_SPAN_HPP
|
||||
#define MPT_BASE_SPAN_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <array>
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
#include <span>
|
||||
#else // !C++20
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#endif // C++20
|
||||
|
||||
#if MPT_CXX_BEFORE(20)
|
||||
#include <cstddef>
|
||||
#endif // !C++20
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
|
||||
using std::dynamic_extent;
|
||||
using std::span;
|
||||
|
||||
#else // !C++20
|
||||
|
||||
// Simplified version of gsl::span.
|
||||
// Non-owning read-only or read-write view into a contiguous block of T
|
||||
// objects, i.e. equivalent to a (beg,end) or (data,size) tuple.
|
||||
// Can eventually be replaced without further modifications with a full C++20
|
||||
// std::span.
|
||||
|
||||
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
|
||||
|
||||
template <typename T>
|
||||
class span {
|
||||
|
||||
public:
|
||||
using element_type = T;
|
||||
using value_type = typename std::remove_cv<T>::type;
|
||||
using index_type = std::size_t;
|
||||
using pointer = T *;
|
||||
using const_pointer = const T *;
|
||||
using reference = T &;
|
||||
using const_reference = const T &;
|
||||
|
||||
using iterator = pointer;
|
||||
|
||||
using difference_type = typename std::iterator_traits<iterator>::difference_type;
|
||||
|
||||
private:
|
||||
T * m_data;
|
||||
std::size_t m_size;
|
||||
|
||||
public:
|
||||
span() noexcept
|
||||
: m_data(nullptr)
|
||||
, m_size(0) {
|
||||
}
|
||||
|
||||
span(pointer beg, pointer end)
|
||||
: m_data(beg)
|
||||
, m_size(end - beg) {
|
||||
}
|
||||
|
||||
span(pointer data, index_type size)
|
||||
: m_data(data)
|
||||
, m_size(size) {
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
span(element_type (&arr)[N])
|
||||
: m_data(arr)
|
||||
, m_size(N) {
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
span(std::array<value_type, N> & arr)
|
||||
: m_data(arr.data())
|
||||
, m_size(arr.size()) {
|
||||
}
|
||||
|
||||
template <std::size_t N>
|
||||
span(const std::array<value_type, N> & arr)
|
||||
: m_data(arr.data())
|
||||
, m_size(arr.size()) {
|
||||
}
|
||||
|
||||
span(const span & other) noexcept = default;
|
||||
|
||||
template <typename U>
|
||||
span(const span<U> & other)
|
||||
: m_data(other.data())
|
||||
, m_size(other.size()) {
|
||||
}
|
||||
|
||||
span & operator=(const span & other) noexcept = default;
|
||||
|
||||
iterator begin() const {
|
||||
return iterator(m_data);
|
||||
}
|
||||
|
||||
iterator end() const {
|
||||
return iterator(m_data + m_size);
|
||||
}
|
||||
|
||||
reference operator[](index_type index) {
|
||||
return m_data[index];
|
||||
}
|
||||
|
||||
const_reference operator[](index_type index) const {
|
||||
return m_data[index];
|
||||
}
|
||||
|
||||
pointer data() const noexcept {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
bool empty() const noexcept {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
index_type size() const noexcept {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
index_type length() const noexcept {
|
||||
return size();
|
||||
}
|
||||
|
||||
span subspan(std::size_t offset, std::size_t count = mpt::dynamic_extent) const {
|
||||
return span(data() + offset, (count == mpt::dynamic_extent) ? (size() - offset) : count);
|
||||
}
|
||||
|
||||
span first(std::size_t count) const {
|
||||
return span(data(), count);
|
||||
}
|
||||
|
||||
span last(std::size_t count) const {
|
||||
return span(data() + (size() - count), count);
|
||||
}
|
||||
|
||||
}; // class span
|
||||
|
||||
#endif // C++20
|
||||
|
||||
template <typename T>
|
||||
inline span<T> as_span(T * beg, T * end) {
|
||||
return span<T>(beg, end);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline span<T> as_span(T * data, std::size_t size) {
|
||||
return span<T>(data, size);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline span<T> as_span(T (&arr)[N]) {
|
||||
return span<T>(std::begin(arr), std::end(arr));
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline span<T> as_span(std::array<T, N> & cont) {
|
||||
return span<T>(cont);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
inline span<const T> as_span(const std::array<T, N> & cont) {
|
||||
return span<const T>(cont);
|
||||
}
|
||||
|
||||
template <typename Ca, typename Cb>
|
||||
bool span_elements_equal(const Ca & a, const Cb & b) {
|
||||
return a.size() == b.size() && (a.data() == b.data() || std::equal(a.begin(), a.end(), b.begin()));
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_SPAN_HPP
|
||||
@@ -0,0 +1,358 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_ARITHMETIC_SHIFT_HPP
|
||||
#define MPT_BASE_TESTS_ARITHMETIC_SHIFT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/arithmetic_shift.hpp"
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace arithmetic_shift {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/arithmetic_shift")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32768, 1), mpt::rshift_signed_portable<int16>(-32768, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32767, 1), mpt::rshift_signed_portable<int16>(-32767, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32766, 1), mpt::rshift_signed_portable<int16>(-32766, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-2, 1), mpt::rshift_signed_portable<int16>(-2, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-1, 1), mpt::rshift_signed_portable<int16>(-1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(0, 1), mpt::rshift_signed_portable<int16>(0, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(1, 1), mpt::rshift_signed_portable<int16>(1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(2, 1), mpt::rshift_signed_portable<int16>(2, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32766, 1), mpt::rshift_signed_portable<int16>(32766, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32767, 1), mpt::rshift_signed_portable<int16>(32767, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32768, 14), mpt::rshift_signed_portable<int16>(-32768, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32767, 14), mpt::rshift_signed_portable<int16>(-32767, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32766, 14), mpt::rshift_signed_portable<int16>(-32766, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-2, 14), mpt::rshift_signed_portable<int16>(-2, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-1, 14), mpt::rshift_signed_portable<int16>(-1, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(0, 14), mpt::rshift_signed_portable<int16>(0, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(1, 14), mpt::rshift_signed_portable<int16>(1, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(2, 14), mpt::rshift_signed_portable<int16>(2, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32766, 14), mpt::rshift_signed_portable<int16>(32766, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32767, 14), mpt::rshift_signed_portable<int16>(32767, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32768, 15), mpt::rshift_signed_portable<int16>(-32768, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32767, 15), mpt::rshift_signed_portable<int16>(-32767, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32766, 15), mpt::rshift_signed_portable<int16>(-32766, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-2, 15), mpt::rshift_signed_portable<int16>(-2, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-1, 15), mpt::rshift_signed_portable<int16>(-1, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(0, 15), mpt::rshift_signed_portable<int16>(0, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(1, 15), mpt::rshift_signed_portable<int16>(1, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(2, 15), mpt::rshift_signed_portable<int16>(2, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32766, 15), mpt::rshift_signed_portable<int16>(32766, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32767, 15), mpt::rshift_signed_portable<int16>(32767, 15));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32768, 1), mpt::lshift_signed_portable<int16>(-32768, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32767, 1), mpt::lshift_signed_portable<int16>(-32767, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32766, 1), mpt::lshift_signed_portable<int16>(-32766, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-2, 1), mpt::lshift_signed_portable<int16>(-2, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-1, 1), mpt::lshift_signed_portable<int16>(-1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(0, 1), mpt::lshift_signed_portable<int16>(0, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(1, 1), mpt::lshift_signed_portable<int16>(1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(2, 1), mpt::lshift_signed_portable<int16>(2, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32766, 1), mpt::lshift_signed_portable<int16>(32766, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32767, 1), mpt::lshift_signed_portable<int16>(32767, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32768, 14), mpt::lshift_signed_portable<int16>(-32768, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32767, 14), mpt::lshift_signed_portable<int16>(-32767, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32766, 14), mpt::lshift_signed_portable<int16>(-32766, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-2, 14), mpt::lshift_signed_portable<int16>(-2, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-1, 14), mpt::lshift_signed_portable<int16>(-1, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(0, 14), mpt::lshift_signed_portable<int16>(0, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(1, 14), mpt::lshift_signed_portable<int16>(1, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(2, 14), mpt::lshift_signed_portable<int16>(2, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32766, 14), mpt::lshift_signed_portable<int16>(32766, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32767, 14), mpt::lshift_signed_portable<int16>(32767, 14));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32768, 15), mpt::lshift_signed_portable<int16>(-32768, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32767, 15), mpt::lshift_signed_portable<int16>(-32767, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32766, 15), mpt::lshift_signed_portable<int16>(-32766, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-2, 15), mpt::lshift_signed_portable<int16>(-2, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-1, 15), mpt::lshift_signed_portable<int16>(-1, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(0, 15), mpt::lshift_signed_portable<int16>(0, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(1, 15), mpt::lshift_signed_portable<int16>(1, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(2, 15), mpt::lshift_signed_portable<int16>(2, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32766, 15), mpt::lshift_signed_portable<int16>(32766, 15));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32767, 15), mpt::lshift_signed_portable<int16>(32767, 15));
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) || MPT_COMPILER_SHIFT_SIGNED
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826>
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wshift-negative-value"
|
||||
#endif // MPT_COMPILER_GCC
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32768, 1), (-32768) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32767, 1), (-32767) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32766, 1), (-32766) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-2, 1), (-2) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-1, 1), (-1) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(0, 1), (0) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(1, 1), (1) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(2, 1), (2) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32766, 1), (32766) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32767, 1), (32767) >> 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32768, 14), (-32768) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32767, 14), (-32767) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32766, 14), (-32766) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-2, 14), (-2) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-1, 14), (-1) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(0, 14), (0) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(1, 14), (1) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(2, 14), (2) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32766, 14), (32766) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32767, 14), (32767) >> 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32768, 15), (-32768) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32767, 15), (-32767) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-32766, 15), (-32766) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-2, 15), (-2) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(-1, 15), (-1) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(0, 15), (0) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(1, 15), (1) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(2, 15), (2) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32766, 15), (32766) >> 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int16>(32767, 15), (32767) >> 15);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32768, 1), (-32768) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32767, 1), (-32767) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32766, 1), (-32766) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-2, 1), (-2) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-1, 1), (-1) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(0, 1), (0) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(1, 1), (1) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(2, 1), (2) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32766, 1), (32766) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32767, 1), (32767) << 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32768, 14), (-32768) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32767, 14), (-32767) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32766, 14), (-32766) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-2, 14), (-2) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-1, 14), (-1) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(0, 14), (0) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(1, 14), (1) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(2, 14), (2) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32766, 14), (32766) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32767, 14), (32767) << 14);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32768, 15), (-32768) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32767, 15), (-32767) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-32766, 15), (-32766) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-2, 15), (-2) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(-1, 15), (-1) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(0, 15), (0) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(1, 15), (1) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(2, 15), (2) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32766, 15), (32766) << 15);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int16>(32767, 15), (32767) << 15);
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // MPT_COMPILER_GCC
|
||||
|
||||
#endif
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0 - 0x80000000, 1), mpt::rshift_signed_portable<int32>(0 - 0x80000000, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7fffffff, 1), mpt::rshift_signed_portable<int32>(-0x7fffffff, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7ffffffe, 1), mpt::rshift_signed_portable<int32>(-0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-1, 1), mpt::rshift_signed_portable<int32>(-1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0, 1), mpt::rshift_signed_portable<int32>(0, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(1, 1), mpt::rshift_signed_portable<int32>(1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7ffffffe, 1), mpt::rshift_signed_portable<int32>(0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7fffffff, 1), mpt::rshift_signed_portable<int32>(0x7fffffff, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0 - 0x80000000, 31), mpt::rshift_signed_portable<int32>(0 - 0x80000000, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7fffffff, 31), mpt::rshift_signed_portable<int32>(-0x7fffffff, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7ffffffe, 31), mpt::rshift_signed_portable<int32>(-0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-1, 31), mpt::rshift_signed_portable<int32>(-1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0, 31), mpt::rshift_signed_portable<int32>(0, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(1, 31), mpt::rshift_signed_portable<int32>(1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7ffffffe, 31), mpt::rshift_signed_portable<int32>(0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7fffffff, 31), mpt::rshift_signed_portable<int32>(0x7fffffff, 31));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0 - 0x80000000, 1), mpt::lshift_signed_portable<int32>(0 - 0x80000000, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7fffffff, 1), mpt::lshift_signed_portable<int32>(-0x7fffffff, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7ffffffe, 1), mpt::lshift_signed_portable<int32>(-0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-1, 1), mpt::lshift_signed_portable<int32>(-1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0, 1), mpt::lshift_signed_portable<int32>(0, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(1, 1), mpt::lshift_signed_portable<int32>(1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7ffffffe, 1), mpt::lshift_signed_portable<int32>(0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7fffffff, 1), mpt::lshift_signed_portable<int32>(0x7fffffff, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0 - 0x80000000, 31), mpt::lshift_signed_portable<int32>(0 - 0x80000000, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7fffffff, 31), mpt::lshift_signed_portable<int32>(-0x7fffffff, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7ffffffe, 31), mpt::lshift_signed_portable<int32>(-0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-1, 31), mpt::lshift_signed_portable<int32>(-1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0, 31), mpt::lshift_signed_portable<int32>(0, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(1, 31), mpt::lshift_signed_portable<int32>(1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7ffffffe, 31), mpt::lshift_signed_portable<int32>(0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7fffffff, 31), mpt::lshift_signed_portable<int32>(0x7fffffff, 31));
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) || MPT_COMPILER_SHIFT_SIGNED
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826>
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wshift-negative-value"
|
||||
#endif // MPT_COMPILER_GCC
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0 - 0x80000000, 1), mpt::rshift_signed_cxx20<int32>(0 - 0x80000000, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7fffffff, 1), mpt::rshift_signed_cxx20<int32>(-0x7fffffff, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7ffffffe, 1), mpt::rshift_signed_cxx20<int32>(-0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-1, 1), mpt::rshift_signed_cxx20<int32>(-1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0, 1), mpt::rshift_signed_cxx20<int32>(0, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(1, 1), mpt::rshift_signed_cxx20<int32>(1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7ffffffe, 1), mpt::rshift_signed_cxx20<int32>(0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7fffffff, 1), mpt::rshift_signed_cxx20<int32>(0x7fffffff, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0 - 0x80000000, 31), mpt::rshift_signed_cxx20<int32>(0 - 0x80000000, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7fffffff, 31), mpt::rshift_signed_cxx20<int32>(-0x7fffffff, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-0x7ffffffe, 31), mpt::rshift_signed_cxx20<int32>(-0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(-1, 31), mpt::rshift_signed_cxx20<int32>(-1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0, 31), mpt::rshift_signed_cxx20<int32>(0, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(1, 31), mpt::rshift_signed_cxx20<int32>(1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7ffffffe, 31), mpt::rshift_signed_cxx20<int32>(0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int32>(0x7fffffff, 31), mpt::rshift_signed_cxx20<int32>(0x7fffffff, 31));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0 - 0x80000000, 1), mpt::lshift_signed_cxx20<int32>(0 - 0x80000000, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7fffffff, 1), mpt::lshift_signed_cxx20<int32>(-0x7fffffff, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7ffffffe, 1), mpt::lshift_signed_cxx20<int32>(-0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-1, 1), mpt::lshift_signed_cxx20<int32>(-1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0, 1), mpt::lshift_signed_cxx20<int32>(0, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(1, 1), mpt::lshift_signed_cxx20<int32>(1, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7ffffffe, 1), mpt::lshift_signed_cxx20<int32>(0x7ffffffe, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7fffffff, 1), mpt::lshift_signed_cxx20<int32>(0x7fffffff, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0 - 0x80000000, 31), mpt::lshift_signed_cxx20<int32>(0 - 0x80000000, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7fffffff, 31), mpt::lshift_signed_cxx20<int32>(-0x7fffffff, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-0x7ffffffe, 31), mpt::lshift_signed_cxx20<int32>(-0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(-1, 31), mpt::lshift_signed_cxx20<int32>(-1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0, 31), mpt::lshift_signed_cxx20<int32>(0, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(1, 31), mpt::lshift_signed_cxx20<int32>(1, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7ffffffe, 31), mpt::lshift_signed_cxx20<int32>(0x7ffffffe, 31));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int32>(0x7fffffff, 31), mpt::lshift_signed_cxx20<int32>(0x7fffffff, 31));
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // MPT_COMPILER_GCC
|
||||
|
||||
#endif
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ull - 0x8000000000000000ull, 1), mpt::rshift_signed_portable<int64>(0ull - 0x8000000000000000ull, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7fffffffffffffffll, 1), mpt::rshift_signed_portable<int64>(-0x7fffffffffffffffll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7ffffffffffffffell, 1), mpt::rshift_signed_portable<int64>(-0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-1ll, 1), mpt::rshift_signed_portable<int64>(-1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ll, 1), mpt::rshift_signed_portable<int64>(0ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(1ll, 1), mpt::rshift_signed_portable<int64>(1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7ffffffffffffffell, 1), mpt::rshift_signed_portable<int64>(0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7fffffffffffffffll, 1), mpt::rshift_signed_portable<int64>(0x7fffffffffffffffll, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ull - 0x8000000000000000ull, 63), mpt::rshift_signed_portable<int64>(0ull - 0x8000000000000000ull, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7fffffffffffffffll, 63), mpt::rshift_signed_portable<int64>(-0x7fffffffffffffffll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7ffffffffffffffell, 63), mpt::rshift_signed_portable<int64>(-0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-1ll, 63), mpt::rshift_signed_portable<int64>(-1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ll, 63), mpt::rshift_signed_portable<int64>(0ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(1ll, 63), mpt::rshift_signed_portable<int64>(1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7ffffffffffffffell, 63), mpt::rshift_signed_portable<int64>(0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7fffffffffffffffll, 63), mpt::rshift_signed_portable<int64>(0x7fffffffffffffffll, 63));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ull - 0x8000000000000000ull, 1), mpt::lshift_signed_portable<int64>(0ull - 0x8000000000000000ull, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7fffffffffffffffll, 1), mpt::lshift_signed_portable<int64>(-0x7fffffffffffffffll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7ffffffffffffffell, 1), mpt::lshift_signed_portable<int64>(-0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-1ll, 1), mpt::lshift_signed_portable<int64>(-1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ll, 1), mpt::lshift_signed_portable<int64>(0ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(1ll, 1), mpt::lshift_signed_portable<int64>(1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7ffffffffffffffell, 1), mpt::lshift_signed_portable<int64>(0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7fffffffffffffffll, 1), mpt::lshift_signed_portable<int64>(0x7fffffffffffffffll, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ull - 0x8000000000000000ull, 63), mpt::lshift_signed_portable<int64>(0ull - 0x8000000000000000ull, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7fffffffffffffffll, 63), mpt::lshift_signed_portable<int64>(-0x7fffffffffffffffll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7ffffffffffffffell, 63), mpt::lshift_signed_portable<int64>(-0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-1ll, 63), mpt::lshift_signed_portable<int64>(-1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ll, 63), mpt::lshift_signed_portable<int64>(0ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(1ll, 63), mpt::lshift_signed_portable<int64>(1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7ffffffffffffffell, 63), mpt::lshift_signed_portable<int64>(0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7fffffffffffffffll, 63), mpt::lshift_signed_portable<int64>(0x7fffffffffffffffll, 63));
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) || MPT_COMPILER_SHIFT_SIGNED
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
// <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103826>
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wshift-negative-value"
|
||||
#endif // MPT_COMPILER_GCC
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ull - 0x8000000000000000ull, 1), mpt::rshift_signed_cxx20<int64>(0ull - 0x8000000000000000ull, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7fffffffffffffffll, 1), mpt::rshift_signed_cxx20<int64>(-0x7fffffffffffffffll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7ffffffffffffffell, 1), mpt::rshift_signed_cxx20<int64>(-0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-1ll, 1), mpt::rshift_signed_cxx20<int64>(-1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ll, 1), mpt::rshift_signed_cxx20<int64>(0ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(1ll, 1), mpt::rshift_signed_cxx20<int64>(1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7ffffffffffffffell, 1), mpt::rshift_signed_cxx20<int64>(0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7fffffffffffffffll, 1), mpt::rshift_signed_cxx20<int64>(0x7fffffffffffffffll, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ull - 0x8000000000000000ull, 63), mpt::rshift_signed_cxx20<int64>(0ull - 0x8000000000000000ull, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7fffffffffffffffll, 63), mpt::rshift_signed_cxx20<int64>(-0x7fffffffffffffffll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-0x7ffffffffffffffell, 63), mpt::rshift_signed_cxx20<int64>(-0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(-1ll, 63), mpt::rshift_signed_cxx20<int64>(-1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0ll, 63), mpt::rshift_signed_cxx20<int64>(0ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(1ll, 63), mpt::rshift_signed_cxx20<int64>(1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7ffffffffffffffell, 63), mpt::rshift_signed_cxx20<int64>(0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::rshift_signed<int64>(0x7fffffffffffffffll, 63), mpt::rshift_signed_cxx20<int64>(0x7fffffffffffffffll, 63));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ull - 0x8000000000000000ull, 1), mpt::lshift_signed_cxx20<int64>(0ull - 0x8000000000000000ull, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7fffffffffffffffll, 1), mpt::lshift_signed_cxx20<int64>(-0x7fffffffffffffffll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7ffffffffffffffell, 1), mpt::lshift_signed_cxx20<int64>(-0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-1ll, 1), mpt::lshift_signed_cxx20<int64>(-1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ll, 1), mpt::lshift_signed_cxx20<int64>(0ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(1ll, 1), mpt::lshift_signed_cxx20<int64>(1ll, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7ffffffffffffffell, 1), mpt::lshift_signed_cxx20<int64>(0x7ffffffffffffffell, 1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7fffffffffffffffll, 1), mpt::lshift_signed_cxx20<int64>(0x7fffffffffffffffll, 1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ull - 0x8000000000000000ull, 63), mpt::lshift_signed_cxx20<int64>(0ull - 0x8000000000000000ull, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7fffffffffffffffll, 63), mpt::lshift_signed_cxx20<int64>(-0x7fffffffffffffffll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-0x7ffffffffffffffell, 63), mpt::lshift_signed_cxx20<int64>(-0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(-1ll, 63), mpt::lshift_signed_cxx20<int64>(-1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0ll, 63), mpt::lshift_signed_cxx20<int64>(0ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(1ll, 63), mpt::lshift_signed_cxx20<int64>(1ll, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7ffffffffffffffell, 63), mpt::lshift_signed_cxx20<int64>(0x7ffffffffffffffell, 63));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lshift_signed<int64>(0x7fffffffffffffffll, 63), mpt::lshift_signed_cxx20<int64>(0x7fffffffffffffffll, 63));
|
||||
|
||||
#if MPT_COMPILER_GCC
|
||||
#pragma GCC diagnostic pop
|
||||
#endif // MPT_COMPILER_GCC
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace arithmetic_shift
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_ARITHMETIC_SHIFT_HPP
|
||||
@@ -0,0 +1,239 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_BASE_BIT_HPP
|
||||
#define MPT_BASE_TESTS_BASE_BIT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace bit {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/bit")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
#if MPT_CXX_BEFORE(20)
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::get_endian(), mpt::endian_probe());
|
||||
#endif
|
||||
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_little()) {
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::get_endian(), mpt::endian::little);
|
||||
MPT_MAYBE_CONSTANT_IF ((mpt::endian::native == mpt::endian::little) || (mpt::endian::native == mpt::endian::big)) {
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::endian::native, mpt::endian::little);
|
||||
}
|
||||
#if MPT_CXX_BEFORE(20)
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::endian_probe(), mpt::endian::little);
|
||||
#endif
|
||||
}
|
||||
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) {
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::get_endian(), mpt::endian::big);
|
||||
MPT_MAYBE_CONSTANT_IF ((mpt::endian::native == mpt::endian::little) || (mpt::endian::native == mpt::endian::big)) {
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::endian::native, mpt::endian::big);
|
||||
}
|
||||
#if MPT_CXX_BEFORE(20)
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::endian_probe(), mpt::endian::big);
|
||||
#endif
|
||||
}
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::popcount(static_cast<uint32>(int32(-1))), 32);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::popcount(0u), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::popcount(1u), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::popcount(2u), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::popcount(3u), 2);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(0u), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(1u), true);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(2u), true);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(3u), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(4u), true);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(5u), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(6u), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(7u), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(8u), true);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(9u), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(uint32(0x7fffffffu)), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(uint32(0x80000000u)), true);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(uint32(0x80000001u)), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(uint32(0xfffffffeu)), false);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::has_single_bit(uint32(0xffffffffu)), false);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(0u), 1u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(1u), 1u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(2u), 2u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(3u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(4u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(5u), 8u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(6u), 8u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(7u), 8u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(8u), 8u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(9u), 16u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(uint32(0x7fffffffu)), 0x80000000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(uint32(0x80000000u)), 0x80000000u);
|
||||
//MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(uint32(0x80000001u)), 0u);
|
||||
//MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(uint32(0xfffffffeu)), 0u);
|
||||
//MPT_TEST_EXPECT_EQUAL(mpt::bit_ceil(uint32(0xffffffffu)), 0u);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(0u), 0u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(1u), 1u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(2u), 2u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(3u), 2u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(4u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(5u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(6u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(7u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(8u), 8u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(9u), 8u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(uint32(0x7fffffffu)), 0x40000000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(uint32(0x80000000u)), 0x80000000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(uint32(0x80000001u)), 0x80000000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(uint32(0xfffffffeu)), 0x80000000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_floor(uint32(0xffffffffu)), 0x80000000u);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(0u), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(1u), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(2u), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(3u), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(4u), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(5u), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(6u), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(7u), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(8u), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(9u), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(uint32(0x7fffffffu)), 31);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(uint32(0x80000000u)), 32);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(uint32(0x80000001u)), 32);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(uint32(0xfffffffeu)), 32);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::bit_width(uint32(0xffffffffu)), 32);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00000000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00000001)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00000011)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00000111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00001111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00011111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00111111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b01111111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11111111)), 8);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11111110)), 7);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11111100)), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11111000)), 5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11110000)), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11100000)), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b11000000)), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b10000000)), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_one(uint8(0b00000000)), 0);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00000000)), 8);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00000001)), 7);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00000011)), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00000111)), 5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00001111)), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00011111)), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00111111)), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b01111111)), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11111111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11111110)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11111100)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11111000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11110000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11100000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b11000000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b10000000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countl_zero(uint8(0b00000000)), 8);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00000000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00000001)), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00000011)), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00000111)), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00001111)), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00011111)), 5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00111111)), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b01111111)), 7);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11111111)), 8);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11111110)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11111100)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11111000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11110000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11100000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b11000000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b10000000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_one(uint8(0b00000000)), 0);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00000000)), 8);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00000001)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00000011)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00000111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00001111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00011111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00111111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b01111111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11111111)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11111110)), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11111100)), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11111000)), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11110000)), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11100000)), 5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b11000000)), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b10000000)), 7);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::countr_zero(uint8(0b00000000)), 8);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0xffffffffu), 32);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0xfffffffeu), 31);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x80000000u), 31);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x7fffffffu), 31);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x7ffffffeu), 30);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000007u), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000006u), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000005u), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000004u), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000003u), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000002u), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000001u), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::lower_bound_entropy_bits(0x00000000u), 0);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(uint8(0x12)), 0x12);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(uint16(0x1234)), 0x3412);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(uint32(0x12345678u)), 0x78563412u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(uint64(0x123456789abcdef0ull)), 0xf0debc9a78563412ull);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(int8(std::numeric_limits<int8>::min())), std::numeric_limits<int8>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(int16(std::numeric_limits<int16>::min())), int16(0x80));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(int32(std::numeric_limits<int32>::min())), int32(0x80));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::byteswap(int64(std::numeric_limits<int64>::min())), int64(0x80));
|
||||
}
|
||||
|
||||
} // namespace bit
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_BASE_BIT_HPP
|
||||
@@ -0,0 +1,148 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_MATH_HPP
|
||||
#define MPT_BASE_TESTS_MATH_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/math.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace math {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/math")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.99f), 2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.5f), 2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.1f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.1f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.5f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.9f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-1.4f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-1.7f), -2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.99), 2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.5), 2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.1), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.1), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.5), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.9), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-1.4), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-1.7), -2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.99l), 2.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.5l), 2.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(1.1l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.1l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.5l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-0.9l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-1.4l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::round(-1.7l), -2.0l);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.99f), 2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.5f), 2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.1f), 2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.1f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.5f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.9f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-1.4f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-1.7f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.99), 2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.5), 2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.1), 2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.1), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.5), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.9), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-1.4), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-1.7), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.99l), 2.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.5l), 2.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(1.1l), 2.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.1l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.5l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-0.9l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-1.4l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::ceil(-1.7l), -1.0l);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.99f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.5f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.1f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.1f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.5f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.9f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-1.4f), -2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-1.7f), -2.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.99), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.5), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.1), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.1), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.5), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.9), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-1.4), -2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-1.7), -2.0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.99l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.5l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(1.1l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.1l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.5l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-0.9l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-1.4l), -2.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(std::floor(-1.7l), -2.0l);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.99f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.5f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.1f), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.1f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.5f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.9f), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-1.4f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-1.7f), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.99), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.5), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.1), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.1), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.5), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.9), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-1.4), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-1.7), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.99l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.5l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(1.1l), 1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.1l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.5l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-0.9l), 0.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-1.4l), -1.0l);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::trunc(-1.7l), -1.0l);
|
||||
}
|
||||
|
||||
} // namespace math
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_MATH_HPP
|
||||
@@ -0,0 +1,78 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_NUMERIC_HPP
|
||||
#define MPT_BASE_TESTS_NUMERIC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/numeric.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace numeric {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/numeric")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(0u, 4u), 0u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(1u, 4u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(2u, 4u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(3u, 4u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(4u, 4u), 4u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(5u, 4u), 8u);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(std::numeric_limits<uint32>::max() - 5u, 4u), std::numeric_limits<uint32>::max() - 3u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(std::numeric_limits<uint32>::max() - 4u, 4u), std::numeric_limits<uint32>::max() - 3u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(std::numeric_limits<uint32>::max() - 3u, 4u), std::numeric_limits<uint32>::max() - 3u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(std::numeric_limits<uint32>::max() - 2u, 4u), std::numeric_limits<uint32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(std::numeric_limits<uint32>::max() - 1u, 4u), std::numeric_limits<uint32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<uint32>(std::numeric_limits<uint32>::max() - 0u, 4u), std::numeric_limits<uint32>::max());
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(0, 4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(1, 4), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(2, 4), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(3, 4), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(4, 4), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(5, 4), 8);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(std::numeric_limits<int32>::max() - 5, 4), std::numeric_limits<int32>::max() - 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(std::numeric_limits<int32>::max() - 4, 4), std::numeric_limits<int32>::max() - 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(std::numeric_limits<int32>::max() - 3, 4), std::numeric_limits<int32>::max() - 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(std::numeric_limits<int32>::max() - 2, 4), std::numeric_limits<int32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(std::numeric_limits<int32>::max() - 1, 4), std::numeric_limits<int32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_align_up<int32>(std::numeric_limits<int32>::max() - 0, 4), std::numeric_limits<int32>::max());
|
||||
}
|
||||
|
||||
} // namespace numeric
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_NUMERIC_HPP
|
||||
@@ -0,0 +1,110 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_SATURATE_CAST_HPP
|
||||
#define MPT_BASE_TESTS_SATURATE_CAST_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/saturate_cast.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace saturate_cast {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/saturate_cast")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
// trivials
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int>(-1), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int>(0), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int>(1), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int>(std::numeric_limits<int>::min()), std::numeric_limits<int>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int>(std::numeric_limits<int>::max()), std::numeric_limits<int>::max());
|
||||
|
||||
// signed / unsigned
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(std::numeric_limits<uint16>::min()), std::numeric_limits<uint16>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(std::numeric_limits<uint16>::max()), std::numeric_limits<int16>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(std::numeric_limits<uint32>::min()), (int32)std::numeric_limits<uint32>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(std::numeric_limits<uint32>::max()), std::numeric_limits<int32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int64>(std::numeric_limits<uint64>::min()), (int64)std::numeric_limits<uint64>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int64>(std::numeric_limits<uint64>::max()), std::numeric_limits<int64>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(std::numeric_limits<int16>::min()), std::numeric_limits<uint16>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(std::numeric_limits<int16>::max()), std::numeric_limits<int16>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(std::numeric_limits<int32>::min()), std::numeric_limits<uint32>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(std::numeric_limits<int32>::max()), (uint32)std::numeric_limits<int32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint64>(std::numeric_limits<int64>::min()), std::numeric_limits<uint64>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint64>(std::numeric_limits<int64>::max()), (uint64)std::numeric_limits<int64>::max());
|
||||
|
||||
// overflow
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(std::numeric_limits<int16>::min() - 1), std::numeric_limits<int16>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(std::numeric_limits<int16>::max() + 1), std::numeric_limits<int16>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(std::numeric_limits<int32>::min() - int64(1)), std::numeric_limits<int32>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(std::numeric_limits<int32>::max() + int64(1)), std::numeric_limits<int32>::max());
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(std::numeric_limits<int16>::min() - 1), std::numeric_limits<uint16>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(std::numeric_limits<int16>::max() + 1), (uint16)std::numeric_limits<int16>::max() + 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(std::numeric_limits<int32>::min() - int64(1)), std::numeric_limits<uint32>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(std::numeric_limits<int32>::max() + int64(1)), (uint32)std::numeric_limits<int32>::max() + 1);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int8>(int16(32000)), 127);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int8>(int16(-32000)), -128);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int8>(uint16(32000)), 127);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int8>(uint16(64000)), 127);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint8>(int16(32000)), 255);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint8>(int16(-32000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint8>(uint16(32000)), 255);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint8>(uint16(64000)), 255);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(int16(32000)), 32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(int16(-32000)), -32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(uint16(32000)), 32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int16>(uint16(64000)), 32767);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(int16(32000)), 32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(int16(-32000)), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(uint16(32000)), 32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint16>(uint16(64000)), 64000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(int16(32000)), 32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(int16(-32000)), -32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(uint16(32000)), 32000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(uint16(64000)), 64000);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(int16(32000)), 32000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(int16(-32000)), 0u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(uint16(32000)), 32000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(uint16(64000)), 64000u);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<uint32>(std::numeric_limits<int64>::max() - 1), std::numeric_limits<uint32>::max());
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_cast<int32>(std::numeric_limits<uint64>::max() - 1), std::numeric_limits<int32>::max());
|
||||
}
|
||||
|
||||
} // namespace saturate_cast
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_SATURATE_CAST_HPP
|
||||
@@ -0,0 +1,95 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_SATURATE_ROUND_HPP
|
||||
#define MPT_BASE_TESTS_SATURATE_ROUND_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/saturate_round.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace saturate_round {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/saturate_round")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<uint32>(static_cast<double>(std::numeric_limits<int64>::max())), std::numeric_limits<uint32>::max());
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int32>(std::numeric_limits<int32>::max() + 0.1), std::numeric_limits<int32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int32>(std::numeric_limits<int32>::max() - 0.4), std::numeric_limits<int32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int32>(std::numeric_limits<int32>::min() + 0.1), std::numeric_limits<int32>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int32>(std::numeric_limits<int32>::min() - 0.1), std::numeric_limits<int32>::min());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<uint32>(std::numeric_limits<uint32>::max() + 0.499), std::numeric_limits<uint32>::max());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(110.1), 110);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(-110.1), -110);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<int8>(-0.6), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<int8>(-0.5), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<int8>(-0.4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<int8>(0.4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<int8>(0.5), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_trunc<int8>(0.6), 0);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(-0.6), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(-0.5), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(-0.4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(0.4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(0.5), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(0.6), 1);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_ceil<int8>(-0.6), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_ceil<int8>(-0.5), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_ceil<int8>(-0.4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_ceil<int8>(0.4), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_ceil<int8>(0.5), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_ceil<int8>(0.6), 1);
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_floor<int8>(-0.6), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_floor<int8>(-0.5), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_floor<int8>(-0.4), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_floor<int8>(0.4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_floor<int8>(0.5), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::saturate_floor<int8>(0.6), 0);
|
||||
|
||||
// These should fail to compile
|
||||
//mpt::saturate_round<std::string>(1.0);
|
||||
//mpt::saturate_round<int64>(1.0);
|
||||
//mpt::saturate_round<uint64>(1.0);
|
||||
|
||||
// This should trigger assert in Round.
|
||||
//MPT_TEST_EXPECT_EQUAL(mpt::saturate_round<int8>(-129), 0);
|
||||
}
|
||||
|
||||
} // namespace saturate_round
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_SATURATE_ROUND_HPP
|
||||
@@ -0,0 +1,178 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_WRAPPING_DIVIDE_HPP
|
||||
#define MPT_BASE_TESTS_WRAPPING_DIVIDE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/wrapping_divide.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace base {
|
||||
namespace wrapping_divide {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/base/wrapping_divide")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-25, 12), 11);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-24, 12), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-23, 12), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-8, 7), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-7, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-6, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-5, 7), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-4, 7), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-3, 7), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-2, 7), 5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(-1, 7), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(0, 12), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(0, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(1, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(2, 7), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(3, 7), 3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(4, 7), 4);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(5, 7), 5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(6, 7), 6);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(7, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(8, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(23, 12), 11);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(24, 12), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(25, 12), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(uint32(0x7fffffff), uint32(0x80000000)), uint32(0x7fffffff));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(0x7ffffffe), int32(0x7fffffff)), int32(0x7ffffffe));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), int32(1)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), int32(2)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), int32(1)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), int32(2)), int32(1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), int32(1)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), int32(2)), int32(0));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), int32(0x7fffffff)), int32(0x7ffffffe));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), int32(0x7fffffff)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), int32(0x7fffffff)), int32(1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), int32(0x7ffffffe)), int32(0x7ffffffc));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), int32(0x7ffffffe)), int32(0x7ffffffd));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), int32(0x7ffffffe)), int32(0));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), int32(0x7ffffffd)), int32(0x7ffffffa));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), int32(0x7ffffffd)), int32(0x7ffffffb));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), int32(0x7ffffffd)), int32(0x7ffffffc));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(0), int32(0x7fffffff)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-1), int32(0x7fffffff)), int32(0x7ffffffe));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-2), int32(0x7fffffff)), int32(0x7ffffffd));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(0), int32(0x7ffffffe)), int32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-1), int32(0x7ffffffe)), int32(0x7ffffffd));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-2), int32(0x7ffffffe)), int32(0x7ffffffc));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(1)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(2)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(1)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(2)), uint32(1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(1)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(2)), uint32(0));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x40000001), uint32(0xffffffff)), uint32(0xbffffffe));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x40000000), uint32(0xffffffff)), uint32(0xbfffffff));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x3fffffff), uint32(0xffffffff)), uint32(0xc0000000));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(0x80000000)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(0x80000000)), uint32(1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(0x80000000)), uint32(2));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(0x80000001)), uint32(1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(0x80000001)), uint32(2));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(0x80000001)), uint32(3));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(0x80000000)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(0x80000000)), uint32(1));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(0x80000000)), uint32(2));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(0x7fffffff)), uint32(0x7ffffffe));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(0x7fffffff)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(0x7fffffff)), uint32(1));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(0x7ffffffe)), uint32(0x7ffffffc));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(0x7ffffffe)), uint32(0x7ffffffd));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(0x7ffffffe)), uint32(0));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x80000000ll), uint32(0x7ffffffd)), uint32(0x7ffffffa));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7fffffff), uint32(0x7ffffffd)), uint32(0x7ffffffb));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-0x7ffffffe), uint32(0x7ffffffd)), uint32(0x7ffffffc));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(0), uint32(0x7fffffff)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-1), uint32(0x7fffffff)), uint32(0x7ffffffe));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-2), uint32(0x7fffffff)), uint32(0x7ffffffd));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(0), uint32(0x7ffffffe)), uint32(0));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-1), uint32(0x7ffffffe)), uint32(0x7ffffffd));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_modulo(int32(-2), uint32(0x7ffffffe)), uint32(0x7ffffffc));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-15, 7), -3);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-14, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-13, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-12, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-11, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-10, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-9, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-8, 7), -2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-7, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-6, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-5, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-4, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-3, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-2, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(-1, 7), -1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(0, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(1, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(2, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(3, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(4, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(5, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(6, 7), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(7, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(8, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(9, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(10, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(11, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(12, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(13, 7), 1);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(14, 7), 2);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::wrapping_divide(15, 7), 2);
|
||||
}
|
||||
|
||||
} // namespace wrapping_divide
|
||||
} // namespace base
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_WRAPPING_DIVIDE_HPP
|
||||
@@ -0,0 +1,40 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TYPE_TRAITS_HPP
|
||||
#define MPT_BASE_TYPE_TRAITS_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
|
||||
using std::type_identity;
|
||||
|
||||
#else // ! C++20
|
||||
|
||||
template <typename T>
|
||||
struct type_identity {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
#endif // C++20
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TYPE_TRAITS_HPP
|
||||
@@ -0,0 +1,265 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_UTILITY_HPP
|
||||
#define MPT_BASE_UTILITY_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#if MPT_CXX_BEFORE(20) || MPT_LIBCXX_LLVM_BEFORE(13000)
|
||||
#include "mpt/base/saturate_cast.hpp"
|
||||
#include "mpt/base/saturate_round.hpp"
|
||||
#endif
|
||||
|
||||
#if MPT_CXX_BEFORE(23) && !MPT_COMPILER_MSVC && !MPT_COMPILER_GCC && !MPT_COMPILER_CLANG
|
||||
#include <exception>
|
||||
#endif
|
||||
#include <new>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
MPT_CONSTEXPRINLINE Tdst c_cast(Tsrc && x) {
|
||||
return (Tdst)std::forward<Tsrc>(x);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
MPT_CONSTEXPRINLINE Tdst function_pointer_cast(Tsrc f) {
|
||||
#if !defined(MPT_LIBCXX_QUIRK_INCOMPLETE_IS_FUNCTION)
|
||||
// MinGW64 std::is_function is always false for non __cdecl functions.
|
||||
// Issue is similar to <https://connect.microsoft.com/VisualStudio/feedback/details/774720/stl-is-function-bug>.
|
||||
static_assert(std::is_pointer<typename std::remove_cv<Tsrc>::type>::value);
|
||||
static_assert(std::is_pointer<typename std::remove_cv<Tdst>::type>::value);
|
||||
static_assert(std::is_function<typename std::remove_pointer<typename std::remove_cv<Tsrc>::type>::type>::value);
|
||||
static_assert(std::is_function<typename std::remove_pointer<typename std::remove_cv<Tdst>::type>::type>::value);
|
||||
#endif
|
||||
#if (MPT_CLANG_AT_LEAST(19, 0, 0) && !MPT_OS_ANDROID) || MPT_CLANG_AT_LEAST(20, 0, 0)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
|
||||
#endif
|
||||
return reinterpret_cast<Tdst>(f);
|
||||
#if (MPT_CLANG_AT_LEAST(19, 0, 0) && !MPT_OS_ANDROID) || MPT_CLANG_AT_LEAST(20, 0, 0)
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && !MPT_LIBCXX_LLVM_BEFORE(13000)
|
||||
|
||||
using std::in_range;
|
||||
|
||||
#else
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename Tdst, typename Tsrc>
|
||||
constexpr Tdst saturate_cast(Tsrc src) noexcept {
|
||||
return mpt::saturate_cast<Tdst>(src);
|
||||
}
|
||||
|
||||
template <typename Tdst>
|
||||
constexpr Tdst saturate_cast(double src) {
|
||||
return mpt::saturate_trunc<Tdst>(src);
|
||||
}
|
||||
|
||||
template <typename Tdst>
|
||||
constexpr Tdst saturate_cast(float src) {
|
||||
return mpt::saturate_trunc<Tdst>(src);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Returns true iff Tdst can represent the value val.
|
||||
// Use as if(mpt::in_range<uint8>(-1)).
|
||||
template <typename Tdst, typename Tsrc>
|
||||
constexpr bool in_range(Tsrc val) {
|
||||
return (static_cast<Tsrc>(mpt::detail::saturate_cast<Tdst>(val)) == val);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(23)
|
||||
|
||||
using std::to_underlying;
|
||||
|
||||
#else // !C++23
|
||||
|
||||
template <typename T>
|
||||
constexpr std::underlying_type_t<T> to_underlying(T value) noexcept {
|
||||
return static_cast<typename std::underlying_type<T>::type>(value);
|
||||
}
|
||||
|
||||
#endif // C++23
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct value_initializer {
|
||||
inline void operator()(T & x) {
|
||||
x = T{};
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
struct value_initializer<T[N]> {
|
||||
inline void operator()(T (&a)[N]) {
|
||||
for (auto & e : a) {
|
||||
value_initializer<T>{}(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline void reset(T & x) {
|
||||
value_initializer<T>{}(x);
|
||||
}
|
||||
|
||||
template <typename T, typename... Targs>
|
||||
void reset(T & x, Targs &&... args) {
|
||||
x = T{std::forward<Targs>(args)...};
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T, typename... Targs>
|
||||
void reconstruct(T & x, Targs &&... args) {
|
||||
x.~T();
|
||||
new (&x) T{std::forward<Targs>(args)...};
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20) && !MPT_LIBCXX_LLVM_BEFORE(13000)
|
||||
|
||||
using std::cmp_equal;
|
||||
using std::cmp_greater;
|
||||
using std::cmp_greater_equal;
|
||||
using std::cmp_less;
|
||||
using std::cmp_less_equal;
|
||||
using std::cmp_not_equal;
|
||||
|
||||
#else
|
||||
|
||||
template <typename Ta, typename Tb>
|
||||
constexpr bool cmp_equal(Ta a, Tb b) noexcept {
|
||||
using UTa = typename std::make_unsigned<Ta>::type;
|
||||
using UTb = typename std::make_unsigned<Tb>::type;
|
||||
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
|
||||
return a == b;
|
||||
} else if constexpr (std::is_signed<Ta>::value) {
|
||||
return (a < 0) ? false : static_cast<UTa>(a) == b;
|
||||
} else {
|
||||
return (b < 0) ? false : a == static_cast<UTb>(b);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Ta, typename Tb>
|
||||
constexpr bool cmp_not_equal(Ta a, Tb b) noexcept {
|
||||
using UTa = typename std::make_unsigned<Ta>::type;
|
||||
using UTb = typename std::make_unsigned<Tb>::type;
|
||||
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
|
||||
return a != b;
|
||||
} else if constexpr (std::is_signed<Ta>::value) {
|
||||
return (a < 0) ? true : static_cast<UTa>(a) != b;
|
||||
} else {
|
||||
return (b < 0) ? true : a != static_cast<UTb>(b);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Ta, typename Tb>
|
||||
constexpr bool cmp_less(Ta a, Tb b) noexcept {
|
||||
using UTa = typename std::make_unsigned<Ta>::type;
|
||||
using UTb = typename std::make_unsigned<Tb>::type;
|
||||
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
|
||||
return a < b;
|
||||
} else if constexpr (std::is_signed<Ta>::value) {
|
||||
return (a < 0) ? true : static_cast<UTa>(a) < b;
|
||||
} else {
|
||||
return (b < 0) ? false : a < static_cast<UTb>(b);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Ta, typename Tb>
|
||||
constexpr bool cmp_greater(Ta a, Tb b) noexcept {
|
||||
using UTa = typename std::make_unsigned<Ta>::type;
|
||||
using UTb = typename std::make_unsigned<Tb>::type;
|
||||
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
|
||||
return a > b;
|
||||
} else if constexpr (std::is_signed<Ta>::value) {
|
||||
return (a < 0) ? false : static_cast<UTa>(a) > b;
|
||||
} else {
|
||||
return (b < 0) ? true : a > static_cast<UTb>(b);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Ta, typename Tb>
|
||||
constexpr bool cmp_less_equal(Ta a, Tb b) noexcept {
|
||||
using UTa = typename std::make_unsigned<Ta>::type;
|
||||
using UTb = typename std::make_unsigned<Tb>::type;
|
||||
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
|
||||
return a <= b;
|
||||
} else if constexpr (std::is_signed<Ta>::value) {
|
||||
return (a < 0) ? true : static_cast<UTa>(a) <= b;
|
||||
} else {
|
||||
return (b < 0) ? false : a <= static_cast<UTb>(b);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Ta, typename Tb>
|
||||
constexpr bool cmp_greater_equal(Ta a, Tb b) noexcept {
|
||||
using UTa = typename std::make_unsigned<Ta>::type;
|
||||
using UTb = typename std::make_unsigned<Tb>::type;
|
||||
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
|
||||
return a >= b;
|
||||
} else if constexpr (std::is_signed<Ta>::value) {
|
||||
return (a < 0) ? false : static_cast<UTa>(a) >= b;
|
||||
} else {
|
||||
return (b < 0) ? true : a >= static_cast<UTb>(b);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if MPT_CXX_AT_LEAST(23) && !MPT_LIBCXX_GNU_BEFORE(12)
|
||||
|
||||
using std::unreachable;
|
||||
|
||||
#else // !C++23
|
||||
|
||||
[[noreturn]] inline void unreachable() {
|
||||
#if MPT_COMPILER_MSVC
|
||||
__assume(false);
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
__builtin_unreachable();
|
||||
#else
|
||||
std::terminate();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // C++23
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_UTILITY_HPP
|
||||
@@ -0,0 +1,15 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_VERSION_HPP
|
||||
#define MPT_BASE_VERSION_HPP
|
||||
|
||||
|
||||
|
||||
#define MPT_VERSION_MAJOR 0
|
||||
#define MPT_VERSION_MINOR 0
|
||||
#define MPT_VERSION_PATCH 0
|
||||
#define MPT_VERSION_BUILD 0
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_VERSION_HPP
|
||||
@@ -0,0 +1,36 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_WRAPPING_DIVIDE_HPP
|
||||
#define MPT_BASE_WRAPPING_DIVIDE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
// Modulo with more intuitive behaviour for some contexts:
|
||||
// Instead of being symmetrical around 0, the pattern for positive numbers is repeated in the negative range.
|
||||
// For example, wrapping_modulo(-1, m) == (m - 1).
|
||||
// Behaviour is undefined if m<=0.
|
||||
template <typename T, typename M>
|
||||
constexpr auto wrapping_modulo(T x, M m) -> decltype(x % m) {
|
||||
return (x >= 0) ? (x % m) : (m - 1 - ((-1 - x) % m));
|
||||
}
|
||||
|
||||
template <typename T, typename D>
|
||||
constexpr auto wrapping_divide(T x, D d) -> decltype(x / d) {
|
||||
return (x >= 0) ? (x / d) : (((x + 1) / d) - 1);
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_WRAPPING_DIVIDE_HPP
|
||||
@@ -0,0 +1,137 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BINARY_BASE64_HPP
|
||||
#define MPT_BINARY_BASE64_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
class base64_parse_error : public std::runtime_error {
|
||||
public:
|
||||
base64_parse_error()
|
||||
: std::runtime_error("invalid Base64 encoding") {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline constexpr std::array<mpt::uchar, 64> base64 = {
|
||||
{MPT_UCHAR('A'), MPT_UCHAR('B'), MPT_UCHAR('C'), MPT_UCHAR('D'), MPT_UCHAR('E'), MPT_UCHAR('F'), MPT_UCHAR('G'), MPT_UCHAR('H'), MPT_UCHAR('I'), MPT_UCHAR('J'), MPT_UCHAR('K'), MPT_UCHAR('L'), MPT_UCHAR('M'), MPT_UCHAR('N'), MPT_UCHAR('O'), MPT_UCHAR('P'),
|
||||
MPT_UCHAR('Q'), MPT_UCHAR('R'), MPT_UCHAR('S'), MPT_UCHAR('T'), MPT_UCHAR('U'), MPT_UCHAR('V'), MPT_UCHAR('W'), MPT_UCHAR('X'), MPT_UCHAR('Y'), MPT_UCHAR('Z'), MPT_UCHAR('a'), MPT_UCHAR('b'), MPT_UCHAR('c'), MPT_UCHAR('d'), MPT_UCHAR('e'), MPT_UCHAR('f'),
|
||||
MPT_UCHAR('g'), MPT_UCHAR('h'), MPT_UCHAR('i'), MPT_UCHAR('j'), MPT_UCHAR('k'), MPT_UCHAR('l'), MPT_UCHAR('m'), MPT_UCHAR('n'), MPT_UCHAR('o'), MPT_UCHAR('p'), MPT_UCHAR('q'), MPT_UCHAR('r'), MPT_UCHAR('s'), MPT_UCHAR('t'), MPT_UCHAR('u'), MPT_UCHAR('v'),
|
||||
MPT_UCHAR('w'), MPT_UCHAR('x'), MPT_UCHAR('y'), MPT_UCHAR('z'), MPT_UCHAR('0'), MPT_UCHAR('1'), MPT_UCHAR('2'), MPT_UCHAR('3'), MPT_UCHAR('4'), MPT_UCHAR('5'), MPT_UCHAR('6'), MPT_UCHAR('7'), MPT_UCHAR('8'), MPT_UCHAR('9'), MPT_UCHAR('+'), MPT_UCHAR('/')}
|
||||
};
|
||||
|
||||
|
||||
template <typename Tbyte>
|
||||
inline mpt::ustring encode_base64(mpt::span<Tbyte> src_) {
|
||||
mpt::const_byte_span src = mpt::byte_cast<mpt::const_byte_span>(src_);
|
||||
mpt::ustring result;
|
||||
result.reserve(4 * ((src.size() + 2) / 3));
|
||||
uint32 bits = 0;
|
||||
std::size_t bytes = 0;
|
||||
for (std::byte byte : src) {
|
||||
bits <<= 8;
|
||||
bits |= mpt::byte_cast<uint8>(byte);
|
||||
bytes++;
|
||||
if (bytes == 3) {
|
||||
result.push_back(base64[(bits >> 18) & 0x3f]);
|
||||
result.push_back(base64[(bits >> 12) & 0x3f]);
|
||||
result.push_back(base64[(bits >> 6) & 0x3f]);
|
||||
result.push_back(base64[(bits >> 0) & 0x3f]);
|
||||
bits = 0;
|
||||
bytes = 0;
|
||||
}
|
||||
}
|
||||
std::size_t padding = 0;
|
||||
while (bytes != 0) {
|
||||
bits <<= 8;
|
||||
padding++;
|
||||
bytes++;
|
||||
if (bytes == 3) {
|
||||
result.push_back(base64[(bits >> 18) & 0x3f]);
|
||||
result.push_back(base64[(bits >> 12) & 0x3f]);
|
||||
if (padding > 1) {
|
||||
result.push_back(MPT_UCHAR('='));
|
||||
} else {
|
||||
result.push_back(base64[(bits >> 6) & 0x3f]);
|
||||
}
|
||||
if (padding > 0) {
|
||||
result.push_back(MPT_UCHAR('='));
|
||||
} else {
|
||||
result.push_back(base64[(bits >> 0) & 0x3f]);
|
||||
}
|
||||
bits = 0;
|
||||
bytes = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline uint8 decode_base64_bits(mpt::uchar c) {
|
||||
for (uint8 i = 0; i < 64; ++i) {
|
||||
if (base64[i] == c) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw base64_parse_error();
|
||||
}
|
||||
|
||||
|
||||
inline std::vector<std::byte> decode_base64(const mpt::ustring & src) {
|
||||
std::vector<std::byte> result;
|
||||
result.reserve(3 * (src.length() / 4));
|
||||
uint32 bits = 0;
|
||||
std::size_t chars = 0;
|
||||
std::size_t padding = 0;
|
||||
for (mpt::uchar c : src) {
|
||||
bits <<= 6;
|
||||
if (c == MPT_UCHAR('=')) {
|
||||
padding++;
|
||||
} else {
|
||||
bits |= decode_base64_bits(c);
|
||||
}
|
||||
chars++;
|
||||
if (chars == 4) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 16) & 0xff)));
|
||||
if (padding < 2) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 8) & 0xff)));
|
||||
}
|
||||
if (padding < 1) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 0) & 0xff)));
|
||||
}
|
||||
bits = 0;
|
||||
chars = 0;
|
||||
padding = 0;
|
||||
}
|
||||
}
|
||||
if (chars != 0) {
|
||||
throw base64_parse_error();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BINARY_BASE64_HPP
|
||||
@@ -0,0 +1,140 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BINARY_BASE64URL_HPP
|
||||
#define MPT_BINARY_BASE64URL_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
class base64url_parse_error : public std::runtime_error {
|
||||
public:
|
||||
base64url_parse_error()
|
||||
: std::runtime_error("invalid Base64URL encoding") {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline constexpr std::array<mpt::uchar, 64> base64url = {
|
||||
{MPT_UCHAR('A'), MPT_UCHAR('B'), MPT_UCHAR('C'), MPT_UCHAR('D'), MPT_UCHAR('E'), MPT_UCHAR('F'), MPT_UCHAR('G'), MPT_UCHAR('H'), MPT_UCHAR('I'), MPT_UCHAR('J'), MPT_UCHAR('K'), MPT_UCHAR('L'), MPT_UCHAR('M'), MPT_UCHAR('N'), MPT_UCHAR('O'), MPT_UCHAR('P'),
|
||||
MPT_UCHAR('Q'), MPT_UCHAR('R'), MPT_UCHAR('S'), MPT_UCHAR('T'), MPT_UCHAR('U'), MPT_UCHAR('V'), MPT_UCHAR('W'), MPT_UCHAR('X'), MPT_UCHAR('Y'), MPT_UCHAR('Z'), MPT_UCHAR('a'), MPT_UCHAR('b'), MPT_UCHAR('c'), MPT_UCHAR('d'), MPT_UCHAR('e'), MPT_UCHAR('f'),
|
||||
MPT_UCHAR('g'), MPT_UCHAR('h'), MPT_UCHAR('i'), MPT_UCHAR('j'), MPT_UCHAR('k'), MPT_UCHAR('l'), MPT_UCHAR('m'), MPT_UCHAR('n'), MPT_UCHAR('o'), MPT_UCHAR('p'), MPT_UCHAR('q'), MPT_UCHAR('r'), MPT_UCHAR('s'), MPT_UCHAR('t'), MPT_UCHAR('u'), MPT_UCHAR('v'),
|
||||
MPT_UCHAR('w'), MPT_UCHAR('x'), MPT_UCHAR('y'), MPT_UCHAR('z'), MPT_UCHAR('0'), MPT_UCHAR('1'), MPT_UCHAR('2'), MPT_UCHAR('3'), MPT_UCHAR('4'), MPT_UCHAR('5'), MPT_UCHAR('6'), MPT_UCHAR('7'), MPT_UCHAR('8'), MPT_UCHAR('9'), MPT_UCHAR('-'), MPT_UCHAR('_')}
|
||||
};
|
||||
|
||||
template <typename Tbyte>
|
||||
inline mpt::ustring encode_base64url(mpt::span<Tbyte> src_) {
|
||||
mpt::const_byte_span src = mpt::byte_cast<mpt::const_byte_span>(src_);
|
||||
mpt::ustring result;
|
||||
result.reserve(4 * ((src.size() + 2) / 3));
|
||||
uint32 bits = 0;
|
||||
std::size_t bytes = 0;
|
||||
for (std::byte byte : src) {
|
||||
bits <<= 8;
|
||||
bits |= mpt::byte_cast<uint8>(byte);
|
||||
bytes++;
|
||||
if (bytes == 3) {
|
||||
result.push_back(base64url[(bits >> 18) & 0x3f]);
|
||||
result.push_back(base64url[(bits >> 12) & 0x3f]);
|
||||
result.push_back(base64url[(bits >> 6) & 0x3f]);
|
||||
result.push_back(base64url[(bits >> 0) & 0x3f]);
|
||||
bits = 0;
|
||||
bytes = 0;
|
||||
}
|
||||
}
|
||||
std::size_t padding = 0;
|
||||
while (bytes != 0) {
|
||||
bits <<= 8;
|
||||
padding++;
|
||||
bytes++;
|
||||
if (bytes == 3) {
|
||||
result.push_back(base64url[(bits >> 18) & 0x3f]);
|
||||
result.push_back(base64url[(bits >> 12) & 0x3f]);
|
||||
if (padding <= 1) {
|
||||
result.push_back(base64url[(bits >> 6) & 0x3f]);
|
||||
}
|
||||
if (padding <= 0) {
|
||||
result.push_back(base64url[(bits >> 0) & 0x3f]);
|
||||
}
|
||||
bits = 0;
|
||||
bytes = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline uint8 decode_base64url_bits(mpt::uchar c) {
|
||||
for (uint8 i = 0; i < 64; ++i)
|
||||
{
|
||||
if (base64url[i] == c)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw base64url_parse_error();
|
||||
}
|
||||
|
||||
inline std::vector<std::byte> decode_base64url(const mpt::ustring & src) {
|
||||
std::vector<std::byte> result;
|
||||
result.reserve(3 * ((src.length() + 2) / 4));
|
||||
uint32 bits = 0;
|
||||
std::size_t chars = 0;
|
||||
for (mpt::uchar c : src) {
|
||||
bits <<= 6;
|
||||
bits |= decode_base64url_bits(c);
|
||||
chars++;
|
||||
if (chars == 4) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 16) & 0xff)));
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 8) & 0xff)));
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 0) & 0xff)));
|
||||
bits = 0;
|
||||
chars = 0;
|
||||
}
|
||||
}
|
||||
uint32 padding = 0;
|
||||
if (chars != 0 && chars < 2) {
|
||||
throw base64url_parse_error();
|
||||
}
|
||||
while (chars != 0) {
|
||||
bits <<= 6;
|
||||
padding++;
|
||||
chars++;
|
||||
if (chars == 4) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 16) & 0xff)));
|
||||
if (padding < 2) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 8) & 0xff)));
|
||||
}
|
||||
if (padding < 1) {
|
||||
result.push_back(mpt::byte_cast<std::byte>(static_cast<uint8>((bits >> 0) & 0xff)));
|
||||
}
|
||||
bits = 0;
|
||||
chars = 0;
|
||||
padding = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BINARY_BASE64URL_HPP
|
||||
@@ -0,0 +1,89 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BINARY_HEX_HPP
|
||||
#define MPT_BINARY_HEX_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
inline constexpr std::array<mpt::uchar, 16> encode_nibble = {
|
||||
{MPT_UCHAR('0'), MPT_UCHAR('1'), MPT_UCHAR('2'), MPT_UCHAR('3'),
|
||||
MPT_UCHAR('4'), MPT_UCHAR('5'), MPT_UCHAR('6'), MPT_UCHAR('7'),
|
||||
MPT_UCHAR('8'), MPT_UCHAR('9'), MPT_UCHAR('A'), MPT_UCHAR('B'),
|
||||
MPT_UCHAR('C'), MPT_UCHAR('D'), MPT_UCHAR('E'), MPT_UCHAR('F')}
|
||||
};
|
||||
|
||||
|
||||
inline bool decode_byte(uint8 & byte, mpt::uchar c1, mpt::uchar c2) {
|
||||
byte = 0;
|
||||
if (MPT_UCHAR('0') <= c1 && c1 <= MPT_UCHAR('9')) {
|
||||
byte += static_cast<uint8>((c1 - MPT_UCHAR('0')) << 4);
|
||||
} else if (MPT_UCHAR('A') <= c1 && c1 <= MPT_UCHAR('F')) {
|
||||
byte += static_cast<uint8>((c1 - MPT_UCHAR('A') + 10) << 4);
|
||||
} else if (MPT_UCHAR('a') <= c1 && c1 <= MPT_UCHAR('f')) {
|
||||
byte += static_cast<uint8>((c1 - MPT_UCHAR('a') + 10) << 4);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (MPT_UCHAR('0') <= c2 && c2 <= MPT_UCHAR('9')) {
|
||||
byte += static_cast<uint8>(c2 - MPT_UCHAR('0'));
|
||||
} else if (MPT_UCHAR('A') <= c2 && c2 <= MPT_UCHAR('F')) {
|
||||
byte += static_cast<uint8>(c2 - MPT_UCHAR('A') + 10);
|
||||
} else if (MPT_UCHAR('a') <= c2 && c2 <= MPT_UCHAR('f')) {
|
||||
byte += static_cast<uint8>(c2 - MPT_UCHAR('a') + 10);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template <typename Tbyte>
|
||||
inline mpt::ustring encode_hex(mpt::span<Tbyte> src_) {
|
||||
mpt::const_byte_span src = mpt::byte_cast<mpt::const_byte_span>(src_);
|
||||
mpt::ustring result;
|
||||
result.reserve(src.size() * 2);
|
||||
for (std::byte byte : src) {
|
||||
result.push_back(encode_nibble[(mpt::byte_cast<uint8>(byte) & 0xf0) >> 4]);
|
||||
result.push_back(encode_nibble[mpt::byte_cast<uint8>(byte) & 0x0f]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::vector<std::byte> decode_hex(const mpt::ustring & src) {
|
||||
std::vector<std::byte> result;
|
||||
result.reserve(src.size() / 2);
|
||||
for (std::size_t i = 0; (i + 1) < src.size(); i += 2) {
|
||||
uint8 byte = 0;
|
||||
if (!decode_byte(byte, src[i], src[i + 1])) {
|
||||
return result;
|
||||
}
|
||||
result.push_back(mpt::byte_cast<std::byte>(byte));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BINARY_HEX_HPP
|
||||
@@ -0,0 +1,116 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_BINARY_HPP
|
||||
#define MPT_BASE_TESTS_BINARY_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/alloc.hpp"
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/span.hpp"
|
||||
#include "mpt/binary/base64.hpp"
|
||||
#include "mpt/binary/base64url.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace binary {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/binary")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
{
|
||||
std::string expecteds = std::string("pleasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::encode_base64(mpt::as_span(expected)), MPT_USTRING("cGxlYXN1cmUu"));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("leasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::encode_base64(mpt::as_span(expected)), MPT_USTRING("bGVhc3VyZS4="));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("easure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::encode_base64(mpt::as_span(expected)), MPT_USTRING("ZWFzdXJlLg=="));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("pleasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(expected, mpt::decode_base64(MPT_USTRING("cGxlYXN1cmUu")));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("leasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(expected, mpt::decode_base64(MPT_USTRING("bGVhc3VyZS4=")));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("easure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(expected, mpt::decode_base64(MPT_USTRING("ZWFzdXJlLg==")));
|
||||
}
|
||||
|
||||
{
|
||||
std::string expecteds = std::string("pleasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::encode_base64url(mpt::as_span(expected)), MPT_USTRING("cGxlYXN1cmUu"));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("leasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::encode_base64url(mpt::as_span(expected)), MPT_USTRING("bGVhc3VyZS4"));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("easure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::encode_base64url(mpt::as_span(expected)), MPT_USTRING("ZWFzdXJlLg"));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("pleasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(expected, mpt::decode_base64url(MPT_USTRING("cGxlYXN1cmUu")));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("leasure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(expected, mpt::decode_base64url(MPT_USTRING("bGVhc3VyZS4")));
|
||||
}
|
||||
{
|
||||
std::string expecteds = std::string("easure.");
|
||||
std::vector<std::byte> expected(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data(), mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).data() + mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(expecteds)).size());
|
||||
MPT_TEST_EXPECT_EQUAL(expected, mpt::decode_base64url(MPT_USTRING("ZWFzdXJlLg")));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace binary
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_BINARY_HPP
|
||||
@@ -0,0 +1,120 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_CHECK_COMPILER_HPP
|
||||
#define MPT_CHECK_COMPILER_HPP
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/detect_quirks.hpp"
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_PREPROCESSOR
|
||||
#if defined(MPT_COMPILER_QUIRK_MSVC_OLD_PREPROCESSOR)
|
||||
MPT_WARNING("C++ preprocessor is not standard conformings.")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_FASTMATH
|
||||
#if MPT_COMPILER_MSVC
|
||||
#if (defined(_M_FP_FAST) && (_M_FP_FAST == 1))
|
||||
MPT_WARNING("C++ compiler has fast-math support enabled. This is not standard-conforming.")
|
||||
#endif
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
#if (defined(__FAST_MATH__) && (__FAST_MATH__ == 1))
|
||||
MPT_WARNING("C++ compiler has fast-math support enabled. This is not standard-conforming.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_FINITEMATH
|
||||
#if MPT_COMPILER_MSVC
|
||||
#if (defined(_M_FP_FAST) && (_M_FP_FAST == 1))
|
||||
MPT_WARNING("C++ compiler assumes finite math only. This is not standard-conforming.")
|
||||
#endif
|
||||
#elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
|
||||
#if (defined(__FINITE_MATH_ONLY__) && (__FINITE_MATH_ONLY__ == 1))
|
||||
MPT_WARNING("C++ compiler assumes finite math only. This is not standard-conforming.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_NO_EXCEPTIONS
|
||||
#if MPT_COMPILER_MSVC
|
||||
#if !defined(_CPPUNWIND)
|
||||
MPT_WARNING("C++ compiler has no exception support.")
|
||||
#endif
|
||||
#elif MPT_COMPILER_GCC
|
||||
#if (!defined(__EXCEPTIONS) || (__EXCEPTIONS != 1))
|
||||
MPT_WARNING("C++ compiler has no exception support.")
|
||||
#endif
|
||||
#elif MPT_COMPILER_CLANG
|
||||
#if (!__has_feature(cxx_exceptions) && (!defined(__EXCEPTIONS) || (__EXCEPTIONS != 1)) && !defined(_CPPUNWIND))
|
||||
MPT_WARNING("C++ compiler has no exception support.")
|
||||
#else
|
||||
#if (MPT_CXX_AT_LEAST(20) && !defined(__cpp_exceptions))
|
||||
MPT_WARNING("C++ compiler has no exception support.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_NO_RTTI
|
||||
#if MPT_COMPILER_MSVC
|
||||
#if (!defined(_CPPRTTI) || (_CPPRTTI != 1))
|
||||
MPT_WARNING("C++ compiler has no RTTI support.")
|
||||
#endif
|
||||
#elif MPT_COMPILER_GCC
|
||||
#if (!defined(__GXX_RTTI) || (__GXX_RTTI != 1))
|
||||
MPT_WARNING("C++ compiler has no RTTI support.")
|
||||
#endif
|
||||
#elif MPT_COMPILER_CLANG && !defined(_MSC_VER)
|
||||
#if (!defined(__GXX_RTTI) || (__GXX_RTTI != 1))
|
||||
MPT_WARNING("C++ compiler has no RTTI support.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_NO_UNICODE
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
#if (!defined(__STDC_ISO_10646__) || (__STDC_ISO_10646__ <= 198700L))
|
||||
#if !MPT_COMPILER_MSVC && !MPT_COMPILER_GCC && !MPT_COMPILER_CLANG
|
||||
// Disabled for all known compilers, as none of them defines __STDC_ISO_10646__, even though all of them provide Unicode wchar_t.
|
||||
MPT_WARNING("C++ compiler uses non-Unicode wchar_t.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_EBCDIC
|
||||
#if defined(__STDC_MB_MIGHT_NEQ_WC__) && (__STDC_MB_MIGHT_NEQ_WC__ == 1)
|
||||
#if !(MPT_COMPILER_CLANG && MPT_OS_FREEBSD)
|
||||
// Disabled on FreeBSD because <https://github.com/llvm/llvm-project/blob/c7a56af3072c2fa89f0968d7f00b22f7bff0812b/clang/lib/Basic/Targets/OSTargets.h#L225>.
|
||||
MPT_WARNING("C++ compiler uses a weird 8bit charset, maybe EBCDIC.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_NO_STDCPP_THREADS
|
||||
#if MPT_PLATFORM_MULTITHREADED
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_STDCPP_THREADS)
|
||||
#if !defined(__STDCPP_THREADS__)
|
||||
MPT_WARNING("C++ __STDCPP_THREADS__ is not defined. Non-conforming compiler detected.")
|
||||
#else
|
||||
#if (__STDCPP_THREADS__ != 1)
|
||||
MPT_WARNING("C++ compiler has no thread support.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_CXX_IGNORE_WARNING_SINGLETHREADED_THREADSAFE_STATICS
|
||||
#if !MPT_PLATFORM_MULTITHREADED
|
||||
#if MPT_CXX_AT_LEAST(20) && MPT_COMPILER_GCC
|
||||
#if (defined(__cpp_threadsafe_static_init) && (__cpp_threadsafe_static_init >= 200806L))
|
||||
MPT_WARNING("C++ compiler provides threadsafe initialization of static variables, however the platform is single-threaded.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_CHECK_COMPILER_HPP
|
||||
@@ -0,0 +1,44 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_CHECK_LIBC_HPP
|
||||
#define MPT_CHECK_LIBC_HPP
|
||||
|
||||
#include "mpt/base/detect_libc.hpp"
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
#include "mpt/base/detect_quirks.hpp"
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
#ifndef MPT_CHECK_LIBC_IGNORE_WARNING_NO_MTRT
|
||||
#if MPT_PLATFORM_MULTITHREADED
|
||||
#if MPT_LIBC_MINGW
|
||||
// MinGW only has `#define _MT` in header files instead of `#define _MT 1`.
|
||||
#if !defined(_MT)
|
||||
MPT_WARNING("C stdlib is not multi-threaded.")
|
||||
#endif
|
||||
#elif MPT_LIBC_MS
|
||||
#if defined(_MT)
|
||||
#if (_MT != 1)
|
||||
MPT_WARNING("C stdlib is not multi-threaded.")
|
||||
#endif
|
||||
#else
|
||||
MPT_WARNING("C stdlib is not multi-threaded.")
|
||||
#endif
|
||||
//#elif !MPT_LIBC_MS && !MPT_LIBC_MINGW && !MPT_LIBC_GENERIC
|
||||
//#if (!defined(_REENTRANT) || (_REENTRANT != 1))
|
||||
//MPT_WARNING("C stdlib is not multi-threaded.")
|
||||
//#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_LIBC_IGNORE_WARNING_UNICODE_MISMATCH
|
||||
#if MPT_OS_WINDOWS
|
||||
#ifdef UNICODE
|
||||
#ifndef _UNICODE
|
||||
MPT_WARNING("UNICODE is defined but _UNICODE is not defined. Please #define _UNICODE.")
|
||||
#endif
|
||||
#endif
|
||||
#endif // MPT_OS_WINDOWS
|
||||
#endif
|
||||
|
||||
#endif // MPT_CHECK_LIBC_HPP
|
||||
@@ -0,0 +1,16 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_CHECK_LIBCXX_HPP
|
||||
#define MPT_CHECK_LIBCXX_HPP
|
||||
|
||||
#include "mpt/base/detect_libcxx.hpp"
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
#include "mpt/base/detect_quirks.hpp"
|
||||
|
||||
#ifndef MPT_CHECK_LIBCXX_IGNORE_WARNING_NO_THREADS
|
||||
#if MPT_OS_WINDOWS && MPT_WIN_BEFORE(MPT_WIN_7) && MPT_LIBCXX_GNU_AT_LEAST(13) && !defined(_GLIBCXX_HAS_GTHREADS)
|
||||
#error "GNU libstdc++ is compiled without gthreads support (likely due to using Win32 threading model as opposed to POSIX or mcfgthread threading model. This a severely crippled C++11 implementation and no is no longer supported for libstdc++ version 13 or later."
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_CHECK_LIBCXX_HPP
|
||||
@@ -0,0 +1,19 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_CHECK_MFC_HPP
|
||||
#define MPT_CHECK_MFC_HPP
|
||||
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
#include "mpt/detect/mfc.hpp"
|
||||
|
||||
#if MPT_DETECTED_MFC
|
||||
|
||||
#ifndef _CSTRING_DISABLE_NARROW_WIDE_CONVERSION
|
||||
#ifndef MPT_CHECK_MFC_IGNORE_WARNING_NO_CSTRING_DISABLE_NARROW_WIDE_CONVERSION
|
||||
MPT_WARNING("MFC uses CString with automatic encoding conversions. Please #define _CSTRING_DISABLE_NARROW_WIDE_CONVERSION.")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_DETECTED_MFC
|
||||
|
||||
#endif // MPT_CHECK_MFC_HPP
|
||||
@@ -0,0 +1,39 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_CHECK_WINDOWS_HPP
|
||||
#define MPT_CHECK_WINDOWS_HPP
|
||||
|
||||
#include "mpt/base/detect_os.hpp"
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
|
||||
#ifndef MPT_CHECK_WINDOWS_IGNORE_WARNING_NO_UNICODE
|
||||
#if MPT_OS_WINDOWS_WINNT
|
||||
#ifndef UNICODE
|
||||
MPT_WARNING("windows.h uses MBCS TCHAR. Please #define UNICODE.")
|
||||
#endif
|
||||
#elif MPT_OS_WINDOWS_WIN9X
|
||||
#ifdef UNICODE
|
||||
MPT_WARNING("Targeting Win9x but windows.h uses UNICODE TCHAR. Please do not #define UNICODE.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MPT_CHECK_WINDOWS_IGNORE_WARNING_UNICODE_MISMATCH
|
||||
#ifdef _UNICODE
|
||||
#ifndef UNICODE
|
||||
MPT_WARNING("_UNICODE is defined but UNICODE is not defined. Please enable UNICODE support in your compiler, or do not #define _UNICODE.")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#ifndef MPT_CHECK_WINDOWS_IGNORE_WARNING_NO_NOMINMAX
|
||||
MPT_WARNING("windows.h defines min and max which conflicts with C++. Please #define NOMINMAX.")
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_OS_WINDOWS
|
||||
|
||||
#endif // MPT_CHECK_WINDOWS_HPP
|
||||
@@ -0,0 +1,204 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_CRC_CRC_HPP
|
||||
#define MPT_CRC_CRC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/array.hpp"
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename T, T polynomial, T initial, T resultXOR, bool reverseData>
|
||||
class crc {
|
||||
|
||||
public:
|
||||
using self_type = crc;
|
||||
using value_type = T;
|
||||
using byte_type = uint8;
|
||||
|
||||
static constexpr std::size_t size_bytes = sizeof(value_type);
|
||||
static constexpr std::size_t size_bits = sizeof(value_type) * 8;
|
||||
static constexpr value_type top_bit = static_cast<value_type>(1) << ((sizeof(value_type) * 8) - 1);
|
||||
|
||||
private:
|
||||
template <typename Tint>
|
||||
static constexpr Tint reverse(Tint value) noexcept {
|
||||
const std::size_t bits = sizeof(Tint) * 8;
|
||||
Tint result = 0;
|
||||
for (std::size_t i = 0; i < bits; ++i) {
|
||||
result <<= 1;
|
||||
result |= static_cast<Tint>(value & 0x1);
|
||||
value >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static constexpr value_type calculate_table_entry(byte_type pos) noexcept {
|
||||
value_type value = 0;
|
||||
value = (static_cast<value_type>(reverseData ? reverse(pos) : pos) << (size_bits - 8));
|
||||
for (std::size_t bit = 0; bit < 8; ++bit) {
|
||||
if (value & top_bit) {
|
||||
value = (value << 1) ^ polynomial;
|
||||
} else {
|
||||
value = (value << 1);
|
||||
}
|
||||
}
|
||||
value = (reverseData ? reverse(value) : value);
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr std::array<value_type, 256> calculate_table() noexcept {
|
||||
std::array<value_type, 256> t = mpt::init_array<value_type, 256>(value_type{});
|
||||
for (std::size_t i = 0; i < 256; ++i) {
|
||||
t[i] = calculate_table_entry(static_cast<byte_type>(i));
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
static constexpr std::array<value_type, 256> table = calculate_table();
|
||||
|
||||
private:
|
||||
constexpr value_type read_table(byte_type pos) const noexcept {
|
||||
return table[pos];
|
||||
}
|
||||
|
||||
private:
|
||||
value_type value;
|
||||
|
||||
public:
|
||||
constexpr crc() noexcept
|
||||
: value(initial) {
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr void processByte(byte_type byte) noexcept {
|
||||
if constexpr (reverseData) {
|
||||
value = (value >> 8) ^ read_table(static_cast<byte_type>((value & 0xff) ^ byte));
|
||||
} else {
|
||||
value = (value << 8) ^ read_table(static_cast<byte_type>(((value >> (size_bits - 8)) & 0xff) ^ byte));
|
||||
}
|
||||
}
|
||||
|
||||
constexpr value_type result() const noexcept {
|
||||
return (value ^ resultXOR);
|
||||
}
|
||||
|
||||
public:
|
||||
constexpr operator value_type() const noexcept {
|
||||
return result();
|
||||
}
|
||||
|
||||
inline crc & process(char c) noexcept {
|
||||
processByte(mpt::byte_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & process(signed char c) noexcept {
|
||||
processByte(static_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & process(unsigned char c) noexcept {
|
||||
processByte(mpt::byte_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & process(std::byte c) noexcept {
|
||||
processByte(mpt::byte_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename InputIt>
|
||||
inline crc & process(InputIt beg, InputIt end) {
|
||||
for (InputIt it = beg; it != end; ++it) {
|
||||
static_assert(sizeof(*it) == 1, "1 byte type required");
|
||||
process(*it);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
inline crc & process(const Container & data) {
|
||||
operator()(data.begin(), data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & operator()(char c) noexcept {
|
||||
processByte(mpt::byte_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & operator()(signed char c) noexcept {
|
||||
processByte(static_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & operator()(unsigned char c) noexcept {
|
||||
processByte(mpt::byte_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline crc & operator()(std::byte c) noexcept {
|
||||
processByte(mpt::byte_cast<byte_type>(c));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename InputIt>
|
||||
crc & operator()(InputIt beg, InputIt end) {
|
||||
for (InputIt it = beg; it != end; ++it) {
|
||||
static_assert(sizeof(*it) == 1, "1 byte type required");
|
||||
operator()(*it);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
inline crc & operator()(const Container & data) {
|
||||
operator()(data.begin(), data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename InputIt>
|
||||
crc(InputIt beg, InputIt end)
|
||||
: value(initial) {
|
||||
for (InputIt it = beg; it != end; ++it) {
|
||||
static_assert(sizeof(*it) == 1, "1 byte type required");
|
||||
process(*it);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
inline crc(const Container & data)
|
||||
: value(initial) {
|
||||
process(data.begin(), data.end());
|
||||
}
|
||||
};
|
||||
|
||||
using crc16 = crc<uint16, 0x8005, 0, 0, true>;
|
||||
using crc32 = crc<uint32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true>;
|
||||
using crc32_ogg = crc<uint32, 0x04C11DB7, 0, 0, false>;
|
||||
using crc32c = crc<uint32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true>;
|
||||
using crc64_jones = crc<uint64, 0xAD93D23594C935A9ull, 0xFFFFFFFFFFFFFFFFull, 0, true>;
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_CRC_CRC_HPP
|
||||
@@ -0,0 +1,49 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_BASE_TESTS_CRC_HPP
|
||||
#define MPT_BASE_TESTS_CRC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/crc/crc.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace crc {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/crc")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::crc32(std::string("123456789")), 0xCBF43926u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::crc32_ogg(std::string("123456789")), 0x89a1897fu);
|
||||
}
|
||||
|
||||
} // namespace crc
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_BASE_TESTS_CRC_HPP
|
||||
@@ -0,0 +1,27 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_DETECT_DL_HPP
|
||||
#define MPT_DETECT_DL_HPP
|
||||
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
#if defined(MPT_WITH_DL)
|
||||
#if !defined(CPPCHECK)
|
||||
#if !__has_include(<dlfcn.h>)
|
||||
#error "MPT_WITH_DL defined but <dlfcn.h> not found."
|
||||
#endif
|
||||
#endif
|
||||
#define MPT_DETECTED_DL 1
|
||||
#else
|
||||
#if defined(CPPCHECK)
|
||||
#define MPT_DETECTED_DL 1
|
||||
#else
|
||||
#if __has_include(<dlfcn.h>)
|
||||
#define MPT_DETECTED_DL 1
|
||||
#else
|
||||
#define MPT_DETECTED_DL 0
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_DETECT_DL_HPP
|
||||
@@ -0,0 +1,27 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_DETECT_LTDL_HPP
|
||||
#define MPT_DETECT_LTDL_HPP
|
||||
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
#if defined(MPT_WITH_LTDL)
|
||||
#if !defined(CPPCHECK)
|
||||
#if !__has_include(<ltdl.h>)
|
||||
#error "MPT_WITH_LTDL defined but <ltdl.h> not found."
|
||||
#endif
|
||||
#endif
|
||||
#define MPT_DETECTED_LTDL 1
|
||||
#else
|
||||
#if defined(CPPCHECK)
|
||||
#define MPT_DETECTED_LTDL 1
|
||||
#else
|
||||
#if __has_include(<ltdl.h>)
|
||||
#define MPT_DETECTED_LTDL 1
|
||||
#else
|
||||
#define MPT_DETECTED_LTDL 0
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_DETECT_LTDL_HPP
|
||||
@@ -0,0 +1,22 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_DETECT_MFC_HPP
|
||||
#define MPT_DETECT_MFC_HPP
|
||||
|
||||
#include "mpt/base/compiletime_warning.hpp"
|
||||
|
||||
#if defined(MPT_WITH_MFC)
|
||||
#if !defined(CPPCHECK)
|
||||
#if !__has_include(<afx.h>)
|
||||
#error "MPT_WITH_MFC defined but <afx.h> not found."
|
||||
#endif
|
||||
#endif
|
||||
#if !MPT_COMPILER_GENERIC && !MPT_COMPILER_MSVC && !MPT_COMPILER_CLANG
|
||||
MPT_WARNING("Using MFC with unsupported compiler.")
|
||||
#endif
|
||||
#define MPT_DETECTED_MFC 1
|
||||
#else
|
||||
#define MPT_DETECTED_MFC 0
|
||||
#endif
|
||||
|
||||
#endif // MPT_DETECT_MFC_HPP
|
||||
@@ -0,0 +1,25 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_DETECT_NLOHMANN_JSON_HPP
|
||||
#define MPT_DETECT_NLOHMANN_JSON_HPP
|
||||
|
||||
#if defined(MPT_WITH_NLOHMANN_JSON)
|
||||
#if !defined(CPPCHECK)
|
||||
#if !__has_include(<nlohmann/json.hpp>)
|
||||
#error "MPT_WITH_NLOHMANN_JSON defined but <nlohmann/json.hpp> not found."
|
||||
#endif
|
||||
#endif
|
||||
#define MPT_DETECTED_NLOHMANN_JSON 1
|
||||
#else
|
||||
#if defined(CPPCHECK)
|
||||
#define MPT_DETECTED_NLOHMANN_JSON 1
|
||||
#else
|
||||
#if __has_include(<nlohmann/json.hpp>)
|
||||
#define MPT_DETECTED_NLOHMANN_JSON 1
|
||||
#else
|
||||
#define MPT_DETECTED_NLOHMANN_JSON 0
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // MPT_DETECT_NLOHMANN_JSON_HPP
|
||||
@@ -0,0 +1,508 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_FLOATINGPOINT_HPP
|
||||
#define MPT_ENDIAN_FLOATINGPOINT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/float.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/endian/type_traits.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
// 1.0f --> 0x3f800000u
|
||||
MPT_FORCEINLINE uint32 EncodeIEEE754binary32(somefloat32 f) {
|
||||
if constexpr (mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian) {
|
||||
return mpt::bit_cast<uint32>(f);
|
||||
} else {
|
||||
int e = 0;
|
||||
float m = std::frexp(f, &e);
|
||||
if (e == 0 && std::fabs(m) == 0.0f) {
|
||||
uint32 expo = 0u;
|
||||
uint32 sign = std::signbit(m) ? 0x01u : 0x00u;
|
||||
uint32 mant = 0u;
|
||||
uint32 i = 0u;
|
||||
i |= (mant << 0) & 0x007fffffu;
|
||||
i |= (expo << 23) & 0x7f800000u;
|
||||
i |= (sign << 31) & 0x80000000u;
|
||||
return i;
|
||||
} else {
|
||||
uint32 expo = e + 127 - 1;
|
||||
uint32 sign = std::signbit(m) ? 0x01u : 0x00u;
|
||||
uint32 mant = static_cast<uint32>(std::fabs(std::ldexp(m, 24)));
|
||||
uint32 i = 0u;
|
||||
i |= (mant << 0) & 0x007fffffu;
|
||||
i |= (expo << 23) & 0x7f800000u;
|
||||
i |= (sign << 31) & 0x80000000u;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE uint64 EncodeIEEE754binary64(somefloat64 f) {
|
||||
if constexpr (mpt::float_traits<somefloat64>::is_float64 && mpt::float_traits<somefloat64>::is_ieee754_binary && mpt::float_traits<somefloat64>::is_native_endian) {
|
||||
return mpt::bit_cast<uint64>(f);
|
||||
} else {
|
||||
int e = 0;
|
||||
double m = std::frexp(f, &e);
|
||||
if (e == 0 && std::fabs(m) == 0.0) {
|
||||
uint64 expo = 0u;
|
||||
uint64 sign = std::signbit(m) ? 0x01u : 0x00u;
|
||||
uint64 mant = 0u;
|
||||
uint64 i = 0u;
|
||||
i |= (mant << 0) & 0x000fffffffffffffull;
|
||||
i |= (expo << 52) & 0x7ff0000000000000ull;
|
||||
i |= (sign << 63) & 0x8000000000000000ull;
|
||||
return i;
|
||||
} else {
|
||||
uint64 expo = static_cast<int64>(e) + 1023 - 1;
|
||||
uint64 sign = std::signbit(m) ? 0x01u : 0x00u;
|
||||
uint64 mant = static_cast<uint64>(std::fabs(std::ldexp(m, 53)));
|
||||
uint64 i = 0u;
|
||||
i |= (mant << 0) & 0x000fffffffffffffull;
|
||||
i |= (expo << 52) & 0x7ff0000000000000ull;
|
||||
i |= (sign << 63) & 0x8000000000000000ull;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 0x3f800000u --> 1.0f
|
||||
MPT_FORCEINLINE somefloat32 DecodeIEEE754binary32(uint32 i) {
|
||||
if constexpr (mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian) {
|
||||
return mpt::bit_cast<somefloat32>(i);
|
||||
} else {
|
||||
uint32 mant = (i & 0x007fffffu) >> 0;
|
||||
uint32 expo = (i & 0x7f800000u) >> 23;
|
||||
uint32 sign = (i & 0x80000000u) >> 31;
|
||||
if (expo == 0) {
|
||||
float m = sign ? -static_cast<float>(mant) : static_cast<float>(mant);
|
||||
int e = static_cast<int>(expo) - 127 + 1 - 24;
|
||||
float f = std::ldexp(m, e);
|
||||
return static_cast<somefloat32>(f);
|
||||
} else {
|
||||
mant |= 0x00800000u;
|
||||
float m = sign ? -static_cast<float>(mant) : static_cast<float>(mant);
|
||||
int e = static_cast<int>(expo) - 127 + 1 - 24;
|
||||
float f = std::ldexp(m, e);
|
||||
return static_cast<somefloat32>(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MPT_FORCEINLINE somefloat64 DecodeIEEE754binary64(uint64 i) {
|
||||
if constexpr (mpt::float_traits<somefloat64>::is_float64 && mpt::float_traits<somefloat64>::is_ieee754_binary && mpt::float_traits<somefloat64>::is_native_endian) {
|
||||
return mpt::bit_cast<somefloat64>(i);
|
||||
} else {
|
||||
uint64 mant = (i & 0x000fffffffffffffull) >> 0;
|
||||
uint64 expo = (i & 0x7ff0000000000000ull) >> 52;
|
||||
uint64 sign = (i & 0x8000000000000000ull) >> 63;
|
||||
if (expo == 0) {
|
||||
double m = sign ? -static_cast<double>(mant) : static_cast<double>(mant);
|
||||
int e = static_cast<int>(expo) - 1023 + 1 - 53;
|
||||
double f = std::ldexp(m, e);
|
||||
return static_cast<somefloat64>(f);
|
||||
} else {
|
||||
mant |= 0x0010000000000000ull;
|
||||
double m = sign ? -static_cast<double>(mant) : static_cast<double>(mant);
|
||||
int e = static_cast<int>(expo) - 1023 + 1 - 53;
|
||||
double f = std::ldexp(m, e);
|
||||
return static_cast<somefloat64>(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// template parameters are byte indices corresponding to the individual bytes of iee754 in memory
|
||||
template <std::size_t hihi, std::size_t hilo, std::size_t lohi, std::size_t lolo>
|
||||
struct IEEE754binary32Emulated {
|
||||
public:
|
||||
using self_t = IEEE754binary32Emulated<hihi, hilo, lohi, lolo>;
|
||||
std::byte bytes[4];
|
||||
|
||||
public:
|
||||
MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
|
||||
return bytes[i];
|
||||
}
|
||||
IEEE754binary32Emulated() = default;
|
||||
MPT_FORCEINLINE explicit IEEE754binary32Emulated(somefloat32 f) {
|
||||
SetInt32(EncodeIEEE754binary32(f));
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary32Emulated & operator=(somefloat32 f) {
|
||||
SetInt32(EncodeIEEE754binary32(f));
|
||||
return *this;
|
||||
}
|
||||
// b0...b3 are in memory order, i.e. depend on the endianness of this type
|
||||
// little endian: (0x00,0x00,0x80,0x3f)
|
||||
// big endian: (0x3f,0x80,0x00,0x00)
|
||||
MPT_FORCEINLINE explicit IEEE754binary32Emulated(std::byte b0, std::byte b1, std::byte b2, std::byte b3) {
|
||||
bytes[0] = b0;
|
||||
bytes[1] = b1;
|
||||
bytes[2] = b2;
|
||||
bytes[3] = b3;
|
||||
}
|
||||
MPT_FORCEINLINE operator somefloat32() const {
|
||||
return DecodeIEEE754binary32(GetInt32());
|
||||
}
|
||||
MPT_FORCEINLINE self_t & set(somefloat32 f) {
|
||||
SetInt32(EncodeIEEE754binary32(f));
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE somefloat32 get() const {
|
||||
return DecodeIEEE754binary32(GetInt32());
|
||||
}
|
||||
MPT_FORCEINLINE self_t & SetInt32(uint32 i) {
|
||||
bytes[hihi] = static_cast<std::byte>(i >> 24);
|
||||
bytes[hilo] = static_cast<std::byte>(i >> 16);
|
||||
bytes[lohi] = static_cast<std::byte>(i >> 8);
|
||||
bytes[lolo] = static_cast<std::byte>(i >> 0);
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE uint32 GetInt32() const {
|
||||
return 0u
|
||||
| (static_cast<uint32>(bytes[hihi]) << 24)
|
||||
| (static_cast<uint32>(bytes[hilo]) << 16)
|
||||
| (static_cast<uint32>(bytes[lohi]) << 8)
|
||||
| (static_cast<uint32>(bytes[lolo]) << 0);
|
||||
}
|
||||
MPT_FORCEINLINE bool operator==(const self_t & cmp) const {
|
||||
return true
|
||||
&& bytes[0] == cmp.bytes[0]
|
||||
&& bytes[1] == cmp.bytes[1]
|
||||
&& bytes[2] == cmp.bytes[2]
|
||||
&& bytes[3] == cmp.bytes[3];
|
||||
}
|
||||
MPT_FORCEINLINE bool operator!=(const self_t & cmp) const {
|
||||
return !(*this == cmp);
|
||||
}
|
||||
};
|
||||
template <std::size_t hihihi, std::size_t hihilo, std::size_t hilohi, std::size_t hilolo, std::size_t lohihi, std::size_t lohilo, std::size_t lolohi, std::size_t lololo>
|
||||
struct IEEE754binary64Emulated {
|
||||
public:
|
||||
using self_t = IEEE754binary64Emulated<hihihi, hihilo, hilohi, hilolo, lohihi, lohilo, lolohi, lololo>;
|
||||
std::byte bytes[8];
|
||||
|
||||
public:
|
||||
MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
|
||||
return bytes[i];
|
||||
}
|
||||
IEEE754binary64Emulated() = default;
|
||||
MPT_FORCEINLINE explicit IEEE754binary64Emulated(somefloat64 f) {
|
||||
SetInt64(EncodeIEEE754binary64(f));
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary64Emulated & operator=(somefloat64 f) {
|
||||
SetInt64(EncodeIEEE754binary64(f));
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE explicit IEEE754binary64Emulated(std::byte b0, std::byte b1, std::byte b2, std::byte b3, std::byte b4, std::byte b5, std::byte b6, std::byte b7) {
|
||||
bytes[0] = b0;
|
||||
bytes[1] = b1;
|
||||
bytes[2] = b2;
|
||||
bytes[3] = b3;
|
||||
bytes[4] = b4;
|
||||
bytes[5] = b5;
|
||||
bytes[6] = b6;
|
||||
bytes[7] = b7;
|
||||
}
|
||||
MPT_FORCEINLINE operator somefloat64() const {
|
||||
return DecodeIEEE754binary64(GetInt64());
|
||||
}
|
||||
MPT_FORCEINLINE self_t & set(somefloat64 f) {
|
||||
SetInt64(EncodeIEEE754binary64(f));
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE somefloat64 get() const {
|
||||
return DecodeIEEE754binary64(GetInt64());
|
||||
}
|
||||
MPT_FORCEINLINE self_t & SetInt64(uint64 i) {
|
||||
bytes[hihihi] = static_cast<std::byte>(i >> 56);
|
||||
bytes[hihilo] = static_cast<std::byte>(i >> 48);
|
||||
bytes[hilohi] = static_cast<std::byte>(i >> 40);
|
||||
bytes[hilolo] = static_cast<std::byte>(i >> 32);
|
||||
bytes[lohihi] = static_cast<std::byte>(i >> 24);
|
||||
bytes[lohilo] = static_cast<std::byte>(i >> 16);
|
||||
bytes[lolohi] = static_cast<std::byte>(i >> 8);
|
||||
bytes[lololo] = static_cast<std::byte>(i >> 0);
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE uint64 GetInt64() const {
|
||||
return 0u
|
||||
| (static_cast<uint64>(bytes[hihihi]) << 56)
|
||||
| (static_cast<uint64>(bytes[hihilo]) << 48)
|
||||
| (static_cast<uint64>(bytes[hilohi]) << 40)
|
||||
| (static_cast<uint64>(bytes[hilolo]) << 32)
|
||||
| (static_cast<uint64>(bytes[lohihi]) << 24)
|
||||
| (static_cast<uint64>(bytes[lohilo]) << 16)
|
||||
| (static_cast<uint64>(bytes[lolohi]) << 8)
|
||||
| (static_cast<uint64>(bytes[lololo]) << 0);
|
||||
}
|
||||
MPT_FORCEINLINE bool operator==(const self_t & cmp) const {
|
||||
return true
|
||||
&& bytes[0] == cmp.bytes[0]
|
||||
&& bytes[1] == cmp.bytes[1]
|
||||
&& bytes[2] == cmp.bytes[2]
|
||||
&& bytes[3] == cmp.bytes[3]
|
||||
&& bytes[4] == cmp.bytes[4]
|
||||
&& bytes[5] == cmp.bytes[5]
|
||||
&& bytes[6] == cmp.bytes[6]
|
||||
&& bytes[7] == cmp.bytes[7];
|
||||
}
|
||||
MPT_FORCEINLINE bool operator!=(const self_t & cmp) const {
|
||||
return !(*this == cmp);
|
||||
}
|
||||
};
|
||||
|
||||
using IEEE754binary32EmulatedBE = IEEE754binary32Emulated<0, 1, 2, 3>;
|
||||
using IEEE754binary32EmulatedLE = IEEE754binary32Emulated<3, 2, 1, 0>;
|
||||
using IEEE754binary64EmulatedBE = IEEE754binary64Emulated<0, 1, 2, 3, 4, 5, 6, 7>;
|
||||
using IEEE754binary64EmulatedLE = IEEE754binary64Emulated<7, 6, 5, 4, 3, 2, 1, 0>;
|
||||
|
||||
constexpr bool declare_binary_safe(const IEEE754binary32EmulatedBE &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const IEEE754binary32EmulatedLE &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const IEEE754binary64EmulatedBE &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const IEEE754binary64EmulatedLE &) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static_assert(mpt::check_binary_size<IEEE754binary32EmulatedBE>(4));
|
||||
static_assert(mpt::check_binary_size<IEEE754binary32EmulatedLE>(4));
|
||||
static_assert(mpt::check_binary_size<IEEE754binary64EmulatedBE>(8));
|
||||
static_assert(mpt::check_binary_size<IEEE754binary64EmulatedLE>(8));
|
||||
|
||||
template <mpt::endian endian = mpt::endian::native>
|
||||
struct IEEE754binary32Native {
|
||||
public:
|
||||
somefloat32 value;
|
||||
|
||||
public:
|
||||
MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
if constexpr (endian == mpt::endian::little) {
|
||||
return static_cast<std::byte>(EncodeIEEE754binary32(value) >> (i * 8));
|
||||
}
|
||||
if constexpr (endian == mpt::endian::big) {
|
||||
return static_cast<std::byte>(EncodeIEEE754binary32(value) >> ((4 - 1 - i) * 8));
|
||||
}
|
||||
}
|
||||
IEEE754binary32Native() = default;
|
||||
MPT_FORCEINLINE explicit IEEE754binary32Native(somefloat32 f) {
|
||||
value = f;
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary32Native & operator=(somefloat32 f) {
|
||||
value = f;
|
||||
return *this;
|
||||
}
|
||||
// b0...b3 are in memory order, i.e. depend on the endianness of this type
|
||||
// little endian: (0x00,0x00,0x80,0x3f)
|
||||
// big endian: (0x3f,0x80,0x00,0x00)
|
||||
MPT_FORCEINLINE explicit IEEE754binary32Native(std::byte b0, std::byte b1, std::byte b2, std::byte b3) {
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
if constexpr (endian == mpt::endian::little) {
|
||||
value = DecodeIEEE754binary32(0u | (static_cast<uint32>(b0) << 0) | (static_cast<uint32>(b1) << 8) | (static_cast<uint32>(b2) << 16) | (static_cast<uint32>(b3) << 24));
|
||||
}
|
||||
if constexpr (endian == mpt::endian::big) {
|
||||
value = DecodeIEEE754binary32(0u | (static_cast<uint32>(b0) << 24) | (static_cast<uint32>(b1) << 16) | (static_cast<uint32>(b2) << 8) | (static_cast<uint32>(b3) << 0));
|
||||
}
|
||||
}
|
||||
MPT_FORCEINLINE operator somefloat32() const {
|
||||
return value;
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary32Native & set(somefloat32 f) {
|
||||
value = f;
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE somefloat32 get() const {
|
||||
return value;
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary32Native & SetInt32(uint32 i) {
|
||||
value = DecodeIEEE754binary32(i);
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE uint32 GetInt32() const {
|
||||
return EncodeIEEE754binary32(value);
|
||||
}
|
||||
MPT_FORCEINLINE bool operator==(const IEEE754binary32Native & cmp) const {
|
||||
return value == cmp.value;
|
||||
}
|
||||
MPT_FORCEINLINE bool operator!=(const IEEE754binary32Native & cmp) const {
|
||||
return value != cmp.value;
|
||||
}
|
||||
};
|
||||
|
||||
template <mpt::endian endian = mpt::endian::native>
|
||||
struct IEEE754binary64Native {
|
||||
public:
|
||||
somefloat64 value;
|
||||
|
||||
public:
|
||||
MPT_FORCEINLINE std::byte GetByte(std::size_t i) const {
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
if constexpr (endian == mpt::endian::little) {
|
||||
return mpt::byte_cast<std::byte>(static_cast<uint8>(EncodeIEEE754binary64(value) >> (i * 8)));
|
||||
}
|
||||
if constexpr (endian == mpt::endian::big) {
|
||||
return mpt::byte_cast<std::byte>(static_cast<uint8>(EncodeIEEE754binary64(value) >> ((8 - 1 - i) * 8)));
|
||||
}
|
||||
}
|
||||
IEEE754binary64Native() = default;
|
||||
MPT_FORCEINLINE explicit IEEE754binary64Native(somefloat64 f) {
|
||||
value = f;
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary64Native & operator=(somefloat64 f) {
|
||||
value = f;
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE explicit IEEE754binary64Native(std::byte b0, std::byte b1, std::byte b2, std::byte b3, std::byte b4, std::byte b5, std::byte b6, std::byte b7) {
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
if constexpr (endian == mpt::endian::little) {
|
||||
value = DecodeIEEE754binary64(0ull | (static_cast<uint64>(b0) << 0) | (static_cast<uint64>(b1) << 8) | (static_cast<uint64>(b2) << 16) | (static_cast<uint64>(b3) << 24) | (static_cast<uint64>(b4) << 32) | (static_cast<uint64>(b5) << 40) | (static_cast<uint64>(b6) << 48) | (static_cast<uint64>(b7) << 56));
|
||||
}
|
||||
if constexpr (endian == mpt::endian::big) {
|
||||
value = DecodeIEEE754binary64(0ull | (static_cast<uint64>(b0) << 56) | (static_cast<uint64>(b1) << 48) | (static_cast<uint64>(b2) << 40) | (static_cast<uint64>(b3) << 32) | (static_cast<uint64>(b4) << 24) | (static_cast<uint64>(b5) << 16) | (static_cast<uint64>(b6) << 8) | (static_cast<uint64>(b7) << 0));
|
||||
}
|
||||
}
|
||||
MPT_FORCEINLINE operator somefloat64() const {
|
||||
return value;
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary64Native & set(somefloat64 f) {
|
||||
value = f;
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE somefloat64 get() const {
|
||||
return value;
|
||||
}
|
||||
MPT_FORCEINLINE IEEE754binary64Native & SetInt64(uint64 i) {
|
||||
value = DecodeIEEE754binary64(i);
|
||||
return *this;
|
||||
}
|
||||
MPT_FORCEINLINE uint64 GetInt64() const {
|
||||
return EncodeIEEE754binary64(value);
|
||||
}
|
||||
MPT_FORCEINLINE bool operator==(const IEEE754binary64Native & cmp) const {
|
||||
return value == cmp.value;
|
||||
}
|
||||
MPT_FORCEINLINE bool operator!=(const IEEE754binary64Native & cmp) const {
|
||||
return value != cmp.value;
|
||||
}
|
||||
};
|
||||
|
||||
static_assert((sizeof(IEEE754binary32Native<>) == 4));
|
||||
static_assert((sizeof(IEEE754binary64Native<>) == 8));
|
||||
|
||||
constexpr bool declare_binary_safe(const IEEE754binary32Native<> &) noexcept {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const IEEE754binary64Native<> &) noexcept {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <bool is_ieee754, mpt::endian endian = mpt::endian::native>
|
||||
struct IEEE754binary_types {
|
||||
using IEEE754binary32LE = IEEE754binary32EmulatedLE;
|
||||
using IEEE754binary32BE = IEEE754binary32EmulatedBE;
|
||||
using IEEE754binary64LE = IEEE754binary64EmulatedLE;
|
||||
using IEEE754binary64BE = IEEE754binary64EmulatedBE;
|
||||
};
|
||||
template <>
|
||||
struct IEEE754binary_types<true, mpt::endian::little> {
|
||||
using IEEE754binary32LE = IEEE754binary32Native<>;
|
||||
using IEEE754binary32BE = IEEE754binary32EmulatedBE;
|
||||
using IEEE754binary64LE = IEEE754binary64Native<>;
|
||||
using IEEE754binary64BE = IEEE754binary64EmulatedBE;
|
||||
};
|
||||
template <>
|
||||
struct IEEE754binary_types<true, mpt::endian::big> {
|
||||
using IEEE754binary32LE = IEEE754binary32EmulatedLE;
|
||||
using IEEE754binary32BE = IEEE754binary32Native<>;
|
||||
using IEEE754binary64LE = IEEE754binary64EmulatedLE;
|
||||
using IEEE754binary64BE = IEEE754binary64Native<>;
|
||||
};
|
||||
|
||||
using IEEE754binary32LE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary32LE;
|
||||
using IEEE754binary32BE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float32 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary32BE;
|
||||
using IEEE754binary64LE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float64 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary64LE;
|
||||
using IEEE754binary64BE = IEEE754binary_types<mpt::float_traits<somefloat32>::is_float64 && mpt::float_traits<somefloat32>::is_ieee754_binary && mpt::float_traits<somefloat32>::is_native_endian, mpt::endian::native>::IEEE754binary64BE;
|
||||
|
||||
static_assert(sizeof(IEEE754binary32LE) == 4);
|
||||
static_assert(sizeof(IEEE754binary32BE) == 4);
|
||||
static_assert(sizeof(IEEE754binary64LE) == 8);
|
||||
static_assert(sizeof(IEEE754binary64BE) == 8);
|
||||
|
||||
|
||||
// unaligned
|
||||
|
||||
using float32le = IEEE754binary32EmulatedLE;
|
||||
using float32be = IEEE754binary32EmulatedBE;
|
||||
using float64le = IEEE754binary64EmulatedLE;
|
||||
using float64be = IEEE754binary64EmulatedBE;
|
||||
|
||||
static_assert(sizeof(float32le) == 4);
|
||||
static_assert(sizeof(float32be) == 4);
|
||||
static_assert(sizeof(float64le) == 8);
|
||||
static_assert(sizeof(float64be) == 8);
|
||||
|
||||
|
||||
// potentially aligned
|
||||
|
||||
using float32le_fast = IEEE754binary32LE;
|
||||
using float32be_fast = IEEE754binary32BE;
|
||||
using float64le_fast = IEEE754binary64LE;
|
||||
using float64be_fast = IEEE754binary64BE;
|
||||
|
||||
static_assert(sizeof(float32le_fast) == 4);
|
||||
static_assert(sizeof(float32be_fast) == 4);
|
||||
static_assert(sizeof(float64le_fast) == 8);
|
||||
static_assert(sizeof(float64be_fast) == 8);
|
||||
|
||||
|
||||
|
||||
template <>
|
||||
struct make_endian<mpt::endian::little, float> {
|
||||
using type = IEEE754binary32LE;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct make_endian<mpt::endian::big, float> {
|
||||
using type = IEEE754binary32BE;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct make_endian<mpt::endian::little, double> {
|
||||
using type = IEEE754binary64LE;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct make_endian<mpt::endian::big, double> {
|
||||
using type = IEEE754binary64BE;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_FLOATINGPOINT_HPP
|
||||
@@ -0,0 +1,246 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_INT24_HPP
|
||||
#define MPT_ENDIAN_INT24_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/endian/integer.hpp"
|
||||
#include "mpt/endian/type_traits.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
struct uint24 {
|
||||
std::array<std::byte, 3> bytes;
|
||||
uint24() = default;
|
||||
template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
|
||||
explicit uint24(T other) noexcept {
|
||||
using Tunsigned = typename std::make_unsigned<T>::type;
|
||||
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) {
|
||||
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff));
|
||||
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff));
|
||||
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff));
|
||||
} else {
|
||||
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff));
|
||||
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff));
|
||||
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff));
|
||||
}
|
||||
}
|
||||
operator unsigned int() const noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) {
|
||||
return (mpt::byte_cast<uint8>(bytes[0]) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[2]);
|
||||
} else {
|
||||
return (mpt::byte_cast<uint8>(bytes[2]) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[0]);
|
||||
}
|
||||
}
|
||||
friend bool operator==(uint24 a, uint24 b) noexcept {
|
||||
return static_cast<unsigned int>(a) == static_cast<unsigned int>(b);
|
||||
}
|
||||
friend bool operator!=(uint24 a, uint24 b) noexcept {
|
||||
return static_cast<unsigned int>(a) != static_cast<unsigned int>(b);
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(uint24) == 3);
|
||||
|
||||
|
||||
struct int24 {
|
||||
std::array<std::byte, 3> bytes;
|
||||
int24() = default;
|
||||
template <typename T, typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
|
||||
explicit int24(T other) noexcept {
|
||||
using Tunsigned = typename std::make_unsigned<T>::type;
|
||||
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) {
|
||||
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff));
|
||||
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff));
|
||||
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff));
|
||||
} else {
|
||||
bytes[0] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 0) & 0xff));
|
||||
bytes[1] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 8) & 0xff));
|
||||
bytes[2] = mpt::byte_cast<std::byte>(static_cast<uint8>((static_cast<Tunsigned>(other) >> 16) & 0xff));
|
||||
}
|
||||
}
|
||||
operator int() const noexcept {
|
||||
MPT_MAYBE_CONSTANT_IF (mpt::endian_is_big()) {
|
||||
return (static_cast<int8>(mpt::byte_cast<uint8>(bytes[0])) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[2]);
|
||||
} else {
|
||||
return (static_cast<int8>(mpt::byte_cast<uint8>(bytes[2])) * 65536) + (mpt::byte_cast<uint8>(bytes[1]) * 256) + mpt::byte_cast<uint8>(bytes[0]);
|
||||
}
|
||||
}
|
||||
friend bool operator==(int24 a, int24 b) noexcept {
|
||||
return static_cast<int>(a) == static_cast<int>(b);
|
||||
}
|
||||
friend bool operator!=(int24 a, int24 b) noexcept {
|
||||
return static_cast<int>(a) != static_cast<int>(b);
|
||||
}
|
||||
};
|
||||
|
||||
static_assert(sizeof(int24) == 3);
|
||||
|
||||
|
||||
|
||||
template <>
|
||||
struct packed_int_type<int24> {
|
||||
using type = int32;
|
||||
};
|
||||
template <>
|
||||
struct packed_int_type<uint24> {
|
||||
using type = uint32;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename base_type, mpt::endian endian, typename int_type = base_type>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, sizeof(base_type)> EndianEncode24(base_type val) noexcept {
|
||||
static_assert(std::is_same<base_type, int24>::value || std::is_same<base_type, uint24>::value);
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
static_assert(std::numeric_limits<int_type>::is_integer);
|
||||
if constexpr (endian == mpt::endian::native) {
|
||||
std::array<std::byte, sizeof(base_type)> data{};
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
data[i] = val.bytes[i];
|
||||
}
|
||||
return data;
|
||||
} else {
|
||||
return EndianEncodeImpl<base_type, endian, int_type>(val);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename base_type, mpt::endian endian, typename int_type = base_type>
|
||||
MPT_CONSTEXPRINLINE base_type EndianDecode24(std::array<std::byte, sizeof(base_type)> data) noexcept {
|
||||
static_assert(std::is_same<base_type, int24>::value || std::is_same<base_type, uint24>::value);
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
static_assert(std::numeric_limits<int_type>::is_integer);
|
||||
if constexpr (endian == mpt::endian::native) {
|
||||
base_type val = base_type();
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
val.bytes[i] = data[i];
|
||||
}
|
||||
return val;
|
||||
} else {
|
||||
return EndianDecodeImpl<base_type, endian, int_type>(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<int24, mpt::endian::little, int32>(int24 val) noexcept {
|
||||
return mpt::EndianEncode24<int24, mpt::endian::little, int32>(val);
|
||||
}
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE int24 EndianDecode<int24, mpt::endian::little, int32>(std::array<std::byte, 3> data) noexcept {
|
||||
return mpt::EndianDecode24<int24, mpt::endian::little, int32>(data);
|
||||
}
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<int24, mpt::endian::big, int32>(int24 val) noexcept {
|
||||
return mpt::EndianEncode24<int24, mpt::endian::big, int32>(val);
|
||||
}
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE int24 EndianDecode<int24, mpt::endian::big, int32>(std::array<std::byte, 3> data) noexcept {
|
||||
return mpt::EndianDecode24<int24, mpt::endian::big, int32>(data);
|
||||
}
|
||||
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<uint24, mpt::endian::little, uint32>(uint24 val) noexcept {
|
||||
return mpt::EndianEncode24<uint24, mpt::endian::little, uint32>(val);
|
||||
}
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE uint24 EndianDecode<uint24, mpt::endian::little, uint32>(std::array<std::byte, 3> data) noexcept {
|
||||
return mpt::EndianDecode24<uint24, mpt::endian::little, uint32>(data);
|
||||
}
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, 3> EndianEncode<uint24, mpt::endian::big, uint32>(uint24 val) noexcept {
|
||||
return mpt::EndianEncode24<uint24, mpt::endian::big, uint32>(val);
|
||||
}
|
||||
template <>
|
||||
MPT_CONSTEXPRINLINE uint24 EndianDecode<uint24, mpt::endian::big, uint32>(std::array<std::byte, 3> data) noexcept {
|
||||
return mpt::EndianDecode24<uint24, mpt::endian::big, uint32>(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
using int24le = packed<int24, mpt::endian::little>;
|
||||
using uint24le = packed<uint24, mpt::endian::little>;
|
||||
|
||||
using int24be = packed<int24, mpt::endian::big>;
|
||||
using uint24be = packed<uint24, mpt::endian::big>;
|
||||
|
||||
constexpr bool declare_binary_safe(const int24le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint24le &) {
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool declare_binary_safe(const int24be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint24be &) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static_assert(mpt::check_binary_size<int24le>(3));
|
||||
static_assert(mpt::check_binary_size<uint24le>(3));
|
||||
|
||||
static_assert(mpt::check_binary_size<int24be>(3));
|
||||
static_assert(mpt::check_binary_size<uint24be>(3));
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#if !defined(CPPCHECK)
|
||||
// work-around crash in cppcheck 2.4.1
|
||||
namespace std {
|
||||
template <>
|
||||
class numeric_limits<mpt::uint24> : public std::numeric_limits<mpt::uint32> {
|
||||
public:
|
||||
static constexpr mpt::uint32 min() noexcept {
|
||||
return 0;
|
||||
}
|
||||
static constexpr mpt::uint32 lowest() noexcept {
|
||||
return 0;
|
||||
}
|
||||
static constexpr mpt::uint32 max() noexcept {
|
||||
return 0x00ffffff;
|
||||
}
|
||||
};
|
||||
template <>
|
||||
class numeric_limits<mpt::int24> : public std::numeric_limits<mpt::int32> {
|
||||
public:
|
||||
static constexpr mpt::int32 min() noexcept {
|
||||
return 0 - 0x00800000;
|
||||
}
|
||||
static constexpr mpt::int32 lowest() noexcept {
|
||||
return 0 - 0x00800000;
|
||||
}
|
||||
static constexpr mpt::int32 max() noexcept {
|
||||
return 0 + 0x007fffff;
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
#endif // !CPPCHECK
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_INT24_HPP
|
||||
@@ -0,0 +1,356 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_INTEGER_HPP
|
||||
#define MPT_ENDIAN_INTEGER_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/endian/type_traits.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename base_type, mpt::endian endian, typename int_type = base_type>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, sizeof(base_type)> EndianEncodeImpl(base_type val) noexcept {
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
static_assert(std::numeric_limits<int_type>::is_integer);
|
||||
using unsigned_int_type = typename std::make_unsigned<int_type>::type;
|
||||
unsigned_int_type uval = static_cast<unsigned_int_type>(static_cast<int_type>(val));
|
||||
std::array<std::byte, sizeof(base_type)> data{};
|
||||
if constexpr (endian == mpt::endian::little) {
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
data[i] = static_cast<std::byte>(static_cast<uint8>((uval >> (i * 8)) & 0xffu));
|
||||
}
|
||||
} else {
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
data[(sizeof(base_type) - 1) - i] = static_cast<std::byte>(static_cast<uint8>((uval >> (i * 8)) & 0xffu));
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
template <typename base_type, mpt::endian endian, typename int_type = base_type>
|
||||
MPT_CONSTEXPRINLINE std::array<std::byte, sizeof(base_type)> EndianEncode(base_type val) noexcept {
|
||||
return EndianEncodeImpl<base_type, endian, int_type>(val);
|
||||
}
|
||||
|
||||
template <typename base_type, mpt::endian endian, typename int_type = base_type>
|
||||
MPT_CONSTEXPRINLINE base_type EndianDecodeImpl(std::array<std::byte, sizeof(base_type)> data) noexcept {
|
||||
static_assert(endian == mpt::endian::little || endian == mpt::endian::big);
|
||||
static_assert(std::numeric_limits<int_type>::is_integer);
|
||||
using unsigned_int_type = typename std::make_unsigned<int_type>::type;
|
||||
base_type val = base_type();
|
||||
unsigned_int_type uval = unsigned_int_type();
|
||||
if constexpr (endian == mpt::endian::little) {
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
uval |= static_cast<unsigned_int_type>(static_cast<unsigned_int_type>(static_cast<uint8>(data[i])) << (i * 8));
|
||||
}
|
||||
} else {
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
uval |= static_cast<unsigned_int_type>(static_cast<unsigned_int_type>(static_cast<uint8>(data[(sizeof(base_type) - 1) - i])) << (i * 8));
|
||||
}
|
||||
}
|
||||
val = static_cast<base_type>(static_cast<int_type>(uval));
|
||||
return val;
|
||||
}
|
||||
|
||||
template <typename base_type, mpt::endian endian, typename int_type = base_type>
|
||||
MPT_CONSTEXPRINLINE base_type EndianDecode(std::array<std::byte, sizeof(base_type)> data) noexcept {
|
||||
return EndianDecodeImpl<base_type, endian, int_type>(data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct packed_int_type {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
// On-disk integer types with defined endianness and no alignemnt requirements
|
||||
// Note: To easily debug module loaders (and anything else that uses this
|
||||
// wrapper struct), you can use the Debugger Visualizers available in
|
||||
// build/vs/debug/ to conveniently view the wrapped contents.
|
||||
|
||||
template <typename T, mpt::endian endian, typename Tint = typename mpt::packed_int_type<T>::type>
|
||||
struct packed {
|
||||
public:
|
||||
using base_type = T;
|
||||
using int_type = Tint;
|
||||
|
||||
public:
|
||||
std::array<std::byte, sizeof(base_type)> data;
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPR20_FUN void set(base_type val) noexcept {
|
||||
static_assert(std::numeric_limits<int_type>::is_integer);
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
if constexpr (endian == mpt::endian::big) {
|
||||
typename std::make_unsigned<int_type>::type uval = val;
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
data[i] = static_cast<std::byte>((uval >> (8 * (sizeof(base_type) - 1 - i))) & 0xffu);
|
||||
}
|
||||
} else {
|
||||
typename std::make_unsigned<int_type>::type uval = val;
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
data[i] = static_cast<std::byte>((uval >> (8 * i)) & 0xffu);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if constexpr (std::is_integral<base_type>::value && (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big)) {
|
||||
if constexpr (mpt::endian::native != endian) {
|
||||
val = mpt::byteswap(val);
|
||||
}
|
||||
std::memcpy(data.data(), &val, sizeof(val));
|
||||
} else {
|
||||
data = EndianEncode<base_type, endian, int_type>(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN base_type get() const noexcept {
|
||||
static_assert(std::numeric_limits<int_type>::is_integer);
|
||||
MPT_MAYBE_CONSTANT_IF (MPT_IS_CONSTANT_EVALUATED20()) {
|
||||
if constexpr (endian == mpt::endian::big) {
|
||||
typename std::make_unsigned<int_type>::type uval = 0;
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
uval |= static_cast<typename std::make_unsigned<int_type>::type>(static_cast<typename std::make_unsigned<int_type>::type>(data[i]) << (8 * (sizeof(base_type) - 1 - i)));
|
||||
}
|
||||
return static_cast<base_type>(uval);
|
||||
} else {
|
||||
typename std::make_unsigned<int_type>::type uval = 0;
|
||||
for (std::size_t i = 0; i < sizeof(base_type); ++i) {
|
||||
uval |= static_cast<typename std::make_unsigned<int_type>::type>(static_cast<typename std::make_unsigned<int_type>::type>(data[i]) << (8 * i));
|
||||
}
|
||||
return static_cast<base_type>(uval);
|
||||
}
|
||||
} else {
|
||||
if constexpr (std::is_integral<base_type>::value && (mpt::endian::native == mpt::endian::little || mpt::endian::native == mpt::endian::big)) {
|
||||
base_type val = base_type();
|
||||
std::memcpy(&val, data.data(), sizeof(val));
|
||||
if constexpr (mpt::endian::native != endian) {
|
||||
val = mpt::byteswap(val);
|
||||
}
|
||||
return val;
|
||||
} else {
|
||||
return EndianDecode<base_type, endian, int_type>(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator=(const base_type & val) noexcept {
|
||||
set(val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN operator base_type() const noexcept {
|
||||
return get();
|
||||
}
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPR20_FUN packed & operator&=(base_type val) noexcept {
|
||||
set(get() & val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator|=(base_type val) noexcept {
|
||||
set(get() | val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator^=(base_type val) noexcept {
|
||||
set(get() ^ val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator+=(base_type val) noexcept {
|
||||
set(get() + val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator-=(base_type val) noexcept {
|
||||
set(get() - val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator*=(base_type val) noexcept {
|
||||
set(get() * val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator/=(base_type val) noexcept {
|
||||
set(get() / val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator%=(base_type val) noexcept {
|
||||
set(get() % val);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator++() noexcept { // prefix
|
||||
set(get() + 1);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN packed & operator--() noexcept { // prefix
|
||||
set(get() - 1);
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN base_type operator++(int) noexcept { // postfix
|
||||
base_type old = get();
|
||||
set(old + 1);
|
||||
return old;
|
||||
}
|
||||
MPT_CONSTEXPR20_FUN base_type operator--(int) noexcept { // postfix
|
||||
base_type old = get();
|
||||
set(old - 1);
|
||||
return old;
|
||||
}
|
||||
};
|
||||
|
||||
using int64le = packed<int64, mpt::endian::little>;
|
||||
using int32le = packed<int32, mpt::endian::little>;
|
||||
using int16le = packed<int16, mpt::endian::little>;
|
||||
using int8le = packed<int8, mpt::endian::little>;
|
||||
using uint64le = packed<uint64, mpt::endian::little>;
|
||||
using uint32le = packed<uint32, mpt::endian::little>;
|
||||
using uint16le = packed<uint16, mpt::endian::little>;
|
||||
using uint8le = packed<uint8, mpt::endian::little>;
|
||||
|
||||
using int64be = packed<int64, mpt::endian::big>;
|
||||
using int32be = packed<int32, mpt::endian::big>;
|
||||
using int16be = packed<int16, mpt::endian::big>;
|
||||
using int8be = packed<int8, mpt::endian::big>;
|
||||
using uint64be = packed<uint64, mpt::endian::big>;
|
||||
using uint32be = packed<uint32, mpt::endian::big>;
|
||||
using uint16be = packed<uint16, mpt::endian::big>;
|
||||
using uint8be = packed<uint8, mpt::endian::big>;
|
||||
|
||||
constexpr bool declare_binary_safe(const int64le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int32le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int16le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int8le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint64le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint32le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint16le &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint8le &) {
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool declare_binary_safe(const int64be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int32be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int16be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const int8be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint64be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint32be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint16be &) {
|
||||
return true;
|
||||
}
|
||||
constexpr bool declare_binary_safe(const uint8be &) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static_assert(mpt::check_binary_size<int64le>(8));
|
||||
static_assert(mpt::check_binary_size<int32le>(4));
|
||||
static_assert(mpt::check_binary_size<int16le>(2));
|
||||
static_assert(mpt::check_binary_size<int8le>(1));
|
||||
static_assert(mpt::check_binary_size<uint64le>(8));
|
||||
static_assert(mpt::check_binary_size<uint32le>(4));
|
||||
static_assert(mpt::check_binary_size<uint16le>(2));
|
||||
static_assert(mpt::check_binary_size<uint8le>(1));
|
||||
|
||||
static_assert(mpt::check_binary_size<int64be>(8));
|
||||
static_assert(mpt::check_binary_size<int32be>(4));
|
||||
static_assert(mpt::check_binary_size<int16be>(2));
|
||||
static_assert(mpt::check_binary_size<int8be>(1));
|
||||
static_assert(mpt::check_binary_size<uint64be>(8));
|
||||
static_assert(mpt::check_binary_size<uint32be>(4));
|
||||
static_assert(mpt::check_binary_size<uint16be>(2));
|
||||
static_assert(mpt::check_binary_size<uint8be>(1));
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
struct make_endian<mpt::endian::little, T> {
|
||||
using type = packed<typename std::remove_const<T>::type, mpt::endian::little>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct make_endian<mpt::endian::big, T> {
|
||||
using type = packed<typename std::remove_const<T>::type, mpt::endian::big>;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
MPT_CONSTEXPR20_FUN auto as_le(T v) noexcept -> typename mpt::make_le<typename std::remove_const<T>::type>::type {
|
||||
typename mpt::make_le<typename std::remove_const<T>::type>::type res{};
|
||||
res = v;
|
||||
return res;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
MPT_CONSTEXPR20_FUN auto as_be(T v) noexcept -> typename mpt::make_be<typename std::remove_const<T>::type>::type {
|
||||
typename mpt::make_be<typename std::remove_const<T>::type>::type res{};
|
||||
res = v;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename Tpacked>
|
||||
MPT_CONSTEXPR20_FUN Tpacked as_endian(typename Tpacked::base_type v) noexcept {
|
||||
Tpacked res{};
|
||||
res = v;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
namespace std {
|
||||
template <typename T, mpt::endian endian>
|
||||
class numeric_limits<mpt::packed<T, endian>> : public std::numeric_limits<T> { };
|
||||
template <typename T, mpt::endian endian>
|
||||
class numeric_limits<const mpt::packed<T, endian>> : public std::numeric_limits<const T> { };
|
||||
} // namespace std
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_INTEGER_HPP
|
||||
@@ -0,0 +1,81 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_TESTS_ENDIAN_FLOATINGPOINT_HPP
|
||||
#define MPT_ENDIAN_TESTS_ENDIAN_FLOATINGPOINT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/endian/floatingpoint.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace endian {
|
||||
namespace floatingpoint {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/endian/floatingpoint")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary32(1.0f), 0x3f800000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary32(-1.0f), 0xbf800000u);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x00000000u), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x41840000u), 16.5f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x3faa0000u), 1.328125f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0xbfaa0000u), -1.328125f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x3f800000u), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x00000000u), 0.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0xbf800000u), -1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary32(0x3f800000u), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary32LE(1.0f).GetInt32(), 0x3f800000u);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary32BE(1.0f).GetInt32(), 0x3f800000u);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary32LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x80), mpt::as_byte(0x3f)), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary32BE(mpt::as_byte(0x3f), mpt::as_byte(0x80), mpt::as_byte(0x00), mpt::as_byte(0x00)), 1.0f);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary32LE(1.0f), IEEE754binary32LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x80), mpt::as_byte(0x3f)));
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary32BE(1.0f), IEEE754binary32BE(mpt::as_byte(0x3f), mpt::as_byte(0x80), mpt::as_byte(0x00), mpt::as_byte(0x00)));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary64(1.0), 0x3ff0000000000000ull);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::EncodeIEEE754binary64(-1.0), 0xbff0000000000000ull);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x0000000000000000ull), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x4030800000000000ull), 16.5);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x3FF5400000000000ull), 1.328125);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0xBFF5400000000000ull), -1.328125);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x3ff0000000000000ull), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x0000000000000000ull), 0.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0xbff0000000000000ull), -1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::DecodeIEEE754binary64(0x3ff0000000000000ull), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary64LE(1.0).GetInt64(), 0x3ff0000000000000ull);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary64BE(1.0).GetInt64(), 0x3ff0000000000000ull);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary64LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0xf0), mpt::as_byte(0x3f)), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary64BE(mpt::as_byte(0x3f), mpt::as_byte(0xf0), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00)), 1.0);
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary64LE(1.0), IEEE754binary64LE(mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0xf0), mpt::as_byte(0x3f)));
|
||||
MPT_TEST_EXPECT_EQUAL(IEEE754binary64BE(1.0), IEEE754binary64BE(mpt::as_byte(0x3f), mpt::as_byte(0xf0), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00), mpt::as_byte(0x00)));
|
||||
}
|
||||
|
||||
} // namespace floatingpoint
|
||||
} // namespace endian
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_TESTS_ENDIAN_FLOATINGPOINT_HPP
|
||||
@@ -0,0 +1,148 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP
|
||||
#define MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/endian/int24.hpp"
|
||||
#include "mpt/endian/integer.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace endian {
|
||||
namespace integer {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/endian/int24")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
static_assert(std::numeric_limits<int24le>::min() == std::numeric_limits<int24>::min());
|
||||
static_assert(std::numeric_limits<uint24le>::min() == std::numeric_limits<uint24>::min());
|
||||
|
||||
{
|
||||
int24le le24;
|
||||
le24.set(int24(0x123456));
|
||||
int24be be24;
|
||||
be24.set(int24(0x123456));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, int24(0x123456));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, int24(0x123456));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x56\x34\x12", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x12\x34\x56", 3), 0);
|
||||
}
|
||||
{
|
||||
int24le le24;
|
||||
le24.set(int24(-0x7fffff));
|
||||
int24be be24;
|
||||
be24.set(int24(-0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x01\x00\x80", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x01", 3), 0);
|
||||
}
|
||||
{
|
||||
int24le le24;
|
||||
le24.set(int24(-0x000001));
|
||||
int24be be24;
|
||||
be24.set(int24(-0x000001));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x000001));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x000001));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\xff", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\xff\xff\xff", 3), 0);
|
||||
}
|
||||
{
|
||||
int24le le24;
|
||||
le24.set(int24(0x7fffff));
|
||||
int24be be24;
|
||||
be24.set(int24(0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, int24(0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, int24(0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\x7f", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x7f\xff\xff", 3), 0);
|
||||
}
|
||||
{
|
||||
int24le le24;
|
||||
le24.set(int24(-0x800000));
|
||||
int24be be24;
|
||||
be24.set(int24(-0x800000));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, int24(-0x800000));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, int24(-0x800000));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x00\x00\x80", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x00", 3), 0);
|
||||
}
|
||||
|
||||
{
|
||||
uint24le le24;
|
||||
le24.set(uint24(0x123456));
|
||||
uint24be be24;
|
||||
be24.set(uint24(0x123456));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x123456));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x123456));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x56\x34\x12", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x12\x34\x56", 3), 0);
|
||||
}
|
||||
{
|
||||
uint24le le24;
|
||||
le24.set(uint24(0xffffff));
|
||||
uint24be be24;
|
||||
be24.set(uint24(0xffffff));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, uint24(0xffffff));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, uint24(0xffffff));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\xff", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\xff\xff\xff", 3), 0);
|
||||
}
|
||||
{
|
||||
uint24le le24;
|
||||
le24.set(uint24(0x7fffff));
|
||||
uint24be be24;
|
||||
be24.set(uint24(0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x7fffff));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\xff\xff\x7f", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x7f\xff\xff", 3), 0);
|
||||
}
|
||||
{
|
||||
uint24le le24;
|
||||
le24.set(uint24(0x800000));
|
||||
uint24be be24;
|
||||
be24.set(uint24(0x800000));
|
||||
MPT_TEST_EXPECT_EQUAL(le24, uint24(0x800000));
|
||||
MPT_TEST_EXPECT_EQUAL(be24, uint24(0x800000));
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le24, "\x00\x00\x80", 3), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be24, "\x80\x00\x00", 3), 0);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace integer
|
||||
} // namespace endian
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_TESTS_ENDIAN_INT24_HPP
|
||||
@@ -0,0 +1,123 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP
|
||||
#define MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect_compiler.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/endian/integer.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace endian {
|
||||
namespace integer {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/endian/integer")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
static_assert(std::numeric_limits<int8le>::min() == std::numeric_limits<int8>::min());
|
||||
static_assert(std::numeric_limits<uint8le>::min() == std::numeric_limits<uint8>::min());
|
||||
|
||||
static_assert(std::numeric_limits<int16le>::min() == std::numeric_limits<int16>::min());
|
||||
static_assert(std::numeric_limits<uint16le>::min() == std::numeric_limits<uint16>::min());
|
||||
|
||||
static_assert(std::numeric_limits<int32le>::min() == std::numeric_limits<int32>::min());
|
||||
static_assert(std::numeric_limits<uint32le>::min() == std::numeric_limits<uint32>::min());
|
||||
|
||||
static_assert(std::numeric_limits<int64le>::min() == std::numeric_limits<int64>::min());
|
||||
static_assert(std::numeric_limits<uint64le>::min() == std::numeric_limits<uint64>::min());
|
||||
|
||||
static_assert(std::numeric_limits<int8le>::max() == std::numeric_limits<int8>::max());
|
||||
static_assert(std::numeric_limits<uint8le>::max() == std::numeric_limits<uint8>::max());
|
||||
|
||||
static_assert(std::numeric_limits<int16le>::max() == std::numeric_limits<int16>::max());
|
||||
static_assert(std::numeric_limits<uint16le>::max() == std::numeric_limits<uint16>::max());
|
||||
|
||||
static_assert(std::numeric_limits<int32le>::max() == std::numeric_limits<int32>::max());
|
||||
static_assert(std::numeric_limits<uint32le>::max() == std::numeric_limits<uint32>::max());
|
||||
|
||||
static_assert(std::numeric_limits<int64le>::max() == std::numeric_limits<int64>::max());
|
||||
static_assert(std::numeric_limits<uint64le>::max() == std::numeric_limits<uint64>::max());
|
||||
|
||||
struct test_endian_constexpr {
|
||||
static MPT_CONSTEXPR20_FUN int32le test(uint32 x) {
|
||||
int32le foo{};
|
||||
foo = x;
|
||||
return foo;
|
||||
}
|
||||
};
|
||||
|
||||
MPT_CONSTEXPR20_VAR int32le foo = test_endian_constexpr::test(23);
|
||||
static_cast<void>(foo);
|
||||
|
||||
// Packed integers with defined endianness
|
||||
{
|
||||
int8le le8;
|
||||
le8.set(-128);
|
||||
int8be be8;
|
||||
be8.set(-128);
|
||||
MPT_TEST_EXPECT_EQUAL(le8, -128);
|
||||
MPT_TEST_EXPECT_EQUAL(be8, -128);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le8, "\x80", 1), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be8, "\x80", 1), 0);
|
||||
int16le le16;
|
||||
le16.set(0x1234);
|
||||
int16be be16;
|
||||
be16.set(0x1234);
|
||||
MPT_TEST_EXPECT_EQUAL(le16, 0x1234);
|
||||
MPT_TEST_EXPECT_EQUAL(be16, 0x1234);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le16, "\x34\x12", 2), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be16, "\x12\x34", 2), 0);
|
||||
uint32le le32;
|
||||
le32.set(0xFFEEDDCCu);
|
||||
uint32be be32;
|
||||
be32.set(0xFFEEDDCCu);
|
||||
MPT_TEST_EXPECT_EQUAL(le32, 0xFFEEDDCCu);
|
||||
MPT_TEST_EXPECT_EQUAL(be32, 0xFFEEDDCCu);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le32, "\xCC\xDD\xEE\xFF", 4), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be32, "\xFF\xEE\xDD\xCC", 4), 0);
|
||||
uint64le le64;
|
||||
le64.set(0xDEADC0DE15C0FFEEull);
|
||||
uint64be be64;
|
||||
be64.set(0xDEADC0DE15C0FFEEull);
|
||||
MPT_TEST_EXPECT_EQUAL(le64, 0xDEADC0DE15C0FFEEull);
|
||||
MPT_TEST_EXPECT_EQUAL(be64, 0xDEADC0DE15C0FFEEull);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&le64, "\xEE\xFF\xC0\x15\xDE\xC0\xAD\xDE", 8), 0);
|
||||
MPT_TEST_EXPECT_EQUAL(std::memcmp(&be64, "\xDE\xAD\xC0\xDE\x15\xC0\xFF\xEE", 8), 0);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace integer
|
||||
} // namespace endian
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_TESTS_ENDIAN_INTEGER_HPP
|
||||
@@ -0,0 +1,39 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENDIAN_TYPE_TRAITS_HPP
|
||||
#define MPT_ENDIAN_TYPE_TRAITS_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <mpt::endian endian, typename T>
|
||||
struct make_endian {
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct make_le {
|
||||
using type = typename mpt::make_endian<mpt::endian::little, T>::type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct make_be {
|
||||
using type = typename mpt::make_endian<mpt::endian::big, T>::type;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENDIAN_TYPE_TRAITS_HPP
|
||||
@@ -0,0 +1,61 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_ENVIRONMENT_ENVIRONMENT_HPP
|
||||
#define MPT_ENVIRONMENT_ENVIRONMENT_HPP
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
#include "mpt/system_error/system_error.hpp"
|
||||
|
||||
#include <optional>
|
||||
#if MPT_OS_WINDOWS
|
||||
#if defined(UNICODE) && !MPT_OS_WINDOWS_WINRT
|
||||
#include <vector>
|
||||
#endif // !MPT_OS_WINDOWS_WINRT
|
||||
#endif // MPT_OS_WINDOWS
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
#include <windows.h>
|
||||
#endif // MPT_OS_WINDOWS
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
inline std::optional<mpt::ustring> getenv(const mpt::ustring & env_var) {
|
||||
#if MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
|
||||
MPT_UNUSED(env_var);
|
||||
return std::nullopt;
|
||||
#elif MPT_OS_WINDOWS && defined(UNICODE)
|
||||
std::vector<WCHAR> buf(32767);
|
||||
DWORD size = GetEnvironmentVariable(mpt::transcode<std::wstring>(env_var).c_str(), buf.data(), 32767);
|
||||
if (size == 0) {
|
||||
mpt::windows::ExpectError(ERROR_ENVVAR_NOT_FOUND);
|
||||
return std::nullopt;
|
||||
}
|
||||
return mpt::transcode<mpt::ustring>(buf.data());
|
||||
#else
|
||||
const char * val = std::getenv(mpt::transcode<std::string>(mpt::environment_encoding, env_var).c_str());
|
||||
if (!val) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return mpt::transcode<mpt::ustring>(mpt::environment_encoding, val);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_ENVIRONMENT_ENVIRONMENT_HPP
|
||||
@@ -0,0 +1,51 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_EXCEPTION_EXCEPTION_HPP
|
||||
#define MPT_EXCEPTION_EXCEPTION_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
class exception_ustring_wrapper_base {
|
||||
protected:
|
||||
exception_ustring_wrapper_base() = default;
|
||||
virtual ~exception_ustring_wrapper_base() = default;
|
||||
public:
|
||||
virtual mpt::ustring uwhat() const = 0;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class exception_ustring_wrapper : public T
|
||||
, public virtual mpt::exception_ustring_wrapper_base {
|
||||
private:
|
||||
mpt::ustring m_what;
|
||||
public:
|
||||
exception_ustring_wrapper(mpt::ustring str)
|
||||
: T(mpt::transcode<std::string>(mpt::exception_encoding, str))
|
||||
, m_what(str) {
|
||||
return;
|
||||
}
|
||||
~exception_ustring_wrapper() override = default;
|
||||
mpt::ustring uwhat() const override {
|
||||
return m_what;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_EXCEPTION_EXCEPTION_HPP
|
||||
@@ -0,0 +1,87 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_EXCEPTION_EXCEPTION_TEXT_HPP
|
||||
#define MPT_EXCEPTION_EXCEPTION_TEXT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/exception/exception.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#include <exception>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
inline Tstring get_exception_text(const std::exception & e) {
|
||||
if (const mpt::exception_ustring_wrapper_base * pue = dynamic_cast<const mpt::exception_ustring_wrapper_base *>(&e)) {
|
||||
const mpt::exception_ustring_wrapper_base & ue = *pue;
|
||||
mpt::ustring what = ue.uwhat();
|
||||
if (what.length() > 0) {
|
||||
return mpt::transcode<Tstring>(std::move(what));
|
||||
} else if (typeid(ue).name() && (std::strlen(typeid(ue).name()) > 0)) {
|
||||
return mpt::transcode<Tstring>(mpt::source_string{typeid(ue).name()});
|
||||
} else {
|
||||
return mpt::transcode<Tstring>(mpt::source_string{"unknown exception name"});
|
||||
}
|
||||
} else if (e.what() && (std::strlen(e.what()) > 0)) {
|
||||
return mpt::transcode<Tstring>(mpt::exception_string{e.what()});
|
||||
} else if (typeid(e).name() && (std::strlen(typeid(e).name()) > 0)) {
|
||||
return mpt::transcode<Tstring>(mpt::source_string{typeid(e).name()});
|
||||
} else {
|
||||
return mpt::transcode<Tstring>(mpt::source_string{"unknown exception name"});
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
inline std::string get_exception_text<std::string>(const std::exception & e) {
|
||||
if (e.what() && (std::strlen(e.what()) > 0)) {
|
||||
return std::string{e.what()};
|
||||
} else if (typeid(e).name() && (std::strlen(typeid(e).name()) > 0)) {
|
||||
return std::string{typeid(e).name()};
|
||||
} else {
|
||||
return std::string{"unknown exception name"};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
inline Tstring get_current_exception_text() {
|
||||
try {
|
||||
throw;
|
||||
} catch (const std::exception & e) {
|
||||
return mpt::get_exception_text<Tstring>(e);
|
||||
} catch (...) {
|
||||
return mpt::transcode<Tstring>(mpt::source_string{"unknown exception"});
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
inline std::string get_current_exception_text<std::string>() {
|
||||
try {
|
||||
throw;
|
||||
} catch (const std::exception & e) {
|
||||
return mpt::get_exception_text<std::string>(e);
|
||||
} catch (...) {
|
||||
return std::string{"unknown exception"};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_EXCEPTION_EXCEPTION_TEXT_HPP
|
||||
@@ -0,0 +1,29 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_EXCEPTION_LOGIC_ERROR_HPP
|
||||
#define MPT_EXCEPTION_LOGIC_ERROR_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/exception/exception.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
using logic_error = mpt::exception_ustring_wrapper<std::logic_error>;
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_EXCEPTION_LOGIC_ERROR_HPP
|
||||
@@ -0,0 +1,29 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_EXCEPTION_RUNTIME_ERROR_HPP
|
||||
#define MPT_EXCEPTION_RUNTIME_ERROR_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/exception/exception.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
using runtime_error = mpt::exception_ustring_wrapper<std::runtime_error>;
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_EXCEPTION_RUNTIME_ERROR_HPP
|
||||
@@ -0,0 +1,32 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_CONCAT_HPP
|
||||
#define MPT_FORMAT_CONCAT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/default_formatter.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename Tstring, typename... Ts>
|
||||
inline Tstring concat(Ts &&... xs) {
|
||||
return (default_formatter::template format<Tstring>(std::forward<Ts>(xs)) + ...);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_CONCAT_HPP
|
||||
@@ -0,0 +1,91 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_DETAULT_FLOATINGPOINT_HPP
|
||||
#define MPT_FORMAT_DETAULT_FLOATINGPOINT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_FLOAT)
|
||||
#define MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 1
|
||||
#else
|
||||
#define MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17 0
|
||||
#endif
|
||||
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17
|
||||
#include "mpt/base/algorithm.hpp"
|
||||
#endif
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/helpers.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17
|
||||
#include <charconv>
|
||||
#endif
|
||||
#if !MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#include <limits>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#endif
|
||||
#include <string>
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17
|
||||
#include <system_error>
|
||||
#endif
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||
inline Tstring to_chars_string(const T & x) {
|
||||
std::string str(1, '\0');
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x);
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(str);
|
||||
}
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||
inline Tstring format_value_default(const T & x) {
|
||||
return mpt::transcode<Tstring>(mpt::to_chars_string<typename mpt::select_format_string_type<Tstring>::type>(x));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if !MPT_FORMAT_FORMAT_DEFAULT_FLOAT_CXX17
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||
inline Tstring to_stream_string(const T & x) {
|
||||
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type;
|
||||
std::basic_ostringstream<stream_char_type> s;
|
||||
s.imbue(std::locale::classic());
|
||||
s << std::setprecision(std::numeric_limits<T>::max_digits10) << x;
|
||||
return mpt::convert_formatted_simple<Tstring>(s.str());
|
||||
}
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||
inline Tstring format_value_default(const T & x) {
|
||||
return mpt::transcode<Tstring>(mpt::to_stream_string<typename mpt::select_format_string_type<Tstring>::type>(x));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_DETAULT_FLOATINGPOINT_HPP
|
||||
@@ -0,0 +1,35 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_DEFAULT_FORMATTER_HPP
|
||||
#define MPT_FORMAT_DEFAULT_FORMATTER_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/default_floatingpoint.hpp"
|
||||
#include "mpt/format/default_integer.hpp"
|
||||
#include "mpt/format/default_string.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
struct default_formatter {
|
||||
template <typename Tstring, typename T>
|
||||
static inline Tstring format(const T & value) {
|
||||
using namespace mpt;
|
||||
return format_value_default<Tstring>(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_DEFAULT_FORMATTER_HPP
|
||||
@@ -0,0 +1,114 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_DEFAULT_INTEGER_HPP
|
||||
#define MPT_FORMAT_DEFAULT_INTEGER_HPP
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT)
|
||||
#define MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 1
|
||||
#else // MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#define MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17 0
|
||||
#endif // !MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include "mpt/base/algorithm.hpp"
|
||||
#endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/utility.hpp"
|
||||
#include "mpt/format/helpers.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include <charconv>
|
||||
#endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#if !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#endif // !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include <string>
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include <system_error>
|
||||
#endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring to_chars_string(const T & x) {
|
||||
std::string str(1, '\0');
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
if constexpr (std::is_same<T, bool>::value) {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), static_cast<int>(x));
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
} else {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x);
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(str);
|
||||
}
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring format_value_default(const T & x) {
|
||||
return mpt::transcode<Tstring>(mpt::to_chars_string<typename mpt::select_format_string_type<Tstring>::type>(x));
|
||||
}
|
||||
|
||||
#endif // MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
|
||||
|
||||
#if !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring to_stream_string(const T & x) {
|
||||
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type;
|
||||
std::basic_ostringstream<stream_char_type> s;
|
||||
s.imbue(std::locale::classic());
|
||||
if constexpr (std::is_same<T, bool>::value) {
|
||||
s << static_cast<int>(x);
|
||||
} else if constexpr (mpt::is_character<T>::value) {
|
||||
s << (x + 0); // force integral promotion
|
||||
} else {
|
||||
s << x;
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(s.str());
|
||||
}
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring format_value_default(const T & x) {
|
||||
return mpt::transcode<Tstring>(mpt::to_stream_string<typename mpt::select_format_string_type<Tstring>::type>(x));
|
||||
}
|
||||
|
||||
#endif // !MPT_FORMAT_FORMAT_DEFAULT_INT_CXX17
|
||||
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_enum<T>::value, bool> = true>
|
||||
inline Tstring format_value_default(const T & x) {
|
||||
return mpt::format_value_default<Tstring>(mpt::to_underlying(x));
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_DEFAULT_INTEGER_HPP
|
||||
@@ -0,0 +1,28 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_DEFAULT_STRING_HPP
|
||||
#define MPT_FORMAT_DEFAULT_STRING_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
template <typename Tstring, typename T>
|
||||
inline auto format_value_default(const T & x) -> decltype(mpt::transcode<Tstring>(x)) {
|
||||
return mpt::transcode<Tstring>(x);
|
||||
}
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_DEFAULT_STRING_HPP
|
||||
@@ -0,0 +1,106 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_HELPERS_HPP
|
||||
#define MPT_FORMAT_HELPERS_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
template <typename Tdststring, typename Tsrcstring>
|
||||
inline Tdststring convert_formatted_simple(const Tsrcstring & src) {
|
||||
if constexpr (std::is_same<Tdststring, Tsrcstring>::value) {
|
||||
return src;
|
||||
} else {
|
||||
Tdststring dst;
|
||||
dst.reserve(src.length());
|
||||
for (std::size_t i = 0; i < src.length(); ++i) {
|
||||
dst.push_back(mpt::unsafe_char_convert<typename Tdststring::value_type>(src[i]));
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename Tchar>
|
||||
struct select_format_char_type {
|
||||
using type = char;
|
||||
};
|
||||
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
template <>
|
||||
struct select_format_char_type<wchar_t> {
|
||||
using type = wchar_t;
|
||||
};
|
||||
#if MPT_USTRING_MODE_WIDE
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
template <>
|
||||
struct select_format_char_type<char8_t> {
|
||||
using type = wchar_t;
|
||||
};
|
||||
#endif // C++20
|
||||
template <>
|
||||
struct select_format_char_type<char16_t> {
|
||||
using type = wchar_t;
|
||||
};
|
||||
template <>
|
||||
struct select_format_char_type<char32_t> {
|
||||
using type = wchar_t;
|
||||
};
|
||||
#endif // MPT_USTRING_MODE_WIDE
|
||||
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
struct select_format_string_type {
|
||||
using type = mpt::ustring;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct select_format_string_type<std::string> {
|
||||
using type = std::string;
|
||||
};
|
||||
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
template <>
|
||||
struct select_format_string_type<std::wstring> {
|
||||
using type = std::wstring;
|
||||
};
|
||||
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
|
||||
#if MPT_CXX_AT_LEAST(20)
|
||||
template <>
|
||||
struct select_format_string_type<std::u8string> {
|
||||
using type = std::u8string;
|
||||
};
|
||||
#endif // C++20
|
||||
|
||||
template <>
|
||||
struct select_format_string_type<std::u16string> {
|
||||
using type = std::u16string;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct select_format_string_type<std::u32string> {
|
||||
using type = std::u32string;
|
||||
};
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_HELPERS_HPP
|
||||
@@ -0,0 +1,42 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_JOIN_HPP
|
||||
#define MPT_FORMAT_JOIN_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/simple.hpp"
|
||||
#include "mpt/string/utility.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename Tstring, typename T>
|
||||
Tstring join_format(const std::vector<T> & vals, const Tstring & sep = Tstring(1, char_constants<typename Tstring::value_type>::comma)) {
|
||||
Tstring str;
|
||||
for (std::size_t i = 0; i < vals.size(); ++i) {
|
||||
if (i > 0) {
|
||||
str += sep;
|
||||
}
|
||||
str += mpt::format<Tstring>::val(vals[i]);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_JOIN_HPP
|
||||
@@ -0,0 +1,335 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_MESSAGE_HPP
|
||||
#define MPT_FORMAT_MESSAGE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/span.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/string/utility.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
class format_message_syntax_error
|
||||
: public std::domain_error {
|
||||
public:
|
||||
format_message_syntax_error()
|
||||
: std::domain_error("format string syntax error") {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tformatter, typename Tstring>
|
||||
class message_formatter {
|
||||
|
||||
private:
|
||||
using Tstringview = typename mpt::make_string_view_type<Tstring>::type;
|
||||
|
||||
private:
|
||||
Tstring format;
|
||||
|
||||
private:
|
||||
MPT_NOINLINE Tstring do_format(const mpt::span<const Tstring> vals) const {
|
||||
using traits = typename mpt::string_traits<Tstring>;
|
||||
using char_type = typename traits::char_type;
|
||||
using size_type = typename traits::size_type;
|
||||
Tstring result;
|
||||
const size_type len = traits::length(format);
|
||||
traits::reserve(result, len);
|
||||
std::size_t max_arg = 0;
|
||||
//std::size_t args = 0;
|
||||
bool success = true;
|
||||
enum class state : int {
|
||||
error = -1,
|
||||
text = 0,
|
||||
open_seen = 1,
|
||||
number_seen = 2,
|
||||
close_seen = 3,
|
||||
};
|
||||
state state = state::text;
|
||||
bool numbered_args = false;
|
||||
bool unnumbered_args = false;
|
||||
std::size_t last_arg = 0;
|
||||
std::size_t this_arg = 0;
|
||||
std::size_t current_arg = 0;
|
||||
for (size_type pos = 0; pos != len; ++pos) {
|
||||
char_type c = format[pos];
|
||||
switch (state) {
|
||||
case state::text:
|
||||
if (c == char_type('{')) {
|
||||
state = state::open_seen;
|
||||
} else if (c == char_type('}')) {
|
||||
state = state::close_seen;
|
||||
} else {
|
||||
state = state::text;
|
||||
traits::append(result, c); // output c here
|
||||
}
|
||||
break;
|
||||
case state::open_seen:
|
||||
if (c == char_type('{')) {
|
||||
state = state::text;
|
||||
traits::append(result, char_type('{')); // output { here
|
||||
} else if (c == char_type('}')) {
|
||||
state = state::text;
|
||||
unnumbered_args = true;
|
||||
last_arg++;
|
||||
this_arg = last_arg;
|
||||
{ // output this_arg here
|
||||
const std::size_t n = this_arg - 1;
|
||||
if (n < std::size(vals)) {
|
||||
traits::append(result, vals[n]);
|
||||
}
|
||||
}
|
||||
if (this_arg > max_arg) {
|
||||
max_arg = this_arg;
|
||||
}
|
||||
//args += 1;
|
||||
} else if (char_type('0') <= c && c <= char_type('9')) {
|
||||
state = state::number_seen;
|
||||
numbered_args = true;
|
||||
current_arg = c - char_type('0');
|
||||
} else {
|
||||
state = state::error;
|
||||
}
|
||||
break;
|
||||
case state::number_seen:
|
||||
if (c == char_type('{')) {
|
||||
state = state::error;
|
||||
} else if (c == char_type('}')) {
|
||||
state = state::text;
|
||||
this_arg = current_arg + 1;
|
||||
{ // output this_arg here
|
||||
const std::size_t n = this_arg - 1;
|
||||
if (n < std::size(vals)) {
|
||||
traits::append(result, vals[n]);
|
||||
}
|
||||
}
|
||||
if (this_arg > max_arg) {
|
||||
max_arg = this_arg;
|
||||
}
|
||||
//args += 1;
|
||||
} else if (char_type('0') <= c && c <= char_type('9')) {
|
||||
state = state::number_seen;
|
||||
numbered_args = true;
|
||||
current_arg = (current_arg * 10) + (c - char_type('0'));
|
||||
} else {
|
||||
state = state::error;
|
||||
}
|
||||
break;
|
||||
case state::close_seen:
|
||||
if (c == char_type('{')) {
|
||||
state = state::error;
|
||||
} else if (c == char_type('}')) {
|
||||
state = state::text;
|
||||
traits::append(result, char_type('}')); // output } here
|
||||
} else {
|
||||
state = state::error;
|
||||
}
|
||||
break;
|
||||
case state::error:
|
||||
state = state::error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (state == state::error) {
|
||||
success = false;
|
||||
}
|
||||
if (state != state::text) {
|
||||
success = false;
|
||||
}
|
||||
if (numbered_args && unnumbered_args) {
|
||||
success = false;
|
||||
}
|
||||
if (!success) {
|
||||
throw format_message_syntax_error();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public:
|
||||
MPT_FORCEINLINE message_formatter(Tstring format_)
|
||||
: format(std::move(format_)) {
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename... Ts>
|
||||
MPT_NOINLINE Tstring operator()(Ts &&... xs) const {
|
||||
const std::array<Tstring, sizeof...(xs)> vals{{Tformatter::template format<Tstring>(std::forward<Ts>(xs))...}};
|
||||
return do_format(mpt::as_span(vals));
|
||||
}
|
||||
|
||||
}; // class message_formatter
|
||||
|
||||
|
||||
template <typename Tformatter, std::ptrdiff_t N, typename Tchar, typename Tstring>
|
||||
class message_formatter_counted {
|
||||
|
||||
private:
|
||||
using Tstringview = typename mpt::make_string_view_type<Tstring>::type;
|
||||
|
||||
private:
|
||||
message_formatter<Tformatter, Tstring> formatter;
|
||||
|
||||
public:
|
||||
template <std::size_t literal_length>
|
||||
inline message_formatter_counted(const Tchar (&format)[literal_length])
|
||||
: formatter(Tstring(format)) {
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename... Ts>
|
||||
inline Tstring operator()(Ts &&... xs) const {
|
||||
static_assert(static_cast<std::ptrdiff_t>(sizeof...(xs)) == N);
|
||||
return formatter(std::forward<Ts>(xs)...);
|
||||
}
|
||||
|
||||
}; // struct message_formatter_counted
|
||||
|
||||
|
||||
template <typename Tchar>
|
||||
MPT_CONSTEXPRINLINE std::ptrdiff_t parse_format_string_argument_count_impl(const Tchar * const format, const std::size_t len) {
|
||||
std::size_t max_arg = 0;
|
||||
std::size_t args = 0;
|
||||
bool success = true;
|
||||
enum class state : int {
|
||||
error = -1,
|
||||
text = 0,
|
||||
open_seen = 1,
|
||||
number_seen = 2,
|
||||
close_seen = 3,
|
||||
};
|
||||
state state = state::text;
|
||||
bool numbered_args = false;
|
||||
bool unnumbered_args = false;
|
||||
std::size_t last_arg = 0;
|
||||
std::size_t this_arg = 0;
|
||||
std::size_t current_arg = 0;
|
||||
for (std::size_t pos = 0; pos != len; ++pos) {
|
||||
Tchar c = format[pos];
|
||||
switch (state) {
|
||||
case state::text:
|
||||
if (c == Tchar('{')) {
|
||||
state = state::open_seen;
|
||||
} else if (c == Tchar('}')) {
|
||||
state = state::close_seen;
|
||||
} else {
|
||||
state = state::text;
|
||||
// output c here
|
||||
}
|
||||
break;
|
||||
case state::open_seen:
|
||||
if (c == Tchar('{')) {
|
||||
state = state::text;
|
||||
// output { here
|
||||
} else if (c == Tchar('}')) {
|
||||
state = state::text;
|
||||
unnumbered_args = true;
|
||||
last_arg++;
|
||||
this_arg = last_arg;
|
||||
// output this_arg here
|
||||
if (this_arg > max_arg)
|
||||
{
|
||||
max_arg = this_arg;
|
||||
}
|
||||
args += 1;
|
||||
} else if (Tchar('0') <= c && c <= Tchar('9')) {
|
||||
state = state::number_seen;
|
||||
numbered_args = true;
|
||||
current_arg = c - Tchar('0');
|
||||
} else {
|
||||
state = state::error;
|
||||
}
|
||||
break;
|
||||
case state::number_seen:
|
||||
if (c == Tchar('{')) {
|
||||
state = state::error;
|
||||
} else if (c == Tchar('}')) {
|
||||
state = state::text;
|
||||
this_arg = current_arg + 1;
|
||||
// output this_arg here
|
||||
if (this_arg > max_arg) {
|
||||
max_arg = this_arg;
|
||||
}
|
||||
args += 1;
|
||||
} else if (Tchar('0') <= c && c <= Tchar('9')) {
|
||||
state = state::number_seen;
|
||||
numbered_args = true;
|
||||
current_arg = (current_arg * 10) + (c - Tchar('0'));
|
||||
} else {
|
||||
state = state::error;
|
||||
}
|
||||
break;
|
||||
case state::close_seen:
|
||||
if (c == Tchar('{')) {
|
||||
state = state::error;
|
||||
} else if (c == Tchar('}')) {
|
||||
state = state::text;
|
||||
// output } here
|
||||
} else {
|
||||
state = state::error;
|
||||
}
|
||||
break;
|
||||
case state::error:
|
||||
state = state::error;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (state == state::error) {
|
||||
success = false;
|
||||
}
|
||||
if (state != state::text) {
|
||||
success = false;
|
||||
}
|
||||
if (numbered_args && unnumbered_args) {
|
||||
success = false;
|
||||
}
|
||||
if (!success) {
|
||||
throw format_message_syntax_error();
|
||||
}
|
||||
if (max_arg != args) {
|
||||
throw format_message_syntax_error();
|
||||
}
|
||||
return max_arg;
|
||||
}
|
||||
|
||||
|
||||
template <typename Tchar, std::size_t literal_length>
|
||||
MPT_CONSTEXPRINLINE std::ptrdiff_t parse_format_string_argument_count(const Tchar (&format)[literal_length]) {
|
||||
return parse_format_string_argument_count_impl(format, literal_length - 1);
|
||||
}
|
||||
|
||||
|
||||
template <typename Tformatter, std::size_t args, typename Tchar, std::size_t N>
|
||||
inline auto format_message(const Tchar (&format)[N]) {
|
||||
using Tstring = typename mpt::make_string_type<const Tchar *>::type;
|
||||
return message_formatter_counted<Tformatter, args, Tchar, Tstring>(format);
|
||||
}
|
||||
|
||||
template <typename Tformatter, std::size_t args, typename Tstring, typename Tchar, std::size_t N>
|
||||
inline auto format_message_typed(const Tchar (&format)[N]) {
|
||||
return message_formatter_counted<Tformatter, args, Tchar, Tstring>(format);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_MESSAGE_HPP
|
||||
@@ -0,0 +1,37 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_MESSAGE_MACROS_HPP
|
||||
#define MPT_FORMAT_MESSAGE_MACROS_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/detect/mfc.hpp"
|
||||
#include "mpt/format/default_formatter.hpp"
|
||||
#include "mpt/format/message.hpp"
|
||||
|
||||
|
||||
|
||||
#define MPT_AFORMAT_MESSAGE(f) mpt::format_message<mpt::default_formatter, mpt::parse_format_string_argument_count(f)>(f)
|
||||
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
#define MPT_WFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(L##f), std::wstring>(L##f)
|
||||
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
|
||||
#define MPT_UFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(MPT_ULITERAL(f)), mpt::ustring>(MPT_ULITERAL(f))
|
||||
|
||||
#define MPT_LFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(f), mpt::lstring>(f)
|
||||
|
||||
#if MPT_OS_WINDOWS
|
||||
#define MPT_TFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(TEXT(f)), mpt::tstring>(TEXT(f))
|
||||
#endif // MPT_OS_WINDOWS
|
||||
|
||||
#if MPT_DETECTED_MFC
|
||||
#define MPT_CWFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(L##f), CStringW>(L##f)
|
||||
#define MPT_CAFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(f), CStringA>(f)
|
||||
#define MPT_CFORMAT_MESSAGE(f) mpt::format_message_typed<mpt::default_formatter, mpt::parse_format_string_argument_count(TEXT(f)), CString>(TEXT(f))
|
||||
#endif // MPT_DETECTED_MFC
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_MESSAGE_MACROS_HPP
|
||||
@@ -0,0 +1,166 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_SIMPLE_HPP
|
||||
#define MPT_FORMAT_SIMPLE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/pointer.hpp"
|
||||
#include "mpt/format/default_formatter.hpp"
|
||||
#include "mpt/format/simple_floatingpoint.hpp"
|
||||
#include "mpt/format/simple_integer.hpp"
|
||||
#include "mpt/format/simple_spec.hpp"
|
||||
#include "mpt/string/utility.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
struct format : format_simple_base {
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring val(const T & x) {
|
||||
return mpt::default_formatter::format<Tstring>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring fmt(const T & x, const format_simple_spec<Tstring> & f) {
|
||||
return mpt::format_simple<Tstring>(x, f);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring dec(const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseDec().FillOff());
|
||||
}
|
||||
template <int width, typename T>
|
||||
static inline Tstring dec0(const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseDec().FillNul().Width(width));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring dec(unsigned int g, Tstring s, const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseDec().FillOff().Group(g).GroupSep(s));
|
||||
}
|
||||
template <int width, typename T>
|
||||
static inline Tstring dec0(unsigned int g, Tstring s, const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseDec().FillNul().Width(width).Group(g).GroupSep(s));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring hex(const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseLow().FillOff());
|
||||
}
|
||||
template <typename T>
|
||||
static inline Tstring HEX(const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseUpp().FillOff());
|
||||
}
|
||||
template <int width, typename T>
|
||||
static inline Tstring hex0(const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseLow().FillNul().Width(width));
|
||||
}
|
||||
template <int width, typename T>
|
||||
static inline Tstring HEX0(const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseUpp().FillNul().Width(width));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring hex(unsigned int g, Tstring s, const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseLow().FillOff().Group(g).GroupSep(s));
|
||||
}
|
||||
template <typename T>
|
||||
static inline Tstring HEX(unsigned int g, Tstring s, const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseUpp().FillOff().Group(g).GroupSep(s));
|
||||
}
|
||||
template <int width, typename T>
|
||||
static inline Tstring hex0(unsigned int g, Tstring s, const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseLow().FillNul().Width(width).Group(g).GroupSep(s));
|
||||
}
|
||||
template <int width, typename T>
|
||||
static inline Tstring HEX0(unsigned int g, Tstring s, const T & x) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().BaseHex().CaseUpp().FillNul().Width(width).Group(g).GroupSep(s));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring flt(const T & x, int precision = -1) {
|
||||
static_assert(std::is_floating_point<T>::value);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().NotaNrm().FillOff().Precision(precision));
|
||||
}
|
||||
template <typename T>
|
||||
static inline Tstring fix(const T & x, int precision = -1) {
|
||||
static_assert(std::is_floating_point<T>::value);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().NotaFix().FillOff().Precision(precision));
|
||||
}
|
||||
template <typename T>
|
||||
static inline Tstring sci(const T & x, int precision = -1) {
|
||||
static_assert(std::is_floating_point<T>::value);
|
||||
return mpt::format_simple<Tstring>(x, format_simple_spec<Tstring>().NotaSci().FillOff().Precision(precision));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static inline Tstring ptr(const T & x) {
|
||||
static_assert(std::is_pointer<T>::value || std::is_same<T, std::uintptr_t>::value || std::is_same<T, std::intptr_t>::value, "");
|
||||
return hex0<mpt::pointer_size * 2>(mpt::pointer_cast<const std::uintptr_t>(x));
|
||||
}
|
||||
template <typename T>
|
||||
static inline Tstring PTR(const T & x) {
|
||||
static_assert(std::is_pointer<T>::value || std::is_same<T, std::uintptr_t>::value || std::is_same<T, std::intptr_t>::value, "");
|
||||
return HEX0<mpt::pointer_size * 2>(mpt::pointer_cast<const std::uintptr_t>(x));
|
||||
}
|
||||
|
||||
static inline Tstring pad_left(std::size_t width_, const Tstring & str) {
|
||||
typedef mpt::string_traits<Tstring> traits;
|
||||
typename traits::size_type width = static_cast<typename traits::size_type>(width_);
|
||||
return traits::pad(str, width, 0);
|
||||
}
|
||||
static inline Tstring pad_right(std::size_t width_, const Tstring & str) {
|
||||
typedef mpt::string_traits<Tstring> traits;
|
||||
typename traits::size_type width = static_cast<typename traits::size_type>(width_);
|
||||
return traits::pad(str, 0, width);
|
||||
}
|
||||
static inline Tstring left(std::size_t width_, const Tstring & str) {
|
||||
typedef mpt::string_traits<Tstring> traits;
|
||||
typename traits::size_type width = static_cast<typename traits::size_type>(width_);
|
||||
return (traits::length(str) < width) ? traits::pad(str, 0, width - traits::length(str)) : str;
|
||||
}
|
||||
static inline Tstring right(std::size_t width_, const Tstring & str) {
|
||||
typedef mpt::string_traits<Tstring> traits;
|
||||
typename traits::size_type width = static_cast<typename traits::size_type>(width_);
|
||||
return (traits::length(str) < width) ? traits::pad(str, width - traits::length(str), 0) : str;
|
||||
}
|
||||
static inline Tstring center(std::size_t width_, const Tstring & str) {
|
||||
typedef mpt::string_traits<Tstring> traits;
|
||||
typename traits::size_type width = static_cast<typename traits::size_type>(width_);
|
||||
return (traits::length(str) < width) ? traits::pad(str, (width - traits::length(str)) / 2, (width - traits::length(str) + 1) / 2) : str;
|
||||
}
|
||||
|
||||
}; // struct format
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_SIMPLE_HPP
|
||||
@@ -0,0 +1,246 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_SIMPLE_FLOATINGPOINT_HPP
|
||||
#define MPT_FORMAT_SIMPLE_FLOATINGPOINT_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_FLOAT)
|
||||
#define MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 1
|
||||
#else
|
||||
#define MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17 0
|
||||
#endif
|
||||
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
#include "mpt/base/algorithm.hpp"
|
||||
#endif
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/default_floatingpoint.hpp"
|
||||
#include "mpt/format/helpers.hpp"
|
||||
#include "mpt/format/simple_spec.hpp"
|
||||
#include "mpt/string/utility.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
#include <charconv>
|
||||
#endif
|
||||
#if !MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#endif
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
#include <iterator>
|
||||
#endif
|
||||
#if !MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
#include <limits>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
#endif
|
||||
#include <string>
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
#include <system_error>
|
||||
#endif
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
|
||||
|
||||
template <typename Tstring, typename T>
|
||||
inline Tstring format_simple_floatingpoint_to_chars(const T & x, std::chars_format fmt) {
|
||||
std::string str(1, '\0');
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x, fmt);
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(str);
|
||||
}
|
||||
|
||||
|
||||
template <typename Tstring, typename T>
|
||||
inline Tstring format_simple_floatingpoint_to_chars(const T & x, std::chars_format fmt, int precision) {
|
||||
std::string str(1, '\0');
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x, fmt, precision);
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(str);
|
||||
}
|
||||
|
||||
template <typename Tstring>
|
||||
inline Tstring format_simple_floatingpoint_postprocess_width(Tstring str, const format_simple_spec<Tstring> & format) {
|
||||
format_simple_flags f = format.GetFlags();
|
||||
std::size_t width = format.GetWidth();
|
||||
if (f & format_simple_base::FillNul) {
|
||||
auto pos = str.begin();
|
||||
if (str.length() > 0) {
|
||||
if (str[0] == mpt::char_constants<typename Tstring::value_type>::plus) {
|
||||
pos++;
|
||||
width++;
|
||||
} else if (str[0] == mpt::char_constants<typename Tstring::value_type>::minus) {
|
||||
pos++;
|
||||
width++;
|
||||
}
|
||||
}
|
||||
if (str.length() - std::distance(str.begin(), pos) < width) {
|
||||
str.insert(pos, width - str.length() - std::distance(str.begin(), pos), mpt::char_constants<typename Tstring::value_type>::number0);
|
||||
}
|
||||
} else {
|
||||
if (str.length() < width) {
|
||||
str.insert(0, width - str.length(), mpt::char_constants<typename Tstring::value_type>::space);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) {
|
||||
using format_string_type = typename mpt::select_format_string_type<Tstring>::type;
|
||||
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format);
|
||||
if (f.GetPrecision() != -1) {
|
||||
if (f.GetFlags() & format_simple_base::NotaSci) {
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::scientific, f.GetPrecision()), f));
|
||||
} else if (f.GetFlags() & format_simple_base::NotaFix) {
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::fixed, f.GetPrecision()), f));
|
||||
} else {
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::general, f.GetPrecision()), f));
|
||||
}
|
||||
} else {
|
||||
if (f.GetFlags() & format_simple_base::NotaSci) {
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::scientific), f));
|
||||
} else if (f.GetFlags() & format_simple_base::NotaFix) {
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::fixed), f));
|
||||
} else {
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_postprocess_width(format_simple_floatingpoint_to_chars<format_string_type>(x, std::chars_format::general), f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#else // !MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
|
||||
|
||||
template <typename Tchar>
|
||||
struct NumPunct : std::numpunct<Tchar> {
|
||||
private:
|
||||
unsigned int group;
|
||||
Tchar sep;
|
||||
|
||||
public:
|
||||
NumPunct(unsigned int g, Tchar s)
|
||||
: group(g)
|
||||
, sep(s) { }
|
||||
std::string do_grouping() const override {
|
||||
return std::string(1, static_cast<char>(group));
|
||||
}
|
||||
Tchar do_thousands_sep() const override {
|
||||
return sep;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Tostream, typename Tstring, typename T>
|
||||
inline void format_simple_floatingpoint_apply_stream_format(Tostream & o, const format_simple_spec<Tstring> & format, const T &) {
|
||||
if (format.GetGroup() > 0) {
|
||||
if (mpt::string_traits<Tstring>::length(format.GetGroupSep()) >= 1) {
|
||||
o.imbue(std::locale(o.getloc(), new NumPunct<typename Tostream::char_type>(format.GetGroup(), format.GetGroupSep()[0])));
|
||||
}
|
||||
}
|
||||
format_simple_flags f = format.GetFlags();
|
||||
std::size_t width = format.GetWidth();
|
||||
int precision = format.GetPrecision();
|
||||
if (precision != -1 && width != 0 && !(f & format_simple_base::NotaFix) && !(f & format_simple_base::NotaSci)) {
|
||||
// fixup:
|
||||
// precision behaves differently from .#
|
||||
// avoid default format when precision and width are set
|
||||
f &= ~format_simple_base::NotaNrm;
|
||||
f |= format_simple_base::NotaFix;
|
||||
}
|
||||
if (f & format_simple_base::BaseDec) {
|
||||
o << std::dec;
|
||||
} else if (f & format_simple_base::BaseHex) {
|
||||
o << std::hex;
|
||||
}
|
||||
if (f & format_simple_base::NotaNrm) { /*nothing*/
|
||||
} else if (f & format_simple_base::NotaFix) {
|
||||
o << std::setiosflags(std::ios::fixed);
|
||||
} else if (f & format_simple_base::NotaSci) {
|
||||
o << std::setiosflags(std::ios::scientific);
|
||||
}
|
||||
if (f & format_simple_base::CaseLow) {
|
||||
o << std::nouppercase;
|
||||
} else if (f & format_simple_base::CaseUpp) {
|
||||
o << std::uppercase;
|
||||
}
|
||||
if (f & format_simple_base::FillOff) { /* nothing */
|
||||
} else if (f & format_simple_base::FillNul) {
|
||||
o << std::setw(width) << std::setfill(mpt::char_constants<typename Tostream::char_type>::number0);
|
||||
}
|
||||
if (precision != -1) {
|
||||
o << std::setprecision(precision);
|
||||
} else {
|
||||
if constexpr (std::is_floating_point<T>::value) {
|
||||
if (f & format_simple_base::NotaNrm) {
|
||||
o << std::setprecision(std::numeric_limits<T>::max_digits10);
|
||||
} else if (f & format_simple_base::NotaFix) {
|
||||
o << std::setprecision(std::numeric_limits<T>::digits10);
|
||||
} else if (f & format_simple_base::NotaSci) {
|
||||
o << std::setprecision(std::numeric_limits<T>::max_digits10 - 1);
|
||||
} else {
|
||||
o << std::setprecision(std::numeric_limits<T>::max_digits10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename Tstring, typename T>
|
||||
inline Tstring format_simple_floatingpoint_stream(const T & x, const format_simple_spec<Tstring> & f) {
|
||||
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type;
|
||||
std::basic_ostringstream<stream_char_type> s;
|
||||
s.imbue(std::locale::classic());
|
||||
mpt::format_simple_floatingpoint_apply_stream_format(s, f, x);
|
||||
s << x;
|
||||
return mpt::convert_formatted_simple<Tstring>(s.str());
|
||||
}
|
||||
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
|
||||
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) {
|
||||
using format_string_type = typename mpt::select_format_string_type<Tstring>::type;
|
||||
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format);
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_floatingpoint_stream<format_string_type>(x, f));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_FLOAT_CXX17
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_SIMPLE_FLOATINGPOINT_HPP
|
||||
@@ -0,0 +1,253 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_SIMPLE_INTEGER_HPP
|
||||
#define MPT_FORMAT_SIMPLE_INTEGER_HPP
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
|
||||
#if !defined(MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT)
|
||||
#define MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 1
|
||||
#else // MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
#define MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17 0
|
||||
#endif // !MPT_LIBCXX_QUIRK_NO_TO_CHARS_INT
|
||||
|
||||
#include "mpt/base/algorithm.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/helpers.hpp"
|
||||
#include "mpt/format/simple_spec.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
|
||||
#if !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <algorithm>
|
||||
#endif // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <charconv>
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#if !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <ios>
|
||||
#include <locale>
|
||||
#endif // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <string>
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#if !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <sstream>
|
||||
#endif // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <system_error>
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring format_simple_integer_to_chars(const T & x, int base) {
|
||||
std::string str(1, '\0');
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
if constexpr (std::is_same<T, bool>::value) {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), static_cast<int>(x), base);
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
} else {
|
||||
std::to_chars_result result = std::to_chars(str.data(), str.data() + str.size(), x, base);
|
||||
if (result.ec != std::errc{}) {
|
||||
str.resize(mpt::exponential_grow(str.size()), '\0');
|
||||
} else {
|
||||
str.resize(result.ptr - str.data());
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(str);
|
||||
}
|
||||
|
||||
#else // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring format_simple_integer_to_stream(const T & x, int base) {
|
||||
using stream_char_type = typename mpt::select_format_char_type<typename Tstring::value_type>::type;
|
||||
if ((base == 10) || ((base == 16) && std::is_unsigned<T>::value) || ((base == 8) && std::is_unsigned<T>::value)) {
|
||||
// fast path
|
||||
std::basic_ostringstream<stream_char_type> s;
|
||||
s.imbue(std::locale::classic());
|
||||
if (base == 16) {
|
||||
s << std::hex;
|
||||
} else if (base == 8) {
|
||||
s << std::oct;
|
||||
}
|
||||
if constexpr (std::is_same<T, bool>::value) {
|
||||
s << static_cast<int>(x);
|
||||
} else if constexpr (mpt::is_character<T>::value) {
|
||||
s << (x + 0); // force integral promotion
|
||||
} else {
|
||||
s << x;
|
||||
}
|
||||
return mpt::convert_formatted_simple<Tstring>(s.str());
|
||||
} else {
|
||||
if constexpr (std::is_same<T, bool>::value) {
|
||||
return x ? Tstring(1, mpt::char_constants<typename Tstring::value_type>::number1) : Tstring(1, mpt::char_constants<typename Tstring::value_type>::number0);
|
||||
} else if constexpr (std::is_unsigned<T>::value) {
|
||||
Tstring result;
|
||||
T val = x;
|
||||
if (val == 0) {
|
||||
result += Tstring(1, mpt::char_constants<typename Tstring::value_type>::number0);
|
||||
} else {
|
||||
using Tunsigned = typename std::make_unsigned<T>::type;
|
||||
while (val > 0) {
|
||||
Tunsigned digit = static_cast<Tunsigned>(val % static_cast<unsigned int>(base));
|
||||
val = static_cast<Tunsigned>(val / static_cast<unsigned int>(base));
|
||||
if (digit >= 10) {
|
||||
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::a - 10 + digit));
|
||||
} else {
|
||||
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::number0 + digit));
|
||||
}
|
||||
}
|
||||
std::reverse(result.begin(), result.end());
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
Tstring result;
|
||||
if (x == 0) {
|
||||
result += Tstring(1, mpt::char_constants<typename Tstring::value_type>::number0);
|
||||
} else {
|
||||
using Tunsigned = typename std::make_unsigned<T>::type;
|
||||
Tunsigned val = (x != -x) ? ((x >= 0) ? x : -x) : (static_cast<Tunsigned>(-(x + 1)) + 1);
|
||||
while (val > 0) {
|
||||
Tunsigned digit = static_cast<Tunsigned>(val % static_cast<unsigned int>(base));
|
||||
val = static_cast<Tunsigned>(val / static_cast<unsigned int>(base));
|
||||
if (digit >= 10) {
|
||||
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::a - 10 + digit));
|
||||
} else {
|
||||
result += Tstring(1, static_cast<typename Tstring::value_type>(mpt::char_constants<typename Tstring::value_type>::number0 + digit));
|
||||
}
|
||||
}
|
||||
if (x < 0) {
|
||||
result += Tstring(1, mpt::char_constants<typename Tstring::value_type>::minus);
|
||||
}
|
||||
std::reverse(result.begin(), result.end());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
inline Tstring format_simple_integer_postprocess_case(Tstring str, const format_simple_spec<Tstring> & format) {
|
||||
format_simple_flags f = format.GetFlags();
|
||||
if (f & format_simple_base::CaseUpp) {
|
||||
for (auto & c : str) {
|
||||
if (mpt::char_constants<typename Tstring::value_type>::a <= c && c <= mpt::char_constants<typename Tstring::value_type>::z) {
|
||||
c -= mpt::char_constants<typename Tstring::value_type>::a - mpt::char_constants<typename Tstring::value_type>::A;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
inline Tstring format_simple_integer_postprocess_digits(Tstring str, const format_simple_spec<Tstring> & format) {
|
||||
format_simple_flags f = format.GetFlags();
|
||||
std::size_t width = format.GetWidth();
|
||||
if (f & format_simple_base::FillNul) {
|
||||
auto pos = str.begin();
|
||||
if (str.length() > 0) {
|
||||
if (str[0] == mpt::char_constants<typename Tstring::value_type>::plus) {
|
||||
pos++;
|
||||
width++;
|
||||
} else if (str[0] == mpt::char_constants<typename Tstring::value_type>::minus) {
|
||||
pos++;
|
||||
width++;
|
||||
}
|
||||
}
|
||||
if (str.length() < width) {
|
||||
str.insert(pos, width - str.length(), mpt::char_constants<typename Tstring::value_type>::number0);
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
#if MPT_COMPILER_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4723) // potential divide by 0
|
||||
#endif // MPT_COMPILER_MSVC
|
||||
template <typename Tstring>
|
||||
inline Tstring format_simple_integer_postprocess_group(Tstring str, const format_simple_spec<Tstring> & format) {
|
||||
if (format.GetGroup() > 0) {
|
||||
const unsigned int groupSize = format.GetGroup();
|
||||
const Tstring groupSep = format.GetGroupSep();
|
||||
std::size_t len = str.length();
|
||||
for (std::size_t n = 0; n < len; ++n) {
|
||||
if (n > 0 && (n % groupSize) == 0) {
|
||||
if (!(n == (len - 1) && (str[0] == mpt::char_constants<typename Tstring::value_type>::plus || str[0] == mpt::char_constants<typename Tstring::value_type>::minus))) {
|
||||
str.insert(len - n, groupSep);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
#if MPT_COMPILER_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif // MPT_COMPILER_MSVC
|
||||
|
||||
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) {
|
||||
int base = 10;
|
||||
if (format.GetFlags() & format_simple_base::BaseDec) {
|
||||
base = 10;
|
||||
}
|
||||
if (format.GetFlags() & format_simple_base::BaseHex) {
|
||||
base = 16;
|
||||
}
|
||||
using format_string_type = typename mpt::select_format_string_type<Tstring>::type;
|
||||
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format);
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_integer_postprocess_group(mpt::format_simple_integer_postprocess_digits(mpt::format_simple_integer_postprocess_case(mpt::format_simple_integer_to_chars<format_string_type>(x, base), f), f), f));
|
||||
}
|
||||
|
||||
#else // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
template <typename Tstring, typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
|
||||
inline Tstring format_simple(const T & x, const format_simple_spec<Tstring> & format) {
|
||||
int base = 10;
|
||||
if (format.GetFlags() & format_simple_base::BaseDec) {
|
||||
base = 10;
|
||||
}
|
||||
if (format.GetFlags() & format_simple_base::BaseHex) {
|
||||
base = 16;
|
||||
}
|
||||
using format_string_type = typename mpt::select_format_string_type<Tstring>::type;
|
||||
const format_simple_spec<format_string_type> f = mpt::transcode_format_simple_spec<format_string_type>(format);
|
||||
return mpt::transcode<Tstring>(mpt::format_simple_integer_postprocess_group(mpt::format_simple_integer_postprocess_digits(mpt::format_simple_integer_postprocess_case(mpt::format_simple_integer_to_stream<format_string_type>(x, base), f), f), f));
|
||||
}
|
||||
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_SIMPLE_INTEGER_HPP
|
||||
@@ -0,0 +1,272 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_SIMPLE_SPEC_HPP
|
||||
#define MPT_FORMAT_SIMPLE_SPEC_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/string_transcode/transcode.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
struct format_simple_base {
|
||||
|
||||
enum FormatFlagsEnum {
|
||||
BaseDec = 0x0001, // base 10 (integers only) // int+float
|
||||
BaseHex = 0x0002, // base 16 (integers only) // int+float
|
||||
CaseLow = 0x0010, // lower case hex digits // int+float
|
||||
CaseUpp = 0x0020, // upper case hex digits // int+float
|
||||
FillOff = 0x0100, // do not fill up width // int+float
|
||||
FillNul = 0x0400, // fill up width with zeros // int+float
|
||||
NotaNrm = 0x1000, // float: normal/default notation // float
|
||||
NotaFix = 0x2000, // float: fixed point notation // float
|
||||
NotaSci = 0x4000, // float: scientific notation // float
|
||||
};
|
||||
|
||||
}; // struct format_simple_base
|
||||
|
||||
using format_simple_flags = unsigned int;
|
||||
|
||||
static_assert(sizeof(format_simple_flags) >= sizeof(format_simple_base::FormatFlagsEnum));
|
||||
|
||||
|
||||
template <typename Tstring>
|
||||
class format_simple_spec {
|
||||
private:
|
||||
format_simple_flags flags;
|
||||
std::size_t width; // int+float
|
||||
int precision; // float
|
||||
unsigned int group; // int
|
||||
Tstring group_sep; // int
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE format_simple_spec() noexcept
|
||||
: flags(0)
|
||||
, width(0)
|
||||
, precision(-1)
|
||||
, group(0)
|
||||
, group_sep() { }
|
||||
MPT_CONSTEXPRINLINE format_simple_flags GetFlags() const noexcept {
|
||||
return flags;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE std::size_t GetWidth() const noexcept {
|
||||
return width;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE int GetPrecision() const noexcept {
|
||||
return precision;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE unsigned int GetGroup() const noexcept {
|
||||
return group;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE Tstring GetGroupSep() const {
|
||||
return group_sep;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & SetFlags(format_simple_flags f) noexcept {
|
||||
flags = f;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & SetWidth(std::size_t w) noexcept {
|
||||
width = w;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & SetPrecision(int p) noexcept {
|
||||
precision = p;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & SetGroup(unsigned int g) noexcept {
|
||||
group = g;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & SetGroupSep(Tstring s) {
|
||||
group_sep = std::move(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & BaseDec() noexcept {
|
||||
flags &= ~(format_simple_base::BaseDec | format_simple_base::BaseHex);
|
||||
flags |= format_simple_base::BaseDec;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & BaseHex() noexcept {
|
||||
flags &= ~(format_simple_base::BaseDec | format_simple_base::BaseHex);
|
||||
flags |= format_simple_base::BaseHex;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & CaseLow() noexcept {
|
||||
flags &= ~(format_simple_base::CaseLow | format_simple_base::CaseUpp);
|
||||
flags |= format_simple_base::CaseLow;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & CaseUpp() noexcept {
|
||||
flags &= ~(format_simple_base::CaseLow | format_simple_base::CaseUpp);
|
||||
flags |= format_simple_base::CaseUpp;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FillOff() noexcept {
|
||||
flags &= ~(format_simple_base::FillOff | format_simple_base::FillNul);
|
||||
flags |= format_simple_base::FillOff;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FillNul() noexcept {
|
||||
flags &= ~(format_simple_base::FillOff | format_simple_base::FillNul);
|
||||
flags |= format_simple_base::FillNul;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & NotaNrm() noexcept {
|
||||
flags &= ~(format_simple_base::NotaNrm | format_simple_base::NotaFix | format_simple_base::NotaSci);
|
||||
flags |= format_simple_base::NotaNrm;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & NotaFix() noexcept {
|
||||
flags &= ~(format_simple_base::NotaNrm | format_simple_base::NotaFix | format_simple_base::NotaSci);
|
||||
flags |= format_simple_base::NotaFix;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & NotaSci() noexcept {
|
||||
flags &= ~(format_simple_base::NotaNrm | format_simple_base::NotaFix | format_simple_base::NotaSci);
|
||||
flags |= format_simple_base::NotaSci;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Width(std::size_t w) noexcept {
|
||||
width = w;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Prec(int p) noexcept {
|
||||
precision = p;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Group(unsigned int g) noexcept {
|
||||
group = g;
|
||||
return *this;
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & GroupSep(Tstring s) {
|
||||
group_sep = std::move(s);
|
||||
return *this;
|
||||
}
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Dec() noexcept {
|
||||
return BaseDec();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Hex() noexcept {
|
||||
return BaseHex();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Low() noexcept {
|
||||
return CaseLow();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Upp() noexcept {
|
||||
return CaseUpp();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Off() noexcept {
|
||||
return FillOff();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Nul() noexcept {
|
||||
return FillNul();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Nrm() noexcept {
|
||||
return NotaNrm();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Fix() noexcept {
|
||||
return NotaFix();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Sci() noexcept {
|
||||
return NotaSci();
|
||||
}
|
||||
|
||||
public:
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Decimal() noexcept {
|
||||
return BaseDec();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Hexadecimal() noexcept {
|
||||
return BaseHex();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Lower() noexcept {
|
||||
return CaseLow();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Upper() noexcept {
|
||||
return CaseUpp();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FillNone() noexcept {
|
||||
return FillOff();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FillZero() noexcept {
|
||||
return FillNul();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FloatNormal() noexcept {
|
||||
return NotaNrm();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FloatFixed() noexcept {
|
||||
return NotaFix();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & FloatScientific() noexcept {
|
||||
return NotaSci();
|
||||
}
|
||||
MPT_CONSTEXPRINLINE format_simple_spec & Precision(int p) noexcept {
|
||||
return Prec(p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Tdststring, typename Tsrcstring>
|
||||
inline format_simple_spec<Tdststring> transcode_format_simple_spec(const format_simple_spec<Tsrcstring> & src) {
|
||||
format_simple_spec<Tdststring> dst;
|
||||
dst.SetFlags(src.GetFlags());
|
||||
dst.SetWidth(src.GetWidth());
|
||||
dst.SetPrecision(src.GetPrecision());
|
||||
dst.SetGroup(src.GetGroup());
|
||||
dst.SetGroupSep(mpt::transcode<Tdststring>(src.GetGroupSep()));
|
||||
return dst;
|
||||
}
|
||||
|
||||
template <typename Tdststring, typename Tsrcstring, typename Tencoding, std::enable_if_t<std::is_same<Tdststring, std::string>::value, bool> = true>
|
||||
inline format_simple_spec<Tdststring> transcode_format_simple_spec(Tencoding to, const format_simple_spec<Tsrcstring> & src) {
|
||||
format_simple_spec<Tdststring> dst;
|
||||
dst.SetFlags(src.GetFlags());
|
||||
dst.SetWidth(src.GetWidth());
|
||||
dst.SetPrecision(src.GetPrecision());
|
||||
dst.SetGroup(src.GetGroup());
|
||||
dst.SetGroupSep(mpt::transcode<Tdststring>(to, src.GetGroupSep()));
|
||||
return dst;
|
||||
}
|
||||
|
||||
template <typename Tdststring, typename Tsrcstring, typename Tencoding, std::enable_if_t<std::is_same<typename mpt::make_string_type<Tsrcstring>::type, std::string>::value, bool> = true>
|
||||
inline format_simple_spec<Tdststring> transcode_format_simple_spec(Tencoding from, const format_simple_spec<Tsrcstring> & src) {
|
||||
format_simple_spec<Tdststring> dst;
|
||||
dst.SetFlags(src.GetFlags());
|
||||
dst.SetWidth(src.GetWidth());
|
||||
dst.SetPrecision(src.GetPrecision());
|
||||
dst.SetGroup(src.GetGroup());
|
||||
dst.SetGroupSep(mpt::transcode<Tdststring>(from, src.GetGroupSep()));
|
||||
return dst;
|
||||
}
|
||||
|
||||
template <typename Tdststring, typename Tsrcstring, typename Tto, typename Tfrom>
|
||||
inline format_simple_spec<Tdststring> transcode_format_simple_spec(Tto to, Tfrom from, const format_simple_spec<Tsrcstring> & src) {
|
||||
format_simple_spec<Tdststring> dst;
|
||||
dst.SetFlags(src.GetFlags());
|
||||
dst.SetWidth(src.GetWidth());
|
||||
dst.SetPrecision(src.GetPrecision());
|
||||
dst.SetGroup(src.GetGroup());
|
||||
dst.SetGroupSep(mpt::transcode<Tdststring>(to, from, src.GetGroupSep()));
|
||||
return dst;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_SIMPLE_SPEC_HPP
|
||||
@@ -0,0 +1,83 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_TESTS_FORMAT_MESSAGE_HPP
|
||||
#define MPT_FORMAT_TESTS_FORMAT_MESSAGE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/format/message.hpp"
|
||||
#include "mpt/format/message_macros.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace format {
|
||||
namespace message {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/format/message")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
static_assert(mpt::parse_format_string_argument_count("") == 0);
|
||||
static_assert(mpt::parse_format_string_argument_count("{{") == 0);
|
||||
static_assert(mpt::parse_format_string_argument_count("}}") == 0);
|
||||
static_assert(mpt::parse_format_string_argument_count("{}") == 1);
|
||||
static_assert(mpt::parse_format_string_argument_count("{}{}") == 2);
|
||||
static_assert(mpt::parse_format_string_argument_count("{0}{1}") == 2);
|
||||
|
||||
// basic
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{}{}{}")(1, 2, 3), "123");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{2}{1}{0}")(1, 2, 3), "321");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{2}{1}{0}{4}{3}{6}{5}{7}{10}{9}{8}")(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a"), "21043657a98");
|
||||
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE(L"{}{}{}")(1, 2, 3), L"123");
|
||||
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
|
||||
// escaping behviour
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("%")(), "%");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("%")(), "%");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("%%")(), "%%");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{}")("a"), "a");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{}%")("a"), "a%");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{}%")("a"), "a%");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{}%%")("a"), "a%%");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("%1")(), "%1");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("%{}")("a"), "%a");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("%b")(), "%b");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{{}}")(), "{}");
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_AFORMAT_MESSAGE("{{{}}}")("a"), "{a}");
|
||||
|
||||
// formatting string_view
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_UFORMAT_MESSAGE("{}")(mpt::ustring(MPT_ULITERAL("foo"))), MPT_USTRING("foo"));
|
||||
MPT_TEST_EXPECT_EQUAL(MPT_UFORMAT_MESSAGE("{}")(mpt::ustring_view(MPT_ULITERAL("foo"))), MPT_USTRING("foo"));
|
||||
}
|
||||
|
||||
} // namespace message
|
||||
} // namespace format
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_TESTS_FORMAT_MESSAGE_HPP
|
||||
@@ -0,0 +1,241 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_FORMAT_TESTS_FORMAT_SIMPLE_HPP
|
||||
#define MPT_FORMAT_TESTS_FORMAT_SIMPLE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/detect.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/detect/mfc.hpp"
|
||||
#include "mpt/format/simple.hpp"
|
||||
#include "mpt/format/simple_integer.hpp"
|
||||
#include "mpt/string/types.hpp"
|
||||
#include "mpt/test/test.hpp"
|
||||
#include "mpt/test/test_macros.hpp"
|
||||
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace tests {
|
||||
namespace format {
|
||||
namespace simple {
|
||||
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wglobal-constructors"
|
||||
#endif
|
||||
MPT_TEST_GROUP_INLINE("mpt/format/simple")
|
||||
#if MPT_COMPILER_CLANG
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(1.5f), "1.5");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(true), "1");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(false), "0");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(0), "0");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(-23), "-23");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(42), "42");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex0<3>((int32)-1), "-001");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex((int32)-1), "-1");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex(-0xabcde), "-abcde");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex(std::numeric_limits<int32>::min()), "-80000000");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex(std::numeric_limits<int32>::min() + 1), "-7fffffff");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex(0x123e), "123e");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex0<6>(0x123e), "00123e");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex0<2>(0x123e), "123e");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<0>(1), "1");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<1>(1), "1");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<2>(1), "01");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<3>(1), "001");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<0>(11), "11");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<1>(11), "11");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<2>(11), "11");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<3>(11), "011");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<0>(-1), "-1");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<1>(-1), "-1");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<2>(-1), "-01");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec0<3>(-1), "-001");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<7>(0xa2345678), MPT_USTRING("A2345678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<8>(0xa2345678), MPT_USTRING("A2345678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<9>(0xa2345678), MPT_USTRING("0A2345678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<10>(0xa2345678), MPT_USTRING("00A2345678"));
|
||||
|
||||
#if MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_chars<std::string>(std::numeric_limits<int16>::min(), 10), "-32768");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_chars<std::string>(std::numeric_limits<int16>::max(), 10), "32767");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_chars<std::string>(std::numeric_limits<int16>::min(), 7), "-164351");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_chars<std::string>(std::numeric_limits<int16>::min() + 1, 7), "-164350");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_chars<std::string>(std::numeric_limits<int16>::max(), 7), "164350");
|
||||
|
||||
#else // !MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_stream<std::string>(std::numeric_limits<int16>::min(), 10), "-32768");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_stream<std::string>(std::numeric_limits<int16>::max(), 10), "32767");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_stream<std::string>(std::numeric_limits<int16>::min(), 7), "-164351");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_stream<std::string>(std::numeric_limits<int16>::min() + 1, 7), "-164350");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format_simple_integer_to_stream<std::string>(std::numeric_limits<int16>::max(), 7), "164350");
|
||||
|
||||
#endif // MPT_FORMAT_FORMAT_SIMPLE_INT_CXX17
|
||||
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::wstring>::hex(0x123e), L"123e");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::wstring>::hex0<6>(0x123e), L"00123e");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::wstring>::hex0<2>(0x123e), L"123e");
|
||||
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::val(-87.0f), "-87");
|
||||
if (mpt::format<std::string>::val(-0.5e-6) != "-5e-007"
|
||||
&& mpt::format<std::string>::val(-0.5e-6) != "-5e-07"
|
||||
&& mpt::format<std::string>::val(-0.5e-6) != "-5e-7"
|
||||
&& mpt::format<std::string>::val(-0.5e-6) != "-4.9999999999999998e-7"
|
||||
&& mpt::format<std::string>::val(-0.5e-6) != "-4.9999999999999998e-07"
|
||||
&& mpt::format<std::string>::val(-0.5e-6) != "-4.9999999999999998e-007")
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(true, false);
|
||||
}
|
||||
if (mpt::format<std::string>::val(-1.0 / 65536.0) != "-1.52587890625e-005"
|
||||
&& mpt::format<std::string>::val(-1.0 / 65536.0) != "-1.52587890625e-05"
|
||||
&& mpt::format<std::string>::val(-1.0 / 65536.0) != "-1.52587890625e-5")
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(true, false);
|
||||
}
|
||||
if (mpt::format<std::string>::val(-1.0f / 65536.0f) != "-1.52587891e-005"
|
||||
&& mpt::format<std::string>::val(-1.0f / 65536.0f) != "-1.52587891e-05"
|
||||
&& mpt::format<std::string>::val(-1.0f / 65536.0f) != "-1.52587891e-5"
|
||||
&& mpt::format<std::string>::val(-1.0f / 65536.0f) != "-1.5258789e-005"
|
||||
&& mpt::format<std::string>::val(-1.0f / 65536.0f) != "-1.5258789e-05"
|
||||
&& mpt::format<std::string>::val(-1.0f / 65536.0f) != "-1.5258789e-5")
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(true, false);
|
||||
}
|
||||
if (mpt::format<std::string>::val(58.65403492763) != "58.654034927630001"
|
||||
&& mpt::format<std::string>::val(58.65403492763) != "58.65403492763")
|
||||
{
|
||||
MPT_TEST_EXPECT_EQUAL(true, false);
|
||||
}
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::flt(58.65403492763, 6), "58.654");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::fix(23.42, 1), "23.4");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::fix(234.2, 1), "234.2");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::fix(2342.0, 1), "2342.0");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec(2, ";", 2345678), std::string("2;34;56;78"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::dec(2, ";", 12345678), std::string("12;34;56;78"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::hex(3, ":", 0xa2345678), std::string("a2:345:678"));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::dec(2, MPT_USTRING(";"), 12345678), MPT_USTRING("12;34;56;78"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::hex(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("a2:345:678"));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<7>(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("A2:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<8>(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("A2:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<9>(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("0A2:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<10>(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("0:0A2:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<11>(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("00:0A2:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<12>(3, MPT_USTRING(":"), 0xa2345678), MPT_USTRING("000:0A2:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<7>(3, MPT_USTRING(":"), -0x12345678), MPT_USTRING("-12:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<8>(3, MPT_USTRING(":"), -0x12345678), MPT_USTRING("-12:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<9>(3, MPT_USTRING(":"), -0x12345678), MPT_USTRING("-012:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<10>(3, MPT_USTRING(":"), -0x12345678), MPT_USTRING("-0:012:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<11>(3, MPT_USTRING(":"), -0x12345678), MPT_USTRING("-00:012:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<12>(3, MPT_USTRING(":"), -0x12345678), MPT_USTRING("-000:012:345:678"));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<5>(3, MPT_USTRING(":"), 0x345678), MPT_USTRING("345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<6>(3, MPT_USTRING(":"), 0x345678), MPT_USTRING("345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<7>(3, MPT_USTRING(":"), 0x345678), MPT_USTRING("0:345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<5>(3, MPT_USTRING(":"), -0x345678), MPT_USTRING("-345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<6>(3, MPT_USTRING(":"), -0x345678), MPT_USTRING("-345:678"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<mpt::ustring>::HEX0<7>(3, MPT_USTRING(":"), -0x345678), MPT_USTRING("-0:345:678"));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::left(3, "a"), "a ");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::right(3, "a"), " a");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::center(3, "a"), " a ");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::center(4, "a"), " a ");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::flt(6.12345, 3), "6.12");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::fix(6.12345, 3), "6.123");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::flt(6.12345, 4), "6.123");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::string>::fix(6.12345, 4), "6.1235");
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::u32string>::val(std::u32string(U"foo")), U"foo");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::u32string>::val(std::u32string_view(U"foo")), U"foo");
|
||||
|
||||
#if MPT_DETECTED_MFC
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::flt(58.65403492763, 6), TEXT("58.654"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::fix(23.42, 1), TEXT("23.4"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::fix(234.2, 1), TEXT("234.2"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::fix(2342.0, 1), TEXT("2342.0"));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::dec(2, TEXT(";"), 2345678), CString(TEXT("2;34;56;78")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::dec(2, TEXT(";"), 12345678), CString(TEXT("12;34;56;78")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::hex(3, TEXT(":"), 0xa2345678), CString(TEXT("a2:345:678")));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::dec(2, TEXT(";"), 12345678), CString(TEXT("12;34;56;78")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::hex(3, TEXT(":"), 0xa2345678), CString(TEXT("a2:345:678")));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<7>(3, TEXT(":"), 0xa2345678), CString(TEXT("A2:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<8>(3, TEXT(":"), 0xa2345678), CString(TEXT("A2:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<9>(3, TEXT(":"), 0xa2345678), CString(TEXT("0A2:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<10>(3, TEXT(":"), 0xa2345678), CString(TEXT("0:0A2:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<11>(3, TEXT(":"), 0xa2345678), CString(TEXT("00:0A2:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<12>(3, TEXT(":"), 0xa2345678), CString(TEXT("000:0A2:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<7>(3, TEXT(":"), -0x12345678), CString(TEXT("-12:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<8>(3, TEXT(":"), -0x12345678), CString(TEXT("-12:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<9>(3, TEXT(":"), -0x12345678), CString(TEXT("-012:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<10>(3, TEXT(":"), -0x12345678), CString(TEXT("-0:012:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<11>(3, TEXT(":"), -0x12345678), CString(TEXT("-00:012:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<12>(3, TEXT(":"), -0x12345678), CString(TEXT("-000:012:345:678")));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<5>(3, TEXT(":"), 0x345678), CString(TEXT("345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<6>(3, TEXT(":"), 0x345678), CString(TEXT("345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<7>(3, TEXT(":"), 0x345678), CString(TEXT("0:345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<5>(3, TEXT(":"), -0x345678), CString(TEXT("-345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<6>(3, TEXT(":"), -0x345678), CString(TEXT("-345:678")));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::HEX0<7>(3, TEXT(":"), -0x345678), CString(TEXT("-0:345:678")));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::left(3, TEXT("a")), TEXT("a "));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::right(3, TEXT("a")), TEXT(" a"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::center(3, TEXT("a")), TEXT(" a "));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::center(4, TEXT("a")), TEXT(" a "));
|
||||
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::flt(6.12345, 3), TEXT("6.12"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::fix(6.12345, 3), TEXT("6.123"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::flt(6.12345, 4), TEXT("6.123"));
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<CString>::fix(6.12345, 4), TEXT("6.1235"));
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR)
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::wstring>::flt(6.12345, 3), L"6.12");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::wstring>::fix(6.12345, 3), L"6.123");
|
||||
MPT_TEST_EXPECT_EQUAL(mpt::format<std::wstring>::flt(6.12345, 4), L"6.123");
|
||||
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR
|
||||
}
|
||||
|
||||
} // namespace simple
|
||||
} // namespace format
|
||||
} // namespace tests
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_FORMAT_TESTS_FORMAT_SIMPLE_HPP
|
||||
@@ -0,0 +1,122 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_IO_BASE_HPP
|
||||
#define MPT_IO_BASE_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace IO {
|
||||
|
||||
|
||||
|
||||
using Offset = int64;
|
||||
|
||||
inline constexpr std::size_t BUFFERSIZE_MINUSCULE = 1 * 256; // on stack usage, tuned for single word/line buffers
|
||||
inline constexpr std::size_t BUFFERSIZE_TINY = 1 * 1024; // on stack usage
|
||||
inline constexpr std::size_t BUFFERSIZE_SMALL = 4 * 1024; // on heap
|
||||
inline constexpr std::size_t BUFFERSIZE_NORMAL = 64 * 1024; // FILE I/O
|
||||
inline constexpr std::size_t BUFFERSIZE_LARGE = 1024 * 1024;
|
||||
|
||||
|
||||
|
||||
template <typename Tfile, typename Enable = void>
|
||||
struct FileOperations {
|
||||
};
|
||||
|
||||
template <typename Tfile>
|
||||
inline FileOperations<Tfile> FileOps(Tfile & f) {
|
||||
return FileOperations<Tfile>{f};
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool IsValid(Tfile & f) {
|
||||
return FileOps(f).IsValid();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool IsReadSeekable(Tfile & f) {
|
||||
return FileOps(f).IsReadSeekable();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool IsWriteSeekable(Tfile & f) {
|
||||
return FileOps(f).IsWriteSeekable();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline IO::Offset TellRead(Tfile & f) {
|
||||
return FileOps(f).TellRead();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline IO::Offset TellWrite(Tfile & f) {
|
||||
return FileOps(f).TellWrite();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool SeekBegin(Tfile & f) {
|
||||
return FileOps(f).SeekBegin();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool SeekEnd(Tfile & f) {
|
||||
return FileOps(f).SeekEnd();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool SeekAbsolute(Tfile & f, IO::Offset pos) {
|
||||
return FileOps(f).SeekAbsolute(pos);
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool SeekRelative(Tfile & f, IO::Offset off) {
|
||||
return FileOps(f).SeekRelative(off);
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline mpt::byte_span ReadRawImpl(Tfile & f, mpt::byte_span data) {
|
||||
return FileOps(f).ReadRawImpl(data);
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteRawImpl(Tfile & f, mpt::const_byte_span data) {
|
||||
return FileOps(f).WriteRawImpl(data);
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool IsEof(Tfile & f) {
|
||||
return FileOps(f).IsEof();
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool Flush(Tfile & f) {
|
||||
return FileOps(f).Flush();
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace IO
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_IO_BASE_HPP
|
||||
@@ -0,0 +1,368 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_IO_IO_HPP
|
||||
#define MPT_IO_IO_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/array.hpp"
|
||||
#include "mpt/base/bit.hpp"
|
||||
#include "mpt/base/integer.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/span.hpp"
|
||||
#include "mpt/endian/integer.hpp"
|
||||
#include "mpt/endian/type_traits.hpp"
|
||||
#include "mpt/io/base.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace IO {
|
||||
|
||||
|
||||
|
||||
template <typename Tbyte, typename Tfile>
|
||||
inline mpt::byte_span ReadRaw(Tfile & f, Tbyte * data, std::size_t size) {
|
||||
return mpt::IO::ReadRawImpl(f, mpt::as_span(mpt::byte_cast<std::byte *>(data), size));
|
||||
}
|
||||
|
||||
template <typename Tbyte, typename Tfile>
|
||||
inline mpt::byte_span ReadRaw(Tfile & f, mpt::span<Tbyte> data) {
|
||||
return mpt::IO::ReadRawImpl(f, mpt::byte_cast<mpt::byte_span>(data));
|
||||
}
|
||||
|
||||
template <typename Tbyte, typename Tfile>
|
||||
inline bool WriteRaw(Tfile & f, const Tbyte * data, std::size_t size) {
|
||||
return mpt::IO::WriteRawImpl(f, mpt::as_span(mpt::byte_cast<const std::byte *>(data), size));
|
||||
}
|
||||
|
||||
template <typename Tbyte, typename Tfile>
|
||||
inline bool WriteRaw(Tfile & f, mpt::span<Tbyte> data) {
|
||||
return mpt::IO::WriteRawImpl(f, mpt::byte_cast<mpt::const_byte_span>(data));
|
||||
}
|
||||
|
||||
template <typename Tbinary, typename Tfile>
|
||||
inline bool Read(Tfile & f, Tbinary & v) {
|
||||
return mpt::IO::ReadRaw(f, mpt::as_raw_memory(v)).size() == mpt::as_raw_memory(v).size();
|
||||
}
|
||||
|
||||
template <typename Tbinary, typename Tfile>
|
||||
inline bool Write(Tfile & f, const Tbinary & v) {
|
||||
return mpt::IO::WriteRaw(f, mpt::as_raw_memory(v));
|
||||
}
|
||||
|
||||
template <typename Tbinary, typename Tfile>
|
||||
inline bool Write(Tfile & f, const std::vector<Tbinary> & v) {
|
||||
static_assert(mpt::is_binary_safe<Tbinary>::value);
|
||||
return mpt::IO::WriteRaw(f, mpt::as_raw_memory(v));
|
||||
}
|
||||
|
||||
template <typename T, typename Tfile>
|
||||
inline bool WritePartial(Tfile & f, const T & v, std::size_t size = sizeof(T)) {
|
||||
assert(size <= sizeof(T));
|
||||
return mpt::IO::WriteRaw(f, mpt::as_span(mpt::as_raw_memory(v).data(), size));
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool ReadByte(Tfile & f, std::byte & v) {
|
||||
bool result = false;
|
||||
std::byte byte = mpt::as_byte(0);
|
||||
const std::size_t readResult = mpt::IO::ReadRaw(f, &byte, sizeof(std::byte)).size();
|
||||
result = (readResult == sizeof(std::byte));
|
||||
v = byte;
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T, typename Tfile>
|
||||
inline bool ReadBinaryTruncatedLE(Tfile & f, T & v, std::size_t size) {
|
||||
bool result = false;
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
std::array<uint8, sizeof(T)> bytes = mpt::init_array<uint8, sizeof(T)>(uint8{0});
|
||||
const std::size_t readResult = mpt::IO::ReadRaw(f, bytes.data(), std::min(size, sizeof(T))).size();
|
||||
result = (readResult == std::min(size, sizeof(T)));
|
||||
v = mpt::bit_cast<typename mpt::make_le<T>::type>(bytes);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T, typename Tfile>
|
||||
inline bool ReadIntLE(Tfile & f, T & v) {
|
||||
bool result = false;
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
std::array<uint8, sizeof(T)> bytes = mpt::init_array<uint8, sizeof(T)>(uint8{0});
|
||||
const std::size_t readResult = mpt::IO::ReadRaw(f, mpt::as_span(bytes)).size();
|
||||
result = (readResult == sizeof(T));
|
||||
v = mpt::bit_cast<typename mpt::make_le<T>::type>(bytes);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T, typename Tfile>
|
||||
inline bool ReadIntBE(Tfile & f, T & v) {
|
||||
bool result = false;
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
std::array<uint8, sizeof(T)> bytes = mpt::init_array<uint8, sizeof(T)>(uint8{0});
|
||||
const std::size_t readResult = mpt::IO::ReadRaw(f, mpt::as_span(bytes)).size();
|
||||
result = (readResult == sizeof(T));
|
||||
v = mpt::bit_cast<typename mpt::make_be<T>::type>(bytes);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool ReadAdaptiveInt16LE(Tfile & f, uint16 & v) {
|
||||
bool result = true;
|
||||
uint8 byte = 0;
|
||||
std::size_t additionalBytes = 0;
|
||||
v = 0;
|
||||
byte = 0;
|
||||
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) {
|
||||
result = false;
|
||||
}
|
||||
additionalBytes = (byte & 0x01);
|
||||
v = byte >> 1;
|
||||
for (std::size_t i = 0; i < additionalBytes; ++i) {
|
||||
byte = 0;
|
||||
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) {
|
||||
result = false;
|
||||
}
|
||||
v |= (static_cast<uint16>(byte) << (((i + 1) * 8) - 1));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool ReadAdaptiveInt32LE(Tfile & f, uint32 & v) {
|
||||
bool result = true;
|
||||
uint8 byte = 0;
|
||||
std::size_t additionalBytes = 0;
|
||||
v = 0;
|
||||
byte = 0;
|
||||
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) {
|
||||
result = false;
|
||||
}
|
||||
additionalBytes = (byte & 0x03);
|
||||
v = byte >> 2;
|
||||
for (std::size_t i = 0; i < additionalBytes; ++i) {
|
||||
byte = 0;
|
||||
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) {
|
||||
result = false;
|
||||
}
|
||||
v |= (static_cast<uint32>(byte) << (((i + 1) * 8) - 2));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool ReadAdaptiveInt64LE(Tfile & f, uint64 & v) {
|
||||
bool result = true;
|
||||
uint8 byte = 0;
|
||||
std::size_t additionalBytes = 0;
|
||||
v = 0;
|
||||
byte = 0;
|
||||
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) {
|
||||
result = false;
|
||||
}
|
||||
additionalBytes = (1 << (byte & 0x03)) - 1;
|
||||
v = byte >> 2;
|
||||
for (std::size_t i = 0; i < additionalBytes; ++i) {
|
||||
byte = 0;
|
||||
if (!mpt::IO::ReadIntLE<uint8>(f, byte)) {
|
||||
result = false;
|
||||
}
|
||||
v |= (static_cast<uint64>(byte) << (((i + 1) * 8) - 2));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Tsize, typename Tfile>
|
||||
inline bool ReadSizedStringLE(Tfile & f, std::string & str, Tsize maxSize = std::numeric_limits<Tsize>::max()) {
|
||||
static_assert(std::numeric_limits<Tsize>::is_integer);
|
||||
str.clear();
|
||||
Tsize size = 0;
|
||||
if (!mpt::IO::ReadIntLE(f, size)) {
|
||||
return false;
|
||||
}
|
||||
if (size > maxSize) {
|
||||
return false;
|
||||
}
|
||||
for (Tsize i = 0; i != size; ++i) {
|
||||
char c = '\0';
|
||||
if (!mpt::IO::ReadIntLE(f, c)) {
|
||||
return false;
|
||||
}
|
||||
str.push_back(c);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T, typename Tfile>
|
||||
inline bool WriteIntLE(Tfile & f, const T v) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::IO::Write(f, mpt::as_le(v));
|
||||
}
|
||||
|
||||
template <typename T, typename Tfile>
|
||||
inline bool WriteIntBE(Tfile & f, const T v) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
return mpt::IO::Write(f, mpt::as_be(v));
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteAdaptiveInt16LE(Tfile & f, const uint16 v, std::size_t fixedSize = 0) {
|
||||
std::size_t minSize = fixedSize;
|
||||
std::size_t maxSize = fixedSize;
|
||||
assert(minSize == 0 || minSize == 1 || minSize == 2);
|
||||
assert(maxSize == 0 || maxSize == 1 || maxSize == 2);
|
||||
assert(maxSize == 0 || maxSize >= minSize);
|
||||
if (maxSize == 0) {
|
||||
maxSize = 2;
|
||||
}
|
||||
if (v < 0x80 && minSize <= 1 && 1 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint8>(f, static_cast<uint8>(v << 1) | 0x00);
|
||||
} else if (v < 0x8000 && minSize <= 2 && 2 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint16>(f, static_cast<uint16>(v << 1) | 0x01);
|
||||
} else {
|
||||
assert(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteAdaptiveInt32LE(Tfile & f, const uint32 v, std::size_t fixedSize = 0) {
|
||||
std::size_t minSize = fixedSize;
|
||||
std::size_t maxSize = fixedSize;
|
||||
assert(minSize == 0 || minSize == 1 || minSize == 2 || minSize == 3 || minSize == 4);
|
||||
assert(maxSize == 0 || maxSize == 1 || maxSize == 2 || maxSize == 3 || maxSize == 4);
|
||||
assert(maxSize == 0 || maxSize >= minSize);
|
||||
if (maxSize == 0) {
|
||||
maxSize = 4;
|
||||
}
|
||||
if (v < 0x40 && minSize <= 1 && 1 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint8>(f, static_cast<uint8>(v << 2) | 0x00);
|
||||
} else if (v < 0x4000 && minSize <= 2 && 2 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint16>(f, static_cast<uint16>(v << 2) | 0x01);
|
||||
} else if (v < 0x400000 && minSize <= 3 && 3 <= maxSize) {
|
||||
uint32 value = static_cast<uint32>(v << 2) | 0x02;
|
||||
std::byte bytes[3];
|
||||
bytes[0] = static_cast<std::byte>(value >> 0);
|
||||
bytes[1] = static_cast<std::byte>(value >> 8);
|
||||
bytes[2] = static_cast<std::byte>(value >> 16);
|
||||
return mpt::IO::WriteRaw(f, bytes, 3);
|
||||
} else if (v < 0x40000000 && minSize <= 4 && 4 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint32>(f, static_cast<uint32>(v << 2) | 0x03);
|
||||
} else {
|
||||
assert(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteAdaptiveInt64LE(Tfile & f, const uint64 v, std::size_t fixedSize = 0) {
|
||||
std::size_t minSize = fixedSize;
|
||||
std::size_t maxSize = fixedSize;
|
||||
assert(minSize == 0 || minSize == 1 || minSize == 2 || minSize == 4 || minSize == 8);
|
||||
assert(maxSize == 0 || maxSize == 1 || maxSize == 2 || maxSize == 4 || maxSize == 8);
|
||||
assert(maxSize == 0 || maxSize >= minSize);
|
||||
if (maxSize == 0) {
|
||||
maxSize = 8;
|
||||
}
|
||||
if (v < 0x40 && minSize <= 1 && 1 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint8>(f, static_cast<uint8>(v << 2) | 0x00);
|
||||
} else if (v < 0x4000 && minSize <= 2 && 2 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint16>(f, static_cast<uint16>(v << 2) | 0x01);
|
||||
} else if (v < 0x40000000 && minSize <= 4 && 4 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint32>(f, static_cast<uint32>(v << 2) | 0x02);
|
||||
} else if (v < 0x4000000000000000ull && minSize <= 8 && 8 <= maxSize) {
|
||||
return mpt::IO::WriteIntLE<uint64>(f, static_cast<uint64>(v << 2) | 0x03);
|
||||
} else {
|
||||
assert(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Write a variable-length integer, as found in MIDI files. The number of written bytes is placed in the bytesWritten parameter.
|
||||
template <typename Tfile, typename T>
|
||||
bool WriteVarInt(Tfile & f, const T v, std::size_t * bytesWritten = nullptr) {
|
||||
static_assert(std::numeric_limits<T>::is_integer);
|
||||
static_assert(!std::numeric_limits<T>::is_signed);
|
||||
std::byte out[(sizeof(T) * 8 + 6) / 7];
|
||||
std::size_t numBytes = 0;
|
||||
for (uint32 n = (sizeof(T) * 8) / 7; n > 0; n--) {
|
||||
if (v >= (static_cast<T>(1) << (n * 7u))) {
|
||||
out[numBytes++] = static_cast<std::byte>(((v >> (n * 7u)) & 0x7F) | 0x80);
|
||||
}
|
||||
}
|
||||
out[numBytes++] = static_cast<std::byte>(v & 0x7F);
|
||||
assert(numBytes <= std::size(out));
|
||||
if (bytesWritten != nullptr) {
|
||||
*bytesWritten = numBytes;
|
||||
}
|
||||
return mpt::IO::WriteRaw(f, out, numBytes);
|
||||
}
|
||||
|
||||
template <typename Tsize, typename Tfile>
|
||||
inline bool WriteSizedStringLE(Tfile & f, const std::string & str) {
|
||||
static_assert(std::numeric_limits<Tsize>::is_integer);
|
||||
if (str.size() > std::numeric_limits<Tsize>::max()) {
|
||||
return false;
|
||||
}
|
||||
Tsize size = static_cast<Tsize>(str.size());
|
||||
if (!mpt::IO::WriteIntLE(f, size)) {
|
||||
return false;
|
||||
}
|
||||
if (!mpt::IO::WriteRaw(f, str.data(), str.size())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteText(Tfile & f, const std::string & s) {
|
||||
return mpt::IO::WriteRaw(f, s.data(), s.size());
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteTextCRLF(Tfile & f) {
|
||||
return mpt::IO::WriteText(f, "\r\n");
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteTextLF(Tfile & f) {
|
||||
return mpt::IO::WriteText(f, "\n");
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteTextCRLF(Tfile & f, const std::string & s) {
|
||||
return mpt::IO::WriteText(f, s) && mpt::IO::WriteTextCRLF(f);
|
||||
}
|
||||
|
||||
template <typename Tfile>
|
||||
inline bool WriteTextLF(Tfile & f, const std::string & s) {
|
||||
return mpt::IO::WriteText(f, s) && mpt::IO::WriteTextLF(f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace IO
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_IO_IO_HPP
|
||||
@@ -0,0 +1,166 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_IO_IO_SPAN_HPP
|
||||
#define MPT_IO_IO_SPAN_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/saturate_cast.hpp"
|
||||
#include "mpt/base/span.hpp"
|
||||
#include "mpt/io/base.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace IO {
|
||||
|
||||
|
||||
|
||||
template <typename Tbyte>
|
||||
struct FileOperations<std::pair<mpt::span<Tbyte>, IO::Offset>> {
|
||||
|
||||
private:
|
||||
std::pair<mpt::span<Tbyte>, IO::Offset> & f;
|
||||
|
||||
public:
|
||||
FileOperations(std::pair<mpt::span<Tbyte>, IO::Offset> & f_)
|
||||
: f(f_) {
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool IsValid() {
|
||||
return (f.second >= 0);
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool IsReadSeekable() {
|
||||
MPT_UNUSED(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool IsWriteSeekable() {
|
||||
MPT_UNUSED(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline IO::Offset TellRead() {
|
||||
return f.second;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline IO::Offset TellWrite() {
|
||||
return f.second;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekBegin() {
|
||||
f.second = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekEnd() {
|
||||
f.second = f.first.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekAbsolute(IO::Offset pos) {
|
||||
f.second = pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekRelative(IO::Offset off) {
|
||||
if (f.second < 0) {
|
||||
return false;
|
||||
}
|
||||
f.second += off;
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline mpt::byte_span ReadRawImpl(mpt::byte_span data) {
|
||||
if (f.second < 0) {
|
||||
return data.first(0);
|
||||
}
|
||||
if (f.second >= static_cast<IO::Offset>(f.first.size())) {
|
||||
return data.first(0);
|
||||
}
|
||||
std::size_t num = mpt::saturate_cast<std::size_t>(std::min(static_cast<IO::Offset>(f.first.size()) - f.second, static_cast<IO::Offset>(data.size())));
|
||||
std::copy(mpt::byte_cast<const std::byte *>(f.first.data() + f.second), mpt::byte_cast<const std::byte *>(f.first.data() + f.second + num), data.data());
|
||||
f.second += num;
|
||||
return data.first(num);
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool WriteRawImpl(mpt::const_byte_span data) {
|
||||
if (f.second < 0) {
|
||||
return false;
|
||||
}
|
||||
if (f.second > static_cast<IO::Offset>(f.first.size())) {
|
||||
return false;
|
||||
}
|
||||
std::size_t num = mpt::saturate_cast<std::size_t>(std::min(static_cast<IO::Offset>(f.first.size()) - f.second, static_cast<IO::Offset>(data.size())));
|
||||
if (num != data.size()) {
|
||||
return false;
|
||||
}
|
||||
std::copy(data.data(), data.data() + num, mpt::byte_cast<std::byte *>(f.first.data() + f.second));
|
||||
f.second += num;
|
||||
return true;
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool IsEof() {
|
||||
return (f.second >= static_cast<IO::Offset>(f.first.size()));
|
||||
}
|
||||
|
||||
// cppcheck false-positive
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool Flush() {
|
||||
MPT_UNUSED(f);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace IO
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_IO_IO_SPAN_HPP
|
||||
@@ -0,0 +1,344 @@
|
||||
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
|
||||
|
||||
#ifndef MPT_IO_IO_STDSTREAM_HPP
|
||||
#define MPT_IO_IO_STDSTREAM_HPP
|
||||
|
||||
|
||||
|
||||
#include "mpt/base/macros.hpp"
|
||||
#include "mpt/base/memory.hpp"
|
||||
#include "mpt/base/namespace.hpp"
|
||||
#include "mpt/base/saturate_cast.hpp"
|
||||
#include "mpt/base/utility.hpp"
|
||||
#include "mpt/io/base.hpp"
|
||||
|
||||
#include <ios>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
|
||||
namespace mpt {
|
||||
inline namespace MPT_INLINE_NS {
|
||||
|
||||
|
||||
|
||||
namespace IO {
|
||||
|
||||
|
||||
|
||||
struct FileOperationsStdIos {
|
||||
|
||||
private:
|
||||
std::ios & f;
|
||||
|
||||
public:
|
||||
inline FileOperationsStdIos(std::ios & f_)
|
||||
: f(f_) {
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
inline bool IsValid() {
|
||||
return !f.fail();
|
||||
}
|
||||
};
|
||||
|
||||
struct FileOperationsStdIstream
|
||||
: public FileOperationsStdIos {
|
||||
|
||||
private:
|
||||
std::istream & f;
|
||||
|
||||
public:
|
||||
inline FileOperationsStdIstream(std::istream & f_)
|
||||
: FileOperationsStdIos(f_)
|
||||
, f(f_) {
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
inline bool IsReadSeekable() {
|
||||
f.clear();
|
||||
std::streampos oldpos = f.tellg();
|
||||
if (f.fail() || oldpos == std::streampos(std::streamoff(-1))) {
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
f.seekg(0, std::ios::beg);
|
||||
if (f.fail()) {
|
||||
f.clear();
|
||||
f.seekg(oldpos);
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
f.seekg(0, std::ios::end);
|
||||
if (f.fail()) {
|
||||
f.clear();
|
||||
f.seekg(oldpos);
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
std::streampos length = f.tellg();
|
||||
if (f.fail() || length == std::streampos(std::streamoff(-1))) {
|
||||
f.clear();
|
||||
f.seekg(oldpos);
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
f.seekg(oldpos);
|
||||
f.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
inline IO::Offset TellRead() {
|
||||
return static_cast<std::streamoff>(f.tellg());
|
||||
}
|
||||
|
||||
inline bool SeekBegin() {
|
||||
f.seekg(0, std::ios::beg);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool SeekEnd() {
|
||||
f.seekg(0, std::ios::end);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool SeekAbsolute(IO::Offset pos) {
|
||||
if (!mpt::in_range<std::streamoff>(pos)) {
|
||||
return false;
|
||||
}
|
||||
f.seekg(static_cast<std::streamoff>(pos), std::ios::beg);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool SeekRelative(IO::Offset off) {
|
||||
if (!mpt::in_range<std::streamoff>(off)) {
|
||||
return false;
|
||||
}
|
||||
f.seekg(static_cast<std::streamoff>(off), std::ios::cur);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline mpt::byte_span ReadRawImpl(mpt::byte_span data) {
|
||||
std::size_t bytesToRead = data.size();
|
||||
std::size_t bytesRead = 0;
|
||||
while (bytesToRead > 0) {
|
||||
std::streamsize bytesChunkToRead = mpt::saturate_cast<std::streamsize>(bytesToRead);
|
||||
f.read(mpt::byte_cast<char *>(data.data()) + bytesRead, bytesChunkToRead);
|
||||
std::streamsize bytesChunkRead = f.gcount();
|
||||
bytesRead += static_cast<std::size_t>(bytesChunkRead);
|
||||
bytesToRead -= static_cast<std::size_t>(bytesChunkRead);
|
||||
if (bytesChunkRead != bytesChunkToRead) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return data.first(bytesRead);
|
||||
}
|
||||
|
||||
inline bool IsEof() {
|
||||
return f.eof();
|
||||
}
|
||||
};
|
||||
|
||||
struct FileOperationsStdOstream
|
||||
: public FileOperationsStdIos {
|
||||
|
||||
private:
|
||||
std::ostream & f;
|
||||
|
||||
public:
|
||||
inline FileOperationsStdOstream(std::ostream & f_)
|
||||
: FileOperationsStdIos(f_)
|
||||
, f(f_) {
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
inline bool IsWriteSeekable() {
|
||||
f.clear();
|
||||
std::streampos oldpos = f.tellp();
|
||||
if (f.fail() || oldpos == std::streampos(std::streamoff(-1))) {
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
f.seekp(0, std::ios::beg);
|
||||
if (f.fail()) {
|
||||
f.clear();
|
||||
f.seekp(oldpos);
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
f.seekp(0, std::ios::end);
|
||||
if (f.fail()) {
|
||||
f.clear();
|
||||
f.seekp(oldpos);
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
std::streampos length = f.tellp();
|
||||
if (f.fail() || length == std::streampos(std::streamoff(-1))) {
|
||||
f.clear();
|
||||
f.seekp(oldpos);
|
||||
f.clear();
|
||||
return false;
|
||||
}
|
||||
f.seekp(oldpos);
|
||||
f.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
inline IO::Offset TellWrite() {
|
||||
return static_cast<std::streamoff>(f.tellp());
|
||||
}
|
||||
|
||||
inline bool SeekBegin() {
|
||||
f.seekp(0, std::ios::beg);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool SeekEnd() {
|
||||
f.seekp(0, std::ios::end);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool SeekAbsolute(IO::Offset pos) {
|
||||
if (!mpt::in_range<std::streamoff>(pos)) {
|
||||
return false;
|
||||
}
|
||||
f.seekp(static_cast<std::streamoff>(pos), std::ios::beg);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool SeekRelative(IO::Offset off) {
|
||||
if (!mpt::in_range<std::streamoff>(off)) {
|
||||
return false;
|
||||
}
|
||||
f.seekp(static_cast<std::streamoff>(off), std::ios::cur);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool WriteRawImpl(mpt::const_byte_span data) {
|
||||
std::size_t bytesToWrite = data.size();
|
||||
std::size_t bytesWritten = 0;
|
||||
while (bytesToWrite > 0) {
|
||||
std::streamsize bytesChunk = mpt::saturate_cast<std::streamsize>(bytesToWrite);
|
||||
f.write(mpt::byte_cast<const char *>(data.data()) + bytesWritten, bytesChunk);
|
||||
if (f.fail()) {
|
||||
break;
|
||||
}
|
||||
bytesWritten += static_cast<std::size_t>(bytesChunk);
|
||||
bytesToWrite -= static_cast<std::size_t>(bytesChunk);
|
||||
}
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
inline bool Flush() {
|
||||
f.flush();
|
||||
return !f.fail();
|
||||
}
|
||||
};
|
||||
|
||||
struct FileOperationsStdIOstream
|
||||
: public FileOperationsStdIstream
|
||||
, public FileOperationsStdOstream {
|
||||
|
||||
private:
|
||||
std::iostream & f;
|
||||
|
||||
public:
|
||||
inline FileOperationsStdIOstream(std::iostream & f_)
|
||||
: FileOperationsStdIstream(f_)
|
||||
, FileOperationsStdOstream(f_)
|
||||
, f(f_) {
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekBegin() {
|
||||
FileOperationsStdIstream::SeekBegin();
|
||||
FileOperationsStdOstream::SeekBegin();
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekEnd() {
|
||||
FileOperationsStdIstream::SeekEnd();
|
||||
FileOperationsStdOstream::SeekEnd();
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekAbsolute(IO::Offset pos) {
|
||||
if (!mpt::in_range<std::streamoff>(pos)) {
|
||||
return false;
|
||||
}
|
||||
FileOperationsStdIstream::SeekAbsolute(pos);
|
||||
FileOperationsStdOstream::SeekAbsolute(pos);
|
||||
return !f.fail();
|
||||
}
|
||||
|
||||
// cppcheck-suppress duplInheritedMember
|
||||
inline bool SeekRelative(IO::Offset off) {
|
||||
if (!mpt::in_range<std::streamoff>(off)) {
|
||||
return false;
|
||||
}
|
||||
FileOperationsStdIstream::SeekRelative(off);
|
||||
FileOperationsStdOstream::SeekRelative(off);
|
||||
return !f.fail();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename Tstream>
|
||||
struct FileOperations<Tstream, typename std::enable_if_t<std::is_base_of<std::iostream, Tstream>::value>>
|
||||
: public FileOperationsStdIOstream {
|
||||
public:
|
||||
inline FileOperations(Tstream & f)
|
||||
: FileOperationsStdIOstream(f) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename Tstream>
|
||||
struct FileOperations<Tstream, typename std::enable_if_t<std::is_base_of<std::istream, Tstream>::value && !std::is_base_of<std::iostream, Tstream>::value>>
|
||||
: public FileOperationsStdIstream {
|
||||
public:
|
||||
inline FileOperations(Tstream & f)
|
||||
: FileOperationsStdIstream(f) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename Tstream>
|
||||
struct FileOperations<Tstream, typename std::enable_if_t<std::is_base_of<std::ostream, Tstream>::value && !std::is_base_of<std::iostream, Tstream>::value>>
|
||||
: public FileOperationsStdOstream {
|
||||
public:
|
||||
inline FileOperations(Tstream & f)
|
||||
: FileOperationsStdOstream(f) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace IO
|
||||
|
||||
|
||||
|
||||
} // namespace MPT_INLINE_NS
|
||||
} // namespace mpt
|
||||
|
||||
|
||||
|
||||
#endif // MPT_IO_IO_STDSTREAM_HPP
|
||||
Loaded 100 of 780 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user