Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify perfect-square testing loop #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions common/smallfact/squfof.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ uint32 squfof_one_cycle(squfof_data_t *data, uint32 mult_idx,
uint64 scaledn;

for (i = 0; i < num_iter; i++) {
uint32 q, bits, tmp;
uint32 q, tmp;

/* compute (sqrtn+p1)/q1; since this is unity
more than half the time, special case the
Expand Down Expand Up @@ -113,18 +113,15 @@ uint32 squfof_one_cycle(squfof_data_t *data, uint32 mult_idx,
/* if q0 is a perfect square, then the factorization
has probably succeeded. Most of the squareness
tests out there require multiple divisions and
complicated loops. We can approximate these tests
by doing two things: testing that the number of
trailing zeros in q0 is even, and then testing
if q0 shifted right this many places is 1 mod 8. */
complicated loops. We approximate these tests by
ensuring that the last 3 bits are 001 after
dividing (shifting) out all factors of 4. */

bits = 0;
tmp = q0;
while(!(tmp & 1)) {
bits++;
tmp >>= 1;
while(!(tmp & 3)) {
tmp >>= 2;
}
if (!(bits & 1) && ((tmp & 7) == 1)) {
if ((tmp & 7) == 1) {

/* q0 is probably a perfect square. Take the
square root by cheating */
Expand Down