Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,830
    • Issues 4,830
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 447
    • Merge requests 447
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #15350

Closed
Open
Created Jul 06, 2018 by Bodigrim@BodigrimDeveloper

gcdExtInteger violates assertion

{-# LANGUAGE UnboxedTuples #-}
import GHC.Integer.GMP.Internals

main = let (# _, s #) = gcdExtInteger 2 (2^65 + 1) in print s

fails with

Assertion failed: (sn <= mp_size_abs(xn)), function integer_gmp_gcdext, file libraries/integer-gmp/cbits/wrappers.c, line 316.
Abort trap: 6

It happens because s = -2^64 and does not fit one-limbed BigNat. The implementation of gcdExtInteger x y (https://github.com/ghc/ghc/blob/master/libraries/integer-gmp/src/GHC/Integer/Type.hs\#L1392) allocates for s a buffer, equal to size of x (one limb in our case), but according to GMP manual (https://gmplib.org/manual/Number-Theoretic-Functions.html\#index-mpz_005fgcdext) it should be equal to size of y (two limbs in our case).

Hopefully, the diff is simple enough for a PR on GitHub (https://github.com/ghc/ghc/pull/163). Otherwise I'll be happy to prepare a patch for Phabricator.

-        s@(MBN# s#) <- newBigNat# (absI# xn#)
+        s@(MBN# s#) <- newBigNat# (absI# yn#)

Reopening, because

{-# LANGUAGE UnboxedTuples #-}
import GHC.Integer.GMP.Internals

main = let (# _, s #) = gcdExtInteger (- (2^63 - 1) * 2^63) 0 in print s

fails in GHC 8.6.1 with

Assertion failed: (0 <= gn && gn <= gn0), function integer_gmp_gcdext, file libraries/integer-gmp/cbits/wrappers.c, line 309.
Abort trap: 6

I have not yet understood what is going on.

Edited Mar 10, 2019 by Bodigrim
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking