avx::Int256

Class which provides functionality to use int type with AVX2. Each object of class avx::Int256 holds 8 int values.

Supported operators

Operator

Supported type(s)

Instructions used

Comment

+, +=

int, avx::Int256

AVX, AVX2

AVX is used when argument is scalar.

-, -=

int, avx::Int256

AVX, AVX2

Same as above.

*, *=

int, avx::Int256

AVX, AVX2

Same as above.

/, /=

int, avx::Int256

AVX, AVX2 (GCC), AVX512F

AVX512F is used when BUILD_USE_AVX512 is ENABLED

%, %=

int, avx::Int256

AVX, AVX2 (GCC), AVX512F

AVX512F is used when BUILD_USE_AVX512 is ENABLED

<<, <<=

int, avx::Int256

AVX2

>>, >>=

int, avx::Int256

AVX2

&, &=

int, avx::Int256

AVX, AVX2

AVX is used when argument is scalar.

|, |=

int, avx::Int256

AVX, AVX2

Same as above.

^, ^=

int, avx::Int256

AVX, AVX2

Same as above.

~

-

AVX2

Unitary operator.

[]

unsigned int

N/A

In GCC and MSVC disassembly no SIMD instructions were used

==

int, avx::Int256

AVX, AVX2

Returns true if ALL values ARE equal.

!=

int, avx::Int256

AVX, AVX2

Returns true if ANY value is NOT equal.

class Int256

Class providing vectorized version of int. Can hold 8 individual int values. Provides arithmetic and bitwise operators. Provides comparison operators == !=. Class providing vectorized version of int. Can hold 8 individual int values. Provides arithmetic and bitwise operators. Provides comparison operators == !=.

Public Types

using storedType = int

Type that is stored inside vector.

Public Functions

inline Int256()

Default constructor. Initializes vector with zeros.

inline Int256(const int *init)

Initializes vector by loading data from memory (via _mm256_lddq_si256).

Parameters:

init – Valid memory addres of minimal size of 256-bits (32 bytes).

inline Int256(const int &init)

Initializes vector with const value. Each cell will be set with value of init.

Parameters:

init – Value to be set.

inline Int256(const __m256i &init)

Initializes vector from __m256i value.

Parameters:

init – Value of type __m256i to initialize the vector.

inline Int256(const Int256 &init)

Copy constructor. Initializes vector from another Int256 vector.

Parameters:

init – Another Int256 vector to copy from.

inline Int256(const std::array<int, 8> &init)

Initializes vector from std::array of 8 int values.

Parameters:

init – Array of 8 int values to initialize the vector.

inline Int256(const std::array<short, 8> &init)

Initializes vector from std::array of 8 short values. Each short value is promoted to int.

Parameters:

init – Array of 8 short values to initialize the vector.

inline Int256(const std::array<char, 8> &init)

Initializes vector from std::array of 8 char values. Each char value is promoted to int.

Parameters:

init – Array of 8 char values to initialize the vector.

inline Int256(std::initializer_list<int> init)

Initializes vector from initializer_list of int values. If the list contains fewer than 8 elements, remaining elements are set to zero. If the list contains more than 8 elements, only the first 8 are used.

Parameters:

init – Initializer list of int values.

inline __m256i get() const

Returns the internal __m256i value stored by the object.

Returns:

The __m256i value.

inline void set(__m256i val)

Sets the internal __m256i value stored by the object.

Parameters:

val – New value of type __m256i.

inline void load (const int *pSrc) N_THROW_REL

Loads data from memory into vector (memory should be of size of at least 32 bytes). Memory doesn’t need to be aligned to any specific boundary. If sP is nullptr this method has no effect.

Parameters:

pSrc – Pointer to memory from which to load data.

Throws:

std::invalid_argument – If in Debug mode and pSrc is nullptr. In Release builds this method never throws (for nullptr method will have no effect).

inline void save(std::array<int, 8> &dest) const noexcept

Saves data to destination in memory.

Parameters:

dest – Reference to the list to which vector will be saved. Array doesn’t need to be aligned to any specific boundary.

inline void save (int *pDest) const N_THROW_REL

Saves data to destination in memory. The memory doesn’t have to be aligned to any specific boundary.

See https://en.cppreference.com/w/cpp/memory/c/aligned_alloc for more details.

Parameters:

pDest – A valid pointer to a memory of at least 32 bytes (8x int).

Throws:

std::invalid_argument – If in Debug mode and pDest is nullptr. In Release builds this method never throws (for nullptr method will have no effect).

inline void saveAligned (int *pDest) const N_THROW_REL

Saves data to destination in memory. The memory must be aligned at 32-byte boundary.

See https://en.cppreference.com/w/cpp/memory/c/aligned_alloc for more details.

Parameters:

pDest – A valid pointer to a memory of at least 32 bytes (8x int).

Throws:

std::invalid_argument – If in Debug mode and pDest is nullptr. In Release builds this method never throws (for nullptr method will have no effect).

inline bool operator==(const Int256 &bV) const

Compares vectors for equality.

Parameters:

bV – Second vector.

Returns:

true if all values in both vectors are equal, false if any value doesn’t match.

inline bool operator==(const int &b) const

Compares vectors for equality.

Parameters:

b – Value to compare.

Returns:

true if all values in vector is equal to b, otherwise false.

inline bool operator!=(const Int256 &bV) const

Compares vectors for inequality.

Parameters:

bV – Second vector.

Returns:

true if ANY value is different between vectors.

inline bool operator!=(const int &b) const

Compares vectors for inequality.

Parameters:

b – Value to compare.

Returns:

true if ANY value in vector is different than b, otherwise false.

inline int operator[](const unsigned int index) const

Indexing operator. Does not support value assignment through this method (e.g. aV[0] = 1 won’t work).

Parameters:

index – Position of desired element between 0 and 7.

Throws:

std::out_of_range – If index is not within the correct range and build type is debug will be thrown. Otherwise bitwise AND will prevent index to be out of range. Side effect is that only 3 LSBs are used from index.

Returns:

Value of underlying element.

inline Int256 operator+(const Int256 &bV) const

Adds values from other vector and returns new vector.

Parameters:

bV – Second vector.

Returns:

New vector being a sum of this vector and bv.

inline Int256 operator+(const int &b) const

Adds single value across all vector fields.

Parameters:

b – Value to add to vector.

Returns:

New vector being a sum of this vector and b.

inline Int256 operator-(const Int256 &bV) const

Substracts values from vector.

Parameters:

bV – Second vector.

Returns:

New vector being result of subtracting bV from vecotr.

inline Int256 operator-(const int &b) const
inline Int256 operator*(const Int256 &bV) const
inline Int256 operator*(const int &b) const
inline Int256 operator/(const Int256 &bV) const
inline Int256 operator/(const int &b) const
inline Int256 operator%(const Int256 &bV) const
inline Int256 operator%(const int &b) const
inline Int256 operator^(const Int256 &bV) const
inline Int256 operator^(const int &b) const
inline Int256 operator|(const Int256 &bV) const
inline Int256 operator|(const int &b) const
inline Int256 operator&(const Int256 &bV) const
inline Int256 operator&(const int &b) const
inline Int256 operator~() const
inline Int256 operator<<(const Int256 &bV) const
inline Int256 operator<<(const int &b) const
inline Int256 operator>>(const Int256 &bV) const
inline Int256 operator>>(const int &b) const
inline Int256 &operator+=(const Int256 &bV)
inline Int256 &operator+=(const int &b)
inline Int256 &operator-=(const Int256 &bV)
inline Int256 &operator-=(const int &b)
inline Int256 &operator*=(const Int256 &bV)
inline Int256 &operator*=(const int &b)
inline Int256 &operator/=(const Int256 &bV)
inline Int256 &operator/=(const int &b)
inline Int256 &operator%=(const Int256 &bV)

Performs integer division. IMPORTANT: Does not work for 0x8000’0000 aka -2 147 483 648

Parameters:

bV – Second modulo operand (divisor)

Returns:

Result of modulo operation.

inline Int256 &operator%=(const int &b)
inline Int256 &operator|=(const Int256 &bV)

Bitwise OR assignment operator. Applies bitwise OR between this vector and the given vector, storing the result in this vector.

Parameters:

bV – Second vector.

Returns:

Reference to the modified object.

inline Int256 &operator|=(const int &b)

Bitwise OR assignment operator. Applies bitwise OR between this vector and the given integer value, storing the result in this vector.

Parameters:

b – Integer value.

Returns:

Reference to the modified object.

inline Int256 &operator&=(const Int256 &bV)

Bitwise AND assignment operator. Applies bitwise AND between this vector and the given vector, storing the result in this vector.

Parameters:

bV – Second vector.

Returns:

Reference to the modified object.

inline Int256 &operator&=(const int &b)

Bitwise AND assignment operator. Applies bitwise AND between this vector and the given integer value, storing the result in this vector.

Parameters:

b – Integer value.

Returns:

Reference to the modified object.

inline Int256 &operator^=(const Int256 &bV)

Bitwise XOR assignment operator. Applies bitwise XOR between this vector and the given vector, storing the result in this vector.

Parameters:

bV – Second vector.

Returns:

Reference to the modified object.

inline Int256 &operator^=(const int &b)

Bitwise XOR assignment operator. Applies bitwise XOR between this vector and the given integer value, storing the result in this vector.

Parameters:

b – Integer value.

Returns:

Reference to the modified object.

inline Int256 &operator<<=(const Int256 &bV)

Shifts values left while shifting in 0.

Parameters:

bV – Vector containing number of bits for which each corresponding element should be shifted.

Returns:

Reference to modified object.

inline Int256 &operator<<=(const int &b)

Shifts values right while shifting in 0.

Parameters:

b – Number of bits by which values should be shifted.

Returns:

Reference to modified object.

inline Int256 &operator>>=(const Int256 &bV)

Shifts values right while shifting in sign bit.

Parameters:

bV – Vector containing number of bits for which each corresponding element should be shifted.

Returns:

Reference to modified object.

inline Int256 &operator>>=(const int &b)

Shifts values right while shifting in sign bit.

Parameters:

b – Number of bits by which values should be shifted.

Returns:

Reference to modified object.

inline std::string str() const

Returns string representation of vector. Printing will result in Int256(<vector_values>) eg. Int256(1, 2, 3, 4, 5, 6, 7, 8)

Returns:

String representation of underlying vector.

Public Static Attributes

static int size = 8

Number of individual values stored by object. This value can be used to iterate over elements.