Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushikcfd committed Apr 26, 2020
1 parent ec9d474 commit 83cb903
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 21 additions & 18 deletions loopy/target/c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def c99_preamble_generator(preamble_info):
yield("10_stdint", "#include <stdint.h>")


def _preamble_generator(preamble_info):
def _preamble_generator(preamble_info, func_qualifier='inline'):
integer_type_names = ["int8", "int16", "int32", "int64"]

def_integer_types_macro = ("03_def_integer_types", r"""
Expand All @@ -102,55 +102,55 @@ def _preamble_generator(preamble_info):
function_defs = {
"loopy_floor_div": r"""
#define LOOPY_DEFINE_FLOOR_DIV(SUFFIX, TYPE) \
inline TYPE loopy_floor_div_##SUFFIX(TYPE a, TYPE b) \
{ \
{} TYPE loopy_floor_div_##SUFFIX(TYPE a, TYPE b) \
{{ \
if ((a<0) != (b<0)) \
a = a - (b + (b<0) - (b>=0)); \
return a/b; \
}
}}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_FLOOR_DIV)
#undef LOOPY_DEFINE_FLOOR_DIV
""",
""".format(func_qualifier),

"loopy_floor_div_pos_b": r"""
#define LOOPY_DEFINE_FLOOR_DIV_POS_B(SUFFIX, TYPE) \
inline TYPE loopy_floor_div_pos_b_##SUFFIX(TYPE a, TYPE b) \
{ \
{} TYPE loopy_floor_div_pos_b_##SUFFIX(TYPE a, TYPE b) \
{{ \
if (a<0) \
a = a - (b-1); \
return a/b; \
}
}}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_FLOOR_DIV_POS_B)
#undef LOOPY_DEFINE_FLOOR_DIV_POS_B
""",
""".format(func_qualifier),

"loopy_mod": r"""
#define LOOPY_DEFINE_MOD(SUFFIX, TYPE) \
inline TYPE loopy_mod_##SUFFIX(TYPE a, TYPE b) \
{ \
{} TYPE loopy_mod_##SUFFIX(TYPE a, TYPE b) \
{{ \
TYPE result = a%b; \
if (result < 0 && b > 0) \
result += b; \
if (result > 0 && b < 0) \
result = result + b; \
return result; \
}
}}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_MOD)
#undef LOOPY_DEFINE_MOD
""",
""".format(func_qualifier),

"loopy_mod_pos_b": r"""
#define LOOPY_DEFINE_MOD_POS_B(SUFFIX, TYPE) \
inline TYPE loopy_mod_pos_b_##SUFFIX(TYPE a, TYPE b) \
{ \
{} TYPE loopy_mod_pos_b_##SUFFIX(TYPE a, TYPE b) \
{{ \
TYPE result = a%b; \
if (result < 0) \
result += b; \
return result; \
}
}}
LOOPY_CALL_WITH_INTEGER_TYPES(LOOPY_DEFINE_MOD_POS_B)
#undef LOOPY_DEFINE_MOD_POS_B
""",
""".format(func_qualifier),
}

c_funcs = set(func.c_name for func in preamble_info.seen_functions)
Expand Down Expand Up @@ -519,6 +519,8 @@ def scope_c_math_functions(target, identifier):


class CFamilyASTBuilder(ASTBuilderBase):
preamble_function_qualifier = 'inline'

# {{{ library

def symbol_manglers(self):
Expand All @@ -530,7 +532,8 @@ def symbol_manglers(self):
def preamble_generators(self):
return (
super(CFamilyASTBuilder, self).preamble_generators() + [
_preamble_generator,
lambda preamble_info: _preamble_generator(preamble_info,
self.preamble_function_qualifier),
])

def function_id_in_knl_callable_mapper(self):
Expand Down
2 changes: 2 additions & 0 deletions loopy/target/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ def cuda_preamble_generator(preamble_info):
# {{{ ast builder

class CUDACASTBuilder(CFamilyASTBuilder):
preamble_function_qualifier = 'inline __device__'

# {{{ library

def function_id_in_knl_callable_mapper(self):
Expand Down

0 comments on commit 83cb903

Please sign in to comment.