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