diff --git a/generators/builtins/common.kt b/generators/builtins/common.kt index 034652ab90b..b29833829d0 100644 --- a/generators/builtins/common.kt +++ b/generators/builtins/common.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.generators.builtins @@ -73,4 +62,6 @@ fun areEqualNumbers(v: String) = "$v == other.$v" fun hashLong(v: String) = "($v xor ($v ushr 32))" -fun convert(v: String, from: UnsignedType, to: UnsignedType) = if (from == to) v else "$v.to${to.capitalized}()" \ No newline at end of file +fun convert(v: String, from: UnsignedType, to: UnsignedType) = if (from == to) v else "$v.to${to.capitalized}()" + +fun convert(v: String, from: PrimitiveType, to: PrimitiveType) = if (from == to) v else "$v.to${to.capitalized}()" diff --git a/generators/builtins/generateBuiltIns.kt b/generators/builtins/generateBuiltIns.kt index b0ed2714b2f..4b5739decdd 100644 --- a/generators/builtins/generateBuiltIns.kt +++ b/generators/builtins/generateBuiltIns.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -11,7 +11,8 @@ import org.jetbrains.kotlin.generators.builtins.functions.GenerateFunctions import org.jetbrains.kotlin.generators.builtins.iterators.GenerateIterators import org.jetbrains.kotlin.generators.builtins.progressionIterators.GenerateProgressionIterators import org.jetbrains.kotlin.generators.builtins.progressions.GenerateProgressions -import org.jetbrains.kotlin.generators.builtins.ranges.GeneratePrimitives +import org.jetbrains.kotlin.generators.builtins.numbers.GeneratePrimitives +import org.jetbrains.kotlin.generators.builtins.numbers.GenerateFloorDivMod import org.jetbrains.kotlin.generators.builtins.ranges.GenerateRanges import org.jetbrains.kotlin.generators.builtins.unsigned.generateUnsignedTypes import org.xml.sax.InputSource @@ -27,16 +28,14 @@ val BUILT_INS_NATIVE_DIR = File("core/builtins/native/") val BUILT_INS_SRC_DIR = File("core/builtins/src/") val RUNTIME_JVM_DIR = File("libraries/stdlib/jvm/runtime/") val UNSIGNED_TYPES_DIR = File("libraries/stdlib/unsigned/src") +val STDLIB_DIR = File("libraries/stdlib/src") abstract class BuiltInsSourceGenerator(val out: PrintWriter) { protected abstract fun generateBody(): Unit protected open fun getPackage(): String = "kotlin" - enum class Language { - KOTLIN, - JAVA - } + protected open fun getMultifileClassName(): String? = null fun generate() { out.println(readCopyrightNoticeFromProfile(File(".idea/copyright/apache.xml"))) @@ -44,6 +43,10 @@ abstract class BuiltInsSourceGenerator(val out: PrintWriter) { // and we don't want to scare users with any internal information about our project out.println("// Auto-generated file. DO NOT EDIT!") out.println() + getMultifileClassName()?.let { name -> + out.println("@file:kotlin.jvm.JvmName(\"$name\")") + out.println("@file:kotlin.jvm.JvmMultifileClass") + } out.print("package ${getPackage()}") out.println() out.println() @@ -77,6 +80,7 @@ fun generateBuiltIns(generate: (File, (PrintWriter) -> BuiltInsSourceGenerator) generate(File(BUILT_INS_SRC_DIR, "kotlin/ProgressionIterators.kt")) { GenerateProgressionIterators(it) } generate(File(BUILT_INS_SRC_DIR, "kotlin/Progressions.kt")) { GenerateProgressions(it) } generate(File(BUILT_INS_SRC_DIR, "kotlin/Ranges.kt")) { GenerateRanges(it) } + generate(File(STDLIB_DIR, "kotlin/util/FloorDivMod.kt")) { GenerateFloorDivMod(it) } generateUnsignedTypes(UNSIGNED_TYPES_DIR, generate) } diff --git a/generators/builtins/primitives.kt b/generators/builtins/primitives.kt index a51bbd5777c..70784958b87 100644 --- a/generators/builtins/primitives.kt +++ b/generators/builtins/primitives.kt @@ -1,11 +1,12 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -package org.jetbrains.kotlin.generators.builtins.ranges +package org.jetbrains.kotlin.generators.builtins.numbers import org.jetbrains.kotlin.generators.builtins.PrimitiveType +import org.jetbrains.kotlin.generators.builtins.convert import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator import java.io.PrintWriter @@ -396,13 +397,112 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) { out.println(" public override fun to$otherName(): $otherName") } } - - private fun maxByDomainCapacity(type1: PrimitiveType, type2: PrimitiveType): PrimitiveType - = if (type1.ordinal > type2.ordinal) type1 else type2 - - private fun getOperatorReturnType(kind1: PrimitiveType, kind2: PrimitiveType): PrimitiveType { - require(kind1 != PrimitiveType.BOOLEAN) { "kind1 must not be BOOLEAN" } - require(kind2 != PrimitiveType.BOOLEAN) { "kind2 must not be BOOLEAN" } - return maxByDomainCapacity(maxByDomainCapacity(kind1, kind2), PrimitiveType.INT) - } } + +class GenerateFloorDivMod(out: PrintWriter) : BuiltInsSourceGenerator(out) { + + override fun getMultifileClassName() = "NumbersKt" + override fun generateBody() { + out.println("import kotlin.math.sign") + out.println() + + val integerTypes = PrimitiveType.integral intersect PrimitiveType.onlyNumeric + for (thisType in integerTypes) { + for (otherType in integerTypes) { + generateFloorDiv(thisType, otherType) + generateMod(thisType, otherType) + } + } + + val fpTypes = PrimitiveType.floatingPoint + for (thisType in fpTypes) { + for (otherType in fpTypes) { + generateFpMod(thisType, otherType) + } + } + + } + + + private fun generateFloorDiv(thisKind: PrimitiveType, otherKind: PrimitiveType) { + val returnType = getOperatorReturnType(thisKind, otherKind) + val returnTypeName = returnType.capitalized + out.println("""@SinceKotlin("1.5")""") + out.println("@kotlin.internal.InlineOnly") + val declaration = "public inline fun ${thisKind.capitalized}.floorDiv(other: ${otherKind.capitalized}): $returnTypeName" + if (thisKind == otherKind && thisKind >= PrimitiveType.INT) { + out.println( + """ + $declaration { + var q = this / other + if (this xor other < 0 && q * other != this) q-- + return q + } + """.trimIndent() + ) + } else { + out.println("$declaration = ") + out.println(" ${ + convert("this", thisKind, returnType)}.floorDiv(${convert("other", otherKind, returnType)})") + } + out.println() + } + + private fun generateMod(thisKind: PrimitiveType, otherKind: PrimitiveType) { + val operationType = getOperatorReturnType(thisKind, otherKind) + val returnType = otherKind + out.println("""@SinceKotlin("1.5")""") + out.println("@kotlin.internal.InlineOnly") + val declaration = "public inline fun ${thisKind.capitalized}.mod(other: ${otherKind.capitalized}): ${returnType.capitalized}" + if (thisKind == otherKind && thisKind >= PrimitiveType.INT) { + out.println( + """ + $declaration { + val r = this % other + return r + (other and (((r xor other) and (r or -r)) shr ${operationType.bitSize - 1})) + } + """.trimIndent() + ) + } else { + out.println("$declaration = ") + out.println(" " + convert( + "${convert("this", thisKind, operationType)}.mod(${convert("other", otherKind, operationType)})", + operationType, returnType + )) + } + out.println() + } + + private fun generateFpMod(thisKind: PrimitiveType, otherKind: PrimitiveType) { + val operationType = getOperatorReturnType(thisKind, otherKind) + out.println("""@SinceKotlin("1.5")""") + out.println("@kotlin.internal.InlineOnly") + val declaration = "public inline fun ${thisKind.capitalized}.mod(other: ${otherKind.capitalized}): ${operationType.capitalized}" + if (thisKind == otherKind && thisKind >= PrimitiveType.INT) { + out.println( + """ + $declaration { + val r = this % other + return if (r != ${convert("0.0", PrimitiveType.DOUBLE, operationType)} && r.sign != other.sign) r + other else r + } + """.trimIndent() + ) + } else { + out.println("$declaration = ") + out.println(" ${convert("this", thisKind, operationType)}.mod(${convert("other", otherKind, operationType)})") + } + out.println() + } + +} + + +private fun maxByDomainCapacity(type1: PrimitiveType, type2: PrimitiveType): PrimitiveType + = if (type1.ordinal > type2.ordinal) type1 else type2 + +private fun getOperatorReturnType(kind1: PrimitiveType, kind2: PrimitiveType): PrimitiveType { + require(kind1 != PrimitiveType.BOOLEAN) { "kind1 must not be BOOLEAN" } + require(kind2 != PrimitiveType.BOOLEAN) { "kind2 must not be BOOLEAN" } + return maxByDomainCapacity(maxByDomainCapacity(kind1, kind2), PrimitiveType.INT) +} + diff --git a/generators/builtins/unsignedTypes.kt b/generators/builtins/unsignedTypes.kt index d0ab0a6933d..0947f253492 100644 --- a/generators/builtins/unsignedTypes.kt +++ b/generators/builtins/unsignedTypes.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -10,7 +10,7 @@ import org.jetbrains.kotlin.generators.builtins.PrimitiveType import org.jetbrains.kotlin.generators.builtins.UnsignedType import org.jetbrains.kotlin.generators.builtins.convert import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator -import org.jetbrains.kotlin.generators.builtins.ranges.GeneratePrimitives +import org.jetbrains.kotlin.generators.builtins.numbers.GeneratePrimitives import java.io.File import java.io.PrintWriter @@ -127,6 +127,8 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns for ((name, doc) in GeneratePrimitives.binaryOperators) { generateOperator(name, doc) } + generateFloorDivMod("floorDiv", "TODO") + generateFloorDivMod("mod", "TODO") } private fun generateOperator(name: String, doc: String) { @@ -150,6 +152,32 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns out.println() } + private fun generateFloorDivMod(name: String, doc: String) { + for (otherType in UnsignedType.values()) { + val operationType = getOperatorReturnType(type, otherType) + val returnType = if (name == "mod") otherType else operationType + + out.println(" /** $doc */") + out.println(" @kotlin.internal.InlineOnly") + out.print(" public inline fun $name(other: ${otherType.capitalized}): ${returnType.capitalized} = ") + if (type == otherType && type == operationType) { + when (name) { + "floorDiv" -> out.println("div(other)") + "mod" -> out.println("rem(other)") + else -> error(name) + } + } else { + out.println( + convert( + "${convert("this", type, operationType)}.$name(${convert("other", otherType, operationType)})", + operationType, returnType + ) + ) + } + } + out.println() + } + private fun generateUnaryOperators() { for ((name, doc) in GeneratePrimitives.unaryOperators) { diff --git a/libraries/stdlib/api/js-v1/kotlin.kt b/libraries/stdlib/api/js-v1/kotlin.kt index 3e7b62dec9f..c13d6ed7650 100644 --- a/libraries/stdlib/api/js-v1/kotlin.kt +++ b/libraries/stdlib/api/js-v1/kotlin.kt @@ -318,6 +318,70 @@ public inline fun kotlin.ULong.countTrailingZeroBits(): kotlin.Int @kotlin.internal.InlineOnly public inline fun kotlin.UShort.countTrailingZeroBits(): kotlin.Int +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Byte): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Short): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Byte): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Short): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Byte): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Int): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Short): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Byte): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Short): kotlin.Int + @kotlin.internal.InlineOnly @kotlin.SinceKotlin(version = "1.3") public inline fun kotlin.Result.fold(onSuccess: (value: T) -> R, onFailure: (exception: kotlin.Throwable) -> R): R @@ -384,6 +448,86 @@ public inline fun kotlin.Result.map(transform: (value: T) -> R): kotli @kotlin.SinceKotlin(version = "1.3") public inline fun kotlin.Result.mapCatching(transform: (value: T) -> R): kotlin.Result +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Short): kotlin.Short + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Double.mod(other: kotlin.Double): kotlin.Double + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Double.mod(other: kotlin.Float): kotlin.Double + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Float.mod(other: kotlin.Double): kotlin.Double + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Float.mod(other: kotlin.Float): kotlin.Float + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Short): kotlin.Short + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Short): kotlin.Short + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Short): kotlin.Short + @kotlin.internal.InlineOnly @kotlin.SinceKotlin(version = "1.3") public inline fun kotlin.Result.onFailure(action: (exception: kotlin.Throwable) -> kotlin.Unit): kotlin.Result @@ -2277,6 +2421,18 @@ public final inline class UByte : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.UInt + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2297,6 +2453,18 @@ public final inline class UByte : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.UInt + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.UByte): kotlin.UByte @@ -2448,6 +2616,18 @@ public final inline class UInt : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.UInt + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2468,6 +2648,18 @@ public final inline class UInt : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.UInt + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.UInt): kotlin.UInt @@ -2625,6 +2817,18 @@ public final inline class ULong : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.ULong + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2645,6 +2849,18 @@ public final inline class ULong : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.ULong + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.ULong): kotlin.ULong @@ -2802,6 +3018,18 @@ public final inline class UShort : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.UInt + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2822,6 +3050,18 @@ public final inline class UShort : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.UInt + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.UShort): kotlin.UShort diff --git a/libraries/stdlib/api/js/kotlin.kt b/libraries/stdlib/api/js/kotlin.kt index 781b8dc3cd0..985c751fc9d 100644 --- a/libraries/stdlib/api/js/kotlin.kt +++ b/libraries/stdlib/api/js/kotlin.kt @@ -281,6 +281,70 @@ public inline fun kotlin.ULong.countTrailingZeroBits(): kotlin.Int @kotlin.internal.InlineOnly public inline fun kotlin.UShort.countTrailingZeroBits(): kotlin.Int +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Byte): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.floorDiv(other: kotlin.Short): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Byte): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.floorDiv(other: kotlin.Short): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Byte): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Int): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.floorDiv(other: kotlin.Short): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Byte): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.floorDiv(other: kotlin.Short): kotlin.Int + @kotlin.internal.InlineOnly @kotlin.SinceKotlin(version = "1.3") public inline fun kotlin.Result.fold(onSuccess: (value: T) -> R, onFailure: (exception: kotlin.Throwable) -> R): R @@ -347,6 +411,86 @@ public inline fun kotlin.Result.map(transform: (value: T) -> R): kotli @kotlin.SinceKotlin(version = "1.3") public inline fun kotlin.Result.mapCatching(transform: (value: T) -> R): kotlin.Result +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Byte.mod(other: kotlin.Short): kotlin.Short + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Double.mod(other: kotlin.Double): kotlin.Double + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Double.mod(other: kotlin.Float): kotlin.Double + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Float.mod(other: kotlin.Double): kotlin.Double + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Float.mod(other: kotlin.Float): kotlin.Float + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Int.mod(other: kotlin.Short): kotlin.Short + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Long.mod(other: kotlin.Short): kotlin.Short + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Byte): kotlin.Byte + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Int): kotlin.Int + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Long): kotlin.Long + +@kotlin.SinceKotlin(version = "1.5") +@kotlin.internal.InlineOnly +public inline fun kotlin.Short.mod(other: kotlin.Short): kotlin.Short + @kotlin.internal.InlineOnly @kotlin.SinceKotlin(version = "1.3") public inline fun kotlin.Result.onFailure(action: (exception: kotlin.Throwable) -> kotlin.Unit): kotlin.Result @@ -2282,6 +2426,18 @@ public final inline class UByte : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.UInt + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2302,6 +2458,18 @@ public final inline class UByte : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.UInt + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.UByte): kotlin.UByte @@ -2453,6 +2621,18 @@ public final inline class UInt : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.UInt + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2473,6 +2653,18 @@ public final inline class UInt : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.UInt + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.UInt): kotlin.UInt @@ -2630,6 +2822,18 @@ public final inline class ULong : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.ULong + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2650,6 +2854,18 @@ public final inline class ULong : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.ULong + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.ULong): kotlin.ULong @@ -2807,6 +3023,18 @@ public final inline class UShort : kotlin.Comparable { public open override operator fun equals(other: kotlin.Any?): kotlin.Boolean + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UByte): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun floorDiv(other: kotlin.UShort): kotlin.UInt + public open override fun hashCode(): kotlin.Int @kotlin.internal.InlineOnly @@ -2827,6 +3055,18 @@ public final inline class UShort : kotlin.Comparable { @kotlin.internal.InlineOnly public final inline operator fun minus(other: kotlin.UShort): kotlin.UInt + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UByte): kotlin.UByte + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UInt): kotlin.UInt + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.ULong): kotlin.ULong + + @kotlin.internal.InlineOnly + public final inline fun mod(other: kotlin.UShort): kotlin.UShort + @kotlin.internal.InlineOnly public final inline infix fun or(other: kotlin.UShort): kotlin.UShort diff --git a/libraries/stdlib/src/kotlin/util/FloorDivMod.kt b/libraries/stdlib/src/kotlin/util/FloorDivMod.kt new file mode 100644 index 00000000000..42764c4d3f2 --- /dev/null +++ b/libraries/stdlib/src/kotlin/util/FloorDivMod.kt @@ -0,0 +1,207 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +// Auto-generated file. DO NOT EDIT! + +@file:kotlin.jvm.JvmName("NumbersKt") +@file:kotlin.jvm.JvmMultifileClass +package kotlin + +import kotlin.math.sign + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.floorDiv(other: Byte): Int = + this.toInt().floorDiv(other.toInt()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.mod(other: Byte): Byte = + this.toInt().mod(other.toInt()).toByte() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.floorDiv(other: Short): Int = + this.toInt().floorDiv(other.toInt()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.mod(other: Short): Short = + this.toInt().mod(other.toInt()).toShort() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.floorDiv(other: Int): Int = + this.toInt().floorDiv(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.mod(other: Int): Int = + this.toInt().mod(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.floorDiv(other: Long): Long = + this.toLong().floorDiv(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Byte.mod(other: Long): Long = + this.toLong().mod(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.floorDiv(other: Byte): Int = + this.toInt().floorDiv(other.toInt()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.mod(other: Byte): Byte = + this.toInt().mod(other.toInt()).toByte() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.floorDiv(other: Short): Int = + this.toInt().floorDiv(other.toInt()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.mod(other: Short): Short = + this.toInt().mod(other.toInt()).toShort() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.floorDiv(other: Int): Int = + this.toInt().floorDiv(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.mod(other: Int): Int = + this.toInt().mod(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.floorDiv(other: Long): Long = + this.toLong().floorDiv(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Short.mod(other: Long): Long = + this.toLong().mod(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.floorDiv(other: Byte): Int = + this.floorDiv(other.toInt()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.mod(other: Byte): Byte = + this.mod(other.toInt()).toByte() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.floorDiv(other: Short): Int = + this.floorDiv(other.toInt()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.mod(other: Short): Short = + this.mod(other.toInt()).toShort() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.floorDiv(other: Int): Int { + var q = this / other + if (this xor other < 0 && q * other != this) q-- + return q +} + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.mod(other: Int): Int { + val r = this % other + return r + (other and (((r xor other) and (r or -r)) shr 31)) +} + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.floorDiv(other: Long): Long = + this.toLong().floorDiv(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Int.mod(other: Long): Long = + this.toLong().mod(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.floorDiv(other: Byte): Long = + this.floorDiv(other.toLong()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.mod(other: Byte): Byte = + this.mod(other.toLong()).toByte() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.floorDiv(other: Short): Long = + this.floorDiv(other.toLong()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.mod(other: Short): Short = + this.mod(other.toLong()).toShort() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.floorDiv(other: Int): Long = + this.floorDiv(other.toLong()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.mod(other: Int): Int = + this.mod(other.toLong()).toInt() + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.floorDiv(other: Long): Long { + var q = this / other + if (this xor other < 0 && q * other != this) q-- + return q +} + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Long.mod(other: Long): Long { + val r = this % other + return r + (other and (((r xor other) and (r or -r)) shr 63)) +} + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Float.mod(other: Float): Float { + val r = this % other + return if (r != 0.0.toFloat() && r.sign != other.sign) r + other else r +} + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Float.mod(other: Double): Double = + this.toDouble().mod(other) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Double.mod(other: Float): Double = + this.mod(other.toDouble()) + +@SinceKotlin("1.5") +@kotlin.internal.InlineOnly +public inline fun Double.mod(other: Double): Double { + val r = this % other + return if (r != 0.0 && r.sign != other.sign) r + other else r +} + diff --git a/libraries/stdlib/test/numbers/FloorDivModTest.kt b/libraries/stdlib/test/numbers/FloorDivModTest.kt new file mode 100644 index 00000000000..b11a6d9305f --- /dev/null +++ b/libraries/stdlib/test/numbers/FloorDivModTest.kt @@ -0,0 +1,283 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package test.numbers + +import kotlin.math.pow +import kotlin.math.sign +import kotlin.random.Random +import kotlin.test.* + +class FloorDivModTest { + + @Test + fun intDivMod() { + fun check(a: Int, b: Int, expectedFd: Int? = null, expectedMod: Int? = null) { + if (a == Int.MIN_VALUE && b == -1) return // do not test, due to native KT-45136 + val div = a / b + val rem = a % b + val fd = a.floorDiv(b) + val mod = a.mod(b) + + try { + expectedFd?.let { assertEquals(it, fd) } + expectedMod?.let { assertEquals(it, mod) } + assertEquals(div - if (a.sign != b.sign && rem != 0) 1 else 0, fd) + assertEquals(a - b * fd, mod) + } catch (e: AssertionError) { + fail("a: $a, b: $b, div: $div, rem: $rem, floorDiv: $fd, mod: $mod", e) + } + } + + check(10, -3, -4, -2) + check(10, 3, 3, 1) + check(-10, 3, -4, 2) + check(-10, -3, 3, -1) + val values = listOf(1, -1, 2, -2, 3, -3, Int.MIN_VALUE, Int.MAX_VALUE) + for (a in values + 0) { + for (b in values) { + check(a, b) + } + } + repeat(1000) { + val a = Random.nextInt() + val b = Random.nextInt().let { if (it == 0) 1 else it } + check(a, b) + } + } + + @Test + fun longDivMod() { + fun check(a: Long, b: Long, expectedFd: Long? = null, expectedMod: Long? = null) { + if (a == Long.MIN_VALUE && b == -1L) return // do not test, due to native KT-45136 + val div = a / b + val rem = a % b + val fd = a.floorDiv(b) + val mod = a.mod(b) + + try { + expectedFd?.let { assertEquals(it, fd) } + expectedMod?.let { assertEquals(it, mod) } + assertEquals(div - if (a.sign != b.sign && rem != 0L) 1 else 0, fd) + assertEquals(a - b * fd, mod) + } catch (e: AssertionError) { + fail("a: $a, b: $b, div: $div, rem: $rem, floorDiv: $fd, mod: $mod", e) + } + } + + check(10, -3, -4, -2) + check(10, 3, 3, 1) + check(-10, 3, -4, 2) + check(-10, -3, 3, -1) + val values = listOf(1, -1, 2, -2, 3, -3, Long.MIN_VALUE, Long.MAX_VALUE) + for (a in values + 0) { + for (b in values) { + check(a, b) + } + } + repeat(1000) { + val a = Random.nextLong() + val b = Random.nextLong().let { if (it == 0L) 1 else it } + check(a, b) + } + } + + @Test + fun byteDivMod() { + fun check(a: Byte, b: Byte, expectedFd: Int? = null, expectedMod: Byte? = null) { + val div = a / b + val rem = a % b + val fd = a.floorDiv(b) + val mod = a.mod(b) + + try { + expectedFd?.let { assertEquals(it, fd) } + expectedMod?.let { assertEquals(it, mod) } + assertEquals(div - if (a.toInt().sign != b.toInt().sign && rem != 0) 1 else 0, fd) + assertEquals(a - b * fd, mod.toInt()) + } catch (e: AssertionError) { + fail("a: $a, b: $b, div: $div, rem: $rem, floorDiv: $fd, mod: $mod", e) + } + } + + check(10, -3, -4, -2) + check(10, 3, 3, 1) + check(-10, 3, -4, 2) + check(-10, -3, 3, -1) + val values = listOf(1, -1, 2, -2, 3, -3, Byte.MIN_VALUE, Byte.MAX_VALUE) + for (a in values + 0) { + for (b in values) { + check(a, b) + } + } + repeat(1000) { + val a = Random.nextInt().toByte() + val b = Random.nextInt().toByte().let { if (it == 0.toByte()) 1 else it } + check(a, b) + } + } + + @Test + fun shortDivMod() { + fun check(a: Short, b: Short, expectedFd: Int? = null, expectedMod: Short? = null) { + val div = a / b + val rem = a % b + val fd = a.floorDiv(b) + val mod = a.mod(b) + + try { + expectedFd?.let { assertEquals(it, fd) } + expectedMod?.let { assertEquals(it, mod) } + assertEquals(div - if (a.toInt().sign != b.toInt().sign && rem != 0) 1 else 0, fd) + assertEquals(a - b * fd, mod.toInt()) + } catch (e: AssertionError) { + fail("a: $a, b: $b, div: $div, rem: $rem, floorDiv: $fd, mod: $mod", e) + } + } + + check(10, -3, -4, -2) + check(10, 3, 3, 1) + check(-10, 3, -4, 2) + check(-10, -3, 3, -1) + val values = listOf(1, -1, 2, -2, 3, -3, Short.MIN_VALUE, Short.MAX_VALUE) + for (a in values + 0) { + for (b in values) { + check(a, b) + } + } + repeat(1000) { + val a = Random.nextInt().toShort() + val b = Random.nextInt().toShort().let { if (it == 0.toShort()) 1 else it } + check(a, b) + } + } + + @Test + fun longIntMod() { + fun check(a: Long, b: Int, expectedFd: Long? = null, expectedMod: Int? = null) { + val div = a / b + val rem = a % b + val fd = a.floorDiv(b) + val mod = a.mod(b) + + try { + expectedFd?.let { assertEquals(it, fd) } + expectedMod?.let { assertEquals(it, mod) } + assertEquals(div - if (a.sign != b.sign && rem != 0L) 1 else 0, fd) + assertEquals(a - b * fd, mod.toLong()) + } catch (e: AssertionError) { + fail("a: $a, b: $b, div: $div, rem: $rem, floorDiv: $fd, mod: $mod", e) + } + } + + check(Long.MAX_VALUE, 2, Long.MAX_VALUE / 2, 1) + check(Long.MAX_VALUE, 1.shl(30), expectedMod = 1.shl(30) - 1) + check(-1L, 1.shl(30), expectedMod = 1.shl(30) - 1) + check(Long.MAX_VALUE, Int.MAX_VALUE, expectedMod = 1) + check(Long.MAX_VALUE, Int.MIN_VALUE, expectedMod = -1) + } + + @Test + fun shortIntMod() { + fun check(a: Short, b: Int, expectedFd: Int? = null, expectedMod: Int? = null) { + val div = a / b + val rem = a % b + val fd = a.floorDiv(b) + val mod = a.mod(b) + + try { + expectedFd?.let { assertEquals(it, fd) } + expectedMod?.let { assertEquals(it, mod) } + assertEquals(div - if (a.toInt().sign != b.sign && rem != 0) 1 else 0, fd) + assertEquals(a.toInt() - b * fd, mod) // a.toInt() to workaround Int-out-of-range in JS legacy + } catch (e: AssertionError) { + fail("a: $a, b: $b, div: $div, rem: $rem, floorDiv: $fd, mod: $mod", e) + } + } + + check(Short.MAX_VALUE, Int.MAX_VALUE, 0, Short.MAX_VALUE.toInt()) + check(Short.MAX_VALUE, Int.MIN_VALUE, -1, Int.MIN_VALUE + Short.MAX_VALUE) + check((-1).toShort(), 1.shl(30), -1, 1.shl(30) - 1) + } + + @Test + fun doubleMod() { + fun check(a: Double, b: Double, expectedMod: Double? = null) { + val rem = a % b + val mod = a.mod(b) + + try { + expectedMod?.let { assertEquals(it, mod) } + assertEquals(if (rem.sign == mod.sign) rem else rem + b, mod) + } catch (e: AssertionError) { + fail("a: $a, b: $b, rem: $rem, mod: $mod", e) + } + } + + check(10.125, -0.5, -0.375) + check(10.125, 0.5, 0.125) + check(-10.125, 0.5, 0.375) + check(-10.125, -0.5, -0.125) + val large = 2.0.pow(53) + check(0.025, large, 0.025) + check(-0.025, large, expectedMod = large) + check(0.025, -large, expectedMod = -large) + check(1.0, Double.NaN, Double.NaN) + check(Double.NaN, 1.0, Double.NaN) + check(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN) + val values = listOf(1.0, -1.0, 3.0, -3.0, large, -large, Double.MIN_VALUE, Double.MAX_VALUE, + Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN) + for (a in values + 0.0) { + for (b in values) { + check(a, b) + } + } + repeat(1000) { + val a = Random.nextDouble() + val b = Random.nextDouble().let { if (it == 0.0) 1.0 else it } + check(a, b) + } + } + @Test + fun floatMod() { + fun check(a: Float, b: Float, expectedMod: Float? = null) { + val rem = a % b + val mod = a.mod(b) + + try { + expectedMod?.let { assertEquals(it, mod) } + assertEquals(if (rem.sign == mod.sign) rem else rem + b, mod) + } catch (e: AssertionError) { + fail("a: $a, b: $b, rem: $rem, mod: $mod", e) + } + } + + check(10.125f, -0.5f, -0.375f) + check(10.125f, 0.5f, 0.125f) + check(-10.125f, 0.5f, 0.375f) + check(-10.125f, -0.5f, -0.125f) + val large = 2.0f.pow(53) + check(0.025f, large, 0.025f) + check(-0.025f, large, expectedMod = large) + check(0.025f, -large, expectedMod = -large) + check(1.0f, Float.NaN, Float.NaN) + check(Float.NaN, 1.0f, Float.NaN) + check(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN) + val values = listOf(1.0f, -1.0f, 3.0f, -3.0f, large, -large, Float.MIN_VALUE, Float.MAX_VALUE, + Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN) + for (a in values + 0.0f) { + for (b in values) { + check(a, b) + } + } + repeat(1000) { + val a = Random.nextFloat() + val b = Random.nextFloat().let { if (it == 0.0f) 1.0f else it } + check(a, b) + } + } + +} + diff --git a/libraries/stdlib/test/unsigned/UIntTest.kt b/libraries/stdlib/test/unsigned/UIntTest.kt index ab4b8136582..5fd593b0d09 100644 --- a/libraries/stdlib/test/unsigned/UIntTest.kt +++ b/libraries/stdlib/test/unsigned/UIntTest.kt @@ -1,15 +1,12 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package test.unsigned -import kotlin.math.nextDown -import kotlin.math.nextUp -import kotlin.math.pow -import kotlin.math.sign -import kotlin.random.Random +import kotlin.math.* +import kotlin.random.* import kotlin.test.* class UIntTest { @@ -69,6 +66,42 @@ class UIntTest { testToString("4294967295", UInt.MAX_VALUE) } + @Test + fun operations() { + assertEquals(2_147_483_648u, Int.MAX_VALUE.toUInt() + identity(1u)) + assertEquals(11u, UInt.MAX_VALUE + identity(12u)) + assertEquals(UInt.MAX_VALUE - 99u, 45u - identity(145u)) + + assertEquals(UInt.MAX_VALUE - 1u, Int.MAX_VALUE.toUInt() * identity(2u)) + assertEquals(2_147_483_645u, Int.MAX_VALUE.toUInt() * identity(3u)) + + testMulDivRem(125u, 3u, 41u, 2u) + testMulDivRem(210u, 5u, 42u, 0u) + testMulDivRem(UInt.MAX_VALUE, 256u, 16777215u, 255u) + testMulDivRem(UInt.MAX_VALUE - 1u, UInt.MAX_VALUE, 0u, UInt.MAX_VALUE - 1u) + testMulDivRem(UInt.MAX_VALUE, UInt.MAX_VALUE - 1u, 1u, 1u) + testMulDivRem(UInt.MAX_VALUE, Int.MAX_VALUE.toUInt(), 2u, 1u) + } + + + private fun testMulDivRem(number: UInt, divisor: UInt, div: UInt, rem: UInt) { + assertEquals(div, number / divisor) + assertEquals(rem, number % divisor) + assertEquals(div, number.floorDiv(divisor)) + assertEquals(rem, number.mod(divisor)) + + assertEquals(number, div * divisor + rem) + assertTrue(rem < divisor) + assertTrue(div < number) + } + + @Test + fun divRem() = repeat(1000) { + val number = Random.nextUInt() + val divisor = Random.nextUInt(until = UInt.MAX_VALUE) + 1u + testMulDivRem(number, divisor, number / divisor, number % divisor) + } + @Test fun comparisons() { fun compare(op1: Comparable, op2: T) = op1.compareTo(op2) diff --git a/libraries/stdlib/test/unsigned/ULongTest.kt b/libraries/stdlib/test/unsigned/ULongTest.kt index 906ab3143b0..7a7fe191b40 100644 --- a/libraries/stdlib/test/unsigned/ULongTest.kt +++ b/libraries/stdlib/test/unsigned/ULongTest.kt @@ -1,12 +1,12 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package test.unsigned import kotlin.math.* -import kotlin.random.Random +import kotlin.random.* import kotlin.test.* class ULongTest { @@ -66,6 +66,42 @@ class ULongTest { testToString("18446744073709551615", ULong.MAX_VALUE) } + @Test + fun operations() { + assertEquals(9223372036854775808u, Long.MAX_VALUE.toULong() + identity(1u)) + assertEquals(11u, ULong.MAX_VALUE + identity(12u)) + assertEquals(ULong.MAX_VALUE - 99u, 45u - identity(145u)) + + assertEquals(ULong.MAX_VALUE - 1u, Long.MAX_VALUE.toULong() * identity(2u)) + assertEquals(9223372036854775805u, Long.MAX_VALUE.toULong() * identity(3u)) + + testMulDivRem(125u, 3u, 41u, 2u) + testMulDivRem(210u, 5u, 42u, 0u) + testMulDivRem(ULong.MAX_VALUE, 65536uL * 65536u, 4294967295u, 4294967295u) + testMulDivRem(ULong.MAX_VALUE - 1u, ULong.MAX_VALUE, 0u, ULong.MAX_VALUE - 1u) + testMulDivRem(ULong.MAX_VALUE, ULong.MAX_VALUE - 1u, 1u, 1u) + testMulDivRem(ULong.MAX_VALUE, Long.MAX_VALUE.toULong(), 2u, 1u) + } + + + private fun testMulDivRem(number: ULong, divisor: ULong, div: ULong, rem: ULong) { + assertEquals(div, number / divisor) + assertEquals(rem, number % divisor) + assertEquals(div, number.floorDiv(divisor)) + assertEquals(rem, number.mod(divisor)) + + assertEquals(number, div * divisor + rem) + assertTrue(rem < divisor) + assertTrue(div < number) + } + + @Test + fun divRem() = repeat(1000) { + val number = Random.nextULong() + val divisor = Random.nextULong(until = ULong.MAX_VALUE) + 1u + testMulDivRem(number, divisor, number / divisor, number % divisor) + } + @Test fun comparisons() { fun compare(op1: Comparable, op2: T) = op1.compareTo(op2) @@ -150,7 +186,7 @@ class ULongTest { assertTrue(diff <= actual.ulp, "$actual should be within one ulp (${actual.ulp}) from the expected $expected") } - fun testRounding(from: ULong, count: UInt) { + fun testRounding(from: ULong, count: ULong) { for (x in from..(from + count)) { val double = x.toDouble() val v = double.toULong() diff --git a/libraries/stdlib/unsigned/src/kotlin/UByte.kt b/libraries/stdlib/unsigned/src/kotlin/UByte.kt index 101209f29e2..7ccee99f568 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UByte.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UByte.kt @@ -135,6 +135,32 @@ public value class UByte @PublishedApi internal constructor(@PublishedApi intern @kotlin.internal.InlineOnly public inline operator fun rem(other: ULong): ULong = this.toULong().rem(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UByte): UInt = this.toUInt().floorDiv(other.toUInt()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UShort): UInt = this.toUInt().floorDiv(other.toUInt()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UInt): UInt = this.toUInt().floorDiv(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: ULong): ULong = this.toULong().floorDiv(other) + + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UByte): UByte = this.toUInt().mod(other.toUInt()).toUByte() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UShort): UShort = this.toUInt().mod(other.toUInt()).toUShort() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UInt): UInt = this.toUInt().mod(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: ULong): ULong = this.toULong().mod(other) + /** Increments this value. */ @kotlin.internal.InlineOnly public inline operator fun inc(): UByte = UByte(data.inc()) diff --git a/libraries/stdlib/unsigned/src/kotlin/UInt.kt b/libraries/stdlib/unsigned/src/kotlin/UInt.kt index 0823846138a..28a7505f199 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UInt.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UInt.kt @@ -135,6 +135,32 @@ public value class UInt @PublishedApi internal constructor(@PublishedApi interna @kotlin.internal.InlineOnly public inline operator fun rem(other: ULong): ULong = this.toULong().rem(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UByte): UInt = this.floorDiv(other.toUInt()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UShort): UInt = this.floorDiv(other.toUInt()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UInt): UInt = div(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: ULong): ULong = this.toULong().floorDiv(other) + + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UByte): UByte = this.mod(other.toUInt()).toUByte() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UShort): UShort = this.mod(other.toUInt()).toUShort() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UInt): UInt = rem(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: ULong): ULong = this.toULong().mod(other) + /** Increments this value. */ @kotlin.internal.InlineOnly public inline operator fun inc(): UInt = UInt(data.inc()) diff --git a/libraries/stdlib/unsigned/src/kotlin/ULong.kt b/libraries/stdlib/unsigned/src/kotlin/ULong.kt index 4506a5290cf..79ab77044f3 100644 --- a/libraries/stdlib/unsigned/src/kotlin/ULong.kt +++ b/libraries/stdlib/unsigned/src/kotlin/ULong.kt @@ -135,6 +135,32 @@ public value class ULong @PublishedApi internal constructor(@PublishedApi intern @kotlin.internal.InlineOnly public inline operator fun rem(other: ULong): ULong = ulongRemainder(this, other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UByte): ULong = this.floorDiv(other.toULong()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UShort): ULong = this.floorDiv(other.toULong()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UInt): ULong = this.floorDiv(other.toULong()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: ULong): ULong = div(other) + + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UByte): UByte = this.mod(other.toULong()).toUByte() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UShort): UShort = this.mod(other.toULong()).toUShort() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UInt): UInt = this.mod(other.toULong()).toUInt() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: ULong): ULong = rem(other) + /** Increments this value. */ @kotlin.internal.InlineOnly public inline operator fun inc(): ULong = ULong(data.inc()) diff --git a/libraries/stdlib/unsigned/src/kotlin/UShort.kt b/libraries/stdlib/unsigned/src/kotlin/UShort.kt index 66571ff9971..33d57aa8a1b 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UShort.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UShort.kt @@ -135,6 +135,32 @@ public value class UShort @PublishedApi internal constructor(@PublishedApi inter @kotlin.internal.InlineOnly public inline operator fun rem(other: ULong): ULong = this.toULong().rem(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UByte): UInt = this.toUInt().floorDiv(other.toUInt()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UShort): UInt = this.toUInt().floorDiv(other.toUInt()) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: UInt): UInt = this.toUInt().floorDiv(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun floorDiv(other: ULong): ULong = this.toULong().floorDiv(other) + + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UByte): UByte = this.toUInt().mod(other.toUInt()).toUByte() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UShort): UShort = this.toUInt().mod(other.toUInt()).toUShort() + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: UInt): UInt = this.toUInt().mod(other) + /** TODO */ + @kotlin.internal.InlineOnly + public inline fun mod(other: ULong): ULong = this.toULong().mod(other) + /** Increments this value. */ @kotlin.internal.InlineOnly public inline operator fun inc(): UShort = UShort(data.inc())