Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.NativeInterpreter/0.1/NativeInterpreter-0.1/src/interpreter.cpp @ 16269

Last change on this file since 16269 was 16269, checked in by bburlacu, 5 years ago

#2958: Add C++ source code

File size: 1.6 KB
RevLine 
[16269]1#include <memory>
2
3#include "interpreter.h"
4
5extern "C" {
6
7// slow (ish?)
8__declspec(dllexport)
9double __cdecl GetValue(instruction* code, int codeLength, int row) noexcept
10{
11    return evaluate(code, codeLength, row);
12}
13
14__declspec(dllexport)
15void __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)
24void __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
Note: See TracBrowser for help on using the repository browser.