include/symbolism/N.Addition.h

00001 
00002 inline void N::add(const N& a, const N& b)
00003 {
00004     if(is_small(a))
00005     {
00006         if(is_small(b))
00007         {
00008             add_small_small(a.limb, b.limb);
00009         }
00010         else
00011         {
00012             add_large_small(b, a.limb);
00013         }
00014     }
00015     else
00016     {
00017         if(is_small(b))
00018         {
00019             add_large_small(a, b.limb);
00020         }
00021         else
00022         {
00023             add_large_large(a, b);
00024         }
00025     }
00026 }
00027 
00028 inline void N::add(const N& a)
00029 {
00030     if(this_small)
00031     {
00032         if(is_small(a))
00033         {
00034             add_small_small(a.limb);
00035         }
00036         else
00037         {
00038             add_small_large(a);
00039         }
00040     }
00041     else
00042     {
00043         if(is_small(a))
00044         {
00045             add_large_small(a.limb);
00046         }
00047         else
00048         {
00049             add_large_large(a);
00050         }
00051     }
00052 }
00053 
00054 inline void N::add_one()
00055 {
00056     if(this_small)
00057     {
00058         add_one_small();
00059     }
00060     else
00061     {
00062         add_one_large();
00063     }
00064 }
00065 
00066 inline N operator+(const N& a, const N& b)
00067 {
00068     N r;
00069     r.add(a, b);
00070     return r;
00071 }
00072 
00073 inline N& N::operator+=(const N& rhs)
00074 {
00075     add(rhs);
00076     return *this;
00077 }
00078 inline N& N::operator++()
00079 {
00080     add_one();
00081     return *this;
00082 }
00083 
00084 inline N N::operator++(int)
00085 {
00086     N copy(*this);
00087     add_one();
00088     return copy;
00089 }
00090 
00091 inline void N::add_carry_small(const limb_t carry)
00092 {
00093     if(fast_false(carry))
00094     {
00095         limb_t value = limb;
00096         size = 1;
00097         void* space = malloc((size + 1)  * sizeof(limb_t));
00098         data = reinterpret_cast<limb_t*>(space);
00099         data[0] = value;
00100         data[1] = carry;
00101     }
00102 }
00103 
00104 inline void N::add_carry_large(const limb_t carry)
00105 {
00106     if(fast_false(carry))
00107     {
00108         size++;
00109         void* space = realloc(data, (size + 1)  * sizeof(limb_t));
00110         data = reinterpret_cast<limb_t*>(space);
00111         data[size] = carry;
00112     }
00113 }
00114 
00115 inline void N::add_one_small()
00116 {
00117     limb_t old = limb;
00118     limb++;
00119     if(fast_false(limb < old))
00120     {
00121         add_carry_small();
00122     }
00123 }
00124 
00125 inline void N::add_small_small(const limb_t a, const limb_t b)
00126 {
00127     N::limb_t lr = a + b;
00128     if(fast_false(lr < a))
00129     {
00130         reserve(1);
00131         data[0] = lr;
00132         data[1] = 1;
00133     }
00134     else
00135     {
00136         reserve(0);
00137         limb = lr;
00138     }
00139 }
00140 
00141 inline void N::add_small_small(const limb_t a)
00142 {
00143     limb += a;
00144     if(fast_false(a > limb))
00145     {
00146         add_carry_small();
00147     }
00148 }

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