From e4216099b3c88cdf825e7b2411f2727d29776435 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 4 May 2018 04:48:21 +0300 Subject: [PATCH] Simplify differenceModulo for unsigned operands --- .../unsigned/src/kotlin/UProgressionUtil.kt | 43 ++++++------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/libraries/stdlib/unsigned/src/kotlin/UProgressionUtil.kt b/libraries/stdlib/unsigned/src/kotlin/UProgressionUtil.kt index 5de50c35b08..a9fdc454e4f 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UProgressionUtil.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UProgressionUtil.kt @@ -5,36 +5,17 @@ package kotlin.internal -import kotlin.jvm.JvmName - - -// a mod b (in arithmetical sense) - -@JvmName("umod") -private fun mod(a: UInt, b: Int): Int = (a % b.toUInt()).toInt() - -private fun mod(a: Int, b: Int): Int { - val mod = a % b - return if (mod >= 0) mod else mod + b -} - -@JvmName("umod") -private fun mod(a: ULong, b: Long): Long = (a % b.toULong()).toLong() - -private fun mod(a: Long, b: Long): Long { - val mod = a % b - return if (mod >= 0) mod else mod + b -} - - - // (a - b) mod c -private fun differenceModulo(a: UInt, b: UInt, c: Int): UInt { - return mod(mod(a, c) - mod(b, c), c).toUInt() +private fun differenceModulo(a: UInt, b: UInt, c: UInt): UInt { + val ac = a % c + val bc = b % c + return if (ac >= bc) ac - bc else ac - bc + c } -private fun differenceModulo(a: ULong, b: ULong, c: Long): ULong { - return mod(mod(a, c) - mod(b, c), c).toULong() +private fun differenceModulo(a: ULong, b: ULong, c: ULong): ULong { + val ac = a % c + val bc = b % c + return if (ac >= bc) ac - bc else ac - bc + c } /** @@ -55,8 +36,8 @@ private fun differenceModulo(a: ULong, b: ULong, c: Long): ULong { */ @PublishedApi internal fun getProgressionLastElement(start: UInt, end: UInt, step: Int): UInt = when { - step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step) - step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step) + step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step.toUInt()) + step < 0 -> if (start <= end) end else end + differenceModulo(start, end, (-step).toUInt()) else -> throw kotlin.IllegalArgumentException("Step is zero.") } @@ -78,7 +59,7 @@ internal fun getProgressionLastElement(start: UInt, end: UInt, step: Int): UInt */ @PublishedApi internal fun getProgressionLastElement(start: ULong, end: ULong, step: Long): ULong = when { - step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step) - step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step) + step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step.toULong()) + step < 0 -> if (start <= end) end else end + differenceModulo(start, end, (-step).toULong()) else -> throw kotlin.IllegalArgumentException("Step is zero.") }