]> rtime.felk.cvut.cz Git - can-utils.git/commitdiff
can-calc-bit-timing: import helper macros from kernel
authorMarc Kleine-Budde <mkl@pengutronix.de>
Wed, 13 Apr 2016 09:16:55 +0000 (11:16 +0200)
committerMarc Kleine-Budde <mkl@pengutronix.de>
Wed, 13 Apr 2016 11:13:01 +0000 (13:13 +0200)
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
can-calc-bit-timing.c

index 52fb83870bd14c51dc4bd327d6430b1ffe61f13d..25a892391ed18e16444d5c75674c63feded6153b 100644 (file)
 #include <linux/types.h>
 #include <linux/can/netlink.h>
 
-/* seems not to be defined in errno.h */
-#ifndef ENOTSUPP
-#define ENOTSUPP       524     /* Operation is not supported */
-#endif
+/* imported from kernel */
 
-/* useful defines */
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
-
-#define do_div(a,b) a = (a) / (b)
+/**
+ * abs - return absolute value of an argument
+ * @x: the value.  If it is unsigned type, it is converted to signed type first.
+ *     char is treated as if it was signed (regardless of whether it really is)
+ *     but the macro's return type is preserved as char.
+ *
+ * Return: an absolute value of x.
+ */
+#define abs(x) __abs_choose_expr(x, long long,                         \
+               __abs_choose_expr(x, long,                              \
+               __abs_choose_expr(x, int,                               \
+               __abs_choose_expr(x, short,                             \
+               __abs_choose_expr(x, char,                              \
+               __builtin_choose_expr(                                  \
+                       __builtin_types_compatible_p(typeof(x), char),  \
+                       (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
+                       ((void)0)))))))
+
+#define __abs_choose_expr(x, type, other) __builtin_choose_expr(       \
+       __builtin_types_compatible_p(typeof(x),   signed type) ||       \
+       __builtin_types_compatible_p(typeof(x), unsigned type),         \
+       ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
 
-#define abs(x) ({                              \
-               long __x = (x);                 \
-               (__x < 0) ? -__x : __x;         \
-       })
+/*
+ * min()/max()/clamp() macros that also do
+ * strict type-checking.. See the
+ * "unnecessary" pointer comparison.
+ */
+#define min(x, y) ({                           \
+       typeof(x) _min1 = (x);                  \
+       typeof(y) _min2 = (y);                  \
+       (void) (&_min1 == &_min2);              \
+       _min1 < _min2 ? _min1 : _min2; })
+
+#define max(x, y) ({                           \
+       typeof(x) _max1 = (x);                  \
+       typeof(y) _max2 = (y);                  \
+       (void) (&_max1 == &_max2);              \
+       _max1 > _max2 ? _max1 : _max2; })
 
 /**
  * clamp - return a value clamped to a given range with strict typechecking
  * @val: current value
- * @min: minimum allowable value
- * @max: maximum allowable value
+ * @lo: lowest allowable value
+ * @hi: highest allowable value
  *
- * This macro does strict typechecking of min/max to make sure they are of the
+ * This macro does strict typechecking of lo/hi 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; })
+#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
+
+# define do_div(n,base) ({                                     \
+       uint32_t __base = (base);                               \
+       uint32_t __rem;                                         \
+       __rem = ((uint64_t)(n)) % __base;                       \
+       (n) = ((uint64_t)(n)) / __base;                         \
+       __rem;                                                  \
+ })
+
+/* */
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 
 /* we don't want to see these prints */
 #define dev_err(dev, format, arg...)   do { } while (0)