Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Definitions of primitive types, the values that inhabit these types, and operations on these values. A primitive value can also be called a scalar.
Essentially, this module describes the subset of the (internal) Futhark language that operates on primitive types.
Synopsis
- data IntType
- allIntTypes :: [IntType]
- data FloatType
- allFloatTypes :: [FloatType]
- data PrimType
- allPrimTypes :: [PrimType]
- data Int8
- data Int16
- data Int32
- data Int64
- data IntValue
- = Int8Value !Int8
- | Int16Value !Int16
- | Int32Value !Int32
- | Int64Value !Int64
- intValue :: Integral int => IntType -> int -> IntValue
- intValueType :: IntValue -> IntType
- valueIntegral :: Integral int => IntValue -> int
- data FloatValue
- floatValue :: Real num => FloatType -> num -> FloatValue
- floatValueType :: FloatValue -> FloatType
- data PrimValue
- = IntValue !IntValue
- | FloatValue !FloatValue
- | BoolValue !Bool
- | Checked
- primValueType :: PrimValue -> PrimType
- blankPrimValue :: PrimType -> PrimValue
- data Overflow
- data Safety
- data UnOp
- allUnOps :: [UnOp]
- data BinOp
- = Add IntType Overflow
- | FAdd FloatType
- | Sub IntType Overflow
- | FSub FloatType
- | Mul IntType Overflow
- | FMul FloatType
- | UDiv IntType Safety
- | UDivUp IntType Safety
- | SDiv IntType Safety
- | SDivUp IntType Safety
- | FDiv FloatType
- | FMod FloatType
- | UMod IntType Safety
- | SMod IntType Safety
- | SQuot IntType Safety
- | SRem IntType Safety
- | SMin IntType
- | UMin IntType
- | FMin FloatType
- | SMax IntType
- | UMax IntType
- | FMax FloatType
- | Shl IntType
- | LShr IntType
- | AShr IntType
- | And IntType
- | Or IntType
- | Xor IntType
- | Pow IntType
- | FPow FloatType
- | LogAnd
- | LogOr
- allBinOps :: [BinOp]
- data ConvOp
- allConvOps :: [ConvOp]
- data CmpOp
- allCmpOps :: [CmpOp]
- doUnOp :: UnOp -> PrimValue -> Maybe PrimValue
- doComplement :: IntValue -> IntValue
- doAbs :: IntValue -> IntValue
- doFAbs :: FloatValue -> FloatValue
- doSSignum :: IntValue -> IntValue
- doUSignum :: IntValue -> IntValue
- doBinOp :: BinOp -> PrimValue -> PrimValue -> Maybe PrimValue
- doAdd :: IntValue -> IntValue -> IntValue
- doMul :: IntValue -> IntValue -> IntValue
- doSDiv :: IntValue -> IntValue -> Maybe IntValue
- doSMod :: IntValue -> IntValue -> Maybe IntValue
- doPow :: IntValue -> IntValue -> Maybe IntValue
- doConvOp :: ConvOp -> PrimValue -> Maybe PrimValue
- doZExt :: IntValue -> IntType -> IntValue
- doSExt :: IntValue -> IntType -> IntValue
- doFPConv :: FloatValue -> FloatType -> FloatValue
- doFPToUI :: FloatValue -> IntType -> IntValue
- doFPToSI :: FloatValue -> IntType -> IntValue
- doUIToFP :: IntValue -> FloatType -> FloatValue
- doSIToFP :: IntValue -> FloatType -> FloatValue
- intToInt64 :: IntValue -> Int64
- intToWord64 :: IntValue -> Word64
- doCmpOp :: CmpOp -> PrimValue -> PrimValue -> Maybe Bool
- doCmpEq :: PrimValue -> PrimValue -> Bool
- doCmpUlt :: IntValue -> IntValue -> Bool
- doCmpUle :: IntValue -> IntValue -> Bool
- doCmpSlt :: IntValue -> IntValue -> Bool
- doCmpSle :: IntValue -> IntValue -> Bool
- doFCmpLt :: FloatValue -> FloatValue -> Bool
- doFCmpLe :: FloatValue -> FloatValue -> Bool
- binOpType :: BinOp -> PrimType
- unOpType :: UnOp -> PrimType
- cmpOpType :: CmpOp -> PrimType
- convOpType :: ConvOp -> (PrimType, PrimType)
- primFuns :: Map String ([PrimType], PrimType, [PrimValue] -> Maybe PrimValue)
- zeroIsh :: PrimValue -> Bool
- zeroIshInt :: IntValue -> Bool
- oneIsh :: PrimValue -> Bool
- oneIshInt :: IntValue -> Bool
- negativeIsh :: PrimValue -> Bool
- primBitSize :: PrimType -> Int
- primByteSize :: Num a => PrimType -> a
- intByteSize :: Num a => IntType -> a
- floatByteSize :: Num a => FloatType -> a
- commutativeBinOp :: BinOp -> Bool
- convOpFun :: ConvOp -> String
- prettySigned :: Bool -> PrimType -> String
Types
An integer type, ordered by size. Note that signedness is not a property of the type, but a property of the operations performed on values of these types.
Instances
Bounded IntType Source # | |
Enum IntType Source # | |
Eq IntType Source # | |
Ord IntType Source # | |
Show IntType Source # | |
Generic IntType Source # | |
Pretty IntType Source # | |
SexpIso IntType Source # | |
Defined in Futhark.IR.Primitive | |
type Rep IntType Source # | |
Defined in Futhark.IR.Primitive type Rep IntType = D1 ('MetaData "IntType" "Futhark.IR.Primitive" "futhark-0.18.2-inplace" 'False) ((C1 ('MetaCons "Int8" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Int16" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "Int32" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Int64" 'PrefixI 'False) (U1 :: Type -> Type))) |
allIntTypes :: [IntType] Source #
A list of all integer types.
A floating point type.
Instances
Bounded FloatType Source # | |
Enum FloatType Source # | |
Defined in Futhark.IR.Primitive succ :: FloatType -> FloatType # pred :: FloatType -> FloatType # fromEnum :: FloatType -> Int # enumFrom :: FloatType -> [FloatType] # enumFromThen :: FloatType -> FloatType -> [FloatType] # enumFromTo :: FloatType -> FloatType -> [FloatType] # enumFromThenTo :: FloatType -> FloatType -> FloatType -> [FloatType] # | |
Eq FloatType Source # | |
Ord FloatType Source # | |
Defined in Futhark.IR.Primitive | |
Show FloatType Source # | |
Generic FloatType Source # | |
Pretty FloatType Source # | |
SexpIso FloatType Source # | |
Defined in Futhark.IR.Primitive | |
type Rep FloatType Source # | |
allFloatTypes :: [FloatType] Source #
A list of all floating-point types.
Low-level primitive types.
Instances
Bounded PrimType Source # | |
Enum PrimType Source # | |
Eq PrimType Source # | |
Ord PrimType Source # | |
Defined in Futhark.IR.Primitive | |
Show PrimType Source # | |
Generic PrimType Source # | |
Pretty PrimType Source # | |
SexpIso PrimType Source # | |
Defined in Futhark.IR.Primitive | |
type Rep PrimType Source # | |
Defined in Futhark.IR.Primitive type Rep PrimType = D1 ('MetaData "PrimType" "Futhark.IR.Primitive" "futhark-0.18.2-inplace" 'False) ((C1 ('MetaCons "IntType" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 IntType)) :+: C1 ('MetaCons "FloatType" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FloatType))) :+: (C1 ('MetaCons "Bool" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Cert" 'PrefixI 'False) (U1 :: Type -> Type))) |
allPrimTypes :: [PrimType] Source #
A list of all primitive types.
8-bit signed integer type
Instances
16-bit signed integer type
Instances
32-bit signed integer type
Instances
64-bit signed integer type
Instances
Values
An integer value.
Instances
intValueType :: IntValue -> IntType Source #
The type of an integer value.
data FloatValue Source #
A floating-point value.
Instances
floatValue :: Real num => FloatType -> num -> FloatValue Source #
Create a FloatValue
from a type and a Rational
.
floatValueType :: FloatValue -> FloatType Source #
The type of a floating-point value.
Non-array values.
IntValue !IntValue | |
FloatValue !FloatValue | |
BoolValue !Bool | |
Checked | The only value of type |
Instances
Eq PrimValue Source # | |
Ord PrimValue Source # | |
Defined in Futhark.IR.Primitive | |
Show PrimValue Source # | |
Generic PrimValue Source # | |
ToExp PrimValue Source # | |
Pretty PrimValue Source # | |
SexpIso PrimValue Source # | |
Defined in Futhark.IR.Primitive | |
IsValue PrimValue Source # | |
type Rep PrimValue Source # | |
Defined in Futhark.IR.Primitive type Rep PrimValue = D1 ('MetaData "PrimValue" "Futhark.IR.Primitive" "futhark-0.18.2-inplace" 'False) ((C1 ('MetaCons "IntValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 IntValue)) :+: C1 ('MetaCons "FloatValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FloatValue))) :+: (C1 ('MetaCons "BoolValue" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)) :+: C1 ('MetaCons "Checked" 'PrefixI 'False) (U1 :: Type -> Type))) |
primValueType :: PrimValue -> PrimType Source #
The type of a basic value.
blankPrimValue :: PrimType -> PrimValue Source #
A "blank" value of the given primitive type - this is zero, or whatever is close to it. Don't depend on this value, but use it for e.g. creating arrays to be populated by do-loops.
Operations
What to do in case of arithmetic overflow. Futhark's semantics are that overflow does wraparound, but for generated code (like address arithmetic), it can be beneficial for overflow to be undefined behaviour, as it allows better optimisation of things such as GPU kernels.
Note that all values of this type are considered equal for Eq
and
Ord
.
Whether something is safe or unsafe (mostly function calls, and
in the context of whether operations are dynamically checked).
When we inline an Unsafe
function, we remove all safety checks in
its body. The Ord
instance picks Unsafe
as being less than
Safe
.
For operations like integer division, a safe division will not explode the computer in case of division by zero, but instead return some unspecified value. This always involves a run-time check, so generally the unsafe variant is what the compiler will insert, but guarded by an explicit assertion elsewhere. Safe operations are useful when the optimiser wants to move e.g. a division to a location where the divisor may be zero, but where the result will only be used when it is non-zero (so it doesn't matter what result is provided with a zero divisor, as long as the program keeps running).
Various unary operators. It is a bit ad-hoc what is a unary operator and what is a built-in function. Perhaps these should all go away eventually.
Not | E.g., |
Complement IntType | E.g., |
Abs IntType |
|
FAbs FloatType |
|
SSignum IntType | Signed sign function: |
USignum IntType | Unsigned sign function: |
Instances
Binary operators. These correspond closely to the binary operators in LLVM. Most are parametrised by their expected input and output types.
Add IntType Overflow | Integer addition. |
FAdd FloatType | Floating-point addition. |
Sub IntType Overflow | Integer subtraction. |
FSub FloatType | Floating-point subtraction. |
Mul IntType Overflow | Integer multiplication. |
FMul FloatType | Floating-point multiplication. |
UDiv IntType Safety | Unsigned integer division. Rounds towards negativity infinity. Note: this is different from LLVM. |
UDivUp IntType Safety | Unsigned integer division. Rounds towards positive infinity. |
SDiv IntType Safety | Signed integer division. Rounds towards negativity infinity. Note: this is different from LLVM. |
SDivUp IntType Safety | Signed integer division. Rounds towards positive infinity. |
FDiv FloatType | Floating-point division. |
FMod FloatType | Floating-point modulus. |
UMod IntType Safety | Unsigned integer modulus; the countepart to |
SMod IntType Safety | Signed integer modulus; the countepart to |
SQuot IntType Safety | Signed integer division. Rounds towards zero. This
corresponds to the |
SRem IntType Safety | Signed integer division. Rounds towards zero. This
corresponds to the |
SMin IntType | Returns the smallest of two signed integers. |
UMin IntType | Returns the smallest of two unsigned integers. |
FMin FloatType | Returns the smallest of two floating-point numbers. |
SMax IntType | Returns the greatest of two signed integers. |
UMax IntType | Returns the greatest of two unsigned integers. |
FMax FloatType | Returns the greatest of two floating-point numbers. |
Shl IntType | Left-shift. |
LShr IntType | Logical right-shift, zero-extended. |
AShr IntType | Arithmetic right-shift, sign-extended. |
And IntType | Bitwise and. |
Or IntType | Bitwise or. |
Xor IntType | Bitwise exclusive-or. |
Pow IntType | Integer exponentiation. |
FPow FloatType | Floating-point exponentiation. |
LogAnd | Boolean and - not short-circuiting. |
LogOr | Boolean or - not short-circuiting. |
Instances
Conversion operators try to generalise the from t0 x to t1
instructions from LLVM.
ZExt IntType IntType | Zero-extend the former integer type to the latter. If the new type is smaller, the result is a truncation. |
SExt IntType IntType | Sign-extend the former integer type to the latter. If the new type is smaller, the result is a truncation. |
FPConv FloatType FloatType | Convert value of the former floating-point type to the latter. If the new type is smaller, the result is a truncation. |
FPToUI FloatType IntType | Convert a floating-point value to the nearest unsigned integer (rounding towards zero). |
FPToSI FloatType IntType | Convert a floating-point value to the nearest signed integer (rounding towards zero). |
UIToFP IntType FloatType | Convert an unsigned integer to a floating-point value. |
SIToFP IntType FloatType | Convert a signed integer to a floating-point value. |
IToB IntType | Convert an integer to a boolean value. Zero becomes false; anything else is true. |
BToI IntType | Convert a boolean to an integer. True is converted to 1 and False to 0. |
Instances
allConvOps :: [ConvOp] Source #
A list of all conversion operators for all types.
Comparison operators are like BinOp
s, but they always return a
boolean value. The somewhat ugly constructor names are straight
out of LLVM.
CmpEq PrimType | All types equality. |
CmpUlt IntType | Unsigned less than. |
CmpUle IntType | Unsigned less than or equal. |
CmpSlt IntType | Signed less than. |
CmpSle IntType | Signed less than or equal. |
FCmpLt FloatType | Floating-point less than. |
FCmpLe FloatType | Floating-point less than or equal. |
CmpLlt | Boolean less than. |
CmpLle | Boolean less than or equal. |
Instances
Unary Operations
doComplement :: IntValue -> IntValue Source #
E.g., ~(~1) = 1
.
doFAbs :: FloatValue -> FloatValue Source #
abs(-2.0) = 2.0
.
Binary Operations
doSDiv :: IntValue -> IntValue -> Maybe IntValue Source #
Signed integer division. Rounds towards negativity infinity. Note: this is different from LLVM.
doSMod :: IntValue -> IntValue -> Maybe IntValue Source #
Signed integer modulus; the countepart to SDiv
.
Conversion Operations
doZExt :: IntValue -> IntType -> IntValue Source #
Zero-extend the given integer value to the size of the given type. If the type is smaller than the given value, the result is a truncation.
doSExt :: IntValue -> IntType -> IntValue Source #
Sign-extend the given integer value to the size of the given type. If the type is smaller than the given value, the result is a truncation.
doFPConv :: FloatValue -> FloatType -> FloatValue Source #
Convert the former floating-point type to the latter.
doFPToUI :: FloatValue -> IntType -> IntValue Source #
Convert a floating-point value to the nearest unsigned integer (rounding towards zero).
doFPToSI :: FloatValue -> IntType -> IntValue Source #
Convert a floating-point value to the nearest signed integer (rounding towards zero).
doUIToFP :: IntValue -> FloatType -> FloatValue Source #
Convert an unsigned integer to a floating-point value.
doSIToFP :: IntValue -> FloatType -> FloatValue Source #
Convert a signed integer to a floating-point value.
intToWord64 :: IntValue -> Word64 Source #
Comparison Operations
doCmpEq :: PrimValue -> PrimValue -> Bool Source #
Compare any two primtive values for exact equality.
doFCmpLt :: FloatValue -> FloatValue -> Bool Source #
Floating-point less than.
doFCmpLe :: FloatValue -> FloatValue -> Bool Source #
Floating-point less than or equal.
Type Of
convOpType :: ConvOp -> (PrimType, PrimType) Source #
The input and output types of a conversion operator.
Primitive functions
primFuns :: Map String ([PrimType], PrimType, [PrimValue] -> Maybe PrimValue) Source #
A mapping from names of primitive functions to their parameter types, their result type, and a function for evaluating them.
Utility
zeroIshInt :: IntValue -> Bool Source #
Is the given integer value kind of zero?
negativeIsh :: PrimValue -> Bool Source #
Is the given value kind of negative?
primBitSize :: PrimType -> Int Source #
The size of a value of a given primitive type in bites.
primByteSize :: Num a => PrimType -> a Source #
The size of a value of a given primitive type in eight-bit bytes.
intByteSize :: Num a => IntType -> a Source #
The size of a value of a given integer type in eight-bit bytes.
floatByteSize :: Num a => FloatType -> a Source #
The size of a value of a given floating-point type in eight-bit bytes.
commutativeBinOp :: BinOp -> Bool Source #
True if the given binary operator is commutative.