[GHC] #9281: Rewrite `integer-gmp` to use only non-allocating GMP functions

GHC ghc-devs at haskell.org
Fri Jul 11 12:27:21 UTC 2014


#9281: Rewrite `integer-gmp` to use only non-allocating GMP functions
--------------------------------------+------------------------------------
        Reporter:  hvr                |            Owner:  hvr
            Type:  task               |           Status:  new
        Priority:  normal             |        Milestone:  7.10.1
       Component:  libraries (other)  |          Version:
      Resolution:                     |         Keywords:  integer-gmp
Operating System:  Unknown/Multiple   |     Architecture:  Unknown/Multiple
 Type of failure:  None/Unknown       |       Difficulty:  Unknown
       Test Case:                     |       Blocked By:
        Blocking:                     |  Related Tickets:
--------------------------------------+------------------------------------

Comment (by hvr):

 As a brain-dump, here's the new representation I'm working on (which
 happens to make it trivial to provide a natural number type `Nat`):

 {{{#!hs
 type GmpLimb = Word -- actually, 'CULong'
 type GmpSize = Int  -- actually, 'CLong'

 -- | Type representing /pure/ BigNats
 --
 -- @ByteArray#@ is interpreted as an unboxed array of 'GmpLimb's
 --
 -- Invariants:
 --
 --  - ByteArray#'s size is the exact multiple of non-zero limbs (but never
 empty)
 --  - @0@ is represented as a 1-limb (available as 'bnZero')
 data BigNat = BN# ByteArray#

 -- | Invariant: 'NatJ#' is used iff value doesn't fit in 'NatS#'
 data Nat = NatS# {-# UNPACK #-} !GmpLimb
          | NatJ# {-# UNPACK #-} !BigNat

 -- | Invariant: 'Jn#'/'Jp#' are used iff value doesn't fit in 'S#'
 --
 -- NB: less than 4 constructors allows pointer-tagging to work
 data Integer = S#  {-# UNPACK #-} !Int
              | Jn# {-# UNPACK #-} !BigNat -- negative
              | Jp# {-# UNPACK #-} !BigNat -- positive
 }}}

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9281#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list