OpenShot Audio Library | OpenShotAudio 0.4.0
juce_BigInteger.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
38class JUCE_API BigInteger
39{
40public:
41 //==============================================================================
43 BigInteger();
44
48 BigInteger (uint32 value);
49
54 BigInteger (int32 value);
55
60 BigInteger (int64 value);
61
63 BigInteger (const BigInteger&);
64
66 BigInteger (BigInteger&&) noexcept;
67
69 BigInteger& operator= (BigInteger&&) noexcept;
70
73
74 //==============================================================================
76 BigInteger& operator= (const BigInteger&);
77
79 void swapWith (BigInteger&) noexcept;
80
81 //==============================================================================
85 bool operator[] (int bit) const noexcept;
86
88 bool isZero() const noexcept;
89
91 bool isOne() const noexcept;
92
96 int toInteger() const noexcept;
97
101 int64 toInt64() const noexcept;
102
103 //==============================================================================
105 BigInteger& clear() noexcept;
106
108 BigInteger& clearBit (int bitNumber) noexcept;
109
111 BigInteger& setBit (int bitNumber);
112
114 BigInteger& setBit (int bitNumber, bool shouldBeSet);
115
122 BigInteger& setRange (int startBit, int numBits, bool shouldBeSet);
123
125 BigInteger& insertBit (int bitNumber, bool shouldBeSet);
126
132 BigInteger getBitRange (int startBit, int numBits) const;
133
141 uint32 getBitRangeAsInt (int startBit, int numBits) const noexcept;
142
148 BigInteger& setBitRangeAsInt (int startBit, int numBits, uint32 valueToSet);
149
155 BigInteger& shiftBits (int howManyBitsLeft, int startBit);
156
158 int countNumberOfSetBits() const noexcept;
159
165 int findNextSetBit (int startIndex) const noexcept;
166
172 int findNextClearBit (int startIndex) const noexcept;
173
177 int getHighestBit() const noexcept;
178
179 //==============================================================================
183 bool isNegative() const noexcept;
184
188 void setNegative (bool shouldBeNegative) noexcept;
189
193 void negate() noexcept;
194
195 //==============================================================================
196 // All the standard arithmetic ops...
197
198 BigInteger& operator+= (const BigInteger&);
199 BigInteger& operator-= (const BigInteger&);
200 BigInteger& operator*= (const BigInteger&);
201 BigInteger& operator/= (const BigInteger&);
202 BigInteger& operator|= (const BigInteger&);
203 BigInteger& operator&= (const BigInteger&);
204 BigInteger& operator^= (const BigInteger&);
205 BigInteger& operator%= (const BigInteger&);
206 BigInteger& operator<<= (int numBitsToShift);
207 BigInteger& operator>>= (int numBitsToShift);
208 BigInteger& operator++();
209 BigInteger& operator--();
210 BigInteger operator++ (int);
211 BigInteger operator-- (int);
212
213 BigInteger operator-() const;
214 BigInteger operator+ (const BigInteger&) const;
215 BigInteger operator- (const BigInteger&) const;
216 BigInteger operator* (const BigInteger&) const;
217 BigInteger operator/ (const BigInteger&) const;
218 BigInteger operator| (const BigInteger&) const;
219 BigInteger operator& (const BigInteger&) const;
220 BigInteger operator^ (const BigInteger&) const;
221 BigInteger operator% (const BigInteger&) const;
222 BigInteger operator<< (int numBitsToShift) const;
223 BigInteger operator>> (int numBitsToShift) const;
224
225 bool operator== (const BigInteger&) const noexcept;
226 bool operator!= (const BigInteger&) const noexcept;
227 bool operator< (const BigInteger&) const noexcept;
228 bool operator<= (const BigInteger&) const noexcept;
229 bool operator> (const BigInteger&) const noexcept;
230 bool operator>= (const BigInteger&) const noexcept;
231
232 //==============================================================================
240 int compare (const BigInteger& other) const noexcept;
241
249 int compareAbsolute (const BigInteger& other) const noexcept;
250
251 //==============================================================================
257 void divideBy (const BigInteger& divisor, BigInteger& remainder);
258
260 BigInteger findGreatestCommonDivisor (BigInteger other) const;
261
265 void exponentModulo (const BigInteger& exponent, const BigInteger& modulus);
266
270 void inverseModulo (const BigInteger& modulus);
271
277 void montgomeryMultiplication (const BigInteger& other, const BigInteger& modulus,
278 const BigInteger& modulusp, int k);
279
284 void extendedEuclidean (const BigInteger& a, const BigInteger& b,
285 BigInteger& xOut, BigInteger& yOut);
286
287 //==============================================================================
294 String toString (int base, int minimumNumCharacters = 1) const;
295
301 void parseString (StringRef text, int base);
302
303 //==============================================================================
311 MemoryBlock toMemoryBlock() const;
312
320 void loadFromMemoryBlock (const MemoryBlock& data);
321
322private:
323 //==============================================================================
324 enum { numPreallocatedInts = 4 };
325 HeapBlock<uint32> heapAllocation;
326 uint32 preallocated[numPreallocatedInts];
327 size_t allocatedSize;
328 int highestBit = -1;
329 bool negative = false;
330
331 uint32* getValues() const noexcept;
332 uint32* ensureSize (size_t);
333 void shiftLeft (int bits, int startBit);
334 void shiftRight (int bits, int startBit);
335
336 JUCE_LEAK_DETECTOR (BigInteger)
337};
338
340OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, const BigInteger& value);
341
342} // namespace juce