+ /* 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 = static_cast< E >(u.chunk[0]) * static_cast< E >(v.chunk[0]);
+ num_type tnum = num_type(reinterpret_cast< 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
+ * FIXME: seria mejor hacer el acomode en la llamada a naif arriba?
+ */
+ 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;
+}
+
+
+/* Algoritmo de multiplicacion de Karatsuba-Ofman
+ * Ver los comentarios del algoritmo naif, es practicamente identico salvo en
+ * los calculos numericos que se especifican debajo.
+ */
+template < typename N, typename E >
+number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v)
+{
+ typedef number< N, E > num_type;
+
+ 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;
+ }
+
+ if (chunk_size == 1) {
+ E tmp;
+ tmp = static_cast< E >(u.chunk[0]) * static_cast< E >(v.chunk[0]);
+ num_type tnum = num_type(static_cast< N* >(&tmp), 2, sign);
+ return tnum;