The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1019 lines
26KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. namespace
  19. {
  20. inline size_t bitToIndex (const int bit) noexcept { return (size_t) (bit >> 5); }
  21. inline uint32 bitToMask (const int bit) noexcept { return (uint32) 1 << (bit & 31); }
  22. }
  23. //==============================================================================
  24. BigInteger::BigInteger()
  25. : numValues (4),
  26. highestBit (-1),
  27. negative (false)
  28. {
  29. values.calloc (numValues + 1);
  30. }
  31. BigInteger::BigInteger (const int32 value)
  32. : numValues (4),
  33. highestBit (31),
  34. negative (value < 0)
  35. {
  36. values.calloc (numValues + 1);
  37. values[0] = (uint32) abs (value);
  38. highestBit = getHighestBit();
  39. }
  40. BigInteger::BigInteger (const uint32 value)
  41. : numValues (4),
  42. highestBit (31),
  43. negative (false)
  44. {
  45. values.calloc (numValues + 1);
  46. values[0] = value;
  47. highestBit = getHighestBit();
  48. }
  49. BigInteger::BigInteger (int64 value)
  50. : numValues (4),
  51. highestBit (63),
  52. negative (value < 0)
  53. {
  54. values.calloc (numValues + 1);
  55. if (value < 0)
  56. value = -value;
  57. values[0] = (uint32) value;
  58. values[1] = (uint32) (value >> 32);
  59. highestBit = getHighestBit();
  60. }
  61. BigInteger::BigInteger (const BigInteger& other)
  62. : numValues ((size_t) jmax ((size_t) 4, bitToIndex (other.highestBit) + 1)),
  63. highestBit (other.getHighestBit()),
  64. negative (other.negative)
  65. {
  66. values.malloc (numValues + 1);
  67. memcpy (values, other.values, sizeof (uint32) * (numValues + 1));
  68. }
  69. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  70. BigInteger::BigInteger (BigInteger&& other) noexcept
  71. : values (static_cast <HeapBlock <uint32>&&> (other.values)),
  72. numValues (other.numValues),
  73. highestBit (other.highestBit),
  74. negative (other.negative)
  75. {
  76. }
  77. BigInteger& BigInteger::operator= (BigInteger&& other) noexcept
  78. {
  79. values = static_cast <HeapBlock <uint32>&&> (other.values);
  80. numValues = other.numValues;
  81. highestBit = other.highestBit;
  82. negative = other.negative;
  83. return *this;
  84. }
  85. #endif
  86. BigInteger::~BigInteger()
  87. {
  88. }
  89. void BigInteger::swapWith (BigInteger& other) noexcept
  90. {
  91. values.swapWith (other.values);
  92. std::swap (numValues, other.numValues);
  93. std::swap (highestBit, other.highestBit);
  94. std::swap (negative, other.negative);
  95. }
  96. BigInteger& BigInteger::operator= (const BigInteger& other)
  97. {
  98. if (this != &other)
  99. {
  100. highestBit = other.getHighestBit();
  101. numValues = (size_t) jmax ((size_t) 4, bitToIndex (highestBit) + 1);
  102. negative = other.negative;
  103. values.malloc (numValues + 1);
  104. memcpy (values, other.values, sizeof (uint32) * (numValues + 1));
  105. }
  106. return *this;
  107. }
  108. void BigInteger::ensureSize (const size_t numVals)
  109. {
  110. if (numVals + 2 >= numValues)
  111. {
  112. size_t oldSize = numValues;
  113. numValues = ((numVals + 2) * 3) / 2;
  114. values.realloc (numValues + 1);
  115. while (oldSize < numValues)
  116. values [oldSize++] = 0;
  117. }
  118. }
  119. //==============================================================================
  120. bool BigInteger::operator[] (const int bit) const noexcept
  121. {
  122. return bit <= highestBit && bit >= 0
  123. && ((values [bitToIndex (bit)] & bitToMask (bit)) != 0);
  124. }
  125. int BigInteger::toInteger() const noexcept
  126. {
  127. const int n = (int) (values[0] & 0x7fffffff);
  128. return negative ? -n : n;
  129. }
  130. BigInteger BigInteger::getBitRange (int startBit, int numBits) const
  131. {
  132. BigInteger r;
  133. numBits = jmin (numBits, getHighestBit() + 1 - startBit);
  134. r.ensureSize ((size_t) bitToIndex (numBits));
  135. r.highestBit = numBits;
  136. int i = 0;
  137. while (numBits > 0)
  138. {
  139. r.values[i++] = getBitRangeAsInt (startBit, (int) jmin (32, numBits));
  140. numBits -= 32;
  141. startBit += 32;
  142. }
  143. r.highestBit = r.getHighestBit();
  144. return r;
  145. }
  146. uint32 BigInteger::getBitRangeAsInt (const int startBit, int numBits) const noexcept
  147. {
  148. if (numBits > 32)
  149. {
  150. jassertfalse; // use getBitRange() if you need more than 32 bits..
  151. numBits = 32;
  152. }
  153. numBits = jmin (numBits, highestBit + 1 - startBit);
  154. if (numBits <= 0)
  155. return 0;
  156. const size_t pos = bitToIndex (startBit);
  157. const int offset = startBit & 31;
  158. const int endSpace = 32 - numBits;
  159. uint32 n = ((uint32) values [pos]) >> offset;
  160. if (offset > endSpace)
  161. n |= ((uint32) values [pos + 1]) << (32 - offset);
  162. return n & (((uint32) 0xffffffff) >> endSpace);
  163. }
  164. void BigInteger::setBitRangeAsInt (const int startBit, int numBits, uint32 valueToSet)
  165. {
  166. if (numBits > 32)
  167. {
  168. jassertfalse;
  169. numBits = 32;
  170. }
  171. for (int i = 0; i < numBits; ++i)
  172. {
  173. setBit (startBit + i, (valueToSet & 1) != 0);
  174. valueToSet >>= 1;
  175. }
  176. }
  177. //==============================================================================
  178. void BigInteger::clear()
  179. {
  180. if (numValues > 16)
  181. {
  182. numValues = 4;
  183. values.calloc (numValues + 1);
  184. }
  185. else
  186. {
  187. values.clear (numValues + 1);
  188. }
  189. highestBit = -1;
  190. negative = false;
  191. }
  192. void BigInteger::setBit (const int bit)
  193. {
  194. if (bit >= 0)
  195. {
  196. if (bit > highestBit)
  197. {
  198. ensureSize (bitToIndex (bit));
  199. highestBit = bit;
  200. }
  201. values [bitToIndex (bit)] |= bitToMask (bit);
  202. }
  203. }
  204. void BigInteger::setBit (const int bit, const bool shouldBeSet)
  205. {
  206. if (shouldBeSet)
  207. setBit (bit);
  208. else
  209. clearBit (bit);
  210. }
  211. void BigInteger::clearBit (const int bit) noexcept
  212. {
  213. if (bit >= 0 && bit <= highestBit)
  214. values [bitToIndex (bit)] &= ~bitToMask (bit);
  215. }
  216. void BigInteger::setRange (int startBit, int numBits, const bool shouldBeSet)
  217. {
  218. while (--numBits >= 0)
  219. setBit (startBit++, shouldBeSet);
  220. }
  221. void BigInteger::insertBit (const int bit, const bool shouldBeSet)
  222. {
  223. if (bit >= 0)
  224. shiftBits (1, bit);
  225. setBit (bit, shouldBeSet);
  226. }
  227. //==============================================================================
  228. bool BigInteger::isZero() const noexcept
  229. {
  230. return getHighestBit() < 0;
  231. }
  232. bool BigInteger::isOne() const noexcept
  233. {
  234. return getHighestBit() == 0 && ! negative;
  235. }
  236. bool BigInteger::isNegative() const noexcept
  237. {
  238. return negative && ! isZero();
  239. }
  240. void BigInteger::setNegative (const bool neg) noexcept
  241. {
  242. negative = neg;
  243. }
  244. void BigInteger::negate() noexcept
  245. {
  246. negative = (! negative) && ! isZero();
  247. }
  248. #if JUCE_USE_INTRINSICS && ! defined (__INTEL_COMPILER)
  249. #pragma intrinsic (_BitScanReverse)
  250. #endif
  251. namespace BitFunctions
  252. {
  253. inline int countBitsInInt32 (uint32 n) noexcept
  254. {
  255. n -= ((n >> 1) & 0x55555555);
  256. n = (((n >> 2) & 0x33333333) + (n & 0x33333333));
  257. n = (((n >> 4) + n) & 0x0f0f0f0f);
  258. n += (n >> 8);
  259. n += (n >> 16);
  260. return (int) (n & 0x3f);
  261. }
  262. inline int highestBitInInt (uint32 n) noexcept
  263. {
  264. jassert (n != 0); // (the built-in functions may not work for n = 0)
  265. #if JUCE_GCC
  266. return 31 - __builtin_clz (n);
  267. #elif JUCE_USE_INTRINSICS
  268. unsigned long highest;
  269. _BitScanReverse (&highest, n);
  270. return (int) highest;
  271. #else
  272. n |= (n >> 1);
  273. n |= (n >> 2);
  274. n |= (n >> 4);
  275. n |= (n >> 8);
  276. n |= (n >> 16);
  277. return countBitsInInt32 (n >> 1);
  278. #endif
  279. }
  280. }
  281. int BigInteger::countNumberOfSetBits() const noexcept
  282. {
  283. int total = 0;
  284. for (int i = (int) bitToIndex (highestBit) + 1; --i >= 0;)
  285. total += BitFunctions::countBitsInInt32 (values[i]);
  286. return total;
  287. }
  288. int BigInteger::getHighestBit() const noexcept
  289. {
  290. for (int i = (int) bitToIndex (highestBit + 1); i >= 0; --i)
  291. {
  292. const uint32 n = values[i];
  293. if (n != 0)
  294. return BitFunctions::highestBitInInt (n) + (i << 5);
  295. }
  296. return -1;
  297. }
  298. int BigInteger::findNextSetBit (int i) const noexcept
  299. {
  300. for (; i <= highestBit; ++i)
  301. if ((values [bitToIndex (i)] & bitToMask (i)) != 0)
  302. return i;
  303. return -1;
  304. }
  305. int BigInteger::findNextClearBit (int i) const noexcept
  306. {
  307. for (; i <= highestBit; ++i)
  308. if ((values [bitToIndex (i)] & bitToMask (i)) == 0)
  309. break;
  310. return i;
  311. }
  312. //==============================================================================
  313. BigInteger& BigInteger::operator+= (const BigInteger& other)
  314. {
  315. if (other.isNegative())
  316. return operator-= (-other);
  317. if (isNegative())
  318. {
  319. if (compareAbsolute (other) < 0)
  320. {
  321. BigInteger temp (*this);
  322. temp.negate();
  323. *this = other;
  324. operator-= (temp);
  325. }
  326. else
  327. {
  328. negate();
  329. operator-= (other);
  330. negate();
  331. }
  332. }
  333. else
  334. {
  335. if (other.highestBit > highestBit)
  336. highestBit = other.highestBit;
  337. ++highestBit;
  338. const size_t numInts = bitToIndex (highestBit) + 1;
  339. ensureSize (numInts);
  340. int64 remainder = 0;
  341. for (size_t i = 0; i <= numInts; ++i)
  342. {
  343. if (i < numValues)
  344. remainder += values[i];
  345. if (i < other.numValues)
  346. remainder += other.values[i];
  347. values[i] = (uint32) remainder;
  348. remainder >>= 32;
  349. }
  350. jassert (remainder == 0);
  351. highestBit = getHighestBit();
  352. }
  353. return *this;
  354. }
  355. BigInteger& BigInteger::operator-= (const BigInteger& other)
  356. {
  357. if (other.isNegative())
  358. return operator+= (-other);
  359. if (! isNegative())
  360. {
  361. if (compareAbsolute (other) < 0)
  362. {
  363. BigInteger temp (other);
  364. swapWith (temp);
  365. operator-= (temp);
  366. negate();
  367. return *this;
  368. }
  369. }
  370. else
  371. {
  372. negate();
  373. operator+= (other);
  374. negate();
  375. return *this;
  376. }
  377. const size_t numInts = bitToIndex (highestBit) + 1;
  378. const size_t maxOtherInts = bitToIndex (other.highestBit) + 1;
  379. int64 amountToSubtract = 0;
  380. for (size_t i = 0; i <= numInts; ++i)
  381. {
  382. if (i <= maxOtherInts)
  383. amountToSubtract += (int64) other.values[i];
  384. if (values[i] >= amountToSubtract)
  385. {
  386. values[i] = (uint32) (values[i] - amountToSubtract);
  387. amountToSubtract = 0;
  388. }
  389. else
  390. {
  391. const int64 n = ((int64) values[i] + (((int64) 1) << 32)) - amountToSubtract;
  392. values[i] = (uint32) n;
  393. amountToSubtract = 1;
  394. }
  395. }
  396. return *this;
  397. }
  398. BigInteger& BigInteger::operator*= (const BigInteger& other)
  399. {
  400. BigInteger total;
  401. highestBit = getHighestBit();
  402. const bool wasNegative = isNegative();
  403. setNegative (false);
  404. for (int i = 0; i <= highestBit; ++i)
  405. {
  406. if (operator[](i))
  407. {
  408. BigInteger n (other);
  409. n.setNegative (false);
  410. n <<= i;
  411. total += n;
  412. }
  413. }
  414. total.setNegative (wasNegative ^ other.isNegative());
  415. swapWith (total);
  416. return *this;
  417. }
  418. void BigInteger::divideBy (const BigInteger& divisor, BigInteger& remainder)
  419. {
  420. jassert (this != &remainder); // (can't handle passing itself in to get the remainder)
  421. const int divHB = divisor.getHighestBit();
  422. const int ourHB = getHighestBit();
  423. if (divHB < 0 || ourHB < 0)
  424. {
  425. // division by zero
  426. remainder.clear();
  427. clear();
  428. }
  429. else
  430. {
  431. const bool wasNegative = isNegative();
  432. swapWith (remainder);
  433. remainder.setNegative (false);
  434. clear();
  435. BigInteger temp (divisor);
  436. temp.setNegative (false);
  437. int leftShift = ourHB - divHB;
  438. temp <<= leftShift;
  439. while (leftShift >= 0)
  440. {
  441. if (remainder.compareAbsolute (temp) >= 0)
  442. {
  443. remainder -= temp;
  444. setBit (leftShift);
  445. }
  446. if (--leftShift >= 0)
  447. temp >>= 1;
  448. }
  449. negative = wasNegative ^ divisor.isNegative();
  450. remainder.setNegative (wasNegative);
  451. }
  452. }
  453. BigInteger& BigInteger::operator/= (const BigInteger& other)
  454. {
  455. BigInteger remainder;
  456. divideBy (other, remainder);
  457. return *this;
  458. }
  459. BigInteger& BigInteger::operator|= (const BigInteger& other)
  460. {
  461. // this operation doesn't take into account negative values..
  462. jassert (isNegative() == other.isNegative());
  463. if (other.highestBit >= 0)
  464. {
  465. ensureSize (bitToIndex (other.highestBit));
  466. int n = (int) bitToIndex (other.highestBit) + 1;
  467. while (--n >= 0)
  468. values[n] |= other.values[n];
  469. if (other.highestBit > highestBit)
  470. highestBit = other.highestBit;
  471. highestBit = getHighestBit();
  472. }
  473. return *this;
  474. }
  475. BigInteger& BigInteger::operator&= (const BigInteger& other)
  476. {
  477. // this operation doesn't take into account negative values..
  478. jassert (isNegative() == other.isNegative());
  479. int n = (int) numValues;
  480. while (n > (int) other.numValues)
  481. values[--n] = 0;
  482. while (--n >= 0)
  483. values[n] &= other.values[n];
  484. if (other.highestBit < highestBit)
  485. highestBit = other.highestBit;
  486. highestBit = getHighestBit();
  487. return *this;
  488. }
  489. BigInteger& BigInteger::operator^= (const BigInteger& other)
  490. {
  491. // this operation will only work with the absolute values
  492. jassert (isNegative() == other.isNegative());
  493. if (other.highestBit >= 0)
  494. {
  495. ensureSize (bitToIndex (other.highestBit));
  496. int n = (int) bitToIndex (other.highestBit) + 1;
  497. while (--n >= 0)
  498. values[n] ^= other.values[n];
  499. if (other.highestBit > highestBit)
  500. highestBit = other.highestBit;
  501. highestBit = getHighestBit();
  502. }
  503. return *this;
  504. }
  505. BigInteger& BigInteger::operator%= (const BigInteger& divisor)
  506. {
  507. BigInteger remainder;
  508. divideBy (divisor, remainder);
  509. swapWith (remainder);
  510. return *this;
  511. }
  512. BigInteger& BigInteger::operator++() { return operator+= (1); }
  513. BigInteger& BigInteger::operator--() { return operator-= (1); }
  514. BigInteger BigInteger::operator++ (int) { const BigInteger old (*this); operator+= (1); return old; }
  515. BigInteger BigInteger::operator-- (int) { const BigInteger old (*this); operator-= (1); return old; }
  516. BigInteger BigInteger::operator-() const { BigInteger b (*this); b.negate(); return b; }
  517. BigInteger BigInteger::operator+ (const BigInteger& other) const { BigInteger b (*this); return b += other; }
  518. BigInteger BigInteger::operator- (const BigInteger& other) const { BigInteger b (*this); return b -= other; }
  519. BigInteger BigInteger::operator* (const BigInteger& other) const { BigInteger b (*this); return b *= other; }
  520. BigInteger BigInteger::operator/ (const BigInteger& other) const { BigInteger b (*this); return b /= other; }
  521. BigInteger BigInteger::operator| (const BigInteger& other) const { BigInteger b (*this); return b |= other; }
  522. BigInteger BigInteger::operator& (const BigInteger& other) const { BigInteger b (*this); return b &= other; }
  523. BigInteger BigInteger::operator^ (const BigInteger& other) const { BigInteger b (*this); return b ^= other; }
  524. BigInteger BigInteger::operator% (const BigInteger& other) const { BigInteger b (*this); return b %= other; }
  525. BigInteger BigInteger::operator<< (const int numBits) const { BigInteger b (*this); return b <<= numBits; }
  526. BigInteger BigInteger::operator>> (const int numBits) const { BigInteger b (*this); return b >>= numBits; }
  527. BigInteger& BigInteger::operator<<= (const int numBits) { shiftBits (numBits, 0); return *this; }
  528. BigInteger& BigInteger::operator>>= (const int numBits) { shiftBits (-numBits, 0); return *this; }
  529. //==============================================================================
  530. int BigInteger::compare (const BigInteger& other) const noexcept
  531. {
  532. if (isNegative() == other.isNegative())
  533. {
  534. const int absComp = compareAbsolute (other);
  535. return isNegative() ? -absComp : absComp;
  536. }
  537. else
  538. {
  539. return isNegative() ? -1 : 1;
  540. }
  541. }
  542. int BigInteger::compareAbsolute (const BigInteger& other) const noexcept
  543. {
  544. const int h1 = getHighestBit();
  545. const int h2 = other.getHighestBit();
  546. if (h1 > h2)
  547. return 1;
  548. else if (h1 < h2)
  549. return -1;
  550. for (int i = (int) bitToIndex (h1) + 1; --i >= 0;)
  551. if (values[i] != other.values[i])
  552. return (values[i] > other.values[i]) ? 1 : -1;
  553. return 0;
  554. }
  555. bool BigInteger::operator== (const BigInteger& other) const noexcept { return compare (other) == 0; }
  556. bool BigInteger::operator!= (const BigInteger& other) const noexcept { return compare (other) != 0; }
  557. bool BigInteger::operator< (const BigInteger& other) const noexcept { return compare (other) < 0; }
  558. bool BigInteger::operator<= (const BigInteger& other) const noexcept { return compare (other) <= 0; }
  559. bool BigInteger::operator> (const BigInteger& other) const noexcept { return compare (other) > 0; }
  560. bool BigInteger::operator>= (const BigInteger& other) const noexcept { return compare (other) >= 0; }
  561. //==============================================================================
  562. void BigInteger::shiftLeft (int bits, const int startBit)
  563. {
  564. if (startBit > 0)
  565. {
  566. for (int i = highestBit + 1; --i >= startBit;)
  567. setBit (i + bits, operator[] (i));
  568. while (--bits >= 0)
  569. clearBit (bits + startBit);
  570. }
  571. else
  572. {
  573. ensureSize (bitToIndex (highestBit + bits) + 1);
  574. const size_t wordsToMove = bitToIndex (bits);
  575. size_t top = 1 + bitToIndex (highestBit);
  576. highestBit += bits;
  577. if (wordsToMove > 0)
  578. {
  579. for (int i = (int) top; --i >= 0;)
  580. values [i + wordsToMove] = values [i];
  581. for (size_t j = 0; j < wordsToMove; ++j)
  582. values [j] = 0;
  583. bits &= 31;
  584. }
  585. if (bits != 0)
  586. {
  587. const int invBits = 32 - bits;
  588. for (size_t i = top + 1 + wordsToMove; --i > wordsToMove;)
  589. values[i] = (values[i] << bits) | (values [i - 1] >> invBits);
  590. values [wordsToMove] = values [wordsToMove] << bits;
  591. }
  592. highestBit = getHighestBit();
  593. }
  594. }
  595. void BigInteger::shiftRight (int bits, const int startBit)
  596. {
  597. if (startBit > 0)
  598. {
  599. for (int i = startBit; i <= highestBit; ++i)
  600. setBit (i, operator[] (i + bits));
  601. highestBit = getHighestBit();
  602. }
  603. else
  604. {
  605. if (bits > highestBit)
  606. {
  607. clear();
  608. }
  609. else
  610. {
  611. const size_t wordsToMove = bitToIndex (bits);
  612. size_t top = 1 + bitToIndex (highestBit) - wordsToMove;
  613. highestBit -= bits;
  614. if (wordsToMove > 0)
  615. {
  616. size_t i;
  617. for (i = 0; i < top; ++i)
  618. values [i] = values [i + wordsToMove];
  619. for (i = 0; i < wordsToMove; ++i)
  620. values [top + i] = 0;
  621. bits &= 31;
  622. }
  623. if (bits != 0)
  624. {
  625. const int invBits = 32 - bits;
  626. --top;
  627. for (size_t i = 0; i < top; ++i)
  628. values[i] = (values[i] >> bits) | (values [i + 1] << invBits);
  629. values[top] = (values[top] >> bits);
  630. }
  631. highestBit = getHighestBit();
  632. }
  633. }
  634. }
  635. void BigInteger::shiftBits (int bits, const int startBit)
  636. {
  637. if (highestBit >= 0)
  638. {
  639. if (bits < 0)
  640. shiftRight (-bits, startBit);
  641. else if (bits > 0)
  642. shiftLeft (bits, startBit);
  643. }
  644. }
  645. //==============================================================================
  646. static BigInteger simpleGCD (BigInteger* m, BigInteger* n)
  647. {
  648. while (! m->isZero())
  649. {
  650. if (n->compareAbsolute (*m) > 0)
  651. std::swap (m, n);
  652. *m -= *n;
  653. }
  654. return *n;
  655. }
  656. BigInteger BigInteger::findGreatestCommonDivisor (BigInteger n) const
  657. {
  658. BigInteger m (*this);
  659. while (! n.isZero())
  660. {
  661. if (abs (m.getHighestBit() - n.getHighestBit()) <= 16)
  662. return simpleGCD (&m, &n);
  663. BigInteger temp2;
  664. m.divideBy (n, temp2);
  665. m.swapWith (n);
  666. n.swapWith (temp2);
  667. }
  668. return m;
  669. }
  670. void BigInteger::exponentModulo (const BigInteger& exponent, const BigInteger& modulus)
  671. {
  672. BigInteger exp (exponent);
  673. exp %= modulus;
  674. BigInteger value (1);
  675. swapWith (value);
  676. value %= modulus;
  677. while (! exp.isZero())
  678. {
  679. if (exp [0])
  680. {
  681. operator*= (value);
  682. operator%= (modulus);
  683. }
  684. value *= value;
  685. value %= modulus;
  686. exp >>= 1;
  687. }
  688. }
  689. void BigInteger::inverseModulo (const BigInteger& modulus)
  690. {
  691. if (modulus.isOne() || modulus.isNegative())
  692. {
  693. clear();
  694. return;
  695. }
  696. if (isNegative() || compareAbsolute (modulus) >= 0)
  697. operator%= (modulus);
  698. if (isOne())
  699. return;
  700. if (! (*this)[0])
  701. {
  702. // not invertible
  703. clear();
  704. return;
  705. }
  706. BigInteger a1 (modulus);
  707. BigInteger a2 (*this);
  708. BigInteger b1 (modulus);
  709. BigInteger b2 (1);
  710. while (! a2.isOne())
  711. {
  712. BigInteger temp1, multiplier (a1);
  713. multiplier.divideBy (a2, temp1);
  714. temp1 = a2;
  715. temp1 *= multiplier;
  716. BigInteger temp2 (a1);
  717. temp2 -= temp1;
  718. a1 = a2;
  719. a2 = temp2;
  720. temp1 = b2;
  721. temp1 *= multiplier;
  722. temp2 = b1;
  723. temp2 -= temp1;
  724. b1 = b2;
  725. b2 = temp2;
  726. }
  727. while (b2.isNegative())
  728. b2 += modulus;
  729. b2 %= modulus;
  730. swapWith (b2);
  731. }
  732. //==============================================================================
  733. OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const BigInteger& value)
  734. {
  735. return stream << value.toString (10);
  736. }
  737. String BigInteger::toString (const int base, const int minimumNumCharacters) const
  738. {
  739. String s;
  740. BigInteger v (*this);
  741. if (base == 2 || base == 8 || base == 16)
  742. {
  743. const int bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
  744. static const char hexDigits[] = "0123456789abcdef";
  745. for (;;)
  746. {
  747. const uint32 remainder = v.getBitRangeAsInt (0, bits);
  748. v >>= bits;
  749. if (remainder == 0 && v.isZero())
  750. break;
  751. s = String::charToString ((juce_wchar) (uint8) hexDigits [remainder]) + s;
  752. }
  753. }
  754. else if (base == 10)
  755. {
  756. const BigInteger ten (10);
  757. BigInteger remainder;
  758. for (;;)
  759. {
  760. v.divideBy (ten, remainder);
  761. if (remainder.isZero() && v.isZero())
  762. break;
  763. s = String (remainder.getBitRangeAsInt (0, 8)) + s;
  764. }
  765. }
  766. else
  767. {
  768. jassertfalse; // can't do the specified base!
  769. return String::empty;
  770. }
  771. s = s.paddedLeft ('0', minimumNumCharacters);
  772. return isNegative() ? "-" + s : s;
  773. }
  774. void BigInteger::parseString (const String& text, const int base)
  775. {
  776. clear();
  777. String::CharPointerType t (text.getCharPointer());
  778. if (base == 2 || base == 8 || base == 16)
  779. {
  780. const int bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
  781. for (;;)
  782. {
  783. const juce_wchar c = t.getAndAdvance();
  784. const int digit = CharacterFunctions::getHexDigitValue (c);
  785. if (((uint32) digit) < (uint32) base)
  786. {
  787. operator<<= (bits);
  788. operator+= (digit);
  789. }
  790. else if (c == 0)
  791. {
  792. break;
  793. }
  794. }
  795. }
  796. else if (base == 10)
  797. {
  798. const BigInteger ten ((uint32) 10);
  799. for (;;)
  800. {
  801. const juce_wchar c = t.getAndAdvance();
  802. if (c >= '0' && c <= '9')
  803. {
  804. operator*= (ten);
  805. operator+= ((int) (c - '0'));
  806. }
  807. else if (c == 0)
  808. {
  809. break;
  810. }
  811. }
  812. }
  813. setNegative (text.trimStart().startsWithChar ('-'));
  814. }
  815. MemoryBlock BigInteger::toMemoryBlock() const
  816. {
  817. const int numBytes = (getHighestBit() + 8) >> 3;
  818. MemoryBlock mb ((size_t) numBytes);
  819. for (int i = 0; i < numBytes; ++i)
  820. mb[i] = (char) getBitRangeAsInt (i << 3, 8);
  821. return mb;
  822. }
  823. void BigInteger::loadFromMemoryBlock (const MemoryBlock& data)
  824. {
  825. clear();
  826. for (int i = (int) data.getSize(); --i >= 0;)
  827. this->setBitRangeAsInt (i << 3, 8, (uint32) data [i]);
  828. }