summaryrefslogtreecommitdiffstats
path: root/src/ring.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-08-09 21:51:53 -0400
committerDavid Robillard <d@drobilla.net>2022-08-12 01:05:15 -0400
commit541e57f889e297af350b38e4e8fc68d45cd35081 (patch)
treef307ac3b4d489321e695191d1ed3f0829b1810b2 /src/ring.c
parent6c4d95faca8b46ffa6352515293765b0e3b93bce (diff)
downloadzix-541e57f889e297af350b38e4e8fc68d45cd35081.tar.gz
zix-541e57f889e297af350b38e4e8fc68d45cd35081.tar.bz2
zix-541e57f889e297af350b38e4e8fc68d45cd35081.zip
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.
Diffstat (limited to 'src/ring.c')
-rw-r--r--src/ring.c16
1 files changed, 2 insertions, 14 deletions
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