From 9f389e512c00d0c45731d93e2977c14c408a4356 Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 15 Sep 2008 22:58:34 +0000 Subject: Remove the cache for BigInt::sig_words. I'm baffled how it is it works on x86, x86-64, and m68k and not other platforms. Something about the memory model I'm hitting? Valgrind shows nothing. Rather than struggle with it further, for minimal gain, I'm reverting. If someone ever does figure it out, this will be easy to reapply. --- include/bigint.h | 91 +++++++++++++++----------------------------------------- 1 file changed, 24 insertions(+), 67 deletions(-) (limited to 'include') diff --git a/include/bigint.h b/include/bigint.h index 2231ba99c..b1286551e 100644 --- a/include/bigint.h +++ b/include/bigint.h @@ -50,10 +50,14 @@ class BOTAN_DLL BigInt bool is_zero() const { - for(u32bit i = 0; i != rep.sig_words(); ++i) - if(rep[i]) return false; + const u32bit sw = sig_words(); + + for(u32bit i = 0; i != sw; ++i) + if(reg[i]) + return false; return true; } + bool is_nonzero() const { return (!is_zero()); } void set_bit(u32bit); @@ -79,18 +83,29 @@ class BOTAN_DLL BigInt BigInt abs() const; u32bit size() const { return get_reg().size(); } - u32bit sig_words() const { return rep.sig_words(); } + + u32bit sig_words() const + { + const word* x = reg.begin(); + u32bit sig = reg.size(); + + while(sig && (x[sig-1] == 0)) + sig--; + return sig; + } + u32bit bytes() const; u32bit bits() const; - const word* data() const { return get_reg().begin(); } - SecureVector& get_reg() { return rep.get_reg(); } - const SecureVector& get_reg() const { return rep.get_reg(); } + const word* data() const { return reg.begin(); } + SecureVector& get_reg() { return reg; } + const SecureVector& get_reg() const { return reg; } + void grow_reg(u32bit); void grow_to(u32bit); - word& operator[](u32bit i) { return rep[i]; } - word operator[](u32bit i) const { return rep[i]; } + word& operator[](u32bit i) { return reg[i]; } + word operator[](u32bit i) const { return reg[i]; } void clear() { get_reg().clear(); } void randomize(RandomNumberGenerator& rng, u32bit n); @@ -117,65 +132,7 @@ class BOTAN_DLL BigInt BigInt(Sign, u32bit); BigInt(NumberType, u32bit); 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 n) - { - sig = INVALID_SIG_WORD; - - if(n > reg.size()) - reg.grow_to(n+1); - return reg[n]; - } - - word operator[](u32bit n) const - { - if(n > reg.size()) - return 0; - return reg[n]; - } - - 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; + SecureVector reg; Sign signedness; }; -- cgit v1.2.3