diff --git a/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs b/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs index 9e02c50e..d5022a88 100644 --- a/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs +++ b/mpir.net/mpir.net-tests/HugeFloatTests/ConstructionAndDisposal.cs @@ -287,6 +287,23 @@ namespace MPIR.Tests.HugeFloatTests } } + [TestMethod] + public void FloatAllocatedPrecision() + { + using (var a = new HugeFloat(1)) + using (var b = HugeFloat.Allocate(256)) + { + var bAllocated = b.AllocatedPrecision; + var aAllocated = a.AllocatedPrecision; + Assert.IsTrue(bAllocated > aAllocated); + + a.Precision = 64; + b.Precision = 64; + Assert.AreEqual(bAllocated, b.AllocatedPrecision); + Assert.AreEqual(aAllocated, a.AllocatedPrecision); + } + } + [TestMethod] [ExpectedException(typeof(ArgumentException))] public void FloatSettingPrecisionOverAllocated() diff --git a/mpir.net/mpir.net-tests/HugeIntTests/IO.cs b/mpir.net/mpir.net-tests/HugeIntTests/IO.cs index 2ab48a1c..1cd78cfa 100644 --- a/mpir.net/mpir.net-tests/HugeIntTests/IO.cs +++ b/mpir.net/mpir.net-tests/HugeIntTests/IO.cs @@ -285,6 +285,23 @@ namespace MPIR.Tests.HugeIntTests } } + [TestMethod] + public void IntAllocatedSize() + { + using (var a = new HugeInt("-0x10123456789ABCDEF0123456789ABCDEF0123456789ABCDEF")) + { + var allocated = a.AllocatedSize; + Assert.IsTrue(allocated >= (int)a.Size()); + a.Value = -a; + Assert.AreEqual(allocated, a.AllocatedSize); + Assert.AreEqual(4UL, a.Size()); + + a.Value >>= 64; + Assert.AreEqual(3UL, a.Size()); + Assert.AreEqual(allocated, a.AllocatedSize); + } + } + [TestMethod] public void IntGetLimb() { diff --git a/mpir.net/mpir.net-tests/Utilities/Accessors.cs b/mpir.net/mpir.net-tests/Utilities/Accessors.cs index 48e6083b..0451b712 100644 --- a/mpir.net/mpir.net-tests/Utilities/Accessors.cs +++ b/mpir.net/mpir.net-tests/Utilities/Accessors.cs @@ -61,10 +61,7 @@ namespace MPIR.Tests if (_value(x) == IntPtr.Zero) return 0; - unsafe - { - return ((int*)_value(x).ToPointer())[0]; - } + return x.AllocatedSize; } internal static int NumberOfLimbsUsed(this HugeInt x) diff --git a/mpir.net/mpir.net/HugeFloat.h b/mpir.net/mpir.net/HugeFloat.h index 36197edc..a2e9df05 100644 --- a/mpir.net/mpir.net/HugeFloat.h +++ b/mpir.net/mpir.net/HugeFloat.h @@ -1180,6 +1180,18 @@ namespace MPIR } } + /// + /// Gets the precision in bits that is currently allocated for internal storage of the mantissa. + /// The precision actually in effect, used in calculations, is initially the same but may be reduced by setting the Precision property. + /// However Precision cannot exceed AllocatedPrecision. + /// To change AllocatedPrecision, call Reallocate(). + /// The value actually allocated may be slightly more than the number of bits requested by Allocate() or Reallocate(). + /// + property mp_bitcnt_t AllocatedPrecision + { + mp_bitcnt_t get() { return _allocatedPrecision; } + } + /// /// Set the precision of rop to be at least prec bits, reallocating its limbs data. /// The value in rop will be truncated to the new precision. diff --git a/mpir.net/mpir.net/HugeInt.h b/mpir.net/mpir.net/HugeInt.h index 8a11f690..ae33b508 100644 --- a/mpir.net/mpir.net/HugeInt.h +++ b/mpir.net/mpir.net/HugeInt.h @@ -1510,6 +1510,20 @@ namespace MPIR #pragma endregion + #pragma region Properties + + /// + /// Gets the number of limbs currently allocated. This value will never be less than Size(). + /// When a new value is assigned to the object, more space is automatically allocated if necessary. + /// Reallocate() can also be used manually. + /// + property int AllocatedSize + { + int get() { return _value->_mp_alloc; } + } + + #pragma endregion + #pragma region conversions ///