Skip to content

Commit d69ebc6

Browse files
committed
Code and test clean up
1 parent f5ffe62 commit d69ebc6

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

jsoniter-scala-core/native/src/main/resources/scala-native/multiply_high.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <intrin.h>
44

55
long long jsoniter_scala_multiply_high(long long x, long long y) {
6-
long long high_product;
7-
(void) _mul128(x, y, &high_product);
8-
return high_product;
6+
return __mulh(x, y);
97
}
108

119
unsigned long long jsoniter_scala_unsigned_multiply_high(unsigned long long x, unsigned long long y) {
@@ -24,22 +22,24 @@ unsigned long long jsoniter_scala_unsigned_multiply_high(unsigned long long x, u
2422

2523
#else
2624

25+
// Based on `int mulhs(int u, int v)` function from Hacker’s Delight 2nd Ed. by Henry S. Warren, Jr.
2726
long long jsoniter_scala_multiply_high(long long x, long long y) {
28-
long long x0 = x & 0xffffffffL;
27+
long long x0 = x & 0xFFFFFFFFL;
2928
long long x1 = x >> 32;
30-
long long y0 = y & 0xffffffffL;
29+
long long y0 = y & 0xFFFFFFFFL;
3130
long long y1 = y >> 32;
3231
long long t = x1 * y0 + ((unsigned long long) (x0 * y0) >> 32);
33-
return x1 * y1 + (t >> 32) + (((t & 0xffffffffL) + x0 * y1) >> 32);
32+
return x1 * y1 + (t >> 32) + (((t & 0xFFFFFFFFL) + x0 * y1) >> 32);
3433
}
3534

35+
// Based on `unsigned mulhu(unsigned u, unsigned v)` function from Hacker’s Delight 2nd Ed. by Henry S. Warren, Jr.
3636
unsigned long long jsoniter_scala_unsigned_multiply_high(unsigned long long x, unsigned long long y) {
37-
unsigned long long x0 = x & 0xffffffffL;
37+
unsigned long long x0 = x & 0xFFFFFFFFL;
3838
unsigned long long x1 = x >> 32;
39-
unsigned long long y0 = y & 0xffffffffL;
39+
unsigned long long y0 = y & 0xFFFFFFFFL;
4040
unsigned long long y1 = y >> 32;
4141
unsigned long long t = x1 * y0 + ((x0 * y0) >> 32);
42-
return x1 * y1 + (t >> 32) + (((t & 0xffffffffL) + x0 * y1) >> 32);
42+
return x1 * y1 + (t >> 32) + (((t & 0xFFFFFFFFL) + x0 * y1) >> 32);
4343
}
4444

4545
#endif

jsoniter-scala-core/native/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/core/NativeMathSpec.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ class NativeMathSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
2727
}
2828
}
2929

30-
def unsignedMultiplyHigh(x: Long, y: Long): Long = { // FIXME: replace by Math.unsignedMultiplyHigh after dropping JDK 17 support
31-
var r = Math.multiplyHigh(x, y)
32-
r += y & (x >> 63)
33-
r += x & (y >> 63)
34-
r
35-
}
30+
def unsignedMultiplyHigh(x: Long, y: Long): Long = // FIXME: replace by Math.unsignedMultiplyHigh after dropping JDK 17 support
31+
Math.multiplyHigh(x, y) + (y & (x >> 63)) + (x & (y >> 63))
3632
}

0 commit comments

Comments
 (0)