Karatsuba enhancement

This commit is contained in:
zhelenskiy 2021-05-12 11:05:42 +03:00
parent ed86a31c50
commit ded01251d1

View File

@ -324,10 +324,8 @@ public class BigInt internal constructor(
} }
internal fun multiplyMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude = when { internal fun multiplyMagnitudes(mag1: Magnitude, mag2: Magnitude): Magnitude = when {
mag1.size + mag2.size < KARATSUBA_THRESHOLD || mag1.isEmpty() || mag2.isEmpty() -> naiveMultiplyMagnitudes( mag1.size + mag2.size < KARATSUBA_THRESHOLD || mag1.isEmpty() || mag2.isEmpty() ->
mag1, naiveMultiplyMagnitudes(mag1, mag2)
mag2
)
// TODO implement Fourier // TODO implement Fourier
else -> karatsubaMultiplyMagnitudes(mag1, mag2) else -> karatsubaMultiplyMagnitudes(mag1, mag2)
} }
@ -361,7 +359,7 @@ public class BigInt internal constructor(
val z0 = x0 * y0 val z0 = x0 * y0
val z2 = x1 * y1 val z2 = x1 * y1
val z1 = (x0 + x1) * (y1 + y0) - z0 - z2 val z1 = (x0 - x1) * (y1 - y0) + z0 + z2
return (z2.shl(2 * halfSize * BASE_SIZE) + z1.shl(halfSize * BASE_SIZE) + z0).magnitude return (z2.shl(2 * halfSize * BASE_SIZE) + z1.shl(halfSize * BASE_SIZE) + z0).magnitude
} }