summaryrefslogtreecommitdiff
path: root/isp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2012-02-01 15:02:07 +0100
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2012-04-16 23:40:56 +0200
commite6d01ebf2b9999a6eb00a0da3266bc26a7b2402e (patch)
treefdd63c56beef1bed7966bad9f74d6ab5dcd0afe0 /isp
parent9d1912c95f99339fe030d39dcae1928bb402b64b (diff)
tools: Add min_t, max_t, clamp and clamp_t macros
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'isp')
-rw-r--r--isp/tools.h41
1 files changed, 39 insertions, 2 deletions
diff --git a/isp/tools.h b/isp/tools.h
index b60cc2e..af29dd9 100644
--- a/isp/tools.h
+++ b/isp/tools.h
@@ -25,8 +25,45 @@
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
-#define min(a, b) ((a) < (b) ? (a) : (b))
-#define max(a, b) ((a) > (b) ? (a) : (b))
+#define min(a, b) ({ \
+ typeof(a) __a = (a); \
+ typeof(b) __b = (b); \
+ __a < __b ? __a : __b; \
+})
+
+#define min_t(type, a, b) ({ \
+ type __a = (a); \
+ type __b = (b); \
+ __a < __b ? __a : __b; \
+})
+
+#define max(a, b) ({ \
+ typeof(a) __a = (a); \
+ typeof(b) __b = (b); \
+ __a > __b ? __a : __b; \
+})
+
+#define max_t(type, a, b) ({ \
+ type __a = (a); \
+ type __b = (b); \
+ __a > __b ? __a : __b; \
+})
+
+#define clamp(val, min, max) ({ \
+ typeof(val) __val = (val); \
+ typeof(min) __min = (min); \
+ typeof(max) __max = (max); \
+ __val = __val < __min ? __min : __val; \
+ __val > __max ? __max : __val; \
+})
+
+#define clamp_t(type, val, min, max) ({ \
+ type __val = (val); \
+ type __min = (min); \
+ type __max = (max); \
+ __val = __val < __min ? __min : __val; \
+ __val > __max ? __max : __val; \
+})
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - offsetof(type, member))