[poincare] Clean Integer constructor

Change-Id: I1ced5587246507d3cd813b0c23af64f3459c6d0c
This commit is contained in:
Émilie Feral
2017-11-30 10:48:38 +01:00
parent 81016fa168
commit da2829b969
2 changed files with 13 additions and 9 deletions

View File

@@ -34,17 +34,22 @@ static inline int8_t sign(bool negative) {
// Constructors
static_assert(sizeof(Integer::double_native_int_t) == 2*sizeof(Integer::native_int_t), "double_native_int_t type has not the right size compared to native_int_t");
static_assert(sizeof(Integer::native_int_t) == sizeof(Integer::native_uint_t), "native_int_t type has not the right size compared to native_uint_t");
Integer::Integer(double_native_int_t i) {
double_native_uint_t j = i < 0 ? -i : i;
if (j <= 0xFFFFFFFF) {
m_digit = j;
m_numberOfDigits = 1;
native_uint_t * digits = (native_uint_t *)&j;
native_uint_t leastSignificantDigit = *digits;
native_uint_t mostSignificantDigit = *(digits+1);
m_numberOfDigits = (mostSignificantDigit == 0) ? 1 : 2;
if (m_numberOfDigits == 1) {
m_digit = leastSignificantDigit;
} else {
native_uint_t * digits = new native_uint_t [2];
digits[0] = j & 0xFFFFFFFF;
digits[1] = (j >> 32) & 0xFFFFFFFF;
digits[0] = leastSignificantDigit;
digits[1] = mostSignificantDigit;
m_digits = digits;
m_numberOfDigits = 2;
}
m_negative = i < 0;
}
@@ -597,8 +602,7 @@ int Integer::writeTextInBuffer(char * buffer, int bufferSize) const {
buffer[size] = 0;
// Flip the string
int startChar = isNegative() ? 1 : 0;
for (int i=startChar, j=size-1 ; i < j ; i++, j--) {
for (int i=m_negative, j=size-1 ; i < j ; i++, j--) {
char c = buffer[i];
buffer[i] = buffer[j];
buffer[j] = c;

View File

@@ -9,7 +9,7 @@ QUIZ_CASE(poincare_integer) {
assert(Integer("123").isEqualTo(Integer(123)));
assert(!Integer("-123").isEqualTo(Integer(123)));
assert(Integer("-123").isEqualTo(Integer(-123)));
//assert(Integer("0123") == Integer(123));
assert(Integer((int64_t)1234567891011121314).isEqualTo(Integer((int64_t)1234567891011121314)));
//FIXME: assert(Integer("0x2BABE") == Integer(178878));
//FIXME: assert(Integer("0b1011") == Integer(11));
}