{-# language MagicHash #-}
{-# language UnboxedTuples #-}
{-# language RoleAnnotations #-}
{-# language UnliftedNewtypes #-}
{-# language KindSignatures #-}
{-# language ScopedTypeVariables #-}
{- OPTIONS_GHC -ddump-simpl #-}

-- See UnsafeCoercions.md for an explanation of why we coerce
-- things the way we do here, and why some operations are marked
-- NOINLINE.

-- |
-- Primitive types representing unlifted arrays and the
-- primops for manipulating them.
module Data.Primitive.Unlifted.SmallArray.Primops
  ( -- * Types
    SmallUnliftedArray#
  , SmallMutableUnliftedArray#
    -- We don't export the newtype constructors because they're bogus and
    -- because there's basically no reason they'd ever be used. This module
    -- contains a wrapped version of every Array# primop.  Eventually, all this
    -- stuff will be in GHC.Prim, possibly with other names.

    -- * Operations
  , newSmallUnliftedArray#
  , unsafeNewSmallUnliftedArray#
  , emptySmallUnliftedArray#
  , sameSmallMutableUnliftedArray#
  , shrinkSmallMutableUnliftedArray#
  , readSmallUnliftedArray#
  , writeSmallUnliftedArray#
  , sizeofSmallUnliftedArray#
  , getSizeofSmallMutableUnliftedArray#
  , indexSmallUnliftedArray#
  , unsafeFreezeSmallUnliftedArray#
  , unsafeThawSmallUnliftedArray#
  , copySmallUnliftedArray#
  , copySmallMutableUnliftedArray#
  , cloneSmallUnliftedArray#
  , cloneSmallMutableUnliftedArray#
  , freezeSmallUnliftedArray#
  , thawSmallUnliftedArray#
  , casSmallUnliftedArray#
  ) where

import GHC.Exts (Int#,State#,SmallArray#,SmallMutableArray#,Any,TYPE,RuntimeRep(UnliftedRep),unsafeCoerce#)
import qualified GHC.Exts as Exts

newtype SmallUnliftedArray# (a :: TYPE 'UnliftedRep) = SmallUnliftedArray# (SmallArray# Any)
type role SmallUnliftedArray# representational

newtype SmallMutableUnliftedArray# s (a :: TYPE 'UnliftedRep) = SmallMutableUnliftedArray# (SmallMutableArray# s Any)
type role SmallMutableUnliftedArray# nominal representational

newSmallUnliftedArray# :: forall a s. Int# -> a -> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
newSmallUnliftedArray# :: Int#
-> a -> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
newSmallUnliftedArray# Int#
sz a
a State# s
s = case Int# -> Any -> State# s -> (# State# s, SmallMutableArray# s Any #)
forall a d.
Int# -> a -> State# d -> (# State# d, SmallMutableArray# d a #)
Exts.newSmallArray# Int#
sz (a -> Any
unsafeCoerce# a
a) State# s
s of
  (# State# s
s', SmallMutableArray# s Any
mary #) -> (# State# s
s', SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
forall s (a :: TYPE 'UnliftedRep).
SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
SmallMutableUnliftedArray# SmallMutableArray# s Any
mary #)
{-# NOINLINE newSmallUnliftedArray# #-}

-- | Create a 'SmallMutableUnliftedArray#' whose entries contain some unspecified
-- static value. This may be more convenient than 'newUnliftedArray#' if there
-- is no value on hand with which to initialize the array. Each entry must be
-- initialized before being read and used. This condition is not checked.
unsafeNewSmallUnliftedArray# :: Int# -> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
-- We fill the array with the Nonsense data constructor. It doesn't much matter
-- *what* we stick in there, as long as it's a pointer the garbage collector
-- can understand and isn't something that might otherwise be released as garbage.
-- There's no point trying to stick an `error` in there, because there's no
-- code anywhere to force the error thunk.
unsafeNewSmallUnliftedArray# :: Int# -> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
unsafeNewSmallUnliftedArray# Int#
sz State# s
s = case Int# -> Any -> State# s -> (# State# s, SmallMutableArray# s Any #)
forall a d.
Int# -> a -> State# d -> (# State# d, SmallMutableArray# d a #)
Exts.newSmallArray# Int#
sz (Nonsense -> Any
unsafeCoerce# Nonsense
Nonsense) State# s
s of
  (# State# s
s', SmallMutableArray# s Any
mary #) -> (# State# s
s', SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
forall s (a :: TYPE 'UnliftedRep).
SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
SmallMutableUnliftedArray# SmallMutableArray# s Any
mary #)
{-# NOINLINE unsafeNewSmallUnliftedArray# #-}

data Nonsense = Nonsense


-- This represents a *statically allocated* value, preferably in a *read-only*
-- segment of memory.
--
-- Why do we bother to noDuplicate#? It generally doesn't much *matter* if
-- different threads have different global empty arrays. However, for
-- performance testing purposes, a user may well want to check whether the
-- empty arrays they expect to be the global ones really are. Such a test
-- is only possible if there's just *one* array to test against. The overhead
-- of the once-ever noDuplicate# call is sure to be trivial anyway.
empty_small_unlifted_array :: SULA a
empty_small_unlifted_array :: SULA a
empty_small_unlifted_array = SmallUnliftedArray# a -> SULA a
forall (a :: TYPE 'UnliftedRep). SmallUnliftedArray# a -> SULA a
SULA
  ((State# RealWorld -> SmallUnliftedArray# a)
-> SmallUnliftedArray# a
forall o. (State# RealWorld -> o) -> o
Exts.runRW# ((State# RealWorld -> SmallUnliftedArray# a)
 -> SmallUnliftedArray# a)
-> (State# RealWorld -> SmallUnliftedArray# a)
-> SmallUnliftedArray# a
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
    case State# RealWorld -> State# RealWorld
forall d. State# d -> State# d
Exts.noDuplicate# State# RealWorld
s of { State# RealWorld
s' ->
    case Int#
-> Any
-> State# RealWorld
-> (# State# RealWorld, SmallMutableArray# RealWorld Any #)
forall a d.
Int# -> a -> State# d -> (# State# d, SmallMutableArray# d a #)
Exts.newSmallArray# Int#
0# (Nonsense -> Any
unsafeCoerce# Nonsense
Nonsense) State# RealWorld
s' of { (# State# RealWorld
s'', SmallMutableArray# RealWorld Any
mary #) ->
    case SmallMutableArray# RealWorld Any
-> State# RealWorld -> (# State# RealWorld, SmallArray# Any #)
forall d a.
SmallMutableArray# d a -> State# d -> (# State# d, SmallArray# a #)
Exts.unsafeFreezeSmallArray# SmallMutableArray# RealWorld Any
mary State# RealWorld
s'' of { (# State# RealWorld
_, SmallArray# Any
ary #) ->
      SmallArray# Any -> SmallUnliftedArray# a
forall (a :: TYPE 'UnliftedRep).
SmallArray# Any -> SmallUnliftedArray# a
SmallUnliftedArray# SmallArray# Any
ary }}})
{-# NOINLINE empty_small_unlifted_array #-}

data SULA a = SULA (SmallUnliftedArray# a)

-- | Warning: Applying 'unsafeThawUnliftedArray#' to the array produced by
-- this function will make demons come out of your nose.
emptySmallUnliftedArray# :: (##) -> SmallUnliftedArray# a
-- We make this primitive because it's the easiest way to get a
-- *shared* primitive unlifted array.
--
-- Why the stern warning above? GHC does not currently support resizing 'Array#',
-- and does not really meaningfully support *growing* arrays of any type. If,
-- however, that ever changes, growing the globally shared empty array would be
-- pretty disastrous.
emptySmallUnliftedArray# :: (# #) -> SmallUnliftedArray# a
emptySmallUnliftedArray# (##) = case SULA a
forall (a :: TYPE 'UnliftedRep). SULA a
empty_small_unlifted_array of
  SULA SmallUnliftedArray# a
ary -> SmallUnliftedArray# a
ary
{-# INLINE emptySmallUnliftedArray# #-}

sameSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> SmallMutableUnliftedArray# s a -> Int#
sameSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a
-> SmallMutableUnliftedArray# s a -> Int#
sameSmallMutableUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
ar1) (SmallMutableUnliftedArray# SmallMutableArray# s Any
ar2)
  = SmallMutableArray# s Any -> SmallMutableArray# s Any -> Int#
forall d a.
SmallMutableArray# d a -> SmallMutableArray# d a -> Int#
Exts.sameSmallMutableArray# SmallMutableArray# s Any
ar1 SmallMutableArray# s Any
ar2
{-# INLINE sameSmallMutableUnliftedArray# #-}

shrinkSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> State# s -> State# s
shrinkSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> State# s -> State# s
shrinkSmallMutableUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
ar) Int#
sz State# s
s
  = SmallMutableArray# s Any -> Int# -> State# s -> State# s
forall d a. SmallMutableArray# d a -> Int# -> State# d -> State# d
Exts.shrinkSmallMutableArray# SmallMutableArray# s Any
ar Int#
sz State# s
s
{-# INLINE shrinkSmallMutableUnliftedArray# #-}

readSmallUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> State# s -> (# State# s, a #)
readSmallUnliftedArray# :: SmallMutableUnliftedArray# s a
-> Int# -> State# s -> (# State# s, a #)
readSmallUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) Int#
i State# s
s
  = (# State# s, Any #) -> (# State# s, a #)
unsafeCoerce# (SmallMutableArray# s Any -> Int# -> State# s -> (# State# s, Any #)
forall d a.
SmallMutableArray# d a -> Int# -> State# d -> (# State# d, a #)
Exts.readSmallArray# SmallMutableArray# s Any
mary Int#
i State# s
s)
{-# NOINLINE readSmallUnliftedArray# #-}

writeSmallUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> a -> State# s -> State# s
writeSmallUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> a -> State# s -> State# s
writeSmallUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) Int#
i a
a State# s
s
  = SmallMutableArray# s Any -> Int# -> Any -> State# s -> State# s
forall d a.
SmallMutableArray# d a -> Int# -> a -> State# d -> State# d
Exts.writeSmallArray# SmallMutableArray# s Any
mary Int#
i (a -> Any
unsafeCoerce# a
a) State# s
s
{-# NOINLINE writeSmallUnliftedArray# #-}

sizeofSmallUnliftedArray# :: SmallUnliftedArray# a -> Int#
sizeofSmallUnliftedArray# :: SmallUnliftedArray# a -> Int#
sizeofSmallUnliftedArray# (SmallUnliftedArray# SmallArray# Any
ary) = SmallArray# Any -> Int#
forall a. SmallArray# a -> Int#
Exts.sizeofSmallArray# SmallArray# Any
ary
{-# INLINE sizeofSmallUnliftedArray# #-}

getSizeofSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> State# s -> (# State# s, Int# #)
getSizeofSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> State# s -> (# State# s, Int# #)
getSizeofSmallMutableUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) State# s
s
  = SmallMutableArray# s Any -> State# s -> (# State# s, Int# #)
forall d a.
SmallMutableArray# d a -> State# d -> (# State# d, Int# #)
Exts.getSizeofSmallMutableArray# SmallMutableArray# s Any
mary State# s
s
{-# INLINE getSizeofSmallMutableUnliftedArray# #-}

{-
--The underlying primop is deprecated in GHC.Prim, so let's not do this.
sizeofSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int#
sizeofSmallMutableUnliftedArray# (SmallMutableUnliftedArray# mary)
  = Exts.sizeofSmallMutableArray# mary
{-# INLINE sizeofSmallMutableUnliftedArray# #-}
-}

indexSmallUnliftedArray# :: SmallUnliftedArray# a -> Int# -> a
indexSmallUnliftedArray# :: SmallUnliftedArray# a -> Int# -> a
indexSmallUnliftedArray# (SmallUnliftedArray# SmallArray# Any
ary) Int#
i
  = (# Any #) -> a
unsafeCoerce# (SmallArray# Any -> Int# -> (# Any #)
forall a. SmallArray# a -> Int# -> (# a #)
Exts.indexSmallArray# SmallArray# Any
ary Int#
i)
{-# NOINLINE indexSmallUnliftedArray# #-}

unsafeFreezeSmallUnliftedArray# :: SmallMutableUnliftedArray# s a -> State# s -> (# State# s, SmallUnliftedArray# a #)
unsafeFreezeSmallUnliftedArray# :: SmallMutableUnliftedArray# s a
-> State# s -> (# State# s, SmallUnliftedArray# a #)
unsafeFreezeSmallUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) State# s
s
  = case SmallMutableArray# s Any
-> State# s -> (# State# s, SmallArray# Any #)
forall d a.
SmallMutableArray# d a -> State# d -> (# State# d, SmallArray# a #)
Exts.unsafeFreezeSmallArray# SmallMutableArray# s Any
mary State# s
s of
      (# State# s
s', SmallArray# Any
ary #) -> (# State# s
s', SmallArray# Any -> SmallUnliftedArray# a
forall (a :: TYPE 'UnliftedRep).
SmallArray# Any -> SmallUnliftedArray# a
SmallUnliftedArray# SmallArray# Any
ary #)
{-# INLINE unsafeFreezeSmallUnliftedArray# #-}

unsafeThawSmallUnliftedArray# :: SmallUnliftedArray# a -> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
unsafeThawSmallUnliftedArray# :: SmallUnliftedArray# a
-> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
unsafeThawSmallUnliftedArray# (SmallUnliftedArray# SmallArray# Any
ary) State# s
s
  = case SmallArray# Any
-> State# s -> (# State# s, SmallMutableArray# s Any #)
forall a d.
SmallArray# a -> State# d -> (# State# d, SmallMutableArray# d a #)
Exts.unsafeThawSmallArray# SmallArray# Any
ary State# s
s of
     (# State# s
s', SmallMutableArray# s Any
mary #) -> (# State# s
s', SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
forall s (a :: TYPE 'UnliftedRep).
SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
SmallMutableUnliftedArray# SmallMutableArray# s Any
mary #)
{-# INLINE unsafeThawSmallUnliftedArray# #-}

copySmallUnliftedArray# :: SmallUnliftedArray# a -> Int# -> SmallMutableUnliftedArray# s a -> Int# -> Int# -> State# s -> State# s
copySmallUnliftedArray# :: SmallUnliftedArray# a
-> Int#
-> SmallMutableUnliftedArray# s a
-> Int#
-> Int#
-> State# s
-> State# s
copySmallUnliftedArray# (SmallUnliftedArray# SmallArray# Any
ary) Int#
i1 (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) Int#
i2 Int#
n State# s
s
  = SmallArray# Any
-> Int#
-> SmallMutableArray# s Any
-> Int#
-> Int#
-> State# s
-> State# s
forall a d.
SmallArray# a
-> Int#
-> SmallMutableArray# d a
-> Int#
-> Int#
-> State# d
-> State# d
Exts.copySmallArray# SmallArray# Any
ary Int#
i1 SmallMutableArray# s Any
mary Int#
i2 Int#
n State# s
s
{-# INLINE copySmallUnliftedArray# #-}

copySmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> SmallMutableUnliftedArray# s a -> Int# -> Int# -> State# s -> State# s
copySmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a
-> Int#
-> SmallMutableUnliftedArray# s a
-> Int#
-> Int#
-> State# s
-> State# s
copySmallMutableUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary1) Int#
i1 (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary2) Int#
i2 Int#
n State# s
s
  = SmallMutableArray# s Any
-> Int#
-> SmallMutableArray# s Any
-> Int#
-> Int#
-> State# s
-> State# s
forall d a.
SmallMutableArray# d a
-> Int#
-> SmallMutableArray# d a
-> Int#
-> Int#
-> State# d
-> State# d
Exts.copySmallMutableArray# SmallMutableArray# s Any
mary1 Int#
i1 SmallMutableArray# s Any
mary2 Int#
i2 Int#
n State# s
s
{-# INLINE copySmallMutableUnliftedArray# #-}

cloneSmallUnliftedArray# :: SmallUnliftedArray# a -> Int# -> Int# -> SmallUnliftedArray# a
cloneSmallUnliftedArray# :: SmallUnliftedArray# a -> Int# -> Int# -> SmallUnliftedArray# a
cloneSmallUnliftedArray# (SmallUnliftedArray# SmallArray# Any
ary) Int#
i Int#
n
  = SmallArray# Any -> SmallUnliftedArray# a
forall (a :: TYPE 'UnliftedRep).
SmallArray# Any -> SmallUnliftedArray# a
SmallUnliftedArray# (SmallArray# Any -> Int# -> Int# -> SmallArray# Any
forall a. SmallArray# a -> Int# -> Int# -> SmallArray# a
Exts.cloneSmallArray# SmallArray# Any
ary Int#
i Int#
n)
{-# INLINE cloneSmallUnliftedArray# #-}

cloneSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> Int# -> State# s
  -> (# State# s, SmallMutableUnliftedArray# s a #)
cloneSmallMutableUnliftedArray# :: SmallMutableUnliftedArray# s a
-> Int#
-> Int#
-> State# s
-> (# State# s, SmallMutableUnliftedArray# s a #)
cloneSmallMutableUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) Int#
i Int#
n State# s
s
  = case SmallMutableArray# s Any
-> Int#
-> Int#
-> State# s
-> (# State# s, SmallMutableArray# s Any #)
forall d a.
SmallMutableArray# d a
-> Int#
-> Int#
-> State# d
-> (# State# d, SmallMutableArray# d a #)
Exts.cloneSmallMutableArray# SmallMutableArray# s Any
mary Int#
i Int#
n State# s
s of
      (# State# s
s', SmallMutableArray# s Any
mary' #) -> (# State# s
s', SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
forall s (a :: TYPE 'UnliftedRep).
SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
SmallMutableUnliftedArray# SmallMutableArray# s Any
mary' #)
{-# INLINE cloneSmallMutableUnliftedArray# #-}

freezeSmallUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> Int# -> State# s -> (# State# s, SmallUnliftedArray# a #)
freezeSmallUnliftedArray# :: SmallMutableUnliftedArray# s a
-> Int#
-> Int#
-> State# s
-> (# State# s, SmallUnliftedArray# a #)
freezeSmallUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) Int#
i Int#
n State# s
s
  = case SmallMutableArray# s Any
-> Int# -> Int# -> State# s -> (# State# s, SmallArray# Any #)
forall d a.
SmallMutableArray# d a
-> Int# -> Int# -> State# d -> (# State# d, SmallArray# a #)
Exts.freezeSmallArray# SmallMutableArray# s Any
mary Int#
i Int#
n State# s
s of
      (# State# s
s', SmallArray# Any
ary #) -> (# State# s
s', SmallArray# Any -> SmallUnliftedArray# a
forall (a :: TYPE 'UnliftedRep).
SmallArray# Any -> SmallUnliftedArray# a
SmallUnliftedArray# SmallArray# Any
ary #)
{-# INLINE freezeSmallUnliftedArray# #-}

thawSmallUnliftedArray# :: SmallUnliftedArray# a -> Int# -> Int# -> State# s -> (# State# s, SmallMutableUnliftedArray# s a #)
thawSmallUnliftedArray# :: SmallUnliftedArray# a
-> Int#
-> Int#
-> State# s
-> (# State# s, SmallMutableUnliftedArray# s a #)
thawSmallUnliftedArray# (SmallUnliftedArray# SmallArray# Any
ary) Int#
i Int#
n State# s
s
  = case SmallArray# Any
-> Int#
-> Int#
-> State# s
-> (# State# s, SmallMutableArray# s Any #)
forall a d.
SmallArray# a
-> Int#
-> Int#
-> State# d
-> (# State# d, SmallMutableArray# d a #)
Exts.thawSmallArray# SmallArray# Any
ary Int#
i Int#
n State# s
s of
      (# State# s
s', SmallMutableArray# s Any
mary #) -> (# State# s
s', SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
forall s (a :: TYPE 'UnliftedRep).
SmallMutableArray# s Any -> SmallMutableUnliftedArray# s a
SmallMutableUnliftedArray# SmallMutableArray# s Any
mary #)
{-# INLINE thawSmallUnliftedArray# #-}

casSmallUnliftedArray# :: SmallMutableUnliftedArray# s a -> Int# -> a -> a -> State# s -> (# State# s, Int#, a #)
casSmallUnliftedArray# :: SmallMutableUnliftedArray# s a
-> Int# -> a -> a -> State# s -> (# State# s, Int#, a #)
casSmallUnliftedArray# (SmallMutableUnliftedArray# SmallMutableArray# s Any
mary) Int#
i a
x a
y State# s
s
  = (# State# s, Int#, Any #) -> (# State# s, Int#, a #)
unsafeCoerce# (SmallMutableArray# s Any
-> Int# -> Any -> Any -> State# s -> (# State# s, Int#, Any #)
forall d a.
SmallMutableArray# d a
-> Int# -> a -> a -> State# d -> (# State# d, Int#, a #)
Exts.casSmallArray# SmallMutableArray# s Any
mary Int#
i (a -> Any
unsafeCoerce# a
x) (a -> Any
unsafeCoerce# a
y) State# s
s)
{-# NOINLINE casSmallUnliftedArray# #-}