(svn r3205) Some more uses for GB/SB
authortron
Wed, 16 Nov 2005 13:11:28 +0000
changeset 2663 f3e7d6d3e3a1
parent 2662 42c11a1a0a4b
child 2664 fcefd29dd9d2
(svn r3205) Some more uses for GB/SB
industry_cmd.c
macros.h
oldloader.c
rail_gui.c
tunnelbridge_cmd.c
--- a/industry_cmd.c	Wed Nov 16 12:52:01 2005 +0000
+++ b/industry_cmd.c	Wed Nov 16 13:11:28 2005 +0000
@@ -561,8 +561,8 @@
 	case 10:
 		if ((_tick_counter & 3) == 0) {
 			m = _m[tile].m1;
-			if ((m & (31<<2)) == (6 << 2)) {
-				_m[tile].m1 = m&~(31<<2);
+			if (GB(m, 2, 5) == 6) {
+				SB(_m[tile].m1, 2, 5, 0);
 				DeleteAnimatedTile(tile);
 			} else {
 				_m[tile].m1 = m + (1<<2);
@@ -614,7 +614,7 @@
 				_m[tile].m5 = 29;
 				DeleteAnimatedTile(tile);
 			} else {
-				_m[tile].m1 = (_m[tile].m1 & ~3) | m;
+				SB(_m[tile].m1, 0, 2, m);
 				_m[tile].m5 = n;
 				MarkTileDirtyByTile(tile);
 			}
--- a/macros.h	Wed Nov 16 12:52:01 2005 +0000
+++ b/macros.h	Wed Nov 16 13:11:28 2005 +0000
@@ -5,6 +5,13 @@
 
 #include "map.h"
 
+/// Fetch n bits starting at bit s from x
+#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
+/// Set n bits starting at bit s in x to d
+#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
+/// Add i to the n bits starting at bit s in x
+#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
+
 #ifdef min
 #undef min
 #endif
@@ -86,20 +93,20 @@
 
 Faster ( or at least cleaner ) implementation below?
 */
-	if ( (byte) value == 0) {
-		return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
+	if (GB(value, 0, 8) == 0) {
+		return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
 	} else {
-		return FIND_FIRST_BIT(value & 0x3F);
+		return FIND_FIRST_BIT(GB(value, 0, 6));
 	}
 
 }
 
 static inline int KillFirstBit2x64(int value)
 {
-	if ( (byte) value == 0) {
-		return KILL_FIRST_BIT((value >> 8) & 0x3F) << 8;
+	if (GB(value, 0, 8) == 0) {
+		return KILL_FIRST_BIT(GB(value, 8, 6)) << 8;
 	} else {
-		return value & (KILL_FIRST_BIT(value & 0x3F)|0x3F00);
+		return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00);
 	}
 }
 
@@ -144,13 +151,6 @@
 	}
 #endif
 
-/// Fetch n bits starting at bit s from x
-#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
-/// Set n bits starting at bit s in x to d
-#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
-/// Add i to the n bits starting at bit s in x
-#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
-
 /**
  * ROtate x Left/Right by n (must be >= 0)
  * @note Assumes a byte has 8 bits
--- a/oldloader.c	Wed Nov 16 12:52:01 2005 +0000
+++ b/oldloader.c	Wed Nov 16 13:11:28 2005 +0000
@@ -193,7 +193,7 @@
 				uint32 res = 0;
 
 				/* Reading from the file: bit 16 to 23 have the FILE */
-				switch (((chunk->type >> 16) & 0xFF) << 16) {
+				switch (GB(chunk->type, 16, 8) << 16) {
 					case OC_FILE_I8:
 						res = ReadByte(ls);
 						res = (int8)res;
@@ -234,7 +234,7 @@
 				assert(base_ptr != NULL || chunk->ptr != NULL);
 
 				/* Writing to the var: bit 8 till 15 have the VAR */
-				switch (((chunk->type >> 8) & 0xFF) << 8) {
+				switch (GB(chunk->type, 8, 8) << 8) {
 					case OC_VAR_I8:
 						/* Write the data */
 						if (chunk->ptr != NULL) {
--- a/rail_gui.c	Wed Nov 16 12:52:01 2005 +0000
+++ b/rail_gui.c	Wed Nov 16 13:11:28 2005 +0000
@@ -85,8 +85,8 @@
 {
 	byte b = _m[tile].m5;
 
-	if (b & 0xC0 || !(b & (extra >> 8)))
-		return;
+	if (GB(b, 6, 2) != RAIL_TYPE_NORMAL >> 6) return;
+	if (!(b & (extra >> 8))) return;
 
 	DoCommandP(tile, _cur_railtype, extra & 0xFF, NULL, CMD_BUILD_SINGLE_RAIL | CMD_AUTO | CMD_NO_WATER);
 }
--- a/tunnelbridge_cmd.c	Wed Nov 16 12:52:01 2005 +0000
+++ b/tunnelbridge_cmd.c	Wed Nov 16 13:11:28 2005 +0000
@@ -430,7 +430,10 @@
 		FindLandscapeHeightByTile(&ti, tile);
 	} while (z < ti.z);
 
-	if (z == ti.z && ti.type == MP_TUNNELBRIDGE && (ti.map5&0xF0) == 0 && (ti.map5&3) == dir) {
+	if (z == ti.z &&
+			ti.type == MP_TUNNELBRIDGE &&
+			GB(ti.map5, 4, 4) == 0 &&
+			GB(ti.map5, 0, 2) == dir) {
 		_error_message = STR_5003_ANOTHER_TUNNEL_IN_THE_WAY;
 		return false;
 	}