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.
This commit is contained in:
parent
f8bd8e1bd6
commit
66bed241f1
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue