# HG changeset patch # User LYH # Date 1395428379 14400 # Node ID 336e64e9e6e39373862883eac639fab7fdbba1de # Parent f080e93de0bab3155182b7bb2a4a8e9f8af6d701 Fully int8 type support diff -r f080e93de0ba -r 336e64e9e6e3 libinterp/corefcn/jit-typeinfo.cc --- 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 @@ -67,6 +67,7 @@ #include "ov-complex.h" #include "ov-scalar.h" #include "ov-float.h" +#include "ov-int8.h" #include "ov-uint16.h" #include "pager.h" @@ -206,6 +207,20 @@ return new octave_float_scalar (value); } +extern "C" int8_t +octave_jit_cast_int8_any (octave_base_value *obv) +{ + int8_t ret = obv->int8_scalar_value (); + obv->release (); + return ret; +} + +extern "C" octave_base_value * +octave_jit_cast_any_int8 (int8_t value) +{ + return new octave_int8_scalar (value); +} + extern "C" uint16_t octave_jit_cast_uint16_any (octave_base_value *obv) { @@ -511,6 +526,75 @@ /************************************************************ * + * int8 related external helper function + * + ************************************************************/ + +extern "C" int8_t +octave_jit_add_int8_int8 (int8_t lhs, int8_t rhs) +{ + uint8_t ulhs = lhs; + uint8_t urhs = rhs; + uint8_t res = ulhs + urhs; + + /* Calculate overflowed result. (Don't change the sign bit of ux) */ + ulhs = (ulhs >> 7) + SCHAR_MAX; + + /* Force compiler to use cmovns instruction */ + if ((int8_t) ((ulhs ^ urhs) | ~(urhs ^ res)) >= 0) + { + res = ulhs; + } + + return res; +} + +extern "C" int8_t +octave_jit_sub_int8_int8 (int8_t lhs, int8_t rhs) +{ + uint8_t ulhs = lhs; + uint8_t urhs = rhs; + uint8_t res = ulhs - urhs; + + ulhs = (ulhs >> 7) + SCHAR_MAX; + + /* Force compiler to use cmovns instruction */ + if ((uint8_t)((ulhs ^ urhs) & (ulhs ^ res)) < 0) + { + res = ulhs; + } + + return res; +} + +extern "C" int8_t +octave_jit_mul_int8_int8 (int8_t lhs, int8_t rhs) +{ + int16_t res = (int16_t) lhs * (int16_t) rhs; + uint8_t res2 = ((uint8_t) (lhs ^ rhs) >> 7) + SCHAR_MAX; + + int8_t hi = (res >> 8); + int8_t lo = res; + + if (hi != (lo >> 7)) res = res2; + + return res; +} + +extern "C" int8_t +octave_jit_incr_int8 (int8_t val) +{ + return octave_jit_add_int8_int8 (val, 1); +} + +extern "C" int8_t +octave_jit_decr_int8 (int8_t val) +{ + return octave_jit_sub_int8_int8 (val, 1); +} + +/************************************************************ + * * uint16 related external helper function * ************************************************************/ @@ -1181,6 +1265,7 @@ llvm::Type *scalar_t = llvm::Type::getDoubleTy (context); llvm::Type *single_t = llvm::Type::getFloatTy (context); + llvm::Type *int8__t = llvm::Type::getIntNTy (context, 8); llvm::Type *uint16__t = llvm::Type::getIntNTy (context, 16); llvm::Type *bool_t = llvm::Type::getInt1Ty (context); llvm::Type *string_t = llvm::Type::getInt8Ty (context); @@ -1330,6 +1415,7 @@ grab_fn.add_overload (create_identity (scalar)); grab_fn.add_overload (create_identity (single)); + grab_fn.add_overload (create_identity (intN (8))); grab_fn.add_overload (create_identity (uintN (16))); grab_fn.add_overload (create_identity (scalar_ptr)); grab_fn.add_overload (create_identity (any_ptr)); @@ -1351,6 +1437,7 @@ destroy_fn.stash_name ("destroy"); destroy_fn.add_overload (create_identity(scalar)); destroy_fn.add_overload (create_identity(single)); + destroy_fn.add_overload (create_identity(intN (8))); destroy_fn.add_overload (create_identity(uintN (16))); destroy_fn.add_overload (create_identity(boolean)); destroy_fn.add_overload (create_identity(index)); @@ -1610,6 +1697,61 @@ /************************************************************ * + * int8 related operations + * + ************************************************************/ + + // now for binary int8 operations + fn = create_external (JIT_FN (octave_jit_add_int8_int8), intN (8), intN (8), + intN (8)); + binary_ops[octave_value::op_add].add_overload (fn); + fn = create_external (JIT_FN (octave_jit_sub_int8_int8), intN (8), intN (8), + intN (8)); + binary_ops[octave_value::op_sub].add_overload (fn); + fn = create_external (JIT_FN (octave_jit_mul_int8_int8), intN (8), intN (8), + intN (8)); + binary_ops[octave_value::op_mul].add_overload (fn); + binary_ops[octave_value::op_el_mul].add_overload (fn); + + add_binary_icmp (intN (8), octave_value::op_lt, llvm::CmpInst::ICMP_SLT); + add_binary_icmp (intN (8), octave_value::op_le, llvm::CmpInst::ICMP_SLE); + add_binary_icmp (intN (8), octave_value::op_eq, llvm::CmpInst::ICMP_EQ); + add_binary_icmp (intN (8), octave_value::op_ge, llvm::CmpInst::ICMP_SGE); + add_binary_icmp (intN (8), octave_value::op_gt, llvm::CmpInst::ICMP_SGT); + add_binary_icmp (intN (8), 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 int8 operations + // FIXME: Impelment not + fn = create_external (JIT_FN (octave_jit_incr_int8), intN (8), intN (8)); + unary_ops[octave_value::op_incr].add_overload (fn); + + fn = create_external (JIT_FN (octave_jit_decr_int8), intN (8), intN (8)); + unary_ops[octave_value::op_decr].add_overload (fn); + + fn = create_internal ("octave_jit_uminus", intN (8), intN (8)); + body = fn.new_block (); + builder.SetInsertPoint (body); + { + llvm::Value *mone = llvm::ConstantInt::get (int8__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 (8)); + 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); + + /************************************************************ + * * uint16 related operations * ************************************************************/ @@ -2072,6 +2214,7 @@ casts[any->type_id ()].stash_name ("(any)"); casts[scalar->type_id ()].stash_name ("(scalar)"); casts[single->type_id ()].stash_name ("(single)"); + casts[intN (8)->type_id ()].stash_name ("(int8)"); casts[uintN (16)->type_id ()].stash_name ("(uint16)"); casts[complex->type_id ()].stash_name ("(complex)"); casts[matrix->type_id ()].stash_name ("(matrix)"); @@ -2109,6 +2252,14 @@ fn = create_external (JIT_FN (octave_jit_cast_single_any), single, any); casts[single->type_id ()].add_overload (fn); + // cast any <- int8 + fn = create_external (JIT_FN (octave_jit_cast_any_int8), any, intN (8)); + casts[any->type_id ()].add_overload (fn); + + // cast int8 <- any + fn = create_external (JIT_FN (octave_jit_cast_int8_any), intN (8), any); + casts[intN (8)->type_id ()].add_overload (fn); + // cast any <- uint16 fn = create_external (JIT_FN (octave_jit_cast_any_uint16), any, uintN (16)); casts[any->type_id ()].add_overload (fn); @@ -2154,6 +2305,10 @@ fn = create_identity (single); casts[single->type_id ()].add_overload (fn); + // cast int8 <- int8 + fn = create_identity (intN (8)); + casts[intN (8)->type_id ()].add_overload (fn); + // cast uint16 <- uint16 fn = create_identity (uintN (16)); casts[uintN (16)->type_id ()].add_overload (fn); @@ -2616,6 +2771,11 @@ return get_single (); } + if (ov.is_int8_type()) + { + return intN (8); + } + if (ov.is_uint16_type()) { return uintN (16);