Skip to content

Commit

Permalink
Linear Clash.Sized.Vector.reverse (#2871)
Browse files Browse the repository at this point in the history
Improve the simulation runtime of `Clash.Sized.Vector.reverse` from quadratic to linear.
  • Loading branch information
kleinreact authored Jan 7, 2025
1 parent 29d7c10 commit e07ea48
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions clash-prelude/src/Clash/Sized/Vector.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-|
Copyright : (C) 2013-2016, University of Twente,
2017 , Myrtle Software Ltd
2022-2024, QBayLogic B.V.
2022-2025, QBayLogic B.V.
2024, Alex Mason
License : BSD2 (see the file LICENSE)
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
Expand Down Expand Up @@ -811,8 +811,11 @@ merge x y = concat (transpose (x :> singleton y))
-- >>> reverse (1:>2:>3:>4:>Nil)
-- 4 :> 3 :> 2 :> 1 :> Nil
reverse :: Vec n a -> Vec n a
reverse Nil = Nil
reverse (x `Cons` xs) = reverse xs :< x
reverse xs = go Nil xs
where
go :: i <= n => Vec (n - i) a -> Vec i a -> Vec n a
go a (y `Cons` ys) = go (y `Cons` a) ys
go a Nil = a
-- See: https://github.com/clash-lang/clash-compiler/pull/2511
{-# CLASH_OPAQUE reverse #-}
{-# ANN reverse hasBlackBox #-}
Expand Down

0 comments on commit e07ea48

Please sign in to comment.