From: Hauke Mehrtens Date: Mon, 5 Apr 2010 21:02:38 +0000 (+0200) Subject: compat: Backport clamp X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=97da2ad6bfc2855bb6700f1c8ed000c332b9ce7d;p=openwrt%2Fstaging%2Fblogic.git compat: Backport clamp This is needed by the ath5k driver. Signed-off-by: Hauke Mehrtens --- diff --git a/include/linux/compat-2.6.26.h b/include/linux/compat-2.6.26.h index 2bee30eca755..825117742ba8 100644 --- a/include/linux/compat-2.6.26.h +++ b/include/linux/compat-2.6.26.h @@ -47,6 +47,24 @@ extern int dev_set_name(struct device *dev, const char *name, ...) __attribute__((format(printf, 2, 3))); +/** + * clamp - return a value clamped to a given range with strict typechecking + * @val: current value + * @min: minimum allowable value + * @max: maximum allowable value + * + * This macro does strict typechecking of min/max to make sure they are of the + * same type as val. See the unnecessary pointer comparisons. + */ +#define clamp(val, min, max) ({ \ + typeof(val) __val = (val); \ + typeof(min) __min = (min); \ + typeof(max) __max = (max); \ + (void) (&__val == &__min); \ + (void) (&__val == &__max); \ + __val = __val < __min ? __min: __val; \ + __val > __max ? __max: __val; }) + /** * clamp_t - return a value clamped to a given range using a given type * @type: the type of variable to use