jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Public Member Functions | Static Public Member Functions | Friends | List of all members
jau::CT::Mask< T, > Class Template Reference

A Mask type used for constant-time operations. More...

#include <ct_utils.hpp>

Collaboration diagram for jau::CT::Mask< T, >:

Public Member Functions

 Mask (const Mask< T > &other) noexcept
 
template<typename U >
 Mask (Mask< U > o) noexcept
 Derive a Mask from a Mask of a larger type. More...
 
if_not_set_return (T x) const noexcept
 Return x if the mask is cleared, or otherwise zero. More...
 
if_set_return (T x) const noexcept
 Return x if the mask is set, or otherwise zero. More...
 
void if_set_zero_out (T buf[], size_t elems) noexcept
 If this mask is set, zero out buf, otherwise do nothing. More...
 
bool is_set () const noexcept
 Return true iff this mask is set. More...
 
Mask< T > & operator&= (Mask< T > o) noexcept
 AND-combine two masks. More...
 
Mask< T > & operator= (const Mask< T > &other) noexcept
 
Mask< T > & operator^= (Mask< T > o) noexcept
 XOR-combine two masks. More...
 
Mask< T > & operator|= (Mask< T > o) noexcept
 OR-combine two masks. More...
 
Mask< T > operator~ () const noexcept
 Negate this mask. More...
 
select (T x, T y) const noexcept
 If this mask is set, return x, otherwise return y. More...
 
select_and_unpoison (T x, T y) const noexcept
 
Mask< T > select_mask (Mask< T > x, Mask< T > y) const noexcept
 If this mask is set, return x, otherwise return y. More...
 
void select_n (T output[], const T x[], const T y[], size_t len) const noexcept
 Conditionally set output to x or y, depending on if mask is set or cleared (resp) More...
 
unpoisoned_value () const noexcept
 Return the value of the mask, unpoisoned. More...
 
value () const noexcept
 Return the underlying value of the mask. More...
 

Static Public Member Functions

static Mask< T > cleared () noexcept
 Return a Mask<T> with all bits cleared. More...
 
template<typename U >
static Mask< T > expand (Mask< U > m) noexcept
 Return a Mask<T> which is set if m is set. More...
 
static Mask< T > expand (T v) noexcept
 Return a Mask<T> which is set if v is != 0. More...
 
static Mask< T > is_any_of (T v, std::initializer_list< T > accepted) noexcept
 
static Mask< T > is_equal (T x, T y) noexcept
 Return a Mask<T> which is set if x == y. More...
 
static Mask< T > is_gt (T x, T y) noexcept
 Return a Mask<T> which is set if x > y. More...
 
static Mask< T > is_gte (T x, T y) noexcept
 Return a Mask<T> which is set if x >= y. More...
 
static Mask< T > is_lt (T x, T y) noexcept
 Return a Mask<T> which is set if x < y. More...
 
static Mask< T > is_lte (T x, T y) noexcept
 Return a Mask<T> which is set if x <= y. More...
 
static Mask< T > is_within_range (T v, T l, T u) noexcept
 
static Mask< T > is_zero (T x) noexcept
 Return a Mask<T> which is set if v is == 0 or cleared otherwise. More...
 
static Mask< T > set () noexcept
 Return a Mask<T> with all bits set. More...
 

Friends

Mask< T > operator& (Mask< T > x, Mask< T > y) noexcept
 AND-combine two masks. More...
 
Mask< T > operator^ (Mask< T > x, Mask< T > y) noexcept
 XOR-combine two masks. More...
 
Mask< T > operator| (Mask< T > x, Mask< T > y) noexcept
 OR-combine two masks. More...
 

Detailed Description

template<typename T, std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
class jau::CT::Mask< T, >

A Mask type used for constant-time operations.

A Mask<T> always has value either 0 (all bits cleared) or ~0 (all bits set). All operations in a Mask<T> are intended to compile to code which does not contain conditional jumps. This must be verified with tooling (eg binary disassembly or using valgrind) since you never know what a compiler might do.

Template Parameters
Tunsigned integral type

Definition at line 80 of file ct_utils.hpp.

Constructor & Destructor Documentation

◆ Mask() [1/2]

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
jau::CT::Mask< T, >::Mask ( const Mask< T > &  other)
inlinenoexcept

Definition at line 83 of file ct_utils.hpp.

◆ Mask() [2/2]

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
template<typename U >
jau::CT::Mask< T, >::Mask ( Mask< U >  o)
inlinenoexcept

Derive a Mask from a Mask of a larger type.

Definition at line 95 of file ct_utils.hpp.

Member Function Documentation

◆ operator=()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > & jau::CT::Mask< T, >::operator= ( const Mask< T > &  other)
inlinenoexcept

Definition at line 86 of file ct_utils.hpp.

◆ set()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::set ( )
inlinestaticnoexcept

Return a Mask<T> with all bits set.

Definition at line 103 of file ct_utils.hpp.

◆ cleared()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::cleared ( )
inlinestaticnoexcept

Return a Mask<T> with all bits cleared.

Definition at line 111 of file ct_utils.hpp.

◆ expand() [1/2]

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::expand ( v)
inlinestaticnoexcept

Return a Mask<T> which is set if v is != 0.

Definition at line 119 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ expand() [2/2]

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
template<typename U >
static Mask< T > jau::CT::Mask< T, >::expand ( Mask< U >  m)
inlinestaticnoexcept

Return a Mask<T> which is set if m is set.

Definition at line 128 of file ct_utils.hpp.

◆ is_zero()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_zero ( x)
inlinestaticnoexcept

Return a Mask<T> which is set if v is == 0 or cleared otherwise.

Definition at line 137 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ is_equal()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_equal ( x,
y 
)
inlinestaticnoexcept

Return a Mask<T> which is set if x == y.

Definition at line 145 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ is_lt()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_lt ( x,
y 
)
inlinestaticnoexcept

Return a Mask<T> which is set if x < y.

Definition at line 153 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ is_gt()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_gt ( x,
y 
)
inlinestaticnoexcept

Return a Mask<T> which is set if x > y.

Definition at line 161 of file ct_utils.hpp.

◆ is_lte()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_lte ( x,
y 
)
inlinestaticnoexcept

Return a Mask<T> which is set if x <= y.

Definition at line 169 of file ct_utils.hpp.

◆ is_gte()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_gte ( x,
y 
)
inlinestaticnoexcept

Return a Mask<T> which is set if x >= y.

Definition at line 177 of file ct_utils.hpp.

◆ is_within_range()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_within_range ( v,
l,
u 
)
inlinestaticnoexcept

Definition at line 182 of file ct_utils.hpp.

◆ is_any_of()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
static Mask< T > jau::CT::Mask< T, >::is_any_of ( v,
std::initializer_list< T >  accepted 
)
inlinestaticnoexcept

Definition at line 192 of file ct_utils.hpp.

◆ operator&=()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > & jau::CT::Mask< T, >::operator&= ( Mask< T >  o)
inlinenoexcept

AND-combine two masks.

Definition at line 209 of file ct_utils.hpp.

◆ operator^=()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > & jau::CT::Mask< T, >::operator^= ( Mask< T >  o)
inlinenoexcept

XOR-combine two masks.

Definition at line 218 of file ct_utils.hpp.

◆ operator|=()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > & jau::CT::Mask< T, >::operator|= ( Mask< T >  o)
inlinenoexcept

OR-combine two masks.

Definition at line 227 of file ct_utils.hpp.

◆ operator~()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > jau::CT::Mask< T, >::operator~ ( ) const
inlinenoexcept

Negate this mask.

Definition at line 260 of file ct_utils.hpp.

◆ if_set_return()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
T jau::CT::Mask< T, >::if_set_return ( x) const
inlinenoexcept

Return x if the mask is set, or otherwise zero.

Definition at line 268 of file ct_utils.hpp.

◆ if_not_set_return()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
T jau::CT::Mask< T, >::if_not_set_return ( x) const
inlinenoexcept

Return x if the mask is cleared, or otherwise zero.

Definition at line 276 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ select()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
T jau::CT::Mask< T, >::select ( x,
y 
) const
inlinenoexcept

If this mask is set, return x, otherwise return y.

Definition at line 284 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ select_and_unpoison()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
T jau::CT::Mask< T, >::select_and_unpoison ( x,
y 
) const
inlinenoexcept

Definition at line 289 of file ct_utils.hpp.

◆ select_mask()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > jau::CT::Mask< T, >::select_mask ( Mask< T >  x,
Mask< T >  y 
) const
inlinenoexcept

If this mask is set, return x, otherwise return y.

Definition at line 299 of file ct_utils.hpp.

◆ select_n()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
void jau::CT::Mask< T, >::select_n ( output[],
const T  x[],
const T  y[],
size_t  len 
) const
inlinenoexcept

Conditionally set output to x or y, depending on if mask is set or cleared (resp)

Definition at line 308 of file ct_utils.hpp.

◆ if_set_zero_out()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
void jau::CT::Mask< T, >::if_set_zero_out ( buf[],
size_t  elems 
)
inlinenoexcept

If this mask is set, zero out buf, otherwise do nothing.

Definition at line 317 of file ct_utils.hpp.

◆ unpoisoned_value()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
T jau::CT::Mask< T, >::unpoisoned_value ( ) const
inlinenoexcept

Return the value of the mask, unpoisoned.

Definition at line 328 of file ct_utils.hpp.

Here is the caller graph for this function:

◆ is_set()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
bool jau::CT::Mask< T, >::is_set ( ) const
inlinenoexcept

Return true iff this mask is set.

Definition at line 338 of file ct_utils.hpp.

◆ value()

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
T jau::CT::Mask< T, >::value ( ) const
inlinenoexcept

Return the underlying value of the mask.

Definition at line 346 of file ct_utils.hpp.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator&

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > operator& ( Mask< T >  x,
Mask< T >  y 
)
friend

AND-combine two masks.

Definition at line 236 of file ct_utils.hpp.

◆ operator^

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > operator^ ( Mask< T >  x,
Mask< T >  y 
)
friend

XOR-combine two masks.

Definition at line 244 of file ct_utils.hpp.

◆ operator|

template<typename T , std::enable_if_t< std::is_integral_v< T > &&std::is_unsigned_v< T >, bool > = true>
Mask< T > operator| ( Mask< T >  x,
Mask< T >  y 
)
friend

OR-combine two masks.

Definition at line 252 of file ct_utils.hpp.


The documentation for this class was generated from the following file: