Angel Pons | ebda03e | 2020-04-02 23:48:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Martin Roth | f4d015d | 2016-01-12 16:21:45 -0700 | [diff] [blame] | 2 | |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 3 | #ifndef COMMONLIB_HELPERS_H |
| 4 | #define COMMONLIB_HELPERS_H |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 5 | |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 6 | /* This file is for helpers for both coreboot firmware and its utilities. Most |
| 7 | of this has moved into <commonlib/bsd/helpers.h> now, this wrapper is just |
| 8 | for the stuff that nobody bothered to confirm BSD-licensability of yet. */ |
Jonathan Neuschäfer | 7bd4715 | 2017-10-30 19:28:52 +0100 | [diff] [blame] | 9 | |
Julius Werner | 98eeb96 | 2019-12-11 15:47:42 -0800 | [diff] [blame] | 10 | #include <commonlib/bsd/helpers.h> |
Julius Werner | d371cf3 | 2015-05-22 18:09:48 -0700 | [diff] [blame] | 11 | |
Arthur Heymans | 29fc9bb | 2016-09-02 23:14:54 +0200 | [diff] [blame] | 12 | /* |
| 13 | * Divide positive or negative dividend by positive divisor and round |
| 14 | * to closest integer. Result is undefined for negative divisors and |
| 15 | * for negative dividends if the divisor variable type is unsigned. |
| 16 | */ |
Julius Werner | d371cf3 | 2015-05-22 18:09:48 -0700 | [diff] [blame] | 17 | #define DIV_ROUND_CLOSEST(x, divisor)({ \ |
| 18 | __typeof__(x) _div_local_x = (x); \ |
| 19 | __typeof__(divisor) _div_local_d = (divisor); \ |
| 20 | (((__typeof__(x))-1) > 0 || \ |
| 21 | ((__typeof__(divisor))-1) > 0 || (_div_local_x) > 0) ? \ |
| 22 | ((_div_local_x + (_div_local_d / 2)) / _div_local_d) : \ |
| 23 | ((_div_local_x - (_div_local_d / 2)) / _div_local_d); \ |
| 24 | }) |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 25 | |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 26 | /** |
| 27 | * container_of - cast a member of a structure out to the containing structure |
| 28 | * @param ptr: the pointer to the member. |
| 29 | * @param type: the type of the container struct this is embedded in. |
| 30 | * @param member: the name of the member within the struct. |
| 31 | * |
| 32 | */ |
| 33 | #define container_of(ptr, type, member) ({ \ |
Lee Leahy | d5a2a29 | 2017-03-10 10:35:43 -0800 | [diff] [blame] | 34 | const __typeof__(((type *)0)->member) *__mptr = (ptr); \ |
| 35 | (type *)((char *)__mptr - offsetof(type, member)); }) |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 36 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 37 | #ifndef __unused |
| 38 | #define __unused __attribute__((unused)) |
| 39 | #endif |
| 40 | |
Tim Wawrzynczak | eef992d | 2019-10-27 14:02:10 -0600 | [diff] [blame] | 41 | #ifndef alloca |
| 42 | #define alloca(x) __builtin_alloca(x) |
| 43 | #endif |
| 44 | |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 45 | #endif /* COMMONLIB_HELPERS_H */ |