Improved AstNode::asReal for large integers
authorClifford Wolf <clifford@clifford.at>
Sun, 15 Jun 2014 06:38:31 +0000 (08:38 +0200)
committerClifford Wolf <clifford@clifford.at>
Sun, 15 Jun 2014 06:38:31 +0000 (08:38 +0200)
frontends/ast/ast.cc
frontends/ast/simplify.cc

index 25ea3a8248ded68a4eec8322cf016a0daad9c2d1..cc7f442bb9c0e36b12064f991fa5df2dbe734ccd 100644 (file)
@@ -775,17 +775,20 @@ int AstNode::isConst()
 double AstNode::asReal(bool is_signed)
 {
        if (type == AST_CONSTANT) {
-               RTLIL::Const val;
-               val.bits = bits;
+               RTLIL::Const val(bits);
 
-               double p = exp2(int(val.bits.size())-32);
-               if (val.bits.size() > 32)
-                       val.bits.erase(val.bits.begin(), val.bits.begin()+(int(val.bits.size())-32));
-               int32_t v = val.as_int() << (32-int(val.bits.size()));
+               bool is_negative = is_signed && val.bits.back() == RTLIL::State::S1;
+               if (is_negative)
+                       val = const_neg(val, val, false, false, val.bits.size());
 
-               if (is_signed)
-                       return v * p;
-               return uint32_t(v) * p;
+               double v = 0;
+               for (size_t i = 0; i < val.bits.size(); i++)
+                       if (val.bits.at(i) == RTLIL::State::S1)
+                               v += exp2(i);
+               if (is_negative)
+                       v *= -1;
+
+               return v;
        }
 
        if (type == AST_REALVALUE)
index ed5ee1721f901b3ed3ebf45462968f7f3f47fb10..80fd28d55db6bba0cd05987cdb4992e3cec9a9be 100644 (file)
@@ -533,7 +533,7 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                        if (children[0]->type == AST_REALVALUE) {
                                RTLIL::Const constvalue = children[0]->realAsConst(width);
                                log("Warning: converting real value %e to binary %s at %s:%d.\n",
-                                               realvalue, log_signal(constvalue), filename.c_str(), linenum);
+                                               children[0]->realvalue, log_signal(constvalue), filename.c_str(), linenum);
                                delete children[0];
                                children[0] = mkconst_bits(constvalue.bits, sign_hint);
                                did_something = true;