{-# language MagicHash #-}
{-# language UnboxedTuples #-}

module Data.Primitive.Class.Atomic
  ( PrimMach(..)
  ) where

import Data.Primitive (Prim)
import GHC.Exts

-- | Class of types supporting primitive operations that are isomorphic to
-- a machine integer. Such types support compare-and-swap and other atomic
-- operations.
class Prim a => PrimMach a where
  primMachToInt# :: a -> Int#
  primMachFromInt# :: Int# -> a

instance PrimMach Int where
  primMachToInt# :: Int -> Int#
primMachToInt# (I# Int#
i) = Int#
i
  primMachFromInt# :: Int# -> Int
primMachFromInt# = Int# -> Int
I#

instance PrimMach Word where
  primMachToInt# :: Word -> Int#
primMachToInt# (W# Word#
i) = Word# -> Int#
word2Int# Word#
i
  primMachFromInt# :: Int# -> Word
primMachFromInt# Int#
i = Word# -> Word
W# (Int# -> Word#
int2Word# Int#
i)