From cf71049bd444274ef91c4f896ec0c28ae2c34262 Mon Sep 17 00:00:00 2001
From: Gregory Hainaut <gregory.hainaut@gmail.com>
Date: Thu, 26 Jan 2017 18:53:16 +0100
Subject: [PATCH] gsdx tc: use unsigned constant

Help the compiler to generate better code

C code:
uint32 addr = (i >> 3u) % MAX_BLOCKS;
uint32 row = addr >> 5u;
uint32 col = 1 << (addr & 31u);

ASM Before
     f48:	mov    eax,esi
     f4a:	mov    ecx,esi
     f4c:	mov    edx,DWORD PTR [ebp+0x8]
     f4f:	sar    eax,0x1f
     f52:	sar    ecx,0x3
     f55:	shr    eax,0x12
     f58:	add    ecx,eax
     f5a:	and    ecx,0x3fff
     f60:	sub    ecx,eax
     f62:	mov    eax,0x1
     f67:	shl    eax,cl
     f69:	shr    ecx,0x5
     f6c:	lea    edx,[edx+ecx*4]

ASM After
     f48:	mov    ecx,edi
     f4a:	mov    eax,0x1
     f4f:	sar    ecx,0x3
     f52:	shl    eax,cl
     f54:	shr    ecx,0x3
     f57:	and    ecx,0x7fc
     f5d:	add    ecx,DWORD PTR [ebp+0x8]
---
 plugins/GSdx/GS.h                 | 10 +++++-----
 plugins/GSdx/GSTextureCache.cpp   | 18 +++++++++---------
 plugins/GSdx/GSTextureCacheSW.cpp |  4 ++--
 plugins/GSdx/GSTextureCacheSW.h   |  2 +-
 4 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/plugins/GSdx/GS.h b/plugins/GSdx/GS.h
index c201a6393c..7e3385d837 100644
--- a/plugins/GSdx/GS.h
+++ b/plugins/GSdx/GS.h
@@ -23,11 +23,11 @@
 
 #define PLUGIN_VERSION 0
 
-#define VM_SIZE 4194304
-#define HALF_VM_SIZE (VM_SIZE / 2)
-#define PAGE_SIZE 8192
-#define BLOCK_SIZE 256
-#define COLUMN_SIZE 64
+#define VM_SIZE 4194304u
+#define HALF_VM_SIZE (VM_SIZE / 2u)
+#define PAGE_SIZE 8192u
+#define BLOCK_SIZE 256u
+#define COLUMN_SIZE 64u
 
 #define MAX_PAGES (VM_SIZE / PAGE_SIZE)
 #define MAX_BLOCKS (VM_SIZE / BLOCK_SIZE)
diff --git a/plugins/GSdx/GSTextureCache.cpp b/plugins/GSdx/GSTextureCache.cpp
index 5dd2baea50..ae7086c06c 100644
--- a/plugins/GSdx/GSTextureCache.cpp
+++ b/plugins/GSdx/GSTextureCache.cpp
@@ -1654,18 +1654,18 @@ void GSTextureCache::Source::Update(const GSVector4i& rect, int layer)
 	{
 		for(int y = r.top; y < r.bottom; y += bs.y)
 		{
-			uint32 base = off->block.row[y >> 3];
+			uint32 base = off->block.row[y >> 3u];
 
 			for(int x = r.left, i = (y << 7) + x; x < r.right; x += bs.x, i += bs.x)
 			{
-				uint32 block = base + off->block.col[x >> 3];
+				uint32 block = base + off->block.col[x >> 3u];
 
 				if(block < MAX_BLOCKS || m_wrap_gs_mem)
 				{
-					uint32 addr = (i >> 3) % MAX_BLOCKS;
+					uint32 addr = (i >> 3u) % MAX_BLOCKS;
 
-					uint32 row = addr >> 5;
-					uint32 col = 1 << (addr & 31);
+					uint32 row = addr >> 5u;
+					uint32 col = 1 << (addr & 31u);
 
 					if((m_valid[row] & col) == 0)
 					{
@@ -1683,18 +1683,18 @@ void GSTextureCache::Source::Update(const GSVector4i& rect, int layer)
 	{
 		for(int y = r.top; y < r.bottom; y += bs.y)
 		{
-			uint32 base = off->block.row[y >> 3];
+			uint32 base = off->block.row[y >> 3u];
 
 			for(int x = r.left; x < r.right; x += bs.x)
 			{
-				uint32 block = base + off->block.col[x >> 3];
+				uint32 block = base + off->block.col[x >> 3u];
 
 				if(block < MAX_BLOCKS || m_wrap_gs_mem)
 				{
 					block %= MAX_BLOCKS;
 
-					uint32 row = block >> 5;
-					uint32 col = 1 << (block & 31);
+					uint32 row = block >> 5u;
+					uint32 col = 1 << (block & 31u);
 
 					if((m_valid[row] & col) == 0)
 					{
diff --git a/plugins/GSdx/GSTextureCacheSW.cpp b/plugins/GSdx/GSTextureCacheSW.cpp
index 6a9b3de67d..cdb28faf56 100644
--- a/plugins/GSdx/GSTextureCacheSW.cpp
+++ b/plugins/GSdx/GSTextureCacheSW.cpp
@@ -125,9 +125,9 @@ void GSTextureCacheSW::RemoveAll()
 
 	m_textures.clear();
 
-	for(int i = 0; i < MAX_PAGES; i++)
+	for(auto& l : m_map)
 	{
-		m_map[i].clear();
+		l.clear();
 	}
 }
 
diff --git a/plugins/GSdx/GSTextureCacheSW.h b/plugins/GSdx/GSTextureCacheSW.h
index ed59acb35d..b090d2e8ae 100644
--- a/plugins/GSdx/GSTextureCacheSW.h
+++ b/plugins/GSdx/GSTextureCacheSW.h
@@ -57,7 +57,7 @@ public:
 protected:
 	GSState* m_state;
 	hash_set<Texture*> m_textures;
-	list<Texture*> m_map[MAX_PAGES];
+	std::array<std::list<Texture*>, MAX_PAGES> m_map;
 
 public:
 	GSTextureCacheSW(GSState* state);