{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE PatternSynonyms #-}
module Crypto.System.CPU (
ProcessorOption (
AESNI,
PCLMUL,
SSSE3,
AVX,
AVX2,
SHANI,
MOVBE,
ADX,
VAES,
VAES512,
AVX512IFMA,
NEON,
ARMAES,
ARMPMULL,
ARMSHA1,
ARMSHA2,
ARMSHA512,
PPCAES,
PPCVPMSUM
),
processorOptions,
hasAESAcceleration,
hasGHASHAcceleration,
) where
import Control.Monad (filterM)
import Data.List (sort)
import Data.Word (Word16)
import Foreign.C.Types (CInt (..), CUInt (..))
import Crypto.Internal.Compat
newtype ProcessorOption = ProcessorOption Word16
deriving (ProcessorOption -> ProcessorOption -> Bool
(ProcessorOption -> ProcessorOption -> Bool)
-> (ProcessorOption -> ProcessorOption -> Bool)
-> Eq ProcessorOption
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProcessorOption -> ProcessorOption -> Bool
== :: ProcessorOption -> ProcessorOption -> Bool
$c/= :: ProcessorOption -> ProcessorOption -> Bool
/= :: ProcessorOption -> ProcessorOption -> Bool
Eq, Eq ProcessorOption
Eq ProcessorOption =>
(ProcessorOption -> ProcessorOption -> Ordering)
-> (ProcessorOption -> ProcessorOption -> Bool)
-> (ProcessorOption -> ProcessorOption -> Bool)
-> (ProcessorOption -> ProcessorOption -> Bool)
-> (ProcessorOption -> ProcessorOption -> Bool)
-> (ProcessorOption -> ProcessorOption -> ProcessorOption)
-> (ProcessorOption -> ProcessorOption -> ProcessorOption)
-> Ord ProcessorOption
ProcessorOption -> ProcessorOption -> Bool
ProcessorOption -> ProcessorOption -> Ordering
ProcessorOption -> ProcessorOption -> ProcessorOption
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ProcessorOption -> ProcessorOption -> Ordering
compare :: ProcessorOption -> ProcessorOption -> Ordering
$c< :: ProcessorOption -> ProcessorOption -> Bool
< :: ProcessorOption -> ProcessorOption -> Bool
$c<= :: ProcessorOption -> ProcessorOption -> Bool
<= :: ProcessorOption -> ProcessorOption -> Bool
$c> :: ProcessorOption -> ProcessorOption -> Bool
> :: ProcessorOption -> ProcessorOption -> Bool
$c>= :: ProcessorOption -> ProcessorOption -> Bool
>= :: ProcessorOption -> ProcessorOption -> Bool
$cmax :: ProcessorOption -> ProcessorOption -> ProcessorOption
max :: ProcessorOption -> ProcessorOption -> ProcessorOption
$cmin :: ProcessorOption -> ProcessorOption -> ProcessorOption
min :: ProcessorOption -> ProcessorOption -> ProcessorOption
Ord)
pattern AESNI :: ProcessorOption
pattern $mAESNI :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bAESNI :: ProcessorOption
AESNI = ProcessorOption 0
pattern PCLMUL :: ProcessorOption
pattern $mPCLMUL :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bPCLMUL :: ProcessorOption
PCLMUL = ProcessorOption 1
pattern SSSE3 :: ProcessorOption
pattern $mSSSE3 :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bSSSE3 :: ProcessorOption
SSSE3 = ProcessorOption 3
pattern AVX :: ProcessorOption
pattern $mAVX :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bAVX :: ProcessorOption
AVX = ProcessorOption 4
pattern AVX2 :: ProcessorOption
pattern $mAVX2 :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bAVX2 :: ProcessorOption
AVX2 = ProcessorOption 5
pattern SHANI :: ProcessorOption
pattern $mSHANI :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bSHANI :: ProcessorOption
SHANI = ProcessorOption 6
pattern MOVBE :: ProcessorOption
pattern $mMOVBE :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bMOVBE :: ProcessorOption
MOVBE = ProcessorOption 7
pattern ADX :: ProcessorOption
pattern $mADX :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bADX :: ProcessorOption
ADX = ProcessorOption 8
pattern VAES :: ProcessorOption
pattern $mVAES :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bVAES :: ProcessorOption
VAES = ProcessorOption 9
pattern VAES512 :: ProcessorOption
pattern $mVAES512 :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bVAES512 :: ProcessorOption
VAES512 = ProcessorOption 10
pattern AVX512IFMA :: ProcessorOption
pattern $mAVX512IFMA :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bAVX512IFMA :: ProcessorOption
AVX512IFMA = ProcessorOption 19
pattern NEON :: ProcessorOption
pattern $mNEON :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bNEON :: ProcessorOption
NEON = ProcessorOption 11
pattern ARMAES :: ProcessorOption
pattern $mARMAES :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bARMAES :: ProcessorOption
ARMAES = ProcessorOption 12
pattern ARMPMULL :: ProcessorOption
pattern $mARMPMULL :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bARMPMULL :: ProcessorOption
ARMPMULL = ProcessorOption 13
pattern ARMSHA1 :: ProcessorOption
pattern $mARMSHA1 :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bARMSHA1 :: ProcessorOption
ARMSHA1 = ProcessorOption 14
pattern ARMSHA2 :: ProcessorOption
pattern $mARMSHA2 :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bARMSHA2 :: ProcessorOption
ARMSHA2 = ProcessorOption 15
pattern ARMSHA512 :: ProcessorOption
pattern $mARMSHA512 :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bARMSHA512 :: ProcessorOption
ARMSHA512 = ProcessorOption 16
pattern PPCAES :: ProcessorOption
pattern $mPPCAES :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bPPCAES :: ProcessorOption
PPCAES = ProcessorOption 17
pattern PPCVPMSUM :: ProcessorOption
pattern $mPPCVPMSUM :: forall {r}. ProcessorOption -> ((# #) -> r) -> ((# #) -> r) -> r
$bPPCVPMSUM :: ProcessorOption
PPCVPMSUM = ProcessorOption 18
instance Show ProcessorOption where
show :: ProcessorOption -> String
show ProcessorOption
AESNI = String
"AESNI"
show ProcessorOption
PCLMUL = String
"PCLMUL"
show ProcessorOption
SSSE3 = String
"SSSE3"
show ProcessorOption
AVX = String
"AVX"
show ProcessorOption
AVX2 = String
"AVX2"
show ProcessorOption
SHANI = String
"SHANI"
show ProcessorOption
MOVBE = String
"MOVBE"
show ProcessorOption
ADX = String
"ADX"
show ProcessorOption
VAES = String
"VAES"
show ProcessorOption
VAES512 = String
"VAES512"
show ProcessorOption
AVX512IFMA = String
"AVX512IFMA"
show ProcessorOption
NEON = String
"NEON"
show ProcessorOption
ARMAES = String
"ARMAES"
show ProcessorOption
ARMPMULL = String
"ARMPMULL"
show ProcessorOption
ARMSHA1 = String
"ARMSHA1"
show ProcessorOption
ARMSHA2 = String
"ARMSHA2"
show ProcessorOption
ARMSHA512 = String
"ARMSHA512"
show ProcessorOption
PPCAES = String
"PPCAES"
show ProcessorOption
PPCVPMSUM = String
"PPCVPMSUM"
show (ProcessorOption Word16
n) = String
"ProcessorOption " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word16 -> String
forall a. Show a => a -> String
show Word16
n
processorOptions :: [ProcessorOption]
processorOptions :: [ProcessorOption]
processorOptions = IO [ProcessorOption] -> [ProcessorOption]
forall a. IO a -> a
unsafeDoIO ([ProcessorOption] -> [ProcessorOption]
forall a. Ord a => [a] -> [a]
sort ([ProcessorOption] -> [ProcessorOption])
-> IO [ProcessorOption] -> IO [ProcessorOption]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ProcessorOption -> IO Bool)
-> [ProcessorOption] -> IO [ProcessorOption]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM ProcessorOption -> IO Bool
askC [ProcessorOption]
allOptions)
where
allOptions :: [ProcessorOption]
allOptions = [Word16 -> ProcessorOption
ProcessorOption Word16
n | Word16
n <- [Word16
0 .. Word16
19], Word16
n Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word16
2]
askC :: ProcessorOption -> IO Bool
askC (ProcessorOption Word16
n) =
(CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CUInt -> IO CInt
crypton_cpu_option (Word16 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
n)
{-# NOINLINE processorOptions #-}
hasAESAcceleration :: Bool
hasAESAcceleration :: Bool
hasAESAcceleration =
(ProcessorOption -> Bool) -> [ProcessorOption] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any
(ProcessorOption -> [ProcessorOption] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ProcessorOption]
processorOptions)
[ProcessorOption
AESNI, ProcessorOption
ARMAES, ProcessorOption
PPCAES]
hasGHASHAcceleration :: Bool
hasGHASHAcceleration :: Bool
hasGHASHAcceleration =
(ProcessorOption -> Bool) -> [ProcessorOption] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any
(ProcessorOption -> [ProcessorOption] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ProcessorOption]
processorOptions)
[ProcessorOption
PCLMUL, ProcessorOption
ARMPMULL, ProcessorOption
PPCVPMSUM]
foreign import ccall unsafe "crypton_cpu_option"
crypton_cpu_option :: CUInt -> IO CInt