Esempio n. 1
0
def generate_ocl_fn(src, dst, size='', mode='', sat=''):
  close_conditional = conditional_guard(src, dst)

  print("""_CLC_DEF _CLC_OVERLOAD
{DST}{N} convert_{DST}{N}{S}{M}({SRC}{N} x)
{{
  return {CORE_FN}(x);
}}
""".format(CORE_FN=clc_core_fn_name(dst, size=size, sat=sat, mode=mode),
           SRC=src, DST=dst, N=size, S=sat, M=mode))

  close_conditional_guard(close_conditional)
Esempio n. 2
0
def generate_spirv_fn_impl(src, dst, size='', mode='', sat='', force_decoration=False):
  close_conditional = conditional_guard(src, dst)

  print("""_CLC_DEF _CLC_OVERLOAD _CLC_CONSTFN
{DST}{N} {FN}({SRC}{N} x)
{{
  return {CORE_FN}(x);
}}
""".format(FN=spirv_fn_name(src, dst, size=size, sat=sat, mode=mode, force_sat_decoration=force_decoration),
           CORE_FN=clc_core_fn_name(dst, size=size, sat=sat, mode=mode),
           SRC=src, DST=dst, N=size))

  close_conditional_guard(close_conditional)
Esempio n. 3
0
def generate_saturated_conversion_with_rounding(src, dst, size, mode):
    # Header
    close_conditional = conditional_guard(src, dst)

    # Body
    print("""_CLC_DEF _CLC_OVERLOAD
{DST}{N} {FN}({SRC}{N} x)
{{
  return {FN_DEFAULT}(x);
}}
""".format(FN=clc_core_fn_name(dst, size=size, sat='_sat', mode=mode),
           FN_DEFAULT=clc_core_fn_name(dst, size=size, mode=mode),
           DST=dst,
           SRC=src,
           N=size))

    # Footer
    close_conditional_guard(close_conditional)
Esempio n. 4
0
def generate_default_conversion(src, dst, mode):
    close_conditional = conditional_guard(src, dst)

    # scalar conversions
    print("""_CLC_DEF _CLC_OVERLOAD
{DST} {FN}({SRC} x)
{{
  return ({DST})x;
}}
""".format(FN=clc_core_fn_name(dst, mode=mode), SRC=src, DST=dst, M=mode))

    # vector conversions, done through decomposition to components
    for size, half_size in half_sizes:
        conv_fn = clc_core_fn_name(dst, size=size, mode=mode)
        # default mode, so drop the mode for the called function
        half_size_conv_fn = clc_core_fn_name(dst, size=half_size)
        print("""_CLC_DEF _CLC_OVERLOAD
{DST}{N} {FN}({SRC}{N} x)
{{
  return ({DST}{N})({HALF_SIZE_FN}(x.lo), {HALF_SIZE_FN}(x.hi));
}}
""".format(FN=conv_fn,
           HALF_SIZE_FN=half_size_conv_fn,
           SRC=src,
           DST=dst,
           N=size))

    # 3-component vector conversions
    print("""_CLC_DEF _CLC_OVERLOAD
{DST}3 {FN}({SRC}3 x)
{{
  return ({DST}3)({VEC_2_FN}(x.s01), {SCALAR_FN}(x.s2));
}}""".format(FN=clc_core_fn_name(dst, size='3', mode=mode),
             VEC_2_FN=clc_core_fn_name(dst, size='2'),
             SCALAR_FN=clc_core_fn_name(dst),
             SRC=src,
             DST=dst))

    close_conditional_guard(close_conditional)
Esempio n. 5
0
def generate_float_conversion(src, dst, size, mode, sat):
    # Header
    close_conditional = conditional_guard(src, dst)
    print("""_CLC_DEF _CLC_OVERLOAD
  {DST}{N} {FN}({SRC}{N} x)
{{""".format(FN=clc_core_fn_name(dst, size=size, sat=sat, mode=mode),
             SRC=src,
             DST=dst,
             N=size))

    # Perform conversion
    if dst in int_types:
        if mode == '_rte':
            print("  x = __spirv_ocl_rint(x);")
        elif mode == '_rtp':
            print("  x = __spirv_ocl_ceil(x);")
        elif mode == '_rtn':
            print("  x = __spirv_ocl_floor(x);")
        print("  return {FN}(x);".format(
            FN=clc_core_fn_name(dst, size=size, sat=sat)))
    elif mode == '_rte':
        print("  return {FN}(x);".format(FN=clc_core_fn_name(dst, size=size)))
    else:
        print("  {DST}{N} r = {FN}(x);".format(FN=clc_core_fn_name(dst,
                                                                   size=size),
                                               DST=dst,
                                               N=size))
        print("  {SRC}{N} y = {FN}(y);".format(FN=clc_core_fn_name(src,
                                                                   size=size),
                                               SRC=src,
                                               N=size))
        if mode == '_rtz':
            if src in int_types:
                print("  {USRC}{N} abs_x = __clc_abs(x);".format(
                    USRC=unsigned_type[src], N=size))
                print("  {USRC}{N} abs_y = __clc_abs(y);".format(
                    USRC=unsigned_type[src], N=size))
            else:
                print("  {SRC}{N} abs_x = __spirv_ocl_fabs(x);".format(SRC=src,
                                                                       N=size))
                print("  {SRC}{N} abs_y = __spirv_ocl_fabs(y);".format(SRC=src,
                                                                       N=size))
            print(
                "  return {BOOL_CONVERT}(abs_y > abs_x) ? r:  __spirv_ocl_nextafter(r, __spirv_ocl_sign(r) * ({DST}{N})-INFINITY);"
                .format(DST=dst,
                        N=size,
                        BOOL_CONVERT=clc_core_fn_name(bool_type[dst],
                                                      size=size)))
        if mode == '_rtp':
            print(
                "  return {BOOL_CONVERT}(y < x) ? r : __spirv_ocl_nextafter(r, ({DST}{N})INFINITY);"
                .format(DST=dst,
                        N=size,
                        BOOL_CONVERT=clc_core_fn_name(bool_type[dst],
                                                      size=size)))
        if mode == '_rtn':
            print(
                "  return {BOOL_CONVERT}(y > x) ? r : __spirv_ocl_nextafter(r, ({DST}{N})-INFINITY);"
                .format(DST=dst,
                        N=size,
                        BOOL_CONVERT=clc_core_fn_name(bool_type[dst],
                                                      size=size)))

    # Footer
    print("}")
    close_conditional_guard(close_conditional)
Esempio n. 6
0
def generate_saturated_conversion(src, dst, size):
    # Header
    close_conditional = conditional_guard(src, dst)
    print("""_CLC_DEF _CLC_OVERLOAD
{DST}{N} {FN}({SRC}{N} x)
{{""".format(FN=clc_core_fn_name(dst, size=size, sat='_sat'),
             DST=dst,
             SRC=src,
             N=size))

    # FIXME: This is a work around for lack of select function with
    # signed third argument when the first two arguments are unsigned types.
    # We cast to the signed type for sign-extension, then do a bitcast to
    # the unsigned type.
    bool_conv_fn = clc_core_fn_name(bool_type[dst], size=size)
    if dst in unsigned_types:
        bool_prefix = "as_{DST}{N}({BOOL_CONV_FN}".format(
            DST=dst, BOOL_CONV_FN=bool_conv_fn, N=size)
        bool_suffix = ")"
    else:
        bool_prefix = bool_conv_fn
        bool_suffix = ""

    # Body
    if src == dst:

        # Conversion between same types
        print("  return x;")

    elif src in float_types:

        # Conversion from float to int
        print("""  {DST}{N} y = {CONV_DEFAULT}(x);
  y = {BP}(x < ({SRC}{N}){DST_MIN}){BS} ? y : ({DST}{N}){DST_MIN};
  y = {BP}(x > ({SRC}{N}){DST_MAX}){BS} ? y : ({DST}{N}){DST_MAX};
  return y;""".format(SRC=src,
                      DST=dst,
                      N=size,
                      CONV_DEFAULT=clc_core_fn_name(dst, size=size),
                      DST_MIN=limit_min[dst],
                      DST_MAX=limit_max[dst],
                      BP=bool_prefix,
                      BS=bool_suffix))

    else:

        # Integer to integer convesion with sizeof(src) == sizeof(dst)
        if sizeof_type[src] == sizeof_type[dst]:
            if src in unsigned_types:
                print("  x = __clc_min(x, ({SRC}){DST_MAX});".format(
                    SRC=src, DST_MAX=limit_max[dst]))
            else:
                print("  x = __clc_max(x, ({SRC})0);".format(SRC=src))

        # Integer to integer conversion where sizeof(src) > sizeof(dst)
        elif sizeof_type[src] > sizeof_type[dst]:
            if src in unsigned_types:
                print("  x = __clc_min(x, ({SRC}){DST_MAX});".format(
                    SRC=src, DST_MAX=limit_max[dst]))
            else:
                print(
                    "  x = __clc_clamp(x, ({SRC}){DST_MIN}, ({SRC}){DST_MAX});"
                    .format(SRC=src,
                            DST_MIN=limit_min[dst],
                            DST_MAX=limit_max[dst]))

        # Integer to integer conversion where sizeof(src) < sizeof(dst)
        elif src not in unsigned_types and dst in unsigned_types:
            print("  x = __clc_max(x, ({SRC})0);".format(SRC=src))

        print("  return {FN}(x);".format(FN=clc_core_fn_name(dst, size=size)))

    # Footer
    print("}")
    close_conditional_guard(close_conditional)