提交 7a4bc42e 编写于 作者: 饶先宏's avatar 饶先宏

202105210643

上级 48e5d8bb
......@@ -63,6 +63,9 @@ typedef struct sIBigNumber {
int (*AssignFloat)(HOBJECT object, float value);
int (*AssignDouble)(HOBJECT object, double value);
int (*Assign)(HOBJECT object, HOBJECT src);
int (*AssignSub)(HOBJECT object, HOBJECT src, int from, int width);
int (*Bind)(HOBJECT object, HOBJECT src, int width);
int (*Neg)(HOBJECT object);
int (*AddInt)(HOBJECT object, int value);
......@@ -78,8 +81,13 @@ typedef struct sIBigNumber {
int (*SHL)(HOBJECT object, int bits);
int (*SHR)(HOBJECT object, int bits);
int (*Not)(HOBJECT object);
int (*uAnd)(HOBJECT object);
int (*uOr)(HOBJECT object);
int (*uXor)(HOBJECT object);
int (*And)(HOBJECT object, HOBJECT src);
int (*Or)(HOBJECT object, HOBJECT src);
int (*Xor)(HOBJECT object, HOBJECT src);
int (*IsZero)(HOBJECT object);
int (*IsNeg)(HOBJECT object);
......@@ -105,6 +113,8 @@ typedef struct sIBigNumber {
static int _obj##_bn_AssignFloat(HOBJECT object, float value); \
static int _obj##_bn_AssignDouble(HOBJECT object, double value); \
static int _obj##_bn_Assign(HOBJECT object, HOBJECT src); \
static int _obj##_bn_AssignSub(HOBJECT object, HOBJECT src, int from, int width); \
static int _obj##_bn_Bind(HOBJECT object, HOBJECT src, int width); \
static int _obj##_bn_Neg(HOBJECT object); \
static int _obj##_bn_AddInt(HOBJECT object, int value); \
static int _obj##_bn_Add(HOBJECT object, HOBJECT src); \
......@@ -116,8 +126,13 @@ typedef struct sIBigNumber {
static int _obj##_bn_Div(HOBJECT object, HOBJECT src); \
static int _obj##_bn_SHL(HOBJECT object, int bits); \
static int _obj##_bn_SHR(HOBJECT object, int bits); \
static int _obj##_bn_Not(HOBJECT object); \
static int _obj##_bn_uAnd(HOBJECT object); \
static int _obj##_bn_uOr(HOBJECT object); \
static int _obj##_bn_uXor(HOBJECT object); \
static int _obj##_bn_And(HOBJECT object, HOBJECT src); \
static int _obj##_bn_Or(HOBJECT object, HOBJECT src); \
static int _obj##_bn_Xor(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsZero(HOBJECT object); \
static int _obj##_bn_IsNeg(HOBJECT object); \
static int _obj##_bn_IsEQ(HOBJECT object, HOBJECT src); \
......@@ -138,6 +153,8 @@ typedef struct sIBigNumber {
_obj##_bn_AssignFloat, \
_obj##_bn_AssignDouble, \
_obj##_bn_Assign, \
_obj##_bn_AssignSub, \
_obj##_bn_Bind, \
_obj##_bn_Neg, \
_obj##_bn_AddInt, \
_obj##_bn_Add, \
......@@ -149,8 +166,13 @@ typedef struct sIBigNumber {
_obj##_bn_Div, \
_obj##_bn_SHL, \
_obj##_bn_SHR, \
_obj##_bn_Not, \
_obj##_bn_uAnd, \
_obj##_bn_uOr, \
_obj##_bn_uXor, \
_obj##_bn_And, \
_obj##_bn_Or, \
_obj##_bn_Xor, \
_obj##_bn_IsZero, \
_obj##_bn_IsNeg, \
_obj##_bn_IsEQ, \
......
/*
** HDL4SE: 软件Verilog综合仿真平台
** HDL(CELL_WIDTH/8)SE: 软件Verilog综合仿真平台
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** LCOM: 轻量级组件对象模型
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
......@@ -41,6 +41,7 @@
#include "bignumber.h"
#undef IMPLEMENT_GUID
#define CELL_WIDTH 32
typedef struct _sBigInteger {
OBJECT_HEADER
......@@ -79,7 +80,7 @@ static int bigintCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* pObje
pobj->sign = 0;
pobj->buflen = 2;
pobj->buf = malloc(pobj->buflen * sizeof(unsigned int));
pobj->buf = malloc(pobj->buflen * (CELL_WIDTH/8));
pobj->buf[0] = pobj->buf[1] = 0;
/* 返回生成的对象 */
......@@ -92,7 +93,7 @@ static int actualwidth(unsigned int n)
unsigned int c = 1;
int ret = 1;
if (n & 0x80000000)
return 32;
return CELL_WIDTH;
while (c <= n) {
c <<= 1;
ret++;
......@@ -128,7 +129,7 @@ static int bigint_bn_GetBitsCount(HOBJECT object)
pobj = (sBigInteger*)objectThis(object);
for (i = pobj->buflen - 1; i >= 0; i--) {
if (pobj->buf[i] != 0) {
return i * 32 + actualwidth(pobj->buf[i]);
return i * CELL_WIDTH + actualwidth(pobj->buf[i]);
}
}
return 0;
......@@ -140,16 +141,16 @@ static int bigint_bn_SetBitsCount(HOBJECT object, int count)
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
if (count <= 0) {
memset(pobj->buf, 0, pobj->buflen * 4);
memset(pobj->buf, 0, pobj->buflen * (CELL_WIDTH/8));
return 0;
}
bc = bigint_bn_GetBitsCount(pobj);
if (count >= bc)
return 0;
for (i = (count+31) / 32; i < pobj->buflen; i++) {
for (i = (count+(CELL_WIDTH-1)) / CELL_WIDTH; i < pobj->buflen; i++) {
pobj->buf[i] = 0;
}
pobj->buf[count / 32] &= 0xffffffff >> (32 - (count & 31));
pobj->buf[count / CELL_WIDTH] &= 0xffffffff >> (CELL_WIDTH - (count & (CELL_WIDTH-1)));
return 0;
}
......@@ -171,7 +172,7 @@ static int bigint_bn_GetInt64(HOBJECT object, long long* pvalue)
if (pobj->buflen >= 2) {
long long temp;
temp = pobj->buf[1];
temp <<= 32;
temp <<= CELL_WIDTH;
*pvalue += temp;
}
if (pobj->sign)
......@@ -204,13 +205,13 @@ static int bigint_bn_GetStr(HOBJECT object, int base, char* str, int buflen)
return 0;
}
sprintf(str, "%d'h", bc);
i = ((bc + 31) / 32) - 1;
i = ((bc + (CELL_WIDTH-1)) / CELL_WIDTH) - 1;
ac = actualwidth(pobj->buf[i]);
ac = (ac + 7) / 8;
sprintf(fmt, "%%%dx_", ac);
sprintf(buf, fmt, pobj->buf[i]);
strcat(str, buf);
for (i = ((bc + 31) / 32)-2; i >= 0; i--) {
for (i = ((bc + (CELL_WIDTH-1)) / CELL_WIDTH)-2; i >= 0; i--) {
sprintf(buf, "%08x", pobj->buf[i]);
strcat(str, buf);
if (i > 0)
......@@ -360,7 +361,7 @@ static int bigint_bn_AssignInt(HOBJECT object, int value)
} else {
pobj->sign = 0;
}
memset(pobj->buf, 0, pobj->buflen * sizeof(unsigned int));
memset(pobj->buf, 0, pobj->buflen * (CELL_WIDTH/8));
pobj->buf[0] = value;
return 0;
}
......@@ -376,9 +377,9 @@ static int bigint_bn_AssignInt64(HOBJECT object, long long value)
else {
pobj->sign = 0;
}
memset(pobj->buf, 0, pobj->buflen * sizeof(unsigned int));
memset(pobj->buf, 0, pobj->buflen * (CELL_WIDTH/8));
pobj->buf[0] = value & 0xFFFFFFFF;
pobj->buf[1] = value >> 32;
pobj->buf[1] = value >> CELL_WIDTH;
return 0;
}
......@@ -406,17 +407,36 @@ static int bigint_bn_Assign(HOBJECT object, HOBJECT src)
return -1;
}
psrc = (sBigInteger*)objectThis(src);
buf = (unsigned int*)malloc(psrc->buflen * sizeof(unsigned int));
buf = (unsigned int*)malloc(psrc->buflen * (CELL_WIDTH/8));
if (buf == NULL)
return -2;
free(pobj->buf);
pobj->buf = buf;
memcpy(pobj->buf, psrc->buf, psrc->buflen * sizeof(unsigned int));
memcpy(pobj->buf, psrc->buf, psrc->buflen * (CELL_WIDTH/8));
pobj->buflen = psrc->buflen;
pobj->sign = psrc->sign;
return 0;
}
static int bigint_bn_AssignSub(HOBJECT object, HOBJECT src, int from, int width)
{
bigint_bn_Assign(object, src);
bigint_bn_SHR(object, from);
bigint_bn_SetBitsCount(object, width);
return 0;
}
static int bigint_bn_Bind(HOBJECT object, HOBJECT src, int width)
{
IBigNumber** temp = bigintegerCreate();
bigint_bn_Assign(temp, src);
bigint_bn_SetBitsCount(temp, width);
bigint_bn_SHL(object, width);
bigint_bn_Or(object, temp);
objectRelease(temp);
return 0;
}
static int bigint_bn_Neg(HOBJECT object)
{
sBigInteger* pobj;
......@@ -428,11 +448,11 @@ static int bigint_bn_Neg(HOBJECT object)
static int bigint_expandbuf(sBigInteger* pobj)
{
unsigned int* buf;
buf = (unsigned int*)malloc(pobj->buflen * 2 * sizeof(unsigned int));
buf = (unsigned int*)malloc(pobj->buflen * 2 * (CELL_WIDTH/8));
if (buf == NULL)
return -1;
memcpy(buf, pobj->buf, pobj->buflen * sizeof(unsigned int));
memset(&buf[pobj->buflen], 0, pobj->buflen * sizeof(unsigned int));
memcpy(buf, pobj->buf, pobj->buflen * (CELL_WIDTH/8));
memset(&buf[pobj->buflen], 0, pobj->buflen * (CELL_WIDTH/8));
free(pobj->buf);
pobj->buf = buf;
pobj->buflen *= 2;
......@@ -456,7 +476,7 @@ static int bigint_bn_AddInt(HOBJECT object, int value)
int i;
temp = temp + value;
pobj->buf[0] = temp & 0xffffffff;
temp >>= 32;
temp >>= CELL_WIDTH;
i = 1;
/* 处理进位 */
while (temp != 0) {
......@@ -466,12 +486,12 @@ static int bigint_bn_AddInt(HOBJECT object, int value)
}
temp += pobj->buf[i];
pobj->buf[i] = temp & 0xffffffff;
temp >>= 32;
temp >>= CELL_WIDTH;
i++;
}
} else {
int bc = bigint_bn_GetBitsCount(object);
if (bc <= 32) {
if (bc <= CELL_WIDTH) {
if (pobj->buf[0] < value) {
pobj->sign = 1 - pobj->sign;
pobj->buf[0] = value - pobj->buf[0];
......@@ -484,7 +504,7 @@ static int bigint_bn_AddInt(HOBJECT object, int value)
borrow = 0;
temp = pobj->buf[0];
if (temp < value) {
temp += 1ll << 32;
temp += 1ll << CELL_WIDTH;
borrow = 1;
}
i = 1;
......@@ -543,7 +563,7 @@ static int bigint_bn_MulInt(HOBJECT object, int value)
mulr *= pobj->buf[i];
mulr += addin;
pobj->buf[i] = mulr & 0xffffffff;
addin = mulr >> 32;
addin = mulr >> CELL_WIDTH;
}
if (addin != 0) {
if (0 != bigint_expandbuf(pobj))
......@@ -587,21 +607,21 @@ static int bigint_bn_SHL(HOBJECT object, int bits)
if (bits < 0)
return bigint_bn_SHR(object, -bits);
bc = bigint_bn_GetBitsCount(object);
blen = (bc + bits + 31) / 32;
buf = (unsigned int*)malloc(blen * 4);
blen = (bc + bits + (CELL_WIDTH-1)) / CELL_WIDTH;
buf = (unsigned int*)malloc(blen * (CELL_WIDTH/8));
if (buf == NULL)
return -1;
ito = bits / 32;
ito = bits / CELL_WIDTH;
if (ito > 0) {
memset(buf, 0, ito * 4);
memset(buf, 0, ito * (CELL_WIDTH/8));
}
bits -= ito * 32;
bits -= ito * CELL_WIDTH;
ifrom = 0;
current = pobj->buf[0];
current <<= bits;
for (i = 1; i < (bc + 31) / 32; i++) {
for (i = 1; i < (bc + (CELL_WIDTH-1)) / CELL_WIDTH; i++) {
buf[ito++] = (unsigned int)(current & 0xffffffff);
current >>= 32;
current >>= CELL_WIDTH;
next = pobj->buf[i];
next <<= bits;
current |= next;
......@@ -625,15 +645,15 @@ static int bigint_bn_SHR(HOBJECT object, int bits)
if (bits < 0)
return bigint_bn_SHL(object, -bits);
ito = 0;
ifrom = bits / 32;
ifrom = bits / CELL_WIDTH;
if (ifrom >= pobj->buflen) {
memset(pobj->buf, 0, pobj->buflen * 4);
memset(pobj->buf, 0, pobj->buflen * (CELL_WIDTH/8));
return 0;
}
bits -= ifrom * 32;
bits -= ifrom * CELL_WIDTH;
if (ifrom+1 < pobj->buflen) {
current = pobj->buf[ifrom+1];
current <<= 32;
current <<= CELL_WIDTH;
}
else {
current = 0;
......@@ -642,9 +662,9 @@ static int bigint_bn_SHR(HOBJECT object, int bits)
current >>= bits;
for (i = ifrom + 1; i < pobj->buflen-1; i++) {
pobj->buf[ito++] = (unsigned int)(current & 0xffffffff);
current >>= 32;
current >>= CELL_WIDTH;
next = pobj->buf[i + 1];
next <<= 32-bits;
next <<= CELL_WIDTH-bits;
current |= next;
}
pobj->buf[ito++] = (unsigned int)(current & 0xffffffff);
......@@ -653,6 +673,34 @@ static int bigint_bn_SHR(HOBJECT object, int bits)
return 0;
}
static int bigint_bn_Not(HOBJECT object)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_uAnd(HOBJECT object)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_uOr(HOBJECT object)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_uXor(HOBJECT object)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_And(HOBJECT object, HOBJECT src)
{
sBigInteger* pobj;
......@@ -662,6 +710,26 @@ static int bigint_bn_And(HOBJECT object, HOBJECT src)
static int bigint_bn_Or(HOBJECT object, HOBJECT src)
{
sBigInteger* pobj;
sBigInteger* psrc;
int bc, bcsrc, i;
pobj = (sBigInteger*)objectThis(object);
if (!objectIsClass(src, CLSID_BIGINTEGER)) {
return -1;
}
psrc = (sBigInteger*)objectThis(src);
bc = bigint_bn_GetBitsCount(object);
bcsrc = bigint_bn_GetBitsCount(src);
for (i = 0; i < (bcsrc + (CELL_WIDTH-1)) / CELL_WIDTH; i++) {
while (pobj->buflen <= i)
bigint_expandbuf(pobj);
pobj->buf[i] |= psrc->buf[i];
}
return 0;
}
static int bigint_bn_Xor(HOBJECT object, HOBJECT src)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册