From c222169608850fb877c45056d08f32f2ff897920 Mon Sep 17 00:00:00 2001 From: Cary R Date: Sun, 1 Apr 2012 12:29:08 -0700 Subject: [PATCH] Update vecval size calculation in vvp and vpi code. The standard specifies that the size of a vecval should be calculated as (size - 1)/32 + 1. When size is a PLI_INT32 this is needed to prevent an overflow, but when the size is unsigned this can be simplified to (size + 31)/32 since the size must fit into an integer, but we have an extra significant bit in an unsigned so no overflow can happen. This patch changes the code to use the correct version of the equation depending on the context. The previous patch does this in vvp/vpi_priv.cc --- vpi/sys_fileio.c | 5 +++-- vvp/vpi_const.cc | 18 +++++++++--------- vvp/vpi_signal.cc | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/vpi/sys_fileio.c b/vpi/sys_fileio.c index 305775390..667d78fef 100644 --- a/vpi/sys_fileio.c +++ b/vpi/sys_fileio.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2010 Stephen Williams (steve@icarus.com) + * Copyright (c) 2003-2012 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -680,7 +680,8 @@ static PLI_INT32 sys_fread_calltf(ICARUS_VPI_CONST PLI_BYTE8*name) vpi_free_object(argv); } - words = (width+31)/32; + assert(width > 0); + words = (width - 1)/32 + 1; vector = calloc(words, sizeof(s_vpi_vecval)); bpe = (width+7)/8; diff --git a/vvp/vpi_const.cc b/vvp/vpi_const.cc index 2b95fe644..f7ca58757 100644 --- a/vvp/vpi_const.cc +++ b/vvp/vpi_const.cc @@ -121,7 +121,7 @@ void __vpiStringConst::vpi_get_value(p_vpi_value vp) { unsigned uint_value; p_vpi_vecval vecp; - int size = strlen(value_); + unsigned size = strlen(value_); char*rbuf = 0; char*cp; @@ -145,7 +145,7 @@ void __vpiStringConst::vpi_get_value(p_vpi_value vp) } rbuf = need_result_buf(size + 1, RBUF_VAL); uint_value = 0; - for(int i=0; i=0; bit--){ + for(unsigned i=0; i=0; bit -= 1){ *cp++ = "01"[ (value_[i]>>bit)&1 ]; } } @@ -168,8 +168,8 @@ void __vpiStringConst::vpi_get_value(p_vpi_value vp) case vpiHexStrVal: rbuf = need_result_buf(2 * size + 1, RBUF_VAL); cp = rbuf; - for(int i=0; i=0; nibble--){ + for(unsigned i=0; i=0; nibble -= 1){ *cp++ = "0123456789abcdef"[ (value_[i]>>(nibble*4))&15 ]; } } @@ -184,8 +184,8 @@ void __vpiStringConst::vpi_get_value(p_vpi_value vp) case vpiIntVal: vp->value.integer = 0; - for(int i=0; i=0; bit--){ + for(unsigned i=0; i=0; bit -= 1){ vp->value.integer <<= 1; vp->value.integer += (value_[i]>>bit)&1; } @@ -200,7 +200,7 @@ void __vpiStringConst::vpi_get_value(p_vpi_value vp) uint_value = 0; vecp = vp->value.vector; vecp->aval = vecp->bval = 0; - for(int i=0; iaval |= value_[i] << uint_value*8; uint_value += 1; if (uint_value > 3) { diff --git a/vvp/vpi_signal.cc b/vvp/vpi_signal.cc index dd821c6c4..3478fed40 100644 --- a/vvp/vpi_signal.cc +++ b/vvp/vpi_signal.cc @@ -467,7 +467,7 @@ static void format_vpiVectorVal(vvp_signal_value*sig, int base, unsigned wid, { long end = base + (signed)wid; unsigned int obit = 0; - unsigned hwid = (wid - 1)/32 + 1; + unsigned hwid = (wid + 31)/32; s_vpi_vecval *op = (p_vpi_vecval) need_result_buf(hwid * sizeof(s_vpi_vecval), RBUF_VAL);