Skip to content

Commit

Permalink
Fixes native
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jul 25, 2024
1 parent 49987cd commit bd2793b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions korlibs-memory/src/korlibs/memory/_Memory_Internal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ internal fun Int.extractByte(offset: Int): Byte = (this ushr offset).toByte()

/** Takes n[bits] of [this] [Int], and extends the last bit, creating a plain [Int] in one's complement */
internal fun Int.signExtend(bits: Int): Int = (this shl (32 - bits)) shr (32 - bits) // Int.SIZE_BITS

/** Reverses the bytes of [this] [Short]: AABB -> BBAA */
internal fun Short.reverseBytes(): Short {
val low = ((this.toInt() ushr 0) and 0xFF)
val high = ((this.toInt() ushr 8) and 0xFF)
return ((high and 0xFF) or (low shl 8)).toShort()
}

/** Reverses the bytes of [this] [Int]: AABBCCDD -> DDCCBBAA */
internal fun Int.reverseBytes(): Int {
val v0 = ((this ushr 0) and 0xFF)
val v1 = ((this ushr 8) and 0xFF)
val v2 = ((this ushr 16) and 0xFF)
val v3 = ((this ushr 24) and 0xFF)
return (v0 shl 24) or (v1 shl 16) or (v2 shl 8) or (v3 shl 0)
}

/** Reverses the bytes of [this] [Long]: AABBCCDDEEFFGGHH -> HHGGFFEEDDCCBBAA */
internal fun Long.reverseBytes(): Long {
val v0 = (this ushr 0).toInt().reverseBytes().toLong() and 0xFFFFFFFFL
val v1 = (this ushr 32).toInt().reverseBytes().toLong() and 0xFFFFFFFFL
return (v0 shl 32) or (v1 shl 0)
}

1 change: 1 addition & 0 deletions korlibs-memory/src@native/korlibs/memory/Buffer.native.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package korlibs.memory

import korlibs.memory.internal.*
import kotlinx.cinterop.*
import platform.posix.*
import kotlin.experimental.*
Expand Down

0 comments on commit bd2793b

Please sign in to comment.