From cba7f5e13ccea83e60ccfdf1895bef18bf50fe0a Mon Sep 17 00:00:00 2001 From: David Monniaux Date: Fri, 5 Apr 2019 15:13:33 +0200 Subject: implement using our "pattern matching" --- test/monniaux/ternary/ternary.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'test/monniaux/ternary') diff --git a/test/monniaux/ternary/ternary.c b/test/monniaux/ternary/ternary.c index 79025639..61da9597 100644 --- a/test/monniaux/ternary/ternary.c +++ b/test/monniaux/ternary/ternary.c @@ -5,16 +5,23 @@ typedef uint32_t data; -#if 0 -#define TERNARY(a, b, c) ((a) ? (b) : (c)) +static inline int32_t ternary_int32(int32_t a, int32_t b, int32_t c) { + return (((-((a) == 0)) & (c)) | ((-((a) != 0)) & (b))); +} +static inline uint32_t ternary_uint32(uint32_t a, uint32_t b, uint32_t c) { + return ternary_int32(a, b, c); +} + +#if defined(__COMPCERT__) && defined(__K1C__) +#define TERNARY(a, b, c) ternary_uint32((a), (b), (c)) #else -#define TERNARY(a, b, c) (((-(a)) & (b)) | ((-1+(a)) & (c))) +#define TERNARY(a, b, c) ((a) ? (b) : (c)) #endif data silly_computation(void) { data x = 1; for(int i=0; i<10000; i++) { - x = x * TERNARY(((x & 0x100) != 0), 45561U, 337777U); + x = x * TERNARY(x & 0x100, 45561U, 337777U); } return x; } -- cgit