Skip to content
Snippets Groups Projects
Select Git revision
  • 7425762d808db36958805707aea4b21bf614a231
  • master default protected
  • v2.0-dev protected
  • zikeliml/Task-96-dotExporterForAST
  • zikeliml/124-rework-tutorials
  • fma
  • fhennig/v2.0-deprecations
  • holzer-master-patch-46757
  • 66-absolute-access-is-probably-not-copied-correctly-after-_eval_subs
  • gpu_bufferfield_fix
  • hyteg
  • vectorization_sqrt_fix
  • target_dh_refactoring
  • const_fix
  • improved_comm
  • gpu_liveness_opts
  • release/1.3.7 protected
  • release/1.3.6 protected
  • release/2.0.dev0 protected
  • release/1.3.5 protected
  • release/1.3.4 protected
  • release/1.3.3 protected
  • release/1.3.2 protected
  • release/1.3.1 protected
  • release/1.3 protected
  • release/1.2 protected
  • release/1.1.1 protected
  • release/1.1 protected
  • release/1.0.1 protected
  • release/1.0 protected
  • release/0.4.4 protected
  • last/Kerncraft
  • last/OpenCL
  • last/LLVM
  • release/0.4.3 protected
  • release/0.4.2 protected
36 results

integer_functions.py

Blame
  • integer_functions.py 2.50 KiB
    import sympy as sp
    
    from pystencils.data_types import get_type_of_expression, collate_types
    from pystencils.sympyextensions import is_integer_sequence
    
    bitwise_xor = sp.Function("bitwise_xor")
    bit_shift_right = sp.Function("bit_shift_right")
    bit_shift_left = sp.Function("bit_shift_left")
    bitwise_and = sp.Function("bitwise_and")
    bitwise_or = sp.Function("bitwise_or")
    
    
    # noinspection PyPep8Naming
    class modulo_floor(sp.Function):
        """Returns the next smaller integer divisible by given divisor.
    
        Examples:
            >>> modulo_floor(9, 4)
            8
            >>> modulo_floor(11, 4)
            8
            >>> modulo_floor(12, 4)
            12
            >>> from pystencils import TypedSymbol
            >>> a, b = TypedSymbol("a", "int64"), TypedSymbol("b", "int32")
            >>> modulo_floor(a, b).to_c(str)
            '(int64_t)((a) / (b)) * (b)'
        """
        nargs = 2
    
        def __new__(cls, integer, divisor):
            if is_integer_sequence((integer, divisor)):
                return (int(integer) // int(divisor)) * divisor
            else:
                return super().__new__(cls, integer, divisor)
    
        def to_c(self, print_func):
            dtype = collate_types((get_type_of_expression(self.args[0]), get_type_of_expression(self.args[1])))
            assert dtype.is_int()
            return "({dtype})(({0}) / ({1})) * ({1})".format(print_func(self.args[0]),
                                                             print_func(self.args[1]), dtype=dtype)
    
    
    # noinspection PyPep8Naming
    class modulo_ceil(sp.Function):
        """Returns the next bigger integer divisible by given divisor.
    
        Examples:
            >>> modulo_ceil(9, 4)
            12
            >>> modulo_ceil(11, 4)
            12
            >>> modulo_ceil(12, 4)
            12
            >>> from pystencils import TypedSymbol
            >>> a, b = TypedSymbol("a", "int64"), TypedSymbol("b", "int32")
            >>> modulo_ceil(a, b).to_c(str)
            '((a) % (b) == 0 ? a : ((int64_t)((a) / (b))+1) * (b))'
        """
        nargs = 2
    
        def __new__(cls, integer, divisor):
            if is_integer_sequence((integer, divisor)):
                return integer if integer % divisor == 0 else ((integer // divisor) + 1) * divisor
            else:
                return super().__new__(cls, integer, divisor)
    
        def to_c(self, print_func):
            dtype = collate_types((get_type_of_expression(self.args[0]), get_type_of_expression(self.args[1])))
            assert dtype.is_int()
            code = "(({0}) % ({1}) == 0 ? {0} : (({dtype})(({0}) / ({1}))+1) * ({1}))"
            return code.format(print_func(self.args[0]), print_func(self.args[1]), dtype=dtype)