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

This commit is contained in:
Campbell Barton
2013-12-08 17:29:22 +11:00
parent 8a9a060b67
commit c005fb407b
5 changed files with 18 additions and 11 deletions

View File

@@ -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;

View File

@@ -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))))

View File

@@ -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");

View File

@@ -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;

View File

@@ -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) \