From 66bed241f1da7a658f1ef45a690b705a3fa39bd0 Mon Sep 17 00:00:00 2001 From: Stephen Williams Date: Sat, 16 Aug 2008 18:30:07 -0700 Subject: [PATCH] Prevent silent overflow of mantissa When converting a binary constant to a real value in the vvp code generator, make sure the mantissa does not overflow. If it does, panic in some way. --- tgt-vvp/eval_real.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tgt-vvp/eval_real.c b/tgt-vvp/eval_real.c index 8a7dda0f2..15322cdbc 100644 --- a/tgt-vvp/eval_real.c +++ b/tgt-vvp/eval_real.c @@ -168,12 +168,23 @@ static int draw_number_real(ivl_expr_t exp) unsigned long mant = 0, mask = -1UL; int vexp = 0x1000; - for (idx = 0 ; idx < wid ; idx += 1) { + for (idx = 0 ; idx < wid && idx < 8*sizeof(mant) ; idx += 1) { mask <<= 1; if (bits[idx] == '1') mant |= 1 << idx; } + for ( ; idx < wid ; idx += 1) { + if (ivl_expr_signed(exp) && (bits[idx] == bits[8*sizeof(mant)-1])) + continue; + + if (bits[idx] == '0') + continue; + + fprintf(stderr, "internal error: mantissa doesn't fit!\n"); + assert(0); + } + /* If this is actually a negative number, then get the positive equivalent, and set the sign bit in the exponent field. @@ -188,8 +199,8 @@ static int draw_number_real(ivl_expr_t exp) vexp |= 0x4000; } - fprintf(vvp_out, " %%loadi/wr %d, %lu, %d; load(num)= %c%lu\n", - res, mant, vexp, (vexp&0x4000)? '-' : '+', mant); + fprintf(vvp_out, " %%loadi/wr %d, %lu, %d; load(num)= %c%lu (wid=%u)\n", + res, mant, vexp, (vexp&0x4000)? '-' : '+', mant, wid); return res; }