The documentation for "C++ Interface General" contains the following
line:
"An important feature of the implementation is that an expression like
a=b+c results in a single call to the corresponding mpz_add, without
using a temporary for the b+c part. Expressions which by their nature
imply intermediate values, like a=b*c+d*e, still use temporaries
though."
I would like to extend it slightly, similar to below, after I bumped
into the problem the hard way.
"An important feature of the implementation is that an expression like
a=b+c results in a single call to the corresponding mpz_add, without
using a temporary for the b+c part. Expressions which by their nature
imply intermediate values, like a=b*c+d*e and (a+b>c), still use
temporaries though, and these automatic temporaries may cause more
memory allocations and slower performance than careful reuse of named
variables."
Below is the good version of my program. The bad version does not use
tmp and instead uses the line "if ((a >> k) % k == 0) {". The bad
version is significantly slower and causes millions of page faults from
k=3800000 onwards (about 6 minutes).
I'm using GMP-6.3.0 on Cygwin with GCC 16.0.0. Example compilation: g++
-o a073633 -std=c++26 -O3 -march=native -W -Wall a073633.cpp
-L/usr/local/lib/ -lgmpxx -lgmp
#include <gmpxx.h>
int main() {
mpz_class a = 1, tmp;
unsigned n = 0;
for (unsigned long k = 1; true; k++) {
a *= 3;
tmp = a >> k;
tmp %= k;
if (tmp == 0) {
printf("a(%u)=%lu\n", ++n, k);
}
if (k % 100000 == 0) {
fprintf(stderr, "%lu..", k);
}
}
}
Martin
_______________________________________________
gmp-bugs mailing list
[email protected]
https://gmplib.org/mailman/listinfo/gmp-bugs