|
| 1 | +import { IBitwiseOps, Long } from './types'; |
| 2 | +import { BitwiseNumber } from './number'; |
| 3 | + |
| 4 | +export const BitwiseLongAlloc: IBitwiseOps<Long> = { |
| 5 | + and(...args: Long[]): Long { |
| 6 | + return new Uint32Array( |
| 7 | + args.reduce( |
| 8 | + (a, b) => [a[0] & b[0], a[1] & b[1]], |
| 9 | + [0xffffffff, 0xffffffff] |
| 10 | + ) |
| 11 | + ); |
| 12 | + }, |
| 13 | + |
| 14 | + or(...args: Long[]): Long { |
| 15 | + return new Uint32Array( |
| 16 | + args.reduce((a, b) => [a[0] | b[0], a[1] | b[1]], [0, 0]) |
| 17 | + ); |
| 18 | + }, |
| 19 | + |
| 20 | + xor(a: Long, b: Long): Long { |
| 21 | + return new Uint32Array([a[0] ^ b[0], a[1] ^ b[1]]); |
| 22 | + }, |
| 23 | + |
| 24 | + not(a: Long): Long { |
| 25 | + return new Uint32Array([~a[0] >>> 0, ~a[1] >>> 0]); |
| 26 | + }, |
| 27 | + |
| 28 | + rotLeft(a: Long, shift: number): Long { |
| 29 | + const rotation = shift & 63; |
| 30 | + if (rotation === 0) { |
| 31 | + return new Uint32Array(a); |
| 32 | + } |
| 33 | + |
| 34 | + if (rotation < 32) { |
| 35 | + const lowToHigh = a[0] >>> (32 - rotation); |
| 36 | + const highToLow = a[1] >>> (32 - rotation); |
| 37 | + return new Uint32Array([ |
| 38 | + ((a[0] << rotation) | highToLow) >>> 0, |
| 39 | + ((a[1] << rotation) | lowToHigh) >>> 0, |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + const actualRotation = rotation - 32; |
| 44 | + const lowToHigh = a[0] << actualRotation; |
| 45 | + const highToLow = a[1] << actualRotation; |
| 46 | + return new Uint32Array([ |
| 47 | + ((a[1] >>> (32 - actualRotation)) | highToLow) >>> 0, |
| 48 | + ((a[0] >>> (32 - actualRotation)) | lowToHigh) >>> 0, |
| 49 | + ]); |
| 50 | + }, |
| 51 | + |
| 52 | + rotRight(a: Long, shift: number): Long { |
| 53 | + const rotation = shift & 63; |
| 54 | + if (rotation === 0) { |
| 55 | + return new Uint32Array(a); |
| 56 | + } |
| 57 | + |
| 58 | + if (rotation < 32) { |
| 59 | + const lowToHigh = a[0] << (32 - rotation); |
| 60 | + const highToLow = a[1] << (32 - rotation); |
| 61 | + return new Uint32Array([ |
| 62 | + ((a[0] >>> rotation) | highToLow) >>> 0, |
| 63 | + ((a[1] >>> rotation) | lowToHigh) >>> 0, |
| 64 | + ]); |
| 65 | + } |
| 66 | + |
| 67 | + const actualRotation = rotation - 32; |
| 68 | + return new Uint32Array([ |
| 69 | + (a[1] >>> actualRotation) >>> 0, |
| 70 | + (a[0] >>> actualRotation) >>> 0, |
| 71 | + ]); |
| 72 | + }, |
| 73 | + |
| 74 | + cardinality(a: Long): number { |
| 75 | + return BitwiseNumber.cardinality(a[0]) + BitwiseNumber.cardinality(a[1]); |
| 76 | + }, |
| 77 | + |
| 78 | + decompose(a: Long): Long[] { |
| 79 | + const result: Long[] = []; |
| 80 | + let remainingLow = a[0]; |
| 81 | + let remainingHigh = a[1]; |
| 82 | + |
| 83 | + for (let bit = 1; remainingLow; bit <<= 1) { |
| 84 | + if (remainingLow & bit) { |
| 85 | + result.push(new Uint32Array([bit, 0])); |
| 86 | + remainingLow ^= bit; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + for (let bit = 1; remainingHigh; bit <<= 1) { |
| 91 | + if (remainingHigh & bit) { |
| 92 | + result.push(new Uint32Array([0, bit])); |
| 93 | + remainingHigh ^= bit; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return result; |
| 98 | + }, |
| 99 | +} as IBitwiseOps<Long>; |
| 100 | + |
| 101 | +const TEMP_AND = new Uint32Array(2); |
| 102 | +const TEMP_OR = new Uint32Array(2); |
| 103 | +const TEMP_XOR = new Uint32Array(2); |
| 104 | +const TEMP_NOT = new Uint32Array(2); |
| 105 | +const TEMP_ROT_LEFT = new Uint32Array(2); |
| 106 | +const TEMP_ROT_RIGHT = new Uint32Array(2); |
| 107 | + |
| 108 | +export const BitwiseLong: IBitwiseOps<Long> = { |
| 109 | + and(...args: Long[]): Long { |
| 110 | + TEMP_AND[0] = args.reduce((a, b) => a & b[0], 0xffffffff); |
| 111 | + TEMP_AND[1] = args.reduce((a, b) => a & b[1], 0xffffffff); |
| 112 | + return TEMP_AND; |
| 113 | + }, |
| 114 | + |
| 115 | + or(...args: Long[]): Long { |
| 116 | + TEMP_OR[0] = args.reduce((a, b) => a | b[0], 0); |
| 117 | + TEMP_OR[1] = args.reduce((a, b) => a | b[1], 0); |
| 118 | + return TEMP_OR; |
| 119 | + }, |
| 120 | + |
| 121 | + xor(a: Long, b: Long): Long { |
| 122 | + TEMP_XOR[0] = a[0] ^ b[0]; |
| 123 | + TEMP_XOR[1] = a[1] ^ b[1]; |
| 124 | + return TEMP_XOR; |
| 125 | + }, |
| 126 | + |
| 127 | + not(a: Long): Long { |
| 128 | + TEMP_NOT[0] = ~a[0] >>> 0; |
| 129 | + TEMP_NOT[1] = ~a[1] >>> 0; |
| 130 | + return TEMP_NOT; |
| 131 | + }, |
| 132 | + |
| 133 | + rotLeft(a: Long, shift: number): Long { |
| 134 | + const rotation = shift & 63; |
| 135 | + if (rotation === 0) { |
| 136 | + TEMP_ROT_LEFT.set(a); |
| 137 | + return TEMP_ROT_LEFT; |
| 138 | + } |
| 139 | + |
| 140 | + if (rotation < 32) { |
| 141 | + const lowToHigh = a[0] >>> (32 - rotation); |
| 142 | + const highToLow = a[1] >>> (32 - rotation); |
| 143 | + TEMP_ROT_LEFT[0] = ((a[0] << rotation) | highToLow) >>> 0; |
| 144 | + TEMP_ROT_LEFT[1] = ((a[1] << rotation) | lowToHigh) >>> 0; |
| 145 | + return TEMP_ROT_LEFT; |
| 146 | + } |
| 147 | + |
| 148 | + const actualRotation = rotation - 32; |
| 149 | + const lowToHigh = a[0] << actualRotation; |
| 150 | + const highToLow = a[1] << actualRotation; |
| 151 | + TEMP_ROT_LEFT[0] = ((a[1] >>> (32 - actualRotation)) | highToLow) >>> 0; |
| 152 | + TEMP_ROT_LEFT[1] = ((a[0] >>> (32 - actualRotation)) | lowToHigh) >>> 0; |
| 153 | + return TEMP_ROT_LEFT; |
| 154 | + }, |
| 155 | + |
| 156 | + rotRight(a: Long, shift: number): Long { |
| 157 | + const rotation = shift & 63; |
| 158 | + if (rotation === 0) { |
| 159 | + TEMP_ROT_RIGHT.set(a); |
| 160 | + return TEMP_ROT_RIGHT; |
| 161 | + } |
| 162 | + |
| 163 | + if (rotation < 32) { |
| 164 | + const lowToHigh = a[0] << (32 - rotation); |
| 165 | + const highToLow = a[1] << (32 - rotation); |
| 166 | + TEMP_ROT_RIGHT[0] = ((a[0] >>> rotation) | highToLow) >>> 0; |
| 167 | + TEMP_ROT_RIGHT[1] = ((a[1] >>> rotation) | lowToHigh) >>> 0; |
| 168 | + return TEMP_ROT_RIGHT; |
| 169 | + } |
| 170 | + |
| 171 | + const actualRotation = rotation - 32; |
| 172 | + TEMP_ROT_RIGHT[0] = (a[1] >>> actualRotation) >>> 0; |
| 173 | + TEMP_ROT_RIGHT[1] = (a[0] >>> actualRotation) >>> 0; |
| 174 | + return TEMP_ROT_RIGHT; |
| 175 | + }, |
| 176 | + |
| 177 | + cardinality(a: Long): number { |
| 178 | + return BitwiseLongAlloc.cardinality(a); |
| 179 | + }, |
| 180 | + |
| 181 | + decompose(a: Long): Long[] { |
| 182 | + return BitwiseLongAlloc.decompose(a); |
| 183 | + }, |
| 184 | +} as IBitwiseOps<Long>; |
| 185 | + |
| 186 | +const TEMP_CREATE = new Uint32Array(2); |
| 187 | + |
| 188 | +export const LongUtils = { |
| 189 | + createAlloc(low: number, high: number): Long { |
| 190 | + return new Uint32Array([low >>> 0, high >>> 0]); |
| 191 | + }, |
| 192 | + |
| 193 | + create(low: number, high: number): Long { |
| 194 | + TEMP_CREATE[0] = low >>> 0; |
| 195 | + TEMP_CREATE[1] = high >>> 0; |
| 196 | + return TEMP_CREATE; |
| 197 | + }, |
| 198 | +}; |
0 commit comments