Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Bitfield to simplify Move and Transposition entries
  • Loading branch information
razvanfilea committed May 13, 2021
commit ba55ec4b011997b832fbfdf9cf45d00ce71f7027
4 changes: 2 additions & 2 deletions ChessAndroid/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
ndkVersion "22.1.7171670"
ndkVersion "23.0.7272597-beta3"

defaultConfig {
applicationId "net.theluckycoder.chess"
Expand Down Expand Up @@ -66,7 +66,7 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3")

// AndroidX
implementation("androidx.activity:activity-ktx:1.2.2")
implementation("androidx.activity:activity-ktx:1.2.3")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1")
implementation("androidx.datastore:datastore-preferences:1.0.0-beta01")

Expand Down
84 changes: 84 additions & 0 deletions ChessAndroid/app/src/main/cpp/chess/Bitfield.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

#include <cstdint>

namespace bitfield_detail
{

template <class T, std::size_t Size, std::size_t Offset>
class field
{
public:
static constexpr std::size_t Mask = (1u << Size) - 1u;

inline static constexpr T get(const T &t)
{
auto offset = Offset;
return (t >> offset) & Mask;
}

inline static constexpr void set(T &t, const T &value)
{
constexpr auto ShiftedMask = Mask << Offset;
t = (t & ~ShiftedMask) | ((value & Mask) << Offset);
}
};

template <std::size_t Index, std::size_t Size0>
consteval std::size_t field_size() { return Size0; }

template <std::size_t Index, std::size_t Size0, std::size_t Size1, std::size_t... Sizes>
consteval std::size_t field_size()
{
return Index ? field_size<Index - bool(Index), Size1, Sizes...>() : Size0;
}

template <std::size_t Index, std::size_t MaxIndex, std::size_t Size0>
consteval std::size_t field_offset()
{
if constexpr (Index < MaxIndex)
return Size0;
return {};
}

template <std::size_t Index, std::size_t MaxIndex, std::size_t Size0, std::size_t Size1, std::size_t... Sizes>
consteval std::size_t field_offset()
{
if constexpr (Index < MaxIndex)
return Size0 + field_offset<Index + 1, MaxIndex, Size1, Sizes...>();
return {};
}
}

template <class T, std::size_t... Sizes>
class Bitfield
{

template <std::size_t Index>
using bit_field_type = bitfield_detail::field<T,
bitfield_detail::field_size<Index, Sizes...>(), bitfield_detail::field_offset<0, Index, Sizes...>()>;

public:
constexpr Bitfield() = default;

explicit constexpr Bitfield(const T t) : _t(t) {}

template <std::size_t Index>
constexpr T get() const { return bit_field_type<Index>::get(_t); }

template <std::size_t Index, class A>
constexpr A getAs() const { return static_cast<A>(get<Index>()); }

template <std::size_t Index>
constexpr void set(const T &value) { bit_field_type<Index>::set(_t, value); }

template <std::size_t Index, class A>
constexpr void setAs(const A &value) { set<Index>(static_cast<T>(value)); }

constexpr T value() const { return _t; }

constexpr T &ref() { return _t; }

private:
T _t{};
};
Loading