From 189ebb4c7d5134296ce9e2bebe304a795b38ef89 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 5 Jun 2018 21:25:37 -0400 Subject: [PATCH] chelper: Add compiler.h header Add the compiler.h header file to the chelper code - this adds a number of useful gcc definitions. Signed-off-by: Kevin O'Connor --- klippy/chelper/compiler.h | 46 +++++++++++++++++++++++++++++++++++ klippy/chelper/pyhelper.h | 3 --- klippy/chelper/stepcompress.c | 7 +++--- 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 klippy/chelper/compiler.h diff --git a/klippy/chelper/compiler.h b/klippy/chelper/compiler.h new file mode 100644 index 00000000..ab16efbd --- /dev/null +++ b/klippy/chelper/compiler.h @@ -0,0 +1,46 @@ +#ifndef __COMPILER_H +#define __COMPILER_H +// Low level definitions for C languange and gcc compiler. + +#define barrier() __asm__ __volatile__("": : :"memory") + +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + +#define noinline __attribute__((noinline)) +#ifndef __always_inline +#define __always_inline inline __attribute__((always_inline)) +#endif +#define __visible __attribute__((externally_visible)) +#define __noreturn __attribute__((noreturn)) + +#define PACKED __attribute__((packed)) +#ifndef __aligned +#define __aligned(x) __attribute__((aligned(x))) +#endif +#ifndef __section +#define __section(S) __attribute__((section(S))) +#endif + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) +#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) +#define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1)) + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +#define __stringify_1(x) #x +#define __stringify(x) __stringify_1(x) + +#define ___PASTE(a,b) a##b +#define __PASTE(a,b) ___PASTE(a,b) + +#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) +#define DIV_ROUND_CLOSEST(x, divisor)({ \ + typeof(divisor) __divisor = divisor; \ + (((x) + ((__divisor) / 2)) / (__divisor)); \ + }) + +#endif // compiler.h diff --git a/klippy/chelper/pyhelper.h b/klippy/chelper/pyhelper.h index d564c78b..1042214b 100644 --- a/klippy/chelper/pyhelper.h +++ b/klippy/chelper/pyhelper.h @@ -1,9 +1,6 @@ #ifndef PYHELPER_H #define PYHELPER_H -#define likely(x) __builtin_expect(!!(x), 1) -#define unlikely(x) __builtin_expect(!!(x), 0) - double get_monotonic(void); struct timespec fill_time(double time); void set_python_logging_callback(void (*func)(const char *)); diff --git a/klippy/chelper/stepcompress.c b/klippy/chelper/stepcompress.c index 7bb72b96..c65bb431 100644 --- a/klippy/chelper/stepcompress.c +++ b/klippy/chelper/stepcompress.c @@ -20,6 +20,7 @@ #include // fprintf #include // malloc #include // memset +#include "compiler.h" // DIV_ROUND_UP #include "pyhelper.h" // errorf #include "serialqueue.h" // struct queue_message @@ -44,12 +45,10 @@ struct stepcompress { * Step compression ****************************************************************/ -#define DIV_UP(n,d) (((n) + (d) - 1) / (d)) - static inline int32_t idiv_up(int32_t n, int32_t d) { - return (n>=0) ? DIV_UP(n,d) : (n/d); + return (n>=0) ? DIV_ROUND_UP(n,d) : (n/d); } static inline int32_t @@ -116,7 +115,7 @@ compress_bisect_add(struct stepcompress *sc) int32_t nextaddfactor = nextcount*(nextcount-1)/2; int32_t c = add*nextaddfactor; if (nextmininterval*nextcount < nextpoint.minp - c) - nextmininterval = DIV_UP(nextpoint.minp - c, nextcount); + nextmininterval = DIV_ROUND_UP(nextpoint.minp - c, nextcount); if (nextmaxinterval*nextcount > nextpoint.maxp - c) nextmaxinterval = (nextpoint.maxp - c) / nextcount; if (nextmininterval > nextmaxinterval)