1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #ifndef UTIL_H 6 #define UTIL_H 7 8 #include <compiler.h> 9 #include <inttypes.h> 10 11 #define SIZE_4K UINTPTR_C(0x1000) 12 #define SIZE_1M UINTPTR_C(0x100000) 13 #define SIZE_2M UINTPTR_C(0x200000) 14 #define SIZE_4M UINTPTR_C(0x400000) 15 #define SIZE_8M UINTPTR_C(0x800000) 16 #define SIZE_2G UINTPTR_C(0x80000000) 17 18 #ifndef MAX 19 #ifndef __ASSEMBLER__ 20 #define MAX(a, b) \ 21 (__extension__({ __typeof__(a) _a = (a); \ 22 __typeof__(b) _b = (b); \ 23 _a > _b ? _a : _b; })) 24 25 #define MIN(a, b) \ 26 (__extension__({ __typeof__(a) _a = (a); \ 27 __typeof__(b) _b = (b); \ 28 _a < _b ? _a : _b; })) 29 #else 30 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 31 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 32 #endif 33 #endif 34 35 /* 36 * In some particular conditions MAX and MIN macros fail to 37 * build from C source file implmentation. In such case one 38 * need to use MAX_UNSAFE/MIN_UNSAFE instead. 39 */ 40 #define MAX_UNSAFE(a, b) (((a) > (b)) ? (a) : (b)) 41 #define MIN_UNSAFE(a, b) (((a) < (b)) ? (a) : (b)) 42 43 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 44 45 #ifndef __ASSEMBLER__ 46 /* Round up the even multiple of size, size has to be a multiple of 2 */ 47 #define ROUNDUP(v, size) (((v) + ((__typeof__(v))(size) - 1)) & \ 48 ~((__typeof__(v))(size) - 1)) 49 50 #define ROUNDUP_OVERFLOW(v, size, res) (__extension__({ \ 51 typeof(*(res)) __roundup_tmp = 0; \ 52 typeof(v) __roundup_mask = (typeof(v))(size) - 1; \ 53 \ 54 ADD_OVERFLOW((v), __roundup_mask, &__roundup_tmp) ? 1 : \ 55 (void)(*(res) = __roundup_tmp & ~__roundup_mask), 0; \ 56 })) 57 58 /* Round down the even multiple of size, size has to be a multiple of 2 */ 59 #define ROUNDDOWN(v, size) ((v) & ~((__typeof__(v))(size) - 1)) 60 61 /* Unsigned integer division with nearest rounding variant */ 62 #define UDIV_ROUND_NEAREST(x, y) \ 63 (__extension__ ({ __typeof__(x) _x = (x); \ 64 __typeof__(y) _y = (y); \ 65 (_x + (_y / 2)) / _y; })) 66 #else 67 #define ROUNDUP(x, y) ((((x) + (y) - 1) / (y)) * (y)) 68 #define ROUNDDOWN(x, y) (((x) / (y)) * (y)) 69 #define UDIV_ROUND_NEAREST(x, y) (((x) + ((y) / 2)) / (y)) 70 #endif 71 72 /* x has to be of an unsigned type */ 73 #define IS_POWER_OF_TWO(x) (((x) != 0) && (((x) & (~(x) + 1)) == (x))) 74 75 #define ALIGNMENT_IS_OK(p, type) \ 76 (((uintptr_t)(p) & (__alignof__(type) - 1)) == 0) 77 78 #define TO_STR(x) _TO_STR(x) 79 #define _TO_STR(x) #x 80 81 #define CONCAT(x, y) _CONCAT(x, y) 82 #define _CONCAT(x, y) x##y 83 84 #define container_of(ptr, type, member) \ 85 (__extension__({ \ 86 const typeof(((type *)0)->member) *__ptr = (ptr); \ 87 (type *)((unsigned long)(__ptr) - offsetof(type, member)); \ 88 })) 89 90 #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member) 91 92 #ifdef __ASSEMBLER__ 93 #define BIT32(nr) (1 << (nr)) 94 #define BIT64(nr) (1 << (nr)) 95 #define SHIFT_U32(v, shift) ((v) << (shift)) 96 #define SHIFT_U64(v, shift) ((v) << (shift)) 97 #else 98 #define BIT32(nr) (UINT32_C(1) << (nr)) 99 #define BIT64(nr) (UINT64_C(1) << (nr)) 100 #define SHIFT_U32(v, shift) ((uint32_t)(v) << (shift)) 101 #define SHIFT_U64(v, shift) ((uint64_t)(v) << (shift)) 102 #endif 103 #define BIT(nr) BIT32(nr) 104 105 /* 106 * Create a contiguous bitmask starting at bit position @l and ending at 107 * position @h. For example 108 * GENMASK_64(39, 21) gives us the 64bit vector 0x000000ffffe00000. 109 */ 110 #define GENMASK_32(h, l) \ 111 (((~UINT32_C(0)) << (l)) & (~UINT32_C(0) >> (32 - 1 - (h)))) 112 113 #define GENMASK_64(h, l) \ 114 (((~UINT64_C(0)) << (l)) & (~UINT64_C(0) >> (64 - 1 - (h)))) 115 116 /* 117 * Checking overflow for addition, subtraction and multiplication. Result 118 * of operation is stored in res which is a pointer to some kind of 119 * integer. 120 * 121 * The macros return true if an overflow occurred and *res is undefined. 122 */ 123 #define ADD_OVERFLOW(a, b, res) __compiler_add_overflow((a), (b), (res)) 124 #define SUB_OVERFLOW(a, b, res) __compiler_sub_overflow((a), (b), (res)) 125 #define MUL_OVERFLOW(a, b, res) __compiler_mul_overflow((a), (b), (res)) 126 127 /* Return a signed +1, 0 or -1 value based on data comparison */ 128 #define CMP_TRILEAN(a, b) \ 129 (__extension__({ \ 130 __typeof__(a) _a = (a); \ 131 __typeof__(b) _b = (b); \ 132 \ 133 _a > _b ? 1 : _a < _b ? -1 : 0; \ 134 })) 135 136 #ifndef __ASSEMBLER__ 137 static inline uint64_t reg_pair_to_64(uint32_t reg0, uint32_t reg1) 138 { 139 return (uint64_t)reg0 << 32 | reg1; 140 } 141 142 static inline void reg_pair_from_64(uint64_t val, uint32_t *reg0, 143 uint32_t *reg1) 144 { 145 *reg0 = val >> 32; 146 *reg1 = val; 147 } 148 #endif 149 150 #endif /*UTIL_H*/ 151