Commit 9fae1946 authored by Eva Darulova's avatar Eva Darulova
Browse files

adding fixed-point precision to error analysis

parent 1045a1af
......@@ -26,7 +26,8 @@ object RangeErrorPhase extends DaisyPhase with ErrorFunctions {
FlagOptionDef("noInitialErrors", "do not track initial errors specified by user"),
FlagOptionDef("noRoundoff", "do not track roundoff errors"),
ChoiceOptionDef("precision", "Type of precision to use",
Set("Float32", "Float64", "DoubleDouble", "QuadDouble"), "Float64")
Set("Float32", "Float64", "DoubleDouble", "QuadDouble",
"Fixed16", "Fixed32", "Fixed64"), "Float64")
)
implicit val debugSection = DebugSectionAnalysis
......@@ -76,6 +77,12 @@ object RangeErrorPhase extends DaisyPhase with ErrorFunctions {
case "QuadDouble" =>
uniformPrecision = QuadDouble
reporter.info(s"using $s")
case "Fixed16" =>
uniformPrecision = Fixed(16)
case "Fixed32" =>
uniformPrecision = Fixed(32)
case "Fixed64" =>
uniformPrecision = Fixed(64)
case _ =>
reporter.warning(s"Unknown precision specified: $s, choosing default (Float64)!")
}
......@@ -87,7 +94,7 @@ object RangeErrorPhase extends DaisyPhase with ErrorFunctions {
for (fnc <- prg.defs)
if (!fnc.precondition.isEmpty && !fnc.body.isEmpty && fncsToConsider.contains(fnc.id.toString)){
reporter.debug("analyzing fnc: " + fnc.id)
reporter.info("analyzing fnc: " + fnc.id)
val inputValMap: Map[Identifier, Interval] = ctx.inputRanges(fnc.id)
// If we track both input and roundoff errors, then we pre-compute
......
......@@ -26,6 +26,7 @@ object CodeGenerationPhase extends DaisyPhase {
case _ => ;
}
// TODO: why is this here?
def writeScalaFile(filename: String) {
import java.io.FileWriter
import java.io.BufferedWriter
......
......@@ -36,6 +36,7 @@ class ScalaPrinter extends PrettyPrinter {
case Not(Equals(l, r)) => ppBinary(l, r, " != ") // \neq
case Not(expr) => ppUnary(expr, "!(", ")") // \neg
// TODO: support the other types too
case RealType => sb.append("Double")
case Program(id, defs) =>
......
......@@ -45,6 +45,7 @@ object SSATransformerPhase extends DaisyPhase {
val newBody = toSSA(fnc.body.get)
// TODO: probably can just use copy here
FunDef(fnc.id, fnc.returnType, fnc.params, fnc.precondition,
Some(newBody), fnc.postcondition, fnc.isField)
} else {
......
......@@ -12,20 +12,26 @@ object FinitePrecision {
def isExactInFloats(r: Rational): Boolean = {
if (r.isWhole) {
true
} else if (r.d.isValidInt) {
} /*else if (r.d.isValidInt) {
val n = r.d.toInt
(n > 0) && ((n & (n - 1)) == 0)
} else {
}*/ else {
false
}
}
sealed abstract class Precision {
/* The range of values that are representable by this precision. */
def range: (Rational, Rational)
def minNormal: Rational
/* The smallest (absolute) value representable by this precision,
for floating-point precisions, it's the smallest normal (and not denormal) */
//def minNormal: Rational
/* Computes the worst-case roundoff error of the given value */
def absRoundoff(r: Rational): Rational
/* Computes the worst-case roundoff error of the given range of values. */
def absRoundoff(i: Interval): Rational = {
absRoundoff(max(abs(i.xlo), abs(i.xhi)))
}
......@@ -98,9 +104,34 @@ object FinitePrecision {
}
}
/*case class FPPrecision(bitlength: Int) extends Precision {
val range: (Rational, Rational) = FixedPointFormat(true, bitlength, 0, false).range
val minNormal: Rational = zero // dummy
}*/
/*
Represents a fixed-point arithmetic precision.
Supports a signed format with truncation as the rounding mode.
*/
case class Fixed(bitlength: Int) extends Precision {
// TODO: is this correct?
val range: (Rational, Rational) =
(Rational(-math.pow(2, bitlength - 1).toLong, 1l),
Rational(math.pow(2, bitlength - 1).toLong - 1, 1l))
//val minNormal: Rational = ???
def absRoundoff(r: Rational): Rational = {
val intBits = bitsNeeded(math.abs(r.integerPart))
val fracBits = bitlength - intBits
Rational(1, math.pow(2, fracBits).toLong)
}
/**
Returns the number of bits needed to represent the given integer.
@param 32-bit integer
*/
private def bitsNeeded(value: Int): Int = {
assert(value >= 0)
// TODO: don't we have to also subtract 1 for the sign?
32 - Integer.numberOfLeadingZeros(value)
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment