From 541e57f889e297af350b38e4e8fc68d45cd35081 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 9 Aug 2022 21:51:53 -0400 Subject: Simplify ring space calculations It isn't necessary to branch here to avoid underflow. Essentially, the way that unsigned binary integers work "automatically" does what this code was doing. Note that these expressions only work for a ring buffer that, like this one, has a power of 2 (real) size and a maximum capacity 1 less than that. --- src/ring.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'src/ring.c') diff --git a/src/ring.c b/src/ring.c index 4998112..9b6a69b 100644 --- a/src/ring.c +++ b/src/ring.c @@ -102,11 +102,7 @@ zix_ring_reset(ZixRing* ring) static inline uint32_t read_space_internal(const ZixRing* ring, uint32_t r, uint32_t w) { - if (r < w) { - return w - r; - } - - return (w - r + ring->size) & ring->size_mask; + return (w - r) & ring->size_mask; } uint32_t @@ -118,15 +114,7 @@ zix_ring_read_space(const ZixRing* ring) static inline uint32_t write_space_internal(const ZixRing* ring, uint32_t r, uint32_t w) { - if (r == w) { - return ring->size - 1; - } - - if (r < w) { - return ((r - w + ring->size) & ring->size_mask) - 1; - } - - return (r - w) - 1; + return (r - w - 1U) & ring->size_mask; } uint32_t -- cgit v1.2.1