16.5.3 Perfect Square
A significant fraction of non-squares can be quickly identified by checking whether the input is a quadratic residue modul o small integers.
Mpz_perfect_square_p first tests the input mod, which means just examining the low byte. Only the different values occur for squares mod, so 82.8% of inputs can be immediately identified as Non-squares.
On a 32-bit system similar tests is done mod 9, 5, 7, + and, for a total 99.25% of inputs identified as non-squares. On a 64-bit system "is tested too, for a total 99.62%.
These moduli is chosen because they ' re factors of 2^24-1 (or 2^48-1 for 64-bits), and such a remainder can be quickly tak En just using additions (see MPN_MOD_34LSUB1).
When nails is in use Moduli is instead selected by the GEN-PSQR.C program and applied with an mpn_mod_1. The same 2^24-1 or 2^48-1 could be do with nails using some extra bit shifts, which is not currently implemented.
In all case each modulus are applied to the MPN_MOD_34LSUB1 or mpn_mod_1 remainder and a table lookup identifies Non-square S. By using a "modexact" style calculation, and suitably permuted tables, just one multiply are required, see the code For details. Moduli is also combined to save operations, so long as the lookup tables don ' t become too big. GEN-PSQR.C does all the pre-calculations.
A square root must still is taken for any value that passes these tests, to verify it's really a square and not one of the Small fraction of non-squares that get through (i.e. a pseudo-square to all the tested bases).
Clearly more residue tests could is done, mpz_perfect_square_p only uses a compact and efficient set. Big inputs would probably benefit from more residue testing, small inputs might is better off with less. The assumed distribution of squares versus non-squares in the input would affect such considerations.