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.

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