-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibFraction.java
More file actions
116 lines (97 loc) · 4.02 KB
/
Copy pathLibFraction.java
File metadata and controls
116 lines (97 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.nio.ByteBuffer;
import java.util.List;
import org.graalvm.nativeimage.IsolateThread;
import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.function.CEntryPoint;
import org.graalvm.nativeimage.c.function.CFunctionPointer;
import org.graalvm.nativeimage.c.function.InvokeCFunctionPointer;
import org.graalvm.nativeimage.c.struct.CField;
import org.graalvm.nativeimage.c.struct.CStruct;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.word.PointerBase;
@CContext(CHeaderDirectives.class)
@CStruct("Fraction")
interface Fraction extends PointerBase {
@CField("numerator")
int getNumerator();
@CField("numerator")
void setNumerator(int n);
@CField("denominator")
int getDenominator();
@CField("denominator")
void setDenominator(int d);
@CField("str")
CCharPointer getStr();
@CField("str")
void setStr(CCharPointer str);
@CField("print_func")
PrintFunc getPrintFunc();
@CField("print_func")
void setPrintFunc(PrintFunc func);
@CField("in_bytes")
CCharPointer getInBytes();
@CField("in_bytes")
void setInBytes(CCharPointer bytes);
@CField("out_bytes")
CCharPointer getOutBytes();
@CField("out_bytes")
void setOutBytes(CCharPointer bytes);
@CField("bytes_len")
long getBytesLen();
@CField("bytes_len")
void setBytesLen(long len);
}
interface PrintFunc extends CFunctionPointer {
@InvokeCFunctionPointer
void invoke(CCharPointer s);
}
class CHeaderDirectives implements CContext.Directives {
@Override
public List<String> getHeaderFiles() {
return List.of("<fraction.h>");
}
@Override
public List<String> getOptions() {
return List.of("-I" + Constants.PWD);
}
}
public class LibFraction {
@CEntryPoint(name = "graalvm_fraction_multiply")
public static int graalvmFractionMultiply(IsolateThread thread, Fraction frac1, Fraction frac2) {
if (frac1.isNonNull() && frac2.isNonNull()) {
int numerator = frac1.getNumerator() * frac2.getNumerator();
int denominator = frac1.getDenominator() * frac2.getDenominator();
frac1.setNumerator(numerator);
frac1.setDenominator(denominator);
if (frac1.getPrintFunc().isNonNull() && frac1.getStr().isNonNull()) {
frac1.getPrintFunc().invoke(frac1.getStr());
}
if (frac2.getPrintFunc().isNonNull() && frac2.getStr().isNonNull()) {
frac2.getPrintFunc().invoke(frac2.getStr());
}
ByteBuffer frac1InByteBuffer = CTypeConversion.asByteBuffer(frac1.getInBytes(), (int) frac1.getBytesLen());
ByteBuffer frac2InByteBuffer = CTypeConversion.asByteBuffer(frac2.getInBytes(), (int) frac2.getBytesLen());
byte[] frac1InBytes = new byte[frac1InByteBuffer.limit()];
byte[] frac2InBytes = new byte[frac2InByteBuffer.limit()];
frac1InByteBuffer.get(frac1InBytes);
frac2InByteBuffer.get(frac2InBytes);
// Assuming out_bytes has been allocated by the caller of this function!
ByteBuffer frac1OutByteBuffer = CTypeConversion.asByteBuffer(frac1.getOutBytes(), (int) frac1.getBytesLen());
ByteBuffer frac2OutByteBuffer = CTypeConversion.asByteBuffer(frac2.getOutBytes(), (int) frac2.getBytesLen());
int counter = 0;
for (int i = 0; i < frac1InBytes.length; i++) {
counter += frac1InBytes[i];
frac1OutByteBuffer.put((byte)(frac1InBytes[i] ^ 0x5c));
}
for (int i = 0; i < frac2InBytes.length; i++) {
counter += frac2InBytes[i];
frac2OutByteBuffer.put((byte)(frac2InBytes[i] ^ 0x5c));
}
System.out.println("The average of the bytes in frac1 and frac2 = " + counter / (frac1InBytes.length + frac2InBytes.length));
System.out.println("Finished with calculation!");
return 0;
}
return -1;
}
}