Audio plugin host https://kx.studio/carla
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.

246 lines
6.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "BigInteger.h"
  21. namespace water
  22. {
  23. inline uint32 bitToMask (const int bit) noexcept { return (uint32) 1 << (bit & 31); }
  24. inline size_t bitToIndex (const int bit) noexcept { return (size_t) (bit >> 5); }
  25. inline size_t sizeNeededToHold (int highestBit) noexcept { return (size_t) (highestBit >> 5) + 1; }
  26. int findHighestSetBit (uint32 n) noexcept
  27. {
  28. jassert (n != 0); // (the built-in functions may not work for n = 0)
  29. #if defined(__GNUC__) || defined(__clang__)
  30. return 31 - __builtin_clz (n);
  31. #elif _MSVC_VER
  32. unsigned long highest;
  33. _BitScanReverse (&highest, n);
  34. return (int) highest;
  35. #else
  36. n |= (n >> 1);
  37. n |= (n >> 2);
  38. n |= (n >> 4);
  39. n |= (n >> 8);
  40. n |= (n >> 16);
  41. return countNumberOfBits (n >> 1);
  42. #endif
  43. }
  44. //==============================================================================
  45. BigInteger::BigInteger() noexcept
  46. : allocatedSize (numPreallocatedInts),
  47. highestBit (-1)
  48. {
  49. for (int i = 0; i < numPreallocatedInts; ++i)
  50. preallocated[i] = 0;
  51. }
  52. BigInteger::BigInteger (const int32 value) noexcept
  53. : allocatedSize (numPreallocatedInts),
  54. highestBit (31)
  55. {
  56. preallocated[0] = (uint32) std::abs (value);
  57. for (int i = 1; i < numPreallocatedInts; ++i)
  58. preallocated[i] = 0;
  59. highestBit = getHighestBit();
  60. }
  61. BigInteger::BigInteger (const uint32 value) noexcept
  62. : allocatedSize (numPreallocatedInts),
  63. highestBit (31)
  64. {
  65. preallocated[0] = value;
  66. for (int i = 1; i < numPreallocatedInts; ++i)
  67. preallocated[i] = 0;
  68. highestBit = getHighestBit();
  69. }
  70. BigInteger::BigInteger (int64 value) noexcept
  71. : allocatedSize (numPreallocatedInts),
  72. highestBit (63)
  73. {
  74. if (value < 0)
  75. value = -value;
  76. preallocated[0] = (uint32) value;
  77. preallocated[1] = (uint32) (value >> 32);
  78. for (int i = 2; i < numPreallocatedInts; ++i)
  79. preallocated[i] = 0;
  80. highestBit = getHighestBit();
  81. }
  82. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  83. BigInteger::BigInteger (BigInteger&& other) noexcept
  84. : heapAllocation (static_cast<HeapBlock<uint32>&&> (other.heapAllocation)),
  85. allocatedSize (other.allocatedSize),
  86. highestBit (other.highestBit)
  87. {
  88. std::memcpy (preallocated, other.preallocated, sizeof (preallocated));
  89. }
  90. BigInteger& BigInteger::operator= (BigInteger&& other) noexcept
  91. {
  92. heapAllocation = static_cast<HeapBlock<uint32>&&> (other.heapAllocation);
  93. std::memcpy (preallocated, other.preallocated, sizeof (preallocated));
  94. allocatedSize = other.allocatedSize;
  95. highestBit = other.highestBit;
  96. return *this;
  97. }
  98. #endif
  99. BigInteger::~BigInteger() noexcept
  100. {
  101. }
  102. uint32* BigInteger::getValues() const noexcept
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(heapAllocation != nullptr || allocatedSize <= numPreallocatedInts, nullptr);
  105. return heapAllocation != nullptr ? heapAllocation
  106. : (uint32*) preallocated;
  107. }
  108. uint32* BigInteger::ensureSize (const size_t numVals) noexcept
  109. {
  110. if (numVals <= allocatedSize)
  111. return getValues();
  112. size_t oldSize = allocatedSize;
  113. allocatedSize = ((numVals + 2) * 3) / 2;
  114. if (heapAllocation == nullptr)
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(heapAllocation.calloc (allocatedSize), nullptr);
  117. std::memcpy (heapAllocation, preallocated, sizeof (uint32) * numPreallocatedInts);
  118. }
  119. else
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(heapAllocation.realloc (allocatedSize), nullptr);
  122. for (uint32* values = heapAllocation; oldSize < allocatedSize; ++oldSize)
  123. values[oldSize] = 0;
  124. }
  125. return heapAllocation;
  126. }
  127. //==============================================================================
  128. bool BigInteger::operator[] (const int bit) const noexcept
  129. {
  130. if (bit < 0 || bit > highestBit)
  131. return false;
  132. if (const uint32* const values = getValues())
  133. return (values [bitToIndex (bit)] & bitToMask (bit)) != 0;
  134. return false;
  135. }
  136. //==============================================================================
  137. void BigInteger::clear() noexcept
  138. {
  139. heapAllocation.free();
  140. allocatedSize = numPreallocatedInts;
  141. highestBit = -1;
  142. for (int i = 0; i < numPreallocatedInts; ++i)
  143. preallocated[i] = 0;
  144. }
  145. bool BigInteger::setBit (const int bit) noexcept
  146. {
  147. if (bit < 0)
  148. return false;
  149. CARLA_SAFE_ASSERT_RETURN(bit >= 0, false);
  150. if (bit > highestBit)
  151. {
  152. if (ensureSize (sizeNeededToHold (bit)) == nullptr)
  153. return false;
  154. highestBit = bit;
  155. }
  156. if (uint32* const values = getValues())
  157. return values [bitToIndex (bit)] |= bitToMask (bit);
  158. return false;
  159. }
  160. bool BigInteger::setBit (const int bit, const bool shouldBeSet) noexcept
  161. {
  162. if (shouldBeSet)
  163. {
  164. return setBit (bit);
  165. }
  166. else
  167. {
  168. clearBit (bit);
  169. return true;
  170. }
  171. }
  172. bool BigInteger::clearBit (const int bit) noexcept
  173. {
  174. if (bit < 0 || bit > highestBit)
  175. return false;
  176. uint32* const values = getValues();
  177. CARLA_SAFE_ASSERT_RETURN(values != nullptr, false);
  178. values [bitToIndex (bit)] &= ~bitToMask (bit);
  179. if (bit == highestBit)
  180. highestBit = getHighestBit();
  181. return true;
  182. }
  183. int BigInteger::getHighestBit() const noexcept
  184. {
  185. const uint32* values = getValues();
  186. CARLA_SAFE_ASSERT_RETURN(values != nullptr, -1);
  187. for (int i = (int) bitToIndex (highestBit); i >= 0; --i)
  188. if (uint32 n = values[i])
  189. return findHighestSetBit (n) + (i << 5);
  190. return -1;
  191. }
  192. }