1 // Copyright 2019 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef SRC_GRAPHICS_LIB_COMPUTE_COMMON_MACROS_H_ 6 #define SRC_GRAPHICS_LIB_COMPUTE_COMMON_MACROS_H_ 7 8 // 9 // 10 // 11 12 #include <assert.h> 13 #include <stddef.h> 14 #include <stdint.h> 15 16 // 17 // clang-format off 18 // 19 20 #define ARRAY_LENGTH_MACRO(x_) (sizeof(x_)/sizeof(x_[0])) 21 #define OFFSETOF_MACRO(t_,m_) offsetof(t_,m_) 22 #define MEMBER_SIZE_MACRO(t_,m_) sizeof(((t_*)0)->m_) 23 24 // 25 // FIXME(allanmac): 26 // 27 // Consider providing typed min/max() functions: 28 // 29 // <type> [min|max]_<type>(a,b) { ; } 30 // 31 // But note we still need preprocessor-time min/max(). 32 // 33 34 #define MAX_MACRO(t_,a_,b_) (((a_) > (b_)) ? (a_) : (b_)) 35 #define MIN_MACRO(t_,a_,b_) (((a_) < (b_)) ? (a_) : (b_)) 36 37 // 38 // 39 // 40 41 #define BITS_TO_MASK_MACRO(n_) (((uint32_t)1<<(n_))-1) 42 #define BITS_TO_MASK_64_MACRO(n_) (((uint64_t)1<<(n_))-1) 43 44 #define BITS_TO_MASK_AT_MACRO(n_,b_) (BITS_TO_MASK_MACRO(n_) <<(b_)) 45 #define BITS_TO_MASK_AT_64_MACRO(n_,b_) (BITS_TO_MASK_64_MACRO(n_)<<(b_)) 46 47 // 48 // 49 // 50 51 #define STRINGIFY_MACRO_2(a_) #a_ 52 #define STRINGIFY_MACRO(a_) STRINGIFY_MACRO_2(a_) 53 54 // 55 // 56 // 57 58 #define CONCAT_MACRO_2(a_,b_) a_ ## b_ 59 #define CONCAT_MACRO(a_,b_) CONCAT_MACRO_2(a_,b_) 60 61 // 62 // Round up/down 63 // 64 65 #define ROUND_DOWN_MACRO(v_,q_) (((v_) / (q_)) * (q_)) 66 #define ROUND_UP_MACRO(v_,q_) ((((v_) + (q_) - 1) / (q_)) * (q_)) 67 68 // 69 // Round up/down when q is a power-of-two. 70 // 71 72 #define ROUND_DOWN_POW2_MACRO(v_,q_) ((v_) & ~((q_) - 1)) 73 #define ROUND_UP_POW2_MACRO(v_,q_) ROUND_DOWN_POW2_MACRO((v_) + (q_) - 1, q_) 74 75 // 76 // 77 // 78 79 #if defined (_MSC_VER) && !defined (__clang__) 80 #define STATIC_ASSERT_MACRO(c_,m_) static_assert(c_,m_) 81 #else 82 #define STATIC_ASSERT_MACRO(c_,m_) _Static_assert(c_,m_) 83 #endif 84 85 #define STATIC_ASSERT_MACRO_1(c_) STATIC_ASSERT_MACRO(c_,#c_) 86 87 // 88 // 89 // 90 91 #if defined (_MSC_VER) && !defined (__clang__) 92 #define POPCOUNT_MACRO(...) __popcnt(__VA_ARGS__) 93 #else 94 #define POPCOUNT_MACRO(...) __builtin_popcount(__VA_ARGS__) 95 #endif 96 97 // 98 // 99 // 100 101 #if defined (_MSC_VER) && !defined (__clang__) 102 #define ALIGN_MACRO(bytes_) __declspec(align(bytes_)) // only accepts integer as arg 103 #else 104 #include <stdalign.h> 105 #define ALIGN_MACRO(bytes_) alignas(bytes_) 106 #endif 107 108 // 109 // clang-format on 110 // 111 112 #endif // SRC_GRAPHICS_LIB_COMPUTE_COMMON_MACROS_H_ 113