MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / fxBigInt_sqr

Function fxBigInt_sqr

xs/sources/xsBigInt.c:1671–1716  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1669}
1670
1671txBigInt *fxBigInt_sqr(txMachine* the, txBigInt *r, txBigInt *a)
1672{
1673 int i, j, t;
1674 txU4 *ap, *rp;
1675 txU8 uv, t1, t2, t3, ai;
1676 txU4 c, cc;
1677 txU4 overflow = 0; /* overflow flag of 'u' */
1678
1679 if (r == NULL)
1680 r = fxBigInt_alloc(the, a->size * 2);
1681 fxBigInt_fill0(r);
1682 t = a->size;
1683 ap = a->data;
1684 rp = r->data;
1685
1686 for (i = 0; i < t - 1; i++) {
1687 uv = (txU8)ap[i] * ap[i] + rp[i * 2];
1688 rp[i * 2] = mxBigIntLowWord(uv);
1689 c = mxBigIntHighWord(uv);
1690 cc = 0;
1691 ai = ap[i];
1692 for (j = i + 1; j < t; j++) {
1693 int k = i + j;
1694 t1 = ai * ap[j];
1695 t2 = t1 + c + ((txU8)cc << mxBigIntWordSize); /* 'cc:c' must be <= 2(b-1) so no overflow here */
1696 t3 = t1 + t2;
1697 uv = t3 + rp[k];
1698 cc = t3 < t1 || uv < t3;
1699 c = (txU4)mxBigIntHighWord(uv);
1700 rp[k] = mxBigIntLowWord(uv);
1701 }
1702 c += overflow;
1703 rp[i + t] = c; /* c = u */
1704 overflow = cc || c < overflow;
1705 }
1706 /* the last loop */
1707 uv = (txU8)ap[i] * ap[i] + rp[i * 2];
1708 rp[i * 2] = mxBigIntLowWord(uv);
1709 rp[i + t] = mxBigIntHighWord(uv) + overflow;
1710
1711 /* remove leading 0s */
1712 for (i = 2*t; --i > 0 && rp[i] == 0;)
1713 ;
1714 r->size = i + 1;
1715 return(r);
1716}
1717
1718txBigInt *fxBigInt_div(txMachine* the, txBigInt *q, txBigInt *a, txBigInt *b)
1719{

Callers 3

fxBigInt_mod_squareFunction · 0.85
mont_squareFunction · 0.85
fxBigInt_expFunction · 0.85

Calls 2

fxBigInt_allocFunction · 0.85
fxBigInt_fill0Function · 0.85

Tested by

no test coverage detected