diff options
author | lloyd <[email protected]> | 2008-09-07 18:41:38 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-07 18:41:38 +0000 |
commit | 67f1970cf168c4d6b0c773555039a6308694ef9f (patch) | |
tree | af82f2d1831a652664b47d777aef6f196e3b784d | |
parent | 3615001dbdd743413d358a5d815baa8555cc4e79 (diff) |
Inline BigInt::Rep::operator[], BigInt::sig_words, and BigInt::Rep::sig_words
-rw-r--r-- | include/bigint.h | 60 | ||||
-rw-r--r-- | 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<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; diff --git a/src/bigint.cpp b/src/bigint.cpp index 7fb5896f3..b324fd851 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -235,64 +235,6 @@ void BigInt::mask_bits(u32bit n) } /************************************************* -* 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 * *************************************************/ u32bit BigInt::bytes() const |