#include <deque>
#include <utility>
#include <algorithm>
-#include <iterator>
+#include <iomanip>
#include <stdint.h>
+enum sign_type { positive, negative };
+
+
/* sizeof(E) tiene que ser 2*sizeof(N); y son los tipos nativos con los cuales
* se haran las operaciones mas basicas. */
+template < typename N, typename E >
+struct number;
+
+template < typename N, typename E >
+std::ostream& operator<< (std::ostream& os, const number< N, E >& n);
+
template < typename N = uint32_t, typename E = uint64_t >
struct number
{
// Tipos
typedef N native_type;
typedef E extended_type;
- enum sign_type { positive, negative };
typedef typename std::deque< native_type > chunk_type;
typedef typename chunk_type::size_type size_type;
typedef typename chunk_type::iterator iterator;
const_reverse_iterator rbegin() const { return chunk.rbegin(); }
const_reverse_iterator rend() const { return chunk.rend(); }
- private:
+ // Friends
+ template < typename NN, typename EE >
+ friend std::ostream& operator<< (std::ostream& os, const number< NN, EE>& n);
+
// Atributos
chunk_type chunk;
sign_type sign;
std::ostream& operator<< (std::ostream& os, const number< N, E >& n)
{
// FIXME sacar una salida bonita en ASCII =)
- std::copy(n.begin(), n.end(), std::ostream_iterator< N >(os, " "));
+ for (typename number< N, E >::const_iterator i = n.chunk.begin();
+ i != n.chunk.end(); ++i)
+ os << std::setfill('0') << std::setw(sizeof(N) * 2) << std::hex
+ << *i << " ";
return os;
}
std::pair< num_type, num_type > par;
// la primera mitad va al pedazo inferior
- for (i = 0; i < halves_size; i++)
+ par.first.chunk[0] = chunk[0];
+ for (i = 1; i < halves_size; i++)
{
par.first.chunk.push_back(chunk[i]);
}
// la segunda mitad (si full_size es impar es 1 más que la primera
// mitad) va al pedazo superior
- for ( ; i < full_size; i++)
+ par.second.chunk[0] = chunk[i];
+ for (i++ ; i < full_size; i++)
{
par.second.chunk.push_back(chunk[i]);
}
// es el algoritmo de división y conquista, que se llama recursivamente
template < typename N, typename E >
-number < N, E > divide_n_conquer(number< N, E > u, number< N, E > v)
+number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v)
{
typedef number< N, E > num_type;
// m = u1*v1
// d = u2*v2
// h = (u1+v1)*(u2+v2) = u1*u2+u1*v2+u2*v1+u2*v2
- num_type m = divide_n_conquer(u12.first, v12.first);
- num_type d = divide_n_conquer(u12.second, v12.second);
- num_type h = divide_n_conquer(u12.first + v12.first,
+ num_type m = karastuba(u12.first, v12.first);
+ num_type d = karastuba(u12.second, v12.second);
+ num_type h = karastuba(u12.first + v12.first,
u12.second + v12.second);
// H-D-M = u1*u2+u1*v2+u2*v1+u2*v2 - u2*v2 - u1*v1 = u1*v2+u2*v1
}
+
+/* Algoritmo "naif" (por no decir "cabeza" o "bruto") de multiplicacion. */
+template < typename N, typename E >
+number < N, E > naif(const number< N, E > &u, const number< N, E > &v)
+{
+ typedef number< N, E > num_type;
+
+ // tomo el chunk size de u (el de v DEBE ser el mismo)
+ typename num_type::size_type chunk_size = u.chunk.size();
+
+ sign_type sign;
+
+ if ( (u.sign == positive && v.sign == positive) ||
+ (u.sign == negative && v.sign == negative) ) {
+ sign = positive;
+ } else {
+ sign = negative;
+ }
+
+ //printf("naif %d %d\n", u.chunk.size(), v.chunk.size() );
+
+ if (chunk_size == 1)
+ {
+ /* Si llegamos a multiplicar dos de tamaño 1, lo que hacemos
+ * es usar la multiplicacion nativa del tipo N, guardando el
+ * resultado en el tipo E (que sabemos es del doble de tamaño
+ * de N, ni mas ni menos).
+ * Luego, armamos un objeto number usando al resultado como
+ * buffer. Si, es feo.
+ */
+ E tmp;
+ tmp = (E) u.chunk[0] * (E) v.chunk[0];
+ num_type tnum = num_type((N *) &tmp, 2, sign);
+ //std::cout << "T:" << tnum << " " << tmp << "\n";
+ //printf("1: %lu %lu %llu\n", u.chunk[0], v.chunk[0], tmp);
+ return tnum;
+ }
+
+ std::pair< num_type, num_type > u12 = u.split();
+ std::pair< num_type, num_type > v12 = v.split();
+
+ //std::cout << "u:" << u12.first << " - " << u12.second << "\n";
+ //std::cout << "v:" << v12.first << " - " << v12.second << "\n";
+
+ /* m11 = u1*v1
+ * m12 = u1*v2
+ * m21 = u2*v1
+ * m22 = u2*v2
+ */
+ num_type m11 = naif(u12.first, v12.first);
+ num_type m12 = naif(u12.first, v12.second);
+ num_type m21 = naif(u12.second, v12.first);
+ num_type m22 = naif(u12.second, v12.second);
+
+ /*
+ printf("csize: %d\n", chunk_size);
+ std::cout << "11 " << m11 << "\n";
+ std::cout << "12 " << m12 << "\n";
+ std::cout << "21 " << m21 << "\n";
+ std::cout << "22 " << m22 << "\n";
+ */
+
+ /* u*v = (u1*v1) * 2^n + (u1*v2 + u2*v1) * 2^(n/2) + u2*v2
+ * PERO! Como los numeros estan "al reves" nos queda:
+ * = m22 * 2^n + (m12 + m21) * 2^(n/2) + m11
+ */
+ num_type res;
+ res = m22 << chunk_size;
+ res = res + ((m12 + m21) << (chunk_size / 2));
+ res = res + m11;
+ res.sign = sign;
+ /*
+ std::cout << "r: " << res << "\n";
+ std::cout << "\n";
+ */
+ return res;
+}
+
+
+