aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarvey Harrison <harvey.harrison@gmail.com>2008-04-30 03:54:55 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-04-30 11:29:53 -0400
commitbdf4bbaaee3d4b8f555658333cbce1affe9070fb (patch)
treee85af55f310d5bd9391cc374bc32def7c5458046
parent354a1f4d99240f53980275416ca3e1ac2ee73d5d (diff)
Add macros similar to min/max/min_t/max_t
Also, change the variable names used in the min/max macros to avoid shadowed variable warnings when min/max min_t/max_t are nested. Small formatting changes to make all the macros have a similar form. [akpm@linux-foundation.org: coding-style fixes] [akpm@linux-foundation.org: fix v4l build] Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com> Cc: Mauro Carvalho Chehab <mchehab@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Bartlomiej Zolnierkiewicz <bzolnier@gmail.com> Cc: Jeff Garzik <jeff@garzik.org> Cc: Tejun Heo <htejun@gmail.com> Cc: Michael Buesch <mb@bu3sch.de> Cc: "John W. Linville" <linville@tuxdriver.com> Cc: Miklos Szeredi <miklos@szeredi.hu> Cc: Dmitry Torokhov <dtor@mail.ru> Cc: Jiri Kosina <jkosina@suse.cz> Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net> Cc: Randy Dunlap <randy.dunlap@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--drivers/media/video/bt8xx/bttvp.h2
-rw-r--r--drivers/media/video/usbvideo/vicam.c6
-rw-r--r--include/linux/kernel.h91
3 files changed, 74 insertions, 25 deletions
diff --git a/drivers/media/video/bt8xx/bttvp.h b/drivers/media/video/bt8xx/bttvp.h
index 03816b73f847..27da7b423275 100644
--- a/drivers/media/video/bt8xx/bttvp.h
+++ b/drivers/media/video/bt8xx/bttvp.h
@@ -81,8 +81,6 @@
81/* Limits scaled width, which must be a multiple of 4. */ 81/* Limits scaled width, which must be a multiple of 4. */
82#define MAX_HACTIVE (0x3FF & -4) 82#define MAX_HACTIVE (0x3FF & -4)
83 83
84#define clamp(x, low, high) min (max (low, x), high)
85
86#define BTTV_NORMS (\ 84#define BTTV_NORMS (\
87 V4L2_STD_PAL | V4L2_STD_PAL_N | \ 85 V4L2_STD_PAL | V4L2_STD_PAL_N | \
88 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \ 86 V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
diff --git a/drivers/media/video/usbvideo/vicam.c b/drivers/media/video/usbvideo/vicam.c
index 64819353276a..17f542dfb366 100644
--- a/drivers/media/video/usbvideo/vicam.c
+++ b/drivers/media/video/usbvideo/vicam.c
@@ -70,12 +70,6 @@
70 70
71#define VICAM_HEADER_SIZE 64 71#define VICAM_HEADER_SIZE 64
72 72
73#define clamp( x, l, h ) max_t( __typeof__( x ), \
74 ( l ), \
75 min_t( __typeof__( x ), \
76 ( h ), \
77 ( x ) ) )
78
79/* Not sure what all the bytes in these char 73/* Not sure what all the bytes in these char
80 * arrays do, but they're necessary to make 74 * arrays do, but they're necessary to make
81 * the camera work. 75 * the camera work.
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 53839ba265ec..4d46e299afb5 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -338,33 +338,90 @@ extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
338#endif /* __LITTLE_ENDIAN */ 338#endif /* __LITTLE_ENDIAN */
339 339
340/* 340/*
341 * min()/max() macros that also do 341 * min()/max()/clamp() macros that also do
342 * strict type-checking.. See the 342 * strict type-checking.. See the
343 * "unnecessary" pointer comparison. 343 * "unnecessary" pointer comparison.
344 */ 344 */
345#define min(x,y) ({ \ 345#define min(x, y) ({ \
346 typeof(x) _x = (x); \ 346 typeof(x) _min1 = (x); \
347 typeof(y) _y = (y); \ 347 typeof(y) _min2 = (y); \
348 (void) (&_x == &_y); \ 348 (void) (&_min1 == &_min2); \
349 _x < _y ? _x : _y; }) 349 _min1 < _min2 ? _min1 : _min2; })
350 350
351#define max(x,y) ({ \ 351#define max(x, y) ({ \
352 typeof(x) _x = (x); \ 352 typeof(x) _max1 = (x); \
353 typeof(y) _y = (y); \ 353 typeof(y) _max2 = (y); \
354 (void) (&_x == &_y); \ 354 (void) (&_max1 == &_max2); \
355 _x > _y ? _x : _y; }) 355 _max1 > _max2 ? _max1 : _max2; })
356
357/**
358 * clamp - return a value clamped to a given range with strict typechecking
359 * @val: current value
360 * @min: minimum allowable value
361 * @max: maximum allowable value
362 *
363 * This macro does strict typechecking of min/max to make sure they are of the
364 * same type as val. See the unnecessary pointer comparisons.
365 */
366#define clamp(val, min, max) ({ \
367 typeof(val) __val = (val); \
368 typeof(min) __min = (min); \
369 typeof(max) __max = (max); \
370 (void) (&__val == &__min); \
371 (void) (&__val == &__max); \
372 __val = __val < __min ? __min: __val; \
373 __val > __max ? __max: __val; })
356 374
357/* 375/*
358 * ..and if you can't take the strict 376 * ..and if you can't take the strict
359 * types, you can specify one yourself. 377 * types, you can specify one yourself.
360 * 378 *
361 * Or not use min/max at all, of course. 379 * Or not use min/max/clamp at all, of course.
380 */
381#define min_t(type, x, y) ({ \
382 type __min1 = (x); \
383 type __min2 = (y); \
384 __min1 < __min2 ? __min1: __min2; })
385
386#define max_t(type, x, y) ({ \
387 type __max1 = (x); \
388 type __max2 = (y); \
389 __max1 > __max2 ? __max1: __max2; })
390
391/**
392 * clamp_t - return a value clamped to a given range using a given type
393 * @type: the type of variable to use
394 * @val: current value
395 * @min: minimum allowable value
396 * @max: maximum allowable value
397 *
398 * This macro does no typechecking and uses temporary variables of type
399 * 'type' to make all the comparisons.
362 */ 400 */
363#define min_t(type,x,y) \ 401#define clamp_t(type, val, min, max) ({ \
364 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) 402 type __val = (val); \
365#define max_t(type,x,y) \ 403 type __min = (min); \
366 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) 404 type __max = (max); \
405 __val = __val < __min ? __min: __val; \
406 __val > __max ? __max: __val; })
367 407
408/**
409 * clamp_val - return a value clamped to a given range using val's type
410 * @val: current value
411 * @min: minimum allowable value
412 * @max: maximum allowable value
413 *
414 * This macro does no typechecking and uses temporary variables of whatever
415 * type the input argument 'val' is. This is useful when val is an unsigned
416 * type and min and max are literals that will otherwise be assigned a signed
417 * integer type.
418 */
419#define clamp_val(val, min, max) ({ \
420 typeof(val) __val = (val); \
421 typeof(val) __min = (min); \
422 typeof(val) __max = (max); \
423 __val = __val < __min ? __min: __val; \
424 __val > __max ? __max: __val; })
368 425
369/** 426/**
370 * container_of - cast a member of a structure out to the containing structure 427 * container_of - cast a member of a structure out to the containing structure