| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "simd_floating_point.h"
- #include "simd_common.h"
- #include "../aot_emit_exception.h"
- #include "../aot_emit_numberic.h"
- #include "../../aot/aot_runtime.h"
- static LLVMValueRef
- simd_v128_float_cmp(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- FloatArithmetic arith_op,
- LLVMValueRef lhs,
- LLVMValueRef rhs)
- {
- LLVMValueRef result;
- LLVMRealPredicate op;
- op = FLOAT_MIN == arith_op ? LLVMRealULT : LLVMRealUGT;
- if (!(result = LLVMBuildFCmp(comp_ctx->builder, op, lhs, rhs, "cmp"))) {
- HANDLE_FAILURE("LLVMBuildFCmp");
- goto fail;
- }
- if (!(result =
- LLVMBuildSelect(comp_ctx->builder, result, lhs, rhs, "select"))) {
- HANDLE_FAILURE("LLVMBuildSelect");
- goto fail;
- }
- return result;
- fail:
- return NULL;
- }
- static bool
- simd_v128_float_arith(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- FloatArithmetic arith_op,
- LLVMTypeRef vector_type)
- {
- LLVMValueRef lhs, rhs, result;
- if (!(rhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
- "rhs"))) {
- goto fail;
- }
- if (!(lhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
- "lhs"))) {
- goto fail;
- }
- switch (arith_op) {
- case FLOAT_ADD:
- if (!(result =
- LLVMBuildFAdd(comp_ctx->builder, lhs, rhs, "sum"))) {
- HANDLE_FAILURE("LLVMBuildFAdd");
- goto fail;
- }
- break;
- case FLOAT_SUB:
- if (!(result = LLVMBuildFSub(comp_ctx->builder, lhs, rhs,
- "difference"))) {
- HANDLE_FAILURE("LLVMBuildFSub");
- goto fail;
- }
- break;
- case FLOAT_MUL:
- if (!(result =
- LLVMBuildFMul(comp_ctx->builder, lhs, rhs, "product"))) {
- HANDLE_FAILURE("LLVMBuildFMul");
- goto fail;
- }
- break;
- case FLOAT_DIV:
- if (!(result =
- LLVMBuildFDiv(comp_ctx->builder, lhs, rhs, "quotient"))) {
- HANDLE_FAILURE("LLVMBuildFDiv");
- goto fail;
- }
- break;
- case FLOAT_MIN:
- if (!(result = simd_v128_float_cmp(comp_ctx, func_ctx, FLOAT_MIN,
- lhs, rhs))) {
- goto fail;
- }
- break;
- case FLOAT_MAX:
- if (!(result = simd_v128_float_cmp(comp_ctx, func_ctx, FLOAT_MAX,
- lhs, rhs))) {
- goto fail;
- }
- break;
- default:
- result = NULL;
- bh_assert(0);
- break;
- }
- if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
- "ret"))) {
- HANDLE_FAILURE("LLVMBuildBitCast");
- goto fail;
- }
- /* push result into the stack */
- PUSH_V128(result);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_simd_f32x4_arith(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- FloatArithmetic arith_op)
- {
- return simd_v128_float_arith(comp_ctx, func_ctx, arith_op,
- V128_f32x4_TYPE);
- }
- bool
- aot_compile_simd_f64x2_arith(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- FloatArithmetic arith_op)
- {
- return simd_v128_float_arith(comp_ctx, func_ctx, arith_op,
- V128_f64x2_TYPE);
- }
- static bool
- simd_v128_float_neg(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- LLVMTypeRef vector_type)
- {
- LLVMValueRef number, result;
- if (!(number = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
- "number"))) {
- goto fail;
- }
- if (!(result = LLVMBuildFNeg(comp_ctx->builder, number, "neg"))) {
- HANDLE_FAILURE("LLVMBuildFNeg");
- goto fail;
- }
- if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
- "ret"))) {
- HANDLE_FAILURE("LLVMBuildBitCast");
- goto fail;
- }
- /* push result into the stack */
- PUSH_V128(result);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_simd_f32x4_neg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- return simd_v128_float_neg(comp_ctx, func_ctx, V128_f32x4_TYPE);
- }
- bool
- aot_compile_simd_f64x2_neg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- return simd_v128_float_neg(comp_ctx, func_ctx, V128_f64x2_TYPE);
- }
- static bool
- simd_v128_float_abs(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- LLVMTypeRef vector_type,
- const char *intrinsic)
- {
- LLVMValueRef vector, result;
- LLVMTypeRef param_types[1] = { vector_type };
- if (!(vector = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
- "vec"))) {
- goto fail;
- }
- if (!(result = aot_call_llvm_intrinsic(comp_ctx, intrinsic, vector_type,
- param_types, 1, vector))) {
- HANDLE_FAILURE("LLVMBuildCall");
- goto fail;
- }
- if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
- "ret"))) {
- HANDLE_FAILURE("LLVMBuildBitCast");
- goto fail;
- }
- /* push result into the stack */
- PUSH_V128(result);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_simd_f32x4_abs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- return simd_v128_float_abs(comp_ctx, func_ctx, V128_f32x4_TYPE,
- "llvm.fabs.v4f32");
- }
- bool
- aot_compile_simd_f64x2_abs(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- return simd_v128_float_abs(comp_ctx, func_ctx, V128_f64x2_TYPE,
- "llvm.fabs.v2f64");
- }
- static bool
- simd_v128_float_sqrt(AOTCompContext *comp_ctx,
- AOTFuncContext *func_ctx,
- LLVMTypeRef vector_type,
- const char *intrinsic)
- {
- LLVMValueRef number, result;
- LLVMTypeRef param_types[1] = { vector_type };
- if (!(number = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, vector_type,
- "number"))) {
- goto fail;
- }
- if (!(result = aot_call_llvm_intrinsic(comp_ctx, intrinsic, vector_type,
- param_types, 1, number))) {
- HANDLE_FAILURE("LLVMBuildCall");
- goto fail;
- }
- if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
- "ret"))) {
- HANDLE_FAILURE("LLVMBuildBitCast");
- goto fail;
- }
- /* push result into the stack */
- PUSH_V128(result);
- return true;
- fail:
- return false;
- }
- bool
- aot_compile_simd_f32x4_sqrt(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- return simd_v128_float_sqrt(comp_ctx, func_ctx, V128_f32x4_TYPE,
- "llvm.sqrt.v4f32");
- }
- bool
- aot_compile_simd_f64x2_sqrt(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
- {
- return simd_v128_float_sqrt(comp_ctx, func_ctx, V128_f64x2_TYPE,
- "llvm.sqrt.v2f64");
- }
|