提交 136bae10 编写于 作者: 饶先宏's avatar 饶先宏

202105260638

上级 938a3bda
......@@ -51,49 +51,95 @@ typedef struct sIBigNumber {
OBJECT_INTERFACE
int (*GetWidth)(HOBJECT object);
int (*SetWidth)(HOBJECT object, int width, int signexpand);
int (*GetBits32)(HOBJECT object, int index, unsigned int* pbits);
int (*SetBits32)(HOBJECT object, int index, unsigned int bits);
int (*GetInt)(HOBJECT object, int* pvalue);
int (*GetInt32)(HOBJECT object, int* pvalue);
int (*GetInt64)(HOBJECT object, long long* pvalue);
int (*GetUint32)(HOBJECT object, unsigned int* pvalue);
int (*GetUint64)(HOBJECT object, unsigned long long* pvalue);
int (*GetStr)(HOBJECT object, int base, char * str, int buflen);
int (*AssignStr)(HOBJECT object, const char * str, const char ** nstr);
int (*AssignInt)(HOBJECT object, int value);
int (*AssignInt32)(HOBJECT object, int value);
int (*AssignInt64)(HOBJECT object, long long value);
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 (*Clone)(HOBJECT object, HOBJECT src);
int (*CloneSubBits)(HOBJECT object, HOBJECT src, int from, int width);
int (*Assign)(HOBJECT object, HOBJECT src);
int (*AssignSub)(HOBJECT object, HOBJECT src, int from, int width);
int (*AssignSubBits)(HOBJECT object, HOBJECT src, int from, int width);
int (*AssignU)(HOBJECT object, HOBJECT src);
int (*AssignSubBitsU)(HOBJECT object, HOBJECT src, int from, int width);
int (*Bind)(HOBJECT object, HOBJECT src);
int (*AddInt32)(HOBJECT object, HOBJECT src, int value);
int (*SubInt32)(HOBJECT object, HOBJECT src, int value);
int (*MulInt32)(HOBJECT object, HOBJECT src, int value);
int (*DivInt32)(HOBJECT object, HOBJECT src, int value);
int (*ModInt32)(HOBJECT object, HOBJECT src, int value);
int (*Abs)(HOBJECT object);
int (*Neg)(HOBJECT object);
int (*AddInt)(HOBJECT object, int value);
int (*Add)(HOBJECT object, HOBJECT src);
int (*SubInt)(HOBJECT object, int value);
int (*Sub)(HOBJECT object, HOBJECT src);
int (*MulInt)(HOBJECT object, int value);
int (*Mul)(HOBJECT object, HOBJECT src);
int (*AddUint32)(HOBJECT object, HOBJECT src, unsigned int value);
int (*SubUint32)(HOBJECT object, HOBJECT src, unsigned int value);
int (*MulUint32)(HOBJECT object, HOBJECT src, unsigned int value);
int (*DivUint32)(HOBJECT object, HOBJECT src, unsigned int value);
int (*ModUint32)(HOBJECT object, HOBJECT src, unsigned int value);
int (*DivInt)(HOBJECT object, int value);
int (*Div)(HOBJECT object, HOBJECT src);
int (*SHL)(HOBJECT object, int bits);
int (*SHR)(HOBJECT object, int bits);
int (*SAL)(HOBJECT object, int bits);
int (*SAR)(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 (*Add)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Sub)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Mul)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Div)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Mod)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*IsZero)(HOBJECT object);
int (*IsNeg)(HOBJECT object);
int (*IsEQ)(HOBJECT object, HOBJECT src);
int (*IsLE)(HOBJECT object, HOBJECT src);
int (*AddU)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*SubU)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*MulU)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*DivU)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*ModU)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Neg)(HOBJECT object, HOBJECT src);
int (*IsGT)(HOBJECT object, HOBJECT src);
int (*IsGE)(HOBJECT object, HOBJECT src);
int (*IsLT)(HOBJECT object, HOBJECT src);
int (*IsLE)(HOBJECT object, HOBJECT src);
int (*IsEQ)(HOBJECT object, HOBJECT src);
int (*IsNE)(HOBJECT object, HOBJECT src);
int (*IsGTU)(HOBJECT object, HOBJECT src);
int (*IsGEU)(HOBJECT object, HOBJECT src);
int (*IsLTU)(HOBJECT object, HOBJECT src);
int (*IsLEU)(HOBJECT object, HOBJECT src);
int (*IsEQU)(HOBJECT object, HOBJECT src);
int (*IsNEU)(HOBJECT object, HOBJECT src);
int (*IsZero)(HOBJECT object);
int (*IsNotZero)(HOBJECT object);
int (*AndL)(HOBJECT object, HOBJECT src);
int (*OrL)(HOBJECT object, HOBJECT src);
int (*NotL)(HOBJECT object, HOBJECT src);
int (*And)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Or)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Xor)(HOBJECT object, HOBJECT src0, HOBJECT src1);
int (*Not)(HOBJECT object, HOBJECT src);
int (*uAnd)(HOBJECT object, HOBJECT src);
int (*uAndNot)(HOBJECT object, HOBJECT src);
int (*uOr)(HOBJECT object, HOBJECT src);
int (*uOrNot)(HOBJECT object, HOBJECT src);
int (*uXor)(HOBJECT object, HOBJECT src);
int (*uXorNot)(HOBJECT object, HOBJECT src);
int (*SHL)(HOBJECT object, HOBJECT src, int bits);
int (*SHR)(HOBJECT object, HOBJECT src, int bits);
int (*SAL)(HOBJECT object, HOBJECT src, int bits);
int (*SAR)(HOBJECT object, HOBJECT src, int bits);
}IBigNumber;
#define BIGNUMBER_VARDECLARE
......@@ -102,80 +148,149 @@ typedef struct sIBigNumber {
#define BIGNUMBER_FUNCDECLARE(_obj, _clsid, _localstruct) \
static int _obj##_bn_GetWidth(HOBJECT object); \
static int _obj##_bn_SetWidth(HOBJECT object, int width, int signexpand); \
static int _obj##_bn_GetInt(HOBJECT object, int* pvalue); \
static int _obj##_bn_GetBits32(HOBJECT object, int index, unsigned int* pbits); \
static int _obj##_bn_SetBits32(HOBJECT object, int index, unsigned int bits); \
static int _obj##_bn_GetInt32(HOBJECT object, int* pvalue); \
static int _obj##_bn_GetInt64(HOBJECT object, long long* pvalue); \
static int _obj##_bn_GetUint32(HOBJECT object, unsigned int* pvalue); \
static int _obj##_bn_GetUint64(HOBJECT object, unsigned long long* pvalue); \
static int _obj##_bn_GetStr(HOBJECT object, int base, char* str, int buflen); \
static int _obj##_bn_AssignStr(HOBJECT object, const char* str, const char ** nstr); \
static int _obj##_bn_AssignInt(HOBJECT object, int value); \
static int _obj##_bn_AssignInt32(HOBJECT object, int value); \
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_Clone(HOBJECT object, HOBJECT src); \
static int _obj##_bn_CloneSubBits(HOBJECT object, HOBJECT src, int from, int width); \
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); \
static int _obj##_bn_Abs(HOBJECT object); \
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); \
static int _obj##_bn_SubInt(HOBJECT object, int value); \
static int _obj##_bn_Sub(HOBJECT object, HOBJECT src); \
static int _obj##_bn_MulInt(HOBJECT object, int value); \
static int _obj##_bn_Mul(HOBJECT object, HOBJECT src); \
static int _obj##_bn_DivInt(HOBJECT object, int value); \
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_SAL(HOBJECT object, int bits); \
static int _obj##_bn_SAR(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); \
static int _obj##_bn_IsLE(HOBJECT object, HOBJECT src); \
static int _obj##_bn_AssignSubBits(HOBJECT object, HOBJECT src, int from, int width); \
static int _obj##_bn_AssignU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_AssignSubBitsU(HOBJECT object, HOBJECT src, int from, int width); \
static int _obj##_bn_AddInt32(HOBJECT object, HOBJECT src, int value); \
static int _obj##_bn_SubInt32(HOBJECT object, HOBJECT src, int value); \
static int _obj##_bn_MulInt32(HOBJECT object, HOBJECT src, int value); \
static int _obj##_bn_DivInt32(HOBJECT object, HOBJECT src, int value); \
static int _obj##_bn_ModInt32(HOBJECT object, HOBJECT src, int value); \
static int _obj##_bn_AddUint32(HOBJECT object, HOBJECT src, unsigned int value); \
static int _obj##_bn_SubUint32(HOBJECT object, HOBJECT src, unsigned int value); \
static int _obj##_bn_MulUint32(HOBJECT object, HOBJECT src, unsigned int value); \
static int _obj##_bn_DivUint32(HOBJECT object, HOBJECT src, unsigned int value); \
static int _obj##_bn_ModUint32(HOBJECT object, HOBJECT src, unsigned int value); \
static int _obj##_bn_Add(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Sub(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Mul(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Div(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Mod(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_AddU(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_SubU(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_MulU(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_DivU(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_ModU(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Neg(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsGT(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsGE(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsLT(HOBJECT object, HOBJECT src); \
static const IBigNumber _obj##_bn_interface = { \
static int _obj##_bn_IsLE(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsEQ(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsNE(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsGTU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsGEU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsLTU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsLEU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsEQU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsNEU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_IsZero(HOBJECT object); \
static int _obj##_bn_IsNotZero(HOBJECT object); \
static int _obj##_bn_AndL(HOBJECT object, HOBJECT src); \
static int _obj##_bn_OrL(HOBJECT object, HOBJECT src); \
static int _obj##_bn_NotL(HOBJECT object, HOBJECT src); \
static int _obj##_bn_And(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Or(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Xor(HOBJECT object, HOBJECT src0, HOBJECT src1); \
static int _obj##_bn_Not(HOBJECT object, HOBJECT src); \
static int _obj##_bn_uAnd(HOBJECT object, HOBJECT src); \
static int _obj##_bn_uAndNot(HOBJECT object, HOBJECT src); \
static int _obj##_bn_uOr(HOBJECT object, HOBJECT src); \
static int _obj##_bn_uOrNot(HOBJECT object, HOBJECT src); \
static int _obj##_bn_uXor(HOBJECT object, HOBJECT src); \
static int _obj##_bn_uXorNot(HOBJECT object, HOBJECT src); \
static int _obj##_bn_SHL(HOBJECT object, HOBJECT src, int bits); \
static int _obj##_bn_SHR(HOBJECT object, HOBJECT src, int bits); \
static int _obj##_bn_SAL(HOBJECT object, HOBJECT src, int bits); \
static int _obj##_bn_SAR(HOBJECT object, HOBJECT src, int bits); \
static const IBigNumber _obj##_bn_interface = { \
INTERFACE_HEADER(_obj, IBigNumber, _localstruct) \
_obj##_bn_GetWidth, \
_obj##_bn_SetWidth, \
_obj##_bn_GetInt, \
_obj##_bn_GetBits32, \
_obj##_bn_SetBits32, \
_obj##_bn_GetInt32, \
_obj##_bn_GetInt64, \
_obj##_bn_GetUint32, \
_obj##_bn_GetUint64, \
_obj##_bn_GetStr, \
_obj##_bn_AssignStr, \
_obj##_bn_AssignInt, \
_obj##_bn_AssignInt32, \
_obj##_bn_AssignInt64, \
_obj##_bn_AssignUint32, \
_obj##_bn_AssignUint64, \
_obj##_bn_AssignStr, \
_obj##_bn_Clone, \
_obj##_bn_CloneSubBits, \
_obj##_bn_Assign, \
_obj##_bn_AssignSub, \
_obj##_bn_Bind, \
_obj##_bn_Abs, \
_obj##_bn_Neg, \
_obj##_bn_AddInt, \
_obj##_bn_AssignSubBits, \
_obj##_bn_AssignU, \
_obj##_bn_AssignSubBitsU, \
_obj##_bn_AddInt32, \
_obj##_bn_SubInt32, \
_obj##_bn_MulInt32, \
_obj##_bn_DivInt32, \
_obj##_bn_ModInt32, \
_obj##_bn_AddUint32, \
_obj##_bn_SubUint32, \
_obj##_bn_MulUint32, \
_obj##_bn_ModUint32, \
_obj##_bn_Add, \
_obj##_bn_SubInt, \
_obj##_bn_Sub, \
_obj##_bn_MulInt, \
_obj##_bn_Mul, \
_obj##_bn_DivInt, \
_obj##_bn_Div, \
_obj##_bn_SHL, \
_obj##_bn_SHR, \
_obj##_bn_SAL, \
_obj##_bn_SAR, \
_obj##_bn_Mod, \
_obj##_bn_AddU, \
_obj##_bn_SubU, \
_obj##_bn_MulU, \
_obj##_bn_DivU, \
_obj##_bn_ModU, \
_obj##_bn_Neg, \
_obj##_bn_IsGT, \
_obj##_bn_IsGE, \
_obj##_bn_IsLT, \
_obj##_bn_IsLE, \
_obj##_bn_IsEQ, \
_obj##_bn_IsNE, \
_obj##_bn_IsGTU, \
_obj##_bn_IsGEU, \
_obj##_bn_IsLTU, \
_obj##_bn_IsLEU, \
_obj##_bn_IsEQU, \
_obj##_bn_IsNEU, \
_obj##_bn_IsZero, \
_obj##_bn_IsNotZero, \
_obj##_bn_AndL, \
_obj##_bn_OrL, \
_obj##_bn_NotL, \
_obj##_bn_And, \
_obj##_bn_Or, \
_obj##_bn_Xor, \
_obj##_bn_Not, \
_obj##_bn_uAnd, \
_obj##_bn_uAndNot, \
_obj##_bn_uOr, \
_obj##_bn_uOrNot, \
_obj##_bn_uXor, \
_obj##_bn_And, \
_obj##_bn_Or, \
_obj##_bn_Xor, \
_obj##_bn_IsZero, \
_obj##_bn_IsNeg, \
_obj##_bn_IsEQ, \
_obj##_bn_IsLE, \
_obj##_bn_IsLT \
_obj##_bn_uXorNot, \
_obj##_bn_SHL, \
_obj##_bn_SHR, \
_obj##_bn_SAL, \
_obj##_bn_SAR, \
};
......
......@@ -37,6 +37,7 @@
#include "stdlib.h"
#include "stdio.h"
#include "object.h"
#include "string.h"
#define IMPLEMENT_GUID
#include "bignumber.h"
#undef IMPLEMENT_GUID
......@@ -151,7 +152,8 @@ static int bigint_bn_SetWidth(HOBJECT object, int width, int signexpand)
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
if (width <= 0) {
memset(pobj->buf, 0, pobj->buflen * (CELL_WIDTH/8));
for (i = 0; i < pobj->buflen; i++)
pobj->buf[i] = 0;
pobj->width = 0;
return 0;
}
......@@ -186,7 +188,28 @@ static int bigint_bn_SetWidth(HOBJECT object, int width, int signexpand)
return 0;
}
static int bigint_bn_GetInt(HOBJECT object, int* pvalue)
static int bigint_bn_GetBits32(HOBJECT object, int index, unsigned int* pbits)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
if (index < 0 || index >= pobj->buflen)
return -1;
*pbits = pobj->buf[index];
return 0;
}
static int bigint_bn_SetBits32(HOBJECT object, int index, unsigned int bits)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
if (index < 0 || index >= pobj->buflen)
return -1;
pobj->buf[index] = bits;
return 0;
}
static int bigint_bn_GetInt32(HOBJECT object, int* pvalue)
{
sBigInteger* pobj;
int width;
......@@ -208,7 +231,6 @@ static int bigint_bn_GetInt64(HOBJECT object, long long* pvalue)
width = pobj->width;
if (width < 64)
bigint_bn_SetWidth(object, 64, 1);
((unsigned int *)pvalue)[0] = pobj->buf[0];
((unsigned int *)pvalue)[1] = pobj->buf[1];
if (width < 64)
......@@ -217,6 +239,36 @@ static int bigint_bn_GetInt64(HOBJECT object, long long* pvalue)
return 0;
}
static int bigint_bn_GetUint32(HOBJECT object, int* pvalue)
{
sBigInteger* pobj;
int width;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
if (width < 32)
bigint_bn_SetWidth(object, 32, 0);
*(unsigned int*)pvalue = pobj->buf[0];
if (width < 32)
bigint_bn_SetWidth(object, width, 0);
return 0;
}
static int bigint_bn_GetUint64(HOBJECT object, long long* pvalue)
{
sBigInteger* pobj;
int width;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
if (width < 64)
bigint_bn_SetWidth(object, 64, 0);
((unsigned int*)pvalue)[0] = pobj->buf[0];
((unsigned int*)pvalue)[1] = pobj->buf[1];
if (width < 64)
bigint_bn_SetWidth(object, width, 0);
return 0;
}
static int bigint_bn_GetStr(HOBJECT object, int base, char* str, int buflen)
{
int i;
......@@ -247,6 +299,56 @@ static int bigint_bn_GetStr(HOBJECT object, int base, char* str, int buflen)
return 0;
}
static int bigint_bn_AssignInt32(HOBJECT object, int value)
{
sBigInteger* pobj;
int i, width;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
bigint_bn_SetWidth(object, 32, 0);
pobj->buf[0] = *(unsigned int*)&value;
bigint_bn_SetWidth(object, width, 1);
return 0;
}
static int bigint_bn_AssignInt64(HOBJECT object, long long value)
{
sBigInteger* pobj;
int i, width;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
bigint_bn_SetWidth(object, 64, 0);
pobj->buf[0] = (*(unsigned long long*)(&value)) & CELL_MASK;
pobj->buf[1] = ((*(unsigned long long*)(&value)) >> CELL_WIDTH) & CELL_MASK;
bigint_bn_SetWidth(object, width, 1);
return 0;
}
static int bigint_bn_AssignUint32(HOBJECT object, unsigned int value)
{
sBigInteger* pobj;
int i, width;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
bigint_bn_SetWidth(object, 32, 0);
pobj->buf[0] = *(unsigned int*)&value;
bigint_bn_SetWidth(object, width, 0);
return 0;
}
static int bigint_bn_AssignUint64(HOBJECT object, unsigned long long value)
{
sBigInteger* pobj;
int i, width;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
bigint_bn_SetWidth(object, 64, 0);
pobj->buf[0] = (*(unsigned long long*)(&value)) & CELL_MASK;
pobj->buf[1] = ((*(unsigned long long*)(&value)) >> CELL_WIDTH) & CELL_MASK;
bigint_bn_SetWidth(object, width, 0);
return 0;
}
enum TOKEN_STATE {
TOKEN_INITIAL,
TOKEN_NUM,
......@@ -379,33 +481,81 @@ lastnum:
return 0;
}
static int bigint_bn_AssignInt(HOBJECT object, int value)
static int _obj##_bn_Clone(HOBJECT object, HOBJECT src); \
static int _obj##_bn_CloneSubBits(HOBJECT object, HOBJECT src, int from, int width); \
static int _obj##_bn_Assign(HOBJECT object, HOBJECT src); \
static int _obj##_bn_AssignSubBits(HOBJECT object, HOBJECT src, int from, int width); \
static int _obj##_bn_AssignU(HOBJECT object, HOBJECT src); \
static int _obj##_bn_AssignSubBitsU(HOBJECT object, HOBJECT src, int from, int width); \
static int bigint_bn_Clone(HOBJECT object, HOBJECT src)
{
sBigInteger* pobj;
int i, width;
sBigInteger* psrc;
int i;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
bigint_bn_SetWidth(object, 32, 0);
pobj->buf[0] = *(unsigned int*)&value;
if (!objectIsClass(src, CLSID_BIGINTEGER)) {
return -1;
}
psrc = (sBigInteger*)objectThis(src);
if (pobj->buflen < psrc->buflen) {
unsigned int* buf;
buf = (unsigned int*)malloc(psrc->buflen * (CELL_WIDTH / 8));
if (buf == NULL)
return -2;
free(pobj->buf);
pobj->buf = buf;
}
pobj->buflen = psrc->buflen;
pobj->width = psrc->width;
for (i = 0; i < pobj->buflen; i++)
pobj->buf[i] = psrc->buf[i];
return 0;
}
static int bigint_bn_CloneSubBits(HOBJECT object, HOBJECT src, int from, int width)
{
bigint_bn_Assign(object, src);
bigint_bn_SAR(object, from);
bigint_bn_SetWidth(object, width, 1);
return 0;
}
static int bigint_bn_AssignInt64(HOBJECT object, long long value)
{
static int bigint_bn_Assign(HOBJECT object, HOBJECT src)
{
sBigInteger* pobj;
int i, width;
sBigInteger* psrc;
int i;
pobj = (sBigInteger*)objectThis(object);
width = pobj->width;
bigint_bn_SetWidth(object, 64, 0);
pobj->buf[0] = (*(unsigned long long *)(&value)) & CELL_MASK;
pobj->buf[1] = ((*(unsigned long long*)(&value)) >> CELL_WIDTH) & CELL_MASK;
if (!objectIsClass(src, CLSID_BIGINTEGER)) {
return -1;
}
psrc = (sBigInteger*)objectThis(src);
if (pobj->buflen < psrc->buflen) {
unsigned int* buf;
buf = (unsigned int*)malloc(psrc->buflen * (CELL_WIDTH / 8));
if (buf == NULL)
return -2;
free(pobj->buf);
pobj->buf = buf;
}
pobj->buflen = psrc->buflen;
pobj->width = psrc->width;
for (i = 0; i < pobj->buflen; i++)
pobj->buf[i] = psrc->buf[i];
return 0;
}
static int bigint_bn_AssignSubBits(HOBJECT object, HOBJECT src, int from, int width)
{
bigint_bn_Assign(object, src);
bigint_bn_SAR(object, from);
bigint_bn_SetWidth(object, width, 1);
return 0;
}
static int bigint_bn_Assign(HOBJECT object, HOBJECT src)
{
static int bigint_bn_AssignU(HOBJECT object, HOBJECT src)
{
sBigInteger* pobj;
sBigInteger* psrc;
int i;
......@@ -429,7 +579,7 @@ static int bigint_bn_Assign(HOBJECT object, HOBJECT src)
return 0;
}
static int bigint_bn_AssignSub(HOBJECT object, HOBJECT src, int from, int width)
static int bigint_bn_AssignSubBitsU(HOBJECT object, HOBJECT src, int from, int width)
{
bigint_bn_Assign(object, src);
bigint_bn_SAR(object, from);
......@@ -437,32 +587,64 @@ static int bigint_bn_AssignSub(HOBJECT object, HOBJECT src, int from, int width)
return 0;
}
static int bigint_bn_Bind(HOBJECT object, HOBJECT src)
static int bigint_bn_AddInt32(HOBJECT object, int value)
{
unsigned long long temp;
int ind;
unsigned int v;
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
temp = pobj->buf[0];
temp += *(unsigned int*)&value;
pobj->buf[0] = temp & CELL_MASK;
temp >>= CELL_WIDTH;
ind = 1;
v = 0;
if (value < 0)
v = CELL_MASK;
while (temp != 0) {
if (ind >= pobj->buflen)
break;
temp += v;
temp += pobj->buf[ind];
pobj->buf[ind] = temp & CELL_MASK;
temp >>= CELL_WIDTH;
ind++;
}
return 0;
}
static int bigint_bn_SubInt32(HOBJECT object, int value)
{
return bigint_bn_AddInt(object, -value);
}
static int bigint_bn_MulInt32(HOBJECT object, int value)
{
int width = bigint_bn_GetWidth(src);
bigint_bn_SetWidth(object, bigint_bn_GetWidth(object) + width, 0);
bigint_bn_SHL(object, width);
bigint_bn_Or(object, src);
sBigInteger** temp;
temp = bigintegerCreate(32);
bigint_bn_AssignInt(temp, value);
bigint_bn_Mul(object, temp);
objectRelease(temp);
return 0;
}
static int bigint_bn_Abs(HOBJECT object)
static int bigint_bn_DivInt32(HOBJECT object, int value)
{
if (bigint_bn_IsNeg(object))
bigint_bn_Neg(object);
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_Neg(HOBJECT object)
{
/* 反码加一 */
bigint_bn_Not(object);
bigint_bn_AddInt(object, 1);
static int bigint_bn_ModInt32(HOBJECT object, int value)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_AddInt(HOBJECT object, int value)
{
static int bigint_bn_AddUint32(HOBJECT object, unsigned int value)
{
unsigned long long temp;
int ind;
unsigned int v;
......@@ -488,7 +670,36 @@ static int bigint_bn_AddInt(HOBJECT object, int value)
return 0;
}
static int bigint_bn_Add(HOBJECT object, HOBJECT src)
static int bigint_bn_SubUint32(HOBJECT object, unsigned int value)
{
return bigint_bn_AddInt(object, -value);
}
static int bigint_bn_MulUint32(HOBJECT object, unsigned int value)
{
sBigInteger** temp;
temp = bigintegerCreate(32);
bigint_bn_AssignInt(temp, value);
bigint_bn_Mul(object, temp);
objectRelease(temp);
return 0;
}
static int bigint_bn_DivUint32(HOBJECT object, unsigned int value)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_ModUint32(HOBJECT object, unsigned int value)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_Add(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
unsigned long long temp;
sBigInteger* pobj;
......@@ -519,12 +730,7 @@ static int bigint_bn_Add(HOBJECT object, HOBJECT src)
return 0;
}
static int bigint_bn_SubInt(HOBJECT object, int value)
{
return bigint_bn_AddInt(object, -value);
}
static int bigint_bn_Sub(HOBJECT object, HOBJECT src)
static int bigint_bn_Sub(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger** temp;
temp = bigintegerCreate(64);
......@@ -535,18 +741,111 @@ static int bigint_bn_Sub(HOBJECT object, HOBJECT src)
return 0;
}
static int bigint_bn_MulInt(HOBJECT object, int value)
static int bigint_bn_Mul(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger* pobj;
sBigInteger* psrc;
unsigned int* buf;
int i, j;
int widthobj, widthsrc, wobj;
pobj = (sBigInteger*)objectThis(object);
if (!objectIsClass(src, CLSID_BIGINTEGER)) {
return -1;
}
psrc = (sBigInteger*)objectThis(src);
buf = (unsigned int*)malloc(pobj->buflen * CELL_WIDTH/8);
if (buf == NULL)
return -2;
widthobj = pobj->width;
widthsrc = psrc->width;
wobj = pobj->buflen * CELL_WIDTH;
bigint_bn_SetWidth(object, wobj, 1);
if (widthsrc < wobj)
bigint_bn_SetWidth(src, wobj, 1);
for (i = 0; i < pobj->buflen; i++)
buf[i] = 0;
for (i = 0; i < pobj->buflen; i++) {
unsigned long long addin;
addin = 0;
for (j = 0; j < psrc->buflen; j++) {
unsigned long long m0, m1;
if (i + j >= pobj->buflen)
break;
m0 = pobj->buf[i];
m1 = psrc->buf[j];
m0 = m0 * m1 + addin + buf[i+j];
buf[i + j] = m0 & CELL_MASK;
addin = m0 >> CELL_WIDTH;
}
}
free(pobj->buf);
pobj->buf = buf;
bigint_bn_SetWidth(object, widthobj, 0);
if (widthsrc < wobj)
bigint_bn_SetWidth(src, widthsrc, 0);
return 0;
}
static int bigint_bn_Div(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_Mod(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_AddU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
unsigned long long temp;
sBigInteger* pobj;
sBigInteger* psrc;
int widthobj, widthsrc, wobj;
int i;
pobj = (sBigInteger*)objectThis(object);
if (!objectIsClass(src, CLSID_BIGINTEGER)) {
return -1;
}
psrc = (sBigInteger*)objectThis(src);
widthobj = pobj->width;
widthsrc = psrc->width;
wobj = pobj->buflen * CELL_WIDTH;
bigint_bn_SetWidth(object, wobj, 1);
if (widthsrc < wobj)
bigint_bn_SetWidth(src, wobj, 1);
temp = 0;
for (i = 0; i < pobj->buflen; i++) {
temp += pobj->buf[i];
temp += psrc->buf[i];
pobj->buf[i] = temp & CELL_MASK;
temp >>= CELL_WIDTH;
}
bigint_bn_SetWidth(object, widthobj, 0);
if (widthsrc < wobj)
bigint_bn_SetWidth(src, widthsrc, 0);
return 0;
}
static int bigint_bn_SubU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger** temp;
temp = bigintegerCreate(32);
bigint_bn_AssignInt(temp, value);
bigint_bn_Mul(object, temp);
temp = bigintegerCreate(64);
bigint_bn_Assign(temp, src);
bigint_bn_Neg(temp);
bigint_bn_Add(object, temp);
objectRelease(temp);
return 0;
}
static int bigint_bn_Mul(HOBJECT object, HOBJECT src)
{
static int bigint_bn_MulU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger* pobj;
sBigInteger* psrc;
unsigned int* buf;
......@@ -557,7 +856,7 @@ static int bigint_bn_Mul(HOBJECT object, HOBJECT src)
return -1;
}
psrc = (sBigInteger*)objectThis(src);
buf = (unsigned int*)malloc(pobj->buflen * CELL_WIDTH/8);
buf = (unsigned int*)malloc(pobj->buflen * CELL_WIDTH / 8);
if (buf == NULL)
return -2;
......@@ -579,7 +878,7 @@ static int bigint_bn_Mul(HOBJECT object, HOBJECT src)
break;
m0 = pobj->buf[i];
m1 = psrc->buf[j];
m0 = m0 * m1 + addin + buf[i+j];
m0 = m0 * m1 + addin + buf[i + j];
buf[i + j] = m0 & CELL_MASK;
addin = m0 >> CELL_WIDTH;
}
......@@ -592,20 +891,27 @@ static int bigint_bn_Mul(HOBJECT object, HOBJECT src)
return 0;
}
static int bigint_bn_DivInt(HOBJECT object, int value)
{
static int bigint_bn_DivU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_Div(HOBJECT object, HOBJECT src)
{
static int bigint_bn_ModU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
sBigInteger* pobj;
pobj = (sBigInteger*)objectThis(object);
return 0;
}
static int bigint_bn_Neg(HOBJECT object)
{
/* 反码加一 */
bigint_bn_Not(object);
bigint_bn_AddInt(object, 1);
return 0;
}
static int bigint_bn_SHL(HOBJECT object, int bits)
{
sBigInteger* pobj;
......
......@@ -65,10 +65,10 @@ static int hdl4se_print_all_signal(IHDL4SEDetector** detector, const char * path
int width;
const char* name;
count = objectCall0(detector, GetSignalCount);
(*pwidth)++;
for (i = 0; i < count; i++) {
objectCall3(detector, GetSignalInfo, i, &name, &width);
printf("%s/%s, %d\n", pathname, name, width);
*pwidth += (width + 31) / 32;
}
return 0;
}
......@@ -188,7 +188,7 @@ static int hdl4se_print_signal_list_detector()
int main(int argc, char* argv[])
{
int width;
int count;
int count, unitcount;
IHDL4SEUnit** sim_unit;
sim = hdl4sesimCreateSimulator();
topmodule = hdl4seCreateMain(NULL, "", "main");
......@@ -196,12 +196,14 @@ int main(int argc, char* argv[])
objectCall1(sim, SetTopModule, topmodule);
objectCall1(sim, AddDevice, gui);
objectCall1(sim, SetReset, 0);
unitcount = 0;
hdl4sedetectorTraversal(sim, hdl4se_print_all_signal, "", &unitcount);
count = 0;
pSignalFile = fopen("digitled.csv", "w");
signal_value = bigintegerCreate(32);
hdl4se_print_signal_list_header();
hdl4sedetectorTraversal(sim, hdl4se_init_signal_list, "", &count);
printf("want %d, inited %d\n", signal_list_count, count);
printf("want %d, inited %d, total %d units\n", signal_list_count, count, unitcount);
objectQueryInterface(sim, IID_HDL4SEUNIT, (void**) &sim_unit);
do {
//objectCall0(sim, RunClockTick);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册