2017-07-20 10 views
0

私はブールのVecを持っているとしましょう。私は元のVecでこのインデックスまで見た真の値の数に等しい値で同じサイズの新しいVecを塗りたい。私はそれを組み合わせてしたい。チゼル:アンロールループでインクリメントされた変数をモデル化する方法

私のHLSの背景で、私の頭に定住コーディングスタイル、私はこのような何かを書きたい:

def foo (in : Vec[UInt]) = { 

    val out = Vec.fill(in.size) {UInt(in.size)} 
    val nextInd = Wire(init = 0.U) 

    in.zipWithIndex.foreach {case(val, ind) => 
     when(val === true.B) { 
     out(ind) := nextInd 
     nextInd := Wire(init = nextInd+1.U) 
     } 
    } 
    } 

しかし、私は、これは、組み合わせループを作成して理解し、私は良い方法を見つけることができませんそれをモデル化する。どういうわけか私はループの新しい変数の反復を生成し、それを反復の間に渡す必要があります。

答えて

0

私はチゼルでそれを行う方法を考え出したと思います。私は次のfoldLeftを使用し、一回の反復から新しい変数を渡すことができます。

def foo (in : Vec[UInt]) = { 
    val vecSize = in.size 
    val out = Vec.fill(vecSize) {UInt(vecSize)} 

    in.zipWithIndex.foldLeft (Wire(init = 0.U)) {case(prevInd,(val, ind)) => 
     // val nextInd = Wire(init = prevInd) // this will not work due to bitwidth be not enough to hold an incremented value, so i do the following instead 
     val nextInd = Wire(init = UInt(vecSize)) // here i just make sure the width is enough 
     nextInd := prevInd 
     when(val === true.B) { 
     out(ind) := prevInd 
     nextInd := Wire(init = prevInd+1.U) 
     } 
     nextInd 
    } 
    } 
1

次は少し単純に思える:

// return is a Vec with each element equal to the sum of bits up to it's index 
    def foo(inBits: Vec[Bool]): Vec[UInt] = { 
    val counts = Vec(size, UInt(log2Ceil(size + 1).W)) 
    inBits.zipWithIndex.foldLeft(0.U) { case (lastValue, (bit, index)) => 
     counts(index) := bit + lastValue 
     counts(index) 
    } 
    counts 
    } 
関連する問題