text
stringlengths
0
1.05M
start' <- internaliseExp1 "range_start" start
end' <- internaliseExp1 "range_end" $ case end of
DownToExclusive e -> e
ToInclusive e -> e
UpToExclusive e -> e
maybe_second' <-
traverse (internaliseExp1 "range_second") maybe_second
-- Construct an error message in case the range is invalid.
let conv = case E.typeOf start of
E.Scalar (E.Prim (E.Unsigned _)) -> asIntS Int32
_ -> asIntS Int32
start'_i32 <- conv start'
end'_i32 <- conv end'
maybe_second'_i32 <- traverse conv maybe_second'
let errmsg =
errorMsg $
["Range "]
++ [ErrorInt32 start'_i32]
++ ( case maybe_second'_i32 of
Nothing -> []
Just second_i32 -> ["..", ErrorInt32 second_i32]
)
++ ( case end of
DownToExclusive {} -> ["..>"]
ToInclusive {} -> ["..."]
UpToExclusive {} -> ["..<"]
)
++ [ErrorInt32 end'_i32, " is invalid."]
(it, le_op, lt_op) <-
case E.typeOf start of
E.Scalar (E.Prim (E.Signed it)) -> return (it, CmpSle it, CmpSlt it)
E.Scalar (E.Prim (E.Unsigned it)) -> return (it, CmpUle it, CmpUlt it)
start_t -> error $ "Start value in range has type " ++ pretty start_t
let one = intConst it 1
negone = intConst it (-1)
default_step = case end of
DownToExclusive {} -> negone
ToInclusive {} -> one
UpToExclusive {} -> one
(step, step_zero) <- case maybe_second' of
Just second' -> do
subtracted_step <-
letSubExp "subtracted_step" $
I.BasicOp $ I.BinOp (I.Sub it I.OverflowWrap) second' start'
step_zero <- letSubExp "step_zero" $ I.BasicOp $ I.CmpOp (I.CmpEq $ IntType it) start' second'
return (subtracted_step, step_zero)
Nothing ->
return (default_step, constant False)
step_sign <- letSubExp "s_sign" $ BasicOp $ I.UnOp (I.SSignum it) step
step_sign_i32 <- asIntS Int32 step_sign
bounds_invalid_downwards <-
letSubExp "bounds_invalid_downwards" $
I.BasicOp $ I.CmpOp le_op start' end'
bounds_invalid_upwards <-
letSubExp "bounds_invalid_upwards" $
I.BasicOp $ I.CmpOp lt_op end' start'
(distance, step_wrong_dir, bounds_invalid) <- case end of
DownToExclusive {} -> do
step_wrong_dir <-
letSubExp "step_wrong_dir" $
I.BasicOp $ I.CmpOp (I.CmpEq $ IntType it) step_sign one
distance <-
letSubExp "distance" $
I.BasicOp $ I.BinOp (Sub it I.OverflowWrap) start' end'
distance_i32 <- asIntS Int32 distance
return (distance_i32, step_wrong_dir, bounds_invalid_downwards)
UpToExclusive {} -> do
step_wrong_dir <-
letSubExp "step_wrong_dir" $
I.BasicOp $ I.CmpOp (I.CmpEq $ IntType it) step_sign negone
distance <- letSubExp "distance" $ I.BasicOp $ I.BinOp (Sub it I.OverflowWrap) end' start'
distance_i32 <- asIntS Int32 distance
return (distance_i32, step_wrong_dir, bounds_invalid_upwards)
ToInclusive {} -> do
downwards <-
letSubExp "downwards" $
I.BasicOp $ I.CmpOp (I.CmpEq $ IntType it) step_sign negone
distance_downwards_exclusive <-
letSubExp "distance_downwards_exclusive" $
I.BasicOp $ I.BinOp (Sub it I.OverflowWrap) start' end'
distance_upwards_exclusive <-
letSubExp "distance_upwards_exclusive" $
I.BasicOp $ I.BinOp (Sub it I.OverflowWrap) end' start'
bounds_invalid <-
letSubExp "bounds_invalid" $
I.If
downwards
(resultBody [bounds_invalid_downwards])
(resultBody [bounds_invalid_upwards])
$ ifCommon [I.Prim I.Bool]
distance_exclusive <-
letSubExp "distance_exclusive" $