From f84c1dbe57f85b3628ed47deee64cd2ee16a04ef Mon Sep 17 00:00:00 2001
From: "t.animal" <ar79yxiw@cip.cs.fau.de>
Date: Tue, 24 Nov 2015 15:50:06 +0100
Subject: [PATCH] CPU: optimize error checks

---
 vm/comp/instructionBlocks/cpu_addInst.c     |  8 ++++----
 vm/comp/instructionBlocks/cpu_compareInst.c |  8 ++++----
 vm/comp/instructionBlocks/cpu_moveInst.c    |  8 ++++----
 vm/comp/instructionBlocks/cpu_specialInst.c | 14 +++++++-------
 vm/comp/instructionBlocks/cpu_xorInst.c     |  8 ++++----
 5 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/vm/comp/instructionBlocks/cpu_addInst.c b/vm/comp/instructionBlocks/cpu_addInst.c
index 265db9d..1d39717 100644
--- a/vm/comp/instructionBlocks/cpu_addInst.c
+++ b/vm/comp/instructionBlocks/cpu_addInst.c
@@ -1,6 +1,6 @@
 case 0x00:{
 	/*ADD r/m8 rm8*/
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t op1;
 		if(s_op.regmem_type == MEMORY)
 			op1 = cpu_read_byte_from_mem(cpu_state, s_op.regmem_mem);
@@ -32,7 +32,7 @@ case 0x00:{
 
 case 0x01:{
 	/*r/ADD m32 r32.*/
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t op1;
 		if(s_op.regmem_type == MEMORY)
 			op1 = cpu_read_word_from_mem(cpu_state, s_op.regmem_mem);
@@ -66,7 +66,7 @@ case 0x01:{
 
 case 0x02:{
 	/*ADD r8 r/m8.*/
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t op1 = cpu_read_byte_from_reg(s_op.reg, IS_HIGH(s_op.reg));
 		uint8_t op2;
 		if(s_op.regmem_type == MEMORY)
@@ -96,7 +96,7 @@ case 0x02:{
 
 case 0x03:{
 	/*ADD r32 r/m32.*/
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t op1 = cpu_read_word_from_reg(s_op.reg);
 		uint32_t op2;
 		if(s_op.regmem_type == MEMORY)
diff --git a/vm/comp/instructionBlocks/cpu_compareInst.c b/vm/comp/instructionBlocks/cpu_compareInst.c
index 8b47cf7..e6caaaa 100644
--- a/vm/comp/instructionBlocks/cpu_compareInst.c
+++ b/vm/comp/instructionBlocks/cpu_compareInst.c
@@ -31,7 +31,7 @@ case 0x3D: {
 
 case 0x38: {
 	/*Compare r8 with r/m8. */
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t minuend, subtrahend;
 
 		subtrahend = cpu_read_byte_from_reg(s_op.reg, IS_HIGH(s_op.reg));
@@ -54,7 +54,7 @@ case 0x38: {
 }
 case 0x39: {
 	/*Compare r32 with r/m32. */
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t minuend, subtrahend;
 
 		subtrahend = cpu_read_word_from_reg(s_op.reg);
@@ -77,7 +77,7 @@ case 0x39: {
 }
 case 0x3A: {
 	/* Compare r/m8 with r8. */
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t minuend, subtrahend;
 
 		minuend = cpu_read_byte_from_reg(s_op.reg, IS_HIGH(s_op.reg));
@@ -101,7 +101,7 @@ case 0x3A: {
 
 case 0x3B: {
 	/* Compare r/m32 with r32. */
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t minuend, subtrahend;
 
 		minuend = cpu_read_word_from_reg(s_op.reg);
diff --git a/vm/comp/instructionBlocks/cpu_moveInst.c b/vm/comp/instructionBlocks/cpu_moveInst.c
index ea196ee..cdb96cf 100644
--- a/vm/comp/instructionBlocks/cpu_moveInst.c
+++ b/vm/comp/instructionBlocks/cpu_moveInst.c
@@ -1,6 +1,6 @@
 case 0x88: {
 	/* Copy r8 to r/m8 */
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t src = cpu_read_byte_from_reg(s_op.reg, IS_HIGH(s_op.reg));
 		if(s_op.regmem_type == MEMORY){
 			cpu_write_byte_in_mem(cpu_state, src, s_op.regmem_mem);
@@ -18,7 +18,7 @@ case 0x88: {
 }
 case 0x89: {
 	/* Copy r32 to r/m32 */
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t src = cpu_read_word_from_reg(s_op.reg);
 		if(s_op.regmem_type == MEMORY){
 			cpu_write_word_in_mem(cpu_state, src, s_op.regmem_mem);
@@ -37,7 +37,7 @@ case 0x89: {
 
 case 0x8A: {
 	/* Copy r/m8 to r8. */
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t src;
 		if(s_op.regmem_type == MEMORY){
 			src = cpu_read_byte_from_mem(cpu_state, s_op.regmem_mem);
@@ -57,7 +57,7 @@ case 0x8A: {
 
 case 0x8B: {
 	/* Copy r/m32 to r32. */
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t src;
 		if(s_op.regmem_type == MEMORY){
 			src = cpu_read_word_from_mem(cpu_state, s_op.regmem_mem);
diff --git a/vm/comp/instructionBlocks/cpu_specialInst.c b/vm/comp/instructionBlocks/cpu_specialInst.c
index 5c58254..ca32f3c 100644
--- a/vm/comp/instructionBlocks/cpu_specialInst.c
+++ b/vm/comp/instructionBlocks/cpu_specialInst.c
@@ -9,7 +9,7 @@ case 0x0f: {
 case 0x80: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0x80.c"
 		}
@@ -20,7 +20,7 @@ case 0x80: {
 case 0x81: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0x81.c"
 		}
@@ -31,7 +31,7 @@ case 0x81: {
 case 0x83: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0x83.c"
 		}
@@ -42,7 +42,7 @@ case 0x83: {
 case 0xC6: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0xC6.c"
 		}
@@ -53,7 +53,7 @@ case 0xC6: {
 case 0xC7: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0xC7.c"
 		}
@@ -64,7 +64,7 @@ case 0xC7: {
 case 0xF6: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0xF6.c"
 		}
@@ -75,7 +75,7 @@ case 0xF6: {
 case 0xFF: {
 	/* Special case: Specific instruction decoded in Mod/RM byte */
 
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		switch(s_op.reg_value){
 			#include "cpu_special0xFF.c"
 		}
diff --git a/vm/comp/instructionBlocks/cpu_xorInst.c b/vm/comp/instructionBlocks/cpu_xorInst.c
index 47dd04d..144bcc8 100644
--- a/vm/comp/instructionBlocks/cpu_xorInst.c
+++ b/vm/comp/instructionBlocks/cpu_xorInst.c
@@ -1,6 +1,6 @@
 case 0x30:{
 	/*r/m8 XOR r8.*/
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t src;
 		if(s_op.regmem_type == MEMORY)
 			src = cpu_read_byte_from_mem(cpu_state, s_op.regmem_mem);
@@ -32,7 +32,7 @@ case 0x30:{
 
 case 0x31:{
 	/*r/m32 XOR r32.*/
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t op1;
 		if(s_op.regmem_type == MEMORY)
 			op1 = cpu_read_word_from_mem(cpu_state, s_op.regmem_mem);
@@ -64,7 +64,7 @@ case 0x31:{
 
 case 0x32:{
 	/*r8 XOR r/m8.*/
-	if(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, EIGHT_BIT))){
 		uint8_t op1 = cpu_read_byte_from_reg(s_op.reg, IS_HIGH(s_op.reg));
 		uint8_t op2;
 		if(s_op.regmem_type == MEMORY)
@@ -93,7 +93,7 @@ case 0x32:{
 
 case 0x33:{
 	/*r32 XOR r/m32.*/
-	if(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT)){
+	if(likely(cpu_decode_RM(cpu_state, &s_op, !EIGHT_BIT))){
 		uint32_t op1 = cpu_read_word_from_reg(s_op.reg);
 		uint32_t op2;
 		if(s_op.regmem_type == MEMORY)
-- 
GitLab