Skip to content

Commit 66c7e83

Browse files
committed
WIP: partial implementation of bignums, pushed for multi-dev debugging
1 parent 523dba9 commit 66c7e83

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ kotlin.code.style=official
22
kotlin.incremental.js=true
33
kotlin.incremental.multiplatform=true
44
kotlin.mpp.stability.nowarn=true
5+
kotlin.native.binary.sourceInfoType=libbacktrace
56
kotlin.native.ignoreDisabledTargets=true
67

78
# atomicfu
@@ -16,4 +17,4 @@ org.gradle.jvmargs=-Xmx2G -XX:MaxMetaspaceSize=1G
1617
sdkVersion=1.3.30-SNAPSHOT
1718

1819
# codegen
19-
codegenVersion=0.33.30-SNAPSHOT
20+
codegenVersion=0.33.30-SNAPSHOT

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ slf4j-version = "2.0.16"
1515
slf4j-v1x-version = "1.7.36"
1616
crt-kotlin-version = "0.8.10"
1717
micrometer-version = "1.13.6"
18+
kotlin-multiplatform-bignum-version = "0.3.10"
1819

1920
# codegen
2021
smithy-version = "1.51.0"
@@ -101,6 +102,8 @@ ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor-ve
101102
kaml = { module = "com.charleskorn.kaml:kaml", version.ref = "kaml-version" }
102103
jsoup = { module = "org.jsoup:jsoup", version.ref = "jsoup-version" }
103104

105+
kotlin-multiplatform-bignum = { module = "com.ionspin.kotlin:bignum", version.ref = "kotlin-multiplatform-bignum-version" }
106+
104107
[plugins]
105108
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka-version"}
106109
kotlin-jvm = {id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }

runtime/runtime-core/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
2+
13
/*
24
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
35
* SPDX-License-Identifier: Apache-2.0
@@ -43,6 +45,12 @@ kotlin {
4345
}
4446
}
4547

48+
nativeMain {
49+
dependencies {
50+
implementation(libs.kotlin.multiplatform.bignum)
51+
}
52+
}
53+
4654
all {
4755
languageSettings.optIn("aws.smithy.kotlin.runtime.InternalApi")
4856
}

runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/content/BigIntegerTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class BigIntegerTest {
7777
"0x123456789abcdef0" to "1311768467463790320",
7878
"0x00ffffffffffffffffffffffffffffffec" to "340282366920938463463374607431768211436",
7979
"0x81445edf51ddc07216da5621c727bfd379d400f3da08018d45749a" to "-52134902384590238490284023839028330923830129830129301234239834982",
80-
8180
)
8281

8382
tests.forEach { (hex, expected) ->

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/content/BigIntegerNative.kt

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@
44
*/
55
package aws.smithy.kotlin.runtime.content
66

7-
public actual class BigInteger actual constructor(value: String) :
7+
import com.ionspin.kotlin.bignum.integer.util.fromTwosComplementByteArray
8+
import com.ionspin.kotlin.bignum.integer.util.toTwosComplementByteArray
9+
import com.ionspin.kotlin.bignum.integer.BigInteger as IonSpinBigInteger
10+
11+
public actual class BigInteger private constructor(private val delegate: IonSpinBigInteger) :
812
Number(),
913
Comparable<BigInteger> {
10-
public actual constructor(bytes: ByteArray) : this("Not yet implemented")
1114

12-
actual override fun toByte(): Byte {
13-
TODO("Not yet implemented")
15+
private companion object {
16+
fun coalesceOrCreate(value: IonSpinBigInteger, left: BigInteger, right: BigInteger): BigInteger = when (value) {
17+
left.delegate -> left
18+
right.delegate -> right
19+
else -> BigInteger(value)
20+
}
1421
}
1522

16-
actual override fun toDouble(): Double {
17-
TODO("Not yet implemented")
18-
}
23+
public actual constructor(value: String) : this(IonSpinBigInteger.parseString(value, 10))
24+
public actual constructor(bytes: ByteArray) : this(IonSpinBigInteger.fromTwosComplementByteArray(bytes))
1925

20-
actual override fun toFloat(): Float {
21-
TODO("Not yet implemented")
22-
}
26+
actual override fun toByte(): Byte = delegate.byteValue(exactRequired = false)
27+
actual override fun toDouble(): Double = delegate.doubleValue(exactRequired = false)
28+
actual override fun toFloat(): Float = delegate.floatValue(exactRequired = false)
29+
actual override fun toInt(): Int = delegate.intValue(exactRequired = false)
30+
actual override fun toLong(): Long = delegate.longValue(exactRequired = false)
31+
actual override fun toShort(): Short = delegate.shortValue(exactRequired = false)
2332

24-
actual override fun toInt(): Int {
25-
TODO("Not yet implemented")
26-
}
33+
public actual operator fun plus(other: BigInteger): BigInteger =
34+
coalesceOrCreate(delegate + other.delegate, this, other)
2735

28-
actual override fun toLong(): Long {
29-
TODO("Not yet implemented")
30-
}
31-
32-
actual override fun toShort(): Short {
33-
TODO("Not yet implemented")
34-
}
36+
public actual operator fun minus(other: BigInteger): BigInteger =
37+
coalesceOrCreate(delegate - other.delegate, this, other)
3538

36-
public actual operator fun plus(other: BigInteger): BigInteger = TODO("Not yet implemented")
37-
public actual operator fun minus(other: BigInteger): BigInteger = TODO("Not yet implemented")
38-
public actual fun toByteArray(): ByteArray = TODO("Not yet implemented")
39-
actual override fun compareTo(other: BigInteger): Int = TODO("Not yet implemented")
39+
public actual fun toByteArray(): ByteArray = delegate.toTwosComplementByteArray()
40+
actual override fun compareTo(other: BigInteger): Int = delegate.compare(other.delegate)
4041
}

0 commit comments

Comments
 (0)