polynomial.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* boost random/detail/polynomial.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2014
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id$
  11. */
  12. #ifndef BOOST_RANDOM_DETAIL_POLYNOMIAL_HPP
  13. #define BOOST_RANDOM_DETAIL_POLYNOMIAL_HPP
  14. #include <cstddef>
  15. #include <limits>
  16. #include <vector>
  17. #include <algorithm>
  18. #include <boost/assert.hpp>
  19. #include <boost/cstdint.hpp>
  20. namespace boost {
  21. namespace random {
  22. namespace detail {
  23. class polynomial_ops {
  24. public:
  25. typedef unsigned long digit_t;
  26. static void add(std::size_t size, const digit_t * lhs,
  27. const digit_t * rhs, digit_t * output)
  28. {
  29. for(std::size_t i = 0; i < size; ++i) {
  30. output[i] = lhs[i] ^ rhs[i];
  31. }
  32. }
  33. static void add_shifted_inplace(std::size_t size, const digit_t * lhs,
  34. digit_t * output, std::size_t shift)
  35. {
  36. if(shift == 0) {
  37. add(size, lhs, output, output);
  38. return;
  39. }
  40. std::size_t bits = std::numeric_limits<digit_t>::digits;
  41. digit_t prev = 0;
  42. for(std::size_t i = 0; i < size; ++i) {
  43. digit_t tmp = lhs[i];
  44. output[i] ^= (tmp << shift) | (prev >> (bits-shift));
  45. prev = tmp;
  46. }
  47. output[size] ^= (prev >> (bits-shift));
  48. }
  49. static void multiply_simple(std::size_t size, const digit_t * lhs,
  50. const digit_t * rhs, digit_t * output)
  51. {
  52. std::size_t bits = std::numeric_limits<digit_t>::digits;
  53. for(std::size_t i = 0; i < 2*size; ++i) {
  54. output[i] = 0;
  55. }
  56. for(std::size_t i = 0; i < size; ++i) {
  57. for(std::size_t j = 0; j < bits; ++j) {
  58. if((lhs[i] & (digit_t(1) << j)) != 0) {
  59. add_shifted_inplace(size, rhs, output + i, j);
  60. }
  61. }
  62. }
  63. }
  64. // memory requirements: (size - cutoff) * 4 + next_smaller
  65. static void multiply_karatsuba(std::size_t size,
  66. const digit_t * lhs, const digit_t * rhs,
  67. digit_t * output)
  68. {
  69. if(size < 64) {
  70. multiply_simple(size, lhs, rhs, output);
  71. return;
  72. }
  73. // split in half
  74. std::size_t cutoff = size/2;
  75. multiply_karatsuba(cutoff, lhs, rhs, output);
  76. multiply_karatsuba(size - cutoff, lhs + cutoff, rhs + cutoff,
  77. output + cutoff*2);
  78. std::vector<digit_t> local1(size - cutoff);
  79. std::vector<digit_t> local2(size - cutoff);
  80. // combine the digits for the inner multiply
  81. add(cutoff, lhs, lhs + cutoff, &local1[0]);
  82. if(size & 1) local1[cutoff] = lhs[size - 1];
  83. add(cutoff, rhs + cutoff, rhs, &local2[0]);
  84. if(size & 1) local2[cutoff] = rhs[size - 1];
  85. std::vector<digit_t> local3((size - cutoff) * 2);
  86. multiply_karatsuba(size - cutoff, &local1[0], &local2[0], &local3[0]);
  87. add(cutoff * 2, output, &local3[0], &local3[0]);
  88. add((size - cutoff) * 2, output + cutoff*2, &local3[0], &local3[0]);
  89. // Finally, add the inner result
  90. add((size - cutoff) * 2, output + cutoff, &local3[0], output + cutoff);
  91. }
  92. static void multiply_add_karatsuba(std::size_t size,
  93. const digit_t * lhs, const digit_t * rhs,
  94. digit_t * output)
  95. {
  96. std::vector<digit_t> buf(size * 2);
  97. multiply_karatsuba(size, lhs, rhs, &buf[0]);
  98. add(size * 2, &buf[0], output, output);
  99. }
  100. static void multiply(const digit_t * lhs, std::size_t lhs_size,
  101. const digit_t * rhs, std::size_t rhs_size,
  102. digit_t * output)
  103. {
  104. std::fill_n(output, lhs_size + rhs_size, digit_t(0));
  105. multiply_add(lhs, lhs_size, rhs, rhs_size, output);
  106. }
  107. static void multiply_add(const digit_t * lhs, std::size_t lhs_size,
  108. const digit_t * rhs, std::size_t rhs_size,
  109. digit_t * output)
  110. {
  111. // split into pieces that can be passed to
  112. // karatsuba multiply.
  113. while(lhs_size != 0) {
  114. if(lhs_size < rhs_size) {
  115. std::swap(lhs, rhs);
  116. std::swap(lhs_size, rhs_size);
  117. }
  118. multiply_add_karatsuba(rhs_size, lhs, rhs, output);
  119. lhs += rhs_size;
  120. lhs_size -= rhs_size;
  121. output += rhs_size;
  122. }
  123. }
  124. static void copy_bits(const digit_t * x, std::size_t low, std::size_t high,
  125. digit_t * out)
  126. {
  127. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  128. std::size_t offset = low/bits;
  129. x += offset;
  130. low -= offset*bits;
  131. high -= offset*bits;
  132. std::size_t n = (high-low)/bits;
  133. if(low == 0) {
  134. for(std::size_t i = 0; i < n; ++i) {
  135. out[i] = x[i];
  136. }
  137. } else {
  138. for(std::size_t i = 0; i < n; ++i) {
  139. out[i] = (x[i] >> low) | (x[i+1] << (bits-low));
  140. }
  141. }
  142. if((high-low)%bits) {
  143. digit_t low_mask = (digit_t(1) << ((high-low)%bits)) - 1;
  144. digit_t result = (x[n] >> low);
  145. if(low != 0 && (n+1)*bits < high) {
  146. result |= (x[n+1] << (bits-low));
  147. }
  148. out[n] = (result & low_mask);
  149. }
  150. }
  151. static void shift_left(digit_t * val, std::size_t size, std::size_t shift)
  152. {
  153. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  154. BOOST_ASSERT(shift > 0);
  155. BOOST_ASSERT(shift < bits);
  156. digit_t prev = 0;
  157. for(std::size_t i = 0; i < size; ++i) {
  158. digit_t tmp = val[i];
  159. val[i] = (prev >> (bits - shift)) | (val[i] << shift);
  160. prev = tmp;
  161. }
  162. }
  163. static digit_t sqr(digit_t val) {
  164. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  165. digit_t mask = (digit_t(1) << bits/2) - 1;
  166. for(std::size_t i = bits; i > 1; i /= 2) {
  167. val = ((val & ~mask) << i/2) | (val & mask);
  168. mask = mask & (mask >> i/4);
  169. mask = mask | (mask << i/2);
  170. }
  171. return val;
  172. }
  173. static void sqr(digit_t * val, std::size_t size)
  174. {
  175. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  176. digit_t mask = (digit_t(1) << bits/2) - 1;
  177. for(std::size_t i = 0; i < size; ++i) {
  178. digit_t x = val[size - i - 1];
  179. val[(size - i - 1) * 2] = sqr(x & mask);
  180. val[(size - i - 1) * 2 + 1] = sqr(x >> bits/2);
  181. }
  182. }
  183. // optimized for the case when the modulus has few bits set.
  184. struct sparse_mod {
  185. sparse_mod(const digit_t * divisor, std::size_t divisor_bits)
  186. {
  187. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  188. _remainder_bits = divisor_bits - 1;
  189. for(std::size_t i = 0; i < divisor_bits; ++i) {
  190. if(divisor[i/bits] & (digit_t(1) << i%bits)) {
  191. _bit_indices.push_back(i);
  192. }
  193. }
  194. BOOST_ASSERT(_bit_indices.back() == divisor_bits - 1);
  195. _bit_indices.pop_back();
  196. if(_bit_indices.empty()) {
  197. _block_bits = divisor_bits;
  198. _lower_bits = 0;
  199. } else {
  200. _block_bits = divisor_bits - _bit_indices.back() - 1;
  201. _lower_bits = _bit_indices.back() + 1;
  202. }
  203. _partial_quotient.resize((_block_bits + bits - 1)/bits);
  204. }
  205. void operator()(digit_t * dividend, std::size_t dividend_bits)
  206. {
  207. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  208. while(dividend_bits > _remainder_bits) {
  209. std::size_t block_start = (std::max)(dividend_bits - _block_bits, _remainder_bits);
  210. std::size_t block_size = (dividend_bits - block_start + bits - 1) / bits;
  211. copy_bits(dividend, block_start, dividend_bits, &_partial_quotient[0]);
  212. for(std::size_t i = 0; i < _bit_indices.size(); ++i) {
  213. std::size_t pos = _bit_indices[i] + block_start - _remainder_bits;
  214. add_shifted_inplace(block_size, &_partial_quotient[0], dividend + pos/bits, pos%bits);
  215. }
  216. add_shifted_inplace(block_size, &_partial_quotient[0], dividend + block_start/bits, block_start%bits);
  217. dividend_bits = block_start;
  218. }
  219. }
  220. std::vector<digit_t> _partial_quotient;
  221. std::size_t _remainder_bits;
  222. std::size_t _block_bits;
  223. std::size_t _lower_bits;
  224. std::vector<std::size_t> _bit_indices;
  225. };
  226. // base should have the same number of bits as mod
  227. // base, and mod should both be able to hold a power
  228. // of 2 >= mod_bits. out needs to be twice as large.
  229. static void mod_pow_x(boost::uintmax_t exponent, const digit_t * mod, std::size_t mod_bits, digit_t * out)
  230. {
  231. const std::size_t bits = std::numeric_limits<digit_t>::digits;
  232. const std::size_t n = (mod_bits + bits - 1) / bits;
  233. const std::size_t highbit = mod_bits - 1;
  234. if(exponent == 0) {
  235. out[0] = 1;
  236. std::fill_n(out + 1, n - 1, digit_t(0));
  237. return;
  238. }
  239. boost::uintmax_t i = std::numeric_limits<boost::uintmax_t>::digits - 1;
  240. while(((boost::uintmax_t(1) << i) & exponent) == 0) {
  241. --i;
  242. }
  243. out[0] = 2;
  244. std::fill_n(out + 1, n - 1, digit_t(0));
  245. sparse_mod m(mod, mod_bits);
  246. while(i--) {
  247. sqr(out, n);
  248. m(out, 2 * mod_bits - 1);
  249. if((boost::uintmax_t(1) << i) & exponent) {
  250. shift_left(out, n, 1);
  251. if(out[highbit / bits] & (digit_t(1) << highbit%bits))
  252. add(n, out, mod, out);
  253. }
  254. }
  255. }
  256. };
  257. class polynomial
  258. {
  259. typedef polynomial_ops::digit_t digit_t;
  260. public:
  261. polynomial() : _size(0) {}
  262. class reference {
  263. public:
  264. reference(digit_t &value, int idx)
  265. : _value(value), _idx(idx) {}
  266. operator bool() const { return (_value & (digit_t(1) << _idx)) != 0; }
  267. reference& operator=(bool b)
  268. {
  269. if(b) {
  270. _value |= (digit_t(1) << _idx);
  271. } else {
  272. _value &= ~(digit_t(1) << _idx);
  273. }
  274. return *this;
  275. }
  276. reference &operator^=(bool b)
  277. {
  278. _value ^= (digit_t(b) << _idx);
  279. return *this;
  280. }
  281. reference &operator=(const reference &other)
  282. {
  283. return *this = static_cast<bool>(other);
  284. }
  285. private:
  286. digit_t &_value;
  287. int _idx;
  288. };
  289. reference operator[](std::size_t i)
  290. {
  291. static const std::size_t bits = std::numeric_limits<digit_t>::digits;
  292. ensure_bit(i);
  293. return reference(_storage[i/bits], i%bits);
  294. }
  295. bool operator[](std::size_t i) const
  296. {
  297. static const std::size_t bits = std::numeric_limits<digit_t>::digits;
  298. if(i < size())
  299. return (_storage[i/bits] & (digit_t(1) << (i%bits))) != 0;
  300. else
  301. return false;
  302. }
  303. std::size_t size() const
  304. {
  305. return _size;
  306. }
  307. void resize(std::size_t n)
  308. {
  309. static const std::size_t bits = std::numeric_limits<digit_t>::digits;
  310. _storage.resize((n + bits - 1)/bits);
  311. // clear the high order bits in case we're shrinking.
  312. if(n%bits) {
  313. _storage.back() &= ((digit_t(1) << (n%bits)) - 1);
  314. }
  315. _size = n;
  316. }
  317. friend polynomial operator*(const polynomial &lhs, const polynomial &rhs);
  318. friend polynomial mod_pow_x(boost::uintmax_t exponent, polynomial mod);
  319. private:
  320. std::vector<polynomial_ops::digit_t> _storage;
  321. std::size_t _size;
  322. void ensure_bit(std::size_t i)
  323. {
  324. if(i >= size()) {
  325. resize(i + 1);
  326. }
  327. }
  328. void normalize()
  329. {
  330. while(size() && (*this)[size() - 1] == 0)
  331. resize(size() - 1);
  332. }
  333. };
  334. inline polynomial operator*(const polynomial &lhs, const polynomial &rhs)
  335. {
  336. polynomial result;
  337. result._storage.resize(lhs._storage.size() + rhs._storage.size());
  338. polynomial_ops::multiply(&lhs._storage[0], lhs._storage.size(),
  339. &rhs._storage[0], rhs._storage.size(),
  340. &result._storage[0]);
  341. result._size = lhs._size + rhs._size;
  342. return result;
  343. }
  344. inline polynomial mod_pow_x(boost::uintmax_t exponent, polynomial mod)
  345. {
  346. polynomial result;
  347. mod.normalize();
  348. std::size_t mod_size = mod.size();
  349. result._storage.resize(mod._storage.size() * 2);
  350. result._size = mod.size() * 2;
  351. polynomial_ops::mod_pow_x(exponent, &mod._storage[0], mod_size, &result._storage[0]);
  352. result.resize(mod.size() - 1);
  353. return result;
  354. }
  355. }
  356. }
  357. }
  358. #endif // BOOST_RANDOM_DETAIL_POLYNOMIAL_HPP