summaryrefslogtreecommitdiff
path: root/src/amd/vulkan/radix_sort/common/macros.h
blob: 475d4e426c1b165136bc4a9a2dda8c81d3a718e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SRC_GRAPHICS_LIB_COMPUTE_COMMON_MACROS_H_
#define SRC_GRAPHICS_LIB_COMPUTE_COMMON_MACROS_H_

//
//
//

#include <assert.h>
#include <stddef.h>
#include <stdint.h>

//
// clang-format off
//

#define ARRAY_LENGTH_MACRO(x_)          (sizeof(x_)/sizeof(x_[0]))
#define OFFSETOF_MACRO(t_,m_)           offsetof(t_,m_)
#define MEMBER_SIZE_MACRO(t_,m_)        sizeof(((t_*)0)->m_)

//
// FIXME(allanmac):
//
// Consider providing typed min/max() functions:
//
//   <type> [min|max]_<type>(a,b) { ; }
//
// But note we still need preprocessor-time min/max().
//

#define MAX_MACRO(t_,a_,b_)             (((a_) > (b_)) ? (a_) : (b_))
#define MIN_MACRO(t_,a_,b_)             (((a_) < (b_)) ? (a_) : (b_))

//
//
//

#define BITS_TO_MASK_MACRO(n_)          (((uint32_t)1<<(n_))-1)
#define BITS_TO_MASK_64_MACRO(n_)       (((uint64_t)1<<(n_))-1)

#define BITS_TO_MASK_AT_MACRO(n_,b_)    (BITS_TO_MASK_MACRO(n_)   <<(b_))
#define BITS_TO_MASK_AT_64_MACRO(n_,b_) (BITS_TO_MASK_64_MACRO(n_)<<(b_))

//
//
//

#define STRINGIFY_MACRO_2(a_)           #a_
#define STRINGIFY_MACRO(a_)             STRINGIFY_MACRO_2(a_)

//
//
//

#define CONCAT_MACRO_2(a_,b_)           a_ ## b_
#define CONCAT_MACRO(a_,b_)             CONCAT_MACRO_2(a_,b_)

//
// Round up/down
//

#define ROUND_DOWN_MACRO(v_,q_)         (((v_) / (q_)) * (q_))
#define ROUND_UP_MACRO(v_,q_)           ((((v_) + (q_) - 1) / (q_)) * (q_))

//
// Round up/down when q is a power-of-two.
//

#define ROUND_DOWN_POW2_MACRO(v_,q_)    ((v_) & ~((q_) - 1))
#define ROUND_UP_POW2_MACRO(v_,q_)      ROUND_DOWN_POW2_MACRO((v_) + (q_) - 1, q_)

//
//
//

#if defined (_MSC_VER) && !defined (__clang__)
#define STATIC_ASSERT_MACRO(c_,m_)      static_assert(c_,m_)
#else
#define STATIC_ASSERT_MACRO(c_,m_)      _Static_assert(c_,m_)
#endif

#define STATIC_ASSERT_MACRO_1(c_)       STATIC_ASSERT_MACRO(c_,#c_)

//
//
//

#if defined (_MSC_VER) && !defined (__clang__)
#define POPCOUNT_MACRO(...)             __popcnt(__VA_ARGS__)
#else
#define POPCOUNT_MACRO(...)             __builtin_popcount(__VA_ARGS__)
#endif

//
//
//

#if defined (_MSC_VER) && !defined (__clang__)
#define ALIGN_MACRO(bytes_)             __declspec(align(bytes_)) // only accepts integer as arg
#else
#include <stdalign.h>
#define ALIGN_MACRO(bytes_)             alignas(bytes_)
#endif

//
// clang-format on
//

#endif  // SRC_GRAPHICS_LIB_COMPUTE_COMMON_MACROS_H_