18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 5fe68f570SHans Petter Selasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * Copyright (c) 2014-2015 François Tigeot 78d59ecb2SHans Petter Selasky * All rights reserved. 88d59ecb2SHans Petter Selasky * 98d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 108d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 118d59ecb2SHans Petter Selasky * are met: 128d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 138d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 148d59ecb2SHans Petter Selasky * disclaimer. 158d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 168d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 178d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 188d59ecb2SHans Petter Selasky * 198d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298d59ecb2SHans Petter Selasky * 308d59ecb2SHans Petter Selasky * $FreeBSD$ 318d59ecb2SHans Petter Selasky */ 328d59ecb2SHans Petter Selasky #ifndef _LINUX_KERNEL_H_ 338d59ecb2SHans Petter Selasky #define _LINUX_KERNEL_H_ 348d59ecb2SHans Petter Selasky 358d59ecb2SHans Petter Selasky #include <sys/cdefs.h> 368d59ecb2SHans Petter Selasky #include <sys/types.h> 378d59ecb2SHans Petter Selasky #include <sys/systm.h> 388d59ecb2SHans Petter Selasky #include <sys/param.h> 398d59ecb2SHans Petter Selasky #include <sys/libkern.h> 408d59ecb2SHans Petter Selasky #include <sys/stat.h> 418d59ecb2SHans Petter Selasky #include <sys/smp.h> 428d59ecb2SHans Petter Selasky #include <sys/stddef.h> 438d59ecb2SHans Petter Selasky #include <sys/syslog.h> 448d59ecb2SHans Petter Selasky 458d59ecb2SHans Petter Selasky #include <linux/bitops.h> 468d59ecb2SHans Petter Selasky #include <linux/compiler.h> 478d59ecb2SHans Petter Selasky #include <linux/errno.h> 488d59ecb2SHans Petter Selasky #include <linux/kthread.h> 498d59ecb2SHans Petter Selasky #include <linux/types.h> 508d59ecb2SHans Petter Selasky #include <linux/jiffies.h> 518d59ecb2SHans Petter Selasky #include <linux/wait.h> 528d59ecb2SHans Petter Selasky #include <linux/log2.h> 538d59ecb2SHans Petter Selasky #include <asm/byteorder.h> 548d59ecb2SHans Petter Selasky 55aef2a67bSHans Petter Selasky #include <machine/stdarg.h> 56aef2a67bSHans Petter Selasky 578d59ecb2SHans Petter Selasky #define KERN_CONT "" 588d59ecb2SHans Petter Selasky #define KERN_EMERG "<0>" 598d59ecb2SHans Petter Selasky #define KERN_ALERT "<1>" 608d59ecb2SHans Petter Selasky #define KERN_CRIT "<2>" 618d59ecb2SHans Petter Selasky #define KERN_ERR "<3>" 628d59ecb2SHans Petter Selasky #define KERN_WARNING "<4>" 638d59ecb2SHans Petter Selasky #define KERN_NOTICE "<5>" 648d59ecb2SHans Petter Selasky #define KERN_INFO "<6>" 658d59ecb2SHans Petter Selasky #define KERN_DEBUG "<7>" 668d59ecb2SHans Petter Selasky 67abb14a54SHans Petter Selasky #define U8_MAX ((u8)~0U) 68abb14a54SHans Petter Selasky #define S8_MAX ((s8)(U8_MAX >> 1)) 69abb14a54SHans Petter Selasky #define S8_MIN ((s8)(-S8_MAX - 1)) 70abb14a54SHans Petter Selasky #define U16_MAX ((u16)~0U) 71abb14a54SHans Petter Selasky #define S16_MAX ((s16)(U16_MAX >> 1)) 72abb14a54SHans Petter Selasky #define S16_MIN ((s16)(-S16_MAX - 1)) 73abb14a54SHans Petter Selasky #define U32_MAX ((u32)~0U) 74abb14a54SHans Petter Selasky #define S32_MAX ((s32)(U32_MAX >> 1)) 75abb14a54SHans Petter Selasky #define S32_MIN ((s32)(-S32_MAX - 1)) 76abb14a54SHans Petter Selasky #define U64_MAX ((u64)~0ULL) 77abb14a54SHans Petter Selasky #define S64_MAX ((s64)(U64_MAX >> 1)) 78abb14a54SHans Petter Selasky #define S64_MIN ((s64)(-S64_MAX - 1)) 79abb14a54SHans Petter Selasky 80abb14a54SHans Petter Selasky #define S8_C(x) x 81abb14a54SHans Petter Selasky #define U8_C(x) x ## U 82abb14a54SHans Petter Selasky #define S16_C(x) x 83abb14a54SHans Petter Selasky #define U16_C(x) x ## U 84abb14a54SHans Petter Selasky #define S32_C(x) x 85abb14a54SHans Petter Selasky #define U32_C(x) x ## U 86abb14a54SHans Petter Selasky #define S64_C(x) x ## LL 87abb14a54SHans Petter Selasky #define U64_C(x) x ## ULL 88abb14a54SHans Petter Selasky 898d59ecb2SHans Petter Selasky #define BUILD_BUG_ON(x) CTASSERT(!(x)) 908d59ecb2SHans Petter Selasky 91abb14a54SHans Petter Selasky #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 92abb14a54SHans Petter Selasky #define BUG_ON(cond) do { \ 93abb14a54SHans Petter Selasky if (cond) { \ 94abb14a54SHans Petter Selasky panic("BUG ON %s failed at %s:%d", \ 95abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 96abb14a54SHans Petter Selasky } \ 97abb14a54SHans Petter Selasky } while (0) 98abb14a54SHans Petter Selasky 99abb14a54SHans Petter Selasky #define WARN_ON(cond) ({ \ 100abb14a54SHans Petter Selasky bool __ret = (cond); \ 101abb14a54SHans Petter Selasky if (__ret) { \ 102abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 103abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 104abb14a54SHans Petter Selasky } \ 105abb14a54SHans Petter Selasky unlikely(__ret); \ 106abb14a54SHans Petter Selasky }) 107abb14a54SHans Petter Selasky 108abb14a54SHans Petter Selasky #define WARN_ON_SMP(cond) WARN_ON(cond) 109abb14a54SHans Petter Selasky 110abb14a54SHans Petter Selasky #define WARN_ON_ONCE(cond) ({ \ 111abb14a54SHans Petter Selasky static bool __warn_on_once; \ 112abb14a54SHans Petter Selasky bool __ret = (cond); \ 113abb14a54SHans Petter Selasky if (__ret && !__warn_on_once) { \ 114abb14a54SHans Petter Selasky __warn_on_once = 1; \ 115abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 116abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 117abb14a54SHans Petter Selasky } \ 118abb14a54SHans Petter Selasky unlikely(__ret); \ 119abb14a54SHans Petter Selasky }) 1208d59ecb2SHans Petter Selasky 1218d59ecb2SHans Petter Selasky #undef ALIGN 1228d59ecb2SHans Petter Selasky #define ALIGN(x, y) roundup2((x), (y)) 1238d59ecb2SHans Petter Selasky #undef PTR_ALIGN 1248d59ecb2SHans Petter Selasky #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 1258d59ecb2SHans Petter Selasky #define DIV_ROUND_UP(x, n) howmany(x, n) 1268d59ecb2SHans Petter Selasky #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 1278d59ecb2SHans Petter Selasky #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 1288d59ecb2SHans Petter Selasky 129aef2a67bSHans Petter Selasky #define printk(...) printf(__VA_ARGS__) 130aef2a67bSHans Petter Selasky #define vprintk(f, a) vprintf(f, a) 131aef2a67bSHans Petter Selasky 132aef2a67bSHans Petter Selasky struct va_format { 133aef2a67bSHans Petter Selasky const char *fmt; 134aef2a67bSHans Petter Selasky va_list *va; 135aef2a67bSHans Petter Selasky }; 136aef2a67bSHans Petter Selasky 137aef2a67bSHans Petter Selasky static inline int 138aef2a67bSHans Petter Selasky vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 139aef2a67bSHans Petter Selasky { 140aef2a67bSHans Petter Selasky ssize_t ssize = size; 141aef2a67bSHans Petter Selasky int i; 142aef2a67bSHans Petter Selasky 143aef2a67bSHans Petter Selasky i = vsnprintf(buf, size, fmt, args); 144aef2a67bSHans Petter Selasky 145aef2a67bSHans Petter Selasky return ((i >= ssize) ? (ssize - 1) : i); 146aef2a67bSHans Petter Selasky } 147aef2a67bSHans Petter Selasky 148aef2a67bSHans Petter Selasky static inline int 149aef2a67bSHans Petter Selasky scnprintf(char *buf, size_t size, const char *fmt, ...) 150aef2a67bSHans Petter Selasky { 151aef2a67bSHans Petter Selasky va_list args; 152aef2a67bSHans Petter Selasky int i; 153aef2a67bSHans Petter Selasky 154aef2a67bSHans Petter Selasky va_start(args, fmt); 155aef2a67bSHans Petter Selasky i = vscnprintf(buf, size, fmt, args); 156aef2a67bSHans Petter Selasky va_end(args); 157aef2a67bSHans Petter Selasky 158aef2a67bSHans Petter Selasky return (i); 159aef2a67bSHans Petter Selasky } 1608d59ecb2SHans Petter Selasky 1618d59ecb2SHans Petter Selasky /* 1628d59ecb2SHans Petter Selasky * The "pr_debug()" and "pr_devel()" macros should produce zero code 1638d59ecb2SHans Petter Selasky * unless DEBUG is defined: 1648d59ecb2SHans Petter Selasky */ 1658d59ecb2SHans Petter Selasky #ifdef DEBUG 1668d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 1678d59ecb2SHans Petter Selasky log(LOG_DEBUG, fmt, ##__VA_ARGS__) 1688d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 1698d59ecb2SHans Petter Selasky log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 1708d59ecb2SHans Petter Selasky #else 1718d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 1728d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 1738d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 1748d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 1758d59ecb2SHans Petter Selasky #endif 1768d59ecb2SHans Petter Selasky 1778d59ecb2SHans Petter Selasky #ifndef pr_fmt 1788d59ecb2SHans Petter Selasky #define pr_fmt(fmt) fmt 1798d59ecb2SHans Petter Selasky #endif 1808d59ecb2SHans Petter Selasky 1818d59ecb2SHans Petter Selasky /* 1828d59ecb2SHans Petter Selasky * Print a one-time message (analogous to WARN_ONCE() et al): 1838d59ecb2SHans Petter Selasky */ 1848d59ecb2SHans Petter Selasky #define printk_once(...) do { \ 1858d59ecb2SHans Petter Selasky static bool __print_once; \ 1868d59ecb2SHans Petter Selasky \ 1878d59ecb2SHans Petter Selasky if (!__print_once) { \ 1888d59ecb2SHans Petter Selasky __print_once = true; \ 1898d59ecb2SHans Petter Selasky printk(__VA_ARGS__); \ 1908d59ecb2SHans Petter Selasky } \ 1918d59ecb2SHans Petter Selasky } while (0) 1928d59ecb2SHans Petter Selasky 1938d59ecb2SHans Petter Selasky /* 1948d59ecb2SHans Petter Selasky * Log a one-time message (analogous to WARN_ONCE() et al): 1958d59ecb2SHans Petter Selasky */ 1968d59ecb2SHans Petter Selasky #define log_once(level,...) do { \ 1978d59ecb2SHans Petter Selasky static bool __log_once; \ 1988d59ecb2SHans Petter Selasky \ 199abb14a54SHans Petter Selasky if (unlikely(!__log_once)) { \ 2008d59ecb2SHans Petter Selasky __log_once = true; \ 2018d59ecb2SHans Petter Selasky log(level, __VA_ARGS__); \ 2028d59ecb2SHans Petter Selasky } \ 2038d59ecb2SHans Petter Selasky } while (0) 2048d59ecb2SHans Petter Selasky 2058d59ecb2SHans Petter Selasky #define pr_emerg(fmt, ...) \ 2068d59ecb2SHans Petter Selasky log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 2078d59ecb2SHans Petter Selasky #define pr_alert(fmt, ...) \ 2088d59ecb2SHans Petter Selasky log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 2098d59ecb2SHans Petter Selasky #define pr_crit(fmt, ...) \ 2108d59ecb2SHans Petter Selasky log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 2118d59ecb2SHans Petter Selasky #define pr_err(fmt, ...) \ 2128d59ecb2SHans Petter Selasky log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 2138d59ecb2SHans Petter Selasky #define pr_warning(fmt, ...) \ 2148d59ecb2SHans Petter Selasky log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 215abb14a54SHans Petter Selasky #define pr_warn(...) \ 216abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__) 217abb14a54SHans Petter Selasky #define pr_warn_once(fmt, ...) \ 218abb14a54SHans Petter Selasky log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 2198d59ecb2SHans Petter Selasky #define pr_notice(fmt, ...) \ 2208d59ecb2SHans Petter Selasky log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 2218d59ecb2SHans Petter Selasky #define pr_info(fmt, ...) \ 2228d59ecb2SHans Petter Selasky log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2238d59ecb2SHans Petter Selasky #define pr_info_once(fmt, ...) \ 2248d59ecb2SHans Petter Selasky log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2258d59ecb2SHans Petter Selasky #define pr_cont(fmt, ...) \ 2268d59ecb2SHans Petter Selasky printk(KERN_CONT fmt, ##__VA_ARGS__) 2278d59ecb2SHans Petter Selasky 2288d59ecb2SHans Petter Selasky #ifndef WARN 229abb14a54SHans Petter Selasky #define WARN(condition, ...) ({ \ 230abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 2318d59ecb2SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 232abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__); \ 233abb14a54SHans Petter Selasky unlikely(__ret_warn_on); \ 234abb14a54SHans Petter Selasky }) 235abb14a54SHans Petter Selasky #endif 236abb14a54SHans Petter Selasky 237abb14a54SHans Petter Selasky #ifndef WARN_ONCE 238abb14a54SHans Petter Selasky #define WARN_ONCE(condition, ...) ({ \ 239abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 240abb14a54SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 241abb14a54SHans Petter Selasky pr_warn_once(__VA_ARGS__); \ 2428d59ecb2SHans Petter Selasky unlikely(__ret_warn_on); \ 2438d59ecb2SHans Petter Selasky }) 2448d59ecb2SHans Petter Selasky #endif 2458d59ecb2SHans Petter Selasky 2468d59ecb2SHans Petter Selasky #define container_of(ptr, type, member) \ 2478d59ecb2SHans Petter Selasky ({ \ 248*ddad2785SHans Petter Selasky const __typeof(((type *)0)->member) *__p = (ptr); \ 249*ddad2785SHans Petter Selasky (type *)((uintptr_t)__p - offsetof(type, member)); \ 2508d59ecb2SHans Petter Selasky }) 2518d59ecb2SHans Petter Selasky 2528d59ecb2SHans Petter Selasky #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 2538d59ecb2SHans Petter Selasky 254fe68f570SHans Petter Selasky #define simple_strtoul(...) strtoul(__VA_ARGS__) 255fe68f570SHans Petter Selasky #define simple_strtol(...) strtol(__VA_ARGS__) 256fe68f570SHans Petter Selasky #define kstrtol(a,b,c) ({*(c) = strtol(a,0,b); 0;}) 257fe68f570SHans Petter Selasky #define kstrtoint(a,b,c) ({*(c) = strtol(a,0,b); 0;}) 258510ebed7SHans Petter Selasky #define kstrtouint(a,b,c) ({*(c) = strtol(a,0,b); 0;}) 2598d59ecb2SHans Petter Selasky 2608d59ecb2SHans Petter Selasky #define min(x, y) ((x) < (y) ? (x) : (y)) 2618d59ecb2SHans Petter Selasky #define max(x, y) ((x) > (y) ? (x) : (y)) 2628d59ecb2SHans Petter Selasky 2638d59ecb2SHans Petter Selasky #define min3(a, b, c) min(a, min(b,c)) 2648d59ecb2SHans Petter Selasky #define max3(a, b, c) max(a, max(b,c)) 2658d59ecb2SHans Petter Selasky 266abb14a54SHans Petter Selasky #define min_t(type, x, y) ({ \ 267abb14a54SHans Petter Selasky type __min1 = (x); \ 268abb14a54SHans Petter Selasky type __min2 = (y); \ 269abb14a54SHans Petter Selasky __min1 < __min2 ? __min1 : __min2; }) 270abb14a54SHans Petter Selasky 271abb14a54SHans Petter Selasky #define max_t(type, x, y) ({ \ 272abb14a54SHans Petter Selasky type __max1 = (x); \ 273abb14a54SHans Petter Selasky type __max2 = (y); \ 274abb14a54SHans Petter Selasky __max1 > __max2 ? __max1 : __max2; }) 2758d59ecb2SHans Petter Selasky 2768d59ecb2SHans Petter Selasky #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 2778d59ecb2SHans Petter Selasky #define clamp(x, lo, hi) min( max(x,lo), hi) 278abb14a54SHans Petter Selasky #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 2798d59ecb2SHans Petter Selasky 2808d59ecb2SHans Petter Selasky /* 2818d59ecb2SHans Petter Selasky * This looks more complex than it should be. But we need to 2828d59ecb2SHans Petter Selasky * get the type for the ~ right in round_down (it needs to be 2838d59ecb2SHans Petter Selasky * as wide as the result!), and we want to evaluate the macro 2848d59ecb2SHans Petter Selasky * arguments just once each. 2858d59ecb2SHans Petter Selasky */ 2868d59ecb2SHans Petter Selasky #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 2878d59ecb2SHans Petter Selasky #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 2888d59ecb2SHans Petter Selasky #define round_down(x, y) ((x) & ~__round_mask(x, y)) 2898d59ecb2SHans Petter Selasky 290fe68f570SHans Petter Selasky #define smp_processor_id() PCPU_GET(cpuid) 2918d59ecb2SHans Petter Selasky #define num_possible_cpus() mp_ncpus 2928d59ecb2SHans Petter Selasky #define num_online_cpus() mp_ncpus 29303219fbaSHans Petter Selasky 29403219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__) 29503219fbaSHans Petter Selasky extern bool linux_cpu_has_clflush; 29603219fbaSHans Petter Selasky #define cpu_has_clflush linux_cpu_has_clflush 29703219fbaSHans Petter Selasky #endif 2988d59ecb2SHans Petter Selasky 2998d59ecb2SHans Petter Selasky typedef struct pm_message { 3008d59ecb2SHans Petter Selasky int event; 3018d59ecb2SHans Petter Selasky } pm_message_t; 3028d59ecb2SHans Petter Selasky 3038d59ecb2SHans Petter Selasky /* Swap values of a and b */ 3048d59ecb2SHans Petter Selasky #define swap(a, b) do { \ 3058d59ecb2SHans Petter Selasky typeof(a) _swap_tmp = a; \ 3068d59ecb2SHans Petter Selasky a = b; \ 3078d59ecb2SHans Petter Selasky b = _swap_tmp; \ 3088d59ecb2SHans Petter Selasky } while (0) 3098d59ecb2SHans Petter Selasky 3108d59ecb2SHans Petter Selasky #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 3118d59ecb2SHans Petter Selasky 312abb14a54SHans Petter Selasky #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 313abb14a54SHans Petter Selasky __typeof(divisor) __d = (divisor); \ 314abb14a54SHans Petter Selasky unsigned long long __ret = (x) + (__d) / 2; \ 315abb14a54SHans Petter Selasky __ret /= __d; \ 316abb14a54SHans Petter Selasky __ret; \ 317abb14a54SHans Petter Selasky }) 318abb14a54SHans Petter Selasky 3198d59ecb2SHans Petter Selasky static inline uintmax_t 3208d59ecb2SHans Petter Selasky mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 3218d59ecb2SHans Petter Selasky { 3228d59ecb2SHans Petter Selasky uintmax_t q = (x / divisor); 3238d59ecb2SHans Petter Selasky uintmax_t r = (x % divisor); 3248d59ecb2SHans Petter Selasky 3258d59ecb2SHans Petter Selasky return ((q * multiplier) + ((r * multiplier) / divisor)); 3268d59ecb2SHans Petter Selasky } 3278d59ecb2SHans Petter Selasky 3288d59ecb2SHans Petter Selasky static inline int64_t 3298d59ecb2SHans Petter Selasky abs64(int64_t x) 3308d59ecb2SHans Petter Selasky { 3318d59ecb2SHans Petter Selasky return (x < 0 ? -x : x); 3328d59ecb2SHans Petter Selasky } 3338d59ecb2SHans Petter Selasky 3348d59ecb2SHans Petter Selasky #endif /* _LINUX_KERNEL_H_ */ 335