From f66d84fd958e511a3993604f5f476b1fa290350c Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Fri, 23 Sep 2016 15:47:39 -0700 Subject: [PATCH] Integer subtraction with borrow flags. This is the x86-style of borrow flags. ARM uses subtract-with-carry which inverts the sense of the carry flag. --- filetests/parser/ternary.cton | 11 +++++ meta/cretonne/base.py | 79 +++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/filetests/parser/ternary.cton b/filetests/parser/ternary.cton index 5305126c49..99cb15b566 100644 --- a/filetests/parser/ternary.cton +++ b/filetests/parser/ternary.cton @@ -11,3 +11,14 @@ ebb1(v1: i32, v2: i32, v3: i32, v4: i32, v5: i32, v6: i32): ; check: $v30 = iadd_cin $v3, $v6, $v21 return v10, v20, v30 } + +function sub_i96(i32, i32, i32, i32, i32, i32) -> i32, i32, i32 { +ebb1(v1: i32, v2: i32, v3: i32, v4: i32, v5: i32, v6: i32): + v10, v11 = isub_bout v1, v4 + ;check: $v10, $v11 = isub_bout $v1, $v4 + v20, v21 = isub_borrow v2, v5, v11 + ; check: $v20, $v21 = isub_borrow $v2, $v5, $v11 + v30 = isub_bin v3, v6, v21 + ; check: $v30 = isub_bin $v3, $v6, $v21 + return v10, v20, v30 +} diff --git a/meta/cretonne/base.py b/meta/cretonne/base.py index f94668da4a..150155d908 100644 --- a/meta/cretonne/base.py +++ b/meta/cretonne/base.py @@ -406,24 +406,30 @@ isub_imm = Instruction( ins=(X, y), outs=a) # -# Integer arithmetic with carry. +# Integer arithmetic with carry and/or borrow. # a = Operand('a', iB) x = Operand('x', iB) y = Operand('y', iB) -cin = Operand('cin', b1, doc="Input carry flag") -cout = Operand('cout', b1, doc="Output carry flag") +c_in = Operand('c_in', b1, doc="Input carry flag") +c_out = Operand('c_out', b1, doc="Output carry flag") +b_in = Operand('b_in', b1, doc="Input borrow flag") +b_out = Operand('b_out', b1, doc="Output borrow flag") iadd_cin = Instruction( 'iadd_cin', """ Add integers with carry in. - Same as :inst:`iadd` with an additional carry input. + Same as :inst:`iadd` with an additional carry input. Computes: + + .. math:: + + a = x + y + c_{in} \pmod 2^B Polymorphic over all scalar integer types, but does not support vector types. """, - ins=(x, y, cin), outs=a) + ins=(x, y, c_in), outs=a) iadd_cout = Instruction( 'iadd_cout', """ @@ -431,21 +437,78 @@ iadd_cout = Instruction( Same as :inst:`iadd` with an additional carry output. + .. math:: + + a &= x + y \pmod 2^B \\ + c_{out} &= x+y >= 2^B + Polymorphic over all scalar integer types, but does not support vector types. """, - ins=(x, y), outs=(a, cout)) + ins=(x, y), outs=(a, c_out)) iadd_carry = Instruction( 'iadd_carry', """ Add integers with carry in and out. - Same as :inst:`iadd` with an additional carry output. + Same as :inst:`iadd` with an additional carry input and output. + + .. math:: + + a &= x + y + c_{in} \pmod 2^B \\ + c_{out} &= x + y + c_{in} >= 2^B Polymorphic over all scalar integer types, but does not support vector types. """, - ins=(x, y, cin), outs=(a, cout)) + ins=(x, y, c_in), outs=(a, c_out)) + +isub_bin = Instruction( + 'isub_bin', """ + Subtract integers with borrow in. + + Same as :inst:`isub` with an additional borrow flag input. Computes: + + .. math:: + + a = x - (y + b_{in}) \pmod 2^B + + Polymorphic over all scalar integer types, but does not support vector + types. + """, + ins=(x, y, b_in), outs=a) + +isub_bout = Instruction( + 'isub_bout', """ + Subtract integers with borrow out. + + Same as :inst:`isub` with an additional borrow flag output. + + .. math:: + + a &= x - y \pmod 2^B \\ + b_{out} &= x < y + + Polymorphic over all scalar integer types, but does not support vector + types. + """, + ins=(x, y), outs=(a, b_out)) + +isub_borrow = Instruction( + 'isub_borrow', """ + Subtract integers with borrow in and out. + + Same as :inst:`isub` with an additional borrow flag input and output. + + .. math:: + + a &= x - (y + b_{in}) \pmod 2^B \\ + b_{out} &= x < y + b_{in} + + Polymorphic over all scalar integer types, but does not support vector + types. + """, + ins=(x, y, b_in), outs=(a, b_out)) # # Bitwise operations.