Skip to content

Commit 0bb7540

Browse files
authored
Refactor endian API to include working with arrays (#5)
1 parent e50a68e commit 0bb7540

File tree

7 files changed

+2212
-846
lines changed

7 files changed

+2212
-846
lines changed

benchmarks/src/commonMain/kotlin/org/kotlincrypto/bitops/benchmarks/EndianOps.kt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ package org.kotlincrypto.bitops.benchmarks
1919

2020
import kotlinx.benchmark.*
2121
import org.kotlincrypto.bitops.endian.Endian
22+
import kotlin.random.Random
2223

2324
abstract class EndianBenchmarkBase(private val endian: Endian) {
2425

25-
private val buf = ByteArray(Int.SIZE_BYTES) { (it - 100).toByte() }
26+
private val bytes = Random.Default.nextBytes(Int.SIZE_BYTES * 4)
27+
private val ints = IntArray(bytes.size / 4) { Random.Default.nextInt() }
2628

2729
@Benchmark
2830
fun intFrom() {
29-
endian.intFrom(source = buf, offset = 0)
31+
endian.intFrom(source = bytes, offset = 0)
3032
}
3133

3234
@Benchmark
@@ -36,22 +38,42 @@ abstract class EndianBenchmarkBase(private val endian: Endian) {
3638

3739
@Benchmark
3840
fun packAll() {
39-
endian.pack(-42, buf, 0)
41+
endian.pack(-42, bytes, 0)
4042
}
4143

4244
@Benchmark
4345
fun packAllUnsafe() {
44-
endian.packUnsafe(-42, buf, 0)
46+
endian.packUnsafe(-42, bytes, 0)
47+
}
48+
49+
@Benchmark
50+
fun packArray() {
51+
endian.pack(source = bytes, dest = ints, destOffset = 0, sourceIndexStart = 0, sourceIndexEnd = bytes.size)
52+
}
53+
54+
@Benchmark
55+
fun packArrayUnsafe() {
56+
endian.packUnsafe(source = bytes, dest = ints, destOffset = 0, sourceIndexStart = 0, sourceIndexEnd = bytes.size)
4557
}
4658

4759
@Benchmark
4860
fun packPartial() {
49-
endian.pack(-42, buf, 0, startIndex = 1)
61+
endian.pack(-42, bytes, 0, sourceIndexStart = 1)
5062
}
5163

5264
@Benchmark
5365
fun packPartialUnsafe() {
54-
endian.packUnsafe(-42, buf, 0, startIndex = 1)
66+
endian.packUnsafe(-42, bytes, 0, sourceIndexStart = 1)
67+
}
68+
69+
@Benchmark
70+
fun unpackArray() {
71+
endian.pack(source = ints, dest = bytes, destOffset = 0)
72+
}
73+
74+
@Benchmark
75+
fun unpackArrayUnsafe() {
76+
endian.packUnsafe(source = ints, dest = bytes, destOffset = 0)
5577
}
5678
}
5779

library/endian/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ arrayOf(Endian.Big, Endian.Little).forEach { endian ->
1010
println(endian)
1111

1212
// Short
13-
endian.pack(target = 3883.toShort(), dest = b, destOffset = 0)
13+
endian.pack(source = 3883.toShort(), dest = b, destOffset = 0)
1414
println(b.toList())
1515
println(endian.shortFrom(source = b, offset = 0))
1616
// Alternatively: endian.shortOf(b[0], b[1])
1717

1818
// Int
19-
endian.pack(target = 3883541, dest = b, destOffset = 0)
19+
endian.pack(source = 3883541, dest = b, destOffset = 0)
2020
println(b.toList())
2121
println(endian.intFrom(source = b, offset = 0))
2222
// Alternatively: endian.intOf(b[0], b[1], b[2], b[3])
2323

2424
// Long
25-
endian.pack(target = 1948571948571333L, dest = b, destOffset = 0)
25+
endian.pack(source = 1948571948571333L, dest = b, destOffset = 0)
2626
println(b.toList())
2727
println(endian.longFrom(source = b, offset = 0))
2828
// Alternatively: endian.longOf(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])

library/endian/api/endian.api

Lines changed: 102 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,36 @@ public abstract class org/kotlincrypto/bitops/endian/Endian {
99
public final fun pack (J[BIII)[B
1010
public final fun pack (S[BI)[B
1111
public final fun pack (S[BIII)[B
12+
public final fun pack ([B[IIII)[I
13+
public final fun pack ([B[JIII)[J
14+
public final fun pack ([B[SIII)[S
15+
public final fun pack ([I[BIII)[B
16+
public final fun pack ([J[BIII)[B
17+
public final fun pack ([S[BIII)[B
1218
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;I[BIIIILjava/lang/Object;)[B
1319
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;J[BIIIILjava/lang/Object;)[B
1420
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;S[BIIIILjava/lang/Object;)[B
21+
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;[I[BIIIILjava/lang/Object;)[B
22+
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;[J[BIIIILjava/lang/Object;)[B
23+
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;[S[BIIIILjava/lang/Object;)[B
1524
public abstract fun packUnsafe (I[BI)[B
1625
public abstract fun packUnsafe (I[BIII)[B
1726
public abstract fun packUnsafe (J[BI)[B
1827
public abstract fun packUnsafe (J[BIII)[B
1928
public abstract fun packUnsafe (S[BI)[B
2029
public abstract fun packUnsafe (S[BIII)[B
30+
public abstract fun packUnsafe ([B[IIII)[I
31+
public abstract fun packUnsafe ([B[JIII)[J
32+
public abstract fun packUnsafe ([B[SIII)[S
33+
public abstract fun packUnsafe ([I[BIII)[B
34+
public abstract fun packUnsafe ([J[BIII)[B
35+
public abstract fun packUnsafe ([S[BIII)[B
2136
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;I[BIIIILjava/lang/Object;)[B
2237
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;J[BIIIILjava/lang/Object;)[B
2338
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;S[BIIIILjava/lang/Object;)[B
39+
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;[I[BIIIILjava/lang/Object;)[B
40+
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;[J[BIIIILjava/lang/Object;)[B
41+
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;[S[BIIIILjava/lang/Object;)[B
2442
public abstract fun shortFrom ([BI)S
2543
public abstract fun shortOf (BB)S
2644
public final fun toString ()Ljava/lang/String;
@@ -30,24 +48,42 @@ public final class org/kotlincrypto/bitops/endian/Endian$Big : org/kotlincrypto/
3048
public static final field INSTANCE Lorg/kotlincrypto/bitops/endian/Endian$Big;
3149
public static final fun beIntAt ([BI)I
3250
public static final fun beLongAt ([BI)J
33-
public static final fun bePack ([BII)[B
34-
public static final fun bePack ([BIIII)[B
35-
public static final fun bePack ([BJI)[B
36-
public static final fun bePack ([BJIII)[B
37-
public static final fun bePack ([BSI)[B
38-
public static final fun bePack ([BSIII)[B
39-
public static synthetic fun bePack$default ([BIIIIILjava/lang/Object;)[B
40-
public static synthetic fun bePack$default ([BJIIIILjava/lang/Object;)[B
41-
public static synthetic fun bePack$default ([BSIIIILjava/lang/Object;)[B
42-
public static final fun bePackUnsafe ([BII)[B
43-
public static final fun bePackUnsafe ([BIIII)[B
44-
public static final fun bePackUnsafe ([BJI)[B
45-
public static final fun bePackUnsafe ([BJIII)[B
46-
public static final fun bePackUnsafe ([BSI)[B
47-
public static final fun bePackUnsafe ([BSIII)[B
48-
public static synthetic fun bePackUnsafe$default ([BIIIIILjava/lang/Object;)[B
49-
public static synthetic fun bePackUnsafe$default ([BJIIIILjava/lang/Object;)[B
50-
public static synthetic fun bePackUnsafe$default ([BSIIIILjava/lang/Object;)[B
51+
public static final fun bePackInto (I[BI)[B
52+
public static final fun bePackInto (I[BIII)[B
53+
public static final fun bePackInto (J[BI)[B
54+
public static final fun bePackInto (J[BIII)[B
55+
public static final fun bePackInto (S[BI)[B
56+
public static final fun bePackInto (S[BIII)[B
57+
public static final fun bePackInto ([B[IIII)[I
58+
public static final fun bePackInto ([B[JIII)[J
59+
public static final fun bePackInto ([B[SIII)[S
60+
public static final fun bePackInto ([I[BIII)[B
61+
public static final fun bePackInto ([J[BIII)[B
62+
public static final fun bePackInto ([S[BIII)[B
63+
public static synthetic fun bePackInto$default (I[BIIIILjava/lang/Object;)[B
64+
public static synthetic fun bePackInto$default (J[BIIIILjava/lang/Object;)[B
65+
public static synthetic fun bePackInto$default (S[BIIIILjava/lang/Object;)[B
66+
public static synthetic fun bePackInto$default ([I[BIIIILjava/lang/Object;)[B
67+
public static synthetic fun bePackInto$default ([J[BIIIILjava/lang/Object;)[B
68+
public static synthetic fun bePackInto$default ([S[BIIIILjava/lang/Object;)[B
69+
public static final fun bePackIntoUnsafe (I[BI)[B
70+
public static final fun bePackIntoUnsafe (I[BIII)[B
71+
public static final fun bePackIntoUnsafe (J[BI)[B
72+
public static final fun bePackIntoUnsafe (J[BIII)[B
73+
public static final fun bePackIntoUnsafe (S[BI)[B
74+
public static final fun bePackIntoUnsafe (S[BIII)[B
75+
public static final fun bePackIntoUnsafe ([B[IIII)[I
76+
public static final fun bePackIntoUnsafe ([B[JIII)[J
77+
public static final fun bePackIntoUnsafe ([B[SIII)[S
78+
public static final fun bePackIntoUnsafe ([I[BIII)[B
79+
public static final fun bePackIntoUnsafe ([J[BIII)[B
80+
public static final fun bePackIntoUnsafe ([S[BIII)[B
81+
public static synthetic fun bePackIntoUnsafe$default (I[BIIIILjava/lang/Object;)[B
82+
public static synthetic fun bePackIntoUnsafe$default (J[BIIIILjava/lang/Object;)[B
83+
public static synthetic fun bePackIntoUnsafe$default (S[BIIIILjava/lang/Object;)[B
84+
public static synthetic fun bePackIntoUnsafe$default ([I[BIIIILjava/lang/Object;)[B
85+
public static synthetic fun bePackIntoUnsafe$default ([J[BIIIILjava/lang/Object;)[B
86+
public static synthetic fun bePackIntoUnsafe$default ([S[BIIIILjava/lang/Object;)[B
5187
public static final fun beShortAt ([BI)S
5288
public fun equals (Ljava/lang/Object;)Z
5389
public fun hashCode ()I
@@ -61,6 +97,12 @@ public final class org/kotlincrypto/bitops/endian/Endian$Big : org/kotlincrypto/
6197
public fun packUnsafe (J[BIII)[B
6298
public fun packUnsafe (S[BI)[B
6399
public fun packUnsafe (S[BIII)[B
100+
public fun packUnsafe ([B[IIII)[I
101+
public fun packUnsafe ([B[JIII)[J
102+
public fun packUnsafe ([B[SIII)[S
103+
public fun packUnsafe ([I[BIII)[B
104+
public fun packUnsafe ([J[BIII)[B
105+
public fun packUnsafe ([S[BIII)[B
64106
public fun shortFrom ([BI)S
65107
public fun shortOf (BB)S
66108
}
@@ -73,24 +115,42 @@ public final class org/kotlincrypto/bitops/endian/Endian$Little : org/kotlincryp
73115
public fun intOf (BBBB)I
74116
public static final fun leIntAt ([BI)I
75117
public static final fun leLongAt ([BI)J
76-
public static final fun lePack ([BII)[B
77-
public static final fun lePack ([BIIII)[B
78-
public static final fun lePack ([BJI)[B
79-
public static final fun lePack ([BJIII)[B
80-
public static final fun lePack ([BSI)[B
81-
public static final fun lePack ([BSIII)[B
82-
public static synthetic fun lePack$default ([BIIIIILjava/lang/Object;)[B
83-
public static synthetic fun lePack$default ([BJIIIILjava/lang/Object;)[B
84-
public static synthetic fun lePack$default ([BSIIIILjava/lang/Object;)[B
85-
public static final fun lePackUnsafe ([BII)[B
86-
public static final fun lePackUnsafe ([BIIII)[B
87-
public static final fun lePackUnsafe ([BJI)[B
88-
public static final fun lePackUnsafe ([BJIII)[B
89-
public static final fun lePackUnsafe ([BSI)[B
90-
public static final fun lePackUnsafe ([BSIII)[B
91-
public static synthetic fun lePackUnsafe$default ([BIIIIILjava/lang/Object;)[B
92-
public static synthetic fun lePackUnsafe$default ([BJIIIILjava/lang/Object;)[B
93-
public static synthetic fun lePackUnsafe$default ([BSIIIILjava/lang/Object;)[B
118+
public static final fun lePackInto (I[BI)[B
119+
public static final fun lePackInto (I[BIII)[B
120+
public static final fun lePackInto (J[BI)[B
121+
public static final fun lePackInto (J[BIII)[B
122+
public static final fun lePackInto (S[BI)[B
123+
public static final fun lePackInto (S[BIII)[B
124+
public static final fun lePackInto ([B[IIII)[I
125+
public static final fun lePackInto ([B[JIII)[J
126+
public static final fun lePackInto ([B[SIII)[S
127+
public static final fun lePackInto ([I[BIII)[B
128+
public static final fun lePackInto ([J[BIII)[B
129+
public static final fun lePackInto ([S[BIII)[B
130+
public static synthetic fun lePackInto$default (I[BIIIILjava/lang/Object;)[B
131+
public static synthetic fun lePackInto$default (J[BIIIILjava/lang/Object;)[B
132+
public static synthetic fun lePackInto$default (S[BIIIILjava/lang/Object;)[B
133+
public static synthetic fun lePackInto$default ([I[BIIIILjava/lang/Object;)[B
134+
public static synthetic fun lePackInto$default ([J[BIIIILjava/lang/Object;)[B
135+
public static synthetic fun lePackInto$default ([S[BIIIILjava/lang/Object;)[B
136+
public static final fun lePackIntoUnsafe (I[BI)[B
137+
public static final fun lePackIntoUnsafe (I[BIII)[B
138+
public static final fun lePackIntoUnsafe (J[BI)[B
139+
public static final fun lePackIntoUnsafe (J[BIII)[B
140+
public static final fun lePackIntoUnsafe (S[BI)[B
141+
public static final fun lePackIntoUnsafe (S[BIII)[B
142+
public static final fun lePackIntoUnsafe ([B[IIII)[I
143+
public static final fun lePackIntoUnsafe ([B[JIII)[J
144+
public static final fun lePackIntoUnsafe ([B[SIII)[S
145+
public static final fun lePackIntoUnsafe ([I[BIII)[B
146+
public static final fun lePackIntoUnsafe ([J[BIII)[B
147+
public static final fun lePackIntoUnsafe ([S[BIII)[B
148+
public static synthetic fun lePackIntoUnsafe$default (I[BIIIILjava/lang/Object;)[B
149+
public static synthetic fun lePackIntoUnsafe$default (J[BIIIILjava/lang/Object;)[B
150+
public static synthetic fun lePackIntoUnsafe$default (S[BIIIILjava/lang/Object;)[B
151+
public static synthetic fun lePackIntoUnsafe$default ([I[BIIIILjava/lang/Object;)[B
152+
public static synthetic fun lePackIntoUnsafe$default ([J[BIIIILjava/lang/Object;)[B
153+
public static synthetic fun lePackIntoUnsafe$default ([S[BIIIILjava/lang/Object;)[B
94154
public static final fun leShortAt ([BI)S
95155
public fun longFrom ([BI)J
96156
public fun longOf (BBBBBBBB)J
@@ -100,6 +160,12 @@ public final class org/kotlincrypto/bitops/endian/Endian$Little : org/kotlincryp
100160
public fun packUnsafe (J[BIII)[B
101161
public fun packUnsafe (S[BI)[B
102162
public fun packUnsafe (S[BIII)[B
163+
public fun packUnsafe ([B[IIII)[I
164+
public fun packUnsafe ([B[JIII)[J
165+
public fun packUnsafe ([B[SIII)[S
166+
public fun packUnsafe ([I[BIII)[B
167+
public fun packUnsafe ([J[BIII)[B
168+
public fun packUnsafe ([S[BIII)[B
103169
public fun shortFrom ([BI)S
104170
public fun shortOf (BB)S
105171
}

0 commit comments

Comments
 (0)