diff --git a/libclc/generic/lib/gen_convert.py b/libclc/generic/lib/gen_convert.py index fec6ee5f8887..f335d49a950d 100644 --- a/libclc/generic/lib/gen_convert.py +++ b/libclc/generic/lib/gen_convert.py @@ -445,12 +445,18 @@ def generate_float_conversion(src, dst, size, mode, sat): USRC=unsigned_type[src], N=size ) ) - # For integer sources, check for saturation case - print( - " return select(r, nextafter(r, sign(r) * ({DST}{N})-INFINITY), convert_{BOOL}{N}(abs_y > abs_x || (abs_y == abs_x && abs_y == ({USRC}{N}){SRC_MAX})));".format( - DST=dst, N=size, BOOL=bool_type[dst], USRC=unsigned_type[src], SRC_MAX=limit_max[src] + if dst == "float" and src in ["int", "uint"]: + print( + " return select(r, nextafter(r, sign(r) * ({DST}{N})-INFINITY), convert_{BOOL}{N}(abs_y > abs_x || (abs_y == abs_x && abs_y == ({USRC}{N}){SRC_MAX})));".format( + DST=dst, N=size, BOOL=bool_type[dst], USRC=unsigned_type[src], SRC_MAX=limit_max[src] + ) + ) + else: + print( + " return select(r, nextafter(r, sign(r) * ({DST}{N})-INFINITY), convert_{BOOL}{N}(abs_y > abs_x));".format( + DST=dst, N=size, BOOL=bool_type[dst] + ) ) - ) else: print(" {SRC}{N} abs_x = fabs(x);".format(SRC=src, N=size)) print(" {SRC}{N} abs_y = fabs(y);".format(SRC=src, N=size)) @@ -467,15 +473,19 @@ def generate_float_conversion(src, dst, size, mode, sat): ) if mode == "_rtn": if src in int_types: - # For integer sources, check for saturation case when converting back - # Cast the constant to source type to match vector element type - print( - " return select(r, nextafter(r, ({DST}{N})-INFINITY), convert_{BOOL}{N}(y > x || (y == x && y == ({SRC}{N}){SRC_MAX})));".format( - DST=dst, N=size, BOOL=bool_type[dst], SRC=src, SRC_MAX=limit_max[src] + if dst == "float" and src in ["int", "uint"]: + print( + " return select(r, nextafter(r, ({DST}{N})-INFINITY), convert_{BOOL}{N}(y > x || (y == x && y == ({SRC}{N}){SRC_MAX})));".format( + DST=dst, N=size, BOOL=bool_type[dst], SRC=src, SRC_MAX=limit_max[src] + ) + ) + else: + print( + " return select(r, nextafter(r, ({DST}{N})-INFINITY), convert_{BOOL}{N}(y > x));".format( + DST=dst, N=size, BOOL=bool_type[dst] + ) ) - ) else: - # For float sources, use original logic print( " return select(r, nextafter(r, ({DST}{N})-INFINITY), convert_{BOOL}{N}(y > x));".format( DST=dst, N=size, BOOL=bool_type[dst]