diff options
-rw-r--r-- | deimos/core/BUILD | 1 | ||||
-rw-r--r-- | deimos/core/allocator.cpp | 6 | ||||
-rw-r--r-- | deimos/core/allocator.h | 12 | ||||
-rw-r--r-- | deimos/core/base.h | 45 | ||||
-rw-r--r-- | deimos/core/gsl.h | 10 | ||||
-rw-r--r-- | deimos/core/hash.h | 80 | ||||
-rw-r--r-- | deimos/core/id_name.h | 2 | ||||
-rw-r--r-- | deimos/core/os.h | 4 | ||||
-rw-r--r-- | deimos/core/os_win32.cpp | 2 | ||||
-rw-r--r-- | deimos/core/std.h | 24 |
10 files changed, 97 insertions, 89 deletions
diff --git a/deimos/core/BUILD b/deimos/core/BUILD index 134b236..d004aae 100644 --- a/deimos/core/BUILD +++ b/deimos/core/BUILD @@ -4,6 +4,7 @@ cc_library( "allocator.h",
"api_registry.h",
"base.h",
+ "gsl.h",
"hash.h",
"id_name.h",
"os.h",
diff --git a/deimos/core/allocator.cpp b/deimos/core/allocator.cpp index 1478d36..1a823c1 100644 --- a/deimos/core/allocator.cpp +++ b/deimos/core/allocator.cpp @@ -9,12 +9,12 @@ class SystemAllocatorImpl : public IAllocator {
public:
void* Reallocate(
- void* old_ptr, int64 /* old_size */, int64 new_size,
+ void* old_ptr, int64_t /* old_size */, int64_t new_size,
MemoryScope /* scope */, const SourceLocation& /* source_location */) override
{
if (old_ptr == nullptr)
{
- return new_size > 0 ? ::malloc((uint64)new_size) : nullptr;
+ return new_size > 0 ? ::malloc((uint64_t)new_size) : nullptr;
}
if (new_size == 0)
@@ -26,7 +26,7 @@ public: }
else
{
- return ::realloc(old_ptr, (uint64)new_size);
+ return ::realloc(old_ptr, (uint64_t)new_size);
}
return nullptr;
}
diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h index 967bbb1..677501d 100644 --- a/deimos/core/allocator.h +++ b/deimos/core/allocator.h @@ -6,7 +6,7 @@ namespace deimos
{
-struct MemoryScope { uint32 id; };
+struct MemoryScope { uint32_t id; };
struct IAllocator
{
@@ -18,7 +18,7 @@ struct IAllocator [[nodiscard]]
virtual void* Reallocate(
- void* old_ptr, int64 old_size, int64 new_size,
+ void* old_ptr, int64_t old_size, int64_t new_size,
MemoryScope scope, const SourceLocation& source_location = {}) = 0;
};
@@ -39,7 +39,7 @@ public: [[nodiscard]]
constexpr void* Allocate(
- int64 new_size,
+ int64_t new_size,
const SourceLocation& source_location = {})
{
return m_allocator->Reallocate(nullptr, 0, new_size, m_scope, source_location);
@@ -47,14 +47,14 @@ public: [[nodiscard]]
void* Reallocate(
- void* old_ptr, int64 old_size, int64 new_size,
+ void* old_ptr, int64_t old_size, int64_t new_size,
const SourceLocation& source_location = {})
{
return m_allocator->Reallocate(old_ptr, old_size, new_size, m_scope, source_location);
}
constexpr void Free(
- void* old_ptr, int64 old_size,
+ void* old_ptr, int64_t old_size,
const SourceLocation& source_location = {})
{
(void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location);
@@ -117,7 +117,7 @@ public: template<typename T>
void Delete(T* t, const SourceLocation& source_location = {})
{
- if constexpr (!kIsTriviallyDestructible<T>)
+ if constexpr (!std::is_trivially_destructible_v<T>)
{
t->~T();
}
diff --git a/deimos/core/base.h b/deimos/core/base.h index 5ff9090..2482e68 100644 --- a/deimos/core/base.h +++ b/deimos/core/base.h @@ -1,5 +1,8 @@ #pragma once
+#include "deimos/core/std.h"
+#include "deimos/core/gsl.h"
+
#define deimos_StaticAssert(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
#define deimos_NO_COPY(TYPE) \
@@ -26,48 +29,25 @@ deimos_DEFAULT_COPY(TYPE); \
deimos_DEFAULT_MOVE(TYPE);
-namespace gsl
-{
-
-using zstring = char*;
-using czstring = const char*;
-
-} // namespace gsl
-
namespace deimos
{
-using uint8 = unsigned char;
-using uint16 = unsigned short;
-using uint32 = unsigned int;
-using uint64 = unsigned long long;
-
-using int8 = char;
-using int16 = short;
-using int32 = int;
-using int64 = long long;
-
-using float32 = float;
-using float64 = double;
-
-enum __attribute__((__may_alias__)) byte : uint8 {};
-
-struct uint128
+struct uint128_t
{
- uint64 high;
- uint64 low;
+ uint64_t high;
+ uint64_t low;
- constexpr bool operator==(const uint128& other) const = default;
+ constexpr bool operator==(const uint128_t& other) const = default;
};
struct SourceLocation
{
gsl::czstring file;
- int32 line;
+ int32_t line;
constexpr SourceLocation( // NOLINT
gsl::czstring file_ = __builtin_FILE(),
- int32 line_ = __builtin_LINE()) :
+ int32_t line_ = __builtin_LINE()) :
file{file_},
line{line_}
{}
@@ -75,10 +55,3 @@ struct SourceLocation } // namespace deimos
-constexpr void* operator new(deimos::uint64, void* ptr)
-{
- return ptr;
-}
-
-#include "deimos/core/std.h"
-
diff --git a/deimos/core/gsl.h b/deimos/core/gsl.h new file mode 100644 index 0000000..b6a9804 --- /dev/null +++ b/deimos/core/gsl.h @@ -0,0 +1,10 @@ +#pragma once
+
+namespace gsl
+{
+
+using zstring = char*;
+using czstring = const char*;
+
+} // namespace gsl
+
diff --git a/deimos/core/hash.h b/deimos/core/hash.h index e6e9cc8..f0eeafd 100644 --- a/deimos/core/hash.h +++ b/deimos/core/hash.h @@ -5,7 +5,7 @@ namespace deimos
{
-constexpr uint64 MurmurHash3_GetBlock64(const char* key, uint64 block)
+constexpr uint64_t MurmurHash3_GetBlock64(const char* key, uint64_t block)
{
// NOLINTBEGIN
key += block * 8;
@@ -13,25 +13,25 @@ constexpr uint64 MurmurHash3_GetBlock64(const char* key, uint64 block) if consteval
{
return
- (uint64)((uint8)key[0])
- | ((uint64)((uint8)key[1]) << 8)
- | ((uint64)((uint8)key[2]) << 16)
- | ((uint64)((uint8)key[3]) << 24)
- | ((uint64)((uint8)key[4]) << 32)
- | ((uint64)((uint8)key[5]) << 40)
- | ((uint64)((uint8)key[6]) << 48)
- | ((uint64)((uint8)key[7]) << 56);
+ (uint64_t)((uint8_t)key[0])
+ | ((uint64_t)((uint8_t)key[1]) << 8)
+ | ((uint64_t)((uint8_t)key[2]) << 16)
+ | ((uint64_t)((uint8_t)key[3]) << 24)
+ | ((uint64_t)((uint8_t)key[4]) << 32)
+ | ((uint64_t)((uint8_t)key[5]) << 40)
+ | ((uint64_t)((uint8_t)key[6]) << 48)
+ | ((uint64_t)((uint8_t)key[7]) << 56);
}
else
{
- uint64 value{};
+ uint64_t value{};
__builtin_memcpy(&value, key, 8);
return value;
}
// NOLINTEND
}
-constexpr uint64 MurmurHash3_Fmix64(uint64 k)
+constexpr uint64_t MurmurHash3_Fmix64(uint64_t k)
{
k ^= k >> 33U;
k *= 0xff51afd7ed558ccdULL;
@@ -41,27 +41,27 @@ constexpr uint64 MurmurHash3_Fmix64(uint64 k) return k;
}
-constexpr uint128 MurmurHash3_x64_128(const char* key, uint64 len)
+constexpr uint128_t MurmurHash3_x64_128(const char* key, uint64_t len)
{
if consteval { return { 12, 12 }; }
// NOLINTBEGIN
- const uint64 nblocks = len / 16;
+ const uint64_t nblocks = len / 16;
- const int64 seed = 0;
- uint64 h1 = seed;
- uint64 h2 = seed;
+ const uint64_t seed = 0;
+ uint64_t h1 = seed;
+ uint64_t h2 = seed;
- const uint64 c1 = 0x87c37b91114253d5ULL;
- const uint64 c2 = 0x4cf5ad432745937fULL;
+ const uint64_t c1 = 0x87c37b91114253d5ULL;
+ const uint64_t c2 = 0x4cf5ad432745937fULL;
//----------
// body
- for(uint64 i = 0; i < nblocks; i++)
+ for(uint64_t i = 0; i < nblocks; i++)
{
- uint64 k1 = MurmurHash3_GetBlock64(key, i * 2 + 0);
- uint64 k2 = MurmurHash3_GetBlock64(key, i * 2 + 1);
+ uint64_t k1 = MurmurHash3_GetBlock64(key, i * 2 + 0);
+ uint64_t k2 = MurmurHash3_GetBlock64(key, i * 2 + 1);
k1 *= c1; k1 = _rotl64(k1, 31); k1 *= c2; h1 ^= k1;
h1 = _rotl64(h1, 27); h1 += h2; h1 = h1*5+0x52dce729;
@@ -74,28 +74,28 @@ constexpr uint128 MurmurHash3_x64_128(const char* key, uint64 len) const char* tail = key + nblocks * 16;
- uint64 k1 = 0;
- uint64 k2 = 0;
+ uint64_t k1 = 0;
+ uint64_t k2 = 0;
switch (len & 15ULL)
{
- case 15: k2 ^= ((uint64)(uint8)tail[14]) << 48U; [[fallthrough]];
- case 14: k2 ^= ((uint64)(uint8)tail[13]) << 40U; [[fallthrough]];
- case 13: k2 ^= ((uint64)(uint8)tail[12]) << 32U; [[fallthrough]];
- case 12: k2 ^= ((uint64)(uint8)tail[11]) << 24U; [[fallthrough]];
- case 11: k2 ^= ((uint64)(uint8)tail[10]) << 16U; [[fallthrough]];
- case 10: k2 ^= ((uint64)(uint8)tail[ 9]) << 8U; [[fallthrough]];
- case 9: k2 ^= ((uint64)(uint8)tail[ 8]) << 0U;
+ case 15: k2 ^= ((uint64_t)(uint8_t)tail[14]) << 48U; [[fallthrough]];
+ case 14: k2 ^= ((uint64_t)(uint8_t)tail[13]) << 40U; [[fallthrough]];
+ case 13: k2 ^= ((uint64_t)(uint8_t)tail[12]) << 32U; [[fallthrough]];
+ case 12: k2 ^= ((uint64_t)(uint8_t)tail[11]) << 24U; [[fallthrough]];
+ case 11: k2 ^= ((uint64_t)(uint8_t)tail[10]) << 16U; [[fallthrough]];
+ case 10: k2 ^= ((uint64_t)(uint8_t)tail[ 9]) << 8U; [[fallthrough]];
+ case 9: k2 ^= ((uint64_t)(uint8_t)tail[ 8]) << 0U;
k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2;
[[fallthrough]];
- case 8: k1 ^= ((uint64)(uint8)tail[ 7]) << 56U; [[fallthrough]];
- case 7: k1 ^= ((uint64)(uint8)tail[ 6]) << 48U; [[fallthrough]];
- case 6: k1 ^= ((uint64)(uint8)tail[ 5]) << 40U; [[fallthrough]];
- case 5: k1 ^= ((uint64)(uint8)tail[ 4]) << 32U; [[fallthrough]];
- case 4: k1 ^= ((uint64)(uint8)tail[ 3]) << 24U; [[fallthrough]];
- case 3: k1 ^= ((uint64)(uint8)tail[ 2]) << 16U; [[fallthrough]];
- case 2: k1 ^= ((uint64)(uint8)tail[ 1]) << 8U; [[fallthrough]];
- case 1: k1 ^= ((uint64)(uint8)tail[ 0]) << 0U;
+ case 8: k1 ^= ((uint64_t)(uint8_t)tail[ 7]) << 56U; [[fallthrough]];
+ case 7: k1 ^= ((uint64_t)(uint8_t)tail[ 6]) << 48U; [[fallthrough]];
+ case 6: k1 ^= ((uint64_t)(uint8_t)tail[ 5]) << 40U; [[fallthrough]];
+ case 5: k1 ^= ((uint64_t)(uint8_t)tail[ 4]) << 32U; [[fallthrough]];
+ case 4: k1 ^= ((uint64_t)(uint8_t)tail[ 3]) << 24U; [[fallthrough]];
+ case 3: k1 ^= ((uint64_t)(uint8_t)tail[ 2]) << 16U; [[fallthrough]];
+ case 2: k1 ^= ((uint64_t)(uint8_t)tail[ 1]) << 8U; [[fallthrough]];
+ case 1: k1 ^= ((uint64_t)(uint8_t)tail[ 0]) << 0U;
k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1;
};
@@ -114,10 +114,10 @@ constexpr uint128 MurmurHash3_x64_128(const char* key, uint64 len) h2 += h1;
// NOLINTEND
- return uint128{h1, h2};
+ return uint128_t{h1, h2};
}
-constexpr uint128 MurmurHash3_x64_128(gsl::czstring str)
+constexpr uint128_t MurmurHash3_x64_128(gsl::czstring str)
{
return MurmurHash3_x64_128(str, __builtin_strlen(str));
}
diff --git a/deimos/core/id_name.h b/deimos/core/id_name.h index 6ca5a46..e158675 100644 --- a/deimos/core/id_name.h +++ b/deimos/core/id_name.h @@ -8,7 +8,7 @@ namespace deimos struct IdName
{
- uint128 hash;
+ uint128_t hash;
gsl::czstring name;
explicit constexpr IdName(gsl::czstring name_) :
diff --git a/deimos/core/os.h b/deimos/core/os.h index af6a775..af01853 100644 --- a/deimos/core/os.h +++ b/deimos/core/os.h @@ -6,7 +6,7 @@ namespace deimos
{
-enum class OsConsoleType : uint8
+enum class OsConsoleType : uint8_t
{
kStdOut,
kStdErr,
@@ -22,7 +22,7 @@ public: virtual ~OsConsoleApi() = default;
// @Todo Use span
- virtual void Write(OsConsoleType, const char* data, int64 length) = 0;
+ virtual void Write(OsConsoleType, const char* data, int64_t length) = 0;
};
class OsApi
diff --git a/deimos/core/os_win32.cpp b/deimos/core/os_win32.cpp index 07d3247..3f724ef 100644 --- a/deimos/core/os_win32.cpp +++ b/deimos/core/os_win32.cpp @@ -29,7 +29,7 @@ public: m_stderr{::GetStdHandle(STD_ERROR_HANDLE)}
{}
- void Write(OsConsoleType type, const char* data, int64 length) override
+ void Write(OsConsoleType type, const char* data, int64_t length) override
{
::WriteConsoleA(Handle(type), data, (DWORD)length, nullptr, nullptr);
}
diff --git a/deimos/core/std.h b/deimos/core/std.h index 1c611cd..601bfb9 100644 --- a/deimos/core/std.h +++ b/deimos/core/std.h @@ -1,8 +1,24 @@ #pragma once
+using uint8_t = unsigned char;
+using uint16_t = unsigned short;
+using uint32_t = unsigned int;
+using uint64_t = unsigned long long;
+
+using int8_t = char;
+using int16_t = short;
+using int32_t = int;
+using int64_t = long long;
+
+using size_t = uint64_t;
+
+static_assert(sizeof(size_t) == sizeof(void*), "size_t should be pointer size");
+
namespace std
{
+template<typename T> constexpr bool is_trivially_destructible_v = __is_trivially_destructible(T);
+
template<typename U, typename V> constexpr bool _is_same_helper = false;
template<typename T> constexpr bool _is_same_helper<T, T> = true;
template<typename U, typename V> concept same_as = _is_same_helper<U, V> && _is_same_helper<V, U>;
@@ -38,6 +54,9 @@ constexpr T exchange(T& obj, U&& new_value) return old_value;
}
+enum __attribute__((__may_alias__)) byte : uint8_t {};
+static_assert(sizeof(byte) == 1, "");
+
} // namespace std
static_assert(std::same_as<int, int>, "");
@@ -47,3 +66,8 @@ static_assert(std::same_as<std::remove_reference_t<int>, int>, ""); static_assert(std::same_as<std::remove_reference_t<int&>, int>, "");
static_assert(std::same_as<std::remove_reference_t<int&&>, int>, "");
+constexpr void* operator new(size_t, void* ptr)
+{
+ return ptr;
+}
+
|