summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/iwasm/compilation/simd/simd_comparisons.c
blob: 8a87ab25bc7be4c6e74cd59b5646f34c12f5fb15 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2019 Intel Corporation. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "simd_comparisons.h"
#include "simd_common.h"
#include "../aot_emit_exception.h"
#include "../../aot/aot_runtime.h"

static bool
float_cond_2_predicate(FloatCond cond, LLVMRealPredicate *out)
{
    switch (cond) {
        case FLOAT_EQ:
            *out = LLVMRealOEQ;
            break;
        case FLOAT_NE:
            *out = LLVMRealUNE;
            break;
        case FLOAT_LT:
            *out = LLVMRealOLT;
            break;
        case FLOAT_GT:
            *out = LLVMRealOGT;
            break;
        case FLOAT_LE:
            *out = LLVMRealOLE;
            break;
        case FLOAT_GE:
            *out = LLVMRealOGE;
            break;
        default:
            bh_assert(0);
            goto fail;
    }

    return true;
fail:
    return false;
}

static bool
int_cond_2_predicate(IntCond cond, LLVMIntPredicate *out)
{
    switch (cond) {
        case INT_EQZ:
        case INT_EQ:
            *out = LLVMIntEQ;
            break;
        case INT_NE:
            *out = LLVMIntNE;
            break;
        case INT_LT_S:
            *out = LLVMIntSLT;
            break;
        case INT_LT_U:
            *out = LLVMIntULT;
            break;
        case INT_GT_S:
            *out = LLVMIntSGT;
            break;
        case INT_GT_U:
            *out = LLVMIntUGT;
            break;
        case INT_LE_S:
            *out = LLVMIntSLE;
            break;
        case INT_LE_U:
            *out = LLVMIntULE;
            break;
        case INT_GE_S:
            *out = LLVMIntSGE;
            break;
        case INT_GE_U:
            *out = LLVMIntUGE;
            break;
        default:
            bh_assert(0);
            goto fail;
    }

    return true;
fail:
    return false;
}

static bool
interger_vector_compare(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
                        IntCond cond, LLVMTypeRef vector_type)
{
    LLVMValueRef vec1, vec2, result;
    LLVMIntPredicate int_pred;

    if (!(vec2 = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
                                           "vec2"))) {
        goto fail;
    }

    if (!(vec1 = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
                                           "vec1"))) {
        goto fail;
    }

    if (!int_cond_2_predicate(cond, &int_pred)) {
        HANDLE_FAILURE("int_cond_2_predicate");
        goto fail;
    }
    /* icmp <N x iX> %vec1, %vec2 */
    if (!(result =
              LLVMBuildICmp(comp_ctx->builder, int_pred, vec1, vec2, "cmp"))) {
        HANDLE_FAILURE("LLVMBuildICmp");
        goto fail;
    }

    /* sext <N x i1> %result to <N x iX> */
    if (!(result =
              LLVMBuildSExt(comp_ctx->builder, result, vector_type, "ext"))) {
        HANDLE_FAILURE("LLVMBuildSExt");
        goto fail;
    }

    /* bitcast <N x iX> %result to <2 x i64> */
    if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
                                    "result"))) {
        HANDLE_FAILURE("LLVMBuildBitCast");
        goto fail;
    }

    PUSH_V128(result);

    return true;
fail:
    return false;
}

bool
aot_compile_simd_i8x16_compare(AOTCompContext *comp_ctx,
                               AOTFuncContext *func_ctx, IntCond cond)
{
    return interger_vector_compare(comp_ctx, func_ctx, cond, V128_i8x16_TYPE);
}

bool
aot_compile_simd_i16x8_compare(AOTCompContext *comp_ctx,
                               AOTFuncContext *func_ctx, IntCond cond)
{
    return interger_vector_compare(comp_ctx, func_ctx, cond, V128_i16x8_TYPE);
}

bool
aot_compile_simd_i32x4_compare(AOTCompContext *comp_ctx,
                               AOTFuncContext *func_ctx, IntCond cond)
{
    return interger_vector_compare(comp_ctx, func_ctx, cond, V128_i32x4_TYPE);
}

bool
aot_compile_simd_i64x2_compare(AOTCompContext *comp_ctx,
                               AOTFuncContext *func_ctx, IntCond cond)
{
    return interger_vector_compare(comp_ctx, func_ctx, cond, V128_i64x2_TYPE);
}

static bool
float_vector_compare(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
                     FloatCond cond, LLVMTypeRef vector_type,
                     LLVMTypeRef result_type)
{
    LLVMValueRef vec1, vec2, result;
    LLVMRealPredicate real_pred;

    if (!(vec2 = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
                                           "vec2"))) {
        goto fail;
    }

    if (!(vec1 = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
                                           "vec1"))) {
        goto fail;
    }

    if (!float_cond_2_predicate(cond, &real_pred)) {
        HANDLE_FAILURE("float_cond_2_predicate");
        goto fail;
    }
    /* fcmp <N x iX> %vec1, %vec2 */
    if (!(result =
              LLVMBuildFCmp(comp_ctx->builder, real_pred, vec1, vec2, "cmp"))) {
        HANDLE_FAILURE("LLVMBuildFCmp");
        goto fail;
    }

    /* sext <N x i1> %result to <N x iX> */
    if (!(result =
              LLVMBuildSExt(comp_ctx->builder, result, result_type, "ext"))) {
        HANDLE_FAILURE("LLVMBuildSExt");
        goto fail;
    }

    /* bitcast <N x iX> %result to <2 x i64> */
    if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
                                    "result"))) {
        HANDLE_FAILURE("LLVMBuildBitCast");
        goto fail;
    }

    PUSH_V128(result);

    return true;
fail:
    return false;
}

bool
aot_compile_simd_f32x4_compare(AOTCompContext *comp_ctx,
                               AOTFuncContext *func_ctx, FloatCond cond)
{
    return float_vector_compare(comp_ctx, func_ctx, cond, V128_f32x4_TYPE,
                                V128_i32x4_TYPE);
}

bool
aot_compile_simd_f64x2_compare(AOTCompContext *comp_ctx,
                               AOTFuncContext *func_ctx, FloatCond cond)
{
    return float_vector_compare(comp_ctx, func_ctx, cond, V128_f64x2_TYPE,
                                V128_i64x2_TYPE);
}