[project @ 2002-04-01 12:23:20 by panne]
[ghc-hetmet.git] / ghc / compiler / utils / FastMutInt.lhs
1 {-# OPTIONS -cpp #-}
2 --
3 -- (c) The University of Glasgow 2002
4 --
5 -- Unboxed mutable Ints
6
7 \begin{code}
8 module FastMutInt(
9         FastMutInt, newFastMutInt,
10         readFastMutInt, writeFastMutInt,
11         incFastMutInt, incFastMutIntBy
12   ) where
13
14 #include "MachDeps.h"
15
16 #ifndef SIZEOF_HSINT
17 #define SIZEOF_HSINT  INT_SIZE_IN_BYTES
18 #endif
19
20
21 #if __GLASGOW_HASKELL__ < 503
22 import GlaExts
23 import PrelIOBase
24 #else
25 import GHC.Base
26 import GHC.IOBase
27 #endif
28 \end{code}
29
30 \begin{code}
31 #ifdef __GLASGOW_HASKELL__
32 data FastMutInt = FastMutInt (MutableByteArray# RealWorld)
33
34 newFastMutInt :: IO FastMutInt
35 newFastMutInt = IO $ \s ->
36   case newByteArray# size s of { (# s, arr #) ->
37   (# s, FastMutInt arr #) }
38   where I# size = SIZEOF_HSINT
39
40 readFastMutInt :: FastMutInt -> IO Int
41 readFastMutInt (FastMutInt arr) = IO $ \s ->
42   case readIntArray# arr 0# s of { (# s, i #) ->
43   (# s, I# i #) }
44
45 writeFastMutInt :: FastMutInt -> Int -> IO ()
46 writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s ->
47   case writeIntArray# arr 0# i s of { s ->
48   (# s, () #) }
49
50 incFastMutInt :: FastMutInt -> IO Int   -- Returns original value
51 incFastMutInt (FastMutInt arr) = IO $ \s ->
52   case readIntArray# arr 0# s of { (# s, i #) ->
53   case writeIntArray# arr 0# (i +# 1#) s of { s ->
54   (# s, I# i #) } }
55
56 incFastMutIntBy :: FastMutInt -> Int -> IO Int  -- Returns original value
57 incFastMutIntBy (FastMutInt arr) (I# n) = IO $ \s ->
58   case readIntArray# arr 0# s of { (# s, i #) ->
59   case writeIntArray# arr 0# (i +# n) s of { s ->
60   (# s, I# i #) } }
61 \end{code}
62 #endif
63