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.

119 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water 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. #ifndef WATER_BIGINTEGER_H_INCLUDED
  21. #define WATER_BIGINTEGER_H_INCLUDED
  22. #include "../memory/HeapBlock.h"
  23. #include "CarlaJuceUtils.hpp"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. An arbitrarily large integer class.
  28. A BigInteger can be used in a similar way to a normal integer, but has no size
  29. limit (except for memory and performance constraints).
  30. Negative values are possible, but the value isn't stored as 2s-complement, so
  31. be careful if you use negative values and look at the values of individual bits.
  32. */
  33. class BigInteger
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty BigInteger */
  38. BigInteger() noexcept;
  39. /** Creates a BigInteger containing an integer value in its low bits.
  40. The low 32 bits of the number are initialised with this value.
  41. */
  42. BigInteger (uint32 value) noexcept;
  43. /** Creates a BigInteger containing an integer value in its low bits.
  44. The low 32 bits of the number are initialised with the absolute value
  45. passed in, and its sign is set to reflect the sign of the number.
  46. */
  47. BigInteger (int32 value) noexcept;
  48. /** Creates a BigInteger containing an integer value in its low bits.
  49. The low 64 bits of the number are initialised with the absolute value
  50. passed in, and its sign is set to reflect the sign of the number.
  51. */
  52. BigInteger (int64 value) noexcept;
  53. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  54. BigInteger (BigInteger&&) noexcept;
  55. BigInteger& operator= (BigInteger&&) noexcept;
  56. #endif
  57. /** Destructor. */
  58. ~BigInteger() noexcept;
  59. //==============================================================================
  60. /** Returns the value of a specified bit in the number.
  61. If the index is out-of-range, the result will be false.
  62. */
  63. bool operator[] (int bit) const noexcept;
  64. //==============================================================================
  65. /** Resets the value to 0. */
  66. void clear() noexcept;
  67. /** Clears a particular bit in the number. */
  68. bool clearBit (int bitNumber) noexcept;
  69. /** Sets a specified bit to 1. */
  70. bool setBit (int bitNumber) noexcept;
  71. /** Sets or clears a specified bit. */
  72. bool setBit (int bitNumber, bool shouldBeSet) noexcept;
  73. //==============================================================================
  74. /** Returns the index of the highest set bit in the number.
  75. If the value is zero, this will return -1.
  76. */
  77. int getHighestBit() const noexcept;
  78. private:
  79. //==============================================================================
  80. enum { numPreallocatedInts = 4 };
  81. HeapBlock<uint32> heapAllocation;
  82. uint32 preallocated[numPreallocatedInts];
  83. size_t allocatedSize;
  84. int highestBit;
  85. uint32* getValues() const noexcept;
  86. uint32* ensureSize (size_t) noexcept;
  87. CARLA_LEAK_DETECTOR (BigInteger)
  88. };
  89. }
  90. #endif // WATER_BIGINTEGER_H_INCLUDED