switch to signed-first approach
All checks were successful
/ test (pull_request) Successful in 2m47s

It turns out signed math is more performant
This commit is contained in:
2026-01-15 14:10:14 +01:00
parent d06e523914
commit ff0e17b5ad
35 changed files with 862 additions and 826 deletions

View File

@@ -2,32 +2,33 @@ package com.infendro.bytes.int
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.uint.copyInto
fun Int.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toUInt().copyInto(destination, destinationOffset, order)
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun Int.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = toUInt().copyInto(destination, destinationOffset, order)
) {
require(destinationOffset in 0..(destination.size - 4))
fun IntArray.copyInto(
when (order) {
BIG_ENDIAN -> {
destination[destinationOffset] = (this shr 24).toByte()
destination[destinationOffset + 1] = (this shr 16).toByte()
destination[destinationOffset + 2] = (this shr 8).toByte()
destination[destinationOffset + 3] = this.toByte()
}
LITTLE_ENDIAN -> {
destination[destinationOffset] = this.toByte()
destination[destinationOffset + 1] = (this shr 8).toByte()
destination[destinationOffset + 2] = (this shr 16).toByte()
destination[destinationOffset + 3] = (this shr 24).toByte()
}
}
}
fun Int.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
fun IntArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = asUIntArray().copyInto(destination, destinationOffset, startIndex, endIndex, order)
) = copyInto(destination.asByteArray(), destinationOffset, order)