[Poincare] Start a product algorithm on Integer

This commit is contained in:
Romain Goyet
2015-09-14 22:28:36 +02:00
parent d482fd3159
commit 9c2c160536
2 changed files with 27 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
#include <stdint.h>
typedef uint32_t native_uint_t;
typedef uint64_t double_native_uint_t;
class Integer : public Expression {
public:
@@ -16,6 +17,7 @@ class Integer : public Expression {
virtual bool identicalTo(Expression * e);
//Integer add(Integer * i);
const Integer operator+(const Integer &other) const;
const Integer operator*(const Integer &other) const;
bool operator==(const Integer &other) const;
protected:
virtual void layout();

View File

@@ -69,6 +69,31 @@ const Integer Integer::operator+(const Integer &other) const {
return Integer(bits, sumSize);
}
const Integer Integer::operator*(const Integer &other) const {
uint16_t productSize = other.m_numberOfBits + m_numberOfBits;
uint16_t intArraySize = arraySize(productSize);
native_uint_t * bits = (native_uint_t *)malloc(intArraySize*sizeof(native_uint_t));
memset(bits, 0, intArraySize*sizeof(native_uint_t));
uint16_t myArraySize = arraySize(m_numberOfBits);
uint16_t otherArraySize = arraySize(other.m_numberOfBits);
native_uint_t carry = 0;
for (uint16_t i=0; i<myArraySize; i++) {
native_uint_t a = m_bits[i];
carry = 0;
for (uint16_t j=0; j<otherArraySize; j++) {
native_uint_t b = other.m_bits[i];
double_native_uint_t p = a*b + carry; // TODO: Prove it cannot overflow
m_bits[i+j] += (native_uint_t)p; // Only the last "digit"
carry = p>>32; //FIXME: 32 is hardcoded here!
}
m_bits[i+otherArraySize] = carry;
}
return Integer(bits, productSize);
}
/*
char * Integer::bits() {
if (m_numberOfBits > INTEGER_IMMEDIATE_LIMIT) {