Skip to content

Commit 424eb8f

Browse files
committed
add c++ implementation
1 parent fdfbfa6 commit 424eb8f

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

jni/global/templates.hpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,22 @@ using algo4j_util::swap;
3333
namespace algo4j_math {
3434

3535
template<typename T>
36-
auto fast_plus(T a, T b, T _) -> T {
36+
auto fast_plus(T a, T b, T m) -> T {
37+
auto s = a + b;
38+
if (s >= a and s >= b) return s % m;
39+
if (a >= m and a > m - b) return fast_plus(a - m, b, m);
40+
if (b >= m and b > m - a) return fast_plus(a, b - m, m);
41+
return s;
42+
}
43+
44+
template<typename T>
45+
auto fast_mul(T a, T b, T _) -> T {
3746
T ret = 0;
3847
while (b) {
3948
if (b bitand 1)
40-
ret = (ret + a) % _;
49+
ret = fast_plus(ret, a, _);
4150
b >>= 1;
42-
//a <<= 1;
43-
//a %= _;
44-
a = (a << 1) % _;
51+
a = fast_plus(a, a, _);
4552
}
4653
return ret;
4754
}
@@ -51,9 +58,9 @@ namespace algo4j_math {
5158
T ret = 1;
5259
while (b) {
5360
if (b bitand 1)
54-
ret = fast_plus(ret, a, m);
61+
ret = fast_mul(ret, a, m);
5562
b >>= 1;
56-
a = fast_plus(a, a, m);
63+
a = fast_mul(a, a, m);
5764
}
5865
return ret;
5966
}

jni/math/MathUtils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ JNIEXPORT auto JNICALL Java_org_algo4j_math_MathUtils_fastPlus(
7070
return fast_plus(a, b, m);
7171
}
7272

73+
JNIEXPORT auto JNICALL Java_org_algo4j_math_MathUtils_fastMul(
74+
JNIEnv *env,
75+
jclass jc,
76+
jlong a,
77+
jlong b,
78+
jlong m) -> jlong {
79+
return fast_mul(a, b, m);
80+
}
81+
7382
JNIEXPORT auto JNICALL Java_org_algo4j_math_MathUtils_fastPower(
7483
JNIEnv *env,
7584
jclass jc,

jni/math/MathUtils.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ JNIEXPORT auto JNICALL Java_org_algo4j_math_MathUtils_sqrt(
7474
jdouble
7575
) -> jdouble;
7676

77+
/**
78+
* Class: org_algo4j_math_MathUtils
79+
* Method: fastMul
80+
* Signature: (JJJ)J
81+
*/
82+
JNIEXPORT auto JNICALL Java_org_algo4j_math_MathUtils_fastMul(
83+
JNIEnv *,
84+
jclass,
85+
jlong,
86+
jlong,
87+
jlong
88+
) -> jlong;
89+
90+
/**
91+
* Class: org_algo4j_math_MathUtils
92+
* Method: fastPlus
93+
* Signature: (JJJ)J
94+
*/
95+
JNIEXPORT auto JNICALL Java_org_algo4j_math_MathUtils_fastPlus(
96+
JNIEnv *,
97+
jclass,
98+
jlong,
99+
jlong,
100+
jlong
101+
) -> jlong;
77102
/**
78103
* Class: org_algo4j_math_MathUtils
79104
* Method: fastPlus

src/test/kotlin/org/algo4j/math/MathUtilsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class MathUtilsTest {
106106
assertEquals(12, fastPower(2, 9, 100))
107107
assertEquals(4, fastPower(2, 10, 10))
108108
assertEquals(289, fastMul(233, 233, 1000))
109-
assertEquals(3, fastPlus(233, 233, 10))
109+
assertEquals(6, fastPlus(233, 233, 10))
110110
}
111111

112112
/**

0 commit comments

Comments
 (0)