1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*! @file
8 @brief This file defines bit-field operations macros.
9 */
10 
11 #ifndef _CC_BITOPS_H_
12 #define _CC_BITOPS_H_
13 
14 
15 /*! Defintion of number of 32bit maximum value. */
16 #define CC_32BIT_MAX_VALUE  (0xFFFFFFFFUL)
17 
18 /*! Definition for bitmask */
19 #define BITMASK(mask_size) (((mask_size) < 32) ?    \
20     ((1UL << (mask_size)) - 1) : 0xFFFFFFFFUL)
21 /*! Definition for bitmask in a given offset. */
22 #define BITMASK_AT(mask_size, mask_offset) (BITMASK(mask_size) << (mask_offset))
23 
24 /*! Definition for getting bits value from a word. */
25 #define BITFIELD_GET(word, bit_offset, bit_size) \
26     (((word) >> (bit_offset)) & BITMASK(bit_size))
27 /*! Definition for setting bits value from a word. */
28 #define BITFIELD_SET(word, bit_offset, bit_size, new_val)   do {    \
29     word = ((word) & ~BITMASK_AT(bit_size, bit_offset)) |       \
30         (((new_val) & BITMASK(bit_size)) << (bit_offset));  \
31 } while (0)
32 
33 /*!Definition for is val aligned to "align" ("align" must be power of 2). */
34 #ifndef IS_ALIGNED
35 #define IS_ALIGNED(val, align)      \
36     (((uintptr_t)(val) & ((align) - 1)) == 0)
37 #endif
38 /*!Definition swap endianity for 32 bits word. */
39 #define SWAP_ENDIAN(word)       \
40     (((word) >> 24) | (((word) & 0x00FF0000) >> 8) | \
41     (((word) & 0x0000FF00) << 8) | (((word) & 0x000000FF) << 24))
42 
43 #ifdef BIG__ENDIAN
44 #define SWAP_TO_LE(word) SWAP_ENDIAN(word)
45 #define SWAP_TO_BE(word) word
46 #else
47 /*! Definition for swapping to LE. */
48 #define SWAP_TO_LE(word) word
49 /*! Definition for swapping to BE. */
50 #define SWAP_TO_BE(word) SWAP_ENDIAN(word)
51 #endif
52 
53 /*!Align X to uint32_t size. */
54 #ifndef ALIGN_TO_4BYTES
55 #define ALIGN_TO_4BYTES(x)      (((unsigned long)(x) + (CC_32BIT_WORD_SIZE-1)) & ~(CC_32BIT_WORD_SIZE-1))
56 #endif
57 
58 
59 
60 /*! Definition for is val a multiple of "mult" ("mult" must be power of 2). */
61 #define IS_MULT(val, mult)              \
62     (((val) & ((mult) - 1)) == 0)
63 
64 /*! Definition for is NULL address. */
65 #define IS_NULL_ADDR(adr)       \
66     (!(adr))
67 
68 #endif /*_CC_BITOPS_H_*/
69