{-# OPTIONS_HADDOCK hide #-}
-- |
-- Module      : Data.Array.Accelerate.LLVM.Extra
-- Copyright   : [2014..2020] The Accelerate Team
-- License     : BSD3
--
-- Maintainer  : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--

module Data.Array.Accelerate.LLVM.Extra
  where

import Data.Array.Accelerate.Error

import Data.Word
import qualified Data.Bits as B


-- | The number of bits in a type
--
{-# INLINE bitSize #-}
bitSize :: HasCallStack => B.Bits a => a -> Word32
bitSize :: a -> Word32
bitSize a
x
  | Just Int
s <- a -> Maybe Int
forall a. Bits a => a -> Maybe Int
B.bitSizeMaybe a
x  = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
s
  | Bool
otherwise                   = String -> Word32
forall a. HasCallStack => String -> a
internalError String
"could not determine bit size of type"


-- | Convert a boolean value into an integral value, where False is zero and
-- True is one.
--
{-# INLINE fromBool #-}
fromBool :: Integral i => Bool -> i
fromBool :: Bool -> i
fromBool Bool
True  = i
1
fromBool Bool
False = i
0

-- | Convert an integral value into a boolean. We follow the C convention, where
-- zero is False and all other values represent True.
--
{-# INLINE toBool #-}
toBool :: Integral i => i -> Bool
toBool :: i -> Bool
toBool i
0 = Bool
False
toBool i
_ = Bool
True