From b3f71fc5ab2cbedbbb562b34ef977395db8e7e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Wed, 26 Jun 2019 21:27:51 +0200 Subject: [PATCH] Add ChiakiSeqNum32 --- lib/include/chiaki/seqnum.h | 42 ++++++++++++++++++++----------------- test/seqnum.c | 22 +++++++++++++++++++ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/lib/include/chiaki/seqnum.h b/lib/include/chiaki/seqnum.h index 16f2688..99f364e 100644 --- a/lib/include/chiaki/seqnum.h +++ b/lib/include/chiaki/seqnum.h @@ -27,27 +27,31 @@ extern "C" { #endif -typedef uint16_t ChiakiSeqNum16; - -#define CHIAKI_SEQ_NUM_16_MAX 0xffff - -static inline bool chiaki_seq_num_16_lt(ChiakiSeqNum16 a, ChiakiSeqNum16 b) -{ - if(a == b) - return false; - int32_t d = (int32_t)b - (int32_t)a; - return (a < b && d < (1 << 15)) - || ((a > b) && -d > (1 << 15)); +#define CHIAKI_DEFINE_SEQNUM(bits, greater_sint) \ +\ +typedef uint##bits##_t ChiakiSeqNum##bits; \ +\ +static inline bool chiaki_seq_num_##bits##_lt(ChiakiSeqNum##bits a, ChiakiSeqNum##bits b) \ +{ \ + if(a == b) \ + return false; \ + greater_sint d = (greater_sint)b - (greater_sint)a; \ + return (a < b && d < ((ChiakiSeqNum##bits)1 << (bits - 1))) \ + || ((a > b) && -d > ((ChiakiSeqNum##bits)1 << (bits - 1))); \ +} \ +\ +static inline bool chiaki_seq_num_##bits##_gt(ChiakiSeqNum##bits a, ChiakiSeqNum##bits b) \ +{ \ + if(a == b) \ + return false; \ + greater_sint d = (greater_sint)b - (greater_sint)a; \ + return (a < b && d > ((ChiakiSeqNum##bits)1 << (bits - 1))) \ + || ((a > b) && -d < ((ChiakiSeqNum##bits)1 << (bits - 1))); \ } -static inline bool chiaki_seq_num_16_gt(ChiakiSeqNum16 a, ChiakiSeqNum16 b) -{ - if(a == b) - return false; - int32_t d = (int32_t)b - (int32_t)a; - return (a < b && d > (1 << 15)) - || ((a > b) && -d < (1 << 15)); -} +CHIAKI_DEFINE_SEQNUM(16, int32_t) +CHIAKI_DEFINE_SEQNUM(32, int64_t) +#undef CHIAKI_DEFINE_SEQNUM #ifdef __cplusplus } diff --git a/test/seqnum.c b/test/seqnum.c index d4a77d1..42ad6e2 100644 --- a/test/seqnum.c +++ b/test/seqnum.c @@ -51,6 +51,20 @@ static MunitResult test_seq_num_16(const MunitParameter params[], void *user) } +static MunitResult test_seq_num_32(const MunitParameter params[], void *user) +{ + munit_assert(chiaki_seq_num_32_gt(1, 0)); + munit_assert(!chiaki_seq_num_32_gt(0, 1)); + munit_assert(!chiaki_seq_num_32_lt(1, 0)); + munit_assert(chiaki_seq_num_32_lt(0, 1)); + munit_assert(chiaki_seq_num_32_gt(1, 0xfffffff5)); + munit_assert(!chiaki_seq_num_32_gt(0xfffffff5, 1)); + + return MUNIT_OK; +} + + + MunitTest tests_seq_num[] = { { "/seq_num_16", @@ -60,5 +74,13 @@ MunitTest tests_seq_num[] = { MUNIT_TEST_OPTION_NONE, NULL }, + { + "/seq_num_32", + test_seq_num_32, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; \ No newline at end of file