Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitfield class #90

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
Begin bitfield implementation
  • Loading branch information
czaloj committed Jul 17, 2015
commit d39a4ba2a6c0b189585e3e91ae58109584b9b0cd
1 change: 1 addition & 0 deletions Vorb.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@
<None Include="include/typesArray.inl" />
<None Include="include/typesColor.inl" />
<None Include="include\AssetLoader.inl" />
<None Include="include\Bitfields.inl" />
<None Include="include\voxel\IntervalTree.inl" />
<None Include="src/sound/SoundImplFMOD.inl" />
<None Include="src\graphics\ImageIOConv.inl" />
Expand Down
3 changes: 3 additions & 0 deletions Vorb.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,9 @@
<None Include="include\IntersectionUtils.hpp">
<Filter>Utils</Filter>
</None>
<None Include="include\Bitfields.inl">
<Filter>Utils</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Text Include="KEG.txt">
Expand Down
88 changes: 88 additions & 0 deletions include/Bitfields.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*! \file typesArray.inl
* @brief Bitfield implementation for different access methodologies.
*/

#define VORB_BITS_PER_BYTE 8

namespace vorb {
/*! @brief
*/
template<typename T, size_t BITOFF, size_t BITSIZE>
class UncheckedBitfield {
static_assert(std::is_integral<T>::value, "Bitfield may only represent integral types");
static_assert(BITOFF < sizeof(T) * VORB_BITS_PER_BYTE, "Starting offset must be contained within the type");
static_assert((BITOFF + BITSIZE) <= sizeof(T) * VORB_BITS_PER_BYTE, "Bitfield must be contained within the type");
public:
UncheckedBitfield& operator = (T v) {
// Reduced by an extra masking operation for overflowss
value = (value & ~(((1 << BITSIZE) - 1) << BITOFF)) | (v << BITOFF);
return *this;
}

UncheckedBitfield& operator += (T v) {
// Allows for overflows
value += (v << BITOFF);
return *this;
}
UncheckedBitfield& operator -= (T v) {
// Allows for overflows
value += (v << BITOFF);
return *this;
}
UncheckedBitfield& operator *= (T v) {
field *= v;
return *this;
}
UncheckedBitfield& operator /= (T v) {
field /= v;
return *this;
}
UncheckedBitfield& operator |= (T v) {
value |= (v << BITOFF);
return *this;
}
UncheckedBitfield& operator &= (T v) {
value &= (v << BITOFF) | ~(((1 << BITSIZE) - 1) << BITOFF);
return *this;
}
UncheckedBitfield& operator ^= (T v) {
value ^= (v << BITOFF);
return *this;
}

T operator + (T v) {
return field + v;
}
T operator - (T v) {
return field - v;
}
T operator * (T v) {
return field * v;
}
T operator / (T v) {
return field / v;
}
T operator | (T v) {
return field | v;
}
T operator & (T v) {
return field & v;
}
T operator ^ (T v) {
return field ^ v;
}

operator T() const {
return field;
}

union {
T value;
struct {
T : BITOFF;
T field : BITSIZE;
T : ((sizeof(T) * VORB_BITS_PER_BYTE) - (BITOFF + BITSIZE));
};
};
};
}