{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples #-}
{-# OPTIONS_GHC -O2 #-}
-- We always optimise this, otherwise performance of a non-optimised
-- compiler is severely affected
--
-- (c) The University of Glasgow 2002-2006
--
-- Unboxed mutable Ints

module FastMutInt(
        FastMutInt, newFastMutInt,
        readFastMutInt, writeFastMutInt,

        FastMutPtr, newFastMutPtr,
        readFastMutPtr, writeFastMutPtr
  ) where

import GhcPrelude

import Data.Bits
import GHC.Base
import GHC.Ptr

newFastMutInt :: IO FastMutInt
readFastMutInt :: FastMutInt -> IO Int
writeFastMutInt :: FastMutInt -> Int -> IO ()

newFastMutPtr :: IO FastMutPtr
readFastMutPtr :: FastMutPtr -> IO (Ptr a)
writeFastMutPtr :: FastMutPtr -> Ptr a -> IO ()

data FastMutInt = FastMutInt (MutableByteArray# RealWorld)

newFastMutInt :: IO FastMutInt
newFastMutInt = (State# RealWorld -> (# State# RealWorld, FastMutInt #))
-> IO FastMutInt
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, FastMutInt #))
 -> IO FastMutInt)
-> (State# RealWorld -> (# State# RealWorld, FastMutInt #))
-> IO FastMutInt
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
  case Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
size State# RealWorld
s of { (# s :: State# RealWorld
s, arr :: MutableByteArray# RealWorld
arr #) ->
  (# State# RealWorld
s, MutableByteArray# RealWorld -> FastMutInt
FastMutInt MutableByteArray# RealWorld
arr #) }
  where !(I# size :: Int#
size) = Int -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (0 :: Int)

readFastMutInt :: FastMutInt -> IO Int
readFastMutInt (FastMutInt arr :: MutableByteArray# RealWorld
arr) = (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int)
-> (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
  case MutableByteArray# RealWorld
-> Int# -> State# RealWorld -> (# State# RealWorld, Int# #)
forall d.
MutableByteArray# d -> Int# -> State# d -> (# State# d, Int# #)
readIntArray# MutableByteArray# RealWorld
arr 0# State# RealWorld
s of { (# s :: State# RealWorld
s, i :: Int#
i #) ->
  (# State# RealWorld
s, Int# -> Int
I# Int#
i #) }

writeFastMutInt :: FastMutInt -> Int -> IO ()
writeFastMutInt (FastMutInt arr :: MutableByteArray# RealWorld
arr) (I# i :: Int#
i) = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
  case MutableByteArray# RealWorld
-> Int# -> Int# -> State# RealWorld -> State# RealWorld
forall d.
MutableByteArray# d -> Int# -> Int# -> State# d -> State# d
writeIntArray# MutableByteArray# RealWorld
arr 0# Int#
i State# RealWorld
s of { s :: State# RealWorld
s ->
  (# State# RealWorld
s, () #) }

data FastMutPtr = FastMutPtr (MutableByteArray# RealWorld)

newFastMutPtr :: IO FastMutPtr
newFastMutPtr = (State# RealWorld -> (# State# RealWorld, FastMutPtr #))
-> IO FastMutPtr
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, FastMutPtr #))
 -> IO FastMutPtr)
-> (State# RealWorld -> (# State# RealWorld, FastMutPtr #))
-> IO FastMutPtr
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
  case Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
size State# RealWorld
s of { (# s :: State# RealWorld
s, arr :: MutableByteArray# RealWorld
arr #) ->
  (# State# RealWorld
s, MutableByteArray# RealWorld -> FastMutPtr
FastMutPtr MutableByteArray# RealWorld
arr #) }
  -- GHC assumes 'sizeof (Int) == sizeof (Ptr a)'
  where !(I# size :: Int#
size) = Int -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (0 :: Int)

readFastMutPtr :: FastMutPtr -> IO (Ptr a)
readFastMutPtr (FastMutPtr arr :: MutableByteArray# RealWorld
arr) = (State# RealWorld -> (# State# RealWorld, Ptr a #)) -> IO (Ptr a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Ptr a #)) -> IO (Ptr a))
-> (State# RealWorld -> (# State# RealWorld, Ptr a #))
-> IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
  case MutableByteArray# RealWorld
-> Int# -> State# RealWorld -> (# State# RealWorld, Addr# #)
forall d.
MutableByteArray# d -> Int# -> State# d -> (# State# d, Addr# #)
readAddrArray# MutableByteArray# RealWorld
arr 0# State# RealWorld
s of { (# s :: State# RealWorld
s, i :: Addr#
i #) ->
  (# State# RealWorld
s, Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
i #) }

writeFastMutPtr :: FastMutPtr -> Ptr a -> IO ()
writeFastMutPtr (FastMutPtr arr :: MutableByteArray# RealWorld
arr) (Ptr i :: Addr#
i) = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s ->
  case MutableByteArray# RealWorld
-> Int# -> Addr# -> State# RealWorld -> State# RealWorld
forall d.
MutableByteArray# d -> Int# -> Addr# -> State# d -> State# d
writeAddrArray# MutableByteArray# RealWorld
arr 0# Addr#
i State# RealWorld
s of { s :: State# RealWorld
s ->
  (# State# RealWorld
s, () #) }