Skip to content

Commit

Permalink
Add ParticleIDWrapper::flip_valid()
Browse files Browse the repository at this point in the history
A cheaper way to swap validity sign on particle ids.
Not the same as `id = -id`, but also reversible.
  • Loading branch information
ax3l committed Jan 31, 2024
1 parent d861bdf commit b1b0ffb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Src/Particle/AMReX_Particle.H
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ struct ParticleIDWrapper
r = (sign) ? lval : -lval;
return r;
}

/** Swap the the particle from invalid to valid or vice versa.
*
* Swaps the is_valid (sign) bit. This is NOT identical to id = -id,
* but it is equally reversible.
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
void flip_valid () noexcept
{
// the leftmost bit is our id sign - we will XOR it with 1 to flip it
m_idata ^= static_cast<uint64_t>(1) << 63;
}

/** Check the sign of the id.
*
* Returns false if the id is positive, otherwise true (invalid particle).
*/
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool negative () const noexcept
{
// the leftmost bit is our id sign
return m_idata >> 63;
}
};

struct ParticleCPUWrapper
Expand Down Expand Up @@ -173,6 +196,17 @@ struct ConstParticleIDWrapper
r = (sign) ? lval : -lval;
return r;
}

/** Check the sign of the id.
*
* Returns false if the id is positive, otherwise true (invalid particle).
*/
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool negative () const noexcept
{
// the leftmost bit is our id's inverse sign
return !(m_idata >> 63);
}
};

struct ConstParticleCPUWrapper
Expand Down

0 comments on commit b1b0ffb

Please sign in to comment.