[project @ 2002-04-01 08:17:57 by simonpj]
[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 Data.Array
26 #endif
27 \end{code}
28
29 \begin{code}
30 #ifdef __GLASGOW_HASKELL__
31 data FastMutInt = FastMutInt (MutableByteArray# RealWorld)
32
33 newFastMutInt :: IO FastMutInt
34 newFastMutInt = IO $ \s ->
35   case newByteArray# size s of { (# s, arr #) ->
36   (# s, FastMutInt arr #) }
37   where I# size = SIZEOF_HSINT
38
39 readFastMutInt :: FastMutInt -> IO Int
40 readFastMutInt (FastMutInt arr) = IO $ \s ->
41   case readIntArray# arr 0# s of { (# s, i #) ->
42   (# s, I# i #) }
43
44 writeFastMutInt :: FastMutInt -> Int -> IO ()
45 writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s ->
46   case writeIntArray# arr 0# i s of { s ->
47   (# s, () #) }
48
49 incFastMutInt :: FastMutInt -> IO Int   -- Returns original value
50 incFastMutInt (FastMutInt arr) = IO $ \s ->
51   case readIntArray# arr 0# s of { (# s, i #) ->
52   case writeIntArray# arr 0# (i +# 1#) s of { s ->
53   (# s, I# i #) } }
54
55 incFastMutIntBy :: FastMutInt -> Int -> IO Int  -- Returns original value
56 incFastMutIntBy (FastMutInt arr) (I# n) = IO $ \s ->
57   case readIntArray# arr 0# s of { (# s, i #) ->
58   case writeIntArray# arr 0# (i +# n) s of { s ->
59   (# s, I# i #) } }
60 \end{code}
61 #endif
62