authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2021-09-14 18:26:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-28 17:03:43-07:00
logcd8d8add9153b17b4579c2e8951ac3f3f42e1bcd
tree6e140beefb38a1ae50c2006aff0e12fc2ab0ce4e
parent68050852fac6940d04e15900f135e6fc88845f9b

sat-arithmetic: fix shl methods in cbe


1 files changed, 10 insertions(+), 6 deletions(-)

src/link/C/zig.h+10-6
......@@ -428,17 +428,21 @@ zig_mul_sat_s(int, int, long)
428428zig_mul_sat_s(long, long, long long)
429429
430430#define zig_shl_sat_u(ZT, T, bits) static inline T zig_shls_##ZT(T x, T y, T max) { \
431 T leading_zeros = __builtin_clz(x); \
432 return (leading_zeros + y > bits) ? max : x << y; \
431 if(x == 0) return 0; \
432 T bits_set = 64 - __builtin_clzll(x); \
433 return (bits_set + y > bits) ? max : x << y; \
433434}
434435
435436#define zig_shl_sat_s(ZT, T, bits) static inline T zig_shls_##ZT(T x, T y, T min, T max) { \
436 T leading_zeros = __builtin_clz(x & ~max); \
437 return (leading_zeros + y > bits) ? max : x << y; \
437 if(x == 0) return 0; \
438 T x_twos_comp = x < 0 ? -x : x; \
439 T bits_set = 64 - __builtin_clzll(x_twos_comp); \
440 T min_or_max = (x < 0) ? min : max; \
441 return (y + bits_set > bits ) ? min_or_max : x << y; \
438442}
439443
440zig_shl_sat_u(u8, uint8_t, 8)
441zig_shl_sat_s(i8, int8_t, 7)
444zig_shl_sat_u(u8, uint8_t, 8)
445zig_shl_sat_s(i8, int8_t, 7)
442446zig_shl_sat_u(u16, uint16_t, 16)
443447zig_shl_sat_s(i16, int16_t, 15)
444448zig_shl_sat_u(u32, uint32_t, 32)