Skip to content

Commit

Permalink
Simplifying refactoring
Browse files Browse the repository at this point in the history
* split step and step_zero computations.
* no need to compute sign.
* inline some definitions which were used only once.
  • Loading branch information
sortraev committed Nov 25, 2024
1 parent b031497 commit 6423027
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/Futhark/Internalise/Exps.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,26 +221,22 @@ internaliseAppExp desc _ (E.Range start maybe_second inclusiveness loc) = do
E.Scalar (E.Prim (E.Unsigned it)) -> pure (it, CmpUlt it)
start_t -> error $ "Start value in range has type " ++ prettyString start_t

let one = intConst it 1
negone = intConst it (-1)
default_step = case inclusiveness 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'
pure (subtracted_step, step_zero)
Nothing ->
pure (default_step, constant False)
let zero = intConst it 0

step_sign <- letSubExp "s_sign" $ BasicOp $ I.UnOp (I.SSignum it) step
step_sign_i64 <- asIntS Int64 step_sign
step <- case maybe_second' of
Just second' ->
letSubExp "subtracted_step" $
I.BasicOp $
I.BinOp (I.Sub it I.OverflowWrap) second' start'
Nothing -> pure $ intConst it $ -- use default step of 1 or -1.
case inclusiveness of
DownToExclusive {} -> -1
_ -> 1

step_zero <-
letSubExp "step_zero" $
I.BasicOp $
I.CmpOp (I.CmpEq $ IntType it) step zero

distance <- do
difference <-
Expand All @@ -266,8 +262,7 @@ internaliseAppExp desc _ (E.Range start maybe_second inclusiveness loc) = do
downwards <-
letSubExp "downwards" $
I.BasicOp $
I.CmpOp (I.CmpEq $ IntType it) step_sign negone

I.CmpOp (CmpSlt it) step zero
step_wrong_dir <- case inclusiveness of
DownToExclusive {} -> letSubExp "upwards" $ I.BasicOp $ I.UnOp (I.Neg I.Bool) downwards
UpToExclusive {} -> pure downwards
Expand Down Expand Up @@ -307,7 +302,7 @@ internaliseAppExp desc _ (E.Range start maybe_second inclusiveness loc) = do
pos_step <-
letSubExp "pos_step" $
I.BasicOp $
I.BinOp (Mul Int64 I.OverflowWrap) step_i64 step_sign_i64
I.UnOp (Abs Int64) step_i64

num_elems <-
certifying cs $
Expand Down

0 comments on commit 6423027

Please sign in to comment.