summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/compilation/simd/simd_bitwise_ops.c
blob: 66aef3637e498c7060c42c2918945b8bf4b6fcc4 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2019 Intel Corporation. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "simd_bitwise_ops.h"
#include "../aot_emit_exception.h"
#include "../../aot/aot_runtime.h"

static bool
v128_bitwise_two_component(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
                           V128Bitwise bitwise_op)
{
    LLVMValueRef vector1, vector2, result;

    POP_V128(vector2);
    POP_V128(vector1);

    switch (bitwise_op) {
        case V128_AND:
            if (!(result = LLVMBuildAnd(comp_ctx->builder, vector1, vector2,
                                        "and"))) {
                HANDLE_FAILURE("LLVMBuildAnd");
                goto fail;
            }
            break;
        case V128_OR:
            if (!(result =
                      LLVMBuildOr(comp_ctx->builder, vector1, vector2, "or"))) {
                HANDLE_FAILURE("LLVMBuildAnd");
                goto fail;
            }
            break;
        case V128_XOR:
            if (!(result = LLVMBuildXor(comp_ctx->builder, vector1, vector2,
                                        "xor"))) {
                HANDLE_FAILURE("LLVMBuildAnd");
                goto fail;
            }
            break;
        case V128_ANDNOT:
        {
            /* v128.and(a, v128.not(b)) */
            if (!(vector2 = LLVMBuildNot(comp_ctx->builder, vector2, "not"))) {
                HANDLE_FAILURE("LLVMBuildNot");
                goto fail;
            }

            if (!(result = LLVMBuildAnd(comp_ctx->builder, vector1, vector2,
                                        "and"))) {
                HANDLE_FAILURE("LLVMBuildAnd");
                goto fail;
            }

            break;
        }
        default:
            bh_assert(0);
            goto fail;
    }

    PUSH_V128(result);
    return true;
fail:
    return false;
}

static bool
v128_bitwise_not(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
    LLVMValueRef vector, result;

    POP_V128(vector);

    if (!(result = LLVMBuildNot(comp_ctx->builder, vector, "not"))) {
        HANDLE_FAILURE("LLVMBuildNot");
        goto fail;
    }

    PUSH_V128(result);
    return true;
fail:
    return false;
}

/* v128.or(v128.and(v1, c), v128.and(v2, v128.not(c))) */
static bool
v128_bitwise_bitselect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
{
    LLVMValueRef vector1, vector2, vector3, result;

    POP_V128(vector3);
    POP_V128(vector2);
    POP_V128(vector1);

    if (!(vector1 =
              LLVMBuildAnd(comp_ctx->builder, vector1, vector3, "a_and_c"))) {
        HANDLE_FAILURE("LLVMBuildAdd");
        goto fail;
    }

    if (!(vector3 = LLVMBuildNot(comp_ctx->builder, vector3, "not_c"))) {
        HANDLE_FAILURE("LLVMBuildNot");
        goto fail;
    }

    if (!(vector2 =
              LLVMBuildAnd(comp_ctx->builder, vector2, vector3, "b_and_c"))) {
        HANDLE_FAILURE("LLVMBuildAdd");
        goto fail;
    }

    if (!(result =
              LLVMBuildOr(comp_ctx->builder, vector1, vector2, "a_or_b"))) {
        HANDLE_FAILURE("LLVMBuildOr");
        goto fail;
    }

    PUSH_V128(result);

    return true;
fail:
    return false;
}

bool
aot_compile_simd_v128_bitwise(AOTCompContext *comp_ctx,
                              AOTFuncContext *func_ctx, V128Bitwise bitwise_op)
{
    switch (bitwise_op) {
        case V128_AND:
        case V128_OR:
        case V128_XOR:
        case V128_ANDNOT:
            return v128_bitwise_two_component(comp_ctx, func_ctx, bitwise_op);
        case V128_NOT:
            return v128_bitwise_not(comp_ctx, func_ctx);
        case V128_BITSELECT:
            return v128_bitwise_bitselect(comp_ctx, func_ctx);
        default:
            bh_assert(0);
            return false;
    }
}