Skip to content
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

Reformat shifting bytes #152

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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 @@ -125,59 +125,99 @@ fun Long.toTwosComplementByteArray() = when {
fun Int.toTwosComplementByteArray() = toLong().toTwosComplementByteArray()

fun Int.Companion.fromTwosComplementByteArray(it: ByteArray) = when (it.size) {
4 -> (it[0].toInt() shl 24) or (it[1].toUByte().toInt() shl 16) or (it[2].toUByte()
.toInt() shl 8) or (it[3].toUByte().toInt())
4 -> it[0].shiftLeftFirstInt(24) or
(it[1] shiftLeftAsInt 16) or
(it[2] shiftLeftAsInt 8) or
(it[3] shiftLeftAsInt 0)

3 -> (it[0].toInt() shl 16) or (it[1].toUByte().toInt() shl 8) or (it[2].toUByte().toInt())
2 -> (it[0].toInt() shl 8) or (it[1].toUByte().toInt() shl 0)
1 -> (it[0].toInt())
3 -> it[0].shiftLeftFirstInt(16) or
(it[1] shiftLeftAsInt 8) or
(it[2] shiftLeftAsInt 0)

2 -> it[0].shiftLeftFirstInt(8) or
(it[1] shiftLeftAsInt 0)

1 -> it[0].shiftLeftFirstInt(0)
else -> throw IllegalArgumentException("Input with size $it is out of bounds for Int")
}

private infix fun Byte.shiftLeftAsInt(shift: Int) = this.toUByte().toInt() shl shift
private fun Byte.shiftLeftFirstInt(shift: Int) = toInt() shl shift

fun UInt.Companion.fromTwosComplementByteArray(it: ByteArray) =
Long.fromTwosComplementByteArray(it).let {
require((0 <= it) && (it <= 0xFFFFFFFFL)) { "Value $it is out of bounds for UInt" }
it.toUInt()
}

fun Long.Companion.fromTwosComplementByteArray(it: ByteArray) = when (it.size) {
8 -> (it[0].toLong() shl 56) or (it[1].toUByte().toLong() shl 48) or (it[2].toUByte().toLong() shl 40) or
(it[3].toUByte().toLong() shl 32) or (it[4].toUByte().toLong() shl 24) or
(it[5].toUByte().toLong() shl 16) or (it[6].toUByte().toLong() shl 8) or (it[7].toUByte().toLong())

7 -> (it[0].toLong() shl 48) or (it[1].toUByte().toLong() shl 40) or (it[2].toUByte().toLong() shl 32) or
(it[3].toUByte().toLong() shl 24) or (it[4].toUByte().toLong() shl 16) or
(it[5].toUByte().toLong() shl 8) or (it[6].toUByte().toLong())

6 -> (it[0].toLong() shl 40) or (it[1].toUByte().toLong() shl 32) or (it[2].toUByte().toLong() shl 24) or
(it[3].toUByte().toLong() shl 16) or (it[4].toUByte().toLong() shl 8) or (it[5].toUByte().toLong())

5 -> (it[0].toLong() shl 32) or (it[1].toUByte().toLong() shl 24) or (it[2].toUByte().toLong() shl 16) or
(it[3].toUByte().toLong() shl 8) or (it[4].toUByte().toLong())

4 -> (it[0].toLong() shl 24) or (it[1].toUByte().toLong() shl 16) or (it[2].toUByte().toLong() shl 8) or
(it[3].toUByte().toLong())

3 -> (it[0].toLong() shl 16) or (it[1].toUByte().toLong() shl 8) or (it[2].toUByte().toLong())
2 -> (it[0].toLong() shl 8) or (it[1].toUByte().toLong() shl 0)
1 -> (it[0].toLong())
8 -> it[0].shiftLeftFirstLong(56) or
(it[1] shiftLeftAsLong 48) or
(it[2] shiftLeftAsLong 40) or
(it[3] shiftLeftAsLong 32) or
(it[4] shiftLeftAsLong 24) or
(it[5] shiftLeftAsLong 16) or
(it[6] shiftLeftAsLong 8) or
(it[7] shiftLeftAsLong 0)

7 -> it[0].shiftLeftFirstLong(48) or
(it[1] shiftLeftAsLong 40) or
(it[2] shiftLeftAsLong 32) or
(it[3] shiftLeftAsLong 24) or
(it[4] shiftLeftAsLong 16) or
(it[5] shiftLeftAsLong 8) or
(it[6] shiftLeftAsLong 0)

6 -> it[0].shiftLeftFirstLong(40) or
(it[1] shiftLeftAsLong 32) or
(it[2] shiftLeftAsLong 24) or
(it[3] shiftLeftAsLong 16) or
(it[4] shiftLeftAsLong 8) or
(it[5] shiftLeftAsLong 0)

5 -> it[0].shiftLeftFirstLong(32) or
(it[1] shiftLeftAsLong 24) or
(it[2] shiftLeftAsLong 16) or
(it[3] shiftLeftAsLong 8) or
(it[4] shiftLeftAsLong 0)

4 -> it[0].shiftLeftFirstLong(24) or
(it[1] shiftLeftAsLong 16) or
(it[2] shiftLeftAsLong 8) or
(it[3] shiftLeftAsLong 0)

3 -> it[0].shiftLeftFirstLong(16) or
(it[1] shiftLeftAsLong 8) or
(it[2] shiftLeftAsLong 0)

2 -> it[0].shiftLeftFirstLong(8) or
(it[1] shiftLeftAsLong 0)

1 -> it[0].shiftLeftFirstLong(0)
else -> throw IllegalArgumentException("Input with size $it is out of bounds for Long")
}

private infix fun Byte.shiftLeftAsLong(shift: Int) = this.toUByte().toLong() shl shift
private fun Byte.shiftLeftFirstLong(shift: Int) = toLong() shl shift

fun ULong.Companion.fromTwosComplementByteArray(it: ByteArray) = when {
((it.size == 9) && (it[0] == 0.toByte())) ->
(it[1].toUByte().toULong() shl 56) or (it[2].toUByte().toULong() shl 48) or (it[3].toUByte()
.toULong() shl 40) or
(it[4].toUByte().toULong() shl 32) or (it[5].toUByte().toULong() shl 24) or
(it[6].toUByte().toULong() shl 16) or (it[7].toUByte().toULong() shl 8) or
(it[8].toUByte().toULong())
((it.size == 9) && (it[0] == 0.toByte())) -> it.shiftLeftAsULong(1, 56) or
it.shiftLeftAsULong(2, 48) or
it.shiftLeftAsULong(3, 40) or
it.shiftLeftAsULong(4, 32) or
it.shiftLeftAsULong(5, 24) or
it.shiftLeftAsULong(6, 16) or
it.shiftLeftAsULong(7, 8) or
it.shiftLeftAsULong(8, 0)

else -> Long.fromTwosComplementByteArray(it).let {
require(it >= 0) { "Value $it is out of bounds for ULong" }
it.toULong()
}
}

private fun ByteArray.shiftLeftAsULong(index: Int, shift: Int) = this[index].toUByte().toULong() shl shift

/** Encodes an unsigned Long to a minimum-size unsigned byte array */
fun Long.toUnsignedByteArray(): ByteArray {
require(this >= 0)
Expand Down
Loading