1 /* 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Western Digital Corporation or its affiliates. 5 * 6 * Authors: 7 * Anup Patel <anup.patel@wdc.com> 8 */ 9 10 #ifndef __SBI_BITS_H__ 11 #define __SBI_BITS_H__ 12 13 #define likely(x) __builtin_expect((x), 1) 14 #define unlikely(x) __builtin_expect((x), 0) 15 16 #define ROUNDUP(a, b) ((((a)-1) / (b) + 1) * (b)) 17 #define ROUNDDOWN(a, b) ((a) / (b) * (b)) 18 19 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 20 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 21 #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi) 22 23 #define EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1))) 24 #define INSERT_FIELD(val, which, fieldval) \ 25 (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1)))) 26 27 #define STR(x) XSTR(x) 28 #define XSTR(x) #x 29 30 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) 31 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) 32 #endif 33