1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** @file
*****************************************************************************
Declaration of misc math and serialization utility functions
*****************************************************************************
* @author This file is part of libff, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#ifndef UTILS_HPP_
#define UTILS_HPP_
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace skale {
class ostream {
public :
virtual void writeString (std::string& _s) = 0;
virtual void writeUint64 (uint64_t & _s) = 0;
virtual void writeUint32 (uint32_t & _s) = 0;
virtual void writeInt64 (int64_t & _s) = 0;
virtual void writeInt32 (int32_t & _s) = 0;
virtual void write(char* _buf, uint64_t _size);
};
class istream {
public:
virtual istream& read (char* s, uint64_t n) = 0;
virtual void readString (std::string& _s) = 0;
virtual void readUint64 (uint64_t & _s) = 0;
virtual void readUint32 (uint32_t & _s) = 0;
virtual void readInt64 (int64_t & _s) = 0;
virtual void readInt32 (int32_t & _s) = 0;
};
}
namespace libff {
typedef std::vector<bool> bit_vector;
size_t get_power_of_two(size_t n);
/// returns ceil(log2(n)), so 1ul<<log2(n) is the smallest power of 2, that is not less than n
size_t log2(size_t n);
inline size_t exp2(size_t k) { return size_t(1) << k; }
size_t to_twos_complement(int i, size_t w);
int from_twos_complement(size_t i, size_t w);
size_t bitreverse(size_t n, const size_t l);
bit_vector int_list_to_bits(const std::initializer_list<unsigned long> &l, const size_t wordsize);
long long div_ceil(long long x, long long y);
bool is_little_endian();
std::string FORMAT(const std::string &prefix, const char* format, ...);
/* A variadic template to suppress unused argument warnings */
template<typename ... Types>
void UNUSED(Types&&...) {}
#ifdef DEBUG
#define FMT libff::FORMAT
#else
#define FMT(...) (libff::UNUSED(__VA_ARGS__), "")
#endif
void serialize_bit_vector(skale::ostream &out, const bit_vector &v);
void deserialize_bit_vector(skale::istream &in, bit_vector &v);
template<typename T>
size_t size_in_bits(const std::vector<T> &v);
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
} // libff
#include <libff/common/utils.tcc> /* note that utils has a templatized part (utils.tcc) and non-templatized part (utils.cpp) */
#endif // UTILS_HPP_