include/symbolism/N.Multiplication.h

00001 
00002 // Multiplication algorithm selectors
00003 inline void N::mul(const N& a, const N& b)
00004 {
00005     // If a or b is large limb will contain a nonzero pointer
00006     if(a.limb  == 0 || b.limb == 0)
00007     {
00008         set(0);
00009     }
00010     else if(is_small(a))
00011     {
00012         if(is_small(b))
00013         {
00014             mul_small_small(a.limb, b.limb);
00015         }
00016         else
00017         {
00018             if(a.limb > 1)
00019             {
00020                 mul_large_small(b, a.limb);
00021             }
00022             else
00023             {
00024                 set(b);
00025             }
00026         }
00027     }
00028     else
00029     {
00030         if(is_small(b))
00031         {
00032             if(b.limb > 1)
00033             {
00034                 mul_large_small(a, b.limb);
00035             }
00036             else
00037             {
00038                 set(a);
00039             }
00040         }
00041         else
00042         {
00043             mul_large_large(a, b);
00044         }
00045     }
00046 }
00047 
00048 inline void N::mul(const N& a)
00049 {
00050     // If this or a is large limb will contain a nonzero pointer
00051     if(limb  == 0 || a.limb == 0)
00052     {
00053         set(0);
00054     }
00055     if(this_small)
00056     {
00057         if(is_small(a))
00058         {
00059             mul_small_small(a.limb);
00060         }
00061         else
00062         {
00063             if(limb > 1)
00064             {
00065                 mul_small_large(a);
00066             }
00067             else
00068             {
00069                 set(a);
00070             }
00071         }
00072     }
00073     else
00074     {
00075         if(is_small(a))
00076         {
00077             if(a.limb > 1)
00078             {
00079                 mul_large_small(a.limb);
00080             }
00081         }
00082         else
00083         {
00084             mul_large_large(a);
00085         }
00086     }
00087 }
00088 
00089 // Multiplication operators
00090 inline N operator*(const N& a, const N& b)
00091 {
00092     N r;
00093     r.mul(a, b);
00094     return r;
00095 }
00096 
00097 inline N& N::operator*=(const N& rhs)
00098 {
00099     mul(rhs);
00100     return *this;
00101 }
00102 
00103 inline void N::mul_small_small(const limb_t a, const limb_t b)
00104 {
00105     limb_t lo, hi;
00106     asm("mulq %3" : "=a"(lo), "=d"(hi) : "a"(a), "rm"(b));
00107     if(hi == 0)
00108     {
00109         reserve(0);
00110         limb = lo;
00111     }
00112     else
00113     {
00114         reserve(1);
00115         data[0] = lo;
00116         data[1] = hi;
00117     }
00118 }
00119 
00120 inline void N::mul_small_small(const limb_t a)
00121 {
00122     limb_t lo, hi;
00123     asm("mulq %3" : "=a"(lo), "=d"(hi) : "a"(a), "rm"(limb));
00124     limb = lo;
00125     add_carry_small(hi);
00126 }

Copyright © 2007-2008 Remco Bloemen.

Generated on Tue Jan 22 17:35:31 2008 for symbolism by doxygen 1.5.4

Hosted by SourceForge.net Logo