From 965b14f3dabf83c4ea8cd01990db8c09a19688fd Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Thu, 23 Apr 2015 14:18:42 -0700 Subject: [PATCH 1/6] Make Complex have Num trait, introduce conjugate. --- src/main/scala/Complex.scala | 43 +++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/scala/Complex.scala b/src/main/scala/Complex.scala index df7c99d7..931d6691 100644 --- a/src/main/scala/Complex.scala +++ b/src/main/scala/Complex.scala @@ -36,7 +36,14 @@ object Complex { def apply[T<:Data with Num[T]](real: T, imag: T) = new Complex(real, imag) } -class Complex[T<:Data with Num[T]](val real: T, val imag: T) extends Bundle { +object conjugate { + def apply[T<: Data with Num[T]](x : T) : T = x match { + case x : Complex[_] => x.conj.asInstanceOf[T] + case _ => x + } +} + +class Complex[T<:Data with Num[T]](val real: T, val imag: T) extends Bundle with Num[Complex[T]] { override def clone() = { new Complex(real.clone, imag.clone).asInstanceOf[this.type] } @@ -60,12 +67,42 @@ class Complex[T<:Data with Num[T]](val real: T, val imag: T) extends Bundle { } } - def / (r: Complex[T]): Complex[T] = ??? - + def conj : Complex[T] = + { + new Complex(real, -imag) + } + def / (r: Complex[T]): Complex[T] = + { + this * r.conj / r.abs2 + } def * (r: T): Complex[T] = { new Complex(real*r, imag*r) } + def % (r : Complex[T]): Complex[T] = + { + // this is bad, but what can we do? + new Complex(real % r.real, imag % r.imag) + } + def < (b : Complex[T]) : Bool = + { + this.abs2 < b.abs2 + } + def <= (b : Complex[T]) : Bool = + { + this.abs2 <= b.abs2 + } + def > (b : Complex[T]) : Bool = + { + this.abs2 > b.abs2 + } + def >= (b : Complex[T]) : Bool = + { + this.abs2 >= b.abs2 + } + def abs2 : T = { + real * real + imag * imag + } def / (r: T): Complex[T] = { new Complex(real/r, imag/r) From 5489437f55417b545e2409874091e292d72c082d Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Thu, 23 Apr 2015 14:19:15 -0700 Subject: [PATCH 2/6] Add a hook to calcElements in Bundle to let subclasses of Bundle filter out unwanted member, do type checking, etc. --- src/main/scala/Bundle.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/scala/Bundle.scala b/src/main/scala/Bundle.scala index 1b2d1506..d23c572e 100644 --- a/src/main/scala/Bundle.scala +++ b/src/main/scala/Bundle.scala @@ -79,7 +79,8 @@ class Bundle(val view: Seq[String] = null) extends Aggregate { if( types.length == 0 && !isStatic(modifiers) && isInterface && !(name contains '$') && !(Bundle.keywords contains name) - && (view == null || (view contains name)) ) { + && (view == null || (view contains name)) + && checkPort(m, name)) { val obj = m invoke this if(!(seen contains obj)) { obj match { @@ -93,6 +94,8 @@ class Bundle(val view: Seq[String] = null) extends Aggregate { elts } + protected def checkPort(obj : Any, name : String) : Boolean = true + lazy val elements = calcElements(view) def fromMap(elemmap: Map[String, Data]): this.type = { From 84222b664de0e68a522590ca8814c90a865dd2e0 Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Thu, 23 Apr 2015 16:56:42 -0700 Subject: [PATCH 3/6] Fix null pointer exception for nodes with no component. --- src/main/scala/Backend.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/Backend.scala b/src/main/scala/Backend.scala index eeb11e1d..f4214ad6 100644 --- a/src/main/scala/Backend.scala +++ b/src/main/scala/Backend.scala @@ -172,7 +172,7 @@ abstract class Backend extends FileSystemUtilities{ case reg: Reg if reg.name == "" => reg setName "R" + reg.component.nextIndex - case node: Node if !node.isTypeNode && node.name == "" => + case node: Node if !node.isTypeNode && node.name == "" && node.component != null => node.name = "T" + node.component.nextIndex case _ => } } From 5f44ac33bf2938f1c52963b56b7d21a570151111 Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Fri, 24 Apr 2015 10:30:49 -0700 Subject: [PATCH 4/6] Filter out all but real and imag from Complex bundle elements calculation. --- src/main/scala/Complex.scala | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/scala/Complex.scala b/src/main/scala/Complex.scala index 931d6691..5e5ff440 100644 --- a/src/main/scala/Complex.scala +++ b/src/main/scala/Complex.scala @@ -48,6 +48,12 @@ class Complex[T<:Data with Num[T]](val real: T, val imag: T) extends Bundle with new Complex(real.clone, imag.clone).asInstanceOf[this.type] } + override protected def checkPort(obj : Any, name : String) : Boolean = name match { + case "real" => true + case "imag" => true + case _ => false + } + def * (r: Complex[T]): Complex[T] = { val a = real; val b = imag; val c = r.real; val d = r.imag; @@ -100,7 +106,8 @@ class Complex[T<:Data with Num[T]](val real: T, val imag: T) extends Bundle with { this.abs2 >= b.abs2 } - def abs2 : T = { + def abs2 : T = + { real * real + imag * imag } def / (r: T): Complex[T] = From b5c94530fc3d1f0e7b87d7d34734b015a6263594 Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Wed, 29 Apr 2015 09:02:55 -0700 Subject: [PATCH 5/6] Fix stack overflow when computing elements in bundle causes methods like abs2 to be invoked, constructing a new Complex() and repeating the cycle. --- src/main/scala/Complex.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/scala/Complex.scala b/src/main/scala/Complex.scala index 5e5ff440..2dc14f1b 100644 --- a/src/main/scala/Complex.scala +++ b/src/main/scala/Complex.scala @@ -51,7 +51,10 @@ class Complex[T<:Data with Num[T]](val real: T, val imag: T) extends Bundle with override protected def checkPort(obj : Any, name : String) : Boolean = name match { case "real" => true case "imag" => true - case _ => false + case "abs2" => false + case "conj" => false + case "unary_-" => false + case _ => true } def * (r: Complex[T]): Complex[T] = From 8e4d58006e525665b99ae2ab6e639e0965f84290 Mon Sep 17 00:00:00 2001 From: Paul Rigge Date: Wed, 29 Apr 2015 09:03:37 -0700 Subject: [PATCH 6/6] Add f< and other missing operations to Cpp backend. --- src/main/scala/Cpp.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/scala/Cpp.scala b/src/main/scala/Cpp.scala index b5e59b4a..9f58e30d 100644 --- a/src/main/scala/Cpp.scala +++ b/src/main/scala/Cpp.scala @@ -288,7 +288,7 @@ class CppBackend extends Backend { } else if (o.op == "~") { block((0 until words(o)).map(i => emitWordRef(o, i) + " = ~" + emitWordRef(o.inputs(0), i))) + trunc(o) } else if (o.op == "f-") - " " + emitLoWordRef(o) + " = fromFloat(-(toFloat(" + emitLoWordRef(o.inputs(0)) + "));\n" + " " + emitLoWordRef(o) + " = fromFloat(-(toFloat(" + emitLoWordRef(o.inputs(0)) + ")));\n" else if (o.op == "fsin") " " + emitLoWordRef(o) + " = fromFloat(sin(toFloat(" + emitLoWordRef(o.inputs(0)) + ")));\n" else if (o.op == "fcos") @@ -474,6 +474,8 @@ class CppBackend extends Backend { " " + emitLoWordRef(o) + " = toFloat(" + emitLoWordRef(o.inputs(0)) + ") != toFloat(" + emitLoWordRef(o.inputs(1)) + ");\n" } else if (o.op == "f>") { " " + emitLoWordRef(o) + " = toFloat(" + emitLoWordRef(o.inputs(0)) + ") > toFloat(" + emitLoWordRef(o.inputs(1)) + ");\n" + } else if (o.op == "f<") { + " " + emitLoWordRef(o) + " = toFloat(" + emitLoWordRef(o.inputs(0)) + ") < toFloat(" + emitLoWordRef(o.inputs(1)) + ");\n" } else if (o.op == "f<=") { " " + emitLoWordRef(o) + " = toFloat(" + emitLoWordRef(o.inputs(0)) + ") <= toFloat(" + emitLoWordRef(o.inputs(1)) + ");\n" } else if (o.op == "f>=") {