summaryrefslogtreecommitdiff
path: root/gmpxx.h
diff options
context:
space:
mode:
authorMarc Glisse <marc.glisse@inria.fr>2013-02-15 14:49:43 +0100
committerMarc Glisse <marc.glisse@inria.fr>2013-02-15 14:49:43 +0100
commit1b159de2cf871d3d9f1f36d91413887c5fcf08f3 (patch)
tree75c6bb926ec85b66fba72c7f042897b2bd10ce72 /gmpxx.h
parentd8495afc3e058a5a3c5d661d0007b1c128b13452 (diff)
downloadgmp-1b159de2cf871d3d9f1f36d91413887c5fcf08f3.tar.gz
Use __builtin_constant_p in mpz_class::init_* and mpz_class::assign_*.
Introduce macro __GMPXX_CONSTANT_TRUE (to be removed if it makes its way into gmp.h).
Diffstat (limited to 'gmpxx.h')
-rw-r--r--gmpxx.h54
1 files changed, 48 insertions, 6 deletions
diff --git a/gmpxx.h b/gmpxx.h
index bd596403a..95ee995d3 100644
--- a/gmpxx.h
+++ b/gmpxx.h
@@ -39,6 +39,7 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
#else
#define __GMPXX_CONSTANT(X) false
#endif
+#define __GMPXX_CONSTANT_TRUE(X) (__GMPXX_CONSTANT(X) && (X))
// Use C++11 features
#ifndef __GMPXX_USE_CXX11
@@ -1425,13 +1426,53 @@ private:
value_type mp;
// Helper functions used for all arithmetic types
- void assign_ui(unsigned long l) { mpz_set_ui(mp, l); }
- void assign_si(signed long l) { mpz_set_si(mp, l); }
- void assign_d (double d) { mpz_set_d (mp, d); }
+ void assign_ui(unsigned long l)
+ {
+ if (__GMPXX_CONSTANT_TRUE(l == 0))
+ mp->_mp_size = 0;
+ else
+ mpz_set_ui(mp, l);
+ }
+ void assign_si(signed long l)
+ {
+ if (__GMPXX_CONSTANT_TRUE(l >= 0))
+ assign_ui(l);
+ else if (__GMPXX_CONSTANT_TRUE(l <= 0))
+ {
+ assign_ui(-static_cast<unsigned long>(l));
+ mpz_neg(mp, mp);
+ }
+ else
+ mpz_set_si(mp, l);
+ }
+ void assign_d (double d)
+ {
+ mpz_set_d (mp, d);
+ }
- void init_ui(unsigned long l) { mpz_init_set_ui(mp, l); }
- void init_si(signed long l) { mpz_init_set_si(mp, l); }
- void init_d (double d) { mpz_init_set_d (mp, d); }
+ void init_ui(unsigned long l)
+ {
+ if (__GMPXX_CONSTANT_TRUE(l == 0))
+ mpz_init(mp);
+ else
+ mpz_init_set_ui(mp, l);
+ }
+ void init_si(signed long l)
+ {
+ if (__GMPXX_CONSTANT_TRUE(l >= 0))
+ init_ui(l);
+ else if (__GMPXX_CONSTANT_TRUE(l <= 0))
+ {
+ init_ui(-static_cast<unsigned long>(l));
+ mpz_neg(mp, mp);
+ }
+ else
+ mpz_init_set_si(mp, l);
+ }
+ void init_d (double d)
+ {
+ mpz_init_set_d (mp, d);
+ }
public:
mp_bitcnt_t get_prec() const { return mpf_get_default_prec(); }
@@ -3337,6 +3378,7 @@ namespace std {
#undef __GMPQ_DEFINE_INCREMENT_OPERATOR
#undef __GMPF_DEFINE_INCREMENT_OPERATOR
+#undef __GMPXX_CONSTANT_TRUE
#undef __GMPXX_CONSTANT
#endif /* __GMP_PLUSPLUS__ */