2012-01-09 06:09:58 +08:00
|
|
|
#include <clc/clc.h>
|
2014-06-24 17:36:32 +08:00
|
|
|
#include "../clcmacro.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
// From add_sat.ll
|
2012-08-06 06:24:36 +08:00
|
|
|
_CLC_DECL char __clc_add_sat_s8(char, char);
|
|
|
|
_CLC_DECL uchar __clc_add_sat_u8(uchar, uchar);
|
|
|
|
_CLC_DECL short __clc_add_sat_s16(short, short);
|
|
|
|
_CLC_DECL ushort __clc_add_sat_u16(ushort, ushort);
|
|
|
|
_CLC_DECL int __clc_add_sat_s32(int, int);
|
|
|
|
_CLC_DECL uint __clc_add_sat_u32(uint, uint);
|
|
|
|
_CLC_DECL long __clc_add_sat_s64(long, long);
|
|
|
|
_CLC_DECL ulong __clc_add_sat_u64(ulong, ulong);
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF char add_sat(char x, char y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
short r = x + y;
|
|
|
|
return convert_char_sat(r);
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF uchar add_sat(uchar x, uchar y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
ushort r = x + y;
|
|
|
|
return convert_uchar_sat(r);
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF short add_sat(short x, short y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
int r = x + y;
|
|
|
|
return convert_short_sat(r);
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF ushort add_sat(ushort x, ushort y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
uint r = x + y;
|
|
|
|
return convert_ushort_sat(r);
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF int add_sat(int x, int y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
int r;
|
|
|
|
if (__builtin_sadd_overflow(x, y, &r))
|
|
|
|
// The oveflow can only occur if both are pos or both are neg,
|
|
|
|
// thus we only need to check one operand
|
|
|
|
return x > 0 ? INT_MAX : INT_MIN;
|
|
|
|
return r;
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF uint add_sat(uint x, uint y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
uint r;
|
|
|
|
if (__builtin_uadd_overflow(x, y, &r))
|
|
|
|
return UINT_MAX;
|
|
|
|
return r;
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF long add_sat(long x, long y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
long r;
|
|
|
|
if (__builtin_saddl_overflow(x, y, &r))
|
|
|
|
// The oveflow can only occur if both are pos or both are neg,
|
|
|
|
// thus we only need to check one operand
|
|
|
|
return x > 0 ? LONG_MAX : LONG_MIN;
|
|
|
|
return r;
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_OVERLOAD _CLC_DEF ulong add_sat(ulong x, ulong y) {
|
2017-10-03 02:39:00 +08:00
|
|
|
ulong r;
|
|
|
|
if (__builtin_uaddl_overflow(x, y, &r))
|
|
|
|
return ULONG_MAX;
|
|
|
|
return r;
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, char, add_sat, char, char)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, uchar, add_sat, uchar, uchar)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, short, add_sat, short, short)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, ushort, add_sat, ushort, ushort)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, int, add_sat, int, int)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, uint, add_sat, uint, uint)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, long, add_sat, long, long)
|
|
|
|
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, ulong, add_sat, ulong, ulong)
|