chsvlib
chsv helper source code

◆ memneg_LE()

bool Chusov::Memory::memneg_LE ( void *  pDest,
std::size_t  cbDest,
const void *  pSrc,
std::size_t  cbSrc 
)
noexcept

Zero-extends or contracts an arbitrary-precision integer given in the Little-Endian byte order, then computes an additive inversion of it in a separate of the same destination buffer returning a value of the highest bit of the result.

Parameters
[out]pDestA pointer to the destination buffer which receives a two's complement of the input integer with respect to 2CHAR_BIT cbDest.
cbDestCapacity of the destination buffer and length of the resulting integer in bytes.
[in]pSrcA pointer to an input integer to negate by the function. The buffer may be the same as the destination buffer. Other than that, the buffers may not overlap.
cbSrcLength of the input integer in bytes. If cbSrc is less than cbDest, then the input integer is zero-extended to cbDest bytes before the negation. Otherwise, if cbSrc is greater than cbDest, then cbSrc - cbDest high-order bytes of the input are discarded.
Returns
A value of the highest bit of the result, i.e. the value of the highest bit in the byte pDest[cbDest - 1], if cbDest is greater than zero, or zero otherwise.

The function considers the input vector to specify the integer in the Little-Endian byte order. That is, given that \(V_X = \{x_0, \dots, x_n\}\) is the input vector pointed to by pSrc, such that \(x_0\) is the first byte in memory of the vector, \(x_n\) is the last byte and n = cbSrc - 1, the input value for the operation is \(X = \sum_{i=0}^{n}x_i\cdot b^i\), where b = 2CHAR_BIT is a number of values that can be represented with one byte.

The operation is performed modulo \(b^{\textrm{cbDest}}\). That is, before the negation takes place the input value \(X\) is zero-extended or contracted to fit the buffer of cbDest bytes. The input value is therefore transformed to the value \(X' = \sum_{i=0}^{m}x'_i\cdot b^i\), where m = cbDest - 1, \(x'_i = x_i\) if \(0 \le i \le n\), and \(x'_i = 0\) otherwise. The value \(X'\) is then negated and written into the destination buffer pDest, again, in the Little-Endian byte order.

To negate Big-Endian integers use memneg_BE.

If the sizes of the input and output integers are equal, and it is known beforehand whether the two buffers are different or the two pointers address the same memory, the preferable faster functions for the operation are memneg_LE_copy and memneg_LE_inplace.

See also
memneg_LE_copy;
memneg_LE_inplace;
memneg_BE;
memincr_LE;
memdecr_LE;
memadd_LE;
memsub_LE.