diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 622027252..ec3391ca4 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -24,7 +24,7 @@ class Integer : public Expression { /* WARNING: This constructor takes ownership of the bits array and will free it! */ Integer(native_uint_t * bits, uint16_t length); uint16_t m_numberOfBits; - native_uint_t * m_bits; + native_uint_t * m_bits; // LITTLE-ENDIAN /* // TODO: Small-int optimization union { diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index f473b7fea..373035cbe 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -2,8 +2,11 @@ #include #include #include +#include +#include #define MAX(a,b) ((a)>(b)?a:b) +#define NATIVE_UINT_BIT_COUNT (8*sizeof(native_uint_t)) #define INTEGER_IMMEDIATE_LIMIT 32 @@ -11,7 +14,18 @@ uint16_t Integer::arraySize(uint16_t bitSize) { return (bitSize-1)/(8*sizeof(native_uint_t))+1; } +uint8_t log2(native_uint_t v) { + assert(NATIVE_UINT_BIT_COUNT < 256); // Otherwise uint8_t isn't OK + for (uint8_t i=0; isum)||(b>sum)); } if (carry) { bits[intArraySize] = 0x1; } else { - sumSize -= 1; - /* At this point we may realloc m_bits to a smaller size. + sumSize = (intArraySize-1)*NATIVE_UINT_BIT_COUNT + log2(bits[intArraySize-1]); + /* At this point we may realloc m_bits to a smaller size in some cases. * It might not be worth the trouble though : it won't happen very often * and we're wasting a single native_uint_t. */ } @@ -65,8 +80,8 @@ const Integer Integer::operator+(const Integer &other) const { */ Integer::Integer(native_uint_t * bits, uint16_t size) : - m_bits(bits), - m_numberOfBits(size) { + m_numberOfBits(size), + m_bits(bits) { } Integer::Integer(char * string) {