Make Float values in Memory be actually stored with Double

This commit is contained in:
Iaroslav Postovalov 2020-12-10 19:17:04 +07:00
parent 3adbec15cb
commit 08afa59dcc
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
5 changed files with 19 additions and 20 deletions

View File

@ -56,6 +56,8 @@ public interface MemoryReader {
/**
* Reads [Float] at certain [offset].
*
* **Warning: since JS can't handle Float properly, the size of segment read by this function is 8 bytes.**
*/
public fun readFloat(offset: Int): Float
@ -112,6 +114,8 @@ public interface MemoryWriter {
/**
* Writes [Float] at certain [offset].
*
* **Warning: since JS can't handle Float properly, the size of segment written by this function is 8 bytes.**
*/
public fun writeFloat(offset: Int, value: Float)

View File

@ -54,20 +54,15 @@ internal class MemoryTest {
fun rwFloat() {
val mem = Memory.allocate(64)
mem.write { writeFloat(0, 12.12345f) }
println(1)
assertEquals(12.12345027923584, mem.read { readFloat(0) }.toDouble())
mem.write { writeFloat(4, -313.13f) }
println(2)
assertEquals(-313.1300048828125, mem.read { readFloat(4) }.toDouble())
mem.write { writeFloat(8, Float.NaN) }
println(3)
assertEquals(Float.NaN, mem.read { readFloat(8) })
mem.write { writeFloat(12, Float.POSITIVE_INFINITY) }
println(4)
assertEquals(Float.POSITIVE_INFINITY, mem.read { readFloat(12) })
mem.write { writeFloat(12, Float.NEGATIVE_INFINITY) }
println(5)
assertEquals(Float.NEGATIVE_INFINITY, mem.read { readFloat(12) })
assertEquals(12.12345f, mem.read { readFloat(0) })
mem.write { writeFloat(8, -313.13f) }
assertEquals(-313.13f, mem.read { readFloat(8) })
mem.write { writeFloat(16, Float.NaN) }
assertEquals(Float.NaN, mem.read { readFloat(16) })
mem.write { writeFloat(24, Float.POSITIVE_INFINITY) }
assertEquals(Float.POSITIVE_INFINITY, mem.read { readFloat(24) })
mem.write { writeFloat(32, Float.NEGATIVE_INFINITY) }
assertEquals(Float.NEGATIVE_INFINITY, mem.read { readFloat(32) })
}
@Test

View File

@ -30,7 +30,7 @@ private class DataViewMemory(val view: DataView) : Memory {
override fun readDouble(offset: Int): Double = view.getFloat64(offset, true)
override fun readFloat(offset: Int): Float = view.getFloat32(offset, true)
override fun readFloat(offset: Int): Float = view.getFloat64(offset, true).toFloat()
override fun readByte(offset: Int): Byte = view.getInt8(offset)
@ -56,7 +56,7 @@ private class DataViewMemory(val view: DataView) : Memory {
}
override fun writeFloat(offset: Int, value: Float) {
view.setFloat32(offset, value, true)
view.setFloat64(offset, value.toDouble(), true)
}
override fun writeByte(offset: Int, value: Byte) {

View File

@ -44,7 +44,7 @@ internal class ByteBufferMemory(
override fun readDouble(offset: Int) = buffer.getDouble(position(offset))
override fun readFloat(offset: Int) = buffer.getFloat(position(offset))
override fun readFloat(offset: Int) = buffer.getDouble(position(offset)).toFloat()
override fun readByte(offset: Int) = buffer.get(position(offset))
@ -69,7 +69,7 @@ internal class ByteBufferMemory(
}
override fun writeFloat(offset: Int, value: Float) {
buffer.putFloat(position(offset), value)
buffer.putDouble(position(offset), value.toDouble())
}
override fun writeByte(offset: Int, value: Byte) {

View File

@ -31,7 +31,7 @@ internal class NativeMemory(
override fun readDouble(offset: Int) = array.getDoubleAt(position(offset))
override fun readFloat(offset: Int) = array.getFloatAt(position(offset))
override fun readFloat(offset: Int) = array.getDoubleAt(position(offset)).toFloat()
override fun readByte(offset: Int) = array[position(offset)]
@ -56,7 +56,7 @@ internal class NativeMemory(
}
override fun writeFloat(offset: Int, value: Float) {
array.setFloatAt(position(offset), value)
array.setDoubleAt(position(offset), value.toDouble())
}
override fun writeByte(offset: Int, value: Byte) {