]> git.openfabrics.org - compat-rdma/compat.git/commitdiff
compat: Backport clamp
authorHauke Mehrtens <hauke@hauke-m.de>
Mon, 5 Apr 2010 21:02:38 +0000 (23:02 +0200)
committerLuis R. Rodriguez <lrodriguez@atheros.com>
Mon, 5 Apr 2010 21:22:30 +0000 (14:22 -0700)
This is needed by the ath5k driver.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
include/linux/compat-2.6.26.h

index 2bee30eca755bcf5463e3f973e513597dfcebda2..825117742ba844fa68a3c354b96439680f87bbbb 100644 (file)
 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