changeset 18596:9f72101d109d draft

Fully int32 type support
author LYH <lyh.kernel@gmail.com>
date Fri, 21 Mar 2014 14:59:39 -0400
parents b2ac17e14a68
children 2b548935b334
files libinterp/corefcn/jit-typeinfo.cc
diffstat 1 files changed, 161 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/jit-typeinfo.cc	Fri Mar 21 14:59:39 2014 -0400
+++ b/libinterp/corefcn/jit-typeinfo.cc	Fri Mar 21 14:59:39 2014 -0400
@@ -71,6 +71,7 @@
 #include "ov-float.h"
 #include "ov-int8.h"
 #include "ov-int16.h"
+#include "ov-int32.h"
 #include "ov-int64.h"
 #include "ov-uint16.h"
 #include "pager.h"
@@ -242,6 +243,20 @@
   return new octave_int16_scalar (value);
 }
 
+extern "C" int32_t
+octave_jit_cast_int32_any (octave_base_value *obv)
+{
+  int32_t ret = obv->int32_scalar_value ();
+  obv->release ();
+  return ret;
+}
+
+extern "C" octave_base_value *
+octave_jit_cast_any_int32 (int32_t value)
+{
+  return new octave_int32_scalar (value);
+}
+
 extern "C" int64_t
 octave_jit_cast_int64_any (octave_base_value *obv)
 {
@@ -699,6 +714,75 @@
 
 /************************************************************
  *
+ * int32 related external helper function
+ *
+ ************************************************************/
+
+extern "C" int32_t
+octave_jit_add_int32_int32 (int32_t lhs, int32_t rhs)
+{
+  uint32_t ulhs = lhs;
+  uint32_t urhs = rhs;
+  uint32_t res = ulhs + urhs;
+
+  /* Calculate overflowed result. (Don't change the sign bit of ux) */
+  ulhs = (ulhs >> 31) + INT_MAX;
+
+  /* Force compiler to use cmovns instruction */
+  if ((int32_t) ((ulhs ^ urhs) | ~(urhs ^ res)) >= 0)
+    {
+      res = ulhs;
+    }
+
+  return res;
+}
+
+extern "C" int32_t
+octave_jit_sub_int32_int32 (int32_t lhs, int32_t rhs)
+{
+  uint32_t ulhs = lhs;
+  uint32_t urhs = rhs;
+  uint32_t res = ulhs - urhs;
+
+  ulhs = (ulhs >> 31) + INT_MAX;
+
+  /* Force compiler to use cmovns instruction */
+  if ((int32_t)((ulhs ^ urhs) & (ulhs ^ res)) < 0)
+    {
+      res = ulhs;
+    }
+
+  return res;
+}
+
+extern "C" int32_t
+octave_jit_mul_int32_int32 (int32_t lhs, int32_t rhs)
+{
+  int64_t res = (int64_t) lhs * (int64_t) rhs;
+  uint32_t res2 = ((uint32_t) (lhs ^ rhs) >> 31) + INT_MAX;
+
+  int32_t hi = (res >> 32);
+  int32_t lo = res;
+
+  if (hi != (lo >> 31)) res = res2;
+
+  return res;
+}
+
+extern "C" int32_t
+octave_jit_incr_int32 (int32_t val)
+{
+  return octave_jit_add_int32_int32 (val, 1);
+}
+
+extern "C" int32_t
+octave_jit_decr_int32 (int32_t val)
+{
+  return octave_jit_sub_int32_int32 (val, 1);
+}
+
+/************************************************************
+ *
  * int64 related external helper function
  *
  ************************************************************/
@@ -1440,6 +1524,7 @@
   llvm::Type *single_t = llvm::Type::getFloatTy (context);
   llvm::Type *int8__t = llvm::Type::getIntNTy (context, 8);
   llvm::Type *int16__t = llvm::Type::getIntNTy (context, 16);
+  llvm::Type *int32__t = llvm::Type::getIntNTy (context, 32);
   llvm::Type *int64__t = llvm::Type::getIntNTy (context, 64);
   llvm::Type *uint16__t = llvm::Type::getIntNTy (context, 16);
   llvm::Type *bool_t = llvm::Type::getInt1Ty (context);
@@ -1592,6 +1677,7 @@
   grab_fn.add_overload (create_identity (single));
   grab_fn.add_overload (create_identity (intN (8)));
   grab_fn.add_overload (create_identity (intN (16)));
+  grab_fn.add_overload (create_identity (intN (32)));
   grab_fn.add_overload (create_identity (intN (64)));
   grab_fn.add_overload (create_identity (uintN (16)));
   grab_fn.add_overload (create_identity (scalar_ptr));
@@ -1616,6 +1702,7 @@
   destroy_fn.add_overload (create_identity(single));
   destroy_fn.add_overload (create_identity(intN (8)));
   destroy_fn.add_overload (create_identity(intN (16)));
+  destroy_fn.add_overload (create_identity(intN (32)));
   destroy_fn.add_overload (create_identity(intN (64)));
   destroy_fn.add_overload (create_identity(uintN (16)));
   destroy_fn.add_overload (create_identity(boolean));
@@ -1986,6 +2073,62 @@
 
   /************************************************************
    *
+   * int32 related operations
+   *
+   ************************************************************/
+
+  // FIXME: interpreter overflow occurs at minus + minus, minus - plus
+  // now for binary int32 operations
+  fn = create_external (JIT_FN (octave_jit_add_int32_int32), intN (32), intN (32),
+                        intN (32));
+  binary_ops[octave_value::op_add].add_overload (fn);
+  fn = create_external (JIT_FN (octave_jit_sub_int32_int32), intN (32), intN (32),
+                        intN (32));
+  binary_ops[octave_value::op_sub].add_overload (fn);
+  fn = create_external (JIT_FN (octave_jit_mul_int32_int32), intN (32), intN (32),
+                        intN (32));
+  binary_ops[octave_value::op_mul].add_overload (fn);
+  binary_ops[octave_value::op_el_mul].add_overload (fn);
+
+  add_binary_icmp (intN (32), octave_value::op_lt, llvm::CmpInst::ICMP_SLT);
+  add_binary_icmp (intN (32), octave_value::op_le, llvm::CmpInst::ICMP_SLE);
+  add_binary_icmp (intN (32), octave_value::op_eq, llvm::CmpInst::ICMP_EQ);
+  add_binary_icmp (intN (32), octave_value::op_ge, llvm::CmpInst::ICMP_SGE);
+  add_binary_icmp (intN (32), octave_value::op_gt, llvm::CmpInst::ICMP_SGT);
+  add_binary_icmp (intN (32), octave_value::op_ne, llvm::CmpInst::ICMP_NE);
+
+  // FIXME: saturation divide definition? interpreter convert int to double, calculate and round.
+  // divide is annoying because it might error
+  // FIXME: Implement div
+
+  // FIXME: Implement pow
+
+  // now for unary int32 operations
+  // FIXME: Impelment not
+  fn = create_external (JIT_FN (octave_jit_incr_int32), intN (32), intN (32));
+  unary_ops[octave_value::op_incr].add_overload (fn);
+
+  fn = create_external (JIT_FN (octave_jit_decr_int32), intN (32), intN (32));
+  unary_ops[octave_value::op_decr].add_overload (fn);
+
+  fn = create_internal ("octave_jit_uminus", intN (32), intN (32));
+  body = fn.new_block ();
+  builder.SetInsertPoint (body);
+  {
+    llvm::Value *mone = llvm::ConstantInt::get (int32__t, -1);
+    llvm::Value *val = fn.argument (builder, 0);
+    val = builder.CreateMul (val, mone);
+    fn.do_return (builder, val);
+  }
+  unary_ops[octave_value::op_uminus].add_overload (fn);
+
+  fn = create_identity (intN (32));
+  unary_ops[octave_value::op_uplus].add_overload (fn);
+  unary_ops[octave_value::op_transpose].add_overload (fn);
+  unary_ops[octave_value::op_hermitian].add_overload (fn);
+
+  /************************************************************
+   *
    * int64 related operations
    *
    ************************************************************/
@@ -2506,6 +2649,7 @@
   casts[single->type_id ()].stash_name ("(single)");
   casts[intN (8)->type_id ()].stash_name ("(int8)");
   casts[intN (16)->type_id ()].stash_name ("(int16)");
+  casts[intN (32)->type_id ()].stash_name ("(int32)");
   casts[intN (64)->type_id ()].stash_name ("(int64)");
   casts[uintN (16)->type_id ()].stash_name ("(uint16)");
   casts[complex->type_id ()].stash_name ("(complex)");
@@ -2560,6 +2704,14 @@
   fn = create_external (JIT_FN (octave_jit_cast_int16_any), intN (16), any);
   casts[intN (16)->type_id ()].add_overload (fn);
 
+  // cast any <- int32
+  fn = create_external (JIT_FN (octave_jit_cast_any_int32), any, intN (32));
+  casts[any->type_id ()].add_overload (fn);
+
+  // cast int32 <- any
+  fn = create_external (JIT_FN (octave_jit_cast_int32_any), intN (32), any);
+  casts[intN (32)->type_id ()].add_overload (fn);
+
   // cast any <- int64
   fn = create_external (JIT_FN (octave_jit_cast_any_int64), any, intN (64));
   casts[any->type_id ()].add_overload (fn);
@@ -2621,6 +2773,10 @@
   fn = create_identity (intN (16));
   casts[intN (16)->type_id ()].add_overload (fn);
 
+  // cast int32 <- int32
+  fn = create_identity (intN (32));
+  casts[intN (32)->type_id ()].add_overload (fn);
+
   // cast int64 <- int64
   fn = create_identity (intN (64));
   casts[intN (64)->type_id ()].add_overload (fn);
@@ -3097,6 +3253,11 @@
       return intN (16);
     }
 
+  if (ov.is_int32_type())
+    {
+      return intN (32);
+    }
+
   if (ov.is_int64_type())
     {
       return intN (64);