The code in Hacker's Delight says
nc = -1 - (-d)%d;
But we have
NC = AllOnes - (AllOnes-D)%D
The Hacker's Delight code is written for the LeadingZeros==0 case.
AllOnes - D is not the same as -d from Hacker's Delight.
This patch changes the code to
NC = AllOnes - (AllOnes+1-D)%D
This will increment AllOnes to 0 in the LeadingZeros==0 case. This
will make it equivalent to -D. I believe this is also correct for
LeadingZeros>0.
I've been unable to find a case that changes with the new code.
I've checked all divisors for i8 and i16 udiv and the resulting
code is the same. i32 is a much larger search space to check.