a[2] | a[1] |
a[0] |
|
X | b[1] |
b[0] |
|
--------------------------------------------------------------------------------------- |
|||
a[2]*b[0] |
a[1]*b[0] |
a[0]*b[0] |
|
a[2]*b[1] |
a[1]*b[1] |
a[0]*b[1] |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void bigint_mul_i(bigint *dest, const bigint *src1, const bigint *src2) { u8 res; memset (dest->dats, 0, dest->len* sizeof (u4)); for (u4 i=0;i<src1->len;++i) { for (u4 j=0;j<src2->len;++j) { res=(u8)(src1->dats[i])*src2->dats[j]; bigint_addu4(dest, (u4)res, i+j); bigint_addu4(dest, (u4)(res>>32), i+j+1); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void bigint_mulu4(bigint *dest, u4 src) { u8 res; u4 tmp[dest->len]; memcpy (tmp, dest->dats, dest->len* sizeof (u4)); memset (dest->dats, 0, dest->len* sizeof (u4)); for (u4 i=0;i<dest->len;++i) { res=(u8)(tmp[i])*src; bigint_addu4(dest, (u4)res, i); bigint_addu4(dest, (u4)(res>>32), i+1); } } |
1 2 3 4 5 6 7 8 9 10 |
int bigint_sign( const bigint *xi) { if (xi->len==0) return 0; if (xi->dats[xi->len-1] & 0x80000000) return -1; for (u4 i=0;i<xi->len;++i) { if (xi->dats[i]!=0) return 1; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void bigint_mul(bigint *dest, const bigint *src1, const bigint *src2) { int s1=bigint_sign(src1)<0; int s2=bigint_sign(src2)<0; bigint src1n, src2n; if (s1) { bigint_createnegcopy(&src1n, src1); src1=&src1n; } if (s2) { bigint_createnegcopy(&src2n, src2); src2=&src2n; } bigint_mul_i(dest, src1, src2); if (s1^s2)bigint_neg(dest); if (s1)bigint_release(&src1n); if (s2)bigint_release(&src2n); } |
BigInteger를 구현해보자 - 7. 나누기 (2) | 2009.04.25 |
---|---|
BigInteger를 구현해보자 - 6. 크기비교 (4) | 2009.04.21 |
BigInteger를 구현해보자 - 5. 시프트 연산 (0) | 2009.04.12 |
BigInteger를 구현해보자 - 3. 음수의 표현, 뺄셈 (4) | 2009.03.29 |
BigInteger를 구현해보자 - 2. 정의와 생성, 파괴, 덧셈 (8) | 2009.03.29 |
BigInteger를 구현해보자 - 1. BigInteger의 개요 (2) | 2009.03.29 |
댓글 영역