aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/instruction/generic_cell_cast.cpp
blob: 16f47a56222dd0a7df09a35f95277eabcad6cd62 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "generic_cell_cast.h"
#include <vespa/eval/eval/value.h>
#include <vespa/eval/eval/wrap_param.h>
#include <vespa/vespalib/util/stash.h>
#include <vespa/vespalib/util/typify.h>
#include <cassert>

using namespace vespalib::eval::tensor_function;

namespace vespalib::eval::instruction {

using State = InterpretedFunction::State;
using Instruction = InterpretedFunction::Instruction;

namespace {

template <typename ICT, typename OCT>
void my_generic_cell_cast_op(State &state, uint64_t param_in) {
    const auto &res_type = unwrap_param<ValueType>(param_in);
    const Value &a = state.peek(0);
    auto input_cells = a.cells().typify<ICT>();
    auto output_cells = state.stash.create_uninitialized_array<OCT>(input_cells.size());
    auto pos = output_cells.begin();
    for (ICT value : input_cells) {
        *pos++ = (OCT) value;
    }
    assert(pos == output_cells.end());
    Value &result_ref = state.stash.create<ValueView>(res_type, a.index(), TypedCells(output_cells));
    state.pop_push(result_ref);
}

struct SelectGenericCellCastOp {
    template <typename ICT, typename OCT>
    static InterpretedFunction::op_function invoke() {
        if constexpr (std::is_same_v<ICT,OCT>) {
            // handeled by nop case below
            abort();
        } else {
            return my_generic_cell_cast_op<ICT, OCT>;
        }
    }
};

} // namespace <unnamed>

InterpretedFunction::Instruction
GenericCellCast::make_instruction(const ValueType &result_type,
                                  const ValueType &input_type,
                                  CellType to_cell_type,
                                  Stash &stash)
{
    assert(result_type == input_type.cell_cast(to_cell_type));
    auto from = input_type.cell_type();
    auto to = result_type.cell_type();
    if (to == from) {
        return Instruction::nop();
    } else {
        assert(!input_type.is_double());
        auto &param = stash.create<ValueType>(result_type);
        auto op = typify_invoke<2,TypifyCellType,SelectGenericCellCastOp>(from, to);
        return Instruction(op, wrap_param<ValueType>(param));
    }
}

} // namespace