From 2ffa6f3f1616d7db3503600ac047a10b01a6bb54 Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 19 Nov 2009 18:03:17 +0000 Subject: Add move assignment and constructor operators to BigInt. On macro benchmarks (timing the test suite) there doesn't seem to be much of a difference either way, but putting printfs in the implementations shows they are being used. Since they pretty much can't possibly hurt (right?) and might well help a lot in certain cases, it's worth including. Todo: adding overloads to binary operators taking move operations might be nice. Details in http://cpp-next.com/archive/2009/09/making-your-next-move/ --- src/math/bigint/bigint.cpp | 25 ++++++++++++++++++++++++- src/math/bigint/bigint.h | 19 ++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) (limited to 'src/math/bigint') diff --git a/src/math/bigint/bigint.cpp b/src/math/bigint/bigint.cpp index 7592ec439..36739f047 100644 --- a/src/math/bigint/bigint.cpp +++ b/src/math/bigint/bigint.cpp @@ -40,7 +40,7 @@ BigInt::BigInt(Sign s, u32bit size) } /* -* Construct a BigInt from a "raw" BigInt +* Copy constructor */ BigInt::BigInt(const BigInt& b) { @@ -100,6 +100,29 @@ BigInt::BigInt(RandomNumberGenerator& rng, u32bit bits) randomize(rng, bits); } +/** +* Move constructor +*/ +BigInt::BigInt(BigInt&& other) + { + reg.swap(other.reg); + signedness = other.signedness; + } + +/** +* Move assignment +*/ +BigInt& BigInt::operator=(BigInt&& other) + { + if(this != &other) + { + reg.swap(other.reg); + signedness = other.signedness; + } + + return (*this); + } + /* * Swap this BigInt with another */ diff --git a/src/math/bigint/bigint.h b/src/math/bigint/bigint.h index 4f7f6f5bd..3db9a1a04 100644 --- a/src/math/bigint/bigint.h +++ b/src/math/bigint/bigint.h @@ -425,10 +425,14 @@ class BOTAN_DLL BigInt BigInt(u64bit n); /** - * Copy-Constructor: clone given BigInt - * @param bigint the BigInt to clone + * Copy constructor */ - BigInt(const BigInt& bigint); + BigInt(const BigInt& other); + + /** + * Assignment operator + */ + BigInt& operator=(const BigInt&) = default; /** * Create BigInt from a string. @@ -471,6 +475,15 @@ class BOTAN_DLL BigInt */ BigInt(NumberType type, u32bit n); + /** + * Move constructor + */ + BigInt(BigInt&& other); + + /** + * Move assignment + */ + BigInt& operator=(BigInt&& other); private: SecureVector reg; Sign signedness; -- cgit v1.2.3 From bab01ef1514266e6f23d9163e503c115147ab993 Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 19 Nov 2009 18:12:32 +0000 Subject: Define move assignment and constructors in terms of std::swap (which boils down to BigInt::swap, which uses the memvec swap). Checking with g++ 4.5 -O3 shows it compiles down the same code as before. --- src/math/bigint/bigint.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/math/bigint') diff --git a/src/math/bigint/bigint.cpp b/src/math/bigint/bigint.cpp index 36739f047..c777e770b 100644 --- a/src/math/bigint/bigint.cpp +++ b/src/math/bigint/bigint.cpp @@ -105,8 +105,7 @@ BigInt::BigInt(RandomNumberGenerator& rng, u32bit bits) */ BigInt::BigInt(BigInt&& other) { - reg.swap(other.reg); - signedness = other.signedness; + std::swap(*this, other); } /** @@ -115,10 +114,7 @@ BigInt::BigInt(BigInt&& other) BigInt& BigInt::operator=(BigInt&& other) { if(this != &other) - { - reg.swap(other.reg); - signedness = other.signedness; - } + std::swap(*this, other); return (*this); } -- cgit v1.2.3