can-calc-bit-timing: import helper macros from kernel
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>pull/17/head
parent
9ae9e7da02
commit
95f62518d1
|
|
@ -26,38 +26,70 @@
|
||||||
#include <linux/types.h>
|
#include <linux/types.h>
|
||||||
#include <linux/can/netlink.h>
|
#include <linux/can/netlink.h>
|
||||||
|
|
||||||
/* seems not to be defined in errno.h */
|
/* imported from kernel */
|
||||||
#ifndef ENOTSUPP
|
|
||||||
#define ENOTSUPP 524 /* Operation is not supported */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* useful defines */
|
/**
|
||||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
* 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 do_div(a,b) a = (a) / (b)
|
#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); \
|
* min()/max()/clamp() macros that also do
|
||||||
(__x < 0) ? -__x : __x; \
|
* 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
|
* clamp - return a value clamped to a given range with strict typechecking
|
||||||
* @val: current value
|
* @val: current value
|
||||||
* @min: minimum allowable value
|
* @lo: lowest allowable value
|
||||||
* @max: maximum 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.
|
* same type as val. See the unnecessary pointer comparisons.
|
||||||
*/
|
*/
|
||||||
#define clamp(val, min, max) ({ \
|
#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
|
||||||
typeof(val) __val = (val); \
|
|
||||||
typeof(min) __min = (min); \
|
# define do_div(n,base) ({ \
|
||||||
typeof(max) __max = (max); \
|
uint32_t __base = (base); \
|
||||||
(void) (&__val == &__min); \
|
uint32_t __rem; \
|
||||||
(void) (&__val == &__max); \
|
__rem = ((uint64_t)(n)) % __base; \
|
||||||
__val = __val < __min ? __min: __val; \
|
(n) = ((uint64_t)(n)) / __base; \
|
||||||
__val > __max ? __max: __val; })
|
__rem; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/* */
|
||||||
|
|
||||||
|
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||||
|
|
||||||
/* we don't want to see these prints */
|
/* we don't want to see these prints */
|
||||||
#define dev_err(dev, format, arg...) do { } while (0)
|
#define dev_err(dev, format, arg...) do { } while (0)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue