src/data_structures/HybridVector.cpp
bd6604c5
 #include "HybridVector.h"
 #include "../math/Math.h"
df622802
 #include "../math/SIMD.h"
 
9c705994
 #define SIMD_PAD(x) (gaps::simd::Index::increment() + \
     gaps::simd::Index::increment() * ((x) / gaps::simd::Index::increment())) 
 
3a0667b8
 HybridVector::HybridVector(unsigned sz)
df622802
     :
3a0667b8
 mIndexBitFlags(sz / 64 + 1, 0),
9c705994
 mData(SIMD_PAD(sz), 0.f),
3a0667b8
 mSize(sz)
9c705994
 {
     GAPS_ASSERT(mData.size() % gaps::simd::Index::increment() == 0);
 }
bd6604c5
 
 HybridVector::HybridVector(const std::vector<float> &v)
     :
 mIndexBitFlags(v.size() / 64 + 1, 0),
9c705994
 mData(SIMD_PAD(v.size()), 0.f),
df622802
 mSize(v.size())
bd6604c5
 {
9c705994
     GAPS_ASSERT(mData.size() % gaps::simd::Index::increment() == 0);
 
     for (unsigned i = 0; i < mSize; ++i)
bd6604c5
     {
         mData[i] = v[i];
         if (v[i] > 0.f)
         {
             mIndexBitFlags[i / 64] ^= (1ull << (i % 64));
         }
     }
 }
 
 bool HybridVector::empty() const
 {
     for (unsigned i = 0; i < mIndexBitFlags.size(); ++i)
     {
         if (mIndexBitFlags[i] != 0)
         {
             return false;
         }
     }
     return true;
 }
 
 unsigned HybridVector::size() const
 {
df622802
     return mSize;
bd6604c5
 }
 
 bool HybridVector::add(unsigned i, float v)
 {
     if (mData[i] + v < gaps::epsilon)
     {
         mIndexBitFlags[i / 64] ^= (1ull << (i % 64));
         mData[i] = 0.f;
         return true;
     }
     else
     {
         mIndexBitFlags[i / 64] |= (1ull << (i % 64));
         mData[i] += v;
         return false;
     }
 }
 
 float HybridVector::operator[](unsigned i) const
 {
9c705994
     GAPS_ASSERT(i < mSize);
bd6604c5
     return mData[i];
 }
 
 const float* HybridVector::densePtr() const
 {
     return &(mData[0]);
 }
 
a38fefcb
 Archive& operator<<(Archive &ar, const HybridVector &vec)
bd6604c5
 {
df622802
     ar << vec.mSize;
bd6604c5
     for (unsigned i = 0; i < vec.mIndexBitFlags.size(); ++i)
     {
         ar << vec.mIndexBitFlags[i];
     }
 
df622802
     for (unsigned i = 0; i < vec.mSize; ++i)
bd6604c5
     {
         ar << vec.mData[i];
     }
     return ar;
 }
 
 Archive& operator>>(Archive &ar, HybridVector &vec)
 {
     unsigned sz = 0;
     ar >> sz;
df622802
     GAPS_ASSERT(sz == vec.size());
bd6604c5
 
     for (unsigned i = 0; i < vec.mIndexBitFlags.size(); ++i)
     {
         ar >> vec.mIndexBitFlags[i];
     }
 
df622802
     for (unsigned i = 0; i < vec.mSize; ++i)
bd6604c5
     {
         ar >> vec.mData[i];
     }
     return ar;
 }