28 inline uint32 bitToMask (
const int bit)
noexcept {
return (
uint32) 1 << (bit & 31); }
29 inline size_t bitToIndex (
const int bit)
noexcept {
return (
size_t) (bit >> 5); }
30 inline size_t sizeNeededToHold (
int highestBit)
noexcept {
return (
size_t) (highestBit >> 5) + 1; }
37 #if JUCE_GCC || JUCE_CLANG
38 return 31 - __builtin_clz (
n);
40 unsigned long highest;
41 _BitScanReverse (&highest,
n);
141 std::swap (
negative, other.negative);
198bool BigInteger::operator[] (
const int bit)
const noexcept
200 return bit <= highestBit && bit >= 0
201 && ((
getValues() [bitToIndex (bit)] & bitToMask (bit)) != 0);
213 auto n = (((
int64) (values[1] & 0x7fffffff)) << 32) | values[0];
221 auto* destValues =
r.ensureSize (sizeNeededToHold (numBits));
222 r.highestBit = numBits;
224 for (
int i = 0; numBits > 0;)
231 r.highestBit =
r.getHighestBit();
248 auto pos = bitToIndex (startBit);
249 auto offset = startBit & 31;
250 auto endSpace = 32 - numBits;
253 auto n = ((
uint32) values [pos]) >> offset;
255 if (offset > endSpace)
256 n |= ((
uint32) values [pos + 1]) << (32 - offset);
258 return n & (((
uint32) 0xffffffff) >> endSpace);
269 for (
int i = 0;
i < numBits; ++
i)
271 setBit (startBit +
i, (valueToSet & 1) != 0);
298 getValues() [bitToIndex (bit)] |= bitToMask (bit);
314 getValues() [bitToIndex (bit)] &= ~bitToMask (bit);
323 while (--numBits >= 0)
324 setBit (startBit++, shouldBeSet);
332 setBit (bit, shouldBeSet);
361#if JUCE_MSVC && ! defined (__INTEL_COMPILER)
362 #pragma intrinsic (_BitScanReverse)
370 for (
int i = (
int) sizeNeededToHold (
highestBit); --
i >= 0;)
392 if ((values [bitToIndex (
i)] & bitToMask (
i)) != 0)
403 if ((values [bitToIndex (
i)] & bitToMask (
i)) == 0)
416 return operator-= (-other);
443 for (
size_t i = 0;
i < numInts; ++
i)
445 remainder += values[
i];
448 remainder += otherValues[
i];
450 values[
i] = (
uint32) remainder;
470 return operator+= (-other);
490 auto maxOtherInts = sizeNeededToHold (other.
getHighestBit());
491 jassert (numInts >= maxOtherInts);
494 int64 amountToSubtract = 0;
496 for (
size_t i = 0;
i < numInts; ++
i)
498 if (
i < maxOtherInts)
499 amountToSubtract += (
int64) otherValues[
i];
501 if (values[
i] >= amountToSubtract)
503 values[
i] = (
uint32) (values[
i] - amountToSubtract);
504 amountToSubtract = 0;
510 amountToSubtract = 1;
537 m.setNegative (
false);
539 auto* mValues =
m.getValues();
542 for (
int i = 0;
i <=
t; ++
i)
546 for (
int j = 0;
j <=
n; ++
j)
550 c =
static_cast<uint32> (uv >> 32);
553 totalValues[
i +
n + 1] =
c;
565 if (
this == &divisor)
573 if (divHB < 0 || ourHB < 0)
590 auto leftShift = ourHB - divHB;
593 while (leftShift >= 0)
601 if (--leftShift >= 0)
633 values[
n] |= otherValues[
n];
661 values[
n] &= otherValues[
n];
689 values[
n] ^= otherValues[
n];
710BigInteger BigInteger::operator++ (
int) {
const auto old (*
this); operator+= (1);
return old; }
711BigInteger BigInteger::operator-- (
int) {
const auto old (*
this); operator-= (1);
return old; }
722BigInteger BigInteger::operator<< (
const int numBits)
const {
auto b (*
this);
return b <<= numBits; }
723BigInteger BigInteger::operator>> (
const int numBits)
const {
auto b (*
this);
return b >>= numBits; }
732 if (isNeg == other.isNegative())
735 return isNeg ? -absComp : absComp;
738 return isNeg ? -1 : 1;
744 auto h2 = other.getHighestBit();
746 if (h1 > h2)
return 1;
747 if (h1 < h2)
return -1;
750 auto* otherValues = other.getValues();
752 for (
int i = (
int) bitToIndex (h1);
i >= 0; --
i)
753 if (values[
i] != otherValues[
i])
754 return values[
i] > otherValues[
i] ? 1 : -1;
759bool BigInteger::operator== (
const BigInteger& other)
const noexcept {
return compare (other) == 0; }
760bool BigInteger::operator!= (
const BigInteger& other)
const noexcept {
return compare (other) != 0; }
761bool BigInteger::operator< (
const BigInteger& other)
const noexcept {
return compare (other) < 0; }
762bool BigInteger::operator<= (
const BigInteger& other)
const noexcept {
return compare (other) <= 0; }
763bool BigInteger::operator> (
const BigInteger& other)
const noexcept {
return compare (other) > 0; }
764bool BigInteger::operator>= (
const BigInteger& other)
const noexcept {
return compare (other) >= 0; }
780 auto wordsToMove = bitToIndex (bits);
781 auto numOriginalInts = bitToIndex (
highestBit);
786 for (
int i = (
int) numOriginalInts;
i >= 0; --
i)
787 values[(
size_t)
i + wordsToMove] = values[
i];
789 for (
size_t j = 0;
j < wordsToMove; ++
j)
797 auto invBits = 32 - bits;
799 for (
size_t i = bitToIndex (
highestBit);
i > wordsToMove; --
i)
800 values[
i] = (values[
i] << bits) | (values[
i - 1] >> invBits);
802 values[wordsToMove] = values[wordsToMove] << bits;
826 auto wordsToMove = bitToIndex (bits);
827 auto top = 1 + bitToIndex (
highestBit) - wordsToMove;
833 for (
size_t i = 0;
i < top; ++
i)
834 values[
i] = values[
i + wordsToMove];
836 for (
size_t i = 0;
i < wordsToMove; ++
i)
844 auto invBits = 32 - bits;
847 for (
size_t i = 0;
i < top; ++
i)
848 values[
i] = (values[
i] >> bits) | (values[
i + 1] << invBits);
850 values[top] = (values[top] >> bits);
872 while (!
m->isZero())
874 if (
n->compareAbsolute (*
m) > 0)
889 if (std::abs (
m.getHighestBit() -
n.getHighestBit()) <= 16)
911 auto n = exp.getHighestBit();
913 for (
int i =
n; --
i >= 0;)
931 g.extendedEuclidean (modulus, R, m1, R1);
937 for (
int i = exp.getHighestBit(); --
i >= 0;)
950 auto am = (*
this * R) % modulus;
952 auto um = R % modulus;
954 for (
int i = exp.getHighestBit(); --
i >= 0;)
956 xm.montgomeryMultiplication (xm, modulus, m1, Rfactor);
959 xm.montgomeryMultiplication (am, modulus, m1, Rfactor);
996 tempValues.
add (
p /
q);
1005 for (
int i = 1;
i < tempValues.
size(); ++
i)
1046 b1 (modulus), b2 (1);
1048 while (! a2.isOne())
1054 temp1 *= multiplier;
1061 temp1 *= multiplier;
1078 return stream <<
value.toString (10);
1086 if (base == 2 || base == 8 || base == 16)
1088 auto bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
1089 static const char hexDigits[] =
"0123456789abcdef";
1093 auto remainder =
v.getBitRangeAsInt (0, bits);
1096 if (remainder == 0 &&
v.isZero())
1102 else if (base == 10)
1109 v.divideBy (ten, remainder);
1111 if (remainder.
isZero() &&
v.isZero())
1123 s =
s.paddedLeft (
'0', minimumNumCharacters);
1131 auto t =
text.text.findEndOfWhitespace();
1135 if (base == 2 || base == 8 || base == 16)
1137 auto bits = (base == 2) ? 1 : (base == 8 ? 3 : 4);
1141 auto c =
t.getAndAdvance();
1155 else if (base == 10)
1161 auto c =
t.getAndAdvance();
1163 if (
c >=
'0' &&
c <=
'9')
1166 *
this += (
int) (
c -
'0');
1182 for (
int i = 0;
i < numBytes; ++
i)
1183 mb[
i] = (
char) ((values[
i / 4] >> ((
i & 3) * 8)) & 0xff);
1190 auto numBytes =
data.getSize();
1191 auto numInts = 1 + (numBytes /
sizeof (
uint32));
1194 for (
int i = 0;
i < (
int) numInts - 1; ++
i)
1197 values[numInts - 1] = 0;
1199 for (
int i = (
int) (numBytes & ~3u);
i < (
int) numBytes; ++
i)
1210 jassert (numBits > 0 && numBits <= 32);
1215 if (
const uint32 offset = (startBit & 7))
1217 const uint32 bitsInByte = 8 - offset;
1220 if (bitsInByte >= numBits)
1222 *
data = (
uint8) ((current & ~(((1u << numBits) - 1u) << offset)) | (
value << offset));
1226 *
data++ = current ^ (
uint8) (((
value << offset) ^ current) & (((1u << bitsInByte) - 1u) << offset));
1227 numBits -= bitsInByte;
1228 value >>= bitsInByte;
1231 while (numBits >= 8)
1245 jassert (numBits > 0 && numBits <= 32);
1249 const uint8*
data =
static_cast<const uint8*
> (buffer) + startBit / 8;
1251 if (
const uint32 offset = (startBit & 7))
1253 const uint32 bitsInByte = 8 - offset;
1256 if (bitsInByte >= numBits)
1257 return result & ((1u << numBits) - 1u);
1259 numBits -= bitsInByte;
1260 bitsRead += bitsInByte;
1264 while (numBits >= 8)
1272 result |= ((*
data & ((1u << numBits) - 1u)) << bitsRead);
1282class BigIntegerTests :
public UnitTest
1286 : UnitTest (
"BigInteger", UnitTestCategories::maths)
1289 static BigInteger getBigRandom (Random&
r)
1294 r.fillBitsRandomly (
b, 0,
r.nextInt (150) + 1);
1299 void runTest()
override
1302 beginTest (
"BigInteger");
1304 Random
r = getRandom();
1306 expect (BigInteger().isZero());
1307 expect (BigInteger(1).isOne());
1309 for (
int j = 10000; --
j >= 0;)
1311 BigInteger b1 (getBigRandom(
r)),
1312 b2 (getBigRandom(
r));
1314 BigInteger b3 = b1 + b2;
1315 expect (b3 > b1 && b3 > b2);
1316 expect (b3 - b1 == b2);
1317 expect (b3 - b2 == b1);
1319 BigInteger b4 = b1 * b2;
1320 expect (b4 > b1 && b4 > b2);
1321 expect (b4 / b1 == b2);
1322 expect (b4 / b2 == b1);
1323 expect (((b4 << 1) >> 1) == b4);
1324 expect (((b4 << 10) >> 10) == b4);
1325 expect (((b4 << 100) >> 100) == b4);
1330 b5.loadFromMemoryBlock (b3.toMemoryBlock());
1336 beginTest (
"Bit setting");
1338 Random
r = getRandom();
1341 for (
int j = 100000; --
j >= 0;)
1348 value &= ((1u << num) - 1);
1356 expect (old1 == readLittleEndianBitsInBuffer (
test, offset - 6, 6));
1357 expect (old2 == readLittleEndianBitsInBuffer (
test, offset + num, 6));
1363static BigIntegerTests bigIntegerTests;
Type jmin(const Type a, const Type b)
Definition MathsFunctions.h:60
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
static const unsigned char temp2[]
Definition DistrhoArtwork3BandEQ.cpp:2750
static const unsigned char temp1[]
Definition DistrhoArtwork3BandEQ.cpp:5
#define noexcept
Definition DistrhoDefines.h:72
uint8_t a
Definition Spc_Cpu.h:141
ostream & operator<<(ostream &out, const MidiEvent &ev)
Definition InMgr.cpp:9
uint8_t uint8
Definition basics.h:86
uint32_t uint32
Definition basics.h:90
static uint32 littleEndianInt(const void *bytes) noexcept
Definition ByteOrder.h:236
static String charToString(water_uchar character)
Definition String.cpp:311
Definition juce_Array.h:56
int size() const noexcept
Definition juce_Array.h:215
void add(const ElementType &newElement)
Definition juce_Array.h:418
ElementType & getReference(int index) noexcept
Definition juce_Array.h:267
Definition juce_BigInteger.h:39
MemoryBlock toMemoryBlock() const
Definition juce_BigInteger.cpp:1176
@ numPreallocatedInts
Definition juce_BigInteger.h:324
void shiftLeft(int bits, int startBit)
Definition juce_BigInteger.cpp:767
bool isOne() const noexcept
Definition juce_BigInteger.cpp:341
void clearBit(int bitNumber) noexcept
Definition juce_BigInteger.cpp:310
BigInteger getBitRange(int startBit, int numBits) const
Definition juce_BigInteger.cpp:217
void parseString(StringRef text, int base)
Definition juce_BigInteger.cpp:1128
uint32 preallocated[numPreallocatedInts]
Definition juce_BigInteger.h:326
int64 toInt64() const noexcept
Definition juce_BigInteger.cpp:210
BigInteger & operator++()
Definition juce_BigInteger.cpp:708
void exponentModulo(const BigInteger &exponent, const BigInteger &modulus)
Definition juce_BigInteger.cpp:902
void setNegative(bool shouldBeNegative) noexcept
Definition juce_BigInteger.cpp:351
uint32 getBitRangeAsInt(int startBit, int numBits) const noexcept
Definition juce_BigInteger.cpp:235
bool negative
Definition juce_BigInteger.h:329
void clear() noexcept
Definition juce_BigInteger.cpp:277
BigInteger findGreatestCommonDivisor(BigInteger other) const
Definition juce_BigInteger.cpp:883
uint32 * getValues() const noexcept
Definition juce_BigInteger.cpp:165
HeapBlock< uint32 > heapAllocation
Definition juce_BigInteger.h:325
void shiftRight(int bits, int startBit)
Definition juce_BigInteger.cpp:809
void extendedEuclidean(const BigInteger &a, const BigInteger &b, BigInteger &xOut, BigInteger &yOut)
Definition juce_BigInteger.cpp:988
void setRange(int startBit, int numBits, bool shouldBeSet)
Definition juce_BigInteger.cpp:321
void shiftBits(int howManyBitsLeft, int startBit)
Definition juce_BigInteger.cpp:858
int getHighestBit() const noexcept
Definition juce_BigInteger.cpp:376
BigInteger & operator--()
Definition juce_BigInteger.cpp:709
void divideBy(const BigInteger &divisor, BigInteger &remainder)
Definition juce_BigInteger.cpp:563
String toString(int base, int minimumNumCharacters=1) const
Definition juce_BigInteger.cpp:1081
int findNextClearBit(int startIndex) const noexcept
Definition juce_BigInteger.cpp:398
int compare(const BigInteger &other) const noexcept
Definition juce_BigInteger.cpp:728
BigInteger()
Definition juce_BigInteger.cpp:54
BigInteger operator-() const
Definition juce_BigInteger.cpp:713
size_t allocatedSize
Definition juce_BigInteger.h:327
void negate() noexcept
Definition juce_BigInteger.cpp:356
void setBit(int bitNumber)
Definition juce_BigInteger.cpp:288
void setBitRangeAsInt(int startBit, int numBits, uint32 valueToSet)
Definition juce_BigInteger.cpp:261
int findNextSetBit(int startIndex) const noexcept
Definition juce_BigInteger.cpp:387
uint32 * ensureSize(size_t)
Definition juce_BigInteger.cpp:173
void loadFromMemoryBlock(const MemoryBlock &data)
Definition juce_BigInteger.cpp:1188
bool isZero() const noexcept
Definition juce_BigInteger.cpp:336
int countNumberOfSetBits() const noexcept
Definition juce_BigInteger.cpp:365
void swapWith(BigInteger &) noexcept
Definition juce_BigInteger.cpp:133
void montgomeryMultiplication(const BigInteger &other, const BigInteger &modulus, const BigInteger &modulusp, int k)
Definition juce_BigInteger.cpp:968
bool isNegative() const noexcept
Definition juce_BigInteger.cpp:346
int highestBit
Definition juce_BigInteger.h:328
int compareAbsolute(const BigInteger &other) const noexcept
Definition juce_BigInteger.cpp:741
void insertBit(int bitNumber, bool shouldBeSet)
Definition juce_BigInteger.cpp:327
int toInteger() const noexcept
Definition juce_BigInteger.cpp:204
void inverseModulo(const BigInteger &modulus)
Definition juce_BigInteger.cpp:1025
static int getHexDigitValue(juce_wchar digit) noexcept
Definition juce_CharacterFunctions.cpp:112
Definition juce_MemoryBlock.h:33
Definition juce_OutputStream.h:38
Definition juce_String.h:53
Definition juce_StringRef.h:62
unsigned * m
Definition inflate.c:1559
struct huft * t
Definition inflate.c:943
register unsigned k
Definition inflate.c:946
register unsigned j
Definition inflate.c:1576
int y
Definition inflate.c:1588
unsigned v[N_MAX]
Definition inflate.c:1584
int g
Definition inflate.c:1573
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned x[BMAX+1]
Definition inflate.c:1586
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
Definition carla_juce.cpp:31
unsigned long long uint64
Definition juce_MathsFunctions.h:56
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
int findHighestSetBit(uint32 n) noexcept
Definition juce_BigInteger.cpp:33
unsigned int uint32
Definition juce_MathsFunctions.h:45
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
uint32 readLittleEndianBitsInBuffer(const void *buffer, uint32 startBit, uint32 numBits) noexcept
Definition juce_BigInteger.cpp:1242
long long int64
Definition juce_MathsFunctions.h:54
void writeLittleEndianBitsInBuffer(void *buffer, uint32 startBit, uint32 numBits, uint32 value) noexcept
Definition juce_BigInteger.cpp:1207
wchar_t juce_wchar
Definition juce_CharacterFunctions.h:42
int countNumberOfBits(uint32 n) noexcept
Definition juce_MathsFunctions.h:551
static const char hexDigits[]
Definition juce_String.cpp:1899
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
Definition juce_Memory.h:111
signed int int32
Definition juce_MathsFunctions.h:43
unsigned char uint8
Definition juce_MathsFunctions.h:37
static BigInteger simpleGCD(BigInteger *m, BigInteger *n)
Definition juce_BigInteger.cpp:870
static int test(SerdEnv *env, bool top_level, bool pretty_numbers)
Definition sratom_test.c:79
const char * text
Definition swell-functions.h:167
int n
Definition crypt.c:458
uch * p
Definition crypt.c:594
return c
Definition crypt.c:175
memcpy(hh, h, RAND_HEAD_LEN)
int r
Definition crypt.c:458
register uch * q
Definition fileio.c:817
int result
Definition process.c:1455
typedef int(UZ_EXP MsgFn)()
int negative
Definition zipinfo.c:455
#define const
Definition zconf.h:137