simd_int_arith.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "simd_int_arith.h"
  6. #include "simd_common.h"
  7. #include "../aot_emit_exception.h"
  8. #include "../../aot/aot_runtime.h"
  9. static bool
  10. simd_v128_integer_arith(AOTCompContext *comp_ctx,
  11. AOTFuncContext *func_ctx,
  12. V128Arithmetic arith_op,
  13. LLVMValueRef lhs,
  14. LLVMValueRef rhs)
  15. {
  16. LLVMValueRef result;
  17. switch (arith_op) {
  18. case V128_ADD:
  19. if (!(result = LLVMBuildAdd(comp_ctx->builder, lhs, rhs, "sum"))) {
  20. HANDLE_FAILURE("LLVMBuildAdd");
  21. goto fail;
  22. }
  23. break;
  24. case V128_SUB:
  25. if (!(result =
  26. LLVMBuildSub(comp_ctx->builder, lhs, rhs, "difference"))) {
  27. HANDLE_FAILURE("LLVMBuildSub");
  28. goto fail;
  29. }
  30. break;
  31. case V128_MUL:
  32. if (!(result =
  33. LLVMBuildMul(comp_ctx->builder, lhs, rhs, "product"))) {
  34. HANDLE_FAILURE("LLVMBuildMul");
  35. goto fail;
  36. }
  37. break;
  38. case V128_NEG:
  39. if (!(result = LLVMBuildNeg(comp_ctx->builder, lhs, "neg"))) {
  40. HANDLE_FAILURE("LLVMBuildNeg");
  41. goto fail;
  42. }
  43. break;
  44. default:
  45. result = NULL;
  46. bh_assert(0);
  47. break;
  48. }
  49. if (!(result = LLVMBuildBitCast(comp_ctx->builder, result, V128_i64x2_TYPE,
  50. "ret"))) {
  51. HANDLE_FAILURE("LLVMBuildBitCast");
  52. goto fail;
  53. }
  54. /* push result into the stack */
  55. PUSH_V128(result);
  56. return true;
  57. fail:
  58. return false;
  59. }
  60. bool
  61. aot_compile_simd_i8x16_arith(AOTCompContext *comp_ctx,
  62. AOTFuncContext *func_ctx,
  63. V128Arithmetic arith_op)
  64. {
  65. LLVMValueRef lhs, rhs;
  66. if (!(rhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, V128_i8x16_TYPE,
  67. "rhs"))) {
  68. goto fail;
  69. }
  70. if (!(lhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, V128_i8x16_TYPE,
  71. "lhs"))) {
  72. goto fail;
  73. }
  74. return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs);
  75. fail:
  76. return NULL;
  77. }
  78. bool
  79. aot_compile_simd_i16x8_arith(AOTCompContext *comp_ctx,
  80. AOTFuncContext *func_ctx,
  81. V128Arithmetic arith_op)
  82. {
  83. LLVMValueRef lhs, rhs;
  84. if (!(rhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, V128_i16x8_TYPE,
  85. "rhs"))) {
  86. goto fail;
  87. }
  88. if (!(lhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, V128_i16x8_TYPE,
  89. "lhs"))) {
  90. goto fail;
  91. }
  92. return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs);
  93. fail:
  94. return NULL;
  95. }
  96. bool
  97. aot_compile_simd_i32x4_arith(AOTCompContext *comp_ctx,
  98. AOTFuncContext *func_ctx,
  99. V128Arithmetic arith_op)
  100. {
  101. LLVMValueRef lhs, rhs;
  102. if (!(rhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, V128_i32x4_TYPE,
  103. "rhs"))) {
  104. goto fail;
  105. }
  106. if (!(lhs = simd_pop_v128_and_bitcast(comp_ctx, func_ctx, V128_i32x4_TYPE,
  107. "lhs"))) {
  108. goto fail;
  109. }
  110. return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs);
  111. fail:
  112. return NULL;
  113. }
  114. bool
  115. aot_compile_simd_i64x2_arith(AOTCompContext *comp_ctx,
  116. AOTFuncContext *func_ctx,
  117. V128Arithmetic arith_op)
  118. {
  119. LLVMValueRef lhs, rhs;
  120. POP_V128(rhs);
  121. POP_V128(lhs);
  122. return simd_v128_integer_arith(comp_ctx, func_ctx, arith_op, lhs, rhs);
  123. fail:
  124. return false;
  125. }
  126. bool
  127. aot_compile_simd_i8x16_neg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  128. {
  129. LLVMValueRef number;
  130. if (!(number = simd_pop_v128_and_bitcast(comp_ctx, func_ctx,
  131. V128_i8x16_TYPE, "number"))) {
  132. goto fail;
  133. }
  134. return simd_v128_integer_arith(comp_ctx, func_ctx, V128_NEG, number, NULL);
  135. fail:
  136. return false;
  137. }
  138. bool
  139. aot_compile_simd_i16x8_neg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  140. {
  141. LLVMValueRef number;
  142. if (!(number = simd_pop_v128_and_bitcast(comp_ctx, func_ctx,
  143. V128_i16x8_TYPE, "number"))) {
  144. goto fail;
  145. }
  146. return simd_v128_integer_arith(comp_ctx, func_ctx, V128_NEG, number, NULL);
  147. fail:
  148. return false;
  149. }
  150. bool
  151. aot_compile_simd_i32x4_neg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  152. {
  153. LLVMValueRef number;
  154. if (!(number = simd_pop_v128_and_bitcast(comp_ctx, func_ctx,
  155. V128_i32x4_TYPE, "number"))) {
  156. goto fail;
  157. }
  158. return simd_v128_integer_arith(comp_ctx, func_ctx, V128_NEG, number, NULL);
  159. fail:
  160. return false;
  161. }
  162. bool
  163. aot_compile_simd_i64x2_neg(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx)
  164. {
  165. LLVMValueRef number;
  166. POP_V128(number);
  167. return simd_v128_integer_arith(comp_ctx, func_ctx, V128_NEG, number, NULL);
  168. fail:
  169. return false;
  170. }