Skip to content

Commit

Permalink
Add (mostly @nogc) attributes to bigint module.
Browse files Browse the repository at this point in the history
Also add comment to `internal.math.biguintcore.fromDecimalString`.
  • Loading branch information
denis-sh committed Feb 27, 2015
1 parent 9d4f5ef commit 7820bf3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
32 changes: 16 additions & 16 deletions std/bigint.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public:
}

///
BigInt opAssign(T:BigInt)(T x) pure
BigInt opAssign(T:BigInt)(T x) pure @nogc
{
data = x.data;
sign = x.sign;
Expand Down Expand Up @@ -369,49 +369,49 @@ public:
}

///
bool opEquals()(auto ref const BigInt y) const pure
bool opEquals()(auto ref const BigInt y) const pure @nogc
{
return sign == y.sign && y.data == data;
}

///
bool opEquals(T)(T y) const pure if (isIntegral!T)
bool opEquals(T)(T y) const pure nothrow @nogc if (isIntegral!T)
{
if (sign != (y<0))
return 0;
return data.opEquals(cast(ulong)absUnsign(y));
}

///
T opCast(T:bool)() pure const
T opCast(T:bool)() pure nothrow @nogc const
{
return !isZero();
}

///
T opCast(T)() pure const if (is(Unqual!T == BigInt)) {
T opCast(T)() pure nothrow @nogc const if (is(Unqual!T == BigInt)) {
return this;
}

// Hack to make BigInt's typeinfo.compare work properly.
// Note that this must appear before the other opCmp overloads, otherwise
// DMD won't find it.
int opCmp(ref const BigInt y) pure nothrow const
int opCmp(ref const BigInt y) pure nothrow @nogc const
{
// Simply redirect to the "real" opCmp implementation.
return this.opCmp!BigInt(y);
}

///
int opCmp(T)(T y) pure nothrow const if (isIntegral!T)
int opCmp(T)(T y) pure nothrow @nogc const if (isIntegral!T)
{
if (sign != (y<0) )
return sign ? -1 : 1;
int cmp = data.opCmp(cast(ulong)absUnsign(y));
return sign? -cmp: cmp;
}
///
int opCmp(T:BigInt)(const T y) pure nothrow const
int opCmp(T:BigInt)(const T y) pure nothrow @nogc const
{
if (sign!=y.sign)
return sign ? -1 : 1;
Expand All @@ -420,7 +420,7 @@ public:
}
/// Returns the value of this BigInt as a long,
/// or +- long.max if outside the representable range.
long toLong() pure nothrow const @nogc
long toLong() @safe pure nothrow const @nogc
{
return (sign ? -1 : 1) *
(data.ulongLength == 1 && (data.peekUlong(0) <= sign+cast(ulong)(long.max)) // 1+long.max = |long.min|
Expand All @@ -429,7 +429,7 @@ public:
}
/// Returns the value of this BigInt as an int,
/// or +- int.max if outside the representable range.
int toInt() pure nothrow const
int toInt() @safe pure nothrow @nogc const
{
return (sign ? -1 : 1) *
(data.uintLength == 1 && (data.peekUint(0) <= sign+cast(uint)(int.max)) // 1+int.max = |int.min|
Expand All @@ -438,13 +438,13 @@ public:
}
/// Number of significant uints which are used in storing this number.
/// The absolute value of this BigInt is always < 2^^(32*uintLength)
@property size_t uintLength() pure nothrow const
@property size_t uintLength() @safe pure nothrow @nogc const
{
return data.uintLength;
}
/// Number of significant ulongs which are used in storing this number.
/// The absolute value of this BigInt is always < 2^^(64*ulongLength)
@property size_t ulongLength() pure nothrow const
@property size_t ulongLength() @safe pure nothrow @nogc const
{
return data.ulongLength;
}
Expand Down Expand Up @@ -515,22 +515,22 @@ public:
}

// Implement toHash so that BigInt works properly as an AA key.
size_t toHash() const @trusted nothrow
size_t toHash() const @safe nothrow
{
return data.toHash() + sign;
}

private:
void negate() @safe pure nothrow
void negate() @safe pure nothrow @nogc
{
if (!data.isZero())
sign = !sign;
}
bool isZero() pure const nothrow @safe
bool isZero() pure const nothrow @nogc @safe
{
return data.isZero();
}
bool isNegative() pure const nothrow @safe
bool isNegative() pure const nothrow @nogc @safe
{
return sign;
}
Expand Down
29 changes: 15 additions & 14 deletions std/internal/math/biguintcore.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private:
assert( data.length >= 1 && (data.length == 1 || data[$-1] != 0 ));
}
immutable(BigDigit) [] data = ZERO;
this(immutable(BigDigit) [] x) pure nothrow @safe
this(immutable(BigDigit) [] x) pure nothrow @nogc @safe
{
data = x;
}
Expand Down Expand Up @@ -191,13 +191,13 @@ public:
}
}
}
void opAssign(Tdummy = void)(BigUint y) pure nothrow @safe
void opAssign(Tdummy = void)(BigUint y) pure nothrow @nogc @safe
{
this.data = y.data;
}

///
int opCmp(Tdummy = void)(const BigUint y) pure const @safe
int opCmp(Tdummy = void)(const BigUint y) pure nothrow @nogc const @safe
{
if (data.length != y.data.length)
return (data.length > y.data.length) ? 1 : -1;
Expand All @@ -208,7 +208,7 @@ public:
}

///
int opCmp(Tulong)(Tulong y) pure const @safe if(is (Tulong == ulong))
int opCmp(Tulong)(Tulong y) pure nothrow @nogc const @safe if(is (Tulong == ulong))
{
if (data.length > maxBigDigits!Tulong)
return 1;
Expand All @@ -234,12 +234,12 @@ public:
return 0;
}

bool opEquals(Tdummy = void)(ref const BigUint y) pure const @safe
bool opEquals(Tdummy = void)(ref const BigUint y) pure nothrow @nogc const @safe
{
return y.data[] == data[];
}

bool opEquals(Tdummy = void)(ulong y) pure const @safe
bool opEquals(Tdummy = void)(ulong y) pure nothrow @nogc const @safe
{
if (data.length > 2)
return false;
Expand All @@ -257,13 +257,13 @@ public:
return data.length == 1 && data[0] == 0;
}

size_t numBytes() pure const @safe @nogc
size_t numBytes() pure nothrow const @safe @nogc
{
return data.length * BigDigit.sizeof;
}

// the extra bytes are added to the start of the string
char [] toDecimalString(int frontExtraBytes) const pure
char [] toDecimalString(int frontExtraBytes) const pure nothrow
{
auto predictlength = 20+20*(data.length/2); // just over 19
char [] buff = new char[frontExtraBytes + predictlength];
Expand All @@ -279,7 +279,7 @@ public:
* Separator characters do not contribute to the minPadding.
*/
char [] toHexString(int frontExtraBytes, char separator = 0,
int minPadding=0, char padChar = '0') const pure @safe
int minPadding=0, char padChar = '0') const pure nothrow @safe
{
// Calculate number of extra padding bytes
size_t extraPad = (minPadding > data.length * 2 * BigDigit.sizeof)
Expand Down Expand Up @@ -392,6 +392,7 @@ public:
}

// return true if OK; false if erroneous characters found
// FIXME: actually throws `ConvException` on error.
bool fromDecimalString(const(char)[] s) pure @trusted
{
//Strip leading zeros
Expand Down Expand Up @@ -912,7 +913,7 @@ public:

} // end BigUint

@safe pure unittest
@safe pure nothrow unittest
{
// ulong comparison test
BigUint a = [1];
Expand Down Expand Up @@ -1507,7 +1508,7 @@ private:
// buff.length must be data.length*8 if separator is zero,
// or data.length*9 if separator is non-zero. It will be completely filled.
char [] biguintToHex(char [] buff, const BigDigit [] data, char separator=0)
pure @safe
pure nothrow @safe
{
int x=0;
for (ptrdiff_t i=data.length - 1; i>=0; --i)
Expand Down Expand Up @@ -1535,7 +1536,7 @@ char [] biguintToHex(char [] buff, const BigDigit [] data, char separator=0)
* Returns:
* the lowest index of buff which was used.
*/
size_t biguintToDecimal(char [] buff, BigDigit [] data) pure
size_t biguintToDecimal(char [] buff, BigDigit [] data) pure nothrow
{
ptrdiff_t sofar = buff.length;
// Might be better to divide by (10^38/2^32) since that gives 38 digits for
Expand Down Expand Up @@ -2162,7 +2163,7 @@ private:
// Returns the highest value of i for which left[i]!=right[i],
// or 0 if left[] == right[]
size_t highestDifferentDigit(const BigDigit [] left, const BigDigit [] right)
pure nothrow @safe
pure nothrow @nogc @safe
{
assert(left.length == right.length);
for (ptrdiff_t i = left.length - 1; i>0; --i)
Expand All @@ -2174,7 +2175,7 @@ pure nothrow @safe
}

// Returns the lowest value of i for which x[i]!=0.
int firstNonZeroDigit(const BigDigit [] x) pure nothrow @safe
int firstNonZeroDigit(const BigDigit [] x) pure nothrow @nogc @safe
{
int k = 0;
while (x[k]==0)
Expand Down

0 comments on commit 7820bf3

Please sign in to comment.