From f95c47638d192d9f0109db9af4c1a38ab8eb2ecf Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Tue, 6 Jul 2021 07:57:26 +0200 Subject: [PATCH] CircularIndex: fight against type promotion in size checks To generate optimal code, size itself needs to be of the same type as the index to avoid promotion to the largest type. In full(), wrap the subtraction explicity in another type cast to avoid another automatic type promotion. --- src/hal/circular_buffer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hal/circular_buffer.h b/src/hal/circular_buffer.h index 4201a87..3c96faa 100644 --- a/src/hal/circular_buffer.h +++ b/src/hal/circular_buffer.h @@ -12,11 +12,11 @@ /// (recommended to keep uint8_fast8_t as single byte operations are atomical on the AVR) /// @param size number of index positions. /// It is recommended to keep a power of 2 to allow for optimal code generation on the AVR (there is no HW modulo instruction) -template +template class CircularIndex { public: #ifndef __AVR__ - static_assert(size <= std::numeric_limits::max(), + static_assert(size <= std::numeric_limits::max() / 2, "index_t is too small for the requested size"); #endif @@ -33,7 +33,7 @@ public: inline bool full() const { // alternative without wrap-around logic: // return tail != head && mask(tail) == mask(head); - return (head - tail) % (size * 2) == size; + return ((index_t)(head - tail) % (size * 2)) == size; } /// Advance the head index of the buffer.