stronglucas.c

Niels Möller nisse at lysator.liu.se
Wed May 18 07:48:59 CEST 2022


Seth Troisi <braintwo at gmail.com> writes:

> I was reading the stronglucas code and I think this small change may
> improve clarity
>
> diff -r 970b7221873f mpz/stronglucas.c
> --- a/mpz/stronglucas.c Mon Nov 16 19:48:04 2020 +0100
> +++ b/mpz/stronglucas.c Thu May 12 14:10:15 2022 -0700
> @@ -134,7 +134,7 @@
>      int jac;
>
>      /* n is odd, to possibly be a square, n % 8 = 1 is needed. */
> -    if (((*PTR (n) & 6) == 0) && UNLIKELY (mpz_perfect_square_p (n)))
> +    if (((*PTR (n) & 7) == 1) && UNLIKELY (mpz_perfect_square_p (n)))
>        return 0; /* A square is composite. */
>
>      /* Check Ds up to square root (in case, n is prime)
>
> Technically n/x should be odd per the function comment but IMO this
> improves readability by having the "n % 8 = 1" in the nearer comment match
> the code.

I think the way it's currently written is a micro optimization, testing
n % 6 == 0 can typically be compiled to a tst instruction, while n % 7
== 1 would typically be and + cmp.

That said, I don't think this code is that performance critical, so
saving a single instruction doesn't matter much. I agree your change
improves readability a bit.

> It's also possible lines 122-124 should also be indented

Indentation looks right to me, at the current place. But perhaps more
consistent with nearby code if comments are moved below the "else if"
line (and then indented accordingly for that location).

Regards,
/Niels

-- 
Niels Möller. PGP key CB4962D070D77D7FCB8BA36271D8F1FF368C6677.
Internet email is subject to wholesale government surveillance.


More information about the gmp-devel mailing list