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.

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