aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-07 18:41:38 +0000
committerlloyd <[email protected]>2008-09-07 18:41:38 +0000
commit67f1970cf168c4d6b0c773555039a6308694ef9f (patch)
treeaf82f2d1831a652664b47d777aef6f196e3b784d /include
parent3615001dbdd743413d358a5d815baa8555cc4e79 (diff)
Inline BigInt::Rep::operator[], BigInt::sig_words, and BigInt::Rep::sig_words
Diffstat (limited to 'include')
-rw-r--r--include/bigint.h60
1 files changed, 46 insertions, 14 deletions
diff --git a/include/bigint.h b/include/bigint.h
index e5ab880df..f67ba5740 100644
--- a/include/bigint.h
+++ b/include/bigint.h
@@ -79,7 +79,7 @@ class BOTAN_DLL BigInt
BigInt abs() const;
u32bit size() const { return get_reg().size(); }
- u32bit sig_words() const;
+ u32bit sig_words() const { return rep.sig_words(); }
u32bit bytes() const;
u32bit bits() const;
@@ -119,28 +119,60 @@ class BOTAN_DLL BigInt
private:
class Rep
{
+ private:
+ static const u32bit INVALID_SIG_WORD = 0xFFFFFFFF;
+ mutable u32bit sig;
+ SecureVector<word> reg;
+
public:
+ Rep() { sig = INVALID_SIG_WORD; }
+
+ void swap(Rep& other)
+ {
+ std::swap(reg, other.reg);
+ std::swap(sig, other.sig);
+ }
+
SecureVector<word>& get_reg()
{ sig = INVALID_SIG_WORD; return reg; }
- word& operator[](u32bit);
- word operator[](u32bit) const;
-
- const SecureVector<word>& get_reg() const { return reg; }
+ word& operator[](u32bit n)
+ {
+ sig = INVALID_SIG_WORD;
- u32bit sig_words() const;
+ if(n > reg.size())
+ reg.grow_to(n+1);
+ return reg[n];
+ }
- void swap(Rep& other)
+ word operator[](u32bit n) const
{
- std::swap(reg, other.reg);
- std::swap(sig, other.sig);
+ if(n > reg.size())
+ return 0;
+ return reg[n];
}
- Rep() { sig = INVALID_SIG_WORD; }
- private:
- static const u32bit INVALID_SIG_WORD = 0xFFFFFFFF;
- mutable u32bit sig;
- SecureVector<word> reg;
+ const SecureVector<word>& get_reg() const { return reg; }
+
+ /*************************************************
+ * Count the significant words, if cached value is
+ * not valid
+ *************************************************/
+ u32bit sig_words() const
+ {
+ if(sig == INVALID_SIG_WORD)
+ {
+ const word* x = reg.begin();
+ u32bit top_set = reg.size();
+
+ while(top_set && (x[top_set-1] == 0))
+ top_set--;
+
+ sig = top_set;
+ }
+
+ return sig;
+ }
};
Rep rep;