I’m happy to announce the release of cpu-features 0.1.0.0.
This is a library to detect the features provided by the CPU running the Haskell program. It supports multiple architectures (x86/AArch64/RISC-V) and has no external dependency.
It has three interfaces: One that returns Bool
:
$ cabal repl --build-depends cpu-features
ghci> :m + System.CPUFeatures.X86
ghci> bAVX2
True
ghci> bFMA
True
ghci> bAVX10_2
False
One that uses type-level booleans:
ghci> :m + System.CPUFeatures.X86.TypeBool
ghci> :k AVX2 -- type-level boolean
AVX2 :: Bool
ghci> :t sAVX2 -- SBool is the singleton type for Bool
sAVX2 :: SBool AVX2
ghci> :set -XMonoLocalBinds
ghci> (case sAVX2 of STrue -> "Yes"; SFalse -> "No") :: String
"Yes"
And finally, one that uses nullary constraints:
ghci> :m + System.CPUFeatures.X86.Constraint
ghci> :k HasAVX2 -- nullary constraint
HasAVX2 :: Constraint
ghci> :t queryAVX2
queryAVX2 :: Maybe (Dict HasAVX2)
ghci> case queryAVX2 of Just Dict -> "Yes"; Nothing -> "No"
"Yes"
With the type-level interface, it becomes possible to annotate your function that it requires a specific CPU feature:
someSpecializedImplementationForAVX2 :: HasAVX2 => SomeResult
someFallbackImplementation :: SomeResult
someUserCode = case queryAVX2 of
Just Dict -> someSpecializedImplementationForAVX2
Nothing -> someFallbackImplementation
Please report any bugs or feature requests on the GitHub repo. Thank you for reading!