2 #include
"../clcmacro.h"
4 _CLC_OVERLOAD _CLC_DEF char mad_sat
(char x
, char y
, char z
) {
5 return clamp
((short)mad24
((short)x
, (short)y
, (short)z
), (short)CHAR_MIN
, (short) CHAR_MAX
);
8 _CLC_OVERLOAD _CLC_DEF uchar mad_sat
(uchar x
, uchar y
, uchar z
) {
9 return clamp
((ushort)mad24
((ushort)x
, (ushort)y
, (ushort)z
), (ushort)0, (ushort) UCHAR_MAX
);
12 _CLC_OVERLOAD _CLC_DEF short mad_sat
(short x
, short y
, short z
) {
13 return clamp
((int)mad24
((int)x
, (int)y
, (int)z
), (int)SHRT_MIN
, (int) SHRT_MAX
);
16 _CLC_OVERLOAD _CLC_DEF ushort mad_sat
(ushort x
, ushort y
, ushort z
) {
17 return clamp
((uint)mad24
((uint)x
, (uint)y
, (uint)z
), (uint)0, (uint) USHRT_MAX
);
20 _CLC_OVERLOAD _CLC_DEF int mad_sat
(int x
, int y
, int z
) {
21 int mhi
= mul_hi
(x, y
);
23 long m
= upsample
(mhi, mlo
);
32 _CLC_OVERLOAD _CLC_DEF uint mad_sat
(uint x
, uint y
, uint z
) {
33 if
(mul_hi(x, y
) != 0)
35 return add_sat
(x * y
, z
);
38 _CLC_OVERLOAD _CLC_DEF long mad_sat
(long x
, long y
, long z
) {
39 long hi
= mul_hi
(x, y
);
42 /* Big overflow of more than
2 bits
, add can
't fix this
*/
43 if
(((x < 0) == (y < 0)) && hi
!= 0)
45 /* Low overflow in mul and z not neg enough to correct it
*/
46 if
(hi == 0 && ulo
>= LONG_MAX
&& (z > 0 ||
(ulo + z
) > LONG_MAX
))
48 /* Big overflow of more than
2 bits
, add can
't fix this
*/
49 if
(((x < 0) != (y < 0)) && hi
!= -
1)
51 /* Low overflow in mul and z not pos enough to correct it
*/
52 if
(hi == -
1 && ulo
<= ((ulong)LONG_MAX
+ 1UL) && (z < 0 || z
< (LONG_MAX - ulo
)))
54 /* We have checked all conditions
, any overflow in addition returns
55 * the correct value
*/
59 _CLC_OVERLOAD _CLC_DEF ulong mad_sat
(ulong x
, ulong y
, ulong z
) {
60 if
(mul_hi(x, y
) != 0)
62 return add_sat
(x * y
, z
);
65 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, char
, mad_sat
, char
, char
, char
)
66 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, uchar
, mad_sat
, uchar
, uchar
, uchar
)
67 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, short
, mad_sat
, short
, short
, short
)
68 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, ushort
, mad_sat
, ushort
, ushort
, ushort
)
69 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, int
, mad_sat
, int
, int
, int
)
70 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, uint
, mad_sat
, uint
, uint
, uint
)
71 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, long
, mad_sat
, long
, long
, long
)
72 _CLC_TERNARY_VECTORIZE
(_CLC_OVERLOAD _CLC_DEF
, ulong
, mad_sat
, ulong
, ulong
, ulong
)