def map_floor_div(self, expr, type_context): from loopy.symbolic import get_dependencies iname_deps = get_dependencies(expr) & self.kernel.all_inames() domain = self.kernel.get_inames_domain(iname_deps) assumption_non_param = isl.BasicSet.from_params( self.kernel.assumptions) assumptions, domain = isl.align_two(assumption_non_param, domain) domain = domain & assumptions from loopy.isl_helpers import is_nonnegative num_nonneg = is_nonnegative(expr.numerator, domain) den_nonneg = is_nonnegative(expr.denominator, domain) def seen_func(name): idt = self.kernel.index_dtype from loopy.codegen import SeenFunction self.codegen_state.seen_functions.add( SeenFunction(name, name, (idt, idt))) if den_nonneg: if num_nonneg: # parenthesize to avoid negative signs being dragged in from the # outside by associativity return (self.rec(expr.numerator, type_context) // self.rec(expr.denominator, type_context)) else: seen_func("int_floor_div_pos_b") return var("int_floor_div_pos_b")(self.rec( expr.numerator, 'i'), self.rec(expr.denominator, 'i')) else: seen_func("int_floor_div") return var("int_floor_div")(self.rec(expr.numerator, 'i'), self.rec(expr.denominator, 'i'))
def map_floor_div(self, expr, enclosing_prec, type_context): from loopy.symbolic import get_dependencies iname_deps = get_dependencies(expr) & self.kernel.all_inames() domain = self.kernel.get_inames_domain(iname_deps) assumption_non_param = isl.BasicSet.from_params(self.kernel.assumptions) assumptions, domain = isl.align_two(assumption_non_param, domain) domain = domain & assumptions from loopy.isl_helpers import is_nonnegative num_nonneg = is_nonnegative(expr.numerator, domain) den_nonneg = is_nonnegative(expr.denominator, domain) def seen_func(name): idt = self.kernel.index_dtype from loopy.codegen import SeenFunction self.codegen_state.seen_functions.add(SeenFunction(name, name, (idt, idt))) if den_nonneg: if num_nonneg: # parenthesize to avoid negative signs being dragged in from the # outside by associativity return "(%s / %s)" % ( self.rec(expr.numerator, PREC_PRODUCT, type_context), # analogous to ^{-1} self.rec(expr.denominator, PREC_POWER, type_context), ) else: seen_func("int_floor_div_pos_b") return "int_floor_div_pos_b(%s, %s)" % ( self.rec(expr.numerator, PREC_NONE, "i"), self.rec(expr.denominator, PREC_NONE, "i"), ) else: seen_func("int_floor_div") return "int_floor_div(%s, %s)" % ( self.rec(expr.numerator, PREC_NONE, "i"), self.rec(expr.denominator, PREC_NONE, "i"), )
def map_integer_div_operator(self, base_func_name, op_func, expr, type_context): from loopy.symbolic import get_dependencies iname_deps = get_dependencies(expr) & self.kernel.all_inames() domain = self.kernel.get_inames_domain(iname_deps) assumption_non_param = isl.BasicSet.from_params(self.kernel.assumptions) assumptions, domain = isl.align_two(assumption_non_param, domain) domain = domain & assumptions from loopy.isl_helpers import is_nonnegative num_nonneg = is_nonnegative(expr.numerator, domain) den_nonneg = is_nonnegative(expr.denominator, domain) result_dtype = self.infer_type(expr) suffix = result_dtype.numpy_dtype.type.__name__ def seen_func(name): from loopy.codegen import SeenFunction self.codegen_state.seen_functions.add( SeenFunction( name, "%s_%s" % (name, suffix), (result_dtype, result_dtype))) if den_nonneg: if num_nonneg: return op_func( self.rec(expr.numerator, type_context), self.rec(expr.denominator, type_context)) else: seen_func("%s_pos_b" % base_func_name) return var("%s_pos_b_%s" % (base_func_name, suffix))( self.rec(expr.numerator, 'i'), self.rec(expr.denominator, 'i')) else: seen_func(base_func_name) return var("%s_%s" % (base_func_name, suffix))( self.rec(expr.numerator, 'i'), self.rec(expr.denominator, 'i'))