* Multiply this value with other one * @this {!UInt64} * @param {!Int64|!UInt64|number|string} other Other value * @returns {!UInt64} Multiplication of two values
(other)
| 1524 | * @returns {!UInt64} Multiplication of two values |
| 1525 | */ |
| 1526 | mul (other) { |
| 1527 | if (this.isZero) { |
| 1528 | return UINT64_ZERO |
| 1529 | } |
| 1530 | if (!Int64.isInt64(other) && !UInt64.isUInt64(other)) { |
| 1531 | other = UInt64.fromValue(other) |
| 1532 | } |
| 1533 | |
| 1534 | if (other.isZero) { |
| 1535 | return UINT64_ZERO |
| 1536 | } |
| 1537 | if (this.eq(UINT64_MIN)) { |
| 1538 | return other.isOdd ? UINT64_MIN : UINT64_ZERO |
| 1539 | } |
| 1540 | if (other.eq(UINT64_MIN)) { |
| 1541 | return this.isOdd ? UINT64_MIN : UINT64_ZERO |
| 1542 | } |
| 1543 | |
| 1544 | if (this.isNegative) { |
| 1545 | if (other.isNegative) { |
| 1546 | return this.neg().mul(other.neg()) |
| 1547 | } else { |
| 1548 | return this.neg().mul(other).neg() |
| 1549 | } |
| 1550 | } else if (other.isNegative) { |
| 1551 | return this.mul(other.neg()).neg() |
| 1552 | } |
| 1553 | |
| 1554 | // If both longs are small, use float multiplication |
| 1555 | if (this.lt(UINT64_TWO_PWR_24) && other.lt(UINT64_TWO_PWR_24)) { |
| 1556 | return UInt64.fromNumber(this.toNumber() * other.toNumber()) |
| 1557 | } |
| 1558 | |
| 1559 | // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. |
| 1560 | // We can skip products that would overflow. |
| 1561 | |
| 1562 | let a48 = this.high >>> 16 |
| 1563 | let a32 = this.high & 0xFFFF |
| 1564 | let a16 = this.low >>> 16 |
| 1565 | let a00 = this.low & 0xFFFF |
| 1566 | |
| 1567 | let b48 = other.high >>> 16 |
| 1568 | let b32 = other.high & 0xFFFF |
| 1569 | let b16 = other.low >>> 16 |
| 1570 | let b00 = other.low & 0xFFFF |
| 1571 | |
| 1572 | let c48 = 0 |
| 1573 | let c32 = 0 |
| 1574 | let c16 = 0 |
| 1575 | let c00 = 0 |
| 1576 | c00 += a00 * b00 |
| 1577 | c16 += c00 >>> 16 |
| 1578 | c00 &= 0xFFFF |
| 1579 | c16 += a16 * b00 |
| 1580 | c32 += c16 >>> 16 |
| 1581 | c16 &= 0xFFFF |
| 1582 | c16 += a00 * b16 |
| 1583 | c32 += c16 >>> 16 |