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.

988 lines
25KB

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