diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp
index 30d2a53903ec..36fe4a6c9154 100644
--- a/src/ipa/ipu3/ipu3.cpp
+++ b/src/ipa/ipu3/ipu3.cpp
@@ -293,33 +293,42 @@ int IPAIPU3::start()
  */
 void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
 {
-	uint32_t minError = std::numeric_limits<uint32_t>::max();
 	Size best;
 	Size bestLog2;
 
 	/* Set the BDS output size in the IPAConfiguration structure */
 	context_.configuration.grid.bdsOutputSize = bdsOutputSize;
 
-	for (uint32_t widthShift = 3; widthShift <= 7; ++widthShift) {
+	uint32_t minError = std::numeric_limits<uint32_t>::max();
+
+	for (uint32_t shift = 3; shift <= 7; ++shift) {
 		uint32_t width = std::min(kMaxCellWidthPerSet,
-					  bdsOutputSize.width >> widthShift);
-		width = width << widthShift;
-		for (uint32_t heightShift = 3; heightShift <= 7; ++heightShift) {
-			int32_t height = std::min(kMaxCellHeightPerSet,
-						  bdsOutputSize.height >> heightShift);
-			height = height << heightShift;
-			uint32_t error  = std::abs(static_cast<int>(width - bdsOutputSize.width))
-							+ std::abs(static_cast<int>(height - bdsOutputSize.height));
+					  bdsOutputSize.width >> shift);
+		width = width << shift;
+		uint32_t error = std::abs(static_cast<int>(width - bdsOutputSize.width));
 
-			if (error > minError)
-				continue;
+		if (error > minError)
+			continue;
 
-			minError = error;
-			best.width = width;
-			best.height = height;
-			bestLog2.width = widthShift;
-			bestLog2.height = heightShift;
-		}
+		minError = error;
+		best.width = width;
+		bestLog2.width = shift;
+	}
+
+	minError = std::numeric_limits<uint32_t>::max();
+
+	for (uint32_t shift = 3; shift <= 7; ++shift) {
+		uint32_t height = std::min(kMaxCellHeightPerSet,
+					   bdsOutputSize.height >> shift);
+		height = height << shift;
+		uint32_t error = std::abs(static_cast<int>(height - bdsOutputSize.height));
+
+		if (error > minError)
+			continue;
+
+		minError = error;
+		best.height = height;
+		bestLog2.height = shift;
 	}
 
 	struct ipu3_uapi_grid_config &bdsGrid = context_.configuration.grid.bdsGrid;
