Skip to content

Commit

Permalink
Format epee
Browse files Browse the repository at this point in the history
  • Loading branch information
jagerman committed Aug 21, 2024
1 parent 1a084c0 commit 8059763
Show file tree
Hide file tree
Showing 65 changed files with 8,624 additions and 8,766 deletions.
758 changes: 353 additions & 405 deletions contrib/epee/include/epee/console_handler.h

Large diffs are not rendered by default.

36 changes: 12 additions & 24 deletions contrib/epee/include/epee/copyable_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,17 @@

#include <atomic>

namespace epee
{
class copyable_atomic: public std::atomic<uint32_t>
{
namespace epee {
class copyable_atomic : public std::atomic<uint32_t> {
public:
copyable_atomic()
{};
copyable_atomic(uint32_t value)
{ store(value); }
copyable_atomic(const copyable_atomic& a):std::atomic<uint32_t>(a.load())
{}
copyable_atomic& operator= (const copyable_atomic& a)
{
store(a.load());
return *this;
copyable_atomic(){};
copyable_atomic(uint32_t value) { store(value); }
copyable_atomic(const copyable_atomic& a) : std::atomic<uint32_t>(a.load()) {}
copyable_atomic& operator=(const copyable_atomic& a) {
store(a.load());
return *this;
}
uint32_t operator++()
{
return std::atomic<uint32_t>::operator++();
}
uint32_t operator++(int fake)
{
return std::atomic<uint32_t>::operator++(fake);
}
};
}
uint32_t operator++() { return std::atomic<uint32_t>::operator++(); }
uint32_t operator++(int fake) { return std::atomic<uint32_t>::operator++(fake); }
};
} // namespace epee
232 changes: 122 additions & 110 deletions contrib/epee/include/epee/int-util.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Copyright (c) 2014-2018, The Monero Project
//
//
// All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
Expand All @@ -25,7 +25,7 @@
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers

#pragma once
Expand All @@ -40,14 +40,14 @@
#endif

#if defined(__clang__) || defined(__GNUC__)
# define epee_int_utils_bswap_64(x) __builtin_bswap64(x)
# define epee_int_utils_bswap_32(x) __builtin_bswap32(x)
#define epee_int_utils_bswap_64(x) __builtin_bswap64(x)
#define epee_int_utils_bswap_32(x) __builtin_bswap32(x)
#else
# if defined(__linux__) || defined(__ANDROID__)
# include <byteswap.h>
# endif
# define epee_int_utils_bswap_64(x) bswap_64(x)
# define epee_int_utils_bswap_32(x) bswap_32(x)
#if defined(__linux__) || defined(__ANDROID__)
#include <byteswap.h>
#endif
#define epee_int_utils_bswap_64(x) bswap_64(x)
#define epee_int_utils_bswap_32(x) bswap_32(x)
#endif

#if defined(__sun) && defined(__SVR4)
Expand All @@ -58,125 +58,137 @@
#include <stdlib.h>

static inline uint32_t rol32(uint32_t x, int r) {
static_assert(sizeof(uint32_t) == sizeof(unsigned int), "this code assumes 32-bit integers");
return _rotl(x, r);
static_assert(sizeof(uint32_t) == sizeof(unsigned int), "this code assumes 32-bit integers");
return _rotl(x, r);
}

static inline uint64_t rol64(uint64_t x, int r) {
return _rotl64(x, r);
return _rotl64(x, r);
}

#else

static inline uint32_t rol32(uint32_t x, int r) {
return (x << (r & 31)) | (x >> (-r & 31));
return (x << (r & 31)) | (x >> (-r & 31));
}

static inline uint64_t rol64(uint64_t x, int r) {
return (x << (r & 63)) | (x >> (-r & 63));
return (x << (r & 63)) | (x >> (-r & 63));
}

#endif

#ifndef __SIZEOF_INT128__
static inline uint64_t hi_dword(uint64_t val) {
return val >> 32;
return val >> 32;
}

static inline uint64_t lo_dword(uint64_t val) {
return val & 0xFFFFFFFF;
return val & 0xFFFFFFFF;
}

static inline uint64_t div_with_remainder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
dividend |= ((uint64_t)*remainder) << 32;
*remainder = dividend % divisor;
return dividend / divisor;
static inline uint64_t div_with_remainder(
uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
dividend |= ((uint64_t)*remainder) << 32;
*remainder = dividend % divisor;
return dividend / divisor;
}

static inline bool shl128(uint64_t* hi, uint64_t* lo) {
bool carry = ((*hi) >> 63);
*hi <<= 1;
*hi += ((*lo) >> 63);
*lo <<= 1;
return carry;
bool carry = ((*hi) >> 63);
*hi <<= 1;
*hi += ((*lo) >> 63);
*lo <<= 1;
return carry;
}
#endif

static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
#ifdef __SIZEOF_INT128__
unsigned __int128 result = (unsigned __int128) multiplier * (unsigned __int128) multiplicand;
*product_hi = result >> 64;
return (uint64_t) result;
unsigned __int128 result = (unsigned __int128)multiplier * (unsigned __int128)multiplicand;
*product_hi = result >> 64;
return (uint64_t)result;
#else
// multiplier = ab = a * 2^32 + b
// multiplicand = cd = c * 2^32 + d
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
uint64_t a = hi_dword(multiplier);
uint64_t b = lo_dword(multiplier);
uint64_t c = hi_dword(multiplicand);
uint64_t d = lo_dword(multiplicand);

uint64_t ac = a * c;
uint64_t ad = a * d;
uint64_t bc = b * c;
uint64_t bd = b * d;

uint64_t adbc = ad + bc;
uint64_t adbc_carry = adbc < ad ? 1 : 0;

// multiplier * multiplicand = product_hi * 2^64 + product_lo
uint64_t product_lo = bd + (adbc << 32);
uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
*product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;
assert(ac <= *product_hi);

return product_lo;
// multiplier = ab = a * 2^32 + b
// multiplicand = cd = c * 2^32 + d
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
uint64_t a = hi_dword(multiplier);
uint64_t b = lo_dword(multiplier);
uint64_t c = hi_dword(multiplicand);
uint64_t d = lo_dword(multiplicand);

uint64_t ac = a * c;
uint64_t ad = a * d;
uint64_t bc = b * c;
uint64_t bd = b * d;

uint64_t adbc = ad + bc;
uint64_t adbc_carry = adbc < ad ? 1 : 0;

// multiplier * multiplicand = product_hi * 2^64 + product_lo
uint64_t product_lo = bd + (adbc << 32);
uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
*product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;
assert(ac <= *product_hi);

return product_lo;
#endif
}

// Long division with 2^32 base
static inline void div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) {
static inline void div128_32(
uint64_t dividend_hi,
uint64_t dividend_lo,
uint32_t divisor,
uint64_t* quotient_hi,
uint64_t* quotient_lo) {
#ifdef __SIZEOF_INT128__
unsigned __int128 result = (((unsigned __int128) dividend_hi) << 64 | ((unsigned __int128) dividend_lo)) / divisor;
*quotient_lo = (uint64_t) result;
*quotient_hi = (uint64_t)(result >> 64);
unsigned __int128 result =
(((unsigned __int128)dividend_hi) << 64 | ((unsigned __int128)dividend_lo)) / divisor;
*quotient_lo = (uint64_t)result;
*quotient_hi = (uint64_t)(result >> 64);
#else
uint64_t dividend_dwords[4];
uint32_t remainder = 0;

dividend_dwords[3] = hi_dword(dividend_hi);
dividend_dwords[2] = lo_dword(dividend_hi);
dividend_dwords[1] = hi_dword(dividend_lo);
dividend_dwords[0] = lo_dword(dividend_lo);

*quotient_hi = div_with_remainder(dividend_dwords[3], divisor, &remainder) << 32;
*quotient_hi |= div_with_remainder(dividend_dwords[2], divisor, &remainder);
*quotient_lo = div_with_remainder(dividend_dwords[1], divisor, &remainder) << 32;
*quotient_lo |= div_with_remainder(dividend_dwords[0], divisor, &remainder);
uint64_t dividend_dwords[4];
uint32_t remainder = 0;

dividend_dwords[3] = hi_dword(dividend_hi);
dividend_dwords[2] = lo_dword(dividend_hi);
dividend_dwords[1] = hi_dword(dividend_lo);
dividend_dwords[0] = lo_dword(dividend_lo);

*quotient_hi = div_with_remainder(dividend_dwords[3], divisor, &remainder) << 32;
*quotient_hi |= div_with_remainder(dividend_dwords[2], divisor, &remainder);
*quotient_lo = div_with_remainder(dividend_dwords[1], divisor, &remainder) << 32;
*quotient_lo |= div_with_remainder(dividend_dwords[0], divisor, &remainder);
#endif
}


// Long division with 2^64 base
static inline void div128_64(uint64_t dividend_hi, uint64_t dividend_lo, uint64_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) {
static inline void div128_64(
uint64_t dividend_hi,
uint64_t dividend_lo,
uint64_t divisor,
uint64_t* quotient_hi,
uint64_t* quotient_lo) {
#ifdef __SIZEOF_INT128__
unsigned __int128 result = (((unsigned __int128) dividend_hi) << 64 | ((unsigned __int128) dividend_lo)) / divisor;
*quotient_lo = (uint64_t) result;
*quotient_hi = (uint64_t)(result >> 64);
unsigned __int128 result =
(((unsigned __int128)dividend_hi) << 64 | ((unsigned __int128)dividend_lo)) / divisor;
*quotient_lo = (uint64_t)result;
*quotient_hi = (uint64_t)(result >> 64);
#else
uint64_t remainder = 0;
for (size_t i = 0; i < 128; i++) {
bool carry = remainder >> 63;
remainder <<= 1;
if (shl128(&dividend_hi, &dividend_lo))
remainder |= 1;
if (carry || remainder >= divisor) {
remainder -= divisor;
dividend_lo |= 1;
uint64_t remainder = 0;
for (size_t i = 0; i < 128; i++) {
bool carry = remainder >> 63;
remainder <<= 1;
if (shl128(&dividend_hi, &dividend_lo))
remainder |= 1;
if (carry || remainder >= divisor) {
remainder -= divisor;
dividend_lo |= 1;
}
}
}
*quotient_hi = dividend_hi;
*quotient_lo = dividend_lo;
*quotient_hi = dividend_hi;
*quotient_lo = dividend_lo;
#endif
}

Expand All @@ -185,21 +197,21 @@ static inline void div128_64(uint64_t dividend_hi, uint64_t dividend_lo, uint64_
// when this is true: for instance, when c is known to be greater than either a or b.
static inline uint64_t mul128_div64(uint64_t a, uint64_t b, uint64_t c) {
#ifdef __SIZEOF_INT128__
return (uint64_t) ((unsigned __int128) a) * ((unsigned __int128) b) / ((unsigned __int128) c);
return (uint64_t)((unsigned __int128)a) * ((unsigned __int128)b) / ((unsigned __int128)c);
#else
uint64_t hi;
uint64_t lo = mul128(a, b, &hi);
uint64_t resulthi, resultlo;
div128_64(hi, lo, c, &resulthi, &resultlo);
assert(resulthi == 0);
return resultlo;
uint64_t hi;
uint64_t lo = mul128(a, b, &hi);
uint64_t resulthi, resultlo;
div128_64(hi, lo, c, &resulthi, &resultlo);
assert(resulthi == 0);
return resultlo;
#endif
}

#ifdef _MSC_VER
# define LITTLE_ENDIAN 1234
# define BIG_ENDIAN 4321
# define BYTE_ORDER LITTLE_ENDIAN
#define LITTLE_ENDIAN 1234
#define BIG_ENDIAN 4321
#define BYTE_ORDER LITTLE_ENDIAN
#endif

#if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN)
Expand All @@ -208,27 +220,27 @@ static_assert(false, "BYTE_ORDER is undefined. Perhaps, GNU extensions are not e

#if BYTE_ORDER == LITTLE_ENDIAN

#define SWAP64LE(x) ((uint64_t) (x))
#define SWAP64LE(x) ((uint64_t)(x))
#define SWAP64BE epee_int_utils_bswap_64
#define SWAP32LE(x) ((uint32_t) (x))
#define SWAP32LE(x) ((uint32_t)(x))
#define SWAP32BE(x) epee_int_utils_bswap_32

static inline void memcpy_swap64le(void *dst, const void *src, size_t n) {
memcpy(dst, src, 8 * n);
static inline void memcpy_swap64le(void* dst, const void* src, size_t n) {
memcpy(dst, src, 8 * n);
}

#else

#define SWAP64BE(x) ((uint64_t) (x))
#define SWAP64BE(x) ((uint64_t)(x))
#define SWAP64LE epee_int_utils_bswap_64
#define SWAP32BE(x) ((uint32_t) (x))
#define SWAP32BE(x) ((uint32_t)(x))
#define SWAP32LE(x) epee_int_utils_bswap_32

static inline void memcpy_swap64le(void *dst, const void *src, size_t n) {
size_t i;
for (i = 0; i < n; i++) {
((uint64_t *) dst)[i] = swap64(((const uint64_t *) src)[i]);
}
static inline void memcpy_swap64le(void* dst, const void* src, size_t n) {
size_t i;
for (i = 0; i < n; i++) {
((uint64_t*)dst)[i] = swap64(((const uint64_t*)src)[i]);
}
}

#endif
Loading

0 comments on commit 8059763

Please sign in to comment.