Move UInt to UInt.h
GitOrigin-RevId: 52b6d36d1ece8c267f5548000b2592ffd3a62ded
This commit is contained in:
parent
bc991da146
commit
b676fe509a
@ -12,6 +12,7 @@
|
|||||||
#include "td/utils/port/thread.h"
|
#include "td/utils/port/thread.h"
|
||||||
#include "td/utils/Random.h"
|
#include "td/utils/Random.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "td/utils/int_types.h"
|
#include "td/utils/int_types.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#include "td/utils/Random.h"
|
#include "td/utils/Random.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
#include "td/utils/Time.h"
|
#include "td/utils/Time.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include "td/mtproto/mtproto_api.h"
|
#include "td/mtproto/mtproto_api.h"
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include "td/utils/crypto.h"
|
#include "td/utils/crypto.h"
|
||||||
#include "td/utils/port/detail/PollableFd.h"
|
#include "td/utils/port/detail/PollableFd.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace mtproto {
|
namespace mtproto {
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "td/utils/logging.h"
|
#include "td/utils/logging.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
#include "td/utils/Time.h"
|
#include "td/utils/Time.h"
|
||||||
#include "td/utils/tl_helpers.h"
|
#include "td/utils/tl_helpers.h"
|
||||||
#include "td/utils/tl_parsers.h"
|
#include "td/utils/tl_parsers.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include "td/utils/port/FileFd.h"
|
#include "td/utils/port/FileFd.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
// Types
|
// Types
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
#include "td/utils/port/Stat.h"
|
#include "td/utils/port/Stat.h"
|
||||||
#include "td/utils/ScopeGuard.h"
|
#include "td/utils/ScopeGuard.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#include "td/utils/StringBuilder.h"
|
#include "td/utils/StringBuilder.h"
|
||||||
#include "td/utils/tl_helpers.h"
|
#include "td/utils/tl_helpers.h"
|
||||||
#include "td/utils/tl_storers.h"
|
#include "td/utils/tl_storers.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
#include "td/utils/Variant.h"
|
#include "td/utils/Variant.h"
|
||||||
|
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "td/utils/port/FileFd.h"
|
#include "td/utils/port/FileFd.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
#include "td/tl/TlObject.h"
|
#include "td/tl/TlObject.h"
|
||||||
|
|
||||||
#include "td/utils/int_types.h"
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include "td/utils/port/FileFd.h"
|
#include "td/utils/port/FileFd.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
@ -207,6 +207,7 @@ set(TDUTILS_SOURCE
|
|||||||
td/utils/tl_storers.h
|
td/utils/tl_storers.h
|
||||||
td/utils/translit.h
|
td/utils/translit.h
|
||||||
td/utils/type_traits.h
|
td/utils/type_traits.h
|
||||||
|
td/utils/UInt.h
|
||||||
td/utils/unicode.h
|
td/utils/unicode.h
|
||||||
td/utils/unique_ptr.h
|
td/utils/unique_ptr.h
|
||||||
td/utils/utf8.h
|
td/utils/utf8.h
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "td/utils/crypto.h"
|
#include "td/utils/crypto.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
@ -308,21 +308,4 @@ inline MutableSlice as_slice(MutableSlice slice) {
|
|||||||
return slice;
|
return slice;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <size_t N>
|
|
||||||
Slice UInt<N>::as_slice() const {
|
|
||||||
return Slice(raw, N / 8);
|
|
||||||
}
|
|
||||||
template <size_t N>
|
|
||||||
MutableSlice UInt<N>::as_slice() {
|
|
||||||
return MutableSlice(raw, N / 8);
|
|
||||||
}
|
|
||||||
template <size_t N>
|
|
||||||
Slice as_slice(const UInt<N> &value) {
|
|
||||||
return value.as_slice();
|
|
||||||
}
|
|
||||||
template <size_t N>
|
|
||||||
MutableSlice as_slice(UInt<N> &value) {
|
|
||||||
return value.as_slice();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
93
tdutils/td/utils/UInt.h
Normal file
93
tdutils/td/utils/UInt.h
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
//
|
||||||
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "td/utils/common.h"
|
||||||
|
#include "td/utils/Slice.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
struct UInt {
|
||||||
|
static_assert(size % 8 == 0, "size should be divisible by 8");
|
||||||
|
uint8 raw[size / 8];
|
||||||
|
|
||||||
|
Slice as_slice() const {
|
||||||
|
return Slice(raw, size / 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
MutableSlice as_slice() {
|
||||||
|
return MutableSlice(raw, size / 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_zero() const {
|
||||||
|
for (size_t i = 0; i < size / 8; i++) {
|
||||||
|
if (raw[i] != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
void set_zero() {
|
||||||
|
for (size_t i = 0; i < size / 8; i++) {
|
||||||
|
raw[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static UInt zero() {
|
||||||
|
UInt v;
|
||||||
|
v.set_zero();
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
bool operator==(const UInt<size> &a, const UInt<size> &b) {
|
||||||
|
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
bool operator!=(const UInt<size> &a, const UInt<size> &b) {
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
td::UInt<size> operator^(const UInt<size> &a, const UInt<size> &b) {
|
||||||
|
td::UInt<size> res;
|
||||||
|
for (size_t i = 0; i < size / 8; i++) {
|
||||||
|
res.raw[i] = static_cast<uint8>(a.raw[i] ^ b.raw[i]);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
int get_kth_bit(const UInt<size> &a, uint32 bit) {
|
||||||
|
uint8 b = a.raw[bit / 8];
|
||||||
|
bit &= 7;
|
||||||
|
return (b >> (7 - bit)) & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
Slice as_slice(const UInt<size> &value) {
|
||||||
|
return value.as_slice();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
MutableSlice as_slice(UInt<size> &value) {
|
||||||
|
return value.as_slice();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <size_t size>
|
||||||
|
bool operator<(const UInt<size> &a, const UInt<size> &b) {
|
||||||
|
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
using UInt128 = UInt<128>;
|
||||||
|
using UInt256 = UInt<256>;
|
||||||
|
|
||||||
|
} // namespace td
|
@ -45,6 +45,7 @@
|
|||||||
#include "td/utils/int_types.h"
|
#include "td/utils/int_types.h"
|
||||||
#include "td/utils/unique_ptr.h"
|
#include "td/utils/unique_ptr.h"
|
||||||
|
|
||||||
|
#include <cstring> // temporary for std::memcpy
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
@ -43,67 +42,4 @@ static_assert(static_cast<char>(-256) == 0, "Unexpected cast to char implementat
|
|||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class Slice;
|
|
||||||
class MutableSlice;
|
|
||||||
template <size_t size>
|
|
||||||
struct UInt {
|
|
||||||
static_assert(size % 8 == 0, "size should be divisible by 8");
|
|
||||||
uint8 raw[size / 8];
|
|
||||||
Slice as_slice() const;
|
|
||||||
MutableSlice as_slice();
|
|
||||||
|
|
||||||
bool is_zero() const {
|
|
||||||
for (size_t i = 0; i * 8 < size; i++) {
|
|
||||||
if (raw[i] != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
void set_zero() {
|
|
||||||
for (size_t i = 0; i * 8 < size; i++) {
|
|
||||||
raw[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
static UInt zero() {
|
|
||||||
UInt v;
|
|
||||||
v.set_zero();
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <size_t size>
|
|
||||||
bool operator==(const UInt<size> &a, const UInt<size> &b) {
|
|
||||||
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t size>
|
|
||||||
bool operator!=(const UInt<size> &a, const UInt<size> &b) {
|
|
||||||
return !(a == b);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t size>
|
|
||||||
td::UInt<size> operator^(const UInt<size> &a, const UInt<size> &b) {
|
|
||||||
td::UInt<size> res;
|
|
||||||
for (size_t i = 0; i * 8 < size; i++) {
|
|
||||||
res.raw[i] = static_cast<uint8>(a.raw[i] ^ b.raw[i]);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t size>
|
|
||||||
int get_kth_bit(const UInt<size> &a, uint32 bit) {
|
|
||||||
uint8 b = a.raw[bit / 8];
|
|
||||||
bit &= 7;
|
|
||||||
return (b >> (7 - bit)) & 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t size>
|
|
||||||
bool operator<(const UInt<size> &a, const UInt<size> &b) {
|
|
||||||
return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
using UInt128 = UInt<128>;
|
|
||||||
using UInt256 = UInt<256>;
|
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "td/utils/misc.h"
|
#include "td/utils/misc.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
#include "td/utils/utf8.h"
|
#include "td/utils/utf8.h"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include "td/utils/misc.h"
|
#include "td/utils/misc.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/StorerBase.h"
|
#include "td/utils/StorerBase.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "td/utils/crypto.h"
|
#include "td/utils/crypto.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/tests.h"
|
#include "td/utils/tests.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#include "td/utils/Random.h"
|
#include "td/utils/Random.h"
|
||||||
#include "td/utils/Slice.h"
|
#include "td/utils/Slice.h"
|
||||||
#include "td/utils/Status.h"
|
#include "td/utils/Status.h"
|
||||||
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include "test/data.h"
|
#include "test/data.h"
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user