tools/source/misc/fix16.cxx |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

New commits:
commit 746f66f4b1062f604cd1cfc638508c79477e13ee
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Sat Oct 5 21:18:20 2024 +0100
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Tue Oct 22 20:37:17 2024 +0200

    cid#1607748 Overflowed constant
    
    Change-Id: Id077fdc914f461a3631c42c37b2d5be37632e0e2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/175374
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/tools/source/misc/fix16.cxx b/tools/source/misc/fix16.cxx
index b726acb8f660..3e2abbb2a435 100644
--- a/tools/source/misc/fix16.cxx
+++ b/tools/source/misc/fix16.cxx
@@ -76,6 +76,8 @@ fix16_t fix16_mul(fix16_t inArg0, fix16_t inArg1)
     return result;
 }
 
+static uint32_t mask(int bits) { return (1 << bits) - 1; }
+
 /* 32-bit implementation of fix16_div. Fastest version for e.g. ARM Cortex M3.
  * Performs 32-bit divisions repeatedly to reduce the remainder. For this to
  * be efficient, the processor has to have 32-bit hardware division.
@@ -111,13 +113,13 @@ fix16_t fix16_div(fix16_t a, fix16_t b)
         bit_pos -= 4;
     }
 
-    while (remainder && bit_pos >= 0)
+    while (remainder > 0 && bit_pos >= 0)
     {
         // Shift remainder as much as we can without overflowing
         int shift = std::countl_zero(remainder);
         if (shift > bit_pos)
             shift = bit_pos;
-        remainder <<= shift;
+        remainder = (remainder & mask(32 - shift)) << shift;
         bit_pos -= shift;
 
         uint32_t div = remainder / divider;

Reply via email to