Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / intel / isl / isl_priv.h
1 /*
2 * Copyright 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include <assert.h>
27
28 #include "brw_device_info.h"
29 #include "util/macros.h"
30
31 #include "isl.h"
32
33 #define isl_finishme(format, ...) \
34 __isl_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__)
35
36 void PRINTFLIKE(3, 4) UNUSED
37 __isl_finishme(const char *file, int line, const char *fmt, ...);
38
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 #define MAX(a, b) ((a) > (b) ? (a) : (b))
41
42 static inline uint32_t
43 ffs(uint32_t n) {
44 return __builtin_ffs(n);
45 }
46
47 static inline bool
48 isl_is_pow2(uintmax_t n)
49 {
50 return !(n & (n - 1));
51 }
52
53 /**
54 * Alignment must be a power of 2.
55 */
56 static inline bool
57 isl_is_aligned(uintmax_t n, uintmax_t a)
58 {
59 assert(isl_is_pow2(a));
60 return (n & (a - 1)) == 0;
61 }
62
63 /**
64 * Alignment must be a power of 2.
65 */
66 static inline uintmax_t
67 isl_align(uintmax_t n, uintmax_t a)
68 {
69 assert(a != 0 && isl_is_pow2(a));
70 return (n + a - 1) & ~(a - 1);
71 }
72
73 static inline uintmax_t
74 isl_align_npot(uintmax_t n, uintmax_t a)
75 {
76 assert(a > 0);
77 return ((n + a - 1) / a) * a;
78 }
79
80 /**
81 * Alignment must be a power of 2.
82 */
83 static inline uintmax_t
84 isl_align_div(uintmax_t n, uintmax_t a)
85 {
86 return isl_align(n, a) / a;
87 }
88
89 static inline uintmax_t
90 isl_align_div_npot(uintmax_t n, uintmax_t a)
91 {
92 return isl_align_npot(n, a) / a;
93 }
94
95 /**
96 * Log base 2, rounding towards zero.
97 */
98 static inline uint32_t
99 isl_log2u(uint32_t n)
100 {
101 assert(n != 0);
102 return 31 - __builtin_clz(n);
103 }
104
105 static inline uint32_t
106 isl_minify(uint32_t n, uint32_t levels)
107 {
108 if (unlikely(n == 0))
109 return 0;
110 else
111 return MAX(n >> levels, 1);
112 }
113
114 static inline struct isl_extent3d
115 isl_extent3d_sa_to_el(enum isl_format fmt, struct isl_extent3d extent_sa)
116 {
117 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
118
119 assert(extent_sa.w % fmtl->bw == 0);
120 assert(extent_sa.h % fmtl->bh == 0);
121 assert(extent_sa.d % fmtl->bd == 0);
122
123 return (struct isl_extent3d) {
124 .w = extent_sa.w / fmtl->bw,
125 .h = extent_sa.h / fmtl->bh,
126 .d = extent_sa.d / fmtl->bd,
127 };
128 }
129
130 static inline struct isl_extent3d
131 isl_extent3d_el_to_sa(enum isl_format fmt, struct isl_extent3d extent_el)
132 {
133 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
134
135 return (struct isl_extent3d) {
136 .w = extent_el.w * fmtl->bw,
137 .h = extent_el.h * fmtl->bh,
138 .d = extent_el.d * fmtl->bd,
139 };
140 }