2 *****************************************************************************
3 Implementation of misc math and serialization utility functions
4 *****************************************************************************
5 * @author This file is part of libsnark, developed by SCIPR Lab
6 * and contributors (see AUTHORS).
7 * @copyright MIT license (see LICENSE file)
8 *****************************************************************************/
14 #include "common/utils.hpp"
19 /* returns ceil(log2(n)), so 1ul<<log2(n) is the smallest power of 2,
20 that is not less than n. */
22 size_t r = ((n & (n-1)) == 0 ? 0 : 1); // add 1 if n is not power of 2
33 size_t bitreverse(size_t n, const size_t l)
36 for (size_t k = 0; k < l; ++k)
38 r = (r << 1) | (n & 1);
44 bit_vector int_list_to_bits(const std::initializer_list<unsigned long> &l, const size_t wordsize)
46 bit_vector res(wordsize*l.size());
47 for (size_t i = 0; i < l.size(); ++i)
49 for (size_t j = 0; j < wordsize; ++j)
51 res[i*wordsize + j] = (*(l.begin()+i) & (1ul<<(wordsize-1-j)));
57 long long div_ceil(long long x, long long y)
59 return (x + (y-1)) / y;
62 bool is_little_endian()
64 uint64_t a = 0x12345678;
65 unsigned char *c = (unsigned char*)(&a);
69 std::string FORMAT(const std::string &prefix, const char* format, ...)
71 const static size_t MAX_FMT = 256;
74 va_start(args, format);
75 vsnprintf(buf, MAX_FMT, format, args);
78 return prefix + std::string(buf);
81 void serialize_bit_vector(std::ostream &out, const bit_vector &v)
83 out << v.size() << "\n";
84 for (size_t i = 0; i < v.size(); ++i)
90 void deserialize_bit_vector(std::istream &in, bit_vector &v)
95 for (size_t i = 0; i < size; ++i)