1#include <clc/clc.h> 2#include "../clcmacro.h" 3 4_CLC_OVERLOAD _CLC_DEF char sub_sat(char x, char y) { 5 short r = x - y; 6 return convert_char_sat(r); 7} 8 9_CLC_OVERLOAD _CLC_DEF uchar sub_sat(uchar x, uchar y) { 10 short r = x - y; 11 return convert_uchar_sat(r); 12} 13 14_CLC_OVERLOAD _CLC_DEF short sub_sat(short x, short y) { 15 int r = x - y; 16 return convert_short_sat(r); 17} 18 19_CLC_OVERLOAD _CLC_DEF ushort sub_sat(ushort x, ushort y) { 20 int r = x - y; 21 return convert_ushort_sat(r); 22} 23 24_CLC_OVERLOAD _CLC_DEF int sub_sat(int x, int y) { 25 int r; 26 if (__builtin_ssub_overflow(x, y, &r)) 27 // The oveflow can only occur in the direction of the first operand 28 return x > 0 ? INT_MAX : INT_MIN; 29 return r; 30} 31 32_CLC_OVERLOAD _CLC_DEF uint sub_sat(uint x, uint y) { 33 uint r; 34 if (__builtin_usub_overflow(x, y, &r)) 35 return 0; 36 return r; 37} 38 39_CLC_OVERLOAD _CLC_DEF long sub_sat(long x, long y) { 40 long r; 41 if (__builtin_ssubl_overflow(x, y, &r)) 42 // The oveflow can only occur in the direction of the first operand 43 return x > 0 ? LONG_MAX : LONG_MIN; 44 return r; 45} 46 47_CLC_OVERLOAD _CLC_DEF ulong sub_sat(ulong x, ulong y) { 48 ulong r; 49 if (__builtin_usubl_overflow(x, y, &r)) 50 return 0; 51 return r; 52} 53 54_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, char, sub_sat, char, char) 55_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, uchar, sub_sat, uchar, uchar) 56_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, short, sub_sat, short, short) 57_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, ushort, sub_sat, ushort, ushort) 58_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, sub_sat, int, int) 59_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, uint, sub_sat, uint, uint) 60_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, long, sub_sat, long, long) 61_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, ulong, sub_sat, ulong, ulong) 62