#ifndef FUNCGEN_UTIL_H_ #define FUNCGEN_UTIL_H_ #include #include #include #include /* Some of these macros are derived from the Linux Kernel sources. */ #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #undef offsetof #define offsetof(type, member) ((size_t)&((type *)0)->member) #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define abs(x) ((x) >= 0 ? (x) : -(x)) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) /* Memory barrier. * The CPU doesn't have runtime reordering, so we just * need a compiler memory clobber. */ #define mb() __asm__ __volatile__("" : : : "memory") /* Function annotation. Warn if the return value is unused. */ #define WARN_UNUSED_RES __attribute__((__warn_unused_result__)) /* Convert something indirectly to a string */ #define __stringify(x) #x #define stringify(x) __stringify(x) /* Progmem pointer annotation. */ #define PROGPTR /* progmem pointer */ /* Assertions */ void panic(const char PROGPTR *msg) __attribute__((noreturn)); #define BUILD_BUG_ON(x) ((void)sizeof(char[1 - 2 * !!(x)])) #define BUG_ON(x) \ do { \ if (unlikely(x)) \ panic(PSTR(__FILE__ \ stringify(__LINE__))); \ } while (0) /* Delay */ void mdelay(uint16_t msecs); void udelay(uint16_t usecs); typedef _Bool bool; static inline void irq_disable(void) { cli(); mb(); } static inline void irq_enable(void) { mb(); sei(); } static inline uint8_t WARN_UNUSED_RES irq_disable_save(void) { uint8_t sreg = SREG; cli(); mb(); return sreg; } static inline void irq_restore(uint8_t sreg_flags) { mb(); SREG = sreg_flags; } /* Jiffies timing helpers derived from the Linux Kernel sources. * These inlines deal with timer wrapping correctly. * * time_after(a, b) returns true if the time a is after time b. * * Do this with "<0" and ">=0" to only test the sign of the result. A * good compiler would generate better code (and a really good compiler * wouldn't care). Gcc is currently neither. */ #define time_after(a, b) ((int16_t)(b) - (int16_t)(a) < 0) #define time_before(a, b) time_after(b, a) #undef __naked #define __naked __attribute__((__naked__)) #undef __used #define __used __attribute__((__used__)) #endif /* FUNCGEN_UTIL_H_ */