From 77987d73d410dea51b1335d6f098ccce9f9b082c Mon Sep 17 00:00:00 2001 From: m0_56903617 Date: Thu, 27 May 2021 14:50:27 +0800 Subject: [PATCH] 202105271450 --- bignumber/include/bignumber.h | 4 +- bignumber/src/bignumber.c | 132 ++++++++++++++++++++++--- examples/testbignumber/testbignumber.c | 2 +- hdl4secell/src/hdl4se_bind2.c | 2 +- hdl4secell/src/hdl4se_bind3.c | 2 +- hdl4secell/src/hdl4se_bind4.c | 2 +- hdl4secell/src/hdl4se_binop.c | 6 +- hdl4secell/src/hdl4se_const.c | 4 +- hdl4secell/src/hdl4se_mux16.c | 2 +- hdl4secell/src/hdl4se_mux2.c | 2 +- hdl4secell/src/hdl4se_mux4.c | 2 +- hdl4secell/src/hdl4se_mux8.c | 2 +- hdl4secell/src/hdl4se_reg.c | 2 +- hdl4secell/src/hdl4se_split2.c | 6 +- hdl4secell/src/hdl4se_split4.c | 6 +- hdl4secell/src/hdl4se_unop.c | 6 +- hdl4secell/src/hdl4se_wire.c | 2 +- 17 files changed, 145 insertions(+), 39 deletions(-) diff --git a/bignumber/include/bignumber.h b/bignumber/include/bignumber.h index 6ebc99a..0fd059c 100644 --- a/bignumber/include/bignumber.h +++ b/bignumber/include/bignumber.h @@ -70,7 +70,7 @@ typedef struct sIBigNumber { int (*AssignUint32)(HOBJECT object, unsigned int value); int (*AssignUint64)(HOBJECT object, unsigned long long value); - int (*AssignStr)(HOBJECT object, const char* str, const char** nstr); + int (*AssignStr)(HOBJECT object, const char* str, const char** nstr, int autowidth); int (*Clone)(HOBJECT object, HOBJECT src); int (*CloneSubBits)(HOBJECT object, HOBJECT src, int from, int width, int signexpand); @@ -166,7 +166,7 @@ typedef struct sIBigNumber { static int _obj##_bn_AssignInt64(HOBJECT object, long long value); \ static int _obj##_bn_AssignUint32(HOBJECT object, unsigned int value); \ static int _obj##_bn_AssignUint64(HOBJECT object, unsigned long long value); \ - static int _obj##_bn_AssignStr(HOBJECT object, const char* str, const char** nstr); \ + static int _obj##_bn_AssignStr(HOBJECT object, const char* str, const char** nstr, int autowidth); \ static int _obj##_bn_Clone(HOBJECT object, HOBJECT src); \ static int _obj##_bn_CloneSubBits(HOBJECT object, HOBJECT src, int from, int width, int signexpand); \ static int _obj##_bn_Assign(HOBJECT object, HOBJECT src); \ diff --git a/bignumber/src/bignumber.c b/bignumber/src/bignumber.c index 29582e2..c0664e7 100644 --- a/bignumber/src/bignumber.c +++ b/bignumber/src/bignumber.c @@ -379,14 +379,23 @@ enum TOKEN_STATE { TOKEN_HEX }; -static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nstr) +#define MAXNUMWIDTH (1 << 30) + +static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nstr, int autowidth) { + int width, objwidth, sign; enum TOKEN_STATE state; const char* strt = str; int numvalid = 0; sBigInteger* pobj; pobj = (sBigInteger*)objectThis(object); + width = -1; + objwidth = pobj->width; + if (autowidth == 0 && objwidth <= 0) + return -1; + bigint_bn_SetWidth(object, 64, 0); state = TOKEN_INITIAL; + sign = 0; while (*strt != 0) { int ch = *strt; switch (state) { @@ -402,11 +411,43 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst } break; case TOKEN_NUM: { if (ch >= '0' && ch <= '9') { + if (pobj->buf[pobj->buflen - 1] > (1 << 27)) { + int w; + w = pobj->width * 2; + if (w > MAXNUMWIDTH) { + w = MAXNUMWIDTH; + } + if (0 != bigint_bn_SetWidth(object, w, 0)) { + return -1; + } + } bigint_bn_MulInt32(object, object, 10); bigint_bn_AddInt32(object, object, ch - '0'); } else if (ch == '\'') { + int i; + for (i = pobj->buflen - 1; i > 0; i--) { + if (pobj->buf[i] != 0) { + /* too large width of a number */ + width = MAXNUMWIDTH; + break; + } + } + if (width == -1) { + if (pobj->buf[0] > MAXNUMWIDTH) { + width = MAXNUMWIDTH; + } + else { + width = pobj->buf[0]; + } + } state = TOKEN_BASE; + if (width == 0) + width = -1; + else + if (0 != bigint_bn_SetWidth(object, width, 0)) { + return -1; + } numvalid = 0; } else { @@ -414,7 +455,10 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst } }break; case TOKEN_BASE: { - if (ch == 'b' || ch == 'B') { + if (ch == 's' || ch == 'S') { + sign = 1; + } + else if (ch == 'b' || ch == 'B') { bigint_bn_AssignInt32(object, 0); state = TOKEN_BIN; } @@ -439,8 +483,19 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst }break; case TOKEN_BIN: { if (ch == '0' || ch == '1') { - bigint_bn_MulInt32(object, object, 2); - bigint_bn_AddInt32(object, object, ch - '0'); + if (autowidth && (width == -1)) { + if (pobj->buf[pobj->buflen - 1] > (1 << 27)) { + int w; + w = pobj->width * 2; + if (w > MAXNUMWIDTH) { + w = MAXNUMWIDTH; + } + if (0 != bigint_bn_SetWidth(object, w, 0)) + return -1; + } + } + bigint_bn_MulUint32(object, object, 2); + bigint_bn_AddUint32(object, object, ch - '0'); numvalid = 1; } else if (ch == '_') { @@ -451,8 +506,19 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst }break; case TOKEN_OCT: { if (ch >= '0' && ch <= '7') { - bigint_bn_MulInt32(object, object, 8); - bigint_bn_AddInt32(object, object, ch - '0'); + if (autowidth && (width == -1)) { + if (pobj->buf[pobj->buflen - 1] > (1 << 27)) { + int w; + w = pobj->width * 2; + if (w > MAXNUMWIDTH) { + w = MAXNUMWIDTH; + } + if (0 != bigint_bn_SetWidth(object, w, 0)) + return -1; + } + } + bigint_bn_MulUint32(object, object, 8); + bigint_bn_AddUint32(object, object, ch - '0'); numvalid = 1; } else if (ch == '_') { @@ -463,8 +529,19 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst }break; case TOKEN_DEC: { if (ch >= '0' && ch <= '9') { - bigint_bn_MulInt32(object, object, 10); - bigint_bn_AddInt32(object, object, ch - '0'); + if (autowidth && (width == -1)) { + if (pobj->buf[pobj->buflen - 1] > (1 << 27)) { + int w; + w = pobj->width * 2; + if (w > MAXNUMWIDTH) { + w = MAXNUMWIDTH; + } + if (0 != bigint_bn_SetWidth(object, w, 0)) + return -1; + } + } + bigint_bn_MulUint32(object, object, 10); + bigint_bn_AddUint32(object, object, ch - '0'); numvalid = 1; } else { @@ -474,13 +551,24 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst case TOKEN_HEX: { if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) { numvalid = 1; - bigint_bn_MulInt32(object, object, 16); + if (autowidth && (width == -1)) { + if (pobj->buf[pobj->buflen - 1] > (1 << 27)) { + int w; + w = pobj->width * 2; + if (w > MAXNUMWIDTH) { + w = MAXNUMWIDTH; + } + if (0 != bigint_bn_SetWidth(object, w, 0)) + return -1; + } + } + bigint_bn_MulUint32(object, object, 16); if (ch > '0' && ch <= '9') - bigint_bn_AddInt32(object, object, ch - '0'); + bigint_bn_AddUint32(object, object, ch - '0'); else if (ch >= 'a' && ch <= 'f') - bigint_bn_AddInt32(object, object, ch - 'a' + 10); + bigint_bn_AddUint32(object, object, ch - 'a' + 10); else if (ch >= 'A' && ch <= 'F') - bigint_bn_AddInt32(object, object, ch - 'A' + 10); + bigint_bn_AddUint32(object, object, ch - 'A' + 10); } else if (ch == '_') { } @@ -497,7 +585,25 @@ static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nst lastnum: if (nstr != NULL) *nstr = strt; - bigint_bn_SetWidth(object, pobj->width, 1); + if (autowidth && (width == -1)) { + int i; + width = 0; + for (i = pobj->buflen - 1; i >= 0; i--) { + if (pobj->buf[i] != 0) { + width = actualwidth(pobj->buf[i]); + width += i * CELL_WIDTH; + break; + } + } + if (width == 0) + width = 1; + if (sign == 0) + width++; + } + else { + width = pobj->width; + } + bigint_bn_SetWidth(object, width, sign); return 0; } diff --git a/examples/testbignumber/testbignumber.c b/examples/testbignumber/testbignumber.c index 5510fc3..517fcad 100644 --- a/examples/testbignumber/testbignumber.c +++ b/examples/testbignumber/testbignumber.c @@ -44,7 +44,7 @@ int main(int argc, char* argv[]) const char* nstr = testnumber; const char* lstr = testnumber; /* - while (0 == objectCall2(bignumber, AssignStr, lstr, &nstr)) { + while (0 == objectCall3(bignumber, AssignStr, lstr, &nstr, 1)) { objectCall3(bignumber, GetStr, 16, buf, 128); printf("%s=%s, \nnext=%s\n", lstr, buf, nstr); lstr = nstr; diff --git a/hdl4secell/src/hdl4se_bind2.c b/hdl4secell/src/hdl4se_bind2.c index 1d19167..e9c7db9 100644 --- a/hdl4secell/src/hdl4se_bind2.c +++ b/hdl4secell/src/hdl4se_bind2.c @@ -137,7 +137,7 @@ static int hdl4se_bind2Create(const PARAMITEM* pParams, int paramcount, HOBJECT* int j; lstr = (const char*)pParams[i].pvalue; for (j = 0; j < BINDCOUNT; j++) { - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->inputwidth[j]); lstr = nstr; } diff --git a/hdl4secell/src/hdl4se_bind3.c b/hdl4secell/src/hdl4se_bind3.c index 272964e..1a7c092 100644 --- a/hdl4secell/src/hdl4se_bind3.c +++ b/hdl4secell/src/hdl4se_bind3.c @@ -137,7 +137,7 @@ static int hdl4se_bind3Create(const PARAMITEM* pParams, int paramcount, HOBJECT* int j; lstr = (const char*)pParams[i].pvalue; for (j = 0; j < BINDCOUNT; j++) { - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->inputwidth[j]); lstr = nstr; } diff --git a/hdl4secell/src/hdl4se_bind4.c b/hdl4secell/src/hdl4se_bind4.c index d338767..8cbf960 100644 --- a/hdl4secell/src/hdl4se_bind4.c +++ b/hdl4secell/src/hdl4se_bind4.c @@ -137,7 +137,7 @@ static int hdl4se_bind4Create(const PARAMITEM* pParams, int paramcount, HOBJECT* int j; lstr = (const char*)pParams[i].pvalue; for (j = 0; j < BINDCOUNT; j++) { - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->inputwidth[j]); lstr = nstr; } diff --git a/hdl4secell/src/hdl4se_binop.c b/hdl4secell/src/hdl4se_binop.c index a699820..91e361d 100644 --- a/hdl4secell/src/hdl4se_binop.c +++ b/hdl4secell/src/hdl4se_binop.c @@ -151,7 +151,7 @@ static int hdl4se_binopCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* int j; lstr = (const char*)pParams[i].pvalue; for (j = 0; j < INCOUNT; j++) { - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->in_width[j]); } else { @@ -159,14 +159,14 @@ static int hdl4se_binopCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* } lstr = nstr; } - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->out_width); } else { return EIID_INVALIDPARAM; } lstr = nstr; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->op); } else { diff --git a/hdl4secell/src/hdl4se_const.c b/hdl4secell/src/hdl4se_const.c index e49c167..936ba6f 100644 --- a/hdl4secell/src/hdl4se_const.c +++ b/hdl4secell/src/hdl4se_const.c @@ -111,13 +111,13 @@ static int hdl4se_constCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } if (pobj->width <= 0 || pobj->width > (1 << 24)) return -1; pobj->out_data = bigintegerCreate(pobj->width); - objectCall2(pobj->out_data, AssignStr, nstr, NULL); + objectCall3(pobj->out_data, AssignStr, nstr, NULL, 0); objectRelease(temp); } } diff --git a/hdl4secell/src/hdl4se_mux16.c b/hdl4secell/src/hdl4se_mux16.c index a9c6f71..8c56f93 100644 --- a/hdl4secell/src/hdl4se_mux16.c +++ b/hdl4secell/src/hdl4se_mux16.c @@ -134,7 +134,7 @@ static int hdl4se_mux16Create(const PARAMITEM* pParams, int paramcount, HOBJECT* IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } objectRelease(temp); diff --git a/hdl4secell/src/hdl4se_mux2.c b/hdl4secell/src/hdl4se_mux2.c index 9bdf0e7..e4979da 100644 --- a/hdl4secell/src/hdl4se_mux2.c +++ b/hdl4secell/src/hdl4se_mux2.c @@ -136,7 +136,7 @@ static int hdl4se_mux2Create(const PARAMITEM* pParams, int paramcount, HOBJECT* IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } objectRelease(temp); diff --git a/hdl4secell/src/hdl4se_mux4.c b/hdl4secell/src/hdl4se_mux4.c index 8d6f4a1..054a643 100644 --- a/hdl4secell/src/hdl4se_mux4.c +++ b/hdl4secell/src/hdl4se_mux4.c @@ -134,7 +134,7 @@ static int hdl4se_mux4Create(const PARAMITEM* pParams, int paramcount, HOBJECT* IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } objectRelease(temp); diff --git a/hdl4secell/src/hdl4se_mux8.c b/hdl4secell/src/hdl4se_mux8.c index f5234f4..c29b975 100644 --- a/hdl4secell/src/hdl4se_mux8.c +++ b/hdl4secell/src/hdl4se_mux8.c @@ -134,7 +134,7 @@ static int hdl4se_mux8Create(const PARAMITEM* pParams, int paramcount, HOBJECT* IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } objectRelease(temp); diff --git a/hdl4secell/src/hdl4se_reg.c b/hdl4secell/src/hdl4se_reg.c index ef7e48d..9912560 100644 --- a/hdl4secell/src/hdl4se_reg.c +++ b/hdl4secell/src/hdl4se_reg.c @@ -120,7 +120,7 @@ static int hdl4se_regCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* p IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } objectRelease(temp); diff --git a/hdl4secell/src/hdl4se_split2.c b/hdl4secell/src/hdl4se_split2.c index df9356d..3594b64 100644 --- a/hdl4secell/src/hdl4se_split2.c +++ b/hdl4secell/src/hdl4se_split2.c @@ -140,7 +140,7 @@ static int hdl4se_split2Create(const PARAMITEM* pParams, int paramcount, HOBJECT const char* lstr; int j; lstr = (const char*)pParams[i].pvalue; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } else { @@ -148,14 +148,14 @@ static int hdl4se_split2Create(const PARAMITEM* pParams, int paramcount, HOBJECT } lstr = nstr; for (j = 0; j < SPLITCOUNT; j++) { - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->out_info[j][0]); } else { return -1; } lstr = nstr; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->out_info[j][1]); } else { diff --git a/hdl4secell/src/hdl4se_split4.c b/hdl4secell/src/hdl4se_split4.c index 79ca756..b6d582b 100644 --- a/hdl4secell/src/hdl4se_split4.c +++ b/hdl4secell/src/hdl4se_split4.c @@ -143,7 +143,7 @@ static int hdl4se_split4Create(const PARAMITEM* pParams, int paramcount, HOBJECT const char* lstr; int j; lstr = (const char*)pParams[i].pvalue; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } else { @@ -151,14 +151,14 @@ static int hdl4se_split4Create(const PARAMITEM* pParams, int paramcount, HOBJECT } lstr = nstr; for (j = 0; j < SPLITCOUNT; j++) { - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->out_info[j][0]); } else { return -1; } lstr = nstr; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->out_info[j][1]); } else { diff --git a/hdl4secell/src/hdl4se_unop.c b/hdl4secell/src/hdl4se_unop.c index b7727b4..7954324 100644 --- a/hdl4secell/src/hdl4se_unop.c +++ b/hdl4secell/src/hdl4se_unop.c @@ -137,21 +137,21 @@ static int hdl4se_unopCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* const char* nstr; const char* lstr; lstr = (const char*)pParams[i].pvalue; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->in_width); } else { return EIID_INVALIDPARAM; } lstr = nstr; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->out_width); } else { return EIID_INVALIDPARAM; } lstr = nstr; - if (0 == objectCall2(temp, AssignStr, lstr, &nstr)) { + if (0 == objectCall3(temp, AssignStr, lstr, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->op); } else { diff --git a/hdl4secell/src/hdl4se_wire.c b/hdl4secell/src/hdl4se_wire.c index eea153e..c4d38e0 100644 --- a/hdl4secell/src/hdl4se_wire.c +++ b/hdl4secell/src/hdl4se_wire.c @@ -125,7 +125,7 @@ static int hdl4se_wireCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* IBigNumber** temp = bigintegerCreate(32); if (temp != NULL) { const char* nstr; - if (0 == objectCall2(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr)) { + if (0 == objectCall3(temp, AssignStr, (const char*)pParams[i].pvalue, &nstr, 0)) { objectCall1(temp, GetInt32, &pobj->width); } objectRelease(temp); -- GitLab