sb-gmp strange results

Bug #1206191 reported by Stas Boukarev
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Fix Released
Low
Stephan Frank

Bug Description

(sb-gmp:mpz-fac 6) => 2596141001629391711272443916059344

And the low bits are actually correct:
(logand #xFFFFFF (sb-gmp:mpz-fac 6)) => 720

But what's even more strange, sometimes after doing (sb-gmp:mpz-pow 17 2) (which causes a memory fault, because it only checks the type of the second argument but not the first) or some other erroneous thing, (sb-gmp:mpz-fac 6) starts returning 720.
The correct result after errors can be explained that it first does __GMPZ_INIT, but __GMPZ_CLEAR is not protected by UNWIND-PROTECT.

Tags: sb-gmp
Revision history for this message
Stas Boukarev (stassats) wrote :

After doing (with-alien ((fac (struct gmpint))) (__gmpz_init (addr fac))) several times, it starts returning 720 too.

Revision history for this message
Stas Boukarev (stassats) wrote :

with-gmp-mpz-results uses (1+ (abs (slot gres 'mp_size))), the nature of this 1+ is unclear.
On SBCL, bignums do have more than 1 digit in case if the most significant digit has MSB of 1, and in order for the number not to become a negative one, it uses another digit filled with 0.
Removing 1+ solves it for this case.

Revision history for this message
Paul Khuong (pvk) wrote : Re: [Bug 1206191] Re: sb-gmp strange results

Stas Boukarev wrote:
> with-gmp-mpz-results uses (1+ (abs (slot gres 'mp_size))), the nature of this 1+ is unclear.
> On SBCL, bignums do have more than 1 digit in case if the most significant digit has MSB of 1, and in order for the number not to become a negative one, it uses another digit filled with 0.
> Removing 1+ solves it for this case.

I think the right fix is to remove the 1+ when computing size, but to do allocate an extra limb for the result bignum (like the docstrings tell us to). gmp-z-to-bignum-neg also seems to be missing a write to the most significant limb, and a normalisation step.

Revision history for this message
Stephan Frank (sfrank) wrote :

pushed fixes for both problems (i.e. mpz-fac and mpz-pow) as 59e72c69c00abf9095d82e9a7b65f9c1b54d105c to https://github.com/sfrank/sb-gmp

Changed in sbcl:
assignee: nobody → Stephan Frank (sfrank)
status: Triaged → Fix Committed
status: Fix Committed → In Progress
Changed in sbcl:
status: In Progress → Fix Committed
Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.