[Bf-blender-cvs] [c005fb4] master: Util-defines: avoid multiple calculation/access for MIN/MAX macros

Campbell Barton noreply at git.blender.org
Sun Dec 8 07:30:27 CET 2013


Commit: c005fb407bbb7aac4cf138fbba903bbb2c821c93
Author: Campbell Barton
Date:   Sun Dec 8 17:29:22 2013 +1100
http://developer.blender.org/rBc005fb407bbb7aac4cf138fbba903bbb2c821c93

Util-defines: avoid multiple calculation/access for MIN/MAX macros

===================================================================

M	source/blender/blenkernel/intern/bmfont.c
M	source/blender/blenlib/BLI_utildefines.h
M	source/blender/blenlib/intern/BLI_heap.c
M	source/blender/blenlib/intern/BLI_mempool.c
M	source/blender/makesrna/intern/rna_define.c

===================================================================

diff --git a/source/blender/blenkernel/intern/bmfont.c b/source/blender/blenkernel/intern/bmfont.c
index e962e8c..79b3f89 100644
--- a/source/blender/blenkernel/intern/bmfont.c
+++ b/source/blender/blenkernel/intern/bmfont.c
@@ -68,9 +68,6 @@ void printfGlyph(bmGlyph *glyph)
 	printf(" advan: %3d reser: %3d\n", glyph->advance, glyph->reserved);
 }
 
-#define MAX2(x, y)          ((x) > (y) ? (x) : (y))
-#define MAX3(x, y, z)       (MAX2(MAX2((x), (y)), (z)))
-
 void calcAlpha(ImBuf *ibuf)
 {
 	int i;
diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h
index 9e12976..ef2c61c 100644
--- a/source/blender/blenlib/BLI_utildefines.h
+++ b/source/blender/blenlib/BLI_utildefines.h
@@ -48,11 +48,26 @@
 #endif
 
 /* min/max */
+#if defined(__GNUC__) || defined(__clang__)
+
+#define MIN2(x, y)  ({  \
+	typeof(x) x_ = (x); \
+	typeof(y) y_ = (y); \
+	((x_) < (y_) ? (x_) : (y_)); })
+
+#define MAX2(x, y)  ({  \
+	typeof(x) x_ = (x); \
+	typeof(y) y_ = (y); \
+	((x_) > (y_) ? (x_) : (y_)); })
+
+#else
 #define MIN2(x, y)          ((x) < (y) ? (x) : (y))
+#define MAX2(x, y)          ((x) > (y) ? (x) : (y))
+#endif
+
 #define MIN3(x, y, z)       (MIN2(MIN2((x), (y)), (z)))
 #define MIN4(x, y, z, a)    (MIN2(MIN2((x), (y)), MIN2((z), (a))))
 
-#define MAX2(x, y)          ((x) > (y) ? (x) : (y))
 #define MAX3(x, y, z)       (MAX2(MAX2((x), (y)), (z)))
 #define MAX4(x, y, z, a)    (MAX2(MAX2((x), (y)), MAX2((z), (a))))
 
diff --git a/source/blender/blenlib/intern/BLI_heap.c b/source/blender/blenlib/intern/BLI_heap.c
index 206a804..55dee4e 100644
--- a/source/blender/blenlib/intern/BLI_heap.c
+++ b/source/blender/blenlib/intern/BLI_heap.c
@@ -125,7 +125,7 @@ Heap *BLI_heap_new_ex(unsigned int tot_reserve)
 {
 	Heap *heap = (Heap *)MEM_callocN(sizeof(Heap), __func__);
 	/* ensure we have at least one so we can keep doubling it */
-	heap->bufsize = MAX2(1, tot_reserve);
+	heap->bufsize = MAX2(1u, tot_reserve);
 	heap->tree = (HeapNode **)MEM_mallocN(heap->bufsize * sizeof(HeapNode *), "BLIHeapTree");
 	heap->arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), "heap arena");
 
diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c
index 28539d9..c2b5402 100644
--- a/source/blender/blenlib/intern/BLI_mempool.c
+++ b/source/blender/blenlib/intern/BLI_mempool.c
@@ -257,7 +257,7 @@ BLI_mempool *BLI_mempool_create(unsigned int esize, unsigned int totelem,
 	}
 
 	if (flag & BLI_MEMPOOL_ALLOW_ITER) {
-		pool->esize = MAX2(esize, (int)sizeof(BLI_freenode));
+		pool->esize = MAX2(esize, (unsigned int)sizeof(BLI_freenode));
 	}
 	else {
 		pool->esize = esize;
diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c
index fe9e957..fe38ce2 100644
--- a/source/blender/makesrna/intern/rna_define.c
+++ b/source/blender/makesrna/intern/rna_define.c
@@ -66,11 +66,6 @@ BlenderDefRNA DefRNA = {NULL, {NULL, NULL}, {NULL, NULL}, NULL, 0, 0, 0, 1, 1};
 
 /* Duplicated code since we can't link in blenkernel or blenlib */
 
-#ifndef MIN2
-#define MIN2(x, y) ((x) < (y) ? (x) : (y))
-#define MAX2(x, y) ((x) > (y) ? (x) : (y))
-#endif
-
 /* pedantic check for '.', do this since its a hassle for translators */
 #ifndef NDEBUG
 #  define DESCR_CHECK(description, id1, id2)                                  \




More information about the Bf-blender-cvs mailing list