RFR: 8336274: MutableBigInteger.leftShift(int) optimization [v9]
Raffaello Giulietti
rgiulietti at openjdk.org
Thu Sep 12 13:26:06 UTC 2024
On Thu, 12 Sep 2024 12:27:01 GMT, fabioromano1 <duke at openjdk.org> wrote:
>> I mean something like
>>
>> private void primitiveRightShift(int n, int[] result, int resPos) {
>> int[] val = value;
>> int n2 = 32 - n;
>> int c = 0;
>> for (int i = 0; i < intLen; i++) {
>> int b = val[offset + i];
>> result[resPos + i] = c << n2 | b >>> n;
>> c = b;
>> }
>> }
>>
>> and
>>
>> private void primitiveLeftShift(int n, int[] result, int resPos) {
>> int[] val = value;
>> int n2 = 32 - n;
>> int c = 0;
>> for (int i = intLen - 1; i >= 0; i--) {
>> int b = val[offset + i];
>> result[resPos + i] = c >>> n2 | b << n;
>> c = b;
>> }
>>
>> They are compatible with the precondition, right?
>
> But in the second version, the precondition must be `resPos <= offset - intLen || resPos >= offset` to make sure that the result is correct whether `result == value`, and the condition `resPos <= offset - intLen` is stronger than `resPos <= offset`.
Yes, I agree.
This one should correctly account for the precondition, and IMO is slightly simpler to read.
private void primitiveLeftShift(int n, int[] result, int resPos) {
int[] val = value;
int n2 = 32 - n;
final int m = intLen - 1;
int b = val[offset];
for (int i = 0; i < m; i++) {
int c = val[offset + i + 1];
result[resPos + i] = (b << n) | (c >>> n2);
b = c;
}
result[resPos + m] = b << n;
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/20008#discussion_r1756855722
More information about the core-libs-dev
mailing list