Skip to content

Commit 6eb2b31

Browse files
committed
Add missing primitives for reading/writing float and double
1 parent bde2816 commit 6eb2b31

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/java/org/truffleruby/extra/ffi/Pointer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public void writeLong(long offset, long value) {
8080
UNSAFE.putLong(address + offset, value);
8181
}
8282

83+
public void writeFloat(long offset, float value) {
84+
assert address + offset != 0;
85+
UNSAFE.putFloat(address + offset, value);
86+
}
87+
88+
public void writeDouble(long offset, double value) {
89+
assert address + offset != 0;
90+
UNSAFE.putDouble(address + offset, value);
91+
}
92+
8393
public void writePointer(long offset, Pointer value) {
8494
writeLong(offset, value.getAddress());
8595
}
@@ -147,6 +157,11 @@ public long readLong(long offset) {
147157
return UNSAFE.getLong(address + offset);
148158
}
149159

160+
public float readFloat(long offset) {
161+
assert address + offset != 0;
162+
return UNSAFE.getFloat(address + offset);
163+
}
164+
150165
public double readDouble(long offset) {
151166
assert address + offset != 0;
152167
return UNSAFE.getDouble(address + offset);

src/main/java/org/truffleruby/extra/ffi/PointerNodes.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,20 @@ private static Object readUnsignedLong(RubyContext context, Pointer ptr, int off
429429

430430
}
431431

432+
@Primitive(name = "pointer_read_float")
433+
public static abstract class PointerReadFloatNode extends PointerPrimitiveArrayArgumentsNode {
434+
435+
@Specialization
436+
public double readFloat(long address) {
437+
final Pointer ptr = new Pointer(address);
438+
checkNull(ptr);
439+
return ptr.readFloat(0);
440+
}
441+
442+
}
443+
432444
@Primitive(name = "pointer_read_double")
433-
public static abstract class PointerReadDoublePrimitiveNode extends PointerPrimitiveArrayArgumentsNode {
445+
public static abstract class PointerReadDoubleNode extends PointerPrimitiveArrayArgumentsNode {
434446

435447
@Specialization
436448
public double readDouble(long address) {
@@ -843,6 +855,32 @@ private static void writeUnsignedLong(Pointer ptr, int offset, DynamicObject val
843855

844856
}
845857

858+
@Primitive(name = "pointer_write_float")
859+
public static abstract class PointerWriteFloatNode extends PointerPrimitiveArrayArgumentsNode {
860+
861+
@Specialization
862+
public DynamicObject writeFloat(long address, double value) {
863+
final Pointer ptr = new Pointer(address);
864+
checkNull(ptr);
865+
ptr.writeFloat(0, (float) value);
866+
return nil();
867+
}
868+
869+
}
870+
871+
@Primitive(name = "pointer_write_double")
872+
public static abstract class PointerWriteDoubleNode extends PointerPrimitiveArrayArgumentsNode {
873+
874+
@Specialization
875+
public DynamicObject writeDouble(long address, double value) {
876+
final Pointer ptr = new Pointer(address);
877+
checkNull(ptr);
878+
ptr.writeDouble(0, value);
879+
return nil();
880+
}
881+
882+
}
883+
846884
@Primitive(name = "pointer_write_pointer", lowerFixnum = 2)
847885
public static abstract class PointerWritePointerPrimitiveNode extends PointerPrimitiveArrayArgumentsNode {
848886

0 commit comments

Comments
 (0)