|
| 1 | +/** |
| 2 | + * An ArrayInt represents an integer larger than what can be represented in |
| 3 | + * classical JavaScript. |
| 4 | + * |
| 5 | + * The values stored in data must be in the range `[0, 0xffffffff]`. |
| 6 | + * |
| 7 | + * ```ts |
| 8 | + * { sign: 1, data: [ 42 ] } // = 42 |
| 9 | + * { sign: -1, data: [ 42 ] } // = -42 |
| 10 | + * { sign: -1, data: [ 5, 42 ] } // = -1 * (5 * 2**32 + 42) |
| 11 | + * { sign: -1, data: [ 1, 5, 42 ] } // = -1 * (1 * 2**64 + 5 * 2**32 + 42) |
| 12 | + * ``` |
| 13 | + */ |
| 14 | +export interface ArrayInt { |
| 15 | + /** |
| 16 | + * Sign of the represented number |
| 17 | + */ |
| 18 | + sign: -1 | 1 |
| 19 | + /** |
| 20 | + * Value of the number, must only contain numbers in the range [0, 0xffffffff] |
| 21 | + */ |
| 22 | + data: number[] |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Add two ArrayInt |
| 27 | + * @internal |
| 28 | + */ |
| 29 | +export function addArrayIntToNew(arrayIntA: ArrayInt, arrayIntB: ArrayInt): ArrayInt { |
| 30 | + if (arrayIntA.sign !== arrayIntB.sign) { |
| 31 | + return substractArrayIntToNew(arrayIntA, { |
| 32 | + sign: -arrayIntB.sign as -1 | 1, |
| 33 | + data: arrayIntB.data |
| 34 | + }) |
| 35 | + } |
| 36 | + const data = [] |
| 37 | + let reminder = 0 |
| 38 | + const dataA = arrayIntA.data |
| 39 | + const dataB = arrayIntB.data |
| 40 | + for (let indexA = dataA.length - 1, indexB = dataB.length - 1; indexA >= 0 || indexB >= 0; --indexA, --indexB) { |
| 41 | + const vA = indexA >= 0 ? dataA[indexA]! : 0 |
| 42 | + const vB = indexB >= 0 ? dataB[indexB]! : 0 |
| 43 | + const current = vA + vB + reminder |
| 44 | + data.push(current >>> 0) |
| 45 | + reminder = ~~(current / 0x100000000) |
| 46 | + } |
| 47 | + if (reminder !== 0) { |
| 48 | + data.push(reminder) |
| 49 | + } |
| 50 | + return { sign: arrayIntA.sign, data: data.reverse() } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Add one to a given positive ArrayInt |
| 55 | + * @internal |
| 56 | + */ |
| 57 | +export function addOneToPositiveArrayInt(arrayInt: ArrayInt): ArrayInt { |
| 58 | + arrayInt.sign = 1 // handling case { sign: -1, data: [0,...,0] } |
| 59 | + const data = arrayInt.data |
| 60 | + for (let index = data.length - 1; index >= 0; --index) { |
| 61 | + if (data[index] === 0xffffffff) { |
| 62 | + data[index] = 0 |
| 63 | + } else { |
| 64 | + data[index] += 1 |
| 65 | + return arrayInt |
| 66 | + } |
| 67 | + } |
| 68 | + data.unshift(1) |
| 69 | + return arrayInt |
| 70 | +} |
| 71 | + |
| 72 | +/** @internal */ |
| 73 | +function isStrictlySmaller(dataA: number[], dataB: number[]): boolean { |
| 74 | + const maxLength = Math.max(dataA.length, dataB.length) |
| 75 | + for (let index = 0; index < maxLength; ++index) { |
| 76 | + const indexA = index + dataA.length - maxLength |
| 77 | + const indexB = index + dataB.length - maxLength |
| 78 | + const vA = indexA >= 0 ? dataA[indexA]! : 0 |
| 79 | + const vB = indexB >= 0 ? dataB[indexB]! : 0 |
| 80 | + if (vA < vB) return true |
| 81 | + if (vA > vB) return false |
| 82 | + } |
| 83 | + return false |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Substract two ArrayInt |
| 88 | + * @internal |
| 89 | + */ |
| 90 | +export function substractArrayIntToNew(arrayIntA: ArrayInt, arrayIntB: ArrayInt): ArrayInt { |
| 91 | + if (arrayIntA.sign !== arrayIntB.sign) { |
| 92 | + return addArrayIntToNew(arrayIntA, { sign: -arrayIntB.sign as -1 | 1, data: arrayIntB.data }) |
| 93 | + } |
| 94 | + const dataA = arrayIntA.data |
| 95 | + const dataB = arrayIntB.data |
| 96 | + if (isStrictlySmaller(dataA, dataB)) { |
| 97 | + const out = substractArrayIntToNew(arrayIntB, arrayIntA) |
| 98 | + out.sign = -out.sign as -1 | 1 |
| 99 | + return out |
| 100 | + } |
| 101 | + const data = [] |
| 102 | + let reminder = 0 |
| 103 | + for (let indexA = dataA.length - 1, indexB = dataB.length - 1; indexA >= 0 || indexB >= 0; --indexA, --indexB) { |
| 104 | + const vA = indexA >= 0 ? dataA[indexA]! : 0 |
| 105 | + const vB = indexB >= 0 ? dataB[indexB]! : 0 |
| 106 | + const current = vA - vB - reminder |
| 107 | + data.push(current >>> 0) |
| 108 | + reminder = current < 0 ? 1 : 0 |
| 109 | + } |
| 110 | + return { sign: arrayIntA.sign, data: data.reverse() } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Trim uneeded zeros in ArrayInt |
| 115 | + * and uniform notation for zero: {sign: 1, data: [0]} |
| 116 | + */ |
| 117 | +export function trimArrayIntInplace(arrayInt: ArrayInt) { |
| 118 | + const data = arrayInt.data |
| 119 | + let firstNonZero = 0 |
| 120 | + // eslint-disable-next-line no-empty |
| 121 | + for (; firstNonZero !== data.length && data[firstNonZero] === 0; ++firstNonZero) {} |
| 122 | + if (firstNonZero === data.length) { |
| 123 | + // only zeros |
| 124 | + arrayInt.sign = 1 |
| 125 | + arrayInt.data = [0] |
| 126 | + return arrayInt |
| 127 | + } |
| 128 | + data.splice(0, firstNonZero) |
| 129 | + return arrayInt |
| 130 | +} |
| 131 | + |
| 132 | +// Helpers specific to 64 bits versions |
| 133 | + |
| 134 | +/** @internal */ |
| 135 | +export type ArrayInt64 = Required<ArrayInt> & { data: [number, number] } |
| 136 | + |
| 137 | +/** |
| 138 | + * We only accept safe integers here |
| 139 | + * @internal |
| 140 | + */ |
| 141 | +export function fromNumberToArrayInt64(out: ArrayInt64, n: number): ArrayInt64 { |
| 142 | + if (n < 0) { |
| 143 | + const posN = -n |
| 144 | + out.sign = -1 |
| 145 | + out.data[0] = ~~(posN / 0x100000000) |
| 146 | + out.data[1] = posN >>> 0 |
| 147 | + } else { |
| 148 | + out.sign = 1 |
| 149 | + out.data[0] = ~~(n / 0x100000000) |
| 150 | + out.data[1] = n >>> 0 |
| 151 | + } |
| 152 | + return out |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Substract two ArrayInt of 64 bits on 64 bits. |
| 157 | + * With arrayIntA - arrayIntB >= 0 |
| 158 | + * @internal |
| 159 | + */ |
| 160 | +export function substractArrayInt64(out: ArrayInt64, arrayIntA: ArrayInt64, arrayIntB: ArrayInt64): ArrayInt64 { |
| 161 | + const lowA = arrayIntA.data[1] |
| 162 | + const highA = arrayIntA.data[0] |
| 163 | + const signA = arrayIntA.sign |
| 164 | + const lowB = arrayIntB.data[1] |
| 165 | + const highB = arrayIntB.data[0] |
| 166 | + const signB = arrayIntB.sign |
| 167 | + |
| 168 | + // Requirement: arrayIntA - arrayIntB >= 0 |
| 169 | + out.sign = 1 |
| 170 | + |
| 171 | + if (signA === 1 && signB === -1) { |
| 172 | + // Operation is a simple sum of arrayIntA + abs(arrayIntB) |
| 173 | + const low = lowA + lowB |
| 174 | + const high = highA + highB + (low > 0xffffffff ? 1 : 0) |
| 175 | + out.data[0] = high >>> 0 |
| 176 | + out.data[1] = low >>> 0 |
| 177 | + return out |
| 178 | + } |
| 179 | + // signA === -1 with signB === 1 is impossible given: arrayIntA - arrayIntB >= 0 |
| 180 | + |
| 181 | + // Operation is a substraction |
| 182 | + let lowFirst = lowA |
| 183 | + let highFirst = highA |
| 184 | + let lowSecond = lowB |
| 185 | + let highSecond = highB |
| 186 | + if (signA === -1) { |
| 187 | + lowFirst = lowB |
| 188 | + highFirst = highB |
| 189 | + lowSecond = lowA |
| 190 | + highSecond = highA |
| 191 | + } |
| 192 | + let reminderLow = 0 |
| 193 | + let low = lowFirst - lowSecond |
| 194 | + if (low < 0) { |
| 195 | + reminderLow = 1 |
| 196 | + low = low >>> 0 |
| 197 | + } |
| 198 | + out.data[0] = highFirst - highSecond - reminderLow |
| 199 | + out.data[1] = low |
| 200 | + return out |
| 201 | +} |
0 commit comments