[PATCH] mpn/generic/perfsqr: Improve alternate (currently disabled) test.

David Sparks sparks05 at proton.me
Thu Mar 5 06:41:46 CET 2026


The one thing I'd do is add an overall comment to both implementations
explaining what's going on.  Something like:
/* In binary, we can check for two things:
   - All odd square numbers must be congruent to 1 mod 8 (i.e.
     end in ...001), and
   - Except for zero, all square numbers must be a power of 4 times an
     odd square number (i.e. end in an even number of trailing 0 bits).
   There are 6 possible residues mod 8 which are not multiples of 4,
   and only one is a possible square, so this excludes 5/6 = 83.333%
   of possible arguments.  However, we don't implement this test
   exactly, and if the number has a large number of trailing zero bits, 
   we let the number fall through through to the general case.
   There are two implementations:
   - One simply uses a table lookup on the low 8 bits.  This
     excludes 212/256 = 82.81% of arguments.
   - Another looks at the entire low limb.  This excludes everything
     except numbers of the form (8*n+5)*2^(GMP_NUMB_BITS-2).  The
     additional cost is tiny, but measurable, and the question is
     whether the 0.52% improvement is worth it.  */

   



More information about the gmp-devel mailing list