From e07ea489e445cdc99265ad5ddc14c2d26deefbe6 Mon Sep 17 00:00:00 2001 From: Felix Klein Date: Tue, 7 Jan 2025 08:51:28 +0100 Subject: [PATCH] Linear Clash.Sized.Vector.reverse (#2871) Improve the simulation runtime of `Clash.Sized.Vector.reverse` from quadratic to linear. --- clash-prelude/src/Clash/Sized/Vector.hs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clash-prelude/src/Clash/Sized/Vector.hs b/clash-prelude/src/Clash/Sized/Vector.hs index 815b9d7d44..9683b96edd 100644 --- a/clash-prelude/src/Clash/Sized/Vector.hs +++ b/clash-prelude/src/Clash/Sized/Vector.hs @@ -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. @@ -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 #-}