Skip to content

Refactor endian API to include working with arrays #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ package org.kotlincrypto.bitops.benchmarks

import kotlinx.benchmark.*
import org.kotlincrypto.bitops.endian.Endian
import kotlin.random.Random

abstract class EndianBenchmarkBase(private val endian: Endian) {

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

@Benchmark
fun intFrom() {
endian.intFrom(source = buf, offset = 0)
endian.intFrom(source = bytes, offset = 0)
}

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

@Benchmark
fun packAll() {
endian.pack(-42, buf, 0)
endian.pack(-42, bytes, 0)
}

@Benchmark
fun packAllUnsafe() {
endian.packUnsafe(-42, buf, 0)
endian.packUnsafe(-42, bytes, 0)
}

@Benchmark
fun packArray() {
endian.pack(source = bytes, dest = ints, destOffset = 0, sourceIndexStart = 0, sourceIndexEnd = bytes.size)
}

@Benchmark
fun packArrayUnsafe() {
endian.packUnsafe(source = bytes, dest = ints, destOffset = 0, sourceIndexStart = 0, sourceIndexEnd = bytes.size)
}

@Benchmark
fun packPartial() {
endian.pack(-42, buf, 0, startIndex = 1)
endian.pack(-42, bytes, 0, sourceIndexStart = 1)
}

@Benchmark
fun packPartialUnsafe() {
endian.packUnsafe(-42, buf, 0, startIndex = 1)
endian.packUnsafe(-42, bytes, 0, sourceIndexStart = 1)
}

@Benchmark
fun unpackArray() {
endian.pack(source = ints, dest = bytes, destOffset = 0)
}

@Benchmark
fun unpackArrayUnsafe() {
endian.packUnsafe(source = ints, dest = bytes, destOffset = 0)
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/endian/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ arrayOf(Endian.Big, Endian.Little).forEach { endian ->
println(endian)

// Short
endian.pack(target = 3883.toShort(), dest = b, destOffset = 0)
endian.pack(source = 3883.toShort(), dest = b, destOffset = 0)
println(b.toList())
println(endian.shortFrom(source = b, offset = 0))
// Alternatively: endian.shortOf(b[0], b[1])

// Int
endian.pack(target = 3883541, dest = b, destOffset = 0)
endian.pack(source = 3883541, dest = b, destOffset = 0)
println(b.toList())
println(endian.intFrom(source = b, offset = 0))
// Alternatively: endian.intOf(b[0], b[1], b[2], b[3])

// Long
endian.pack(target = 1948571948571333L, dest = b, destOffset = 0)
endian.pack(source = 1948571948571333L, dest = b, destOffset = 0)
println(b.toList())
println(endian.longFrom(source = b, offset = 0))
// Alternatively: endian.longOf(b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7])
Expand Down
138 changes: 102 additions & 36 deletions library/endian/api/endian.api
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,36 @@ public abstract class org/kotlincrypto/bitops/endian/Endian {
public final fun pack (J[BIII)[B
public final fun pack (S[BI)[B
public final fun pack (S[BIII)[B
public final fun pack ([B[IIII)[I
public final fun pack ([B[JIII)[J
public final fun pack ([B[SIII)[S
public final fun pack ([I[BIII)[B
public final fun pack ([J[BIII)[B
public final fun pack ([S[BIII)[B
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;I[BIIIILjava/lang/Object;)[B
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;J[BIIIILjava/lang/Object;)[B
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;S[BIIIILjava/lang/Object;)[B
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;[I[BIIIILjava/lang/Object;)[B
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;[J[BIIIILjava/lang/Object;)[B
public static synthetic fun pack$default (Lorg/kotlincrypto/bitops/endian/Endian;[S[BIIIILjava/lang/Object;)[B
public abstract fun packUnsafe (I[BI)[B
public abstract fun packUnsafe (I[BIII)[B
public abstract fun packUnsafe (J[BI)[B
public abstract fun packUnsafe (J[BIII)[B
public abstract fun packUnsafe (S[BI)[B
public abstract fun packUnsafe (S[BIII)[B
public abstract fun packUnsafe ([B[IIII)[I
public abstract fun packUnsafe ([B[JIII)[J
public abstract fun packUnsafe ([B[SIII)[S
public abstract fun packUnsafe ([I[BIII)[B
public abstract fun packUnsafe ([J[BIII)[B
public abstract fun packUnsafe ([S[BIII)[B
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;I[BIIIILjava/lang/Object;)[B
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;J[BIIIILjava/lang/Object;)[B
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;S[BIIIILjava/lang/Object;)[B
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;[I[BIIIILjava/lang/Object;)[B
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;[J[BIIIILjava/lang/Object;)[B
public static synthetic fun packUnsafe$default (Lorg/kotlincrypto/bitops/endian/Endian;[S[BIIIILjava/lang/Object;)[B
public abstract fun shortFrom ([BI)S
public abstract fun shortOf (BB)S
public final fun toString ()Ljava/lang/String;
Expand All @@ -30,24 +48,42 @@ public final class org/kotlincrypto/bitops/endian/Endian$Big : org/kotlincrypto/
public static final field INSTANCE Lorg/kotlincrypto/bitops/endian/Endian$Big;
public static final fun beIntAt ([BI)I
public static final fun beLongAt ([BI)J
public static final fun bePack ([BII)[B
public static final fun bePack ([BIIII)[B
public static final fun bePack ([BJI)[B
public static final fun bePack ([BJIII)[B
public static final fun bePack ([BSI)[B
public static final fun bePack ([BSIII)[B
public static synthetic fun bePack$default ([BIIIIILjava/lang/Object;)[B
public static synthetic fun bePack$default ([BJIIIILjava/lang/Object;)[B
public static synthetic fun bePack$default ([BSIIIILjava/lang/Object;)[B
public static final fun bePackUnsafe ([BII)[B
public static final fun bePackUnsafe ([BIIII)[B
public static final fun bePackUnsafe ([BJI)[B
public static final fun bePackUnsafe ([BJIII)[B
public static final fun bePackUnsafe ([BSI)[B
public static final fun bePackUnsafe ([BSIII)[B
public static synthetic fun bePackUnsafe$default ([BIIIIILjava/lang/Object;)[B
public static synthetic fun bePackUnsafe$default ([BJIIIILjava/lang/Object;)[B
public static synthetic fun bePackUnsafe$default ([BSIIIILjava/lang/Object;)[B
public static final fun bePackInto (I[BI)[B
public static final fun bePackInto (I[BIII)[B
public static final fun bePackInto (J[BI)[B
public static final fun bePackInto (J[BIII)[B
public static final fun bePackInto (S[BI)[B
public static final fun bePackInto (S[BIII)[B
public static final fun bePackInto ([B[IIII)[I
public static final fun bePackInto ([B[JIII)[J
public static final fun bePackInto ([B[SIII)[S
public static final fun bePackInto ([I[BIII)[B
public static final fun bePackInto ([J[BIII)[B
public static final fun bePackInto ([S[BIII)[B
public static synthetic fun bePackInto$default (I[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackInto$default (J[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackInto$default (S[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackInto$default ([I[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackInto$default ([J[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackInto$default ([S[BIIIILjava/lang/Object;)[B
public static final fun bePackIntoUnsafe (I[BI)[B
public static final fun bePackIntoUnsafe (I[BIII)[B
public static final fun bePackIntoUnsafe (J[BI)[B
public static final fun bePackIntoUnsafe (J[BIII)[B
public static final fun bePackIntoUnsafe (S[BI)[B
public static final fun bePackIntoUnsafe (S[BIII)[B
public static final fun bePackIntoUnsafe ([B[IIII)[I
public static final fun bePackIntoUnsafe ([B[JIII)[J
public static final fun bePackIntoUnsafe ([B[SIII)[S
public static final fun bePackIntoUnsafe ([I[BIII)[B
public static final fun bePackIntoUnsafe ([J[BIII)[B
public static final fun bePackIntoUnsafe ([S[BIII)[B
public static synthetic fun bePackIntoUnsafe$default (I[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackIntoUnsafe$default (J[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackIntoUnsafe$default (S[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackIntoUnsafe$default ([I[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackIntoUnsafe$default ([J[BIIIILjava/lang/Object;)[B
public static synthetic fun bePackIntoUnsafe$default ([S[BIIIILjava/lang/Object;)[B
public static final fun beShortAt ([BI)S
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
Expand All @@ -61,6 +97,12 @@ public final class org/kotlincrypto/bitops/endian/Endian$Big : org/kotlincrypto/
public fun packUnsafe (J[BIII)[B
public fun packUnsafe (S[BI)[B
public fun packUnsafe (S[BIII)[B
public fun packUnsafe ([B[IIII)[I
public fun packUnsafe ([B[JIII)[J
public fun packUnsafe ([B[SIII)[S
public fun packUnsafe ([I[BIII)[B
public fun packUnsafe ([J[BIII)[B
public fun packUnsafe ([S[BIII)[B
public fun shortFrom ([BI)S
public fun shortOf (BB)S
}
Expand All @@ -73,24 +115,42 @@ public final class org/kotlincrypto/bitops/endian/Endian$Little : org/kotlincryp
public fun intOf (BBBB)I
public static final fun leIntAt ([BI)I
public static final fun leLongAt ([BI)J
public static final fun lePack ([BII)[B
public static final fun lePack ([BIIII)[B
public static final fun lePack ([BJI)[B
public static final fun lePack ([BJIII)[B
public static final fun lePack ([BSI)[B
public static final fun lePack ([BSIII)[B
public static synthetic fun lePack$default ([BIIIIILjava/lang/Object;)[B
public static synthetic fun lePack$default ([BJIIIILjava/lang/Object;)[B
public static synthetic fun lePack$default ([BSIIIILjava/lang/Object;)[B
public static final fun lePackUnsafe ([BII)[B
public static final fun lePackUnsafe ([BIIII)[B
public static final fun lePackUnsafe ([BJI)[B
public static final fun lePackUnsafe ([BJIII)[B
public static final fun lePackUnsafe ([BSI)[B
public static final fun lePackUnsafe ([BSIII)[B
public static synthetic fun lePackUnsafe$default ([BIIIIILjava/lang/Object;)[B
public static synthetic fun lePackUnsafe$default ([BJIIIILjava/lang/Object;)[B
public static synthetic fun lePackUnsafe$default ([BSIIIILjava/lang/Object;)[B
public static final fun lePackInto (I[BI)[B
public static final fun lePackInto (I[BIII)[B
public static final fun lePackInto (J[BI)[B
public static final fun lePackInto (J[BIII)[B
public static final fun lePackInto (S[BI)[B
public static final fun lePackInto (S[BIII)[B
public static final fun lePackInto ([B[IIII)[I
public static final fun lePackInto ([B[JIII)[J
public static final fun lePackInto ([B[SIII)[S
public static final fun lePackInto ([I[BIII)[B
public static final fun lePackInto ([J[BIII)[B
public static final fun lePackInto ([S[BIII)[B
public static synthetic fun lePackInto$default (I[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackInto$default (J[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackInto$default (S[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackInto$default ([I[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackInto$default ([J[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackInto$default ([S[BIIIILjava/lang/Object;)[B
public static final fun lePackIntoUnsafe (I[BI)[B
public static final fun lePackIntoUnsafe (I[BIII)[B
public static final fun lePackIntoUnsafe (J[BI)[B
public static final fun lePackIntoUnsafe (J[BIII)[B
public static final fun lePackIntoUnsafe (S[BI)[B
public static final fun lePackIntoUnsafe (S[BIII)[B
public static final fun lePackIntoUnsafe ([B[IIII)[I
public static final fun lePackIntoUnsafe ([B[JIII)[J
public static final fun lePackIntoUnsafe ([B[SIII)[S
public static final fun lePackIntoUnsafe ([I[BIII)[B
public static final fun lePackIntoUnsafe ([J[BIII)[B
public static final fun lePackIntoUnsafe ([S[BIII)[B
public static synthetic fun lePackIntoUnsafe$default (I[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackIntoUnsafe$default (J[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackIntoUnsafe$default (S[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackIntoUnsafe$default ([I[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackIntoUnsafe$default ([J[BIIIILjava/lang/Object;)[B
public static synthetic fun lePackIntoUnsafe$default ([S[BIIIILjava/lang/Object;)[B
public static final fun leShortAt ([BI)S
public fun longFrom ([BI)J
public fun longOf (BBBBBBBB)J
Expand All @@ -100,6 +160,12 @@ public final class org/kotlincrypto/bitops/endian/Endian$Little : org/kotlincryp
public fun packUnsafe (J[BIII)[B
public fun packUnsafe (S[BI)[B
public fun packUnsafe (S[BIII)[B
public fun packUnsafe ([B[IIII)[I
public fun packUnsafe ([B[JIII)[J
public fun packUnsafe ([B[SIII)[S
public fun packUnsafe ([I[BIII)[B
public fun packUnsafe ([J[BIII)[B
public fun packUnsafe ([S[BIII)[B
public fun shortFrom ([BI)S
public fun shortOf (BB)S
}
Expand Down
Loading
Loading