From 8a4af478cf1db333ee23a1d781eb0f396f471c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 10 Nov 2017 18:19:15 +0100 Subject: [PATCH] [poincare] Add a constructor in Integer Change-Id: Icf84821d1b898dbe353a2ee3cda7cfbb5c0b5468 --- poincare/include/poincare/expression.h | 2 +- poincare/include/poincare/integer.h | 2 ++ poincare/src/integer.cpp | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 70dbe718f..cbce99903 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -217,7 +217,7 @@ protected: typedef float SinglePrecision; typedef double DoublePrecision; template static T epsilon(); - constexpr static float k_maxNumberOfSteps = 10000.0f; + constexpr static int k_maxNumberOfSteps = 10000; /* Simplification */ /* SimplificationOrder returns: diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 564eca3d0..47e4bce12 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -16,6 +16,7 @@ class Integer { public: typedef uint16_t half_native_uint_t; typedef int32_t native_int_t; + typedef int64_t double_native_int_t; typedef uint32_t native_uint_t; typedef uint64_t double_native_uint_t; @@ -26,6 +27,7 @@ public: m_negative(i<0) { } + Integer(double_native_int_t i); Integer(const char * digits, bool negative = false); // Digits are NOT NULL-terminated static Integer exponent(int fractionalPartLength, const char * exponent, int exponentLength, bool exponentNegative); static Integer numerator(const char * integralPart, int integralPartLength, const char * fractionalPart, int fractionalPartLength, bool negative, Integer * exponent); diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index c7199d412..65db7a66f 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -34,6 +34,21 @@ static inline int8_t sign(bool negative) { // Constructors +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; + } else { + native_uint_t * digits = new native_uint_t [2]; + digits[0] = j & 0xFFFFFFFF; + digits[1] = (j >> 32) & 0xFFFFFFFF; + m_digits = digits; + m_numberOfDigits = 2; + } + m_negative = i < 0; +} + /* Caution: string is NOT guaranteed to be NULL-terminated! */ Integer::Integer(const char * digits, bool negative) : Integer(0) @@ -408,9 +423,9 @@ IntegerDivision Integer::udiv(const Integer & numerator, const Integer & denomin } Integer A = numerator; Integer B = denominator; - native_uint_t base = (double_native_uint_t)1 << 16; + native_int_t base = (double_native_uint_t)1 << 16; // TODO: optimize by just swifting digit and finding 2^kB that makes B normalized - native_uint_t d = base/(native_uint_t)(B.halfDigit(B.numberOfHalfDigits()-1)+1); + native_int_t d = base/(native_int_t)(B.halfDigit(B.numberOfHalfDigits()-1)+1); A = Multiplication(Integer(d), A); B = Multiplication(Integer(d), B);