From 67f1970cf168c4d6b0c773555039a6308694ef9f Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 7 Sep 2008 18:41:38 +0000 Subject: Inline BigInt::Rep::operator[], BigInt::sig_words, and BigInt::Rep::sig_words --- include/bigint.h | 60 +++++++++++++++++++++++++++++++++++++++++++------------- src/bigint.cpp | 58 ------------------------------------------------------ 2 files changed, 46 insertions(+), 72 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 reg; + public: + Rep() { sig = INVALID_SIG_WORD; } + + void swap(Rep& other) + { + std::swap(reg, other.reg); + std::swap(sig, other.sig); + } + SecureVector& get_reg() { sig = INVALID_SIG_WORD; return reg; } - word& operator[](u32bit); - word operator[](u32bit) const; - - const SecureVector& 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 reg; + const SecureVector& 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; diff --git a/src/bigint.cpp b/src/bigint.cpp index 7fb5896f3..b324fd851 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -234,64 +234,6 @@ void BigInt::mask_bits(u32bit n) rep[top_word] &= mask; } -/************************************************* -* Count the significant words, if cached value is -* not valid -*************************************************/ -u32bit BigInt::Rep::sig_words() const - { - if(sig == INVALID_SIG_WORD) - { - const word* x = reg.begin(); - u32bit top_set = reg.size(); - - while(top_set >= 4) - { - if(x[top_set-1]) - break; - if(x[top_set-2]) - break; - if(x[top_set-3]) - break; - if(x[top_set-4]) - break; - - top_set -= 4; - } - while(top_set && (x[top_set-1] == 0)) - top_set--; - - sig = top_set; - } - - return sig; - } - -word& BigInt::Rep::operator[](u32bit n) - { - sig = INVALID_SIG_WORD; - - if(n > reg.size()) - reg.grow_to(n+1); - return reg[n]; - } - -word BigInt::Rep::operator[](u32bit n) const - { - if(n > reg.size()) - return 0; - return reg[n]; - } - - -/************************************************* -* Count the significant words * -*************************************************/ -u32bit BigInt::sig_words() const - { - return rep.sig_words(); - } - /************************************************* * Count how many bytes are being used * *************************************************/ -- cgit v1.2.3