From f6ebfff9036b5c2b34982d465c6ff002ed569217 Mon Sep 17 00:00:00 2001
From: Alexander Golubev <fatzer2@gmail.com>
Date: Wed, 20 May 2026 10:29:53 +0300
Subject: tqlayoutengine: use TQ_INT64 for fixed-point arithmetic

When running on a sufficiently big displays (4K+) 32bit int is getting
overflowed.

Also simplify fRound().

Thanks to ugemkow for debugging this.

Closes: https://mirror.git.trinitydesktop.org/gitea/TDE/tdelibs/issues/361
Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
---
 src/kernel/tqlayoutengine.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/kernel/tqlayoutengine.cpp b/src/kernel/tqlayoutengine.cpp
index 131e64d4b..fd93b3046 100644
--- a/src/kernel/tqlayoutengine.cpp
+++ b/src/kernel/tqlayoutengine.cpp
@@ -43,9 +43,11 @@
 
 #ifndef TQT_NO_LAYOUT
 
-static inline int toFixed( int i ) { return i * 256; }
-static inline int fRound( int i ) {
-    return ( i % 256 < 128 ) ? i / 256 : 1 + i / 256;
+typedef TQ_INT64 fixed_t;
+
+static inline fixed_t toFixed( int i ) { return (fixed_t)i << 8; }
+static inline int fRound( fixed_t i ) {
+    return ( i + 128 ) >> 8;
 }
 
 /*
@@ -66,7 +68,6 @@ static inline int fRound( int i ) {
 TQ_EXPORT void qGeomCalc( TQMemArray<TQLayoutStruct> &chain, int start, int count,
 			 int pos, int space, int spacer )
 {
-    typedef int fixed;
     int cHint = 0;
     int cMin = 0;
     int cMax = 0;
@@ -121,8 +122,8 @@ TQ_EXPORT void qGeomCalc( TQMemArray<TQLayoutStruct> &chain, int start, int coun
 	bool finished = n == 0;
 	while ( !finished ) {
 	    finished = true;
-	    fixed fp_over = toFixed( overdraft );
-	    fixed fp_w = 0;
+	    fixed_t fp_over = toFixed( overdraft );
+	    fixed_t fp_w = 0;
 
 	    for ( i = start; i < start+count; i++ ) {
 		if ( chain[i].done )
@@ -176,8 +177,8 @@ TQ_EXPORT void qGeomCalc( TQMemArray<TQLayoutStruct> &chain, int start, int coun
 	int surplus, deficit;
 	do {
 	    surplus = deficit = 0;
-	    fixed fp_space = toFixed( space_left );
-	    fixed fp_w = 0;
+	    fixed_t fp_space = toFixed( space_left );
+	    fixed_t fp_w = 0;
 	    for ( i = start; i < start+count; i++ ) {
 		if ( chain[i].done )
 		    continue;
-- 
cgit v1.2.3

