bignumber.c 29.8 KB
Newer Older
饶先宏's avatar
饶先宏 已提交
1
/*
饶先宏's avatar
饶先宏 已提交
2
** HDL4SE: 软件Verilog综合仿真平台
饶先宏's avatar
饶先宏 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** LCOM: 轻量级组件对象模型
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** * Redistributions of source code must retain the above copyright notice,
**   this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright notice,
**   this list of conditions and the following disclaimer in the documentation
**   and/or other materials provided with the distribution.
** * The name of the author may be used to endorse or promote products
**   derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
** THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* bignumber.c
    202105201110: rxh, initial version
*/

#include "stdlib.h" 
#include "stdio.h"
#include "object.h"
饶先宏's avatar
饶先宏 已提交
40
#include "string.h"
饶先宏's avatar
饶先宏 已提交
41 42
#define IMPLEMENT_GUID
#include "bignumber.h"
饶先宏's avatar
饶先宏 已提交
43 44
#undef IMPLEMENT_GUID

饶先宏's avatar
饶先宏 已提交
45
#define CELL_WIDTH 32
饶先宏's avatar
饶先宏 已提交
46
#define CELL_MASK 0xffffffff
饶先宏's avatar
饶先宏 已提交
47 48 49 50 51 52 53

typedef struct _sBigInteger {
	OBJECT_HEADER
	INTERFACE_DECLARE(IBigNumber)
	BIGNUMBER_VARDECLARE

	int buflen;
饶先宏's avatar
饶先宏 已提交
54
	int width;
饶先宏's avatar
饶先宏 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	unsigned int* buf;

}sBigInteger;

OBJECT_FUNCDECLARE(bigint, CLSID_BIGINTEGER);
BIGNUMBER_FUNCDECLARE(bigint, CLSID_BIGINTEGER, sBigInteger);

OBJECT_FUNCIMPL(bigint, sBigInteger, CLSID_BIGINTEGER);

QUERYINTERFACE_BEGIN(bigint, CLSID_BIGINTEGER)
QUERYINTERFACE_ITEM(IID_BIGNUMBER, IBigNumber, sBigInteger)
QUERYINTERFACE_END

static const char* bigintModuleInfo()
{
	return "1.0.0-20210520.1355 Big integer library";
}

static int bigintCreate(const PARAMITEM* pParams, int paramcount, HOBJECT* pObject)
{
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
76
	int i;
饶先宏's avatar
饶先宏 已提交
77 78 79 80 81 82 83
	pobj = (sBigInteger*)malloc(sizeof(sBigInteger));
	if (pobj == NULL)
		return -1;
	*pObject = 0;
	BIGNUMBER_VARINIT(pobj, CLSID_BIGINTEGER);
	INTERFACE_INIT(IBigNumber, pobj, bigint, bn);

饶先宏's avatar
饶先宏 已提交
84 85 86 87 88 89 90 91 92
	pobj->width = 64;
	for (i = 0; i < paramcount; i++) {
		if (pParams[i].name == PARAMID_BIGINTEGERWIDTH)
			pobj->width = pParams[i].i32value;
	}

	pobj->buflen = (pobj->width + CELL_WIDTH - 1) / CELL_WIDTH;
	if (pobj->buflen < 2)
		pobj->buflen = 2;
饶先宏's avatar
饶先宏 已提交
93
	pobj->buf = malloc(pobj->buflen * sizeof(unsigned int));
饶先宏's avatar
饶先宏 已提交
94 95 96 97 98
	if (pobj->buf == NULL)
		return -1;

	for (i = 0;i<pobj->buflen;i++)
		pobj->buf[i] = 0;
饶先宏's avatar
饶先宏 已提交
99 100 101 102 103 104 105 106 107 108

	/* 返回生成的对象 */
	OBJECT_RETURN_GEN(bigint, pobj, pObject, CLSID_BIGINTEGER);
	return EIID_OK;
}

static int actualwidth(unsigned int n)
{
	unsigned int c = 1;
	int ret = 1;
饶先宏's avatar
饶先宏 已提交
109
	if (n & 0x80000000)
饶先宏's avatar
饶先宏 已提交
110
		return CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
111
	while (c <= n) {
饶先宏's avatar
饶先宏 已提交
112 113 114
		c <<= 1;
		ret++;
	}
饶先宏's avatar
饶先宏 已提交
115
	return ret-1;
饶先宏's avatar
饶先宏 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
}

static void bigintDestroy(HOBJECT object)
{
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	free(pobj->buf);
	free(pobj);
}

static int bigintValid(HOBJECT object)
{
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	if (pobj == NULL)
		return 0;
	if (pobj->buflen < 2)
		return 0;
饶先宏's avatar
饶先宏 已提交
134 135
	if (pobj->buflen * CELL_WIDTH < pobj->width)
		return 0;
饶先宏's avatar
饶先宏 已提交
136 137 138 139 140
	if (pobj->buf == NULL)
		return 0;
	return 1;
}

饶先宏's avatar
饶先宏 已提交
141
static int bigint_bn_GetWidth(HOBJECT object)
饶先宏's avatar
饶先宏 已提交
142 143 144 145
{
	int i;
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
146
	return pobj->width;
饶先宏's avatar
饶先宏 已提交
147 148
}

饶先宏's avatar
饶先宏 已提交
149
static int bigint_bn_SetWidth(HOBJECT object, int width, int signexpand)
饶先宏's avatar
饶先宏 已提交
150
{
饶先宏's avatar
饶先宏 已提交
151
	int i, bc,blen, sign;
饶先宏's avatar
饶先宏 已提交
152 153
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
154
	if (width <= 0) {
饶先宏's avatar
饶先宏 已提交
155 156
		for (i = 0; i < pobj->buflen; i++)
			pobj->buf[i] = 0;
饶先宏's avatar
饶先宏 已提交
157
		pobj->width = 0;
饶先宏's avatar
饶先宏 已提交
158 159
		return 0;
	}
饶先宏's avatar
饶先宏 已提交
160 161 162
	sign = 0;
	if (signexpand) {
		bc = pobj->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
163
		sign = pobj->buf[bc] & (1 << ((pobj->width-1) & (CELL_WIDTH-1)));
饶先宏's avatar
饶先宏 已提交
164 165 166 167 168 169
	}

	blen = (width + CELL_WIDTH - 1) / CELL_WIDTH;
	if (pobj->buflen < blen) {
		unsigned int * buf;
		buf = (unsigned int *)malloc(blen * (CELL_WIDTH / 8));
饶先宏's avatar
饶先宏 已提交
170 171 172 173 174 175 176 177
		if (buf == NULL)
			return -1;
		for (i = 0; i < pobj->buflen; i++)
			buf[i] = pobj->buf[i];
		pobj->buflen = blen;
		free(pobj->buf);
		pobj->buf = buf;
	}
饶先宏's avatar
饶先宏 已提交
178 179 180 181
	
	blen = pobj->width / CELL_WIDTH;
	for (i = blen+1; i < pobj->buflen; i++) {
		pobj->buf[i] = sign ? CELL_MASK : 0;
饶先宏's avatar
饶先宏 已提交
182
	}
饶先宏's avatar
饶先宏 已提交
183 184 185 186 187
	if (sign)
		pobj->buf[blen] |= CELL_MASK << (pobj->width & (CELL_WIDTH - 1));
	else
		pobj->buf[blen] &= CELL_MASK >> (CELL_WIDTH - (pobj->width & (CELL_WIDTH-1)));
	pobj->width = width;
饶先宏's avatar
饶先宏 已提交
188 189 190
	return 0;
}

饶先宏's avatar
饶先宏 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
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)
饶先宏's avatar
饶先宏 已提交
213 214
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
215
	int width;
饶先宏's avatar
饶先宏 已提交
216
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
217 218 219 220 221 222
	width = pobj->width;
	if (width < 32)
		bigint_bn_SetWidth(object, 32, 1);
	*(unsigned int*)pvalue = pobj->buf[0];
	if (width < 32)
		bigint_bn_SetWidth(object, width, 0);
饶先宏's avatar
饶先宏 已提交
223 224 225 226 227 228
	return 0; 
}

static int bigint_bn_GetInt64(HOBJECT object, long long* pvalue)
{
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
229
	int width;
饶先宏's avatar
饶先宏 已提交
230
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
231 232 233 234 235 236 237 238
	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)
		bigint_bn_SetWidth(object, width, 0);

饶先宏's avatar
饶先宏 已提交
239 240 241
	return 0;
}

饶先宏's avatar
饶先宏 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
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;
}

饶先宏's avatar
饶先宏 已提交
272 273 274 275 276 277 278 279 280
static int bigint_bn_GetStr(HOBJECT object, int base, char* str, int buflen)
{ 
	int i;
	int bc, ac;
	char fmt[10];
	char buf[10];
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	str[0] = 0;
饶先宏's avatar
饶先宏 已提交
281
	bc = bigint_bn_GetWidth(object);
饶先宏's avatar
饶先宏 已提交
282 283 284 285
	if (bc == 0) {
		strcpy(str, "0");
		return 0;
	}
饶先宏's avatar
饶先宏 已提交
286
	sprintf(str, "%d'h", bc);
饶先宏's avatar
饶先宏 已提交
287
	i = ((bc + (CELL_WIDTH-1)) / CELL_WIDTH) - 1;
饶先宏's avatar
饶先宏 已提交
288 289 290 291 292
	ac = actualwidth(pobj->buf[i]);
	ac = (ac + 7) / 8;
	sprintf(fmt, "%%%dx_", ac);
	sprintf(buf, fmt, pobj->buf[i]);
	strcat(str, buf);
饶先宏's avatar
饶先宏 已提交
293
	for (i = ((bc + (CELL_WIDTH-1)) / CELL_WIDTH)-2; i >= 0; i--) {
饶先宏's avatar
饶先宏 已提交
294 295 296 297 298 299 300 301
		sprintf(buf, "%08x", pobj->buf[i]);
		strcat(str, buf);
		if (i > 0)
			strcat(str, "_");
	}
	return 0; 
}

饶先宏's avatar
饶先宏 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
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;
}

饶先宏's avatar
饶先宏 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
enum TOKEN_STATE {
	TOKEN_INITIAL,
	TOKEN_NUM,
	TOKEN_BASE,
	TOKEN_BIN,
	TOKEN_OCT,
	TOKEN_DEC,
	TOKEN_HEX
};

static int bigint_bn_AssignStr(HOBJECT object, const char* str, const char **nstr)
{ 
	enum TOKEN_STATE state;
	const char* strt = str;
	int numvalid = 0;
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	state = TOKEN_INITIAL;
	while (*strt != 0) {
		int ch = *strt;
		switch (state) {
		case TOKEN_INITIAL: {
			if (ch == '\'') {
				state = TOKEN_BASE;
			}
			else if (ch >= '0' && ch <= '9') {
				bigint_bn_AssignInt(object, ch - '0');
				numvalid = 1;
				state = TOKEN_NUM;
			}
		} break;
		case TOKEN_NUM: {
			if (ch >= '0' && ch <= '9') {
				bigint_bn_MulInt(object, 10);
				bigint_bn_AddInt(object, ch - '0');
			}
			else if (ch == '\'') {
				state = TOKEN_BASE;
				numvalid = 0;
			}
			else {
				goto lastnum;
			}
		}break;
		case TOKEN_BASE: {
			if (ch == 'b' || ch == 'B') {
				bigint_bn_AssignInt(object, 0);
				state = TOKEN_BIN;
			}
			else if (ch == 'h' || ch == 'H') {
				bigint_bn_AssignInt(object, 0);
				state = TOKEN_HEX;
			}
			else if (ch == 'o' || ch == 'O') {
				bigint_bn_AssignInt(object, 0);
				state = TOKEN_OCT;
			}
			else if (ch == 'd' || ch == 'D') {
				bigint_bn_AssignInt(object, 0);
				state = TOKEN_DEC;
			}
			else if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {

			}
			else {
				return -1;
			}
		}break;
		case TOKEN_BIN: {
			if (ch == '0' || ch == '1') {
				bigint_bn_MulInt(object, 2);
				bigint_bn_AddInt(object, ch - '0');
				numvalid = 1;
			}
			else if (ch == '_') {
			}
			else {
				goto lastnum;
			}
		}break;
		case TOKEN_OCT: {
			if (ch >= '0' && ch <= '7') {
				bigint_bn_MulInt(object, 8);
				bigint_bn_AddInt(object, ch - '0');
				numvalid = 1;
			}
			else if (ch == '_') {
			}
			else {
				goto lastnum;
			}
		}break;
		case TOKEN_DEC: {
			if (ch >= '0' && ch <= '9') {
				bigint_bn_MulInt(object, 10);
				bigint_bn_AddInt(object, ch - '0');
				numvalid = 1;
			}
			else {
				goto lastnum;
			}
		}break;
		case TOKEN_HEX: {
			if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
				numvalid = 1;
				bigint_bn_MulInt(object, 16);
				if (ch > '0' && ch <= '9')
					bigint_bn_AddInt(object, ch - '0');
				else if (ch >= 'a' && ch <= 'f')
					bigint_bn_AddInt(object, ch - 'a' + 10);
				else if (ch >= 'A' && ch <= 'F')
					bigint_bn_AddInt(object, ch - 'A' + 10);
			}
			else if (ch == '_') {
			}
			else {
				goto lastnum;
			}

		}break;
		}
		strt++;
	}
	if (numvalid == 0)
		return -1;
lastnum:
饶先宏's avatar
饶先宏 已提交
478 479
	if (nstr != NULL)
		*nstr = strt;
饶先宏's avatar
饶先宏 已提交
480
	bigint_bn_SetWidth(object, pobj->width, 1);
饶先宏's avatar
饶先宏 已提交
481 482 483
	return 0;
}

饶先宏's avatar
饶先宏 已提交
484 485 486 487 488 489 490 491
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)
饶先宏's avatar
饶先宏 已提交
492 493
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
494 495
	sBigInteger* psrc;
	int i;
饶先宏's avatar
饶先宏 已提交
496
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	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);
饶先宏's avatar
饶先宏 已提交
520
	bigint_bn_SetWidth(object, width, 1);
饶先宏's avatar
饶先宏 已提交
521 522 523
	return 0;
}

饶先宏's avatar
饶先宏 已提交
524 525
static int bigint_bn_Assign(HOBJECT object, HOBJECT src)
{
饶先宏's avatar
饶先宏 已提交
526
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
527 528
	sBigInteger* psrc;
	int i;
饶先宏's avatar
饶先宏 已提交
529
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	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);
饶先宏's avatar
饶先宏 已提交
553
	bigint_bn_SetWidth(object, width, 1);
饶先宏's avatar
饶先宏 已提交
554 555 556
	return 0;
}

饶先宏's avatar
饶先宏 已提交
557 558
static int bigint_bn_AssignU(HOBJECT object, HOBJECT src)
{
饶先宏's avatar
饶先宏 已提交
559 560
	sBigInteger* pobj;
	sBigInteger* psrc;
饶先宏's avatar
饶先宏 已提交
561
	int i;
饶先宏's avatar
饶先宏 已提交
562 563 564 565 566
	pobj = (sBigInteger*)objectThis(object);
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return -1;
	}
	psrc = (sBigInteger*)objectThis(src);
饶先宏's avatar
饶先宏 已提交
567 568 569 570 571 572 573 574
	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;
	}
饶先宏's avatar
饶先宏 已提交
575
	pobj->buflen = psrc->buflen;
饶先宏's avatar
饶先宏 已提交
576
	pobj->width = psrc->width;
饶先宏's avatar
饶先宏 已提交
577 578
	for (i = 0; i < pobj->buflen; i++)
		pobj->buf[i] = psrc->buf[i];
饶先宏's avatar
饶先宏 已提交
579 580 581
	return 0;
}

饶先宏's avatar
饶先宏 已提交
582
static int bigint_bn_AssignSubBitsU(HOBJECT object, HOBJECT src, int from, int width)
饶先宏's avatar
饶先宏 已提交
583 584
{
	bigint_bn_Assign(object, src);
饶先宏's avatar
饶先宏 已提交
585 586
	bigint_bn_SAR(object, from);
	bigint_bn_SetWidth(object, width, 1);
饶先宏's avatar
饶先宏 已提交
587 588 589
	return 0;
}

饶先宏's avatar
饶先宏 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
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)
饶先宏's avatar
饶先宏 已提交
623
{
饶先宏's avatar
饶先宏 已提交
624 625 626 627 628
	sBigInteger** temp;
	temp = bigintegerCreate(32);
	bigint_bn_AssignInt(temp, value);
	bigint_bn_Mul(object, temp);
	objectRelease(temp);
饶先宏's avatar
饶先宏 已提交
629 630 631
	return 0;
}

饶先宏's avatar
饶先宏 已提交
632
static int bigint_bn_DivInt32(HOBJECT object, int value)
饶先宏's avatar
饶先宏 已提交
633
{
饶先宏's avatar
饶先宏 已提交
634 635
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
636 637 638
	return 0;
}

饶先宏's avatar
饶先宏 已提交
639 640 641 642
static int bigint_bn_ModInt32(HOBJECT object, int value)
{
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
643 644 645
	return 0;
}

饶先宏's avatar
饶先宏 已提交
646 647
static int bigint_bn_AddUint32(HOBJECT object, unsigned int value)
{
饶先宏's avatar
饶先宏 已提交
648 649 650
	unsigned long long temp;
	int ind;
	unsigned int v;
饶先宏's avatar
饶先宏 已提交
651 652 653
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	temp = pobj->buf[0];
饶先宏's avatar
饶先宏 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666
	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;
饶先宏's avatar
饶先宏 已提交
667
		temp >>= CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
668
		ind++;
饶先宏's avatar
饶先宏 已提交
669 670 671 672
	}
	return 0;
}

饶先宏's avatar
饶先宏 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
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)
饶先宏's avatar
饶先宏 已提交
703
{ 
饶先宏's avatar
饶先宏 已提交
704
	unsigned long long temp;
饶先宏's avatar
饶先宏 已提交
705
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
706 707 708
	sBigInteger* psrc;
	int widthobj, widthsrc, wobj;
	int i;
饶先宏's avatar
饶先宏 已提交
709
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
	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);
饶先宏's avatar
饶先宏 已提交
730 731 732
	return 0;
}

饶先宏's avatar
饶先宏 已提交
733
static int bigint_bn_Sub(HOBJECT object, HOBJECT src0, HOBJECT src1)
饶先宏's avatar
饶先宏 已提交
734
{ 
饶先宏's avatar
饶先宏 已提交
735 736 737 738 739 740
	sBigInteger** temp;
	temp = bigintegerCreate(64);
	bigint_bn_Assign(temp, src);
	bigint_bn_Neg(temp);
	bigint_bn_Add(object, temp);
	objectRelease(temp);
饶先宏's avatar
饶先宏 已提交
741 742 743
	return 0;
}

饶先宏's avatar
饶先宏 已提交
744
static int bigint_bn_Mul(HOBJECT object, HOBJECT src0, HOBJECT src1)
饶先宏's avatar
饶先宏 已提交
745
{ 
饶先宏's avatar
饶先宏 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	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)
{
饶先宏's avatar
饶先宏 已提交
838
	sBigInteger** temp;
饶先宏's avatar
饶先宏 已提交
839 840 841 842
	temp = bigintegerCreate(64);
	bigint_bn_Assign(temp, src);
	bigint_bn_Neg(temp);
	bigint_bn_Add(object, temp);
饶先宏's avatar
饶先宏 已提交
843
	objectRelease(temp);
饶先宏's avatar
饶先宏 已提交
844 845 846
	return 0;
}

饶先宏's avatar
饶先宏 已提交
847 848
static int bigint_bn_MulU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
饶先宏's avatar
饶先宏 已提交
849
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
850 851 852
	sBigInteger* psrc;
	unsigned int* buf;
	int i, j;
饶先宏's avatar
饶先宏 已提交
853
	int widthobj, widthsrc, wobj;
饶先宏's avatar
饶先宏 已提交
854
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
855 856 857 858
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return -1;
	}
	psrc = (sBigInteger*)objectThis(src);
饶先宏's avatar
饶先宏 已提交
859
	buf = (unsigned int*)malloc(pobj->buflen * CELL_WIDTH / 8);
饶先宏's avatar
饶先宏 已提交
860 861
	if (buf == NULL)
		return -2;
饶先宏's avatar
饶先宏 已提交
862 863 864 865 866 867 868 869

	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);

饶先宏's avatar
饶先宏 已提交
870 871 872 873 874 875 876 877 878 879 880
	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];
饶先宏's avatar
饶先宏 已提交
881
			m0 = m0 * m1 + addin + buf[i + j];
饶先宏's avatar
饶先宏 已提交
882
			buf[i + j] = m0 & CELL_MASK;
饶先宏's avatar
饶先宏 已提交
883 884 885 886 887
			addin = m0 >> CELL_WIDTH;
		}
	}
	free(pobj->buf);
	pobj->buf = buf;
饶先宏's avatar
饶先宏 已提交
888 889 890
	bigint_bn_SetWidth(object, widthobj, 0);
	if (widthsrc < wobj)
		bigint_bn_SetWidth(src, widthsrc, 0);
饶先宏's avatar
饶先宏 已提交
891 892 893
	return 0;
}

饶先宏's avatar
饶先宏 已提交
894 895
static int bigint_bn_DivU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
饶先宏's avatar
饶先宏 已提交
896 897 898 899 900
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	return 0;
}

饶先宏's avatar
饶先宏 已提交
901 902
static int bigint_bn_ModU(HOBJECT object, HOBJECT src0, HOBJECT src1)
{
饶先宏's avatar
饶先宏 已提交
903 904 905 906 907
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	return 0;
}

饶先宏's avatar
饶先宏 已提交
908 909 910 911 912 913 914
static int bigint_bn_Neg(HOBJECT object)
{
	/* 反码加一 */
	bigint_bn_Not(object);
	bigint_bn_AddInt(object, 1);
	return 0;
}
饶先宏's avatar
饶先宏 已提交
915 916 917
static int bigint_bn_SHL(HOBJECT object, int bits)
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
918 919
	int ifrom, ito, i;
	unsigned long long current, next;
饶先宏's avatar
饶先宏 已提交
920
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
921 922 923 924
	if (bits == 0)
		return 0;
	if (bits < 0)
		return bigint_bn_SHR(object, -bits);
饶先宏's avatar
饶先宏 已提交
925 926
	ito = bits / CELL_WIDTH;
	bits -= ito * CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
927
	ifrom = 0;
饶先宏's avatar
饶先宏 已提交
928 929 930 931 932 933 934 935 936 937 938
	for (i = pobj->buflen-1; i>=0; i--) {
		current = 0;
		if (i >= ito) {
			current = pobj->buf[i - ito];
			current <<= CELL_WIDTH;
		}
		if (i >= ito+1) {
			next = pobj->buf[i - ito -1];
			current |= next;
		}
		current >>= CELL_WIDTH - bits;
饶先宏's avatar
饶先宏 已提交
939
		pobj->buf[i] = (unsigned int)(current & CELL_MASK);
饶先宏's avatar
饶先宏 已提交
940 941 942 943 944
	}
	return 0;
}

static int bigint_bn_SHR(HOBJECT object, int bits)
饶先宏's avatar
饶先宏 已提交
945
{
饶先宏's avatar
饶先宏 已提交
946
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
947 948 949
	int bc;
	int ifrom, ito, i, zerolen;
	unsigned long long current, next;
饶先宏's avatar
饶先宏 已提交
950
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
951 952 953 954 955
	if (bits == 0)
		return 0;
	if (bits < 0)
		return bigint_bn_SHL(object, -bits);
	ito = 0;
饶先宏's avatar
饶先宏 已提交
956
	ifrom = bits / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
957
	if (ifrom >= pobj->buflen) {
饶先宏's avatar
饶先宏 已提交
958
		memset(pobj->buf, 0, pobj->buflen * (CELL_WIDTH/8));
饶先宏's avatar
饶先宏 已提交
959 960
		return 0;
	}
饶先宏's avatar
饶先宏 已提交
961
	bits -= ifrom * CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
962 963
	if (ifrom+1 < pobj->buflen) {
		current = pobj->buf[ifrom+1];
饶先宏's avatar
饶先宏 已提交
964
		current <<= CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
965 966 967 968 969 970 971
	}
	else {
		current = 0;
	}
	current |= pobj->buf[ifrom];
	current >>= bits;
	for (i = ifrom + 1; i < pobj->buflen-1; i++) {
饶先宏's avatar
饶先宏 已提交
972
		pobj->buf[ito++] = (unsigned int)(current & CELL_MASK);
饶先宏's avatar
饶先宏 已提交
973
		current >>= CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
974
		next = pobj->buf[i + 1];
饶先宏's avatar
饶先宏 已提交
975
		next <<= CELL_WIDTH-bits;
饶先宏's avatar
饶先宏 已提交
976 977
		current |= next;
	}
饶先宏's avatar
饶先宏 已提交
978
	pobj->buf[ito++] = (unsigned int)(current & CELL_MASK);
饶先宏's avatar
饶先宏 已提交
979 980
	while (ito < pobj->buflen)
		pobj->buf[ito++] = 0;
饶先宏's avatar
饶先宏 已提交
981 982 983
	return 0;
}

饶先宏's avatar
饶先宏 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
static int bigint_bn_SAL(HOBJECT object, int bits)
{
	return bigint_bn_SHL(object, bits);
}

static int bigint_bn_SAR(HOBJECT object, int bits)
{
	sBigInteger* pobj;
	int bc, sign;
	int ifrom, ito, i, zerolen;
	unsigned long long current, next;
	pobj = (sBigInteger*)objectThis(object);
	if (bits == 0)
		return 0;
	if (bits < 0)
		return bigint_bn_SHL(object, -bits);
	bc = pobj->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1001
	sign = pobj->buf[bc] & (1 << ((pobj->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	ito = 0;
	ifrom = bits / CELL_WIDTH;
	if (ifrom >= pobj->buflen) {
		for (i = 0;i<pobj->buflen;i++)
			pobj->buf[i] = sign ? CELL_MASK : 0;
		return 0;
	}
	bits -= ifrom * CELL_WIDTH;
	if (ifrom + 1 < pobj->buflen) {
		current = pobj->buf[ifrom + 1];
		current <<= CELL_WIDTH;
	}
	else {
		current = 0;
	}
	current |= pobj->buf[ifrom];
	current >>= bits;
	for (i = ifrom + 1; i < pobj->buflen - 1; i++) {
		pobj->buf[ito++] = (unsigned int)(current & CELL_MASK);
		current >>= CELL_WIDTH;
		next = pobj->buf[i + 1];
		next <<= CELL_WIDTH - bits;
		current |= next;
	}
	next = sign ? CELL_MASK : 0;
	next <<= CELL_WIDTH - bits;
	current |= next;
	pobj->buf[ito++] = (unsigned int)(current & CELL_MASK);
	while (ito < pobj->buflen)
		pobj->buf[ito++] = sign ? CELL_MASK : 0;
	return 0;
}


饶先宏's avatar
饶先宏 已提交
1036 1037
static int bigint_bn_Not(HOBJECT object)
{
饶先宏's avatar
饶先宏 已提交
1038
	int i;
饶先宏's avatar
饶先宏 已提交
1039 1040
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1041 1042
	for (i = 0; i < pobj->buflen; i++)
		pobj->buf[i] = ~pobj->buf[i];
饶先宏's avatar
饶先宏 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	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)
{
饶先宏's avatar
饶先宏 已提交
1062 1063 1064 1065 1066 1067 1068 1069
	sBigInteger* pobj;
	pobj = (sBigInteger*)objectThis(object);
	return 0;
}

static int bigint_bn_And(HOBJECT object, HOBJECT src)
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1070 1071
	sBigInteger* psrc;
	int i;
饶先宏's avatar
饶先宏 已提交
1072
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return -1;
	}
	psrc = (sBigInteger*)objectThis(src);
	for (i = 0; i < pobj->buflen && i < psrc->buflen; i++) {
		pobj->buf[i] &= psrc->buf[i];
	}
	for (; i < pobj->buflen; i++) {
		pobj->buf[i] = 0;
	}
饶先宏's avatar
饶先宏 已提交
1083 1084 1085 1086 1087
	return 0;
}

static int bigint_bn_Or(HOBJECT object, HOBJECT src)
{ 
饶先宏's avatar
饶先宏 已提交
1088 1089
	sBigInteger* pobj;
	sBigInteger* psrc;
饶先宏's avatar
饶先宏 已提交
1090
	int i;
饶先宏's avatar
饶先宏 已提交
1091 1092 1093 1094 1095
	pobj = (sBigInteger*)objectThis(object);
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return -1;
	}
	psrc = (sBigInteger*)objectThis(src);
饶先宏's avatar
饶先宏 已提交
1096
	for (i = 0; i < pobj->buflen && i < psrc->buflen; i++) {
饶先宏's avatar
饶先宏 已提交
1097 1098 1099 1100 1101 1102 1103
		pobj->buf[i] |= psrc->buf[i];
	}
	return 0;
}

static int bigint_bn_Xor(HOBJECT object, HOBJECT src)
{
饶先宏's avatar
饶先宏 已提交
1104
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1105 1106
	sBigInteger* psrc;
	int i;
饶先宏's avatar
饶先宏 已提交
1107
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return -1;
	}
	psrc = (sBigInteger*)objectThis(src);
	for (i = 0; i < pobj->buflen && i < psrc->buflen; i++) {
		pobj->buf[i] ^= psrc->buf[i];
	}
	for (; i < pobj->buflen; i++) {
		pobj->buf[i] ^= 0;
	}
饶先宏's avatar
饶先宏 已提交
1118 1119 1120 1121 1122 1123
	return 0;
}

static int bigint_bn_IsZero(HOBJECT object)
{
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1124
	int i;
饶先宏's avatar
饶先宏 已提交
1125
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1126 1127 1128 1129
	for (i = 0; i < pobj->buflen; i++)
		if (pobj->buf[i] != 0)
			return 0;
	return 1;
饶先宏's avatar
饶先宏 已提交
1130 1131 1132 1133 1134
}

static int bigint_bn_IsNeg(HOBJECT object)
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1135
	int bc, sign;
饶先宏's avatar
饶先宏 已提交
1136
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1137
	bc = pobj->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1138
	sign = pobj->buf[bc] & (1 << ((pobj->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1139
	return sign?1:0;
饶先宏's avatar
饶先宏 已提交
1140 1141 1142 1143 1144
}

static int bigint_bn_IsEQ(HOBJECT object, HOBJECT src)
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1145 1146 1147
	sBigInteger* psrc;
	int i;
	int bc, sign, signsrc;
饶先宏's avatar
饶先宏 已提交
1148
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1149 1150 1151 1152 1153
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return 0;
	}
	psrc = (sBigInteger*)objectThis(src);
	bc = pobj->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1154
	sign = pobj->buf[bc] & (1 << ((pobj->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1155
	bc = psrc->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1156
	signsrc = psrc->buf[bc] & (1 << ((psrc->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	if (sign != signsrc) {
		return 0;
	}
	for (i = 0; i < pobj->buflen && i < psrc->buflen; i++) {
		if (pobj->buf[i] != psrc->buf[i])
			return 0;
	}
	for (; i < pobj->buflen; i++) {
		if (sign == 0 && pobj->buf[i] != 0)
			return 0;
		else if (sign != 0 && pobj->buf[i] != CELL_MASK)
			return 0;
	}
	for (; i < psrc->buflen; i++) {
		if (sign == 0 && psrc->buf[i] != 0)
			return 0;
		else if (sign != 0 && psrc->buf[i] != CELL_MASK)
			return 0;
	}
	return 1;
饶先宏's avatar
饶先宏 已提交
1177 1178 1179 1180 1181
}

static int bigint_bn_IsLE(HOBJECT object, HOBJECT src)
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1182 1183 1184 1185
	sBigInteger* psrc;
	int i;
	int bc, sign, signsrc;

饶先宏's avatar
饶先宏 已提交
1186
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1187 1188 1189 1190 1191
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return 0;
	}
	psrc = (sBigInteger*)objectThis(src);
	bc = pobj->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1192
	sign = pobj->buf[bc] & (1 << ((pobj->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1193
	bc = psrc->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1194
	signsrc = psrc->buf[bc] & (1 << ((psrc->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	if (sign == 0 && signsrc != 0) {
		return 0;
	}
	else if (sign != 0 && signsrc == 0) {
		return 1;
	}
	
	bc = pobj->buflen;
	if (bc < psrc->buflen)
		bc = psrc->buflen;
	for (i = bc - 1; i >= pobj->buflen; i--) {
		if (sign != 0) {
			if (psrc->buf[i] != 0xffffffff)
				return 0;
		}
		else {
			if (psrc->buf[0] != 0)
				return 1;
		}
	}
	for (; i >= psrc->buflen; i--) {
		if (sign != 0) {
			if (pobj->buf[i] != 0xffffffff)
				return 1;
		}
		else {
			if (pobj->buf[0] != 0)
				return 0;
		}
	}
	for (; i >= 0; i--) {
		if (sign != 0) {
			if (pobj->buf[i] > psrc->buf[i])
				return 1;
			else if (pobj->buf[i] < psrc->buf[i])
				return 0;
		}
		else {
			if (pobj->buf[i] < psrc->buf[i])
				return 1;
			if (pobj->buf[i] > psrc->buf[i])
				return 0;
		}
	}
	return 1;
饶先宏's avatar
饶先宏 已提交
1240 1241 1242 1243 1244
}

static int bigint_bn_IsLT(HOBJECT object, HOBJECT src)
{ 
	sBigInteger* pobj;
饶先宏's avatar
饶先宏 已提交
1245 1246 1247 1248
	sBigInteger* psrc;
	int i;
	int bc, sign, signsrc;

饶先宏's avatar
饶先宏 已提交
1249
	pobj = (sBigInteger*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
1250 1251 1252 1253 1254
	if (!objectIsClass(src, CLSID_BIGINTEGER)) {
		return 0;
	}
	psrc = (sBigInteger*)objectThis(src);
	bc = pobj->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1255
	sign = pobj->buf[bc] & (1 << ((pobj->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1256
	bc = psrc->width / CELL_WIDTH;
饶先宏's avatar
饶先宏 已提交
1257
	signsrc = psrc->buf[bc] & (1 << ((psrc->width-1) & (CELL_WIDTH - 1)));
饶先宏's avatar
饶先宏 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	if (sign == 0 && signsrc != 0) {
		return 0;
	}
	else if (sign != 0 && signsrc == 0) {
		return 1;
	}

	bc = pobj->buflen;
	if (bc < psrc->buflen)
		bc = psrc->buflen;
	for (i = bc - 1; i >= pobj->buflen; i--) {
		if (sign != 0) {
			if (psrc->buf[i] != 0xffffffff)
				return 0;
		}
		else {
			if (psrc->buf[0] != 0)
				return 1;
		}
	}
	for (; i >= psrc->buflen; i--) {
		if (sign != 0) {
			if (pobj->buf[i] != 0xffffffff)
				return 1;
		}
		else {
			if (pobj->buf[0] != 0)
				return 0;
		}
	}
	for (; i >= 0; i--) {
		if (sign != 0) {
			if (pobj->buf[i] > psrc->buf[i])
				return 1;
			else if (pobj->buf[i] < psrc->buf[i])
				return 0;
		}
		else {
			if (pobj->buf[i] < psrc->buf[i])
				return 1;
			if (pobj->buf[i] > psrc->buf[i])
				return 0;
		}
	}
饶先宏's avatar
饶先宏 已提交
1302 1303 1304
	return 0;
}

饶先宏's avatar
饶先宏 已提交
1305
IBigNumber** bigintegerCreate(int width)
饶先宏's avatar
饶先宏 已提交
1306 1307 1308
{
	int ret;
	IBigNumber** bn;
饶先宏's avatar
饶先宏 已提交
1309
	PARAMITEM param;
饶先宏's avatar
饶先宏 已提交
1310
	A_u_t_o_registor_bigint();
饶先宏's avatar
饶先宏 已提交
1311 1312 1313
	param.name = PARAMID_BIGINTEGERWIDTH;
	param.i32value = width;
	ret = objectCreateEx(CLSID_BIGINTEGER, &param, 1, IID_BIGNUMBER, (const void**)&bn);
饶先宏's avatar
饶先宏 已提交
1314 1315
	return bn;
}