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.

1022 lines
27KB

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