(svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
authortron
Sun, 22 May 2005 07:12:09 +0000
changeset 1852 9cfa8bf9d39f
parent 1851 575bc84a181d
child 1853 2c2f5699e75f
(svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
Use them exemplarily to prettify (Get|Set)Tile(Type|Height)
macros.h
tile.h
--- a/macros.h	Sat May 21 21:30:13 2005 +0000
+++ b/macros.h	Sun May 22 07:12:09 2005 +0000
@@ -151,4 +151,9 @@
 	}
 #endif
 
+// Fetch count bits starting at bit start from value
+#define GB(value, start, count) (((value) >> (start)) & ((1 << (count)) - 1))
+// Set count bits in value starting at bit start to data
+#define SB(value, start, count, data) ((value) = ((value) & ~(((1 << (count)) - 1) << (start))) | ((data) << (start)))
+
 #endif /* MACROS_H */
--- a/tile.h	Sat May 21 21:30:13 2005 +0000
+++ b/tile.h	Sun May 22 07:12:09 2005 +0000
@@ -46,15 +46,14 @@
 static inline uint TileHeight(TileIndex tile)
 {
 	assert(tile < MapSize());
-	return _map_type_and_height[tile] & 0xf;
+	return GB(_map_type_and_height[tile], 0, 4);
 }
 
 static inline void SetTileHeight(TileIndex tile, uint height)
 {
 	assert(tile < MapSize());
 	assert(height < 16);
-	_map_type_and_height[tile] &= ~0x0F;
-	_map_type_and_height[tile] |= height;
+	SB(_map_type_and_height[tile], 0, 4, height);
 }
 
 static inline uint TilePixelHeight(TileIndex tile)
@@ -65,14 +64,13 @@
 static inline TileType GetTileType(TileIndex tile)
 {
 	assert(tile < MapSize());
-	return _map_type_and_height[tile] >> 4;
+	return GB(_map_type_and_height[tile], 4, 4);
 }
 
 static inline void SetTileType(TileIndex tile, TileType type)
 {
 	assert(tile < MapSize());
-	_map_type_and_height[tile] &= ~0xF0;
-	_map_type_and_height[tile] |= type << 4;
+	SB(_map_type_and_height[tile], 4, 4, type);
 }
 
 static inline bool IsTileType(TileIndex tile, TileType type)