1 | #include <memory>
|
---|
2 |
|
---|
3 | #include "interpreter.h"
|
---|
4 |
|
---|
5 | extern "C" {
|
---|
6 |
|
---|
7 | // slow (ish?)
|
---|
8 | __declspec(dllexport)
|
---|
9 | double __cdecl GetValue(instruction* code, int codeLength, int row) noexcept
|
---|
10 | {
|
---|
11 | return evaluate(code, codeLength, row);
|
---|
12 | }
|
---|
13 |
|
---|
14 | __declspec(dllexport)
|
---|
15 | void __cdecl GetValues(instruction* code, int codeLength, int* rows, int totalRows, double* result) noexcept
|
---|
16 | {
|
---|
17 | for (int i = 0; i < totalRows; ++i)
|
---|
18 | {
|
---|
19 | result[i] = evaluate(code, codeLength, rows[i]);
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | __declspec(dllexport)
|
---|
24 | void __cdecl GetValuesVectorized(instruction* code, int codeLength, int* rows, int totalRows, double* result) noexcept
|
---|
25 | {
|
---|
26 | std::vector<double[BUFSIZE]> buffers(codeLength);
|
---|
27 | // initialize instruction buffers
|
---|
28 | for (int i = 0; i < codeLength; ++i)
|
---|
29 | {
|
---|
30 | instruction& in = code[i];
|
---|
31 | in.buf = buffers[i];
|
---|
32 |
|
---|
33 | if (in.opcode == OpCodes::Const)
|
---|
34 | {
|
---|
35 | load(in.buf, in.value);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | int remainingRows = totalRows % BUFSIZE;
|
---|
40 | int total = totalRows - remainingRows;
|
---|
41 |
|
---|
42 | for (int rowIndex = 0; rowIndex < total; rowIndex += BUFSIZE)
|
---|
43 | {
|
---|
44 | evaluate(code, codeLength, rows, rowIndex, BUFSIZE);
|
---|
45 | std::memcpy(result + rowIndex, code[0].buf, BUFSIZE * sizeof(double));
|
---|
46 | }
|
---|
47 |
|
---|
48 | // are there any rows left?
|
---|
49 | if (remainingRows > 0) {
|
---|
50 | for (int rowIndex = total; rowIndex < totalRows; rowIndex += remainingRows)
|
---|
51 | {
|
---|
52 | evaluate(code, codeLength, rows, rowIndex, remainingRows);
|
---|
53 | std::memcpy(result + rowIndex, code[0].buf, remainingRows * sizeof(double));
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | #ifdef __cplusplus
|
---|
59 | }
|
---|
60 | #endif
|
---|