qwr
qwr

Reputation: 10958

Does Boost's gmp_int backend automatically optimize?

From my understanding of Boost's multi-precision wrappers, they will automatically take expressions like c = a+b and turn them into mpz_add(c, a, b). Will Boost automatically perform optimizations such as turning multiplication by a power of 2 into left/right shifts, or other similar changes?

Upvotes: 1

Views: 114

Answers (1)

Marc Glisse
Marc Glisse

Reputation: 7925

Boost.Multiprecision performs a number of advanced optimizations, mostly to minimize the number of temporary variables. For instance, it will reorder a=(b*c)*a to a*=c; a*=b. However, it does not (currently, Aug 2014) take advantage of gcc's __builtin_constant_p to optimize even further. gmpxx (the official wrapper distributed with GMP) on the other hand will replace a=b*8 with a=b<<3. Note that without __builtin_constant_p, it would be a bad idea (in a generic wrapper) to test if the number is a power of 2 before multiplying: if it was worthwhile, mpz_mul_ui would already do it.

Also, last time I checked, other compilers (clang, intel, oracle) only had minimal support for __builtin_constant_p and couldn't take advantage of its use in gmpxx.

Upvotes: 3

Related Questions